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.assertTrue;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.TableNotEnabledException;
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 TestDisableTableProcedure extends TestTableDDLProcedureBase {
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestDisableTableProcedure.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 testDisableTable() 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
069    // Disable the table
070    long procId = procExec
071      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
072    // Wait the completion
073    ProcedureTestingUtility.waitProcedure(procExec, procId);
074    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
075    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
076  }
077
078  @Test
079  public void testDisableTableMultipleTimes() throws Exception {
080    final TableName tableName = TableName.valueOf(testMethodName);
081    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
082
083    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
084
085    // Disable the table
086    long procId1 = procExec
087      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
088    // Wait the completion
089    ProcedureTestingUtility.waitProcedure(procExec, procId1);
090    ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
091    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
092
093    // Disable the table again - expect failure. We used to get it via procExec#getResult but we
094    // added fail fast so now happens on construction.
095    Throwable e = null;
096    Throwable cause = null;
097    try {
098      long procId2 = procExec
099        .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
100      // Wait the completion
101      ProcedureTestingUtility.waitProcedure(procExec, procId2);
102      Procedure<?> result = procExec.getResult(procId2);
103      assertTrue(result.isFailed());
104      cause = ProcedureTestingUtility.getExceptionCause(result);
105      e = result.getException();
106    } catch (TableNotEnabledException tnde) {
107      // Expected.
108      e = tnde;
109      cause = tnde;
110    }
111    LOG.debug("Disable failed with exception {}" + e);
112    assertTrue(cause instanceof TableNotEnabledException);
113
114    // Disable the table - expect failure from ProcedurePrepareLatch
115    try {
116      final ProcedurePrepareLatch prepareLatch = new ProcedurePrepareLatch.CompatibilityLatch();
117
118      long procId3 = procExec.submitProcedure(
119        new DisableTableProcedure(procExec.getEnvironment(), tableName, false, prepareLatch));
120      prepareLatch.await();
121      fail("Disable should throw exception through latch.");
122    } catch (TableNotEnabledException tnee) {
123      // Expected
124      LOG.debug("Disable failed with expected exception {}", tnee);
125    }
126
127    // Disable the table again with skipping table state check flag (simulate recovery scenario)
128    try {
129      long procId4 = procExec
130        .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, true));
131      // Wait the completion
132      ProcedureTestingUtility.waitProcedure(procExec, procId4);
133      ProcedureTestingUtility.assertProcNotFailed(procExec, procId4);
134    } catch (TableNotEnabledException tnee) {
135      // Expected
136      LOG.debug("Disable failed with expected exception {}", tnee);
137    }
138    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
139  }
140
141  @Test
142  public void testRecoveryAndDoubleExecution() throws Exception {
143    final TableName tableName = TableName.valueOf(testMethodName);
144    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
145
146    final byte[][] splitKeys =
147      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
148    MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2");
149
150    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
151
152    // Start the Disable procedure && kill the executor
153    long procId = procExec
154      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
155
156    // Restart the executor and execute the step twice
157    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
158
159    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
160  }
161}