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.hbase.CompatibilitySingletonFactory;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.test.MetricsAssertHelper;
025import org.apache.hadoop.hbase.testclassification.RegionServerTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.Before;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032/**
033 * Unit test version of rs metrics tests.
034 */
035@Category({ RegionServerTests.class, SmallTests.class })
036public class TestMetricsHeapMemoryManager {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestMetricsHeapMemoryManager.class);
041
042  public static MetricsAssertHelper HELPER = CompatibilitySingletonFactory
043      .getInstance(MetricsAssertHelper.class);
044
045  private MetricsHeapMemoryManager hmm;
046  private MetricsHeapMemoryManagerSource source;
047
048  @Before
049  public void setUp() {
050    hmm = new MetricsHeapMemoryManager();
051    source = hmm.getMetricsSource();
052  }
053
054  @Test
055  public void testConstuctor() {
056    assertNotNull("There should be a hadoop1/hadoop2 metrics source", source);
057  }
058
059  @Test
060  public void testCounter() {
061    for (int i = 0; i < 10; i++) {
062      hmm.increaseAboveHeapOccupancyLowWatermarkCounter();
063    }
064    for (int i = 0; i < 11; i++) {
065      hmm.increaseTunerDoNothingCounter();
066    }
067
068    HELPER.assertCounter("aboveHeapOccupancyLowWaterMarkCounter", 10L, source);
069    HELPER.assertCounter("tunerDoNothingCounter", 11L, source);
070  }
071
072  @Test
073  public void testGauge() {
074    hmm.updateBlockedFlushCount(200);
075    hmm.updateUnblockedFlushCount(50);
076    hmm.setCurMemStoreSizeGauge(256 * 1024 * 1024);
077    hmm.setCurBlockCacheSizeGauge(100 * 1024 * 1024);
078
079    HELPER.assertGauge("blockedFlushGauge", 200, source);
080    HELPER.assertGauge("unblockedFlushGauge", 50, source);
081    HELPER.assertGauge("memStoreSize", 256 * 1024 * 1024, source);
082    HELPER.assertGauge("blockCacheSize", 100 * 1024 * 1024, source);
083  }
084}