001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.junit.Assert.*;
021
022import java.util.AbstractMap.SimpleImmutableEntry;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
026import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
027import org.apache.hadoop.hbase.quotas.SpaceViolationPolicy;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.util.Threads;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Category({MasterTests.class, MediumTests.class})
040public class TestMasterMetricsWrapper {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestMasterMetricsWrapper.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetricsWrapper.class);
047
048  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
049  private static final int NUM_RS = 4;
050
051  @BeforeClass
052  public static void setup() throws Exception {
053    TEST_UTIL.startMiniCluster(1, NUM_RS);
054  }
055
056  @AfterClass
057  public static void teardown() throws Exception {
058    TEST_UTIL.shutdownMiniCluster();
059  }
060
061  @Test
062  public void testInfo() {
063    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
064    MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
065    assertEquals(master.getSplitPlanCount(), info.getSplitPlanCount(), 0);
066    assertEquals(master.getMergePlanCount(), info.getMergePlanCount(), 0);
067    assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
068    assertEquals(master.getClusterId(), info.getClusterId());
069    assertEquals(master.getMasterActiveTime(), info.getActiveTime());
070    assertEquals(master.getMasterStartTime(), info.getStartTime());
071    assertEquals(master.getMasterCoprocessors().length, info.getCoprocessors().length);
072    assertEquals(master.getServerManager().getOnlineServersList().size(), info.getNumRegionServers());
073    int regionServerCount =
074      NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 1: 0);
075    assertEquals(regionServerCount, info.getNumRegionServers());
076
077    String zkServers = info.getZookeeperQuorum();
078    assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
079
080    final int index = 3;
081    LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
082    TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
083    TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
084    // We stopped the regionserver but could take a while for the master to notice it so hang here
085    // until it does... then move forward to see if metrics wrapper notices.
086    while (TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size() ==
087        regionServerCount ) {
088      Threads.sleep(10);
089    }
090    assertEquals(regionServerCount - 1, info.getNumRegionServers());
091    assertEquals(1, info.getNumDeadRegionServers());
092    assertEquals(1, info.getNumWALFiles());
093  }
094
095  @Test
096  public void testQuotaSnapshotConversion() {
097    MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(
098        TEST_UTIL.getHBaseCluster().getMaster());
099    assertEquals(new SimpleImmutableEntry<Long,Long>(1024L, 2048L),
100        info.convertSnapshot(new SpaceQuotaSnapshot(
101            SpaceQuotaStatus.notInViolation(), 1024L, 2048L)));
102    assertEquals(new SimpleImmutableEntry<Long,Long>(4096L, 2048L),
103        info.convertSnapshot(new SpaceQuotaSnapshot(
104            new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L)));
105  }
106}