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.io.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertThrows;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.Before;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MiscTests.class, SmallTests.class })
034public class TestMemorySizeUtil {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestMemorySizeUtil.class);
039
040  private Configuration conf;
041
042  @Before
043  public void setup() {
044    conf = new Configuration();
045  }
046
047  @Test
048  public void testValidateRegionServerHeapMemoryAllocation() {
049    // when memstore size + block cache size + default free heap min size == 1.0
050    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, 0.4f);
051    conf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.4f);
052    assertEquals(HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD, 0.2f, 0.0f);
053    MemorySizeUtil.validateRegionServerHeapMemoryAllocation(conf);
054
055    // when memstore size + block cache size + default free heap min size > 1.0
056    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, 0.5f);
057    assertThrows(RuntimeException.class,
058      () -> MemorySizeUtil.validateRegionServerHeapMemoryAllocation(conf));
059
060    // when free heap min size is set to 0, it should not throw an exception
061    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, 0.5f);
062    conf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.5f);
063    conf.setLong(MemorySizeUtil.HBASE_REGION_SERVER_FREE_HEAP_MIN_MEMORY_SIZE_KEY, 0L);
064    MemorySizeUtil.validateRegionServerHeapMemoryAllocation(conf);
065
066    // when free heap min size is set to a negative value, it should be regarded as default value
067    conf.setLong(MemorySizeUtil.HBASE_REGION_SERVER_FREE_HEAP_MIN_MEMORY_SIZE_KEY, -1024L);
068    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, 0.4f);
069    conf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.4f);
070    MemorySizeUtil.validateRegionServerHeapMemoryAllocation(conf);
071
072    conf.setFloat(MemorySizeUtil.MEMSTORE_SIZE_KEY, 0.41f);
073    assertThrows(RuntimeException.class,
074      () -> MemorySizeUtil.validateRegionServerHeapMemoryAllocation(conf));
075  }
076
077  @Test
078  public void testGetRegionServerMinFreeHeapFraction() {
079    // when setting is not set, it should return the default value
080    conf.set(MemorySizeUtil.HBASE_REGION_SERVER_FREE_HEAP_MIN_MEMORY_SIZE_KEY, "");
081    float minFreeHeapFraction = MemorySizeUtil.getRegionServerMinFreeHeapFraction(conf);
082    assertEquals(HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD, minFreeHeapFraction, 0.0f);
083
084    // when setting to 0, it should return 0.0f
085    conf.set(MemorySizeUtil.HBASE_REGION_SERVER_FREE_HEAP_MIN_MEMORY_SIZE_KEY, "0");
086    minFreeHeapFraction = MemorySizeUtil.getRegionServerMinFreeHeapFraction(conf);
087    assertEquals(0.0f, minFreeHeapFraction, 0.0f);
088  }
089}