View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    * <p>
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * <p>
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.CompatibilityFactory;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.MiniHBaseCluster;
26  import org.apache.hadoop.hbase.NamespaceDescriptor;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.HBaseAdmin;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.apache.hadoop.hbase.client.RegionLocator;
31  import org.apache.hadoop.hbase.client.Table;
32  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
33  import org.apache.hadoop.hbase.testclassification.LargeTests;
34  
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.util.Threads;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import java.io.IOException;
42  
43  @Category({LargeTests.class})
44  public class TestRemoveRegionMetrics {
45  
46    private static MiniHBaseCluster cluster;
47    private static Configuration conf;
48    private static HBaseTestingUtility TEST_UTIL;
49    private static MetricsAssertHelper metricsHelper;
50  
51    @BeforeClass
52    public static void startCluster() throws Exception {
53      metricsHelper = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
54      TEST_UTIL = new HBaseTestingUtility();
55      conf = TEST_UTIL.getConfiguration();
56      conf.getLong("hbase.splitlog.max.resubmit", 0);
57      // Make the failure test faster
58      conf.setInt("zookeeper.recovery.retry", 0);
59      conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
60  
61      TEST_UTIL.startMiniCluster(1, 2);
62      cluster = TEST_UTIL.getHBaseCluster();
63  
64      cluster.waitForActiveAndReadyMaster();
65  
66      while (cluster.getLiveRegionServerThreads().size() < 2) {
67        Threads.sleep(100);
68      }
69    }
70  
71  
72    @Test
73    public void testMoveRegion() throws IOException, InterruptedException {
74      String tableNameString = "testMoveRegion";
75      TableName tableName = TableName.valueOf(tableNameString);
76      Table t = TEST_UTIL.createTable(tableName, Bytes.toBytes("D"));
77      TEST_UTIL.waitUntilAllRegionsAssigned(t.getName());
78      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
79      HRegionInfo regionInfo;
80      byte[] row =  Bytes.toBytes("r1");
81  
82  
83      for (int i = 0; i < 30; i++) {
84        boolean moved = false;
85        try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
86          regionInfo = locator.getRegionLocation(row, true).getRegionInfo();
87        }
88  
89        int currentServerIdx = cluster.getServerWith(regionInfo.getRegionName());
90        int destServerIdx = (currentServerIdx +1)% cluster.getLiveRegionServerThreads().size();
91        HRegionServer currentServer = cluster.getRegionServer(currentServerIdx);
92        HRegionServer destServer = cluster.getRegionServer(destServerIdx);
93        byte[] destServerName = Bytes.toBytes(destServer.getServerName().getServerName());
94  
95  
96        // Do a put. The counters should be non-zero now
97        Put p = new Put(row);
98        p.addColumn(Bytes.toBytes("D"), Bytes.toBytes("Zero"), Bytes.toBytes("VALUE"));
99        t.put(p);
100 
101 
102       MetricsRegionAggregateSource currentAgg = currentServer.getRegion(regionInfo.getRegionName())
103           .getMetrics()
104           .getSource()
105           .getAggregateSource();
106 
107       String prefix = "namespace_"+ NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
108           "_table_"+tableNameString +
109           "_region_" + regionInfo.getEncodedName()+
110           "_metric";
111 
112       metricsHelper.assertCounter(prefix + "_mutateCount", 1, currentAgg);
113 
114 
115       try {
116         admin.move(regionInfo.getEncodedNameAsBytes(), destServerName);
117         moved = true;
118         Thread.sleep(5000);
119       } catch (IOException ioe) {
120         moved = false;
121       }
122       TEST_UTIL.waitUntilAllRegionsAssigned(t.getName());
123 
124       if (moved) {
125         MetricsRegionAggregateSource destAgg = destServer.getRegion(regionInfo.getRegionName())
126             .getMetrics()
127             .getSource()
128             .getAggregateSource();
129         metricsHelper.assertCounter(prefix + "_mutateCount", 0, destAgg);
130       }
131     }
132 
133     TEST_UTIL.deleteTable(tableName);
134 
135   }
136 }