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 java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.coprocessor.BadMasterObserverForCreateDeleteTable;
028import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
029import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
030import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.ModifyRegionUtils;
034import org.junit.AfterClass;
035import org.junit.Assert;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041@Category({ MasterTests.class, MediumTests.class })
042public class TestCreateDeleteTableProcedureWithRetry {
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestCreateDeleteTableProcedureWithRetry.class);
046
047  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
048
049  private static final TableName TABLE_NAME =
050    TableName.valueOf(TestCreateDeleteTableProcedureWithRetry.class.getSimpleName());
051
052  private static final String CF = "cf";
053
054  @BeforeClass
055  public static void setUp() throws Exception {
056    Configuration conf = UTIL.getConfiguration();
057    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
058      BadMasterObserverForCreateDeleteTable.class.getName());
059    UTIL.startMiniCluster(1);
060  }
061
062  @AfterClass
063  public static void tearDown() throws Exception {
064    UTIL.shutdownMiniCluster();
065  }
066
067  @Test
068  public void testCreateDeleteTableRetry() throws IOException {
069    ProcedureExecutor<MasterProcedureEnv> procExec =
070      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
071    TableDescriptor htd = MasterProcedureTestingUtility.createHTD(TABLE_NAME, CF);
072    RegionInfo[] regions = ModifyRegionUtils.createRegionInfos(htd, null);
073    CreateTableProcedure createProc =
074      new CreateTableProcedure(procExec.getEnvironment(), htd, regions);
075    ProcedureTestingUtility.submitAndWait(procExec, createProc);
076    Assert.assertTrue(UTIL.getAdmin().tableExists(TABLE_NAME));
077    MasterProcedureTestingUtility.validateTableCreation(UTIL.getMiniHBaseCluster().getMaster(),
078      TABLE_NAME, regions, CF);
079
080    UTIL.getAdmin().disableTable(TABLE_NAME);
081    DeleteTableProcedure deleteProc =
082      new DeleteTableProcedure(procExec.getEnvironment(), TABLE_NAME);
083    ProcedureTestingUtility.submitAndWait(procExec, deleteProc);
084    Assert.assertFalse(UTIL.getAdmin().tableExists(TABLE_NAME));
085    MasterProcedureTestingUtility.validateTableDeletion(UTIL.getMiniHBaseCluster().getMaster(),
086      TABLE_NAME);
087  }
088}