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