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.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import java.util.concurrent.ThreadLocalRandom;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.procedure2.Procedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.junit.After;
037import org.junit.AfterClass;
038import org.junit.Before;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.junit.rules.TestName;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@Category({ MasterTests.class, MediumTests.class })
049public class TestProcedureAdmin {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestProcedureAdmin.class);
054
055  private static final Logger LOG = LoggerFactory.getLogger(TestProcedureAdmin.class);
056  @Rule
057  public TestName name = new TestName();
058
059  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
060
061  private static void setupConf(Configuration conf) {
062    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
063  }
064
065  @BeforeClass
066  public static void setupCluster() throws Exception {
067    setupConf(UTIL.getConfiguration());
068    UTIL.startMiniCluster(1);
069  }
070
071  @AfterClass
072  public static void cleanupTest() throws Exception {
073    try {
074      UTIL.shutdownMiniCluster();
075    } catch (Exception e) {
076      LOG.warn("failure shutting down cluster", e);
077    }
078  }
079
080  @Before
081  public void setup() throws Exception {
082    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
083    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
084    assertTrue("expected executor to be running", procExec.isRunning());
085  }
086
087  @After
088  public void tearDown() throws Exception {
089    assertTrue("expected executor to be running", getMasterProcedureExecutor().isRunning());
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 testAbortProcedureSuccess() throws Exception {
099    final TableName tableName = TableName.valueOf(name.getMethodName());
100    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
101
102    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
103    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
104    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
105    // Submit an abortable procedure
106    long procId = procExec
107      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
108    // Wait for one step to complete
109    ProcedureTestingUtility.waitProcedure(procExec, procId);
110
111    boolean abortResult = procExec.abort(procId, true);
112    assertTrue(abortResult);
113
114    MasterProcedureTestingUtility.testRestartWithAbort(procExec, procId);
115    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
116    // Validate the disable table procedure was aborted successfully
117    MasterProcedureTestingUtility.validateTableIsEnabled(UTIL.getHBaseCluster().getMaster(),
118      tableName);
119  }
120
121  @Test
122  public void testAbortProcedureFailure() throws Exception {
123    final TableName tableName = TableName.valueOf(name.getMethodName());
124    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
125
126    RegionInfo[] regions =
127      MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
128    UTIL.getAdmin().disableTable(tableName);
129    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
130    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
131    // Submit an un-abortable procedure
132    long procId =
133      procExec.submitProcedure(new DeleteTableProcedure(procExec.getEnvironment(), tableName));
134    // Wait for a couple of steps to complete (first step "prepare" is abortable)
135    ProcedureTestingUtility.waitProcedure(procExec, procId);
136    for (int i = 0; i < 2; ++i) {
137      ProcedureTestingUtility.assertProcNotYetCompleted(procExec, procId);
138      ProcedureTestingUtility.restart(procExec);
139      ProcedureTestingUtility.waitProcedure(procExec, procId);
140    }
141
142    boolean abortResult = procExec.abort(procId, true);
143    assertFalse(abortResult);
144
145    MasterProcedureTestingUtility.testRestartWithAbort(procExec, procId);
146    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
147    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
148    // Validate the delete table procedure was not aborted
149    MasterProcedureTestingUtility.validateTableDeletion(UTIL.getHBaseCluster().getMaster(),
150      tableName);
151  }
152
153  @Test
154  public void testAbortProcedureInterruptedNotAllowed() throws Exception {
155    final TableName tableName = TableName.valueOf(name.getMethodName());
156    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
157
158    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
159    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
160    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
161    // Submit a procedure
162    long procId = procExec
163      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, true));
164    // Wait for one step to complete
165    ProcedureTestingUtility.waitProcedure(procExec, procId);
166    // After HBASE-28210, the injection of kill before update is moved before we add rollback
167    // step, so here we need to run two steps, otherwise we will not consider the procedure as
168    // executed
169    MasterProcedureTestingUtility.restartMasterProcedureExecutor(procExec);
170    ProcedureTestingUtility.waitProcedure(procExec, procId);
171
172    // Set the mayInterruptIfRunning flag to false
173    boolean abortResult = procExec.abort(procId, false);
174    assertFalse(abortResult);
175
176    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
177    ProcedureTestingUtility.restart(procExec);
178    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
179    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
180    // Validate the delete table procedure was not aborted
181    MasterProcedureTestingUtility.validateTableIsDisabled(UTIL.getHBaseCluster().getMaster(),
182      tableName);
183  }
184
185  @Test
186  public void testAbortNonExistProcedure() throws Exception {
187    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
188    long procId;
189    // Generate a non-existing procedure
190    do {
191      procId = ThreadLocalRandom.current().nextLong();
192    } while (procExec.getResult(procId) != null);
193
194    boolean abortResult = procExec.abort(procId, true);
195    assertFalse(abortResult);
196  }
197
198  @Test
199  public void testGetProcedure() throws Exception {
200    final TableName tableName = TableName.valueOf(name.getMethodName());
201    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
202
203    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
204    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
205    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
206
207    long procId = procExec
208      .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false));
209    // Wait for one step to complete
210    ProcedureTestingUtility.waitProcedure(procExec, procId);
211
212    List<Procedure<MasterProcedureEnv>> procedures = procExec.getProcedures();
213    assertTrue(procedures.size() >= 1);
214    boolean found = false;
215    for (Procedure<?> proc : procedures) {
216      if (proc.getProcId() == procId) {
217        assertTrue(proc.isRunnable());
218        found = true;
219      } else {
220        assertTrue(proc.isSuccess());
221      }
222    }
223    assertTrue(found);
224
225    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
226    ProcedureTestingUtility.restart(procExec);
227    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
228    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
229    procedures = procExec.getProcedures();
230    for (Procedure proc : procedures) {
231      assertTrue(proc.isSuccess());
232    }
233  }
234
235  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
236    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
237  }
238}