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.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025import static org.junit.Assert.fail; 026 027import java.util.ArrayList; 028import java.util.List; 029import org.apache.hadoop.fs.FileSystem; 030import org.apache.hadoop.fs.FileUtil; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.HBaseClassTestRule; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.TableNotDisabledException; 035import org.apache.hadoop.hbase.TableNotFoundException; 036import org.apache.hadoop.hbase.client.RegionInfo; 037import org.apache.hadoop.hbase.client.Table; 038import org.apache.hadoop.hbase.master.MasterFileSystem; 039import org.apache.hadoop.hbase.procedure2.Procedure; 040import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 041import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 042import org.apache.hadoop.hbase.regionserver.HRegion; 043import org.apache.hadoop.hbase.testclassification.MasterTests; 044import org.apache.hadoop.hbase.testclassification.MediumTests; 045import org.apache.hadoop.hbase.util.Bytes; 046import org.apache.hadoop.hbase.util.CommonFSUtils; 047import org.apache.hadoop.hbase.util.HFileArchiveTestingUtil; 048import org.junit.ClassRule; 049import org.junit.Rule; 050import org.junit.Test; 051import org.junit.experimental.categories.Category; 052import org.junit.rules.TestName; 053import org.slf4j.Logger; 054import org.slf4j.LoggerFactory; 055 056 057@Category({MasterTests.class, MediumTests.class}) 058public class TestDeleteTableProcedure extends TestTableDDLProcedureBase { 059 060 @ClassRule 061 public static final HBaseClassTestRule CLASS_RULE = 062 HBaseClassTestRule.forClass(TestDeleteTableProcedure.class); 063 064 private static final Logger LOG = LoggerFactory.getLogger(TestDeleteTableProcedure.class); 065 @Rule public TestName name = new TestName(); 066 067 @Test(expected=TableNotFoundException.class) 068 public void testDeleteNotExistentTable() throws Exception { 069 final TableName tableName = TableName.valueOf(name.getMethodName()); 070 071 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 072 ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch(); 073 long procId = ProcedureTestingUtility.submitAndWait(procExec, 074 new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch)); 075 latch.await(); 076 } 077 078 @Test(expected=TableNotDisabledException.class) 079 public void testDeleteNotDisabledTable() throws Exception { 080 final TableName tableName = TableName.valueOf(name.getMethodName()); 081 082 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 083 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 084 085 ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch(); 086 long procId = ProcedureTestingUtility.submitAndWait(procExec, 087 new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch)); 088 latch.await(); 089 } 090 091 @Test 092 public void testDeleteDeletedTable() throws Exception { 093 final TableName tableName = TableName.valueOf(name.getMethodName()); 094 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 095 096 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 097 procExec, tableName, null, "f"); 098 UTIL.getAdmin().disableTable(tableName); 099 100 // delete the table (that exists) 101 long procId1 = procExec.submitProcedure( 102 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 103 // delete the table (that will no longer exist) 104 long procId2 = procExec.submitProcedure( 105 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 106 107 // Wait the completion 108 ProcedureTestingUtility.waitProcedure(procExec, procId1); 109 ProcedureTestingUtility.waitProcedure(procExec, procId2); 110 111 // First delete should succeed 112 ProcedureTestingUtility.assertProcNotFailed(procExec, procId1); 113 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 114 115 // Second delete should fail with TableNotFound 116 Procedure<?> result = procExec.getResult(procId2); 117 assertTrue(result.isFailed()); 118 LOG.debug("Delete failed with exception: " + result.getException()); 119 assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotFoundException); 120 } 121 122 @Test 123 public void testSimpleDelete() throws Exception { 124 final TableName tableName = TableName.valueOf(name.getMethodName()); 125 final byte[][] splitKeys = null; 126 testSimpleDelete(tableName, splitKeys); 127 } 128 129 @Test 130 public void testSimpleDeleteWithSplits() throws Exception { 131 final TableName tableName = TableName.valueOf(name.getMethodName()); 132 final byte[][] splitKeys = new byte[][] { 133 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") 134 }; 135 testSimpleDelete(tableName, splitKeys); 136 } 137 138 @Test 139 public void testDeleteFromMeta() throws Exception { 140 final TableName tableName = TableName.valueOf(name.getMethodName()); 141 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 142 getMasterProcedureExecutor(), tableName, null, "f1", "f2"); 143 List<RegionInfo> regionsList = new ArrayList<>(); 144 UTIL.getAdmin().disableTable(tableName); 145 MasterProcedureEnv procedureEnv = getMasterProcedureExecutor().getEnvironment(); 146 assertNotNull("Table should be on TableDescriptors cache.", 147 procedureEnv.getMasterServices().getTableDescriptors().get(tableName)); 148 DeleteTableProcedure.deleteFromMeta(procedureEnv, tableName, regionsList); 149 assertNull("Table shouldn't be on TableDescriptors anymore.", 150 procedureEnv.getMasterServices().getTableDescriptors().get(tableName)); 151 } 152 153 private void testSimpleDelete(final TableName tableName, byte[][] splitKeys) throws Exception { 154 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 155 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2"); 156 UTIL.getAdmin().disableTable(tableName); 157 158 // delete the table 159 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 160 long procId = ProcedureTestingUtility.submitAndWait(procExec, 161 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 162 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 163 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 164 } 165 166 @Test 167 public void testRecoveryAndDoubleExecution() throws Exception { 168 final TableName tableName = TableName.valueOf(name.getMethodName()); 169 170 // create the table 171 byte[][] splitKeys = null; 172 RegionInfo[] regions = MasterProcedureTestingUtility.createTable( 173 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2"); 174 UTIL.getAdmin().disableTable(tableName); 175 176 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 177 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 178 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 179 180 // Start the Delete procedure && kill the executor 181 long procId = procExec.submitProcedure( 182 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 183 184 // Restart the executor and execute the step twice 185 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId); 186 187 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 188 } 189 190 @Test 191 public void testDeleteWhenTempDirIsNotEmpty() throws Exception { 192 final TableName tableName = TableName.valueOf(name.getMethodName()); 193 final String FAM = "fam"; 194 final byte[][] splitKeys = new byte[][] { 195 Bytes.toBytes("b"), Bytes.toBytes("c"), Bytes.toBytes("d") 196 }; 197 198 // create the table 199 MasterProcedureTestingUtility.createTable( 200 getMasterProcedureExecutor(), tableName, splitKeys, FAM); 201 202 // get the current store files for the regions 203 List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName); 204 // make sure we have 4 regions serving this table 205 assertEquals(4, regions.size()); 206 207 // load the table 208 try (Table table = UTIL.getConnection().getTable(tableName)) { 209 UTIL.loadTable(table, Bytes.toBytes(FAM)); 210 } 211 212 // disable the table so that we can manipulate the files 213 UTIL.getAdmin().disableTable(tableName); 214 215 final MasterFileSystem masterFileSystem = 216 UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem(); 217 final Path tableDir = CommonFSUtils.getTableDir(masterFileSystem.getRootDir(), tableName); 218 final Path tempDir = masterFileSystem.getTempDir(); 219 final Path tempTableDir = CommonFSUtils.getTableDir(tempDir, tableName); 220 final FileSystem fs = masterFileSystem.getFileSystem(); 221 222 // copy the table to the temporary directory to make sure the temp directory is not empty 223 if (!FileUtil.copy(fs, tableDir, fs, tempTableDir, false, UTIL.getConfiguration())) { 224 fail(); 225 } 226 227 // delete the table 228 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 229 long procId = ProcedureTestingUtility.submitAndWait(procExec, 230 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 231 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 232 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 233 234 // check if the temporary directory is deleted 235 assertFalse(fs.exists(tempTableDir)); 236 237 // check for the existence of the archive directory 238 for (HRegion region : regions) { 239 Path archiveDir = HFileArchiveTestingUtil.getRegionArchiveDir(UTIL.getConfiguration(), 240 region); 241 assertTrue(fs.exists(archiveDir)); 242 } 243 } 244}