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.assertNotNull; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.NamespaceDescriptor; 029import org.apache.hadoop.hbase.NamespaceNotFoundException; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.TableDescriptor; 032import org.apache.hadoop.hbase.constraint.ConstraintException; 033import org.apache.hadoop.hbase.procedure2.Procedure; 034import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 035import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 036import org.apache.hadoop.hbase.testclassification.MasterTests; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.junit.After; 039import org.junit.AfterClass; 040import org.junit.Before; 041import org.junit.BeforeClass; 042import org.junit.ClassRule; 043import org.junit.Rule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.junit.rules.TestName; 047import org.slf4j.Logger; 048import org.slf4j.LoggerFactory; 049 050@Category({MasterTests.class, MediumTests.class}) 051public class TestDeleteNamespaceProcedure { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestDeleteNamespaceProcedure.class); 056 057 private static final Logger LOG = LoggerFactory.getLogger(TestDeleteNamespaceProcedure.class); 058 059 protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 060 061 @Rule 062 public TestName name = new TestName(); 063 064 private static void setupConf(Configuration conf) { 065 conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1); 066 } 067 068 @BeforeClass 069 public static void setupCluster() throws Exception { 070 setupConf(UTIL.getConfiguration()); 071 UTIL.startMiniCluster(1); 072 } 073 074 @AfterClass 075 public static void cleanupTest() throws Exception { 076 try { 077 UTIL.shutdownMiniCluster(); 078 } catch (Exception e) { 079 LOG.warn("failure shutting down cluster", e); 080 } 081 } 082 083 @Before 084 public void setup() throws Exception { 085 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false); 086 } 087 088 @After 089 public void tearDown() throws Exception { 090 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false); 091 for (TableDescriptor htd: UTIL.getAdmin().listTableDescriptors()) { 092 LOG.info("Tear down, remove table=" + htd.getTableName()); 093 UTIL.deleteTable(htd.getTableName()); 094 } 095 } 096 097 @Test 098 public void testDeleteNamespace() throws Exception { 099 final String namespaceName = "testDeleteNamespace"; 100 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 101 102 createNamespaceForTesting(namespaceName); 103 104 long procId = procExec.submitProcedure( 105 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 106 // Wait the completion 107 ProcedureTestingUtility.waitProcedure(procExec, procId); 108 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 109 110 validateNamespaceNotExist(namespaceName); 111 } 112 113 @Test 114 public void testDeleteNonExistNamespace() throws Exception { 115 final String namespaceName = "testDeleteNonExistNamespace"; 116 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 117 118 validateNamespaceNotExist(namespaceName); 119 120 long procId = procExec.submitProcedure( 121 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 122 // Wait the completion 123 ProcedureTestingUtility.waitProcedure(procExec, procId); 124 // Expect fail with NamespaceNotFoundException 125 Procedure<?> result = procExec.getResult(procId); 126 assertTrue(result.isFailed()); 127 LOG.debug("Delete namespace failed with exception: " + result.getException()); 128 assertTrue( 129 ProcedureTestingUtility.getExceptionCause(result) instanceof NamespaceNotFoundException); 130 } 131 132 @Test 133 public void testDeleteSystemNamespace() throws Exception { 134 final String namespaceName = NamespaceDescriptor.SYSTEM_NAMESPACE.getName(); 135 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 136 137 long procId = procExec.submitProcedure( 138 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 139 // Wait the completion 140 ProcedureTestingUtility.waitProcedure(procExec, procId); 141 Procedure<?> result = procExec.getResult(procId); 142 assertTrue(result.isFailed()); 143 LOG.debug("Delete namespace failed with exception: " + result.getException()); 144 assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException); 145 } 146 147 @Test 148 public void testDeleteNonEmptyNamespace() throws Exception { 149 final String namespaceName = "testDeleteNonExistNamespace"; 150 final TableName tableName = TableName.valueOf("testDeleteNonExistNamespace:" + name.getMethodName()); 151 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 152 // create namespace 153 createNamespaceForTesting(namespaceName); 154 // create the table under the new namespace 155 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1"); 156 157 long procId = procExec.submitProcedure( 158 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 159 // Wait the completion 160 ProcedureTestingUtility.waitProcedure(procExec, procId); 161 Procedure<?> result = procExec.getResult(procId); 162 assertTrue(result.isFailed()); 163 LOG.debug("Delete namespace failed with exception: " + result.getException()); 164 assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException); 165 } 166 167 @Test 168 public void testRecoveryAndDoubleExecution() throws Exception { 169 final String namespaceName = "testRecoveryAndDoubleExecution"; 170 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 171 172 createNamespaceForTesting(namespaceName); 173 174 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 175 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 176 177 // Start the DeleteNamespace procedure && kill the executor 178 long procId = procExec.submitProcedure( 179 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 180 181 // Restart the executor and execute the step twice 182 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId); 183 184 // Validate the deletion of namespace 185 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 186 validateNamespaceNotExist(namespaceName); 187 } 188 189 @Test 190 public void testRollbackAndDoubleExecution() throws Exception { 191 final String namespaceName = "testRollbackAndDoubleExecution"; 192 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 193 194 createNamespaceForTesting(namespaceName); 195 196 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 197 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 198 199 // Start the DeleteNamespace procedure && kill the executor 200 long procId = procExec.submitProcedure( 201 new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 202 203 int lastStep = 2; // failing before DELETE_NAMESPACE_DELETE_FROM_NS_TABLE 204 MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, lastStep); 205 206 // Validate the namespace still exists 207 NamespaceDescriptor createdNsDescriptor= 208 UTIL.getAdmin().getNamespaceDescriptor(namespaceName); 209 assertNotNull(createdNsDescriptor); 210 } 211 212 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 213 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 214 } 215 216 private void createNamespaceForTesting(final String namespaceName) throws Exception { 217 final NamespaceDescriptor nsd = NamespaceDescriptor.create(namespaceName).build(); 218 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 219 220 long procId = procExec.submitProcedure( 221 new CreateNamespaceProcedure(procExec.getEnvironment(), nsd)); 222 // Wait the completion 223 ProcedureTestingUtility.waitProcedure(procExec, procId); 224 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 225 } 226 227 public static void validateNamespaceNotExist(final String nsName) throws IOException { 228 try { 229 NamespaceDescriptor nsDescriptor = UTIL.getAdmin().getNamespaceDescriptor(nsName); 230 assertNull(nsDescriptor); 231 } catch (NamespaceNotFoundException nsnfe) { 232 // Expected 233 } 234 } 235}