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.master.HMaster;
26  import org.apache.hadoop.hbase.MediumTests;
27  import org.apache.hadoop.hbase.master.MetricsMasterWrapperImpl;
28  import org.apache.hadoop.hbase.util.Threads;
29  import org.junit.AfterClass;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(MediumTests.class)
35  public class TestMasterMetricsWrapper {
36    private static final Log LOG = LogFactory.getLog(TestMasterMetricsWrapper.class);
37  
38    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
39  
40    @BeforeClass
41    public static void setup() throws Exception {
42      TEST_UTIL.startMiniCluster(1, 4);
43    }
44  
45    @AfterClass
46    public static void teardown() throws Exception {
47      TEST_UTIL.shutdownMiniCluster();
48    }
49  
50    @Test (timeout = 30000)
51    public void testInfo() {
52      HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
53      MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
54      assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
55      assertEquals(master.getClusterId(), info.getClusterId());
56      assertEquals(master.getMasterActiveTime(), info.getActiveTime());
57      assertEquals(master.getMasterStartTime(), info.getStartTime());
58      assertEquals(master.getCoprocessors().length, info.getCoprocessors().length);
59      assertEquals(master.getServerManager().getOnlineServersList().size(), info.getNumRegionServers());
60      assertTrue(info.getNumRegionServers() == 4);
61  
62      String zkServers = info.getZookeeperQuorum();
63      assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
64  
65      final int index = 3;
66      LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
67      TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
68      TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
69      // We stopped the regionserver but could take a while for the master to notice it so hang here
70      // until it does... then move forward to see if metrics wrapper notices.
71      while (TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size() !=
72          index) {
73        Threads.sleep(10);
74      }
75      assertTrue(info.getNumRegionServers() == 3);
76      assertTrue(info.getNumDeadRegionServers() == 1);
77    }
78  }