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    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.master;
19  
20  import java.util.HashMap;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.CompatibilityFactory;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.MiniHBaseCluster;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  @Category(MediumTests.class)
44  public class TestAssignmentManagerMetrics {
45  
46    private static final Log LOG = LogFactory.getLog(TestAssignmentManagerMetrics.class);
47    private static final MetricsAssertHelper metricsHelper = CompatibilityFactory
48        .getInstance(MetricsAssertHelper.class);
49  
50    private static MiniHBaseCluster cluster;
51    private static HMaster master;
52    private static HBaseTestingUtility TEST_UTIL;
53    private static Configuration conf;
54    private static final int msgInterval = 1000;
55  
56    @BeforeClass
57    public static void startCluster() throws Exception {
58      LOG.info("Starting cluster");
59      TEST_UTIL = new HBaseTestingUtility();
60      conf = TEST_UTIL.getConfiguration();
61  
62      // Disable sanity check for coprocessor
63      conf.setBoolean("hbase.table.sanity.checks", false);
64  
65      // set RIT stuck warning threshold to a small value
66      conf.setInt(HConstants.METRICS_RIT_STUCK_WARNING_THRESHOLD, 20);
67  
68      // set msgInterval to 1 second
69      conf.setInt("hbase.regionserver.msginterval", msgInterval);
70  
71      // set tablesOnMaster to none
72      conf.set("hbase.balancer.tablesOnMaster", "none");
73  
74      TEST_UTIL.startMiniCluster(1);
75      cluster = TEST_UTIL.getHBaseCluster();
76      master = cluster.getMaster();
77    }
78  
79    @AfterClass
80    public static void after() throws Exception {
81      if (TEST_UTIL != null) {
82        TEST_UTIL.shutdownMiniCluster();
83      }
84    }
85  
86    @Test
87    public void testRITAssignmentManagerMetrics() throws Exception {
88  
89      final TableName TABLENAME = TableName.valueOf("testRITMetrics");
90      final byte[] FAMILY = Bytes.toBytes("family");
91  
92      Table table = null;
93      try {
94        table = TEST_UTIL.createTable(TABLENAME, FAMILY);
95  
96        final byte[] row = Bytes.toBytes("row");
97        final byte[] qualifier = Bytes.toBytes("qualifier");
98        final byte[] value = Bytes.toBytes("value");
99  
100       Put put = new Put(row);
101       put.addColumn(FAMILY, qualifier, value);
102       table.put(put);
103 
104       // Sleep 3 seconds, wait for doMetrics chore catching up
105       Thread.sleep(msgInterval * 3);
106 
107       // check the RIT is 0
108       MetricsAssignmentManagerSource amSource =
109           master.getAssignmentManager().getAssignmentManagerMetrics().getMetricsProcSource();
110 
111       metricsHelper.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_NAME, 0, amSource);
112       metricsHelper.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_OVER_THRESHOLD_NAME, 0,
113           amSource);
114 
115       // alter table with a non-existing coprocessor
116       HTableDescriptor htd = new HTableDescriptor(TABLENAME);
117       HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
118 
119       htd.addFamily(hcd);
120 
121       HashMap<String,String> kvs = new HashMap<>();
122       kvs.put("arg1", "1");
123       kvs.put("arg2", "2");
124       htd.addCoprocessor("com.foo.FooRegionObserver", new Path("hdfs:///foo.jar"), 1001, kvs);
125 
126       TEST_UTIL.getHBaseAdmin().modifyTable(TABLENAME, htd);
127 
128       // Sleep 3 seconds, wait for doMetrics chore catching up
129       Thread.sleep(msgInterval * 3);
130       metricsHelper.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_NAME, 1, amSource);
131       metricsHelper.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_OVER_THRESHOLD_NAME, 1,
132           amSource);
133 
134     } finally {
135       if (table != null) {
136         table.close();
137       }
138     }
139   }
140 }