View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.master.balancer;
13  
14  import static org.junit.Assert.assertEquals;
15  
16  import java.util.Random;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.MiniHBaseCluster;
23  import org.apache.hadoop.hbase.master.HMaster;
24  import org.apache.hadoop.hbase.testclassification.MediumTests;
25  import org.apache.hadoop.metrics2.MetricsSource;
26  import org.apache.hadoop.metrics2.MetricsTag;
27  import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category({ MediumTests.class })
34  public class TestBalancerStatusTagInJMXMetrics extends BalancerTestBase {
35    private static final Log LOG = LogFactory.getLog(TestBalancerStatusTagInJMXMetrics.class);
36    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
37    private static int connectorPort = 61120;
38    private static HMaster master;
39    private static MiniHBaseCluster cluster;
40    private static Configuration conf = null;
41  
42    /**
43     * Setup the environment for the test.
44     */
45    @BeforeClass
46    public static void setupBeforeClass() throws Exception {
47  
48      conf = UTIL.getConfiguration();
49      Random rand = new Random();
50      for (int i = 0; i < 10; i++) {
51        do {
52          int sign = i % 2 == 0 ? 1 : -1;
53          connectorPort += sign * rand.nextInt(100);
54        } while (!HBaseTestingUtility.available(connectorPort));
55        try {
56          conf.setInt("regionserver.rmi.registry.port", connectorPort);
57          cluster = UTIL.startMiniCluster();
58          LOG.info("Waiting for active/ready master");
59          cluster.waitForActiveAndReadyMaster();
60          master = cluster.getMaster();
61          break;
62        } catch (Exception e) {
63          LOG.debug("Encountered exception when starting mini cluster. Trying port " + connectorPort,
64            e);
65          try {
66            // this is to avoid "IllegalStateException: A mini-cluster is already running"
67            UTIL.shutdownMiniCluster();
68          } catch (Exception ex) {
69            LOG.debug("Encountered exception shutting down cluster", ex);
70          }
71        }
72      }
73    }
74  
75    @AfterClass
76    public static void tearDownAfterClass() throws Exception {
77      UTIL.shutdownMiniCluster();
78    }
79  
80    /**
81     * Tests the status change using the Default Metrics System
82     */
83    @Test
84    public void testJmxMetrics() throws Exception {
85  
86      assertEquals(getStatus(), "true");
87      master.getLoadBalancer().updateBalancerStatus(false);
88      assertEquals(getStatus(), "false");
89  
90    }
91  
92    /**
93     * Gets the balancer status tag from the Metrics registry
94     */
95    public String getStatus() throws Exception {
96      MetricsSource source =
97          DefaultMetricsSystem.instance().getSource(MetricsBalancerSource.METRICS_JMX_CONTEXT);
98      if (source instanceof MetricsBalancerSourceImpl) {
99        MetricsTag status = ((MetricsBalancerSourceImpl) source).getMetricsRegistry()
100           .getTag(MetricsBalancerSource.BALANCER_STATUS);
101       return status.value();
102     } else {
103       LOG.warn("Balancer JMX Metrics not registered");
104       throw new Exception("MetricsBalancer JMX Context not found");
105     }
106   }
107 
108 }