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.TableNotDisabledException; 025import org.apache.hadoop.hbase.TableNotFoundException; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.procedure2.Procedure; 028import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 029import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 030import org.apache.hadoop.hbase.testclassification.MasterTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.ClassRule; 034import org.junit.Rule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037import org.junit.rules.TestName; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041@Category({MasterTests.class, MediumTests.class}) 042public class TestDeleteTableProcedure extends TestTableDDLProcedureBase { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestDeleteTableProcedure.class); 047 048 private static final Logger LOG = LoggerFactory.getLogger(TestDeleteTableProcedure.class); 049 @Rule public TestName name = new TestName(); 050 051 @Test(expected=TableNotFoundException.class) 052 public void testDeleteNotExistentTable() throws Exception { 053 final TableName tableName = TableName.valueOf(name.getMethodName()); 054 055 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 056 ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch(); 057 long procId = ProcedureTestingUtility.submitAndWait(procExec, 058 new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch)); 059 latch.await(); 060 } 061 062 @Test(expected=TableNotDisabledException.class) 063 public void testDeleteNotDisabledTable() throws Exception { 064 final TableName tableName = TableName.valueOf(name.getMethodName()); 065 066 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 067 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 068 069 ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch(); 070 long procId = ProcedureTestingUtility.submitAndWait(procExec, 071 new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch)); 072 latch.await(); 073 } 074 075 @Test 076 public void testDeleteDeletedTable() throws Exception { 077 final TableName tableName = TableName.valueOf(name.getMethodName()); 078 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 079 080 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 081 procExec, tableName, null, "f"); 082 UTIL.getAdmin().disableTable(tableName); 083 084 // delete the table (that exists) 085 long procId1 = procExec.submitProcedure( 086 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 087 // delete the table (that will no longer exist) 088 long procId2 = procExec.submitProcedure( 089 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 090 091 // Wait the completion 092 ProcedureTestingUtility.waitProcedure(procExec, procId1); 093 ProcedureTestingUtility.waitProcedure(procExec, procId2); 094 095 // First delete should succeed 096 ProcedureTestingUtility.assertProcNotFailed(procExec, procId1); 097 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 098 099 // Second delete should fail with TableNotFound 100 Procedure<?> result = procExec.getResult(procId2); 101 assertTrue(result.isFailed()); 102 LOG.debug("Delete failed with exception: " + result.getException()); 103 assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotFoundException); 104 } 105 106 @Test 107 public void testSimpleDelete() throws Exception { 108 final TableName tableName = TableName.valueOf(name.getMethodName()); 109 final byte[][] splitKeys = null; 110 testSimpleDelete(tableName, splitKeys); 111 } 112 113 @Test 114 public void testSimpleDeleteWithSplits() throws Exception { 115 final TableName tableName = TableName.valueOf(name.getMethodName()); 116 final byte[][] splitKeys = new byte[][] { 117 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") 118 }; 119 testSimpleDelete(tableName, splitKeys); 120 } 121 122 private void testSimpleDelete(final TableName tableName, byte[][] splitKeys) throws Exception { 123 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 124 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2"); 125 UTIL.getAdmin().disableTable(tableName); 126 127 // delete the table 128 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 129 long procId = ProcedureTestingUtility.submitAndWait(procExec, 130 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 131 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 132 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 133 } 134 135 @Test 136 public void testRecoveryAndDoubleExecution() throws Exception { 137 final TableName tableName = TableName.valueOf(name.getMethodName()); 138 139 // create the table 140 byte[][] splitKeys = null; 141 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 142 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2"); 143 UTIL.getAdmin().disableTable(tableName); 144 145 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 146 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 147 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 148 149 // Start the Delete procedure && kill the executor 150 long procId = procExec.submitProcedure( 151 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 152 153 // Restart the executor and execute the step twice 154 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId); 155 156 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 157 } 158}