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.HBaseTestingUtil; 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 HBaseTestingUtil UTIL = new HBaseTestingUtil(); 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 105 .submitProcedure(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 121 .submitProcedure(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 138 .submitProcedure(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 = 151 TableName.valueOf("testDeleteNonExistNamespace:" + name.getMethodName()); 152 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 153 // create namespace 154 createNamespaceForTesting(namespaceName); 155 // create the table under the new namespace 156 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1"); 157 158 long procId = procExec 159 .submitProcedure(new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 160 // Wait the completion 161 ProcedureTestingUtility.waitProcedure(procExec, procId); 162 Procedure<?> result = procExec.getResult(procId); 163 assertTrue(result.isFailed()); 164 LOG.debug("Delete namespace failed with exception: " + result.getException()); 165 assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException); 166 } 167 168 @Test 169 public void testRecoveryAndDoubleExecution() throws Exception { 170 final String namespaceName = "testRecoveryAndDoubleExecution"; 171 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 172 173 createNamespaceForTesting(namespaceName); 174 175 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 176 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 177 178 // Start the DeleteNamespace procedure && kill the executor 179 long procId = procExec 180 .submitProcedure(new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 181 182 // Restart the executor and execute the step twice 183 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId); 184 185 // Validate the deletion of namespace 186 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 187 validateNamespaceNotExist(namespaceName); 188 } 189 190 @Test 191 public void testRollbackAndDoubleExecution() throws Exception { 192 final String namespaceName = "testRollbackAndDoubleExecution"; 193 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 194 195 createNamespaceForTesting(namespaceName); 196 197 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 198 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 199 200 // Start the DeleteNamespace procedure && kill the executor 201 long procId = procExec 202 .submitProcedure(new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName)); 203 204 int lastStep = 2; // failing before DELETE_NAMESPACE_DELETE_FROM_NS_TABLE 205 MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, lastStep); 206 207 // Validate the namespace still exists 208 NamespaceDescriptor createdNsDescriptor = 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 = 221 procExec.submitProcedure(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}