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.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.io.util.MemorySizeUtil;
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@Category(SmallTests.class)
033public class TestRegionServerAccounting {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037      HBaseClassTestRule.forClass(TestRegionServerAccounting.class);
038
039  private final static float DEFAULT_MEMSTORE_SIZE = 0.2f;
040
041  private static Configuration conf;
042
043  @Before
044  public void setUpConf() {
045    conf = HBaseConfiguration.create();
046    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, DEFAULT_MEMSTORE_SIZE);
047  }
048
049  @Test
050  public void testOnheapMemstoreHigherWaterMarkLimits() {
051    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
052    long dataSize = regionServerAccounting.getGlobalMemStoreLimit();
053    MemStoreSize memstoreSize = new MemStoreSize(dataSize, dataSize, 0, 0);
054    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
055    assertEquals(FlushType.ABOVE_ONHEAP_HIGHER_MARK, regionServerAccounting.isAboveHighWaterMark());
056  }
057
058  @Test
059  public void testOnheapMemstoreLowerWaterMarkLimits() {
060    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
061    long dataSize = regionServerAccounting.getGlobalMemStoreLimit();
062    MemStoreSize memstoreSize = new MemStoreSize(dataSize, dataSize, 0, 0);
063    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
064    assertEquals(FlushType.ABOVE_ONHEAP_LOWER_MARK, regionServerAccounting.isAboveLowWaterMark());
065  }
066
067  @Test
068  public void testOffheapMemstoreHigherWaterMarkLimitsDueToDataSize() {
069    // setting 1G as offheap data size
070    conf.setLong(MemorySizeUtil.OFFHEAP_MEMSTORE_SIZE_KEY, (1L * 1024L));
071    // try for default cases
072    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
073    // this will breach offheap limit as data size is higher and not due to heap size
074    MemStoreSize memstoreSize =
075        new MemStoreSize((3L * 1024L * 1024L * 1024L), 0, (1L * 1024L * 1024L * 1024L), 100);
076    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
077    assertEquals(FlushType.ABOVE_OFFHEAP_HIGHER_MARK,
078      regionServerAccounting.isAboveHighWaterMark());
079  }
080
081  @Test
082  public void testOffheapMemstoreHigherWaterMarkLimitsDueToHeapSize() {
083    // setting 1G as offheap data size
084    conf.setLong(MemorySizeUtil.OFFHEAP_MEMSTORE_SIZE_KEY, (1L * 1024L));
085    // try for default cases
086    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
087    // this will breach higher limit as heap size is higher and not due to offheap size
088    long dataSize = regionServerAccounting.getGlobalOnHeapMemStoreLimit();
089    MemStoreSize memstoreSize = new MemStoreSize(dataSize, dataSize, 0, 100);
090    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
091    assertEquals(FlushType.ABOVE_ONHEAP_HIGHER_MARK, regionServerAccounting.isAboveHighWaterMark());
092  }
093
094  @Test
095  public void testOffheapMemstoreLowerWaterMarkLimitsDueToDataSize() {
096    // setting 1G as offheap data size
097    conf.setLong(MemorySizeUtil.OFFHEAP_MEMSTORE_SIZE_KEY, (1L * 1024L));
098    // try for default cases
099    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
100    // this will breach offheap limit as data size is higher and not due to heap size
101    MemStoreSize memstoreSize =
102        new MemStoreSize((3L * 1024L * 1024L * 1024L), 0, (1L * 1024L * 1024L * 1024L), 100);
103    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
104    assertEquals(FlushType.ABOVE_OFFHEAP_LOWER_MARK, regionServerAccounting.isAboveLowWaterMark());
105  }
106
107  @Test
108  public void testOffheapMemstoreLowerWaterMarkLimitsDueToHeapSize() {
109    // setting 1G as offheap data size
110    conf.setLong(MemorySizeUtil.OFFHEAP_MEMSTORE_SIZE_KEY, (1L * 1024L));
111    // try for default cases
112    RegionServerAccounting regionServerAccounting = new RegionServerAccounting(conf);
113    // this will breach higher limit as heap size is higher and not due to offheap size
114    long dataSize = regionServerAccounting.getGlobalOnHeapMemStoreLimit();
115    MemStoreSize memstoreSize = new MemStoreSize(dataSize, dataSize, 0, 100);
116    regionServerAccounting.incGlobalMemStoreSize(memstoreSize);
117    assertEquals(FlushType.ABOVE_ONHEAP_LOWER_MARK, regionServerAccounting.isAboveLowWaterMark());
118  }
119}