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.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 HBaseCommonTestingUtility 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 HBaseCommonTestingUtility(); 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 long procId = procExecutor.submitProcedure(new TestSMProcedure()); 175 ProcedureTestingUtility.testRecoveryAndDoubleExecution(procExecutor, procId, true); 176 assertEquals(6, procExecutor.getEnvironment().execCount.get()); 177 assertEquals(6, procExecutor.getEnvironment().rollbackCount.get()); 178 Throwable cause = ProcedureTestingUtility.assertProcFailed(procExecutor, procId); 179 assertEquals(TEST_FAILURE_EXCEPTION, cause); 180 } 181 182 public enum TestSMProcedureState { 183 STEP_1, 184 STEP_2 185 }; 186 187 public static class TestSMProcedure 188 extends StateMachineProcedure<TestProcEnv, TestSMProcedureState> { 189 @Override 190 protected Flow executeFromState(TestProcEnv env, TestSMProcedureState state) { 191 LOG.info("EXEC " + state + " " + this); 192 env.execCount.incrementAndGet(); 193 switch (state) { 194 case STEP_1: 195 if (!env.loop) { 196 setNextState(TestSMProcedureState.STEP_2); 197 } 198 break; 199 case STEP_2: 200 addChildProcedure(new SimpleChildProcedure()); 201 return Flow.NO_MORE_STATE; 202 } 203 return Flow.HAS_MORE_STATE; 204 } 205 206 @Override 207 protected boolean isRollbackSupported(TestSMProcedureState state) { 208 return true; 209 } 210 211 @Override 212 protected void rollbackState(TestProcEnv env, TestSMProcedureState state) { 213 LOG.info("ROLLBACK " + state + " " + this); 214 env.rollbackCount.incrementAndGet(); 215 } 216 217 @Override 218 protected TestSMProcedureState getState(int stateId) { 219 return TestSMProcedureState.values()[stateId]; 220 } 221 222 @Override 223 protected int getStateId(TestSMProcedureState state) { 224 return state.ordinal(); 225 } 226 227 @Override 228 protected TestSMProcedureState getInitialState() { 229 return TestSMProcedureState.STEP_1; 230 } 231 } 232 233 public static class TestSMProcedureBadRollback 234 extends StateMachineProcedure<TestProcEnv, TestSMProcedureState> { 235 @Override 236 protected Flow executeFromState(TestProcEnv env, TestSMProcedureState state) { 237 LOG.info("EXEC " + state + " " + this); 238 env.execCount.incrementAndGet(); 239 switch (state) { 240 case STEP_1: 241 if (!env.loop) { 242 setNextState(TestSMProcedureState.STEP_2); 243 } 244 break; 245 case STEP_2: 246 addChildProcedure(new SimpleChildProcedure()); 247 return Flow.NO_MORE_STATE; 248 } 249 return Flow.HAS_MORE_STATE; 250 } 251 252 @Override 253 protected void rollbackState(TestProcEnv env, TestSMProcedureState state) { 254 LOG.info("ROLLBACK " + state + " " + this); 255 env.rollbackCount.incrementAndGet(); 256 } 257 258 @Override 259 protected TestSMProcedureState getState(int stateId) { 260 return TestSMProcedureState.values()[stateId]; 261 } 262 263 @Override 264 protected int getStateId(TestSMProcedureState state) { 265 return state.ordinal(); 266 } 267 268 @Override 269 protected TestSMProcedureState getInitialState() { 270 return TestSMProcedureState.STEP_1; 271 } 272 273 @Override 274 protected void rollback(final TestProcEnv env) throws IOException, InterruptedException { 275 if (isEofState()) { 276 stateCount--; 277 } 278 try { 279 updateTimestamp(); 280 rollbackState(env, getCurrentState()); 281 throw new IOException(); 282 } catch (IOException e) { 283 // do nothing for now 284 } finally { 285 stateCount--; 286 updateTimestamp(); 287 } 288 } 289 } 290 291 public static class SimpleChildProcedure extends NoopProcedure<TestProcEnv> { 292 @Override 293 protected Procedure<TestProcEnv>[] execute(TestProcEnv env) { 294 LOG.info("EXEC " + this); 295 env.execCount.incrementAndGet(); 296 if (env.triggerChildRollback) { 297 setFailure("test-failure", TEST_FAILURE_EXCEPTION); 298 } 299 return null; 300 } 301 302 @Override 303 protected void rollback(TestProcEnv env) { 304 LOG.info("ROLLBACK " + this); 305 env.rollbackCount.incrementAndGet(); 306 } 307 } 308 309 public static class TestProcEnv { 310 AtomicInteger execCount = new AtomicInteger(0); 311 AtomicInteger rollbackCount = new AtomicInteger(0); 312 boolean triggerChildRollback = false; 313 boolean loop = false; 314 } 315}