From b63d6cc0afbc34d1f1660a18df7a6788039f1b26 Mon Sep 17 00:00:00 2001 From: chenheng Date: Mon, 25 Apr 2016 18:08:36 +0800 Subject: [PATCH] HBASE-15702 Improve PerClientRandomNonceGenerator --- .../hbase/client/ConnectionImplementation.java | 24 ++++++++++++++++------ .../client/PerClientRandomNonceGenerator.java | 19 +++++++++++++++-- .../hbase/client/CoprocessorHConnection.java | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java index 9a7dfc7..c50d41e 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java @@ -203,13 +203,9 @@ class ConnectionImplementation implements ClusterConnection, Closeable { HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT); if (conf.getBoolean(CLIENT_NONCES_ENABLED_KEY, true)) { - synchronized (nonceGeneratorCreateLock) { - if (nonceGenerator == null) { - nonceGenerator = new PerClientRandomNonceGenerator(); - } - } + nonceGenerator = PerClientRandomNonceGenerator.getInstance(); } else { - nonceGenerator = new NoNonceGenerator(); + nonceGenerator = NoNonceGenerator.getInstance(); } this.stats = ServerStatisticTracker.create(conf); @@ -1022,6 +1018,22 @@ class ConnectionImplementation implements ClusterConnection, Closeable { /** Dummy nonce generator for disabled nonces. */ static class NoNonceGenerator implements NonceGenerator { + + private static NoNonceGenerator noNonceGenerator = null; + + public static NoNonceGenerator getInstance() { + if (noNonceGenerator == null) { + synchronized (NoNonceGenerator.class) { + if (noNonceGenerator == null) { + noNonceGenerator = new NoNonceGenerator(); + } + } + } + return noNonceGenerator; + } + + private NoNonceGenerator() {} + @Override public long getNonceGroup() { return HConstants.NO_NONCE; diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/PerClientRandomNonceGenerator.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/PerClientRandomNonceGenerator.java index 875e1f6..e73d94c 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/PerClientRandomNonceGenerator.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/PerClientRandomNonceGenerator.java @@ -21,6 +21,8 @@ package org.apache.hadoop.hbase.client; import java.util.Arrays; import java.util.Random; +import com.google.common.annotations.VisibleForTesting; +import io.netty.util.internal.ThreadLocalRandom; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.classification.InterfaceAudience; @@ -30,10 +32,23 @@ import org.apache.hadoop.hbase.classification.InterfaceAudience; */ @InterfaceAudience.Private public class PerClientRandomNonceGenerator implements NonceGenerator { - private final Random rdm = new Random(); + private final ThreadLocalRandom rdm = ThreadLocalRandom.current(); private final long clientId; + private static PerClientRandomNonceGenerator clientRandomNonceGenerator = null; - public PerClientRandomNonceGenerator() { + public static PerClientRandomNonceGenerator getInstance() { + if (clientRandomNonceGenerator == null) { + synchronized (PerClientRandomNonceGenerator.class) { + if (clientRandomNonceGenerator == null) { + clientRandomNonceGenerator = new PerClientRandomNonceGenerator(); + } + } + } + return clientRandomNonceGenerator; + } + + @VisibleForTesting + protected PerClientRandomNonceGenerator() { byte[] clientIdBase = ClientIdGenerator.generateClientId(); this.clientId = (((long)Arrays.hashCode(clientIdBase)) << 32) + rdm.nextInt(); } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java index 285737d..81c3122 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java @@ -40,7 +40,7 @@ import org.apache.hadoop.hbase.security.UserProvider; @InterfaceAudience.Private @InterfaceStability.Evolving public class CoprocessorHConnection extends ConnectionImplementation { - private static final NonceGenerator NO_NONCE_GEN = new NoNonceGenerator(); + private static final NonceGenerator NO_NONCE_GEN = NoNonceGenerator.getInstance(); /** * Create an {@link HConnection} based on the environment in which we are running the -- 1.9.3 (Apple Git-50)