001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019import static org.junit.Assert.fail;
020import java.io.IOException;
021import javax.management.MBeanServerConnection;
022import javax.management.remote.JMXConnector;
023import javax.management.remote.JMXConnectorFactory;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
026import org.apache.hadoop.hbase.net.BoundSocketMaker;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.util.JVMClusterUtil;
030import org.junit.AfterClass;
031import org.junit.Assert;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Rule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.junit.rules.ExpectedException;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({MiscTests.class, MediumTests.class})
042public class TestJMXListener {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestJMXListener.class);
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestJMXListener.class);
049  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
050  private static int CONNECTOR_PORT;
051
052  @BeforeClass
053  public static void setupBeforeClass() throws Exception {
054    // Default RMI timeouts are too long. Make them short for test.
055    System.setProperty("sun.rmi.transport.connectionTimeout", Integer.toString(5000));
056    System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", Integer.toString(5000));
057    System.setProperty("sun.rmi.transport.tcp.responseTimeout", Integer.toString(5000));
058    System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(5000));
059    Configuration conf = UTIL.getConfiguration();
060    // To test what happens when the jmx listener can't put up its port, uncomment the below.
061    BoundSocketMaker bsm = null; // new BoundSocketMaker(HBaseTestingUtility::randomFreePort);
062    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, JMXListener.class.getName());
063    CONNECTOR_PORT = bsm == null? HBaseTestingUtility.randomFreePort(): bsm.getPort();
064    // Make sure the JMX listener is up before we proceed. If it is not up, retry. It may not
065    // come up if there is a port clash/bind exception except its called something else in rmi.
066    for (int i = 0; i < 10; i++) {
067      conf.setInt("regionserver.rmi.registry.port", CONNECTOR_PORT);
068      UTIL.startMiniCluster();
069      // Make sure we can get make a JMX connection before proceeding. It may have failed setup
070      // because of port clash/bind-exception. Deal with it here.
071      JMXConnector connector = null;
072      try {
073        connector = JMXConnectorFactory.
074          connect(JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT));
075        break;
076      } catch (IOException ioe) {
077        UTIL.shutdownMiniCluster();
078        CONNECTOR_PORT = HBaseTestingUtility.randomFreePort();
079      } finally {
080        if (connector != null) {
081          connector.close();
082        }
083      }
084    }
085    if (bsm != null) {
086      bsm.close();
087    }
088  }
089
090  @AfterClass
091  public static void tearDownAfterClass() throws Exception {
092    UTIL.shutdownMiniCluster();
093  }
094
095  @Test
096  public void testStart() throws Exception {
097    JMXConnector connector = JMXConnectorFactory.connect(
098      JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT));
099
100    MBeanServerConnection mb = connector.getMBeanServerConnection();
101    String domain = mb.getDefaultDomain();
102    Assert.assertTrue("default domain is not correct",
103      !domain.isEmpty());
104    connector.close();
105
106  }
107
108  //shutdown hbase only. then try connect, IOException expected
109  @Rule
110  public ExpectedException expectedEx = ExpectedException.none();
111  @Test
112  public void testStop() throws Exception {
113    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
114    LOG.info("shutdown hbase cluster...");
115    cluster.shutdown();
116    LOG.info("wait for the hbase cluster shutdown...");
117    cluster.waitUntilShutDown();
118
119    JMXConnector connector = JMXConnectorFactory.newJMXConnector(
120      JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT), null);
121    expectedEx.expect(IOException.class);
122    connector.connect();
123
124  }
125
126  @Test
127  public void testGetRegionServerCoprocessors() throws Exception {
128    for (JVMClusterUtil.RegionServerThread rs : UTIL.getHBaseCluster().getRegionServerThreads()) {
129      boolean find = false;
130      for (String s : rs.getRegionServer().getRegionServerCoprocessors()) {
131        if (s.equals(JMXListener.class.getSimpleName())) {
132          find = true;
133          break;
134        }
135      }
136      if (!find) {
137        fail("where is the JMXListener?");
138      }
139    }
140  }
141}