HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestMetricsTableAggregate.java
blob98a412cf59bfa5e0bf78c8fee8a05ee6ecbd1a19
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.regionserver;
20 import static org.junit.Assert.assertTrue;
21 import java.io.IOException;
22 import java.util.concurrent.CyclicBarrier;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicBoolean;
25 import java.util.stream.IntStream;
26 import java.util.stream.Stream;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.CompatibilityFactory;
29 import org.apache.hadoop.hbase.HBaseClassTestRule;
30 import org.apache.hadoop.hbase.test.MetricsAssertHelper;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 @Category({ RegionServerTests.class, MediumTests.class })
42 public class TestMetricsTableAggregate {
44 @ClassRule
45 public static final HBaseClassTestRule CLASS_RULE =
46 HBaseClassTestRule.forClass(TestMetricsTableAggregate.class);
48 private static final Logger LOG = LoggerFactory.getLogger(TestMetricsTableAggregate.class);
50 private static MetricsAssertHelper HELPER =
51 CompatibilityFactory.getInstance(MetricsAssertHelper.class);
53 private String tableName = "testTableMetrics";
54 private String pre = "Namespace_default_table_" + tableName + "_metric_";
56 private MetricsTableWrapperStub tableWrapper;
57 private MetricsTable mt;
58 private MetricsRegionServerWrapper rsWrapper;
59 private MetricsRegionServer rsm;
60 private MetricsTableAggregateSource agg;
62 @BeforeClass
63 public static void classSetUp() {
64 HELPER.init();
67 @Before
68 public void setUp() {
69 tableWrapper = new MetricsTableWrapperStub(tableName);
70 mt = new MetricsTable(tableWrapper);
71 rsWrapper = new MetricsRegionServerWrapperStub();
72 Configuration conf = new Configuration();
73 rsm = new MetricsRegionServer(rsWrapper, conf, mt);
74 agg = mt.getTableSourceAgg();
77 @Test
78 public void testRequestMetrics() throws IOException {
79 HELPER.assertCounter(pre + "readRequestCount", 10, agg);
80 HELPER.assertCounter(pre + "writeRequestCount", 20, agg);
81 HELPER.assertCounter(pre + "totalRequestCount", 30, agg);
84 @Test
85 public void testRegionAndStoreMetrics() throws IOException {
86 HELPER.assertGauge(pre + "memstoreSize", 1000, agg);
87 HELPER.assertGauge(pre + "storeFileSize", 2000, agg);
88 HELPER.assertGauge(pre + "tableSize", 3000, agg);
90 HELPER.assertGauge(pre + "regionCount", 11, agg);
91 HELPER.assertGauge(pre + "storeCount", 22, agg);
92 HELPER.assertGauge(pre + "storeFileCount", 33, agg);
93 HELPER.assertGauge(pre + "maxStoreFileAge", 44, agg);
94 HELPER.assertGauge(pre + "minStoreFileAge", 55, agg);
95 HELPER.assertGauge(pre + "avgStoreFileAge", 66, agg);
96 HELPER.assertGauge(pre + "numReferenceFiles", 77, agg);
97 HELPER.assertGauge(pre + "averageRegionSize", 88, agg);
100 @Test
101 public void testFlush() {
102 rsm.updateFlush(tableName, 1, 2, 3);
103 HELPER.assertCounter(pre + "flushTime_num_ops", 1, agg);
104 HELPER.assertCounter(pre + "flushMemstoreSize_num_ops", 1, agg);
105 HELPER.assertCounter(pre + "flushOutputSize_num_ops", 1, agg);
106 HELPER.assertCounter(pre + "flushedMemstoreBytes", 2, agg);
107 HELPER.assertCounter(pre + "flushedOutputBytes", 3, agg);
109 rsm.updateFlush(tableName, 10, 20, 30);
110 HELPER.assertCounter(pre + "flushTime_num_ops", 2, agg);
111 HELPER.assertCounter(pre + "flushMemstoreSize_num_ops", 2, agg);
112 HELPER.assertCounter(pre + "flushOutputSize_num_ops", 2, agg);
113 HELPER.assertCounter(pre + "flushedMemstoreBytes", 22, agg);
114 HELPER.assertCounter(pre + "flushedOutputBytes", 33, agg);
117 @Test
118 public void testCompaction() {
119 rsm.updateCompaction(tableName, false, 1, 2, 3, 4, 5);
120 HELPER.assertCounter(pre + "compactionTime_num_ops", 1, agg);
121 HELPER.assertCounter(pre + "compactionInputFileCount_num_ops", 1, agg);
122 HELPER.assertCounter(pre + "compactionInputSize_num_ops", 1, agg);
123 HELPER.assertCounter(pre + "compactionOutputFileCount_num_ops", 1, agg);
124 HELPER.assertCounter(pre + "compactedInputBytes", 4, agg);
125 HELPER.assertCounter(pre + "compactedoutputBytes", 5, agg);
127 rsm.updateCompaction(tableName, false, 10, 20, 30, 40, 50);
128 HELPER.assertCounter(pre + "compactionTime_num_ops", 2, agg);
129 HELPER.assertCounter(pre + "compactionInputFileCount_num_ops", 2, agg);
130 HELPER.assertCounter(pre + "compactionInputSize_num_ops", 2, agg);
131 HELPER.assertCounter(pre + "compactionOutputFileCount_num_ops", 2, agg);
132 HELPER.assertCounter(pre + "compactedInputBytes", 44, agg);
133 HELPER.assertCounter(pre + "compactedoutputBytes", 55, agg);
135 // do major compaction
136 rsm.updateCompaction(tableName, true, 100, 200, 300, 400, 500);
138 HELPER.assertCounter(pre + "compactionTime_num_ops", 3, agg);
139 HELPER.assertCounter(pre + "compactionInputFileCount_num_ops", 3, agg);
140 HELPER.assertCounter(pre + "compactionInputSize_num_ops", 3, agg);
141 HELPER.assertCounter(pre + "compactionOutputFileCount_num_ops", 3, agg);
142 HELPER.assertCounter(pre + "compactedInputBytes", 444, agg);
143 HELPER.assertCounter(pre + "compactedoutputBytes", 555, agg);
145 HELPER.assertCounter(pre + "majorCompactionTime_num_ops", 1, agg);
146 HELPER.assertCounter(pre + "majorCompactionInputFileCount_num_ops", 1, agg);
147 HELPER.assertCounter(pre + "majorCompactionInputSize_num_ops", 1, agg);
148 HELPER.assertCounter(pre + "majorCompactionOutputFileCount_num_ops", 1, agg);
149 HELPER.assertCounter(pre + "majorCompactedInputBytes", 400, agg);
150 HELPER.assertCounter(pre + "majorCompactedoutputBytes", 500, agg);
153 private void update(AtomicBoolean succ, int round, CyclicBarrier barrier) {
154 try {
155 for (int i = 0; i < round; i++) {
156 String tn = tableName + "-" + i;
157 barrier.await(10, TimeUnit.SECONDS);
158 rsm.updateFlush(tn, 100, 1000, 500);
160 } catch (Exception e) {
161 LOG.warn("Failed to update metrics", e);
162 succ.set(false);
166 @Test
167 public void testConcurrentUpdate() throws InterruptedException {
168 int threadNumber = 10;
169 int round = 100;
170 AtomicBoolean succ = new AtomicBoolean(true);
171 CyclicBarrier barrier = new CyclicBarrier(threadNumber);
172 Thread[] threads = IntStream.range(0, threadNumber)
173 .mapToObj(i -> new Thread(() -> update(succ, round, barrier), "Update-Worker-" + i))
174 .toArray(Thread[]::new);
175 Stream.of(threads).forEach(Thread::start);
176 for (Thread t : threads) {
177 t.join();
179 assertTrue(succ.get());