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