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.master.balancer; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.Random; 023import java.util.concurrent.ThreadLocalRandom; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseTestingUtility; 027import org.apache.hadoop.hbase.MiniHBaseCluster; 028import org.apache.hadoop.hbase.master.HMaster; 029import org.apache.hadoop.hbase.testclassification.MediumTests; 030import org.apache.hadoop.metrics2.MetricsSource; 031import org.apache.hadoop.metrics2.MetricsTag; 032import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041@Category({ MediumTests.class }) 042public class TestBalancerStatusTagInJMXMetrics extends BalancerTestBase { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestBalancerStatusTagInJMXMetrics.class); 047 048 private static final Logger LOG = 049 LoggerFactory.getLogger(TestBalancerStatusTagInJMXMetrics.class); 050 private static HBaseTestingUtility UTIL = new HBaseTestingUtility(); 051 private static int connectorPort = 61120; 052 private static HMaster master; 053 private static MiniHBaseCluster cluster; 054 private static Configuration conf = null; 055 056 /** 057 * Setup the environment for the test. 058 */ 059 @BeforeClass 060 public static void setupBeforeClass() throws Exception { 061 conf = UTIL.getConfiguration(); 062 Random rand = ThreadLocalRandom.current(); 063 for (int i = 0; i < 10; i++) { 064 do { 065 int sign = i % 2 == 0 ? 1 : -1; 066 connectorPort += sign * rand.nextInt(100); 067 } while (!HBaseTestingUtility.available(connectorPort)); 068 try { 069 conf.setInt("regionserver.rmi.registry.port", connectorPort); 070 cluster = UTIL.startMiniCluster(); 071 LOG.info("Waiting for active/ready master"); 072 cluster.waitForActiveAndReadyMaster(); 073 master = cluster.getMaster(); 074 break; 075 } catch (Exception e) { 076 LOG.debug("Encountered exception when starting mini cluster. Trying port " + connectorPort, 077 e); 078 try { 079 // this is to avoid "IllegalStateException: A mini-cluster is already running" 080 UTIL.shutdownMiniCluster(); 081 } catch (Exception ex) { 082 LOG.debug("Encountered exception shutting down cluster", ex); 083 } 084 } 085 } 086 } 087 088 @AfterClass 089 public static void tearDownAfterClass() throws Exception { 090 UTIL.shutdownMiniCluster(); 091 } 092 093 /** 094 * Tests the status change using the Default Metrics System 095 */ 096 @Test 097 public void testJmxMetrics() throws Exception { 098 099 assertEquals(getStatus(), "true"); 100 master.getLoadBalancer().updateBalancerStatus(false); 101 assertEquals(getStatus(), "false"); 102 103 } 104 105 /** 106 * Gets the balancer status tag from the Metrics registry 107 */ 108 public String getStatus() throws Exception { 109 MetricsSource source = 110 DefaultMetricsSystem.instance().getSource(MetricsBalancerSource.METRICS_JMX_CONTEXT); 111 if (source instanceof MetricsBalancerSourceImpl) { 112 MetricsTag status = ((MetricsBalancerSourceImpl) source).getMetricsRegistry() 113 .getTag(MetricsBalancerSource.BALANCER_STATUS); 114 return status.value(); 115 } else { 116 LOG.warn("Balancer JMX Metrics not registered"); 117 throw new Exception("MetricsBalancer JMX Context not found"); 118 } 119 } 120 121}