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.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.hadoop.hbase.client.TableDescriptor;
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.ModifyRegionUtils;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CreateTableState;
034
035@Category({ MasterTests.class, MediumTests.class })
036public class TestCreateTableWithMasterFailover extends MasterFailoverWithProceduresTestBase {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestCreateTableWithMasterFailover.class);
041
042  // ==========================================================================
043  // Test Create Table
044  // ==========================================================================
045  @Test
046  public void testCreateWithFailover() throws Exception {
047    // TODO: Should we try every step? (master failover takes long time)
048    // It is already covered by TestCreateTableProcedure
049    // but without the master restart, only the executor/store is restarted.
050    // Without Master restart we may not find bug in the procedure code
051    // like missing "wait" for resources to be available (e.g. RS)
052    testCreateWithFailoverAtStep(CreateTableState.CREATE_TABLE_ASSIGN_REGIONS.ordinal());
053  }
054
055  private void testCreateWithFailoverAtStep(final int step) throws Exception {
056    final TableName tableName = TableName.valueOf("testCreateWithFailoverAtStep" + step);
057
058    // create the table
059    ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
060    ProcedureTestingUtility.setKillBeforeStoreUpdate(procExec, true);
061    ProcedureTestingUtility.setToggleKillBeforeStoreUpdate(procExec, true);
062
063    // Start the Create procedure && kill the executor
064    byte[][] splitKeys = null;
065    TableDescriptor htd = MasterProcedureTestingUtility.createHTD(tableName, "f1", "f2");
066    RegionInfo[] regions = ModifyRegionUtils.createRegionInfos(htd, splitKeys);
067    long procId =
068      procExec.submitProcedure(new CreateTableProcedure(procExec.getEnvironment(), htd, regions));
069    testRecoveryAndDoubleExecution(UTIL, procId, step);
070
071    MasterProcedureTestingUtility.validateTableCreation(UTIL.getHBaseCluster().getMaster(),
072      tableName, regions, "f1", "f2");
073  }
074}