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.regionserver;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.CompatibilityFactory;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.NamespaceDescriptor;
027import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.RegionLocator;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.test.MetricsAssertHelper;
035import org.apache.hadoop.hbase.testclassification.LargeTests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.Threads;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.junit.rules.TestName;
045
046@Category({ RegionServerTests.class, LargeTests.class })
047public class TestRemoveRegionMetrics {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestRemoveRegionMetrics.class);
052
053  private static SingleProcessHBaseCluster cluster;
054  private static Configuration conf;
055  private static HBaseTestingUtil TEST_UTIL;
056  private static MetricsAssertHelper metricsHelper;
057
058  @Rule
059  public TestName name = new TestName();
060
061  @BeforeClass
062  public static void startCluster() throws Exception {
063    metricsHelper = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
064    TEST_UTIL = new HBaseTestingUtil();
065    conf = TEST_UTIL.getConfiguration();
066    conf.getLong("hbase.splitlog.max.resubmit", 0);
067    // Make the failure test faster
068    conf.setInt("zookeeper.recovery.retry", 0);
069    conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
070
071    TEST_UTIL.startMiniCluster(2);
072    cluster = TEST_UTIL.getHBaseCluster();
073
074    cluster.waitForActiveAndReadyMaster();
075
076    while (cluster.getLiveRegionServerThreads().size() < 2) {
077      Threads.sleep(100);
078    }
079  }
080
081  @Test
082  public void testMoveRegion() throws IOException, InterruptedException {
083    String tableNameString = name.getMethodName();
084    TableName tableName = TableName.valueOf(tableNameString);
085    Table t = TEST_UTIL.createTable(tableName, Bytes.toBytes("D"));
086    TEST_UTIL.waitUntilAllRegionsAssigned(t.getName());
087    Admin admin = TEST_UTIL.getAdmin();
088    RegionInfo regionInfo;
089    byte[] row = Bytes.toBytes("r1");
090
091    for (int i = 0; i < 30; i++) {
092      boolean moved = false;
093      try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
094        regionInfo = locator.getRegionLocation(row, true).getRegion();
095      }
096
097      int currentServerIdx = cluster.getServerWith(regionInfo.getRegionName());
098      int destServerIdx = (currentServerIdx + 1) % cluster.getLiveRegionServerThreads().size();
099      HRegionServer currentServer = cluster.getRegionServer(currentServerIdx);
100      HRegionServer destServer = cluster.getRegionServer(destServerIdx);
101
102      // Do a put. The counters should be non-zero now
103      Put p = new Put(row);
104      p.addColumn(Bytes.toBytes("D"), Bytes.toBytes("Zero"), Bytes.toBytes("VALUE"));
105      t.put(p);
106
107      MetricsRegionAggregateSource currentAgg = currentServer.getRegion(regionInfo.getRegionName())
108        .getMetrics().getSource().getAggregateSource();
109
110      String prefix = "namespace_" + NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR + "_table_"
111        + tableNameString + "_region_" + regionInfo.getEncodedName() + "_metric";
112
113      metricsHelper.assertCounter(prefix + "_putCount", 1, currentAgg);
114
115      try {
116        TEST_UTIL.moveRegionAndWait(regionInfo, destServer.getServerName());
117        moved = true;
118      } catch (IOException ioe) {
119        moved = false;
120      }
121
122      if (moved) {
123        MetricsRegionAggregateSource destAgg = destServer.getRegion(regionInfo.getRegionName())
124          .getMetrics().getSource().getAggregateSource();
125        metricsHelper.assertCounter(prefix + "_putCount", 0, destAgg);
126      }
127    }
128
129    TEST_UTIL.deleteTable(tableName);
130
131  }
132}