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