001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.CompatibilityFactory;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.test.MetricsAssertHelper;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.JvmPauseMonitor;
032import org.junit.Before;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038/**
039 * Unit test version of rs metrics tests.
040 */
041@Category({ RegionServerTests.class, SmallTests.class })
042public class TestMetricsRegionServer {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestMetricsRegionServer.class);
047
048  public static MetricsAssertHelper HELPER =
049    CompatibilityFactory.getInstance(MetricsAssertHelper.class);
050
051  private MetricsRegionServerWrapperStub wrapper;
052  private MetricsRegionServer rsm;
053  private MetricsRegionServerSource serverSource;
054
055  @BeforeClass
056  public static void classSetUp() {
057    HELPER.init();
058  }
059
060  @Before
061  public void setUp() {
062    wrapper = new MetricsRegionServerWrapperStub();
063    Configuration conf = new Configuration(false);
064    conf.setBoolean(MetricsRegionServer.RS_ENABLE_SERVER_QUERY_METER_METRICS_KEY, false);
065    rsm = new MetricsRegionServer(wrapper, conf, null);
066    serverSource = rsm.getMetricsSource();
067  }
068
069  @Test
070  public void testWrapperSource() {
071    HELPER.assertTag("serverName", "test", serverSource);
072    HELPER.assertTag("clusterId", "tClusterId", serverSource);
073    HELPER.assertTag("zookeeperQuorum", "zk", serverSource);
074    HELPER.assertGauge("regionServerStartTime", 100, serverSource);
075    HELPER.assertGauge("regionCount", 101, serverSource);
076    HELPER.assertGauge("storeCount", 2, serverSource);
077    HELPER.assertGauge("maxStoreFileCount", 23, serverSource);
078    HELPER.assertGauge("maxStoreFileAge", 2, serverSource);
079    HELPER.assertGauge("minStoreFileAge", 2, serverSource);
080    HELPER.assertGauge("avgStoreFileAge", 2, serverSource);
081    HELPER.assertGauge("numReferenceFiles", 2, serverSource);
082    HELPER.assertGauge("hlogFileCount", 10, serverSource);
083    HELPER.assertGauge("hlogFileSize", 1024000, serverSource);
084    HELPER.assertGauge("storeFileCount", 300, serverSource);
085    HELPER.assertGauge("memstoreSize", 1025, serverSource);
086    HELPER.assertGauge("storeFileSize", 1900, serverSource);
087    HELPER.assertCounter("totalRequestCount", 899, serverSource);
088    HELPER.assertCounter("totalRowActionRequestCount",
089      HELPER.getCounter("readRequestCount", serverSource)
090        + HELPER.getCounter("writeRequestCount", serverSource),
091      serverSource);
092    HELPER.assertCounter("readRequestCount", 997, serverSource);
093    HELPER.assertCounter("filteredReadRequestCount", 1997, serverSource);
094    HELPER.assertCounter("writeRequestCount", 707, serverSource);
095    HELPER.assertCounter("checkMutateFailedCount", 401, serverSource);
096    HELPER.assertCounter("checkMutatePassedCount", 405, serverSource);
097    HELPER.assertGauge("storeFileIndexSize", 406, serverSource);
098    HELPER.assertGauge("staticIndexSize", 407, serverSource);
099    HELPER.assertGauge("staticBloomSize", 408, serverSource);
100    HELPER.assertGauge("mutationsWithoutWALCount", 409, serverSource);
101    HELPER.assertGauge("mutationsWithoutWALSize", 410, serverSource);
102    HELPER.assertCounter("bloomFilterRequestsCount", 411, serverSource);
103    HELPER.assertCounter("bloomFilterNegativeResultsCount", 412, serverSource);
104    HELPER.assertCounter("bloomFilterEligibleRequestsCount", 413, serverSource);
105    HELPER.assertGauge("percentFilesLocal", 99, serverSource);
106    HELPER.assertGauge("percentFilesLocalSecondaryRegions", 99, serverSource);
107    HELPER.assertGauge("compactionQueueLength", 411, serverSource);
108    HELPER.assertGauge("flushQueueLength", 412, serverSource);
109    HELPER.assertGauge("blockCacheFreeSize", 413, serverSource);
110    HELPER.assertGauge("blockCacheCount", 414, serverSource);
111    HELPER.assertGauge("blockCacheDataBlockCount", 300, serverSource);
112    HELPER.assertGauge("blockCacheSize", 415, serverSource);
113    HELPER.assertCounter("blockCacheHitCount", 416, serverSource);
114    HELPER.assertCounter("blockCacheMissCount", 417, serverSource);
115    HELPER.assertCounter("blockCacheEvictionCount", 418, serverSource);
116    HELPER.assertGauge("blockCacheCountHitPercent", 98, serverSource);
117    HELPER.assertGauge("blockCacheExpressHitPercent", 97, serverSource);
118    HELPER.assertCounter("blockCacheFailedInsertionCount", 36, serverSource);
119    HELPER.assertGauge("l1CacheFreeSize", 100, serverSource);
120    HELPER.assertGauge("l1CacheSize", 123, serverSource);
121    HELPER.assertGauge("l1CacheCount", 50, serverSource);
122    HELPER.assertCounter("l1CacheEvictionCount", 1000, serverSource);
123    HELPER.assertGauge("l1CacheHitCount", 200, serverSource);
124    HELPER.assertGauge("l1CacheMissCount", 100, serverSource);
125    HELPER.assertGauge("l1CacheHitRatio", 80, serverSource);
126    HELPER.assertGauge("l1CacheMissRatio", 20, serverSource);
127    HELPER.assertGauge("l2CacheFreeSize", 200, serverSource);
128    HELPER.assertGauge("l2CacheSize", 456, serverSource);
129    HELPER.assertGauge("l2CacheCount", 75, serverSource);
130    HELPER.assertCounter("l2CacheEvictionCount", 2000, serverSource);
131    HELPER.assertGauge("l2CacheHitCount", 800, serverSource);
132    HELPER.assertGauge("l2CacheMissCount", 200, serverSource);
133    HELPER.assertGauge("l2CacheHitRatio", 90, serverSource);
134    HELPER.assertGauge("l2CacheMissRatio", 10, serverSource);
135    HELPER.assertCounter("updatesBlockedTime", 419, serverSource);
136  }
137
138  @Test
139  public void testConstuctor() {
140    assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource());
141    assertNotNull("The RegionServerMetricsWrapper should be accessable",
142      rsm.getRegionServerWrapper());
143  }
144
145  @Test
146  public void testSlowCount() {
147    for (int i = 0; i < 12; i++) {
148      rsm.updateAppend(null, 12);
149      rsm.updateAppend(null, 1002);
150    }
151    for (int i = 0; i < 13; i++) {
152      rsm.updateDeleteBatch(null, 13);
153      rsm.updateDeleteBatch(null, 1003);
154    }
155    for (int i = 0; i < 14; i++) {
156      rsm.updateGet(null, 14);
157      rsm.updateGet(null, 1004);
158    }
159    for (int i = 0; i < 15; i++) {
160      rsm.updateIncrement(null, 15);
161      rsm.updateIncrement(null, 1005);
162    }
163    for (int i = 0; i < 16; i++) {
164      rsm.updatePutBatch(null, 16);
165      rsm.updatePutBatch(null, 1006);
166    }
167
168    for (int i = 0; i < 17; i++) {
169      rsm.updatePut(null, 17);
170      rsm.updateDelete(null, 17);
171      rsm.updatePut(null, 1006);
172      rsm.updateDelete(null, 1003);
173      rsm.updateCheckAndDelete(null, 17);
174      rsm.updateCheckAndPut(null, 17);
175      rsm.updateCheckAndMutate(null, 17);
176    }
177
178    HELPER.assertCounter("appendNumOps", 24, serverSource);
179    HELPER.assertCounter("deleteBatchNumOps", 26, serverSource);
180    HELPER.assertCounter("getNumOps", 28, serverSource);
181    HELPER.assertCounter("incrementNumOps", 30, serverSource);
182    HELPER.assertCounter("putBatchNumOps", 32, serverSource);
183    HELPER.assertCounter("putNumOps", 34, serverSource);
184    HELPER.assertCounter("deleteNumOps", 34, serverSource);
185    HELPER.assertCounter("checkAndDeleteNumOps", 17, serverSource);
186    HELPER.assertCounter("checkAndPutNumOps", 17, serverSource);
187    HELPER.assertCounter("checkAndMutateNumOps", 17, serverSource);
188
189    HELPER.assertCounter("slowAppendCount", 12, serverSource);
190    HELPER.assertCounter("slowDeleteCount", 17, serverSource);
191    HELPER.assertCounter("slowGetCount", 14, serverSource);
192    HELPER.assertCounter("slowIncrementCount", 15, serverSource);
193    HELPER.assertCounter("slowPutCount", 17, serverSource);
194  }
195
196  @Test
197  public void testFlush() {
198    rsm.updateFlush(null, 1, 2, 3);
199    HELPER.assertCounter("flushTime_num_ops", 1, serverSource);
200    HELPER.assertCounter("flushMemstoreSize_num_ops", 1, serverSource);
201    HELPER.assertCounter("flushOutputSize_num_ops", 1, serverSource);
202    HELPER.assertCounter("flushedMemstoreBytes", 2, serverSource);
203    HELPER.assertCounter("flushedOutputBytes", 3, serverSource);
204
205    rsm.updateFlush(null, 10, 20, 30);
206    HELPER.assertCounter("flushTimeNumOps", 2, serverSource);
207    HELPER.assertCounter("flushMemstoreSize_num_ops", 2, serverSource);
208    HELPER.assertCounter("flushOutputSize_num_ops", 2, serverSource);
209    HELPER.assertCounter("flushedMemstoreBytes", 22, serverSource);
210    HELPER.assertCounter("flushedOutputBytes", 33, serverSource);
211  }
212
213  @Test
214  public void testCompaction() {
215    rsm.updateCompaction(null, false, 1, 2, 3, 4, 5);
216    HELPER.assertCounter("compactionTime_num_ops", 1, serverSource);
217    HELPER.assertCounter("compactionInputFileCount_num_ops", 1, serverSource);
218    HELPER.assertCounter("compactionInputSize_num_ops", 1, serverSource);
219    HELPER.assertCounter("compactionOutputFileCount_num_ops", 1, serverSource);
220    HELPER.assertCounter("compactedInputBytes", 4, serverSource);
221    HELPER.assertCounter("compactedoutputBytes", 5, serverSource);
222
223    rsm.updateCompaction(null, false, 10, 20, 30, 40, 50);
224    HELPER.assertCounter("compactionTime_num_ops", 2, serverSource);
225    HELPER.assertCounter("compactionInputFileCount_num_ops", 2, serverSource);
226    HELPER.assertCounter("compactionInputSize_num_ops", 2, serverSource);
227    HELPER.assertCounter("compactionOutputFileCount_num_ops", 2, serverSource);
228    HELPER.assertCounter("compactedInputBytes", 44, serverSource);
229    HELPER.assertCounter("compactedoutputBytes", 55, serverSource);
230
231    // do major compaction
232    rsm.updateCompaction(null, true, 100, 200, 300, 400, 500);
233
234    HELPER.assertCounter("compactionTime_num_ops", 3, serverSource);
235    HELPER.assertCounter("compactionInputFileCount_num_ops", 3, serverSource);
236    HELPER.assertCounter("compactionInputSize_num_ops", 3, serverSource);
237    HELPER.assertCounter("compactionOutputFileCount_num_ops", 3, serverSource);
238    HELPER.assertCounter("compactedInputBytes", 444, serverSource);
239    HELPER.assertCounter("compactedoutputBytes", 555, serverSource);
240
241    HELPER.assertCounter("majorCompactionTime_num_ops", 1, serverSource);
242    HELPER.assertCounter("majorCompactionInputFileCount_num_ops", 1, serverSource);
243    HELPER.assertCounter("majorCompactionInputSize_num_ops", 1, serverSource);
244    HELPER.assertCounter("majorCompactionOutputFileCount_num_ops", 1, serverSource);
245    HELPER.assertCounter("majorCompactedInputBytes", 400, serverSource);
246    HELPER.assertCounter("majorCompactedoutputBytes", 500, serverSource);
247  }
248
249  @Test
250  public void testPauseMonitor() {
251    Configuration conf = new Configuration();
252    conf.setLong(JvmPauseMonitor.INFO_THRESHOLD_KEY, 1000L);
253    conf.setLong(JvmPauseMonitor.WARN_THRESHOLD_KEY, 10000L);
254    JvmPauseMonitor monitor = new JvmPauseMonitor(conf, serverSource);
255    monitor.updateMetrics(1500, false);
256    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
257    HELPER.assertCounter("pauseWarnThresholdExceeded", 0, serverSource);
258    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
259    HELPER.assertCounter("pauseTimeWithGc_num_ops", 0, serverSource);
260    monitor.updateMetrics(15000, true);
261    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
262    HELPER.assertCounter("pauseWarnThresholdExceeded", 1, serverSource);
263    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
264    HELPER.assertCounter("pauseTimeWithGc_num_ops", 1, serverSource);
265  }
266
267  @Test
268  public void testTableQueryMeterSwitch() {
269    TableName tn1 = TableName.valueOf("table1");
270    // has been set disable in setUp()
271    rsm.updateReadQueryMeter(tn1, 500L);
272    assertFalse(HELPER.checkGaugeExists("ServerReadQueryPerSecond_count", serverSource));
273    rsm.updateWriteQueryMeter(tn1, 500L);
274    assertFalse(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
275
276    // enable
277    Configuration conf = new Configuration(false);
278    conf.setBoolean(MetricsRegionServer.RS_ENABLE_SERVER_QUERY_METER_METRICS_KEY, true);
279    rsm = new MetricsRegionServer(wrapper, conf, null);
280    serverSource = rsm.getMetricsSource();
281    rsm.updateReadQueryMeter(tn1, 500L);
282    assertTrue(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
283    HELPER.assertGauge("ServerReadQueryPerSecond_count", 500L, serverSource);
284    assertTrue(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
285    rsm.updateWriteQueryMeter(tn1, 500L);
286    HELPER.assertGauge("ServerWriteQueryPerSecond_count", 500L, serverSource);
287  }
288
289  @Test
290  public void testScannerMetrics() {
291    HELPER.assertCounter("scannerLeaseExpiredCount", 0, serverSource);
292    rsm.incrScannerLeaseExpired();
293    HELPER.assertCounter("scannerLeaseExpiredCount", 1, serverSource);
294    HELPER.assertGauge("activeScanners", 0, serverSource);
295  }
296
297}