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    conf.setInt(MasterProcedureConstants.MASTER_URGENT_PROCEDURE_THREADS, 0);
067  }
068
069  @BeforeClass
070  public static void setupCluster() throws Exception {
071    setupConf(UTIL.getConfiguration());
072    UTIL.startMiniCluster(1);
073  }
074
075  @AfterClass
076  public static void cleanupTest() throws Exception {
077    try {
078      UTIL.shutdownMiniCluster();
079    } catch (Exception e) {
080      LOG.warn("failure shutting down cluster", e);
081    }
082  }
083
084  @Before
085  public void setup() throws Exception {
086    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
087  }
088
089  @After
090  public void tearDown() throws Exception {
091    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
092    for (TableDescriptor htd: UTIL.getAdmin().listTableDescriptors()) {
093      LOG.info("Tear down, remove table=" + htd.getTableName());
094      UTIL.deleteTable(htd.getTableName());
095    }
096  }
097
098  @Test
099  public void testDeleteNamespace() throws Exception {
100    final String namespaceName = "testDeleteNamespace";
101    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
102
103    createNamespaceForTesting(namespaceName);
104
105    long procId = procExec.submitProcedure(
106      new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName));
107    // Wait the completion
108    ProcedureTestingUtility.waitProcedure(procExec, procId);
109    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
110
111    validateNamespaceNotExist(namespaceName);
112  }
113
114  @Test
115  public void testDeleteNonExistNamespace() throws Exception {
116    final String namespaceName = "testDeleteNonExistNamespace";
117    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
118
119    validateNamespaceNotExist(namespaceName);
120
121    long procId = procExec.submitProcedure(
122      new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName));
123    // Wait the completion
124    ProcedureTestingUtility.waitProcedure(procExec, procId);
125    // Expect fail with NamespaceNotFoundException
126    Procedure<?> result = procExec.getResult(procId);
127    assertTrue(result.isFailed());
128    LOG.debug("Delete namespace failed with exception: " + result.getException());
129    assertTrue(
130      ProcedureTestingUtility.getExceptionCause(result) instanceof NamespaceNotFoundException);
131  }
132
133  @Test
134  public void testDeleteSystemNamespace() throws Exception {
135    final String namespaceName = NamespaceDescriptor.SYSTEM_NAMESPACE.getName();
136    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
137
138    long procId = procExec.submitProcedure(
139      new DeleteNamespaceProcedure(procExec.getEnvironment(), namespaceName));
140    // Wait the completion
141    ProcedureTestingUtility.waitProcedure(procExec, procId);
142    Procedure<?> result = procExec.getResult(procId);
143    assertTrue(result.isFailed());
144    LOG.debug("Delete namespace failed with exception: " + result.getException());
145    assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException);
146  }
147
148  @Test
149  public void testDeleteNonEmptyNamespace() throws Exception {
150    final String namespaceName = "testDeleteNonExistNamespace";
151    final TableName tableName = 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.submitProcedure(
159      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.submitProcedure(
180      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.submitProcedure(
202      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=
209        UTIL.getAdmin().getNamespaceDescriptor(namespaceName);
210    assertNotNull(createdNsDescriptor);
211  }
212
213  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
214    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
215  }
216
217  private void createNamespaceForTesting(final String namespaceName) throws Exception {
218    final NamespaceDescriptor nsd = NamespaceDescriptor.create(namespaceName).build();
219    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
220
221    long procId = procExec.submitProcedure(
222      new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
223    // Wait the completion
224    ProcedureTestingUtility.waitProcedure(procExec, procId);
225    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
226  }
227
228  public static void validateNamespaceNotExist(final String nsName) throws IOException {
229    try {
230      NamespaceDescriptor nsDescriptor = UTIL.getAdmin().getNamespaceDescriptor(nsName);
231      assertNull(nsDescriptor);
232    } catch (NamespaceNotFoundException nsnfe) {
233      // Expected
234    }
235  }
236}