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    assertEquals(master.getMasterWalManager().getOldWALsDirSize(), info.getOldWALsDirSize());
078    int regionServerCount =
079      NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()) ? 1 : 0);
080    assertEquals(regionServerCount, info.getNumRegionServers());
081
082    String zkServers = info.getZookeeperQuorum();
083    assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
084
085    final int index = 3;
086    LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
087    TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
088    TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
089    // We stopped the regionserver but could take a while for the master to notice it so hang here
090    // until it does... then move forward to see if metrics wrapper notices.
091    while (
092      TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size()
093          == regionServerCount
094    ) {
095      Threads.sleep(10);
096    }
097    assertEquals(regionServerCount - 1, info.getNumRegionServers());
098    assertEquals(1, info.getNumDeadRegionServers());
099    // now we do not expose this information as WALProcedureStore is not the only ProcedureStore
100    // implementation any more.
101    assertEquals(0, info.getNumWALFiles());
102    // We decommission the first online region server and verify the metrics.
103    TEST_UTIL.getMiniHBaseCluster().getMaster().decommissionRegionServers(
104      master.getServerManager().getOnlineServersList().subList(0, 1), false);
105    assertEquals(1, info.getNumDrainingRegionServers());
106    assertEquals(master.getServerManager().getOnlineServersList().get(0).toString(),
107      info.getDrainingRegionServers());
108  }
109
110  @Test
111  public void testQuotaSnapshotConversion() {
112    MetricsMasterWrapperImpl info =
113      new MetricsMasterWrapperImpl(TEST_UTIL.getHBaseCluster().getMaster());
114    assertEquals(new SimpleImmutableEntry<Long, Long>(1024L, 2048L), info
115      .convertSnapshot(new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), 1024L, 2048L)));
116    assertEquals(new SimpleImmutableEntry<Long, Long>(4096L, 2048L), info.convertSnapshot(
117      new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L)));
118  }
119}