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 java.io.IOException; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.client.Durability; 027import org.apache.hadoop.hbase.client.Put; 028import org.apache.hadoop.hbase.testclassification.LargeTests; 029import org.apache.hadoop.hbase.testclassification.VerySlowRegionServerTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.apache.hadoop.hbase.wal.WAL; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034 035/** 036 * A test similar to TestHRegion, but with in-memory flush families. Also checks wal truncation 037 * after in-memory compaction. 038 */ 039@Tag(VerySlowRegionServerTests.TAG) 040@Tag(LargeTests.TAG) 041public class TestHRegionWithInMemoryFlush extends TestHRegion { 042 043 /** 044 * @return A region on which you must call {@link HBaseTestingUtil#closeRegionAndWAL(HRegion)} 045 * when done. 046 */ 047 @Override 048 protected HRegion initHRegion(TableName tableName, byte[] startKey, byte[] stopKey, 049 Configuration conf, boolean isReadOnly, Durability durability, WAL wal, byte[]... families) 050 throws IOException { 051 boolean[] inMemory = new boolean[families.length]; 052 for (int i = 0; i < inMemory.length; i++) { 053 inMemory[i] = true; 054 } 055 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null, 056 MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 057 return TEST_UTIL.createLocalHRegionWithInMemoryFlags(tableName, startKey, stopKey, conf, 058 isReadOnly, durability, wal, inMemory, families); 059 } 060 061 /** 062 * A test case of HBASE-21041 063 */ 064 @Override 065 @Test 066 public void testFlushAndMemstoreSizeCounting() throws Exception { 067 byte[] family = Bytes.toBytes("family"); 068 this.region = initHRegion(tableName, method, CONF, family); 069 int count = 0; 070 for (byte[] row : HBaseTestingUtil.ROWS) { 071 Put put = new Put(row); 072 put.addColumn(family, family, row); 073 region.put(put); 074 // In memory flush every 1000 puts 075 if (count++ % 1000 == 0) { 076 ((CompactingMemStore) (region.getStore(family).memstore)).flushInMemory(); 077 } 078 } 079 region.flush(true); 080 // After flush, data size should be zero 081 assertEquals(0, region.getMemStoreDataSize()); 082 // After flush, a new active mutable segment is created, so the heap size 083 // should equal to MutableSegment.DEEP_OVERHEAD 084 assertEquals(MutableSegment.DEEP_OVERHEAD, region.getMemStoreHeapSize()); 085 // After flush, offheap size should be zero 086 assertEquals(0, region.getMemStoreOffHeapSize()); 087 } 088}