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.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HTableDescriptor; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.RegionInfo; 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 HBaseTestingUtility UTIL = new HBaseTestingUtility(); 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 (HTableDescriptor htd : UTIL.getAdmin().listTables()) { 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 RegionInfo[] regions = 159 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 160 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 161 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 162 // Submit a procedure 163 long procId = procExec 164 .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, true)); 165 // Wait for one step to complete 166 ProcedureTestingUtility.waitProcedure(procExec, procId); 167 168 // Set the mayInterruptIfRunning flag to false 169 boolean abortResult = procExec.abort(procId, false); 170 assertFalse(abortResult); 171 172 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 173 ProcedureTestingUtility.restart(procExec); 174 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 175 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 176 // Validate the delete table procedure was not aborted 177 MasterProcedureTestingUtility.validateTableIsDisabled(UTIL.getHBaseCluster().getMaster(), 178 tableName); 179 } 180 181 @Test 182 public void testAbortNonExistProcedure() throws Exception { 183 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 184 long procId; 185 // Generate a non-existing procedure 186 do { 187 procId = ThreadLocalRandom.current().nextLong(); 188 } while (procExec.getResult(procId) != null); 189 190 boolean abortResult = procExec.abort(procId, true); 191 assertFalse(abortResult); 192 } 193 194 @Test 195 public void testGetProcedure() throws Exception { 196 final TableName tableName = TableName.valueOf(name.getMethodName()); 197 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 198 199 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 200 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 201 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 202 203 long procId = procExec 204 .submitProcedure(new DisableTableProcedure(procExec.getEnvironment(), tableName, false)); 205 // Wait for one step to complete 206 ProcedureTestingUtility.waitProcedure(procExec, procId); 207 208 List<Procedure<MasterProcedureEnv>> procedures = procExec.getProcedures(); 209 assertTrue(procedures.size() >= 1); 210 boolean found = false; 211 for (Procedure<?> proc : procedures) { 212 if (proc.getProcId() == procId) { 213 assertTrue(proc.isRunnable()); 214 found = true; 215 } else { 216 assertTrue(proc.isSuccess()); 217 } 218 } 219 assertTrue(found); 220 221 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 222 ProcedureTestingUtility.restart(procExec); 223 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 224 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 225 procedures = procExec.getProcedures(); 226 for (Procedure proc : procedures) { 227 assertTrue(proc.isSuccess()); 228 } 229 } 230 231 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 232 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 233 } 234}