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.io.IOException;
023import java.util.AbstractMap.SimpleImmutableEntry;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
027import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
028import org.apache.hadoop.hbase.quotas.SpaceViolationPolicy;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.Threads;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040@Category({ MasterTests.class, MediumTests.class })
041public class TestMasterMetricsWrapper {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestMasterMetricsWrapper.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetricsWrapper.class);
048
049  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050  private static final int NUM_RS = 4;
051
052  @BeforeClass
053  public static void setup() throws Exception {
054    TEST_UTIL.startMiniCluster(NUM_RS);
055  }
056
057  @AfterClass
058  public static void teardown() throws Exception {
059    TEST_UTIL.shutdownMiniCluster();
060  }
061
062  @Test
063  public void testInfo() throws IOException {
064    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
065    MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
066    assertEquals(master.getRegionNormalizerManager().getSplitPlanCount(), info.getSplitPlanCount(),
067      0);
068    assertEquals(master.getRegionNormalizerManager().getMergePlanCount(), info.getMergePlanCount(),
069      0);
070    assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
071    assertEquals(master.getClusterId(), info.getClusterId());
072    assertEquals(master.getMasterActiveTime(), info.getActiveTime());
073    assertEquals(master.getMasterStartTime(), info.getStartTime());
074    assertEquals(master.getMasterCoprocessors().length, info.getCoprocessors().length);
075    assertEquals(master.getServerManager().getOnlineServersList().size(),
076      info.getNumRegionServers());
077    int regionServerCount =
078      NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()) ? 1 : 0);
079    assertEquals(regionServerCount, info.getNumRegionServers());
080
081    String zkServers = info.getZookeeperQuorum();
082    assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
083
084    final int index = 3;
085    LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
086    TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
087    TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
088    // We stopped the regionserver but could take a while for the master to notice it so hang here
089    // until it does... then move forward to see if metrics wrapper notices.
090    while (
091      TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size()
092          == regionServerCount
093    ) {
094      Threads.sleep(10);
095    }
096    assertEquals(regionServerCount - 1, info.getNumRegionServers());
097    assertEquals(1, info.getNumDeadRegionServers());
098    // now we do not expose this information as WALProcedureStore is not the only ProcedureStore
099    // implementation any more.
100    assertEquals(0, info.getNumWALFiles());
101    // We decommission the first online region server and verify the metrics.
102    TEST_UTIL.getMiniHBaseCluster().getMaster().decommissionRegionServers(
103      master.getServerManager().getOnlineServersList().subList(0, 1), false);
104    assertEquals(1, info.getNumDrainingRegionServers());
105    assertEquals(master.getServerManager().getOnlineServersList().get(0).toString(),
106      info.getDrainingRegionServers());
107  }
108
109  @Test
110  public void testQuotaSnapshotConversion() {
111    MetricsMasterWrapperImpl info =
112      new MetricsMasterWrapperImpl(TEST_UTIL.getHBaseCluster().getMaster());
113    assertEquals(new SimpleImmutableEntry<Long, Long>(1024L, 2048L), info
114      .convertSnapshot(new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 1024L, 2048L)));
115    assertEquals(new SimpleImmutableEntry<Long, Long>(4096L, 2048L), info.convertSnapshot(
116      new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L)));
117  }
118}