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