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.assertTrue; 022 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.client.SnapshotDescription; 027import org.apache.hadoop.hbase.procedure2.Procedure; 028import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 029import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException; 030import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 031import org.apache.hadoop.hbase.testclassification.MasterTests; 032import org.apache.hadoop.hbase.testclassification.MediumTests; 033import org.junit.BeforeClass; 034import org.junit.ClassRule; 035import org.junit.Rule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.junit.rules.TestName; 039 040import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.DeleteTableState; 041 042@Category({ MasterTests.class, MediumTests.class }) 043public class TestDeleteTableProcedureWithRecovery extends TestTableDDLProcedureBase { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestDeleteTableProcedureWithRecovery.class); 048 049 @Rule 050 public TestName name = new TestName(); 051 052 @BeforeClass 053 public static void setupCluster() throws Exception { 054 // Enable recovery snapshots 055 UTIL.getConfiguration().setBoolean(HConstants.SNAPSHOT_BEFORE_DESTRUCTIVE_ACTION_ENABLED_KEY, 056 true); 057 TestTableDDLProcedureBase.setupCluster(); 058 } 059 060 @Test 061 public void testRecoverySnapshotRollback() throws Exception { 062 final TableName tableName = TableName.valueOf(name.getMethodName()); 063 final String[] families = new String[] { "f1", "f2" }; 064 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 065 066 // Create table with data 067 MasterProcedureTestingUtility.createTable(procExec, tableName, null, families); 068 MasterProcedureTestingUtility.loadData(UTIL.getConnection(), tableName, 100, new byte[0][], 069 families); 070 UTIL.getAdmin().disableTable(tableName); 071 072 // Submit the failing procedure 073 long procId = procExec 074 .submitProcedure(new FailingDeleteTableProcedure(procExec.getEnvironment(), tableName)); 075 076 // Wait for procedure to complete (should fail) 077 ProcedureTestingUtility.waitProcedure(procExec, procId); 078 Procedure<MasterProcedureEnv> result = procExec.getResult(procId); 079 assertTrue("Procedure should have failed", result.isFailed()); 080 081 // Verify no recovery snapshots remain after rollback 082 boolean snapshotFound = false; 083 for (SnapshotDescription snapshot : UTIL.getAdmin().listSnapshots()) { 084 if (snapshot.getName().startsWith("auto_" + tableName.getNameAsString())) { 085 snapshotFound = true; 086 break; 087 } 088 } 089 assertTrue("Recovery snapshot should have been cleaned up during rollback", !snapshotFound); 090 } 091 092 @Test 093 public void testRecoverySnapshotAndRestore() throws Exception { 094 final TableName tableName = TableName.valueOf(name.getMethodName()); 095 final TableName restoredTableName = TableName.valueOf(name.getMethodName() + "_restored"); 096 final String[] families = new String[] { "f1", "f2" }; 097 098 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 099 100 // Create table with data 101 MasterProcedureTestingUtility.createTable(procExec, tableName, null, families); 102 MasterProcedureTestingUtility.loadData(UTIL.getConnection(), tableName, 100, new byte[0][], 103 families); 104 UTIL.getAdmin().disableTable(tableName); 105 106 // Delete the table (this should create a recovery snapshot) 107 long procId = ProcedureTestingUtility.submitAndWait(procExec, 108 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 109 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 110 111 // Verify table is deleted 112 MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName); 113 114 // Find the recovery snapshot 115 String recoverySnapshotName = null; 116 for (SnapshotDescription snapshot : UTIL.getAdmin().listSnapshots()) { 117 if (snapshot.getName().startsWith("auto_" + tableName.getNameAsString())) { 118 recoverySnapshotName = snapshot.getName(); 119 break; 120 } 121 } 122 assertTrue("Recovery snapshot should exist", recoverySnapshotName != null); 123 124 // Restore from snapshot by cloning to a new table 125 UTIL.getAdmin().cloneSnapshot(recoverySnapshotName, restoredTableName); 126 UTIL.waitUntilAllRegionsAssigned(restoredTableName); 127 128 // Verify restored table has original data 129 assertEquals(100, UTIL.countRows(restoredTableName)); 130 131 // Clean up the cloned table 132 UTIL.getAdmin().disableTable(restoredTableName); 133 UTIL.getAdmin().deleteTable(restoredTableName); 134 } 135 136 // Create a procedure that will fail after snapshot creation 137 public static class FailingDeleteTableProcedure extends DeleteTableProcedure { 138 private boolean failOnce = false; 139 140 public FailingDeleteTableProcedure() { 141 super(); 142 } 143 144 public FailingDeleteTableProcedure(MasterProcedureEnv env, TableName tableName) { 145 super(env, tableName); 146 } 147 148 @Override 149 protected Flow executeFromState(MasterProcedureEnv env, DeleteTableState state) 150 throws InterruptedException, ProcedureSuspendedException { 151 if (!failOnce && state == DeleteTableState.DELETE_TABLE_CLEAR_FS_LAYOUT) { 152 failOnce = true; 153 throw new RuntimeException("Simulated failure"); 154 } 155 return super.executeFromState(env, state); 156 } 157 } 158 159}