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 */ 018 019package org.apache.hadoop.hbase.procedure2; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.OutputStream; 024import java.util.concurrent.atomic.AtomicBoolean; 025import java.util.concurrent.CountDownLatch; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.apache.hadoop.fs.FileSystem; 030import org.apache.hadoop.fs.FileStatus; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.HBaseCommonTestingUtility; 033import org.apache.hadoop.hbase.ProcedureInfo; 034import org.apache.hadoop.hbase.procedure2.store.ProcedureStore; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.hbase.testclassification.MasterTests; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 039import org.apache.hadoop.hbase.util.Threads; 040import org.junit.After; 041import org.junit.Before; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044 045import static org.junit.Assert.assertEquals; 046import static org.junit.Assert.assertFalse; 047import static org.junit.Assert.assertTrue; 048 049@Category({MasterTests.class, SmallTests.class}) 050public class TestProcedureRecovery { 051 private static final Log LOG = LogFactory.getLog(TestProcedureRecovery.class); 052 053 private static final int PROCEDURE_EXECUTOR_SLOTS = 1; 054 055 private static TestProcEnv procEnv; 056 private static ProcedureExecutor<TestProcEnv> procExecutor; 057 private static ProcedureStore procStore; 058 private static int procSleepInterval; 059 060 private HBaseCommonTestingUtility htu; 061 private FileSystem fs; 062 private Path testDir; 063 private Path logDir; 064 065 @Before 066 public void setUp() throws IOException { 067 htu = new HBaseCommonTestingUtility(); 068 testDir = htu.getDataTestDir(); 069 fs = testDir.getFileSystem(htu.getConfiguration()); 070 assertTrue(testDir.depth() > 1); 071 072 logDir = new Path(testDir, "proc-logs"); 073 procEnv = new TestProcEnv(); 074 procStore = ProcedureTestingUtility.createStore(htu.getConfiguration(), fs, logDir); 075 procExecutor = new ProcedureExecutor(htu.getConfiguration(), procEnv, procStore); 076 procExecutor.testing = new ProcedureExecutor.Testing(); 077 procStore.start(PROCEDURE_EXECUTOR_SLOTS); 078 procExecutor.start(PROCEDURE_EXECUTOR_SLOTS, true); 079 procSleepInterval = 0; 080 } 081 082 @After 083 public void tearDown() throws IOException { 084 procExecutor.stop(); 085 procStore.stop(false); 086 fs.delete(logDir, true); 087 } 088 089 private void restart() throws Exception { 090 dumpLogDirState(); 091 ProcedureTestingUtility.restart(procExecutor); 092 dumpLogDirState(); 093 } 094 095 public static class TestSingleStepProcedure extends SequentialProcedure<TestProcEnv> { 096 private int step = 0; 097 098 public TestSingleStepProcedure() { } 099 100 @Override 101 protected Procedure[] execute(TestProcEnv env) throws InterruptedException { 102 env.waitOnLatch(); 103 LOG.debug("execute procedure " + this + " step=" + step); 104 step++; 105 setResult(Bytes.toBytes(step)); 106 return null; 107 } 108 109 @Override 110 protected void rollback(TestProcEnv env) { } 111 112 @Override 113 protected boolean abort(TestProcEnv env) { return true; } 114 } 115 116 public static class BaseTestStepProcedure extends SequentialProcedure<TestProcEnv> { 117 private AtomicBoolean abort = new AtomicBoolean(false); 118 private int step = 0; 119 120 @Override 121 protected Procedure[] execute(TestProcEnv env) throws InterruptedException { 122 env.waitOnLatch(); 123 LOG.debug("execute procedure " + this + " step=" + step); 124 ProcedureTestingUtility.toggleKillBeforeStoreUpdate(procExecutor); 125 step++; 126 Threads.sleepWithoutInterrupt(procSleepInterval); 127 if (isAborted()) { 128 setFailure(new RemoteProcedureException(getClass().getName(), 129 new ProcedureAbortedException( 130 "got an abort at " + getClass().getName() + " step=" + step))); 131 return null; 132 } 133 return null; 134 } 135 136 @Override 137 protected void rollback(TestProcEnv env) { 138 LOG.debug("rollback procedure " + this + " step=" + step); 139 ProcedureTestingUtility.toggleKillBeforeStoreUpdate(procExecutor); 140 step++; 141 } 142 143 @Override 144 protected boolean abort(TestProcEnv env) { 145 abort.set(true); 146 return true; 147 } 148 149 private boolean isAborted() { 150 boolean aborted = abort.get(); 151 BaseTestStepProcedure proc = this; 152 while (proc.hasParent() && !aborted) { 153 proc = (BaseTestStepProcedure)procExecutor.getProcedure(proc.getParentProcId()); 154 aborted = proc.isAborted(); 155 } 156 return aborted; 157 } 158 } 159 160 public static class TestMultiStepProcedure extends BaseTestStepProcedure { 161 public TestMultiStepProcedure() { } 162 163 @Override 164 public Procedure[] execute(TestProcEnv env) throws InterruptedException { 165 super.execute(env); 166 return isFailed() ? null : new Procedure[] { new Step1Procedure() }; 167 } 168 169 public static class Step1Procedure extends BaseTestStepProcedure { 170 public Step1Procedure() { } 171 172 @Override 173 protected Procedure[] execute(TestProcEnv env) throws InterruptedException { 174 super.execute(env); 175 return isFailed() ? null : new Procedure[] { new Step2Procedure() }; 176 } 177 } 178 179 public static class Step2Procedure extends BaseTestStepProcedure { 180 public Step2Procedure() { } 181 } 182 } 183 184 @Test 185 public void testNoopLoad() throws Exception { 186 restart(); 187 } 188 189 @Test(timeout=30000) 190 public void testSingleStepProcRecovery() throws Exception { 191 Procedure proc = new TestSingleStepProcedure(); 192 procExecutor.testing.killBeforeStoreUpdate = true; 193 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc); 194 assertFalse(procExecutor.isRunning()); 195 procExecutor.testing.killBeforeStoreUpdate = false; 196 197 // Restart and verify that the procedures restart 198 long restartTs = EnvironmentEdgeManager.currentTime(); 199 restart(); 200 waitProcedure(procId); 201 ProcedureInfo result = procExecutor.getResult(procId); 202 assertTrue(result.getLastUpdate() > restartTs); 203 ProcedureTestingUtility.assertProcNotFailed(result); 204 assertEquals(1, Bytes.toInt(result.getResult())); 205 long resultTs = result.getLastUpdate(); 206 207 // Verify that after another restart the result is still there 208 restart(); 209 result = procExecutor.getResult(procId); 210 ProcedureTestingUtility.assertProcNotFailed(result); 211 assertEquals(resultTs, result.getLastUpdate()); 212 assertEquals(1, Bytes.toInt(result.getResult())); 213 } 214 215 @Test(timeout=30000) 216 public void testMultiStepProcRecovery() throws Exception { 217 // Step 0 - kill 218 Procedure proc = new TestMultiStepProcedure(); 219 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc); 220 assertFalse(procExecutor.isRunning()); 221 222 // Step 0 exec && Step 1 - kill 223 restart(); 224 waitProcedure(procId); 225 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 226 assertFalse(procExecutor.isRunning()); 227 228 // Step 1 exec && step 2 - kill 229 restart(); 230 waitProcedure(procId); 231 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 232 assertFalse(procExecutor.isRunning()); 233 234 // Step 2 exec 235 restart(); 236 waitProcedure(procId); 237 assertTrue(procExecutor.isRunning()); 238 239 // The procedure is completed 240 ProcedureInfo result = procExecutor.getResult(procId); 241 ProcedureTestingUtility.assertProcNotFailed(result); 242 } 243 244 @Test(timeout=30000) 245 public void testMultiStepRollbackRecovery() throws Exception { 246 // Step 0 - kill 247 Procedure proc = new TestMultiStepProcedure(); 248 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc); 249 assertFalse(procExecutor.isRunning()); 250 251 // Step 0 exec && Step 1 - kill 252 restart(); 253 waitProcedure(procId); 254 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 255 assertFalse(procExecutor.isRunning()); 256 257 // Step 1 exec && step 2 - kill 258 restart(); 259 waitProcedure(procId); 260 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 261 assertFalse(procExecutor.isRunning()); 262 263 // Step 2 exec - rollback - kill 264 procSleepInterval = 2500; 265 restart(); 266 assertTrue(procExecutor.abort(procId)); 267 waitProcedure(procId); 268 assertFalse(procExecutor.isRunning()); 269 270 // rollback - kill 271 restart(); 272 waitProcedure(procId); 273 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 274 assertFalse(procExecutor.isRunning()); 275 276 // rollback - complete 277 restart(); 278 waitProcedure(procId); 279 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 280 assertFalse(procExecutor.isRunning()); 281 282 // Restart the executor and get the result 283 restart(); 284 waitProcedure(procId); 285 286 // The procedure is completed 287 ProcedureInfo result = procExecutor.getResult(procId); 288 ProcedureTestingUtility.assertIsAbortException(result); 289 } 290 291 @Test(timeout=30000) 292 public void testCompletedProcWithSameNonce() throws Exception { 293 final long nonceGroup = 123; 294 final long nonce = 2222; 295 Procedure proc = new TestSingleStepProcedure(); 296 // Submit a proc and wait for its completion 297 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc, nonceGroup, nonce); 298 299 // Restart 300 restart(); 301 waitProcedure(procId); 302 303 Procedure proc2 = new TestSingleStepProcedure(); 304 // Submit a procedure with the same nonce and expect the same procedure would return. 305 long procId2 = ProcedureTestingUtility.submitAndWait(procExecutor, proc2, nonceGroup, nonce); 306 assertTrue(procId == procId2); 307 308 ProcedureInfo result = procExecutor.getResult(procId2); 309 ProcedureTestingUtility.assertProcNotFailed(result); 310 } 311 312 @Test(timeout=30000) 313 public void testRunningProcWithSameNonce() throws Exception { 314 final long nonceGroup = 456; 315 final long nonce = 33333; 316 Procedure proc = new TestMultiStepProcedure(); 317 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc, nonceGroup, nonce); 318 319 // Restart (use a latch to prevent the step execution until we submitted proc2) 320 CountDownLatch latch = new CountDownLatch(1); 321 procEnv.setWaitLatch(latch); 322 restart(); 323 // Submit a procedure with the same nonce and expect the same procedure would return. 324 Procedure proc2 = new TestMultiStepProcedure(); 325 long procId2 = procExecutor.submitProcedure(proc2, nonceGroup, nonce); 326 latch.countDown(); 327 procEnv.setWaitLatch(null); 328 329 // The original proc is not completed and the new submission should have the same proc Id. 330 assertTrue(procId == procId2); 331 } 332 333 334 public static class TestStateMachineProcedure 335 extends StateMachineProcedure<TestProcEnv, TestStateMachineProcedure.State> { 336 enum State { STATE_1, STATE_2, STATE_3, DONE } 337 338 public TestStateMachineProcedure() {} 339 340 private AtomicBoolean aborted = new AtomicBoolean(false); 341 private int iResult = 0; 342 343 @Override 344 protected StateMachineProcedure.Flow executeFromState(TestProcEnv env, State state) { 345 switch (state) { 346 case STATE_1: 347 LOG.info("execute step 1 " + this); 348 setNextState(State.STATE_2); 349 iResult += 3; 350 break; 351 case STATE_2: 352 LOG.info("execute step 2 " + this); 353 setNextState(State.STATE_3); 354 iResult += 5; 355 break; 356 case STATE_3: 357 LOG.info("execute step 3 " + this); 358 Threads.sleepWithoutInterrupt(procSleepInterval); 359 if (aborted.get()) { 360 LOG.info("aborted step 3 " + this); 361 setAbortFailure("test", "aborted"); 362 break; 363 } 364 setNextState(State.DONE); 365 iResult += 7; 366 setResult(Bytes.toBytes(iResult)); 367 return Flow.NO_MORE_STATE; 368 default: 369 throw new UnsupportedOperationException(); 370 } 371 return Flow.HAS_MORE_STATE; 372 } 373 374 @Override 375 protected void rollbackState(TestProcEnv env, final State state) { 376 switch (state) { 377 case STATE_1: 378 LOG.info("rollback step 1 " + this); 379 break; 380 case STATE_2: 381 LOG.info("rollback step 2 " + this); 382 break; 383 case STATE_3: 384 LOG.info("rollback step 3 " + this); 385 break; 386 default: 387 throw new UnsupportedOperationException(); 388 } 389 } 390 391 @Override 392 protected State getState(final int stateId) { 393 return State.values()[stateId]; 394 } 395 396 @Override 397 protected int getStateId(final State state) { 398 return state.ordinal(); 399 } 400 401 @Override 402 protected State getInitialState() { 403 return State.STATE_1; 404 } 405 406 @Override 407 protected boolean abort(TestProcEnv env) { 408 aborted.set(true); 409 return true; 410 } 411 412 @Override 413 protected void serializeStateData(final OutputStream stream) throws IOException { 414 super.serializeStateData(stream); 415 stream.write(Bytes.toBytes(iResult)); 416 } 417 418 @Override 419 protected void deserializeStateData(final InputStream stream) throws IOException { 420 super.deserializeStateData(stream); 421 byte[] data = new byte[4]; 422 stream.read(data); 423 iResult = Bytes.toInt(data); 424 } 425 } 426 427 @Test(timeout=30000) 428 public void testStateMachineRecovery() throws Exception { 429 ProcedureTestingUtility.setToggleKillBeforeStoreUpdate(procExecutor, true); 430 ProcedureTestingUtility.setKillBeforeStoreUpdate(procExecutor, true); 431 432 // Step 1 - kill 433 Procedure proc = new TestStateMachineProcedure(); 434 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc); 435 assertFalse(procExecutor.isRunning()); 436 437 // Step 1 exec && Step 2 - kill 438 restart(); 439 waitProcedure(procId); 440 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 441 assertFalse(procExecutor.isRunning()); 442 443 // Step 2 exec && step 3 - kill 444 restart(); 445 waitProcedure(procId); 446 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 447 assertFalse(procExecutor.isRunning()); 448 449 // Step 3 exec 450 restart(); 451 waitProcedure(procId); 452 assertTrue(procExecutor.isRunning()); 453 454 // The procedure is completed 455 ProcedureInfo result = procExecutor.getResult(procId); 456 ProcedureTestingUtility.assertProcNotFailed(result); 457 assertEquals(15, Bytes.toInt(result.getResult())); 458 } 459 460 @Test(timeout=30000) 461 public void testStateMachineRollbackRecovery() throws Exception { 462 ProcedureTestingUtility.setToggleKillBeforeStoreUpdate(procExecutor, true); 463 ProcedureTestingUtility.setKillBeforeStoreUpdate(procExecutor, true); 464 465 // Step 1 - kill 466 Procedure proc = new TestStateMachineProcedure(); 467 long procId = ProcedureTestingUtility.submitAndWait(procExecutor, proc); 468 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 469 assertFalse(procExecutor.isRunning()); 470 471 // Step 1 exec && Step 2 - kill 472 restart(); 473 waitProcedure(procId); 474 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 475 assertFalse(procExecutor.isRunning()); 476 477 // Step 2 exec && step 3 - kill 478 restart(); 479 waitProcedure(procId); 480 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 481 assertFalse(procExecutor.isRunning()); 482 483 // Step 3 exec - rollback step 3 - kill 484 procSleepInterval = 2500; 485 restart(); 486 assertTrue(procExecutor.abort(procId)); 487 waitProcedure(procId); 488 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 489 assertFalse(procExecutor.isRunning()); 490 491 // Rollback step 3 - rollback step 2 - kill 492 restart(); 493 waitProcedure(procId); 494 assertFalse(procExecutor.isRunning()); 495 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 496 497 // Rollback step 2 - step 1 - kill 498 restart(); 499 waitProcedure(procId); 500 assertFalse(procExecutor.isRunning()); 501 ProcedureTestingUtility.assertProcNotYetCompleted(procExecutor, procId); 502 503 // Rollback step 1 - complete 504 restart(); 505 waitProcedure(procId); 506 assertTrue(procExecutor.isRunning()); 507 508 // The procedure is completed 509 ProcedureInfo result = procExecutor.getResult(procId); 510 ProcedureTestingUtility.assertIsAbortException(result); 511 } 512 513 private void waitProcedure(final long procId) { 514 ProcedureTestingUtility.waitProcedure(procExecutor, procId); 515 dumpLogDirState(); 516 } 517 518 private void dumpLogDirState() { 519 try { 520 FileStatus[] files = fs.listStatus(logDir); 521 if (files != null && files.length > 0) { 522 for (FileStatus file: files) { 523 assertTrue(file.toString(), file.isFile()); 524 LOG.debug("log file " + file.getPath() + " size=" + file.getLen()); 525 } 526 } else { 527 LOG.debug("no files under: " + logDir); 528 } 529 } catch (IOException e) { 530 LOG.warn("Unable to dump " + logDir, e); 531 } 532 } 533 534 private static class TestProcEnv { 535 private CountDownLatch latch = null; 536 537 /** 538 * set/unset a latch. every procedure execute() step will wait on the latch if any. 539 */ 540 public void setWaitLatch(CountDownLatch latch) { 541 this.latch = latch; 542 } 543 544 public void waitOnLatch() throws InterruptedException { 545 if (latch != null) { 546 latch.await(); 547 } 548 } 549 } 550}