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  
94  
95        // Do a put. The counters should be non-zero now
96        Put p = new Put(row);
97        p.addColumn(Bytes.toBytes("D"), Bytes.toBytes("Zero"), Bytes.toBytes("VALUE"));
98        t.put(p);
99  
100 
101       MetricsRegionAggregateSource currentAgg = currentServer.getRegion(regionInfo.getRegionName())
102           .getMetrics()
103           .getSource()
104           .getAggregateSource();
105 
106       String prefix = "namespace_"+ NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
107           "_table_"+tableNameString +
108           "_region_" + regionInfo.getEncodedName()+
109           "_metric";
110 
111       metricsHelper.assertCounter(prefix + "_putCount", 1, currentAgg);
112 
113 
114       try {
115         TEST_UTIL.moveRegionAndWait(regionInfo, destServer.getServerName());
116         moved = true;
117       } catch (IOException ioe) {
118         moved = false;
119       }
120 
121       if (moved) {
122         MetricsRegionAggregateSource destAgg = destServer.getRegion(regionInfo.getRegionName())
123             .getMetrics()
124             .getSource()
125             .getAggregateSource();
126         metricsHelper.assertCounter(prefix + "_putCount", 0, destAgg);
127       }
128     }
129 
130     TEST_UTIL.deleteTable(tableName);
131 
132   }
133 }