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.assertTrue;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.TableNotDisabledException;
025import org.apache.hadoop.hbase.procedure2.Procedure;
026import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
027import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Category({ MasterTests.class, MediumTests.class })
040public class TestEnableTableProcedure extends TestTableDDLProcedureBase {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestEnableTableProcedure.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestEnableTableProcedure.class);
047
048  @Rule
049  public TestName name = new TestName();
050
051  @Test
052  public void testEnableTable() throws Exception {
053    final TableName tableName = TableName.valueOf(name.getMethodName());
054    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
055
056    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
057    UTIL.getAdmin().disableTable(tableName);
058
059    // Enable the table
060    long procId =
061      procExec.submitProcedure(new EnableTableProcedure(procExec.getEnvironment(), tableName));
062    // Wait the completion
063    ProcedureTestingUtility.waitProcedure(procExec, procId);
064    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
065    MasterProcedureTestingUtility.validateTableIsEnabled(getMaster(), tableName);
066  }
067
068  @Test(expected = TableNotDisabledException.class)
069  public void testEnableNonDisabledTable() throws Exception {
070    final TableName tableName = TableName.valueOf(name.getMethodName());
071    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
072
073    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
074
075    // Enable the table - expect failure
076    long procId1 =
077      procExec.submitProcedure(new EnableTableProcedure(procExec.getEnvironment(), tableName));
078    ProcedureTestingUtility.waitProcedure(procExec, procId1);
079
080    Procedure<?> result = procExec.getResult(procId1);
081    assertTrue(result.isFailed());
082    LOG.debug("Enable failed with exception: " + result.getException());
083    assertTrue(
084      ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotDisabledException);
085
086    // Enable the table - expect failure from ProcedurePrepareLatch
087    final ProcedurePrepareLatch prepareLatch = new ProcedurePrepareLatch.CompatibilityLatch();
088    procExec.submitProcedure(
089      new EnableTableProcedure(procExec.getEnvironment(), tableName, prepareLatch));
090    prepareLatch.await();
091  }
092
093  @Test
094  public void testRecoveryAndDoubleExecution() throws Exception {
095    final TableName tableName = TableName.valueOf(name.getMethodName());
096    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
097
098    final byte[][] splitKeys =
099      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
100    MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2");
101    UTIL.getAdmin().disableTable(tableName);
102    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
103    ProcedureTestingUtility.setKillIfHasParent(procExec, false);
104    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
105
106    // Start the Enable procedure && kill the executor
107    long procId =
108      procExec.submitProcedure(new EnableTableProcedure(procExec.getEnvironment(), tableName));
109
110    // Restart the executor and execute the step twice
111    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
112
113    MasterProcedureTestingUtility.validateTableIsEnabled(getMaster(), tableName);
114  }
115
116  @Test
117  public void testRollbackAndDoubleExecution() throws Exception {
118    final TableName tableName = TableName.valueOf(name.getMethodName());
119    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
120
121    final byte[][] splitKeys =
122      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
123    MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2");
124    UTIL.getAdmin().disableTable(tableName);
125    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
126    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
127
128    // Start the Enable procedure && kill the executor
129    long procId =
130      procExec.submitProcedure(new EnableTableProcedure(procExec.getEnvironment(), tableName));
131
132    int lastStep = 3; // fail before ENABLE_TABLE_SET_ENABLING_TABLE_STATE
133    MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, lastStep);
134    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
135  }
136}