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.master.procedure;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HBaseTestingUtility;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.Table;
024import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
025import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher;
026import org.apache.hadoop.hbase.regionserver.HRegion;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.hadoop.hbase.util.RegionSplitter;
029import org.junit.After;
030import org.junit.Assert;
031import org.junit.Before;
032
033public class TestFlushTableProcedureBase {
034
035  protected static HBaseTestingUtility TEST_UTIL;
036
037  protected TableName TABLE_NAME;
038  protected byte[] FAMILY1;
039  protected byte[] FAMILY2;
040  protected byte[] FAMILY3;
041
042  @Before
043  public void setup() throws Exception {
044    TEST_UTIL = new HBaseTestingUtility();
045    addConfiguration(TEST_UTIL.getConfiguration());
046    TEST_UTIL.startMiniCluster(3);
047    TABLE_NAME = TableName.valueOf(Bytes.toBytes("TestFlushTable"));
048    FAMILY1 = Bytes.toBytes("cf1");
049    FAMILY2 = Bytes.toBytes("cf2");
050    FAMILY3 = Bytes.toBytes("cf3");
051    final byte[][] splitKeys = new RegionSplitter.HexStringSplit().split(10);
052    Table table =
053      TEST_UTIL.createTable(TABLE_NAME, new byte[][] { FAMILY1, FAMILY2, FAMILY3 }, splitKeys);
054    TEST_UTIL.loadTable(table, FAMILY1, false);
055    TEST_UTIL.loadTable(table, FAMILY2, false);
056    TEST_UTIL.loadTable(table, FAMILY3, false);
057  }
058
059  protected void addConfiguration(Configuration config) {
060    // delay dispatch so that we can do something, for example kill a target server
061    config.setInt(RemoteProcedureDispatcher.DISPATCH_DELAY_CONF_KEY, 10000);
062    config.setInt(RemoteProcedureDispatcher.DISPATCH_MAX_QUEUE_SIZE_CONF_KEY, 128);
063  }
064
065  protected void assertTableMemStoreNotEmpty() {
066    long totalSize = TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).stream()
067      .mapToLong(HRegion::getMemStoreDataSize).sum();
068    Assert.assertTrue(totalSize > 0);
069  }
070
071  protected void assertTableMemStoreEmpty() {
072    long totalSize = TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).stream()
073      .mapToLong(HRegion::getMemStoreDataSize).sum();
074    Assert.assertEquals(0, totalSize);
075  }
076
077  protected void assertColumnFamilyMemStoreNotEmpty(byte[] columnFamily) {
078    long totalSize = TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).stream()
079      .mapToLong(r -> r.getStore(columnFamily).getMemStoreSize().getDataSize()).sum();
080    Assert.assertTrue(totalSize > 0);
081  }
082
083  protected void assertColumnFamilyMemStoreEmpty(byte[] columnFamily) {
084    long totalSize = TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).stream()
085      .mapToLong(r -> r.getStore(columnFamily).getMemStoreSize().getDataSize()).sum();
086    Assert.assertEquals(0, totalSize);
087  }
088
089  @After
090  public void teardown() throws Exception {
091    if (TEST_UTIL.getHBaseCluster().getMaster() != null) {
092      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(
093        TEST_UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(), false);
094    }
095    TEST_UTIL.shutdownMiniCluster();
096  }
097}