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