hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From te...@apache.org
Subject hbase git commit: HBASE-15802 ConnectionUtils should use ThreadLocalRandom instead of Random (Matt Warhaftig)
Date Sun, 22 May 2016 04:15:10 GMT
Repository: hbase
Updated Branches:
  refs/heads/master 56e4b85d0 -> ae42c65cf


HBASE-15802 ConnectionUtils should use ThreadLocalRandom instead of Random (Matt Warhaftig)


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/ae42c65c
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/ae42c65c
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/ae42c65c

Branch: refs/heads/master
Commit: ae42c65cfde8539da2ccdf7b482f24a83efe829c
Parents: 56e4b85
Author: tedyu <yuzhihong@gmail.com>
Authored: Sat May 21 21:15:10 2016 -0700
Committer: tedyu <yuzhihong@gmail.com>
Committed: Sat May 21 21:15:10 2016 -0700

----------------------------------------------------------------------
 .../hadoop/hbase/client/ConnectionUtils.java    | 10 +++++-----
 .../hbase/client/TestConnectionUtils.java       | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/ae42c65c/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionUtils.java
----------------------------------------------------------------------
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 363a0e0..fbb15d0 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;

http://git-wip-us.apache.org/repos/asf/hbase/blob/ae42c65c/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestConnectionUtils.java
----------------------------------------------------------------------
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 aea8a2b..69729f0 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
@@ -56,6 +56,26 @@ public class TestConnectionUtils {
   }
 
   @Test
+  public void testAddJitter() {
+    long basePause = 10000;
+    long maxTimeExpected = (long) (basePause * 1.25f);
+    long minTimeExpected = (long) (basePause * 0.75f);
+    int testTries = 100;
+
+    Set<Long> timeSet = new TreeSet<Long>();
+    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;
     long baseTime = 100;


Mime
View raw message