HBASE-23741 Data loss when WAL split to HFile enabled (#1254)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestJMXConnectorServer.java
blob2b94b7ff0d6ae79a3b2ae06d96f334733994a29d
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import java.io.IOException;
21 import javax.management.remote.JMXConnector;
22 import javax.management.remote.JMXConnectorFactory;
23 import javax.naming.ServiceUnavailableException;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.client.Admin;
26 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
27 import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
28 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
29 import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
30 import org.apache.hadoop.hbase.security.AccessDeniedException;
31 import org.apache.hadoop.hbase.security.access.AccessController;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.testclassification.MiscTests;
34 import org.junit.After;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 /**
44 * Test case for JMX Connector Server.
46 @Category({ MiscTests.class, MediumTests.class })
47 public class TestJMXConnectorServer {
49 @ClassRule
50 public static final HBaseClassTestRule CLASS_RULE =
51 HBaseClassTestRule.forClass(TestJMXConnectorServer.class);
53 private static final Logger LOG = LoggerFactory.getLogger(TestJMXConnectorServer.class);
54 private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
56 private static Configuration conf = null;
57 private static Admin admin;
58 // RMI registry port
59 private static int rmiRegistryPort = 61120;
60 // Switch for customized Accesscontroller to throw ACD exception while executing test case
61 static boolean hasAccess;
63 @Before
64 public void setUp() throws Exception {
65 UTIL = new HBaseTestingUtility();
66 conf = UTIL.getConfiguration();
69 @After
70 public void tearDown() throws Exception {
71 // Set to true while stopping cluster
72 hasAccess = true;
73 admin.close();
74 UTIL.shutdownMiniCluster();
77 /**
78 * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
80 @Test
81 public void testHMConnectorServerWhenStopMaster() throws Exception {
82 conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
83 JMXListener.class.getName() + "," + MyAccessController.class.getName());
84 conf.setInt("master.rmi.registry.port", rmiRegistryPort);
85 UTIL.startMiniCluster();
86 admin = UTIL.getConnection().getAdmin();
88 // try to stop master
89 boolean accessDenied = false;
90 try {
91 hasAccess = false;
92 LOG.info("Stopping HMaster...");
93 admin.stopMaster();
94 } catch (AccessDeniedException e) {
95 LOG.info("Exception occurred while stopping HMaster. ", e);
96 accessDenied = true;
98 Assert.assertTrue(accessDenied);
100 // Check whether HMaster JMX Connector server can be connected
101 JMXConnector connector = null;
102 try {
103 connector = JMXConnectorFactory
104 .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
105 } catch (IOException e) {
106 if (e.getCause() instanceof ServiceUnavailableException) {
107 Assert.fail("Can't connect to HMaster ConnectorServer.");
110 Assert.assertNotNull("JMXConnector should not be null.", connector);
111 connector.close();
115 * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
116 * call.
118 @Test
119 public void testRSConnectorServerWhenStopRegionServer() throws Exception {
120 conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
121 JMXListener.class.getName() + "," + MyAccessController.class.getName());
122 conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort);
123 UTIL.startMiniCluster();
124 admin = UTIL.getConnection().getAdmin();
126 hasAccess = false;
127 ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
128 LOG.info("Stopping Region Server...");
129 admin.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());
131 // Check whether Region Sever JMX Connector server can be connected
132 JMXConnector connector = null;
133 try {
134 connector = JMXConnectorFactory
135 .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
136 } catch (IOException e) {
137 if (e.getCause() instanceof ServiceUnavailableException) {
138 Assert.fail("Can't connect to Region Server ConnectorServer.");
141 Assert.assertNotNull("JMXConnector should not be null.", connector);
142 connector.close();
146 * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
148 @Test
149 public void testHMConnectorServerWhenShutdownCluster() throws Exception {
150 conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
151 JMXListener.class.getName() + "," + MyAccessController.class.getName());
152 conf.setInt("master.rmi.registry.port", rmiRegistryPort);
154 UTIL.startMiniCluster();
155 admin = UTIL.getConnection().getAdmin();
157 boolean accessDenied = false;
158 try {
159 hasAccess = false;
160 LOG.info("Stopping HMaster...");
161 admin.shutdown();
162 } catch (AccessDeniedException e) {
163 LOG.error("Exception occurred while stopping HMaster. ", e);
164 accessDenied = true;
166 Assert.assertTrue(accessDenied);
168 // Check whether HMaster JMX Connector server can be connected
169 JMXConnector connector = null;
170 try {
171 connector = JMXConnectorFactory
172 .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
173 } catch (IOException e) {
174 if (e.getCause() instanceof ServiceUnavailableException) {
175 Assert.fail("Can't connect to HMaster ConnectorServer.");
178 Assert.assertNotNull("JMXConnector should not be null.", connector);
179 connector.close();
183 * Customized class for test case execution which will throw ACD exception while executing
184 * stopMaster/preStopRegionServer/preShutdown explicitly.
186 public static class MyAccessController extends AccessController {
187 @Override
188 public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) throws IOException {
189 // Do nothing. In particular, stop the creation of the hbase:acl table. It makes the
190 // shutdown take time.
193 @Override
194 public void preStopMaster(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
195 if (!hasAccess) {
196 throw new AccessDeniedException("Insufficient permissions to stop master");
200 @Override
201 public void preStopRegionServer(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
202 throws IOException {
203 if (!hasAccess) {
204 throw new AccessDeniedException("Insufficient permissions to stop region server.");
208 @Override
209 public void preShutdown(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
210 if (!hasAccess) {
211 throw new AccessDeniedException("Insufficient permissions to shut down cluster.");
215 @Override
216 public void preExecuteProcedures(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
217 throws IOException {
218 // FIXME: ignore the procedure permission check since in our UT framework master is neither
219 // the systemuser nor the superuser so we can not call executeProcedures...