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.assertEquals;
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(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(
066      master.getRegionNormalizerManager().getSplitPlanCount(), info.getSplitPlanCount(), 0);
067    assertEquals(
068      master.getRegionNormalizerManager().getMergePlanCount(), info.getMergePlanCount(), 0);
069    assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
070    assertEquals(master.getClusterId(), info.getClusterId());
071    assertEquals(master.getMasterActiveTime(), info.getActiveTime());
072    assertEquals(master.getMasterStartTime(), info.getStartTime());
073    assertEquals(master.getMasterCoprocessors().length, info.getCoprocessors().length);
074    assertEquals(master.getServerManager().getOnlineServersList().size(), info.getNumRegionServers());
075    int regionServerCount =
076      NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 1: 0);
077    assertEquals(regionServerCount, info.getNumRegionServers());
078
079    String zkServers = info.getZookeeperQuorum();
080    assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
081
082    final int index = 3;
083    LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
084    TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
085    TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
086    // We stopped the regionserver but could take a while for the master to notice it so hang here
087    // until it does... then move forward to see if metrics wrapper notices.
088    while (TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size() ==
089        regionServerCount ) {
090      Threads.sleep(10);
091    }
092    assertEquals(regionServerCount - 1, info.getNumRegionServers());
093    assertEquals(1, info.getNumDeadRegionServers());
094    // now we do not expose this information as WALProcedureStore is not the only ProcedureStore
095    // implementation any more.
096    assertEquals(0, info.getNumWALFiles());
097  }
098
099  @Test
100  public void testQuotaSnapshotConversion() {
101    MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(
102        TEST_UTIL.getHBaseCluster().getMaster());
103    assertEquals(new SimpleImmutableEntry<Long,Long>(1024L, 2048L),
104        info.convertSnapshot(new SpaceQuotaSnapshot(
105            SpaceQuotaStatus.notInViolation(), 1024L, 2048L)));
106    assertEquals(new SimpleImmutableEntry<Long,Long>(4096L, 2048L),
107        info.convertSnapshot(new SpaceQuotaSnapshot(
108            new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L)));
109  }
110}