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