diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java index 4ae0286..8897a4a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java @@ -443,7 +443,7 @@ public class HRegionServer extends HasThread implements private RegionServerCoprocessorHost rsHost; private RegionServerProcedureManagerHost rspmHost; - + private RegionServerQuotaManager rsQuotaManager; // Table level lock manager for locking for region operations @@ -573,7 +573,18 @@ public class HRegionServer extends HasThread implements // Open connection to zookeeper and set primary watcher zooKeeper = new ZooKeeperWatcher(conf, getProcessName() + ":" + rpcServices.isa.getPort(), this, canCreateBaseZNode()); - + // Make sure we are connected before we proceed. Can take a while on some systems. If we + // run the command without being connected, we get ConnectionLoss KeeperErrorConnection... + long startTime = System.currentTimeMillis(); + while (!zooKeeper.getRecoverableZooKeeper().getState().isConnected()) { + Thread.sleep(1); + LOG.info("Trying"); + long elapsed = System.currentTimeMillis() - startTime; + if (elapsed > 30000) { + throw new InterruptedException("Failed connect after " + elapsed + "ms; " + zooKeeper); + } + } + LOG.info("Connected"); this.csm = (BaseCoordinatedStateManager) csm; this.csm.initialize(this); this.csm.start(); @@ -853,7 +864,7 @@ public class HRegionServer extends HasThread implements // Setup the Quota Manager rsQuotaManager = new RegionServerQuotaManager(this); - + // Setup RPC client for master communication rpcClient = RpcClientFactory.createClient(conf, clusterId, new InetSocketAddress( rpcServices.isa.getAddress(), 0), clusterConnection.getConnectionMetrics()); @@ -921,7 +932,7 @@ public class HRegionServer extends HasThread implements // since the server is ready to run rspmHost.start(); } - + // Start the Quota Manager if (this.rsQuotaManager != null) { rsQuotaManager.start(getRpcServer().getScheduler()); @@ -1015,7 +1026,7 @@ public class HRegionServer extends HasThread implements if (rsQuotaManager != null) { rsQuotaManager.stop(); } - + // Stop the snapshot and other procedure handlers, forcefully killing all running tasks if (rspmHost != null) { rspmHost.stop(this.abortRequested || this.killed); @@ -2628,7 +2639,7 @@ public class HRegionServer extends HasThread implements public ChoreService getChoreService() { return choreService; } - + @Override public RegionServerQuotaManager getRegionServerQuotaManager() { return rsQuotaManager; @@ -2751,7 +2762,7 @@ public class HRegionServer extends HasThread implements } return tableRegions; } - + /** * Gets the online tables in this RS. * This method looks at the in-memory onlineRegions. diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java index 44220f5..63ddaa0 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java @@ -1,4 +1,4 @@ -/** +/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -39,8 +39,11 @@ import org.apache.hadoop.hbase.testclassification.MiscTests; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; +import org.junit.rules.TestRule; /** * Test case for JMX Connector Server. @@ -48,14 +51,16 @@ import org.junit.experimental.categories.Category; @Category({ MiscTests.class, MediumTests.class }) public class TestJMXConnectorServer { private static final Log LOG = LogFactory.getLog(TestJMXConnectorServer.class); - private static HBaseTestingUtility UTIL = new HBaseTestingUtility(); + @Rule public final TestRule timeout = CategoryBasedTimeout.builder(). + withTimeout(this.getClass()).withLookingForStuckThread(true).build(); + @Rule public TestName name = new TestName(); + private static HBaseTestingUtility UTIL = null; private static Configuration conf = null; - private static Admin admin; // RMI registry port private static int rmiRegistryPort = 61120; // Switch for customized Accesscontroller to throw ACD exception while executing test case - static boolean hasAccess; + static boolean HAS_ACCESS; @Before public void setUp() throws Exception { @@ -66,26 +71,25 @@ public class TestJMXConnectorServer { @After public void tearDown() throws Exception { // Set to true while stopping cluster - hasAccess = true; - admin.close(); + HAS_ACCESS = true; UTIL.shutdownMiniCluster(); } /** * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call. */ - @Test(timeout = 180000) + @Test public void testHMConnectorServerWhenStopMaster() throws Exception { conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, JMXListener.class.getName() + "," + MyAccessController.class.getName()); conf.setInt("master.rmi.registry.port", rmiRegistryPort); UTIL.startMiniCluster(); - admin = UTIL.getConnection().getAdmin(); + Admin admin = UTIL.getConnection().getAdmin(); // try to stop master boolean accessDenied = false; try { - hasAccess = false; + HAS_ACCESS = false; LOG.info("Stopping HMaster..."); admin.stopMaster(); } catch (AccessDeniedException e) { @@ -112,15 +116,15 @@ public class TestJMXConnectorServer { * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer * call. */ - @Test(timeout = 180000) + @Test public void testRSConnectorServerWhenStopRegionServer() throws Exception { conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, JMXListener.class.getName() + "," + MyAccessController.class.getName()); conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort); UTIL.startMiniCluster(); - admin = UTIL.getConnection().getAdmin(); + Admin admin = UTIL.getConnection().getAdmin(); - hasAccess = false; + HAS_ACCESS = false; ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName(); LOG.info("Stopping Region Server..."); admin.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort()); @@ -142,18 +146,18 @@ public class TestJMXConnectorServer { /** * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call. */ - @Test(timeout = 180000) + @Test public void testHMConnectorServerWhenShutdownCluster() throws Exception { conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, JMXListener.class.getName() + "," + MyAccessController.class.getName()); conf.setInt("master.rmi.registry.port", rmiRegistryPort); UTIL.startMiniCluster(); - admin = UTIL.getConnection().getAdmin(); + Admin admin = UTIL.getConnection().getAdmin(); boolean accessDenied = false; try { - hasAccess = false; + HAS_ACCESS = false; LOG.info("Stopping HMaster..."); admin.shutdown(); } catch (AccessDeniedException e) { @@ -183,7 +187,7 @@ public class TestJMXConnectorServer { public static class MyAccessController extends AccessController { @Override public void preStopMaster(ObserverContext c) throws IOException { - if (!hasAccess) { + if (!HAS_ACCESS) { throw new AccessDeniedException("Insufficient permissions to stop master"); } } @@ -191,14 +195,14 @@ public class TestJMXConnectorServer { @Override public void preStopRegionServer(ObserverContext ctx) throws IOException { - if (!hasAccess) { + if (!HAS_ACCESS) { throw new AccessDeniedException("Insufficient permissions to stop region server."); } } @Override public void preShutdown(ObserverContext c) throws IOException { - if (!hasAccess) { + if (!HAS_ACCESS) { throw new AccessDeniedException("Insufficient permissions to shut down cluster."); } }