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.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.Assert;
032import org.junit.ClassRule;
033import org.junit.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestName;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040@Category({ MasterTests.class, MediumTests.class })
041public class TestDisableTableProcedure extends TestTableDDLProcedureBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestDisableTableProcedure.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestDisableTableProcedure.class);
048
049  @Rule
050  public TestName name = new TestName();
051
052  @Test
053  public void testDisableTable() throws Exception {
054    final TableName tableName = TableName.valueOf(name.getMethodName());
055    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
056
057    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
058
059    // Disable the table
060    long procId = procExec
061      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
062    // Wait the completion
063    ProcedureTestingUtility.waitProcedure(procExec, procId);
064    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
065    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
066  }
067
068  @Test
069  public void testDisableTableMultipleTimes() 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    // Disable the table
076    long procId1 = procExec
077      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
078    // Wait the completion
079    ProcedureTestingUtility.waitProcedure(procExec, procId1);
080    ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
081    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
082
083    // Disable the table again - expect failure. We used to get it via procExec#getResult but we
084    // added fail fast so now happens on construction.
085    Throwable e = null;
086    Throwable cause = null;
087    try {
088      long procId2 = procExec
089        .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
090      // Wait the completion
091      ProcedureTestingUtility.waitProcedure(procExec, procId2);
092      Procedure<?> result = procExec.getResult(procId2);
093      assertTrue(result.isFailed());
094      cause = ProcedureTestingUtility.getExceptionCause(result);
095      e = result.getException();
096    } catch (TableNotEnabledException tnde) {
097      // Expected.
098      e = tnde;
099      cause = tnde;
100    }
101    LOG.debug("Disable failed with exception {}" + e);
102    assertTrue(cause instanceof TableNotEnabledException);
103
104    // Disable the table - expect failure from ProcedurePrepareLatch
105    try {
106      final ProcedurePrepareLatch prepareLatch = new ProcedurePrepareLatch.CompatibilityLatch();
107
108      long procId3 = procExec.submitProcedure(
109        new DisableTableProcedure(procExec.getEnvironment(), tableName, false, prepareLatch));
110      prepareLatch.await();
111      Assert.fail("Disable should throw exception through latch.");
112    } catch (TableNotEnabledException tnee) {
113      // Expected
114      LOG.debug("Disable failed with expected exception {}", tnee);
115    }
116
117    // Disable the table again with skipping table state check flag (simulate recovery scenario)
118    try {
119      long procId4 = procExec
120        .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, true));
121      // Wait the completion
122      ProcedureTestingUtility.waitProcedure(procExec, procId4);
123      ProcedureTestingUtility.assertProcNotFailed(procExec, procId4);
124    } catch (TableNotEnabledException tnee) {
125      // Expected
126      LOG.debug("Disable failed with expected exception {}", tnee);
127    }
128    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
129  }
130
131  @Test
132  public void testRecoveryAndDoubleExecution() throws Exception {
133    final TableName tableName = TableName.valueOf(name.getMethodName());
134    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
135
136    final byte[][] splitKeys =
137      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
138    MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2");
139
140    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
141
142    // Start the Disable procedure && kill the executor
143    long procId = procExec
144      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
145
146    // Restart the executor and execute the step twice
147    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
148
149    MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName);
150  }
151}