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