From 818b3375656fc5f94bb678afca610f099fcda58a Mon Sep 17 00:00:00 2001 From: meiyi Date: Fri, 28 Sep 2018 16:04:37 +0800 Subject: [PATCH] HBASE-21249 Add jitter for ProcedureUtil.getBackoffTimeMs Signed-off-by: zhangduo --- .../hadoop/hbase/procedure2/ProcedureUtil.java | 6 +++++- .../hbase/procedure2/TestProcedureUtil.java | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/hbase-procedure/src/main/java/org/apache/hadoop/hbase/procedure2/ProcedureUtil.java b/hbase-procedure/src/main/java/org/apache/hadoop/hbase/procedure2/ProcedureUtil.java index 8c8746e84bb..27802efecd5 100644 --- a/hbase-procedure/src/main/java/org/apache/hadoop/hbase/procedure2/ProcedureUtil.java +++ b/hbase-procedure/src/main/java/org/apache/hadoop/hbase/procedure2/ProcedureUtil.java @@ -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; } } diff --git a/hbase-procedure/src/test/java/org/apache/hadoop/hbase/procedure2/TestProcedureUtil.java b/hbase-procedure/src/test/java/org/apache/hadoop/hbase/procedure2/TestProcedureUtil.java index 6342beccae0..3629fb74bc6 100644 --- a/hbase-procedure/src/test/java/org/apache/hadoop/hbase/procedure2/TestProcedureUtil.java +++ b/hbase-procedure/src/test/java/org/apache/hadoop/hbase/procedure2/TestProcedureUtil.java @@ -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 {