From 8dbe8acf49304f25a29fb696a46739034dbd9eba Mon Sep 17 00:00:00 2001 From: zhangduo Date: Wed, 8 Nov 2017 10:47:03 +0800 Subject: [PATCH] HBASE-19200 Let ZooKeeperRegistry depend on ZKAsyncRegistry to get data from zookeeper --- .../hadoop/hbase/client/AsyncRegistryFactory.java | 2 +- .../hbase/client/ConnectionImplementation.java | 125 ++++++--------------- .../org/apache/hadoop/hbase/client/HBaseAdmin.java | 61 ++-------- .../org/apache/hadoop/hbase/client/Registry.java | 29 +++-- .../hadoop/hbase/client/RegistryFactory.java | 24 ++-- .../hadoop/hbase/client/ZKAsyncRegistry.java | 11 +- .../hadoop/hbase/client/ZooKeeperRegistry.java | 117 ++++++++----------- .../hadoop/hbase/client/TestAsyncProcess.java | 17 ++- .../hadoop/hbase/client/TestBufferedMutator.java | 23 ++-- .../hadoop/hbase/client/TestClientNoCluster.java | 18 ++- .../org/apache/hadoop/hbase/GenericTestUtils.java | 1 + 11 files changed, 167 insertions(+), 261 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncRegistryFactory.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncRegistryFactory.java index 28726ae..f3acc7f 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncRegistryFactory.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncRegistryFactory.java @@ -27,7 +27,7 @@ import org.apache.hadoop.hbase.util.ReflectionUtils; @InterfaceAudience.Private final class AsyncRegistryFactory { - static final String REGISTRY_IMPL_CONF_KEY = "hbase.client.registry.impl"; + static final String REGISTRY_IMPL_CONF_KEY = "hbase.client.registry.async.impl"; private AsyncRegistryFactory() { } 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 56a2e84..19723a3 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 @@ -72,8 +72,6 @@ import org.apache.hadoop.hbase.util.ExceptionUtil; import org.apache.hadoop.hbase.util.Pair; import org.apache.hadoop.hbase.util.ReflectionUtils; import org.apache.hadoop.hbase.util.Threads; -import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker; -import org.apache.hadoop.hbase.zookeeper.ZKUtil; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; import org.apache.hadoop.ipc.RemoteException; import org.apache.yetus.audience.InterfaceAudience; @@ -206,7 +204,7 @@ class ConnectionImplementation implements ClusterConnection, Closeable { /** * Cluster registry of basic info such as clusterid and meta region location. */ - Registry registry; + private final Registry registry; private final ClientBackoffPolicy backoffPolicy; @@ -498,7 +496,7 @@ class ConnectionImplementation implements ClusterConnection, Closeable { * @return The cluster registry implementation to use. */ private Registry setupRegistry() throws IOException { - return RegistryFactory.getRegistry(this); + return RegistryFactory.getRegistry(conf); } /** @@ -535,25 +533,6 @@ class ConnectionImplementation implements ClusterConnection, Closeable { return this.conf; } - private void checkIfBaseNodeAvailable(ZooKeeperWatcher zkw) - throws MasterNotRunningException { - String errorMsg; - try { - if (ZKUtil.checkExists(zkw, zkw.znodePaths.baseZNode) == -1) { - errorMsg = "The node " + zkw.znodePaths.baseZNode+" is not in ZooKeeper. " - + "It should have been written by the master. " - + "Check the value configured in 'zookeeper.znode.parent'. " - + "There could be a mismatch with the one configured in the master."; - LOG.error(errorMsg); - throw new MasterNotRunningException(errorMsg); - } - } catch (KeeperException e) { - errorMsg = "Can't get connection to ZooKeeper: " + e.getMessage(); - LOG.error(errorMsg); - throw new MasterNotRunningException(errorMsg, e); - } - } - /** * @return true if the master is running, throws an exception otherwise * @throws org.apache.hadoop.hbase.MasterNotRunningException - if the master is not running @@ -1124,37 +1103,25 @@ class ConnectionImplementation implements ClusterConnection, Closeable { */ private MasterProtos.MasterService.BlockingInterface makeStubNoRetries() throws IOException, KeeperException { - ZooKeeperKeepAliveConnection zkw; - try { - zkw = getKeepAliveZooKeeperWatcher(); - } catch (IOException e) { - ExceptionUtil.rethrowIfInterrupt(e); - throw new ZooKeeperConnectionException("Can't connect to ZooKeeper", e); - } - try { - checkIfBaseNodeAvailable(zkw); - ServerName sn = MasterAddressTracker.getMasterAddress(zkw); - if (sn == null) { - String msg = "ZooKeeper available but no active master location found"; - LOG.info(msg); - throw new MasterNotRunningException(msg); - } - if (isDeadServer(sn)) { - throw new MasterNotRunningException(sn + " is dead."); - } - // Use the security info interface name as our stub key - String key = getStubKey(MasterProtos.MasterService.getDescriptor().getName(), sn, - hostnamesCanChange); - MasterProtos.MasterService.BlockingInterface stub = - (MasterProtos.MasterService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> { - BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sn, user, rpcTimeout); - return MasterProtos.MasterService.newBlockingStub(channel); - }); - isMasterRunning(stub); - return stub; - } finally { - zkw.close(); - } + ServerName sn = registry.getMasterAddress(); + if (sn == null) { + String msg = "ZooKeeper available but no active master location found"; + LOG.info(msg); + throw new MasterNotRunningException(msg); + } + if (isDeadServer(sn)) { + throw new MasterNotRunningException(sn + " is dead."); + } + // Use the security info interface name as our stub key + String key = + getStubKey(MasterProtos.MasterService.getDescriptor().getName(), sn, hostnamesCanChange); + MasterProtos.MasterService.BlockingInterface stub = + (MasterProtos.MasterService.BlockingInterface) computeIfAbsentEx(stubs, key, () -> { + BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sn, user, rpcTimeout); + return MasterProtos.MasterService.newBlockingStub(channel); + }); + isMasterRunning(stub); + return stub; } /** @@ -1215,26 +1182,6 @@ class ConnectionImplementation implements ClusterConnection, Closeable { private ZooKeeperKeepAliveConnection keepAliveZookeeper; private AtomicInteger keepAliveZookeeperUserCount = new AtomicInteger(0); - /** - * Retrieve a shared ZooKeeperWatcher. You must close it it once you've have finished with it. - * @return The shared instance. Never returns null. - */ - ZooKeeperKeepAliveConnection getKeepAliveZooKeeperWatcher() - throws IOException { - synchronized (masterAndZKLock) { - if (keepAliveZookeeper == null) { - if (this.closed) { - throw new IOException(toString() + " closed"); - } - // We don't check that our link to ZooKeeper is still valid - // But there is a retry mechanism in the ZooKeeperWatcher itself - keepAliveZookeeper = new ZooKeeperKeepAliveConnection(conf, this.toString(), this); - } - keepAliveZookeeperUserCount.addAndGet(1); - return keepAliveZookeeper; - } - } - void releaseZooKeeperWatcher(final ZooKeeperWatcher zkw) { if (zkw == null){ return; @@ -1947,26 +1894,14 @@ class ConnectionImplementation implements ClusterConnection, Closeable { @Override public void abort(final String msg, Throwable t) { - if (t instanceof KeeperException.SessionExpiredException - && keepAliveZookeeper != null) { - synchronized (masterAndZKLock) { - if (keepAliveZookeeper != null) { - LOG.warn("This client just lost it's session with ZooKeeper," + - " closing it." + - " It will be recreated next time someone needs it", t); - closeZooKeeperWatcher(); - } - } + if (t != null) { + LOG.fatal(msg, t); } else { - if (t != null) { - LOG.fatal(msg, t); - } else { - LOG.fatal(msg); - } - this.aborted = true; - close(); - this.closed = true; + LOG.fatal(msg); } + this.aborted = true; + close(); + this.closed = true; } @Override @@ -1995,7 +1930,7 @@ class ConnectionImplementation implements ClusterConnection, Closeable { this.metrics.shutdown(); } this.closed = true; - closeZooKeeperWatcher(); + registry.close(); this.stubs.clear(); if (clusterStatusListener != null) { clusterStatusListener.close(); @@ -2061,4 +1996,8 @@ class ConnectionImplementation implements ClusterConnection, Closeable { public RpcControllerFactory getRpcControllerFactory() { return this.rpcControllerFactory; } + + Registry getRegistry() { + return registry; + } } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java index 556e564..619d0b4 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java @@ -18,6 +18,10 @@ */ package org.apache.hadoop.hbase.client; +import com.google.protobuf.Descriptors; +import com.google.protobuf.Message; +import com.google.protobuf.RpcController; + import java.io.Closeable; import java.io.IOException; import java.io.InterruptedIOException; @@ -95,14 +99,12 @@ import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; import org.apache.hadoop.hbase.util.ForeignExceptionUtil; import org.apache.hadoop.hbase.util.Pair; -import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker; import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.util.StringUtils; import org.apache.yetus.audience.InterfaceAudience; import org.apache.yetus.audience.InterfaceStability; -import org.apache.zookeeper.KeeperException; import org.apache.hadoop.hbase.shaded.com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.hbase.shaded.com.google.protobuf.ServiceException; @@ -203,10 +205,6 @@ import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos; import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos.GetReplicationPeerConfigResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos; -import com.google.protobuf.Descriptors; -import com.google.protobuf.Message; -import com.google.protobuf.RpcController; - /** * HBaseAdmin is no longer a client API. It is marked InterfaceAudience.Private indicating that * this is an HBase-internal class as defined in @@ -2298,32 +2296,14 @@ public class HBaseAdmin implements Admin { } /** - * Check to see if HBase is running. Throw an exception if not. - * @param conf system configuration - * @throws MasterNotRunningException if the master is not running - * @throws ZooKeeperConnectionException if unable to connect to zookeeper - * @deprecated since hbase-2.0.0 because throws a ServiceException. We don't want to have - * protobuf as part of our public API. Use {@link #available(Configuration)} - */ - // Used by tests and by the Merge tool. Merge tool uses it to figure if HBase is up or not. - // MOB uses it too. - // NOTE: hbase-2.0.0 removes ServiceException from the throw. - @Deprecated - public static void checkHBaseAvailable(Configuration conf) - throws MasterNotRunningException, ZooKeeperConnectionException, IOException, - com.google.protobuf.ServiceException { - available(conf); - } - - /** * Is HBase available? Throw an exception if not. * @param conf system configuration * @throws MasterNotRunningException if the master is not running. - * @throws ZooKeeperConnectionException if unable to connect to zookeeper. - * // TODO do not expose ZKConnectionException. + * @throws ZooKeeperConnectionException if unable to connect to zookeeper. // TODO do not expose + * ZKConnectionException. */ public static void available(final Configuration conf) - throws MasterNotRunningException, ZooKeeperConnectionException, IOException { + throws MasterNotRunningException, ZooKeeperConnectionException, IOException { Configuration copyOfConf = HBaseConfiguration.create(conf); // We set it to make it fail as soon as possible if HBase is not available copyOfConf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); @@ -2333,26 +2313,6 @@ public class HBaseAdmin implements Admin { // If the connection exists, we may have a connection to ZK that does not work anymore try (ClusterConnection connection = (ClusterConnection) ConnectionFactory.createConnection(copyOfConf)) { - // Check ZK first. - // If the connection exists, we may have a connection to ZK that does not work anymore - ZooKeeperKeepAliveConnection zkw = null; - try { - // This is NASTY. FIX!!!! Dependent on internal implementation! TODO - zkw = ((ConnectionImplementation) connection) - .getKeepAliveZooKeeperWatcher(); - zkw.getRecoverableZooKeeper().getZooKeeper().exists(zkw.znodePaths.baseZNode, false); - } catch (IOException e) { - throw new ZooKeeperConnectionException("Can't connect to ZooKeeper", e); - } catch (InterruptedException e) { - throw (InterruptedIOException) - new InterruptedIOException("Can't connect to ZooKeeper").initCause(e); - } catch (KeeperException e){ - throw new ZooKeeperConnectionException("Can't connect to ZooKeeper", e); - } finally { - if (zkw != null) { - zkw.close(); - } - } // can throw MasterNotRunningException connection.isMasterRunning(); } @@ -3187,12 +3147,7 @@ public class HBaseAdmin implements Admin { private ServerName getMasterAddress() throws IOException { // TODO: Fix! Reaching into internal implementation!!!! ConnectionImplementation connection = (ConnectionImplementation)this.connection; - ZooKeeperKeepAliveConnection zkw = connection.getKeepAliveZooKeeperWatcher(); - try { - return MasterAddressTracker.getMasterAddress(zkw); - } catch (KeeperException e) { - throw new IOException("Failed to get master server name from MasterAddressTracker", e); - } + return connection.getRegistry().getMasterAddress(); } @Override diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Registry.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Registry.java index 4d0527a..a657d59 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Registry.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Registry.java @@ -17,22 +17,19 @@ */ package org.apache.hadoop.hbase.client; +import java.io.Closeable; import java.io.IOException; import org.apache.hadoop.hbase.RegionLocations; +import org.apache.hadoop.hbase.ServerName; import org.apache.yetus.audience.InterfaceAudience; /** - * Cluster registry. - * Implementations hold cluster information such as this cluster's id, location of hbase:meta, etc. - * Internal use only. + * Cluster registry. Implementations hold cluster information such as this cluster's id, location of + * hbase:meta, etc. Internal use only. */ @InterfaceAudience.Private -interface Registry { - /** - * @param connection - */ - void init(Connection connection); +interface Registry extends Closeable { /** * @return Meta region location @@ -50,4 +47,20 @@ interface Registry { * @throws IOException */ int getCurrentNrHRS() throws IOException; + + /** + * Get the address of HMaster. + */ + ServerName getMasterAddress() throws IOException; + + /** + * Get the info port of HMaster. + */ + Integer getMasterInfoPort() throws IOException; + + /** + * Closes this instance and releases any system resources associated with it + */ + @Override + void close(); } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegistryFactory.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegistryFactory.java index 7b2ac0b..da18095 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegistryFactory.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegistryFactory.java @@ -17,8 +17,8 @@ */ package org.apache.hadoop.hbase.client; -import java.io.IOException; - +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.util.ReflectionUtils; import org.apache.yetus.audience.InterfaceAudience; /** @@ -28,23 +28,15 @@ import org.apache.yetus.audience.InterfaceAudience; final class RegistryFactory { static final String REGISTRY_IMPL_CONF_KEY = "hbase.client.registry.impl"; - private RegistryFactory() {} + private RegistryFactory() { + } /** * @return The cluster registry implementation to use. - * @throws IOException */ - static Registry getRegistry(final Connection connection) - throws IOException { - String registryClass = connection.getConfiguration().get(REGISTRY_IMPL_CONF_KEY, - ZooKeeperRegistry.class.getName()); - Registry registry = null; - try { - registry = (Registry)Class.forName(registryClass).newInstance(); - } catch (Throwable t) { - throw new IOException(t); - } - registry.init(connection); - return registry; + static Registry getRegistry(Configuration conf) { + Class clazz = + conf.getClass(REGISTRY_IMPL_CONF_KEY, ZooKeeperRegistry.class, Registry.class); + return ReflectionUtils.newInstance(clazz, conf); } } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZKAsyncRegistry.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZKAsyncRegistry.java index 259b665..91ca28b 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZKAsyncRegistry.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZKAsyncRegistry.java @@ -19,8 +19,8 @@ package org.apache.hadoop.hbase.client; import static org.apache.hadoop.hbase.HConstants.DEFAULT_ZK_SESSION_TIMEOUT; import static org.apache.hadoop.hbase.HConstants.ZK_SESSION_TIMEOUT; -import static org.apache.hadoop.hbase.HRegionInfo.DEFAULT_REPLICA_ID; -import static org.apache.hadoop.hbase.HRegionInfo.FIRST_META_REGIONINFO; +import static org.apache.hadoop.hbase.client.RegionInfo.DEFAULT_REPLICA_ID; +import static org.apache.hadoop.hbase.client.RegionInfoBuilder.FIRST_META_REGIONINFO; import static org.apache.hadoop.hbase.client.RegionReplicaUtil.getRegionInfoForDefaultReplica; import static org.apache.hadoop.hbase.client.RegionReplicaUtil.getRegionInfoForReplica; import static org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil.lengthOfPBMagic; @@ -42,17 +42,18 @@ import org.apache.hadoop.hbase.ClusterId; import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.RegionLocations; import org.apache.hadoop.hbase.ServerName; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.exceptions.DeserializationException; import org.apache.hadoop.hbase.master.RegionState; -import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; -import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos; import org.apache.hadoop.hbase.util.Pair; import org.apache.hadoop.hbase.util.Threads; import org.apache.hadoop.hbase.zookeeper.ZKConfig; import org.apache.hadoop.hbase.zookeeper.ZNodePaths; +import org.apache.yetus.audience.InterfaceAudience; import org.apache.zookeeper.data.Stat; +import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; +import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos; + /** * Fetch the registry data from zookeeper. */ diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZooKeeperRegistry.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZooKeeperRegistry.java index 746382f..1c7a3c9 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZooKeeperRegistry.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ZooKeeperRegistry.java @@ -17,113 +17,86 @@ */ package org.apache.hadoop.hbase.client; +import com.google.common.base.Throwables; + import java.io.IOException; -import java.util.List; +import java.io.InterruptedIOException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.RegionLocations; import org.apache.hadoop.hbase.ServerName; -import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; -import org.apache.hadoop.hbase.zookeeper.ZKClusterId; -import org.apache.hadoop.hbase.zookeeper.ZKUtil; import org.apache.yetus.audience.InterfaceAudience; -import org.apache.zookeeper.KeeperException; /** * A cluster registry that stores to zookeeper. */ @InterfaceAudience.Private class ZooKeeperRegistry implements Registry { + private static final Log LOG = LogFactory.getLog(ZooKeeperRegistry.class); - // Needs an instance of hci to function. Set after construct this instance. - ConnectionImplementation hci; - @Override - public void init(Connection connection) { - if (!(connection instanceof ConnectionImplementation)) { - throw new RuntimeException("This registry depends on ConnectionImplementation"); - } - this.hci = (ConnectionImplementation)connection; + private final ZKAsyncRegistry registry; + + ZooKeeperRegistry(Configuration conf) { + this.registry = new ZKAsyncRegistry(conf); } - @Override - public RegionLocations getMetaRegionLocation() throws IOException { - ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher(); + private static T get(CompletableFuture future) throws IOException { try { - if (LOG.isTraceEnabled()) { - LOG.trace("Looking up meta region location in ZK," + " connection=" + this); - } - List servers = new MetaTableLocator().blockUntilAvailable(zkw, hci.rpcTimeout, - hci.getConfiguration()); - if (LOG.isTraceEnabled()) { - if (servers == null) { - LOG.trace("Looked up meta region location, connection=" + this + - "; servers = null"); - } else { - StringBuilder str = new StringBuilder(); - for (ServerName s : servers) { - str.append(s.toString()); - str.append(" "); - } - LOG.trace("Looked up meta region location, connection=" + this + - "; servers = " + str.toString()); - } - } - if (servers == null) return null; - HRegionLocation[] locs = new HRegionLocation[servers.size()]; - int i = 0; - for (ServerName server : servers) { - RegionInfo h = RegionReplicaUtil.getRegionInfoForReplica( - RegionInfoBuilder.FIRST_META_REGIONINFO, i); - if (server == null) locs[i++] = null; - else locs[i++] = new HRegionLocation(h, server, 0); - } - return new RegionLocations(locs); + return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - return null; - } finally { - zkw.close(); + throw (IOException) new InterruptedIOException().initCause(e); + } catch (ExecutionException e) { + Throwable cause = e.getCause(); + Throwables.propagateIfPossible(cause, IOException.class); + throw new IOException(cause); } } + @Override + public RegionLocations getMetaRegionLocation() throws IOException { + return get(registry.getMetaRegionLocation()); + } + private String clusterId = null; @Override public String getClusterId() { - if (this.clusterId != null) return this.clusterId; + if (this.clusterId != null) { + return this.clusterId; + } // No synchronized here, worse case we will retrieve it twice, that's - // not an issue. - ZooKeeperKeepAliveConnection zkw = null; + // not an issue. try { - zkw = hci.getKeepAliveZooKeeperWatcher(); - this.clusterId = ZKClusterId.readClusterIdZNode(zkw); - if (this.clusterId == null) { - LOG.info("ClusterId read in ZooKeeper is null"); - } - } catch (KeeperException e) { + this.clusterId = registry.getClusterId().get(); + } catch (Exception e) { LOG.warn("Can't retrieve clusterId from ZooKeeper", e); - } catch (IOException e) { - LOG.warn("Can't retrieve clusterId from ZooKeeper", e); - } finally { - if (zkw != null) zkw.close(); } return this.clusterId; } @Override public int getCurrentNrHRS() throws IOException { - ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher(); - try { - // We go to zk rather than to master to get count of regions to avoid - // HTable having a Master dependency. See HBase-2828 - return ZKUtil.getNumberOfChildren(zkw, zkw.znodePaths.rsZNode); - } catch (KeeperException ke) { - throw new IOException("Unexpected ZooKeeper exception", ke); - } finally { - zkw.close(); - } + return get(registry.getCurrentNrHRS()); + } + + @Override + public ServerName getMasterAddress() throws IOException { + return get(registry.getMasterAddress()); + } + + @Override + public Integer getMasterInfoPort() throws IOException { + return get(registry.getMasterInfoPort()); + } + + @Override + public void close() { + registry.close(); } } diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java index a0f18f4..63ffb68 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java @@ -457,8 +457,8 @@ public class TestAsyncProcess { */ static class MyConnectionImpl extends ConnectionImplementation { public static class TestRegistry implements Registry { - @Override - public void init(Connection connection) {} + + public TestRegistry(Configuration conf) {} @Override public RegionLocations getMetaRegionLocation() throws IOException { @@ -474,6 +474,19 @@ public class TestAsyncProcess { public int getCurrentNrHRS() throws IOException { return 1; } + + @Override + public ServerName getMasterAddress() throws IOException { + return null; + } + + @Override + public Integer getMasterInfoPort() throws IOException { + return null; + } + + @Override + public void close() {} } final AtomicInteger nbThreads = new AtomicInteger(0); diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestBufferedMutator.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestBufferedMutator.java index d1a3eb9..fa1edd7 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestBufferedMutator.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestBufferedMutator.java @@ -25,6 +25,7 @@ import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.RegionLocations; +import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ipc.RpcControllerFactory; import org.apache.hadoop.hbase.testclassification.ClientTests; @@ -44,28 +45,36 @@ public class TestBufferedMutator { * Otherwise, default Registry wants zookeeper up and running. */ public static class DoNothingRegistry implements Registry { - @Override - public void init(Connection connection) { - // TODO Auto-generated method stub - } + + public DoNothingRegistry(Configuration conf) {} @Override public RegionLocations getMetaRegionLocation() throws IOException { - // TODO Auto-generated method stub return null; } @Override public String getClusterId() { - // TODO Auto-generated method stub return null; } @Override public int getCurrentNrHRS() throws IOException { - // TODO Auto-generated method stub return 0; } + + @Override + public ServerName getMasterAddress() throws IOException { + return null; + } + + @Override + public Integer getMasterInfoPort() throws IOException { + return null; + } + + @Override + public void close() {} } /** diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestClientNoCluster.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestClientNoCluster.java index 0f11156..fd331b0 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestClientNoCluster.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestClientNoCluster.java @@ -120,10 +120,7 @@ public class TestClientNoCluster extends Configured implements Tool { static class SimpleRegistry implements Registry { final ServerName META_HOST = META_SERVERNAME; - @Override - public void init(Connection connection) { - } - + public SimpleRegistry(Configuration conf) {} @Override public RegionLocations getMetaRegionLocation() throws IOException { return new RegionLocations( @@ -139,6 +136,19 @@ public class TestClientNoCluster extends Configured implements Tool { public int getCurrentNrHRS() throws IOException { return 1; } + + @Override + public ServerName getMasterAddress() throws IOException { + return null; + } + + @Override + public Integer getMasterInfoPort() throws IOException { + return null; + } + + @Override + public void close() {} } /** diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/GenericTestUtils.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/GenericTestUtils.java index 08565e0..56be657 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/GenericTestUtils.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/GenericTestUtils.java @@ -49,6 +49,7 @@ import org.apache.hadoop.hbase.shaded.com.google.common.collect.Sets; /** * Test provides some very generic helpers which might be used across the tests + * *** for running UTs in hbase-server */ public abstract class GenericTestUtils { -- 2.7.4