Skip to content

Commit

Permalink
HBASE-24913 Refactor TestJMXConnectorServer (#2286)
Browse files Browse the repository at this point in the history
Signed-off-by: Guanghao Zhang <zghao@apache.org>
  • Loading branch information
ddupg authored Aug 31, 2020
1 parent bb64070 commit 25fcc40
Showing 1 changed file with 35 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -56,39 +58,47 @@ public class TestJMXConnectorServer {
private static Configuration conf = null;
private static Admin admin;
// RMI registry port
private static int rmiRegistryPort = 61120;
private static int rmiRegistryPort;
// Switch for customized Accesscontroller to throw ACD exception while executing test case
static boolean hasAccess;
private volatile static boolean hasAccess;

@Before
public void setUp() throws Exception {
UTIL = new HBaseTestingUtility();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
conf = UTIL.getConfiguration();
String cps = JMXListener.class.getName() + "," + MyAccessController.class.getName();
conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, cps);
conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, cps);
rmiRegistryPort = UTIL.randomFreePort();
conf.setInt("master.rmi.registry.port", rmiRegistryPort);
conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort);
UTIL.startMiniCluster();
admin = UTIL.getConnection().getAdmin();
}

@After
public void tearDown() throws Exception {
// Set to true while stopping cluster
hasAccess = true;
@AfterClass
public static void tearDownAfterClass() throws Exception {
admin.close();
UTIL.shutdownMiniCluster();
}

@Before
public void setUp() {
hasAccess = false;
}

@After
public void tearDown() {
hasAccess = true;
}

/**
* This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
*/
@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();

// try to stop master
boolean accessDenied = false;
try {
hasAccess = false;
LOG.info("Stopping HMaster...");
admin.stopMaster();
} catch (AccessDeniedException e) {
Expand All @@ -97,18 +107,7 @@ public void testHMConnectorServerWhenStopMaster() throws Exception {
}
Assert.assertTrue(accessDenied);

// Check whether HMaster JMX Connector server can be connected
JMXConnector connector = null;
try {
connector = JMXConnectorFactory
.connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
} catch (IOException e) {
if (e.getCause() instanceof ServiceUnavailableException) {
Assert.fail("Can't connect to HMaster ConnectorServer.");
}
}
Assert.assertNotNull("JMXConnector should not be null.", connector);
connector.close();
checkConnector();
}

/**
Expand All @@ -117,46 +116,20 @@ public void testHMConnectorServerWhenStopMaster() throws Exception {
*/
@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();

hasAccess = false;
ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
LOG.info("Stopping Region Server...");
admin.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());

// Check whether Region Sever JMX Connector server can be connected
JMXConnector connector = null;
try {
connector = JMXConnectorFactory
.connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
} catch (IOException e) {
if (e.getCause() instanceof ServiceUnavailableException) {
Assert.fail("Can't connect to Region Server ConnectorServer.");
}
}
Assert.assertNotNull("JMXConnector should not be null.", connector);
connector.close();
checkConnector();
}

/**
* This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
*/
@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();

boolean accessDenied = false;
try {
hasAccess = false;
LOG.info("Stopping HMaster...");
admin.shutdown();
} catch (AccessDeniedException e) {
Expand All @@ -165,14 +138,18 @@ public void testHMConnectorServerWhenShutdownCluster() throws Exception {
}
Assert.assertTrue(accessDenied);

checkConnector();
}

private void checkConnector() throws Exception {
// Check whether HMaster JMX Connector server can be connected
JMXConnector connector = null;
try {
connector = JMXConnectorFactory
.connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
.connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
} catch (IOException e) {
if (e.getCause() instanceof ServiceUnavailableException) {
Assert.fail("Can't connect to HMaster ConnectorServer.");
Assert.fail("Can't connect to ConnectorServer.");
}
}
Assert.assertNotNull("JMXConnector should not be null.", connector);
Expand All @@ -185,7 +162,7 @@ public void testHMConnectorServerWhenShutdownCluster() throws Exception {
*/
public static class MyAccessController extends AccessController {
@Override
public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) throws IOException {
public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) {
// Do nothing. In particular, stop the creation of the hbase:acl table. It makes the
// shutdown take time.
}
Expand Down

0 comments on commit 25fcc40

Please sign in to comment.