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;
021
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.RegionInfo;
024import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
025import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.TruncateTableState;
033
034@Tag(MasterTests.TAG)
035@Tag(MediumTests.TAG)
036public class TestTruncateTableWithMasterFailover extends MasterFailoverWithProceduresTestBase {
037
038  // ==========================================================================
039  // Test Truncate Table
040  // ==========================================================================
041  @Test
042  public void testTruncateWithFailover() throws Exception {
043    // TODO: Should we try every step? (master failover takes long time)
044    // It is already covered by TestTruncateTableProcedure
045    // but without the master restart, only the executor/store is restarted.
046    // Without Master restart we may not find bug in the procedure code
047    // like missing "wait" for resources to be available (e.g. RS)
048    testTruncateWithFailoverAtStep(true, TruncateTableState.TRUNCATE_TABLE_ADD_TO_META.ordinal());
049  }
050
051  private void testTruncateWithFailoverAtStep(final boolean preserveSplits, final int step)
052    throws Exception {
053    final TableName tableName = TableName.valueOf("testTruncateWithFailoverAtStep" + step);
054
055    // create the table
056    final String[] families = new String[] { "f1", "f2" };
057    final byte[][] splitKeys =
058      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
059    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(getMasterProcedureExecutor(),
060      tableName, splitKeys, families);
061    // load and verify that there are rows in the table
062    MasterProcedureTestingUtility.loadData(UTIL.getConnection(), tableName, 100, splitKeys,
063      families);
064    assertEquals(100, UTIL.countRows(tableName));
065    // disable the table
066    UTIL.getAdmin().disableTable(tableName);
067
068    ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
069    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
070
071    // Start the Truncate procedure && kill the executor
072    long procId = procExec.submitProcedure(
073      new TruncateTableProcedure(procExec.getEnvironment(), tableName, preserveSplits));
074    testRecoveryAndDoubleExecution(UTIL, procId, step);
075
076    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
077    UTIL.waitUntilAllRegionsAssigned(tableName);
078
079    // validate the table regions and layout
080    regions = UTIL.getAdmin().getRegions(tableName).toArray(new RegionInfo[0]);
081    if (preserveSplits) {
082      assertEquals(1 + splitKeys.length, regions.length);
083    } else {
084      assertEquals(1, regions.length);
085    }
086    MasterProcedureTestingUtility.validateTableCreation(UTIL.getHBaseCluster().getMaster(),
087      tableName, regions, families);
088
089    // verify that there are no rows in the table
090    assertEquals(0, UTIL.countRows(tableName));
091
092    // verify that the table is read/writable
093    MasterProcedureTestingUtility.loadData(UTIL.getConnection(), tableName, 50, splitKeys,
094      families);
095    assertEquals(50, UTIL.countRows(tableName));
096  }
097}