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