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;
019
020import static org.junit.Assert.fail;
021
022import java.io.IOException;
023import javax.management.MBeanServerConnection;
024import javax.management.remote.JMXConnector;
025import javax.management.remote.JMXConnectorFactory;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
028import org.apache.hadoop.hbase.net.BoundSocketMaker;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.apache.hadoop.hbase.util.JVMClusterUtil;
032import org.junit.AfterClass;
033import org.junit.Assert;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.rules.ExpectedException;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043@Category({ MiscTests.class, MediumTests.class })
044public class TestJMXListener {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestJMXListener.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestJMXListener.class);
051  private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
052  private static int CONNECTOR_PORT;
053
054  @BeforeClass
055  public static void setupBeforeClass() throws Exception {
056    // Default RMI timeouts are too long. Make them short for test.
057    System.setProperty("sun.rmi.transport.connectionTimeout", Integer.toString(5000));
058    System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", Integer.toString(5000));
059    System.setProperty("sun.rmi.transport.tcp.responseTimeout", Integer.toString(5000));
060    System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(5000));
061    Configuration conf = UTIL.getConfiguration();
062    // To test what happens when the jmx listener can't put up its port, uncomment the below.
063    BoundSocketMaker bsm = null; // new BoundSocketMaker(HBaseTestingUtility::randomFreePort);
064    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, JMXListener.class.getName());
065    CONNECTOR_PORT = bsm == null ? HBaseTestingUtil.randomFreePort() : bsm.getPort();
066    // Make sure the JMX listener is up before we proceed. If it is not up, retry. It may not
067    // come up if there is a port clash/bind exception except its called something else in rmi.
068    for (int i = 0; i < 10; i++) {
069      conf.setInt("regionserver.rmi.registry.port", CONNECTOR_PORT);
070      UTIL.startMiniCluster();
071      // Make sure we can get make a JMX connection before proceeding. It may have failed setup
072      // because of port clash/bind-exception. Deal with it here.
073      JMXConnector connector = null;
074      try {
075        connector = JMXConnectorFactory
076          .connect(JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT));
077        break;
078      } catch (IOException ioe) {
079        UTIL.shutdownMiniCluster();
080        CONNECTOR_PORT = HBaseTestingUtil.randomFreePort();
081      } finally {
082        if (connector != null) {
083          connector.close();
084        }
085      }
086    }
087    if (bsm != null) {
088      bsm.close();
089    }
090  }
091
092  @AfterClass
093  public static void tearDownAfterClass() throws Exception {
094    UTIL.shutdownMiniCluster();
095  }
096
097  @Test
098  public void testStart() throws Exception {
099    JMXConnector connector =
100      JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT));
101
102    MBeanServerConnection mb = connector.getMBeanServerConnection();
103    String domain = mb.getDefaultDomain();
104    Assert.assertTrue("default domain is not correct", !domain.isEmpty());
105    connector.close();
106
107  }
108
109  // shutdown hbase only. then try connect, IOException expected
110  @Rule
111  public ExpectedException expectedEx = ExpectedException.none();
112
113  @Test
114  public void testStop() throws Exception {
115    SingleProcessHBaseCluster cluster = UTIL.getHBaseCluster();
116    LOG.info("shutdown hbase cluster...");
117    cluster.shutdown();
118    LOG.info("wait for the hbase cluster shutdown...");
119    cluster.waitUntilShutDown();
120
121    JMXConnector connector = JMXConnectorFactory
122      .newJMXConnector(JMXListener.buildJMXServiceURL(CONNECTOR_PORT, CONNECTOR_PORT), null);
123    expectedEx.expect(IOException.class);
124    connector.connect();
125
126  }
127
128  @Test
129  public void testGetRegionServerCoprocessors() throws Exception {
130    for (JVMClusterUtil.RegionServerThread rs : UTIL.getHBaseCluster().getRegionServerThreads()) {
131      boolean find = false;
132      for (String s : rs.getRegionServer().getRegionServerCoprocessors()) {
133        if (s.equals(JMXListener.class.getSimpleName())) {
134          find = true;
135          break;
136        }
137      }
138      if (!find) {
139        fail("where is the JMXListener?");
140      }
141    }
142  }
143}