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.HBaseTestingUtility; 025import org.apache.hadoop.hbase.HConstants; 026import org.apache.hadoop.hbase.HRegionInfo; 027import org.apache.hadoop.hbase.MiniHBaseCluster; 028import org.apache.hadoop.hbase.NamespaceDescriptor; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.Admin; 031import org.apache.hadoop.hbase.client.Put; 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 MiniHBaseCluster cluster; 054 private static Configuration conf; 055 private static HBaseTestingUtility 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 HBaseTestingUtility(); 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 082 @Test 083 public void testMoveRegion() throws IOException, InterruptedException { 084 String tableNameString = name.getMethodName(); 085 TableName tableName = TableName.valueOf(tableNameString); 086 Table t = TEST_UTIL.createTable(tableName, Bytes.toBytes("D")); 087 TEST_UTIL.waitUntilAllRegionsAssigned(t.getName()); 088 Admin admin = TEST_UTIL.getAdmin(); 089 HRegionInfo regionInfo; 090 byte[] row = Bytes.toBytes("r1"); 091 092 093 for (int i = 0; i < 30; i++) { 094 boolean moved = false; 095 try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) { 096 regionInfo = locator.getRegionLocation(row, true).getRegionInfo(); 097 } 098 099 int currentServerIdx = cluster.getServerWith(regionInfo.getRegionName()); 100 int destServerIdx = (currentServerIdx +1)% cluster.getLiveRegionServerThreads().size(); 101 HRegionServer currentServer = cluster.getRegionServer(currentServerIdx); 102 HRegionServer destServer = cluster.getRegionServer(destServerIdx); 103 104 105 // Do a put. The counters should be non-zero now 106 Put p = new Put(row); 107 p.addColumn(Bytes.toBytes("D"), Bytes.toBytes("Zero"), Bytes.toBytes("VALUE")); 108 t.put(p); 109 110 111 MetricsRegionAggregateSource currentAgg = currentServer.getRegion(regionInfo.getRegionName()) 112 .getMetrics() 113 .getSource() 114 .getAggregateSource(); 115 116 String prefix = "namespace_"+ NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+ 117 "_table_"+tableNameString + 118 "_region_" + regionInfo.getEncodedName()+ 119 "_metric"; 120 121 metricsHelper.assertCounter(prefix + "_putCount", 1, currentAgg); 122 123 124 try { 125 TEST_UTIL.moveRegionAndWait(regionInfo, destServer.getServerName()); 126 moved = true; 127 } catch (IOException ioe) { 128 moved = false; 129 } 130 131 if (moved) { 132 MetricsRegionAggregateSource destAgg = destServer.getRegion(regionInfo.getRegionName()) 133 .getMetrics() 134 .getSource() 135 .getAggregateSource(); 136 metricsHelper.assertCounter(prefix + "_putCount", 0, destAgg); 137 } 138 } 139 140 TEST_UTIL.deleteTable(tableName); 141 142 } 143}