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