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 java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import junit.framework.Assert;
25  
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HServerLoad;
28  import org.apache.hadoop.hbase.MediumTests;
29  import org.apache.hadoop.hbase.regionserver.HRegionServer;
30  import org.junit.AfterClass;
31  import org.junit.BeforeClass;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(MediumTests.class)
36  public class TestMXBean {
37  
38    private static final HBaseTestingUtility TEST_UTIL =
39        new HBaseTestingUtility();
40  
41    @BeforeClass
42    public static void setup() throws Exception {
43      TEST_UTIL.startMiniCluster(1, 4);
44    }
45  
46    @AfterClass
47    public static void teardown() throws Exception {
48      TEST_UTIL.shutdownMiniCluster();
49    }
50  
51    private void verifyRegionServers(Map<String, HServerLoad> regions) {
52      Set<String> expected = new HashSet<String>();
53      for (int i = 0; i < 4; ++i) {
54        HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(i);
55        expected.add(rs.getServerName().getServerName());
56      }
57  
58      int found = 0;
59      for (java.util.Map.Entry<String, HServerLoad> entry : regions.entrySet()) {
60        if (expected.contains(entry.getKey())) {
61          ++found;
62        }
63      }
64      Assert.assertEquals(4, found);
65    }
66  
67    @Test
68    public void testInfo() {
69      HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
70      MXBeanImpl info = MXBeanImpl.init(master);
71      Assert.assertEquals(master.getAverageLoad(), info.getAverageLoad());
72      Assert.assertEquals(master.getClusterId(), info.getClusterId());
73      Assert.assertEquals(master.getMasterActiveTime(),
74          info.getMasterActiveTime());
75      Assert.assertEquals(master.getMasterStartTime(),
76          info.getMasterStartTime());
77      Assert.assertEquals(master.getCoprocessors().length,
78          info.getCoprocessors().length);
79      Assert.assertEquals(master.getServerManager().getOnlineServersList().size(),
80          info.getRegionServers().size());
81      Assert.assertEquals(master.getAssignmentManager().isRegionsInTransition(),
82          info.getRegionsInTransition().length > 0);
83      Assert.assertTrue(info.getRegionServers().size() == 4);
84  
85      String zkServers = info.getZookeeperQuorum();
86      Assert.assertEquals(zkServers.split(",").length,
87          TEST_UTIL.getZkCluster().getZooKeeperServerNum());
88  
89      verifyRegionServers(info.getRegionServers());
90  
91      TEST_UTIL.getMiniHBaseCluster().stopRegionServer(3, false);
92      TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(3);
93      Assert.assertTrue(info.getRegionServers().size() == 3);
94      Assert.assertTrue(info.getDeadRegionServers().length == 1);
95  
96    }
97  
98  }