From ae42c65cfde8539da2ccdf7b482f24a83efe829c Mon Sep 17 00:00:00 2001 From: tedyu Date: Sat, 21 May 2016 21:15:10 -0700 Subject: [PATCH] HBASE-15802 ConnectionUtils should use ThreadLocalRandom instead of Random (Matt Warhaftig) --- .../hadoop/hbase/client/ConnectionUtils.java | 10 +++++----- .../hbase/client/TestConnectionUtils.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java index 363a0e0bbbb..fbb15d04bd6 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java @@ -18,8 +18,8 @@ package org.apache.hadoop.hbase.client; import java.io.IOException; -import java.util.Random; import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.logging.Log; import org.apache.hadoop.conf.Configuration; @@ -42,7 +42,6 @@ public final class ConnectionUtils { private ConnectionUtils() {} - private static final Random RANDOM = new Random(); /** * Calculate pause time. * Built on {@link HConstants#RETRY_BACKOFF}. @@ -60,18 +59,19 @@ public final class ConnectionUtils { } long normalPause = pause * HConstants.RETRY_BACKOFF[ntries]; - long jitter = (long)(normalPause * RANDOM.nextFloat() * 0.01f); // 1% possible jitter + // 1% possible jitter + long jitter = (long) (normalPause * ThreadLocalRandom.current().nextFloat() * 0.01f); return normalPause + jitter; } /** - * Adds / subs a 10% jitter to a pause time. Minimum is 1. + * Adds / subs an up to 50% jitter to a pause time. Minimum is 1. * @param pause the expected pause. * @param jitter the jitter ratio, between 0 and 1, exclusive. */ public static long addJitter(final long pause, final float jitter) { - float lag = pause * (RANDOM.nextFloat() - 0.5f) * jitter; + float lag = pause * (ThreadLocalRandom.current().nextFloat() - 0.5f) * jitter; long newPause = pause + (long) lag; if (newPause <= 0) { return 1; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionUtils.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionUtils.java index aea8a2b59a4..69729f04cea 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionUtils.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionUtils.java @@ -55,6 +55,26 @@ public class TestConnectionUtils { assertTrue(retyTimeSet.size() > (retries.length * 0.80)); } + @Test + public void testAddJitter() { + long basePause = 10000; + long maxTimeExpected = (long) (basePause * 1.25f); + long minTimeExpected = (long) (basePause * 0.75f); + int testTries = 100; + + Set timeSet = new TreeSet(); + for (int i = 0; i < testTries; i++) { + long withJitter = ConnectionUtils.addJitter(basePause, 0.5f); + assertTrue(withJitter >= minTimeExpected); + assertTrue(withJitter <= maxTimeExpected); + // Add the long to the set + timeSet.add(withJitter); + } + + //Make sure that most are unique. some overlap will happen + assertTrue(timeSet.size() > (testTries * 0.90)); + } + @Test public void testGetPauseTime() { long pauseTime;