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