HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestAssignmentManagerMetrics.java
blobfa99663d326f1bd237b4dff4a62db74dbabe7eb9
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.fail;
22 import java.io.IOException;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.CompatibilityFactory;
25 import org.apache.hadoop.hbase.HBaseClassTestRule;
26 import org.apache.hadoop.hbase.HBaseTestingUtil;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
31 import org.apache.hadoop.hbase.client.CoprocessorDescriptorBuilder;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Table;
34 import org.apache.hadoop.hbase.client.TableDescriptor;
35 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
36 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
37 import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
38 import org.apache.hadoop.hbase.test.MetricsAssertHelper;
39 import org.apache.hadoop.hbase.testclassification.MasterTests;
40 import org.apache.hadoop.hbase.testclassification.MediumTests;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.apache.hadoop.hbase.util.TableDescriptorChecker;
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.ClassRule;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
49 import org.junit.rules.TestName;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 @Category({ MasterTests.class, MediumTests.class })
54 public class TestAssignmentManagerMetrics {
56 @ClassRule
57 public static final HBaseClassTestRule CLASS_RULE =
58 HBaseClassTestRule.forClass(TestAssignmentManagerMetrics.class);
60 private static final Logger LOG = LoggerFactory.getLogger(TestAssignmentManagerMetrics.class);
61 private static final MetricsAssertHelper METRICS_HELPER = CompatibilityFactory
62 .getInstance(MetricsAssertHelper.class);
64 private static SingleProcessHBaseCluster CLUSTER;
65 private static HMaster MASTER;
66 private static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
67 private static final int MSG_INTERVAL = 1000;
69 @Rule
70 public TestName name = new TestName();
72 @BeforeClass
73 public static void startCluster() throws Exception {
74 LOG.info("Starting cluster");
75 Configuration conf = TEST_UTIL.getConfiguration();
77 // Disable sanity check for coprocessor
78 conf.setBoolean(TableDescriptorChecker.TABLE_SANITY_CHECKS, false);
80 // set RIT stuck warning threshold to a small value
81 conf.setInt(HConstants.METRICS_RIT_STUCK_WARNING_THRESHOLD, 20);
83 // set msgInterval to 1 second
84 conf.setInt("hbase.regionserver.msginterval", MSG_INTERVAL);
86 // set tablesOnMaster to none
87 conf.set("hbase.balancer.tablesOnMaster", "none");
89 // set client sync wait timeout to 5sec
90 conf.setInt("hbase.client.sync.wait.timeout.msec", 5000);
91 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
92 conf.setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 2500);
93 // set a small interval for updating rit metrics
94 conf.setInt(AssignmentManager.RIT_CHORE_INTERVAL_MSEC_CONF_KEY, MSG_INTERVAL);
95 // set a small assign attempts for avoiding assert when retrying. (HBASE-20533)
96 conf.setInt(AssignmentManager.ASSIGN_MAX_ATTEMPTS, 3);
98 // keep rs online so it can report the failed opens.
99 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false);
100 TEST_UTIL.startMiniCluster(1);
101 CLUSTER = TEST_UTIL.getHBaseCluster();
102 MASTER = CLUSTER.getMaster();
105 @AfterClass
106 public static void after() throws Exception {
107 LOG.info("AFTER {} <= IS THIS NULL?", TEST_UTIL);
108 TEST_UTIL.shutdownMiniCluster();
111 @Test
112 public void testRITAssignmentManagerMetrics() throws Exception {
113 final TableName TABLENAME = TableName.valueOf(name.getMethodName());
114 final byte[] FAMILY = Bytes.toBytes("family");
115 try (Table table = TEST_UTIL.createTable(TABLENAME, FAMILY)){
116 final byte[] row = Bytes.toBytes("row");
117 final byte[] qualifier = Bytes.toBytes("qualifier");
118 final byte[] value = Bytes.toBytes("value");
120 Put put = new Put(row);
121 put.addColumn(FAMILY, qualifier, value);
122 table.put(put);
124 // Sleep 3 seconds, wait for doMetrics chore catching up
125 Thread.sleep(MSG_INTERVAL * 3);
127 // check the RIT is 0
128 MetricsAssignmentManagerSource amSource =
129 MASTER.getAssignmentManager().getAssignmentManagerMetrics().getMetricsProcSource();
131 METRICS_HELPER.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_NAME, 0, amSource);
132 METRICS_HELPER.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_OVER_THRESHOLD_NAME, 0,
133 amSource);
135 // alter table with a non-existing coprocessor
137 TableDescriptor htd = TableDescriptorBuilder.newBuilder(TABLENAME)
138 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
139 .setCoprocessor(CoprocessorDescriptorBuilder.newBuilder("com.foo.FooRegionObserver")
140 .setJarPath("hdfs:///foo.jar")
141 .setPriority(1001)
142 .setProperty("arg1", "1")
143 .setProperty("arg2", "2")
144 .build())
145 .build();
146 try {
147 TEST_UTIL.getAdmin().modifyTable(htd);
148 fail("Expected region failed to open");
149 } catch (IOException e) {
150 // expected, the RS will crash and the assignment will spin forever waiting for a RS
151 // to assign the region. the region will not go to FAILED_OPEN because in this case
152 // we have just one RS and it will do one retry.
153 LOG.info("Expected error", e);
156 // Sleep 5 seconds, wait for doMetrics chore catching up
157 // the rit count consists of rit and failed opens. see RegionInTransitionStat#update
158 // Waiting for the completion of rit makes the assert stable.
159 TEST_UTIL.waitUntilNoRegionsInTransition();
160 Thread.sleep(MSG_INTERVAL * 5);
161 METRICS_HELPER.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_NAME, 1, amSource);
162 METRICS_HELPER.assertGauge(MetricsAssignmentManagerSource.RIT_COUNT_OVER_THRESHOLD_NAME, 1,
163 amSource);
164 METRICS_HELPER.assertCounter(MetricsAssignmentManagerSource.ASSIGN_METRIC_PREFIX
165 + "SubmittedCount", 2, amSource);