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

This commit is contained in:
tedyu 2016-05-21 21:15:10 -07:00
parent 56e4b85d06
commit ae42c65cfd
2 changed files with 25 additions and 5 deletions

View File

@ -18,8 +18,8 @@
package org.apache.hadoop.hbase.client; package org.apache.hadoop.hbase.client;
import java.io.IOException; import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -42,7 +42,6 @@ public final class ConnectionUtils {
private ConnectionUtils() {} private ConnectionUtils() {}
private static final Random RANDOM = new Random();
/** /**
* Calculate pause time. * Calculate pause time.
* Built on {@link HConstants#RETRY_BACKOFF}. * Built on {@link HConstants#RETRY_BACKOFF}.
@ -60,18 +59,19 @@ public final class ConnectionUtils {
} }
long normalPause = pause * HConstants.RETRY_BACKOFF[ntries]; 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; 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 pause the expected pause.
* @param jitter the jitter ratio, between 0 and 1, exclusive. * @param jitter the jitter ratio, between 0 and 1, exclusive.
*/ */
public static long addJitter(final long pause, final float jitter) { 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; long newPause = pause + (long) lag;
if (newPause <= 0) { if (newPause <= 0) {
return 1; return 1;

View File

@ -55,6 +55,26 @@ public class TestConnectionUtils {
assertTrue(retyTimeSet.size() > (retries.length * 0.80)); 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<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 @Test
public void testGetPauseTime() { public void testGetPauseTime() {
long pauseTime; long pauseTime;