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;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.CompatibilityFactory;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.regionserver.metrics.MetricsTableRequests;
030import org.apache.hadoop.hbase.test.MetricsAssertHelper;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.JvmPauseMonitor;
034import org.junit.Before;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040/**
041 * Unit test version of rs metrics tests.
042 */
043@Category({ RegionServerTests.class, SmallTests.class })
044public class TestMetricsRegionServer {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestMetricsRegionServer.class);
049
050  public static MetricsAssertHelper HELPER =
051    CompatibilityFactory.getInstance(MetricsAssertHelper.class);
052
053  private MetricsRegionServerWrapperStub wrapper;
054  private MetricsRegionServer rsm;
055  private MetricsRegionServerSource serverSource;
056
057  @BeforeClass
058  public static void classSetUp() {
059    HELPER.init();
060  }
061
062  @Before
063  public void setUp() {
064    wrapper = new MetricsRegionServerWrapperStub();
065    rsm = new MetricsRegionServer(wrapper, new Configuration(false), 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("memstoreHeapSize", 500, serverSource);
087    HELPER.assertGauge("memstoreOffHeapSize", 600, serverSource);
088    HELPER.assertGauge("storeFileSize", 1900, serverSource);
089    HELPER.assertGauge("storeFileSizeGrowthRate", 50.0, serverSource);
090    HELPER.assertCounter("totalRequestCount", 899, serverSource);
091    HELPER.assertCounter("totalRowActionRequestCount",
092      HELPER.getCounter("readRequestCount", serverSource)
093        + HELPER.getCounter("writeRequestCount", serverSource),
094      serverSource);
095    HELPER.assertCounter("readRequestCount", 997, serverSource);
096    HELPER.assertCounter("cpRequestCount", 998, serverSource);
097    HELPER.assertCounter("filteredReadRequestCount", 1997, serverSource);
098    HELPER.assertCounter("writeRequestCount", 707, serverSource);
099    HELPER.assertCounter("checkMutateFailedCount", 401, serverSource);
100    HELPER.assertCounter("checkMutatePassedCount", 405, serverSource);
101    HELPER.assertGauge("storeFileIndexSize", 406, serverSource);
102    HELPER.assertGauge("staticIndexSize", 407, serverSource);
103    HELPER.assertGauge("staticBloomSize", 408, serverSource);
104    HELPER.assertGauge("mutationsWithoutWALCount", 409, serverSource);
105    HELPER.assertGauge("mutationsWithoutWALSize", 410, serverSource);
106    HELPER.assertCounter("bloomFilterRequestsCount", 411, serverSource);
107    HELPER.assertCounter("bloomFilterNegativeResultsCount", 412, serverSource);
108    HELPER.assertCounter("bloomFilterEligibleRequestsCount", 413, serverSource);
109    HELPER.assertGauge("percentFilesLocal", 99, serverSource);
110    HELPER.assertGauge("percentFilesLocalSecondaryRegions", 99, serverSource);
111    HELPER.assertGauge("compactionQueueLength", 411, serverSource);
112    HELPER.assertGauge("flushQueueLength", 412, serverSource);
113    HELPER.assertGauge("blockCacheFreeSize", 413, serverSource);
114    HELPER.assertGauge("blockCacheCount", 414, serverSource);
115    HELPER.assertGauge("blockCacheDataBlockCount", 300, serverSource);
116    HELPER.assertGauge("blockCacheSize", 415, serverSource);
117    HELPER.assertCounter("blockCacheHitCount", 416, serverSource);
118    HELPER.assertCounter("blockCacheHitCachingCount", 16, serverSource);
119    HELPER.assertCounter("blockCacheMissCount", 417, serverSource);
120    HELPER.assertCounter("blockCacheMissCachingCount", 17, serverSource);
121    HELPER.assertCounter("blockCacheEvictionCount", 418, serverSource);
122    HELPER.assertGauge("blockCacheCountHitPercent", 98, serverSource);
123    HELPER.assertGauge("blockCacheExpressHitPercent", 97, serverSource);
124    HELPER.assertCounter("blockCacheFailedInsertionCount", 36, serverSource);
125    HELPER.assertGauge("l1CacheFreeSize", 100, serverSource);
126    HELPER.assertGauge("l1CacheSize", 123, serverSource);
127    HELPER.assertGauge("l1CacheCount", 50, serverSource);
128    HELPER.assertCounter("l1CacheEvictionCount", 1000, serverSource);
129    HELPER.assertGauge("l1CacheHitCount", 200, serverSource);
130    HELPER.assertGauge("l1CacheMissCount", 100, serverSource);
131    HELPER.assertGauge("l1CacheHitRatio", 80, serverSource);
132    HELPER.assertGauge("l1CacheMissRatio", 20, serverSource);
133    HELPER.assertGauge("l2CacheFreeSize", 200, serverSource);
134    HELPER.assertGauge("l2CacheSize", 456, serverSource);
135    HELPER.assertGauge("l2CacheCount", 75, serverSource);
136    HELPER.assertCounter("l2CacheEvictionCount", 2000, serverSource);
137    HELPER.assertGauge("l2CacheHitCount", 800, serverSource);
138    HELPER.assertGauge("l2CacheMissCount", 200, serverSource);
139    HELPER.assertGauge("l2CacheHitRatio", 90, serverSource);
140    HELPER.assertGauge("l2CacheMissRatio", 10, serverSource);
141    HELPER.assertCounter("updatesBlockedTime", 419, serverSource);
142  }
143
144  @Test
145  public void testConstuctor() {
146    assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource());
147    assertNotNull("The RegionServerMetricsWrapper should be accessable",
148      rsm.getRegionServerWrapper());
149  }
150
151  @Test
152  public void testSlowCount() {
153    HRegion region = mock(HRegion.class);
154    MetricsTableRequests metricsTableRequests = mock(MetricsTableRequests.class);
155    when(region.getMetricsTableRequests()).thenReturn(metricsTableRequests);
156    when(metricsTableRequests.isEnableTableLatenciesMetrics()).thenReturn(false);
157    when(metricsTableRequests.isEnabTableQueryMeterMetrics()).thenReturn(false);
158    for (int i = 0; i < 12; i++) {
159      rsm.updateAppend(region, 12, 120);
160      rsm.updateAppend(region, 1002, 10020);
161    }
162    for (int i = 0; i < 13; i++) {
163      rsm.updateDeleteBatch(region, 13);
164      rsm.updateDeleteBatch(region, 1003);
165    }
166    for (int i = 0; i < 14; i++) {
167      rsm.updateGet(region, 14, 140);
168      rsm.updateGet(region, 1004, 10040);
169    }
170    for (int i = 0; i < 15; i++) {
171      rsm.updateIncrement(region, 15, 150);
172      rsm.updateIncrement(region, 1005, 10050);
173    }
174    for (int i = 0; i < 16; i++) {
175      rsm.updatePutBatch(region, 16);
176      rsm.updatePutBatch(region, 1006);
177    }
178
179    for (int i = 0; i < 17; i++) {
180      rsm.updatePut(region, 17);
181      rsm.updateDelete(region, 17);
182      rsm.updatePut(region, 1006);
183      rsm.updateDelete(region, 1003);
184      rsm.updateCheckAndDelete(region, 17);
185      rsm.updateCheckAndPut(region, 17);
186      rsm.updateCheckAndMutate(region, 17, 170);
187    }
188
189    HELPER.assertCounter("blockBytesScannedCount", 420090, serverSource);
190    HELPER.assertCounter("appendNumOps", 24, serverSource);
191    HELPER.assertCounter("appendBlockBytesScannedNumOps", 24, serverSource);
192    HELPER.assertCounter("deleteBatchNumOps", 26, serverSource);
193    HELPER.assertCounter("getNumOps", 28, serverSource);
194    HELPER.assertCounter("getBlockBytesScannedNumOps", 28, serverSource);
195    HELPER.assertCounter("incrementNumOps", 30, serverSource);
196    HELPER.assertCounter("incrementBlockBytesScannedNumOps", 30, serverSource);
197    HELPER.assertCounter("putBatchNumOps", 32, serverSource);
198    HELPER.assertCounter("putNumOps", 34, serverSource);
199    HELPER.assertCounter("deleteNumOps", 34, serverSource);
200    HELPER.assertCounter("checkAndDeleteNumOps", 17, serverSource);
201    HELPER.assertCounter("checkAndPutNumOps", 17, serverSource);
202    HELPER.assertCounter("checkAndMutateNumOps", 17, serverSource);
203    HELPER.assertCounter("checkAndMutateBlockBytesScannedNumOps", 17, serverSource);
204
205    HELPER.assertCounter("slowAppendCount", 12, serverSource);
206    HELPER.assertCounter("slowDeleteCount", 17, serverSource);
207    HELPER.assertCounter("slowGetCount", 14, serverSource);
208    HELPER.assertCounter("slowIncrementCount", 15, serverSource);
209    HELPER.assertCounter("slowPutCount", 17, serverSource);
210  }
211
212  @Test
213  public void testFlush() {
214    rsm.updateFlush(null, 1, 2, 3);
215    HELPER.assertCounter("flushTime_num_ops", 1, serverSource);
216    HELPER.assertCounter("flushMemstoreSize_num_ops", 1, serverSource);
217    HELPER.assertCounter("flushOutputSize_num_ops", 1, serverSource);
218    HELPER.assertCounter("flushedMemstoreBytes", 2, serverSource);
219    HELPER.assertCounter("flushedOutputBytes", 3, serverSource);
220
221    rsm.updateFlush(null, 10, 20, 30);
222    HELPER.assertCounter("flushTimeNumOps", 2, serverSource);
223    HELPER.assertCounter("flushMemstoreSize_num_ops", 2, serverSource);
224    HELPER.assertCounter("flushOutputSize_num_ops", 2, serverSource);
225    HELPER.assertCounter("flushedMemstoreBytes", 22, serverSource);
226    HELPER.assertCounter("flushedOutputBytes", 33, serverSource);
227  }
228
229  @Test
230  public void testCompaction() {
231    rsm.updateCompaction(null, false, 1, 2, 3, 4, 5);
232    HELPER.assertCounter("compactionTime_num_ops", 1, serverSource);
233    HELPER.assertCounter("compactionInputFileCount_num_ops", 1, serverSource);
234    HELPER.assertCounter("compactionInputSize_num_ops", 1, serverSource);
235    HELPER.assertCounter("compactionOutputFileCount_num_ops", 1, serverSource);
236    HELPER.assertCounter("compactedInputBytes", 4, serverSource);
237    HELPER.assertCounter("compactedoutputBytes", 5, serverSource);
238
239    rsm.updateCompaction(null, false, 10, 20, 30, 40, 50);
240    HELPER.assertCounter("compactionTime_num_ops", 2, serverSource);
241    HELPER.assertCounter("compactionInputFileCount_num_ops", 2, serverSource);
242    HELPER.assertCounter("compactionInputSize_num_ops", 2, serverSource);
243    HELPER.assertCounter("compactionOutputFileCount_num_ops", 2, serverSource);
244    HELPER.assertCounter("compactedInputBytes", 44, serverSource);
245    HELPER.assertCounter("compactedoutputBytes", 55, serverSource);
246
247    // do major compaction
248    rsm.updateCompaction(null, true, 100, 200, 300, 400, 500);
249
250    HELPER.assertCounter("compactionTime_num_ops", 3, serverSource);
251    HELPER.assertCounter("compactionInputFileCount_num_ops", 3, serverSource);
252    HELPER.assertCounter("compactionInputSize_num_ops", 3, serverSource);
253    HELPER.assertCounter("compactionOutputFileCount_num_ops", 3, serverSource);
254    HELPER.assertCounter("compactedInputBytes", 444, serverSource);
255    HELPER.assertCounter("compactedoutputBytes", 555, serverSource);
256
257    HELPER.assertCounter("majorCompactionTime_num_ops", 1, serverSource);
258    HELPER.assertCounter("majorCompactionInputFileCount_num_ops", 1, serverSource);
259    HELPER.assertCounter("majorCompactionInputSize_num_ops", 1, serverSource);
260    HELPER.assertCounter("majorCompactionOutputFileCount_num_ops", 1, serverSource);
261    HELPER.assertCounter("majorCompactedInputBytes", 400, serverSource);
262    HELPER.assertCounter("majorCompactedoutputBytes", 500, serverSource);
263  }
264
265  @Test
266  public void testPauseMonitor() {
267    Configuration conf = new Configuration();
268    conf.setLong(JvmPauseMonitor.INFO_THRESHOLD_KEY, 1000L);
269    conf.setLong(JvmPauseMonitor.WARN_THRESHOLD_KEY, 10000L);
270    JvmPauseMonitor monitor = new JvmPauseMonitor(conf, serverSource);
271    monitor.updateMetrics(1500, false);
272    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
273    HELPER.assertCounter("pauseWarnThresholdExceeded", 0, serverSource);
274    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
275    HELPER.assertCounter("pauseTimeWithGc_num_ops", 0, serverSource);
276    monitor.updateMetrics(15000, true);
277    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
278    HELPER.assertCounter("pauseWarnThresholdExceeded", 1, serverSource);
279    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
280    HELPER.assertCounter("pauseTimeWithGc_num_ops", 1, serverSource);
281  }
282
283  @Test
284  public void testScannerMetrics() {
285    HELPER.assertCounter("scannerLeaseExpiredCount", 0, serverSource);
286    rsm.incrScannerLeaseExpired();
287    HELPER.assertCounter("scannerLeaseExpiredCount", 1, serverSource);
288    HELPER.assertGauge("activeScanners", 0, serverSource);
289  }
290
291  @Test
292  public void testTableQueryMeterSwitch() {
293    HRegion region = mock(HRegion.class);
294    MetricsTableRequests metricsTableRequests = mock(MetricsTableRequests.class);
295    when(region.getMetricsTableRequests()).thenReturn(metricsTableRequests);
296    when(metricsTableRequests.isEnableTableLatenciesMetrics()).thenReturn(false);
297    when(metricsTableRequests.isEnabTableQueryMeterMetrics()).thenReturn(false);
298    Configuration conf = new Configuration(false);
299    // disable
300    rsm.updateReadQueryMeter(region, 500L);
301    assertFalse(HELPER.checkGaugeExists("ServerReadQueryPerSecond_count", serverSource));
302    rsm.updateWriteQueryMeter(region, 500L);
303    assertFalse(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
304
305    // enable
306    conf.setBoolean(MetricsRegionServer.RS_ENABLE_SERVER_QUERY_METER_METRICS_KEY, true);
307    rsm = new MetricsRegionServer(wrapper, conf, null);
308    serverSource = rsm.getMetricsSource();
309    rsm.updateReadQueryMeter(region, 500L);
310    assertTrue(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
311    HELPER.assertGauge("ServerReadQueryPerSecond_count", 500L, serverSource);
312    assertTrue(HELPER.checkGaugeExists("ServerWriteQueryPerSecond_count", serverSource));
313    rsm.updateWriteQueryMeter(region, 500L);
314    HELPER.assertGauge("ServerWriteQueryPerSecond_count", 500L, serverSource);
315  }
316}