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.assertNotNull;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.CompatibilityFactory;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.test.MetricsAssertHelper;
026import org.apache.hadoop.hbase.testclassification.RegionServerTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.JvmPauseMonitor;
029import org.junit.Before;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035/**
036 * Unit test version of rs metrics tests.
037 */
038@Category({RegionServerTests.class, SmallTests.class})
039public class TestMetricsRegionServer {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043      HBaseClassTestRule.forClass(TestMetricsRegionServer.class);
044
045  public static MetricsAssertHelper HELPER =
046      CompatibilityFactory.getInstance(MetricsAssertHelper.class);
047
048  private MetricsRegionServerWrapperStub wrapper;
049  private MetricsRegionServer rsm;
050  private MetricsRegionServerSource serverSource;
051
052  @BeforeClass
053  public static void classSetUp() {
054    HELPER.init();
055  }
056
057  @Before
058  public void setUp() {
059    wrapper = new MetricsRegionServerWrapperStub();
060    rsm = new MetricsRegionServer(wrapper, new Configuration(false), null);
061    serverSource = rsm.getMetricsSource();
062  }
063
064  @Test
065  public void testWrapperSource() {
066    HELPER.assertTag("serverName", "test", serverSource);
067    HELPER.assertTag("clusterId", "tClusterId", serverSource);
068    HELPER.assertTag("zookeeperQuorum", "zk", serverSource);
069    HELPER.assertGauge("regionServerStartTime", 100, serverSource);
070    HELPER.assertGauge("regionCount", 101, serverSource);
071    HELPER.assertGauge("storeCount", 2, serverSource);
072    HELPER.assertGauge("maxStoreFileAge", 2, serverSource);
073    HELPER.assertGauge("minStoreFileAge", 2, serverSource);
074    HELPER.assertGauge("avgStoreFileAge", 2, serverSource);
075    HELPER.assertGauge("numReferenceFiles", 2, serverSource);
076    HELPER.assertGauge("hlogFileCount", 10, serverSource);
077    HELPER.assertGauge("hlogFileSize", 1024000, serverSource);
078    HELPER.assertGauge("storeFileCount", 300, serverSource);
079    HELPER.assertGauge("memstoreSize", 1025, serverSource);
080    HELPER.assertGauge("storeFileSize", 1900, serverSource);
081    HELPER.assertCounter("totalRequestCount", 899, serverSource);
082    HELPER.assertCounter("totalRowActionRequestCount",
083      HELPER.getCounter("readRequestCount", serverSource)
084          + HELPER.getCounter("writeRequestCount", serverSource),
085      serverSource);
086    HELPER.assertCounter("readRequestCount", 997, serverSource);
087    HELPER.assertCounter("filteredReadRequestCount", 1997, serverSource);
088    HELPER.assertCounter("writeRequestCount", 707, serverSource);
089    HELPER.assertCounter("checkMutateFailedCount", 401, serverSource);
090    HELPER.assertCounter("checkMutatePassedCount", 405, serverSource);
091    HELPER.assertGauge("storeFileIndexSize", 406, serverSource);
092    HELPER.assertGauge("staticIndexSize", 407, serverSource);
093    HELPER.assertGauge("staticBloomSize", 408, serverSource);
094    HELPER.assertGauge("mutationsWithoutWALCount", 409, serverSource);
095    HELPER.assertGauge("mutationsWithoutWALSize", 410, serverSource);
096    HELPER.assertGauge("percentFilesLocal", 99, serverSource);
097    HELPER.assertGauge("percentFilesLocalSecondaryRegions", 99, serverSource);
098    HELPER.assertGauge("compactionQueueLength", 411, serverSource);
099    HELPER.assertGauge("flushQueueLength", 412, serverSource);
100    HELPER.assertGauge("blockCacheFreeSize", 413, serverSource);
101    HELPER.assertGauge("blockCacheCount", 414, serverSource);
102    HELPER.assertGauge("blockCacheSize", 415, serverSource);
103    HELPER.assertCounter("blockCacheHitCount", 416, serverSource);
104    HELPER.assertCounter("blockCacheMissCount", 417, serverSource);
105    HELPER.assertCounter("blockCacheEvictionCount", 418, serverSource);
106    HELPER.assertGauge("blockCacheCountHitPercent", 98, serverSource);
107    HELPER.assertGauge("blockCacheExpressHitPercent", 97, serverSource);
108    HELPER.assertCounter("blockCacheFailedInsertionCount", 36, serverSource);
109    HELPER.assertGauge("l1CacheHitCount", 200, serverSource);
110    HELPER.assertGauge("l1CacheMissCount", 100, serverSource);
111    HELPER.assertGauge("l1CacheHitRatio", 80, serverSource);
112    HELPER.assertGauge("l1CacheMissRatio", 20, serverSource);
113    HELPER.assertGauge("l2CacheHitCount", 800, serverSource);
114    HELPER.assertGauge("l2CacheMissCount", 200, serverSource);
115    HELPER.assertGauge("l2CacheHitRatio", 90, serverSource);
116    HELPER.assertGauge("l2CacheMissRatio", 10, serverSource);
117    HELPER.assertCounter("updatesBlockedTime", 419, serverSource);
118  }
119
120  @Test
121  public void testConstuctor() {
122    assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource() );
123    assertNotNull("The RegionServerMetricsWrapper should be accessable", rsm.getRegionServerWrapper());
124  }
125
126  @Test
127  public void testSlowCount() {
128    for (int i=0; i < 12; i ++) {
129      rsm.updateAppend(null, 12);
130      rsm.updateAppend(null, 1002);
131    }
132    for (int i=0; i < 13; i ++) {
133      rsm.updateDeleteBatch(null, 13);
134      rsm.updateDeleteBatch(null, 1003);
135    }
136    for (int i=0; i < 14; i ++) {
137      rsm.updateGet(null, 14);
138      rsm.updateGet(null, 1004);
139    }
140    for (int i=0; i < 15; i ++) {
141      rsm.updateIncrement(null, 15);
142      rsm.updateIncrement(null, 1005);
143    }
144    for (int i=0; i < 16; i ++) {
145      rsm.updatePutBatch(null, 16);
146      rsm.updatePutBatch(null, 1006);
147    }
148
149    for (int i=0; i < 17; i ++) {
150      rsm.updatePut(null, 17);
151      rsm.updateDelete(null, 17);
152      rsm.updateCheckAndDelete(null, 17);
153      rsm.updateCheckAndPut(null, 17);
154      rsm.updateCheckAndMutate(null, 17);
155    }
156
157    HELPER.assertCounter("appendNumOps", 24, serverSource);
158    HELPER.assertCounter("deleteBatchNumOps", 26, serverSource);
159    HELPER.assertCounter("getNumOps", 28, serverSource);
160    HELPER.assertCounter("incrementNumOps", 30, serverSource);
161    HELPER.assertCounter("putBatchNumOps", 32, serverSource);
162    HELPER.assertCounter("putNumOps", 17, serverSource);
163    HELPER.assertCounter("deleteNumOps", 17, serverSource);
164    HELPER.assertCounter("checkAndDeleteNumOps", 17, serverSource);
165    HELPER.assertCounter("checkAndPutNumOps", 17, serverSource);
166    HELPER.assertCounter("checkAndMutateNumOps", 17, serverSource);
167
168    HELPER.assertCounter("slowAppendCount", 12, serverSource);
169    HELPER.assertCounter("slowDeleteCount", 13, serverSource);
170    HELPER.assertCounter("slowGetCount", 14, serverSource);
171    HELPER.assertCounter("slowIncrementCount", 15, serverSource);
172    HELPER.assertCounter("slowPutCount", 16, serverSource);
173  }
174
175  @Test
176  public void testFlush() {
177    rsm.updateFlush(null, 1, 2, 3);
178    HELPER.assertCounter("flushTime_num_ops", 1, serverSource);
179    HELPER.assertCounter("flushMemstoreSize_num_ops", 1, serverSource);
180    HELPER.assertCounter("flushOutputSize_num_ops", 1, serverSource);
181    HELPER.assertCounter("flushedMemstoreBytes", 2, serverSource);
182    HELPER.assertCounter("flushedOutputBytes", 3, serverSource);
183
184    rsm.updateFlush(null, 10, 20, 30);
185    HELPER.assertCounter("flushTimeNumOps", 2, serverSource);
186    HELPER.assertCounter("flushMemstoreSize_num_ops", 2, serverSource);
187    HELPER.assertCounter("flushOutputSize_num_ops", 2, serverSource);
188    HELPER.assertCounter("flushedMemstoreBytes", 22, serverSource);
189    HELPER.assertCounter("flushedOutputBytes", 33, serverSource);
190  }
191
192  @Test
193  public void testCompaction() {
194    rsm.updateCompaction(null, false, 1, 2, 3, 4, 5);
195    HELPER.assertCounter("compactionTime_num_ops", 1, serverSource);
196    HELPER.assertCounter("compactionInputFileCount_num_ops", 1, serverSource);
197    HELPER.assertCounter("compactionInputSize_num_ops", 1, serverSource);
198    HELPER.assertCounter("compactionOutputFileCount_num_ops", 1, serverSource);
199    HELPER.assertCounter("compactedInputBytes", 4, serverSource);
200    HELPER.assertCounter("compactedoutputBytes", 5, serverSource);
201
202    rsm.updateCompaction(null, false, 10, 20, 30, 40, 50);
203    HELPER.assertCounter("compactionTime_num_ops", 2, serverSource);
204    HELPER.assertCounter("compactionInputFileCount_num_ops", 2, serverSource);
205    HELPER.assertCounter("compactionInputSize_num_ops", 2, serverSource);
206    HELPER.assertCounter("compactionOutputFileCount_num_ops", 2, serverSource);
207    HELPER.assertCounter("compactedInputBytes", 44, serverSource);
208    HELPER.assertCounter("compactedoutputBytes", 55, serverSource);
209
210    // do major compaction
211    rsm.updateCompaction(null, true, 100, 200, 300, 400, 500);
212
213    HELPER.assertCounter("compactionTime_num_ops", 3, serverSource);
214    HELPER.assertCounter("compactionInputFileCount_num_ops", 3, serverSource);
215    HELPER.assertCounter("compactionInputSize_num_ops", 3, serverSource);
216    HELPER.assertCounter("compactionOutputFileCount_num_ops", 3, serverSource);
217    HELPER.assertCounter("compactedInputBytes", 444, serverSource);
218    HELPER.assertCounter("compactedoutputBytes", 555, serverSource);
219
220    HELPER.assertCounter("majorCompactionTime_num_ops", 1, serverSource);
221    HELPER.assertCounter("majorCompactionInputFileCount_num_ops", 1, serverSource);
222    HELPER.assertCounter("majorCompactionInputSize_num_ops", 1, serverSource);
223    HELPER.assertCounter("majorCompactionOutputFileCount_num_ops", 1, serverSource);
224    HELPER.assertCounter("majorCompactedInputBytes", 400, serverSource);
225    HELPER.assertCounter("majorCompactedoutputBytes", 500, serverSource);
226  }
227
228  @Test
229  public void testPauseMonitor() {
230    Configuration conf = new Configuration();
231    conf.setLong(JvmPauseMonitor.INFO_THRESHOLD_KEY, 1000L);
232    conf.setLong(JvmPauseMonitor.WARN_THRESHOLD_KEY, 10000L);
233    JvmPauseMonitor monitor = new JvmPauseMonitor(conf, serverSource);
234    monitor.updateMetrics(1500, false);
235    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
236    HELPER.assertCounter("pauseWarnThresholdExceeded", 0, serverSource);
237    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
238    HELPER.assertCounter("pauseTimeWithGc_num_ops", 0, serverSource);
239    monitor.updateMetrics(15000, true);
240    HELPER.assertCounter("pauseInfoThresholdExceeded", 1, serverSource);
241    HELPER.assertCounter("pauseWarnThresholdExceeded", 1, serverSource);
242    HELPER.assertCounter("pauseTimeWithoutGc_num_ops", 1, serverSource);
243    HELPER.assertCounter("pauseTimeWithGc_num_ops", 1, serverSource);
244  }
245
246}
247