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 public TestName name = new TestName(); 050 051 @Test 052 public void testDisableTable() 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 058 // Disable the table 059 long procId = procExec.submitProcedure( 060 new DisableTableProcedure(procExec.getEnvironment(), tableName, false)); 061 // Wait the completion 062 ProcedureTestingUtility.waitProcedure(procExec, procId); 063 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 064 MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName); 065 } 066 067 @Test 068 public void testDisableTableMultipleTimes() 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 // Disable the table 075 long procId1 = procExec.submitProcedure(new DisableTableProcedure( 076 procExec.getEnvironment(), tableName, false)); 077 // Wait the completion 078 ProcedureTestingUtility.waitProcedure(procExec, procId1); 079 ProcedureTestingUtility.assertProcNotFailed(procExec, procId1); 080 MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName); 081 082 // Disable the table again - expect failure. We used to get it via procExec#getResult but we 083 // added fail fast so now happens on construction. 084 Throwable e = null; 085 Throwable cause = null; 086 try { 087 long procId2 = procExec.submitProcedure(new DisableTableProcedure( 088 procExec.getEnvironment(), tableName, false)); 089 // Wait the completion 090 ProcedureTestingUtility.waitProcedure(procExec, procId2); 091 Procedure<?> result = procExec.getResult(procId2); 092 assertTrue(result.isFailed()); 093 cause = ProcedureTestingUtility.getExceptionCause(result); 094 e = result.getException(); 095 } catch (TableNotEnabledException tnde) { 096 // Expected. 097 e = tnde; 098 cause = tnde; 099 } 100 LOG.debug("Disable failed with exception {}" + e); 101 assertTrue(cause instanceof TableNotEnabledException); 102 103 // Disable the table - expect failure from ProcedurePrepareLatch 104 try { 105 final ProcedurePrepareLatch prepareLatch = new ProcedurePrepareLatch.CompatibilityLatch(); 106 107 long procId3 = procExec.submitProcedure(new DisableTableProcedure( 108 procExec.getEnvironment(), tableName, false, prepareLatch)); 109 prepareLatch.await(); 110 Assert.fail("Disable should throw exception through latch."); 111 } catch (TableNotEnabledException tnee) { 112 // Expected 113 LOG.debug("Disable failed with expected exception {}", tnee); 114 } 115 116 // Disable the table again with skipping table state check flag (simulate recovery scenario) 117 try { 118 long procId4 = procExec.submitProcedure(new DisableTableProcedure( 119 procExec.getEnvironment(), tableName, true)); 120 // Wait the completion 121 ProcedureTestingUtility.waitProcedure(procExec, procId4); 122 ProcedureTestingUtility.assertProcNotFailed(procExec, procId4); 123 } catch (TableNotEnabledException tnee) { 124 // Expected 125 LOG.debug("Disable failed with expected exception {}", tnee); 126 } 127 MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName); 128 } 129 130 @Test 131 public void testRecoveryAndDoubleExecution() throws Exception { 132 final TableName tableName = TableName.valueOf(name.getMethodName()); 133 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 134 135 final byte[][] splitKeys = new byte[][] { 136 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") 137 }; 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.submitProcedure( 144 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}