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.assertTrue;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.client.TableDescriptor;
025import org.apache.hadoop.hbase.master.HMaster;
026import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
027import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
028import org.junit.jupiter.api.AfterEach;
029import org.junit.jupiter.api.BeforeEach;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033public abstract class TestTableDDLProcedureBase {
034  private static final Logger LOG = LoggerFactory.getLogger(TestTableDDLProcedureBase.class);
035  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
036
037  protected static void setupConf(Configuration conf) {
038    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
039  }
040
041  protected static void setupCluster() throws Exception {
042    setupConf(UTIL.getConfiguration());
043    UTIL.startMiniCluster(1);
044  }
045
046  protected static void cleanupTest() throws Exception {
047    try {
048      UTIL.shutdownMiniCluster();
049    } catch (Exception e) {
050      LOG.warn("failure shutting down cluster", e);
051    }
052  }
053
054  @BeforeEach
055  public void setup() throws Exception {
056    resetProcExecutorTestingKillFlag();
057  }
058
059  @AfterEach
060  public void tearDown() throws Exception {
061    resetProcExecutorTestingKillFlag();
062    for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) {
063      LOG.info("Tear down, remove table=" + htd.getTableName());
064      UTIL.deleteTable(htd.getTableName());
065    }
066  }
067
068  protected void resetProcExecutorTestingKillFlag() {
069    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
070    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
071    assertTrue(procExec.isRunning(), "expected executor to be running");
072  }
073
074  protected ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
075    return getMaster().getMasterProcedureExecutor();
076  }
077
078  protected HMaster getMaster() {
079    return UTIL.getHBaseCluster().getMaster();
080  }
081}