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