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