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.procedure2; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import java.util.concurrent.atomic.AtomicInteger; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseCommonTestingUtil; 029import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility.NoopProcedure; 030import org.apache.hadoop.hbase.procedure2.store.ProcedureStore; 031import org.apache.hadoop.hbase.testclassification.MasterTests; 032import org.apache.hadoop.hbase.testclassification.MediumTests; 033import org.junit.After; 034import org.junit.Before; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041@Category({ MasterTests.class, MediumTests.class }) 042public class TestStateMachineProcedure { 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestStateMachineProcedure.class); 046 047 private static final Logger LOG = LoggerFactory.getLogger(TestStateMachineProcedure.class); 048 049 private static final Exception TEST_FAILURE_EXCEPTION = new Exception("test failure") { 050 051 private static final long serialVersionUID = 2147942238987041310L; 052 053 @Override 054 public boolean equals(final Object other) { 055 if (this == other) { 056 return true; 057 } 058 059 if (!(other instanceof Exception)) { 060 return false; 061 } 062 063 // we are going to serialize the exception in the test, 064 // so the instance comparison will not match 065 return getMessage().equals(((Exception) other).getMessage()); 066 } 067 068 @Override 069 public int hashCode() { 070 return getMessage().hashCode(); 071 } 072 }; 073 074 private static final int PROCEDURE_EXECUTOR_SLOTS = 1; 075 076 private ProcedureExecutor<TestProcEnv> procExecutor; 077 private ProcedureStore procStore; 078 079 private HBaseCommonTestingUtil htu; 080 private FileSystem fs; 081 private Path testDir; 082 private Path logDir; 083 084 @Before 085 public void setUp() throws IOException { 086 htu = new HBaseCommonTestingUtil(); 087 testDir = htu.getDataTestDir(); 088 fs = testDir.getFileSystem(htu.getConfiguration()); 089 090 logDir = new Path(testDir, "proc-logs"); 091 procStore = ProcedureTestingUtility.createWalStore(htu.getConfiguration(), logDir); 092 procExecutor = new ProcedureExecutor<>(htu.getConfiguration(), new TestProcEnv(), procStore); 093 procStore.start(PROCEDURE_EXECUTOR_SLOTS); 094 ProcedureTestingUtility.initAndStartWorkers(procExecutor, PROCEDURE_EXECUTOR_SLOTS, true); 095 } 096 097 @After 098 public void tearDown() throws IOException { 099 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExecutor, false); 100 assertTrue("expected executor to be running", procExecutor.isRunning()); 101 102 procExecutor.stop(); 103 procStore.stop(false); 104 fs.delete(logDir, true); 105 } 106 107 @Test 108 public void testAbortStuckProcedure() throws InterruptedException { 109 try { 110 procExecutor.getEnvironment().loop = true; 111 TestSMProcedure proc = new TestSMProcedure(); 112 long procId = procExecutor.submitProcedure(proc); 113 Thread.sleep(1000 + (int) (Math.random() * 4001)); 114 proc.abort(procExecutor.getEnvironment()); 115 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 116 assertEquals(true, proc.isFailed()); 117 } finally { 118 procExecutor.getEnvironment().loop = false; 119 } 120 } 121 122 @Test 123 public void testChildOnLastStep() { 124 long procId = procExecutor.submitProcedure(new TestSMProcedure()); 125 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 126 assertEquals(3, procExecutor.getEnvironment().execCount.get()); 127 assertEquals(0, procExecutor.getEnvironment().rollbackCount.get()); 128 ProcedureTestingUtility.assertProcNotFailed(procExecutor, procId); 129 } 130 131 @Test 132 public void testChildOnLastStepDoubleExecution() throws Exception { 133 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExecutor, true); 134 long procId = procExecutor.submitProcedure(new TestSMProcedure()); 135 ProcedureTestingUtility.testRecoveryAndDoubleExecution(procExecutor, procId); 136 assertEquals(6, procExecutor.getEnvironment().execCount.get()); 137 assertEquals(0, procExecutor.getEnvironment().rollbackCount.get()); 138 ProcedureTestingUtility.assertProcNotFailed(procExecutor, procId); 139 } 140 141 @Test 142 public void testChildOnLastStepWithRollback() { 143 procExecutor.getEnvironment().triggerChildRollback = true; 144 long procId = procExecutor.submitProcedure(new TestSMProcedure()); 145 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 146 assertEquals(3, procExecutor.getEnvironment().execCount.get()); 147 assertEquals(3, procExecutor.getEnvironment().rollbackCount.get()); 148 Throwable cause = ProcedureTestingUtility.assertProcFailed(procExecutor, procId); 149 assertEquals(TEST_FAILURE_EXCEPTION, cause); 150 } 151 152 @Test 153 public void testChildNormalRollbackStateCount() { 154 procExecutor.getEnvironment().triggerChildRollback = true; 155 TestSMProcedureBadRollback testNormalRollback = new TestSMProcedureBadRollback(); 156 long procId = procExecutor.submitProcedure(testNormalRollback); 157 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 158 assertEquals(0, testNormalRollback.stateCount); 159 } 160 161 @Test 162 public void testChildBadRollbackStateCount() { 163 procExecutor.getEnvironment().triggerChildRollback = true; 164 TestSMProcedureBadRollback testBadRollback = new TestSMProcedureBadRollback(); 165 long procId = procExecutor.submitProcedure(testBadRollback); 166 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 167 assertEquals(0, testBadRollback.stateCount); 168 } 169 170 @Test 171 public void testChildOnLastStepWithRollbackDoubleExecution() throws Exception { 172 procExecutor.getEnvironment().triggerChildRollback = true; 173 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExecutor, true); 174 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdateInRollback(procExecutor, true); 175 long procId = procExecutor.submitProcedure(new TestSMProcedure()); 176 ProcedureTestingUtility.testRecoveryAndDoubleExecution(procExecutor, procId, true); 177 assertEquals(6, procExecutor.getEnvironment().execCount.get()); 178 assertEquals(6, procExecutor.getEnvironment().rollbackCount.get()); 179 Throwable cause = ProcedureTestingUtility.assertProcFailed(procExecutor, procId); 180 assertEquals(TEST_FAILURE_EXCEPTION, cause); 181 } 182 183 public enum TestSMProcedureState { 184 STEP_1, 185 STEP_2 186 } 187 188 public static class TestSMProcedure 189 extends StateMachineProcedure<TestProcEnv, TestSMProcedureState> { 190 @Override 191 protected Flow executeFromState(TestProcEnv env, TestSMProcedureState state) { 192 LOG.info("EXEC " + state + " " + this); 193 env.execCount.incrementAndGet(); 194 switch (state) { 195 case STEP_1: 196 if (!env.loop) { 197 setNextState(TestSMProcedureState.STEP_2); 198 } 199 break; 200 case STEP_2: 201 addChildProcedure(new SimpleChildProcedure()); 202 return Flow.NO_MORE_STATE; 203 } 204 return Flow.HAS_MORE_STATE; 205 } 206 207 @Override 208 protected boolean isRollbackSupported(TestSMProcedureState state) { 209 return true; 210 } 211 212 @Override 213 protected void rollbackState(TestProcEnv env, TestSMProcedureState state) { 214 LOG.info("ROLLBACK " + state + " " + this); 215 env.rollbackCount.incrementAndGet(); 216 } 217 218 @Override 219 protected TestSMProcedureState getState(int stateId) { 220 return TestSMProcedureState.values()[stateId]; 221 } 222 223 @Override 224 protected int getStateId(TestSMProcedureState state) { 225 return state.ordinal(); 226 } 227 228 @Override 229 protected TestSMProcedureState getInitialState() { 230 return TestSMProcedureState.STEP_1; 231 } 232 } 233 234 public static class TestSMProcedureBadRollback 235 extends StateMachineProcedure<TestProcEnv, TestSMProcedureState> { 236 @Override 237 protected Flow executeFromState(TestProcEnv env, TestSMProcedureState state) { 238 LOG.info("EXEC " + state + " " + this); 239 env.execCount.incrementAndGet(); 240 switch (state) { 241 case STEP_1: 242 if (!env.loop) { 243 setNextState(TestSMProcedureState.STEP_2); 244 } 245 break; 246 case STEP_2: 247 addChildProcedure(new SimpleChildProcedure()); 248 return Flow.NO_MORE_STATE; 249 } 250 return Flow.HAS_MORE_STATE; 251 } 252 253 @Override 254 protected boolean isRollbackSupported(TestSMProcedureState state) { 255 return true; 256 } 257 258 @Override 259 protected void rollbackState(TestProcEnv env, TestSMProcedureState state) { 260 LOG.info("ROLLBACK " + state + " " + this); 261 env.rollbackCount.incrementAndGet(); 262 } 263 264 @Override 265 protected TestSMProcedureState getState(int stateId) { 266 return TestSMProcedureState.values()[stateId]; 267 } 268 269 @Override 270 protected int getStateId(TestSMProcedureState state) { 271 return state.ordinal(); 272 } 273 274 @Override 275 protected TestSMProcedureState getInitialState() { 276 return TestSMProcedureState.STEP_1; 277 } 278 279 @Override 280 protected void rollback(final TestProcEnv env) throws IOException, InterruptedException { 281 if (isEofState()) { 282 stateCount--; 283 } 284 try { 285 updateTimestamp(); 286 rollbackState(env, getCurrentState()); 287 throw new IOException(); 288 } catch (IOException e) { 289 // do nothing for now 290 } finally { 291 stateCount--; 292 updateTimestamp(); 293 } 294 } 295 } 296 297 public static class SimpleChildProcedure extends NoopProcedure<TestProcEnv> { 298 @Override 299 protected Procedure<TestProcEnv>[] execute(TestProcEnv env) { 300 LOG.info("EXEC " + this); 301 env.execCount.incrementAndGet(); 302 if (env.triggerChildRollback) { 303 setFailure("test-failure", TEST_FAILURE_EXCEPTION); 304 } 305 return null; 306 } 307 308 @Override 309 protected void rollback(TestProcEnv env) { 310 LOG.info("ROLLBACK " + this); 311 env.rollbackCount.incrementAndGet(); 312 } 313 } 314 315 public static class TestProcEnv { 316 AtomicInteger execCount = new AtomicInteger(0); 317 AtomicInteger rollbackCount = new AtomicInteger(0); 318 boolean triggerChildRollback = false; 319 boolean loop = false; 320 } 321}