View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static org.junit.Assert.*;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.testclassification.MediumTests;
26  import org.apache.hadoop.hbase.util.Threads;
27  import org.junit.AfterClass;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(MediumTests.class)
33  public class TestMasterMetricsWrapper {
34    private static final Log LOG = LogFactory.getLog(TestMasterMetricsWrapper.class);
35  
36    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
37  
38    @BeforeClass
39    public static void setup() throws Exception {
40      TEST_UTIL.startMiniCluster(1, 4);
41    }
42  
43    @AfterClass
44    public static void teardown() throws Exception {
45      TEST_UTIL.shutdownMiniCluster();
46    }
47  
48    @Test (timeout = 30000)
49    public void testInfo() {
50      HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
51      MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
52      assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
53      assertEquals(master.getClusterId(), info.getClusterId());
54      assertEquals(master.getMasterActiveTime(), info.getActiveTime());
55      assertEquals(master.getMasterStartTime(), info.getStartTime());
56      assertEquals(master.getCoprocessors().length, info.getCoprocessors().length);
57      assertEquals(master.getServerManager().getOnlineServersList().size(), info.getNumRegionServers());
58      assertTrue(info.getNumRegionServers() == 4);
59  
60      String zkServers = info.getZookeeperQuorum();
61      assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
62  
63      final int index = 3;
64      LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
65      TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
66      TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
67      // We stopped the regionserver but could take a while for the master to notice it so hang here
68      // until it does... then move forward to see if metrics wrapper notices.
69      while (TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size() !=
70          index) {
71        Threads.sleep(10);
72      }
73      assertTrue(info.getNumRegionServers() == 3);
74      assertTrue(info.getNumDeadRegionServers() == 1);
75    }
76  }