HBASE-21249 Add jitter for ProcedureUtil.getBackoffTimeMs

Signed-off-by: zhangduo <zhangduo@apache.org>
This commit is contained in:
meiyi 2018-09-28 16:04:37 +08:00 committed by zhangduo
parent 1109ebb617
commit 818b337565
2 changed files with 17 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.hadoop.hbase.HConstants;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
@ -343,6 +344,9 @@ public final class ProcedureUtil {
if (attempts >= 30) {
return maxBackoffTime;
}
return Math.min((long) (1000 * Math.pow(2, attempts)), maxBackoffTime);
long backoffTimeMs = Math.min((long) (1000 * Math.pow(2, attempts)), maxBackoffTime);
// 1% possible jitter
long jitter = (long) (backoffTimeMs * ThreadLocalRandom.current().nextFloat() * 0.01f);
return backoffTimeMs + jitter;
}
}

View File

@ -18,6 +18,7 @@
package org.apache.hadoop.hbase.procedure2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.apache.hadoop.hbase.HBaseClassTestRule;
@ -63,9 +64,17 @@ public class TestProcedureUtil {
for (int i = 30; i < 1000; i++) {
assertEquals(TimeUnit.MINUTES.toMillis(10), ProcedureUtil.getBackoffTimeMs(30));
}
assertEquals(1000, ProcedureUtil.getBackoffTimeMs(0));
assertEquals(2000, ProcedureUtil.getBackoffTimeMs(1));
assertEquals(32000, ProcedureUtil.getBackoffTimeMs(5));
long backoffTimeMs = ProcedureUtil.getBackoffTimeMs(0);
assertTrue(backoffTimeMs >= 1000);
assertTrue(backoffTimeMs <= 1000 * 1.01f);
backoffTimeMs = ProcedureUtil.getBackoffTimeMs(1);
assertTrue(backoffTimeMs >= 2000);
assertTrue(backoffTimeMs <= 2000 * 1.01f);
backoffTimeMs = ProcedureUtil.getBackoffTimeMs(5);
assertTrue(backoffTimeMs >= 32000);
assertTrue(backoffTimeMs <= 32000 * 1.01f);
}
public static class TestProcedureNoDefaultConstructor extends TestProcedure {