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