Repository navigation
feat: retry the parent when a child workflow started with asChildOf fails to start #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+887
−95
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d0f25e
feat: start child workflows through a builder that retries the parent…
07ec6fd
docs: note the createExistingWorkflowIsNoop gap for children whose ta…
ab6a6f3
refactor: infer the retrying child start from asChildOf instead of a …
b60c9d9
test: show a RetryableError from a child start still retries the parent
ecf7b08
fix: schedule a started workflow left without a task, and retry child…
ffa7ccd
fix: schedule a stranded workflow after a lease so a slow first start…
daa7f99
test: model concurrent starts and the stranded-start recovery, and tr…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
src/test/java/com/airbnb/skipper/integtest/ChildWorkflowStartFailureTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| package com.airbnb.skipper.integtest | ||
|
|
||
| import com.airbnb.skipper.ComponentFactory | ||
| import com.airbnb.skipper.IWorkflowFactory | ||
| import com.airbnb.skipper.OptimisticLockingError | ||
| import com.airbnb.skipper.Workflow | ||
| import com.airbnb.skipper.WorkflowInstance | ||
| import com.airbnb.skipper.WorkflowMethod | ||
| import com.airbnb.skipper.internal.SkipperEngine | ||
| import com.airbnb.skipper.internal.SkipperSchedulerManager | ||
| import com.airbnb.skipper.internal.api.RunRequest | ||
| import com.airbnb.skipper.internal.scheduler.ScheduleRequest | ||
| import com.airbnb.skipper.internal.scheduler.Scheduler | ||
| import com.airbnb.skipper.internal.scheduler.Task | ||
| import com.airbnb.skipper.internal.scheduler.sqlite.SqliteScheduler | ||
| import com.airbnb.skipper.internal.storage.WorkflowCreationRequest | ||
| import com.airbnb.skipper.internal.storage.WorkflowStore | ||
| import com.airbnb.skipper.internal.storage.sqlite.SqliteWorkflowStore | ||
| import com.airbnb.skipper.testutils.TestHelper | ||
| import com.airbnb.skipper.testutils.TestRequestContext | ||
| import com.airbnb.skipper.testutils.TestRuntime | ||
| import com.airbnb.skipper.util.ExtraRequestData | ||
| import java.util.concurrent.TimeoutException | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
| import javax.inject.Inject | ||
| import org.junit.jupiter.api.AfterEach | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.parallel.Execution | ||
| import org.junit.jupiter.api.parallel.ExecutionMode | ||
|
|
||
| /** | ||
| * A parent workflow starts its children on its own execution thread, so a store or scheduler failure | ||
| * while starting one of them escapes the parent's workflow method. Started with | ||
| * [com.airbnb.skipper.InvocationBuilder.asChildOf], a transient one (a timeout) retries the parent, | ||
| * which then starts the remaining children and completes. Started without it, the same failure is | ||
| * taken for a bug in the workflow code and fails the parent terminally. A request the | ||
| * store rejects as invalid fails the parent either way, since retrying it cannot succeed. | ||
| * | ||
| * Each test fails the first store or scheduler call for the second of three children. | ||
| */ | ||
| @Execution(ExecutionMode.SAME_THREAD) | ||
| class ChildWorkflowStartFailureTest { | ||
| /** Throws [error] from the first call that targets [workflowId]; every other call passes through. */ | ||
| class Fault( | ||
| private val workflowId: String, | ||
| private val error: () -> Exception, | ||
| ) { | ||
| val hits = AtomicInteger() | ||
|
|
||
| fun maybeThrow(id: String) { | ||
| if (id == workflowId && hits.incrementAndGet() == 1) { | ||
| throw error() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class FaultyStore( | ||
| private val delegate: WorkflowStore, | ||
| private val fault: Fault?, | ||
| ) : WorkflowStore by delegate { | ||
| override fun createWorkflow(request: WorkflowCreationRequest): WorkflowInstance { | ||
| fault?.maybeThrow(request.workflowId) | ||
| return delegate.createWorkflow(request) | ||
| } | ||
| } | ||
|
|
||
| class FaultyScheduler( | ||
| private val delegate: Scheduler, | ||
| private val fault: Fault?, | ||
| ) : Scheduler by delegate { | ||
| override fun <T> schedule(request: ScheduleRequest<T>): Task<T> { | ||
| if (request.type == Task.Type.WORKFLOW) { | ||
| fault?.maybeThrow(request.id) | ||
| } | ||
| return delegate.schedule(request) | ||
| } | ||
| } | ||
|
|
||
| open class ChildWorkflow : Workflow() { | ||
| @WorkflowMethod | ||
| open fun run() {} | ||
| } | ||
|
|
||
| open class FanOutWorkflow : Workflow() { | ||
| @Inject lateinit var workflowFactory: IWorkflowFactory | ||
|
|
||
| @WorkflowMethod | ||
| open fun run(): String { | ||
| for (i in 1..CHILDREN) { | ||
| workflowFactory.builder(ChildWorkflow::class.java, childId(id, i)).runAsync().detached().asChildOf(this).build().run() | ||
| } | ||
| return "started $CHILDREN children" | ||
| } | ||
| } | ||
|
|
||
| open class PlainFanOutWorkflow : Workflow() { | ||
| @Inject lateinit var workflowFactory: IWorkflowFactory | ||
|
|
||
| @WorkflowMethod | ||
| open fun run(): String { | ||
| for (i in 1..CHILDREN) { | ||
| workflowFactory.builder(ChildWorkflow::class.java, childId(id, i)).runAsync().detached().build().run() | ||
| } | ||
| return "started $CHILDREN children" | ||
| } | ||
| } | ||
|
|
||
| private lateinit var engine: SkipperEngine | ||
| private lateinit var scheduler: Scheduler | ||
| private lateinit var manager: SkipperSchedulerManager | ||
|
|
||
| @AfterEach | ||
| fun tearDown() { | ||
| if (::manager.isInitialized) manager.forceStop() | ||
| } | ||
|
|
||
| private fun start( | ||
| storeFault: Fault? = null, | ||
| schedulerFault: Fault? = null, | ||
| ) { | ||
| val deps = TestRuntime() | ||
| deps.config.workflowStore = ComponentFactory { FaultyStore(SqliteWorkflowStore.Factory().create(it), storeFault) } | ||
| deps.config.scheduler = ComponentFactory { FaultyScheduler(SqliteScheduler.Factory().create(it), schedulerFault) } | ||
| val runtime = deps.runtime | ||
| engine = runtime.skipperEngine.get() | ||
| scheduler = runtime.scheduler.get() | ||
| manager = runtime.skipperSchedulerManager.get() | ||
| manager.start() | ||
| } | ||
|
|
||
| private fun startParent( | ||
| parentId: String, | ||
| parentClass: Class<out Workflow> = FanOutWorkflow::class.java, | ||
| ) { | ||
| engine.startWorkflow( | ||
| RunRequest.builder() | ||
| .workflowId(parentId) | ||
| .workflowClass(parentClass) | ||
| .workflowMethod("run") | ||
| .requestContext(TestRequestContext.builder().build()) | ||
| .extraRequestData(ExtraRequestData()) | ||
| .runAsync(true) // hand it to the scheduler so the task handler retries it like any other task | ||
| .build(), | ||
| ) | ||
| } | ||
|
|
||
| private fun awaitTerminal(workflowId: String): WorkflowInstance = | ||
| TestHelper(engine, scheduler, workflowId) | ||
| .waitForWorkflowToReachStatus(setOf(WorkflowInstance.Status.COMPLETED, WorkflowInstance.Status.ERROR)) | ||
|
|
||
| private fun assertParentRetriedAndCompleted( | ||
| parentId: String, | ||
| fault: Fault, | ||
| ) { | ||
| val parent = awaitTerminal(parentId) | ||
| assertEquals(WorkflowInstance.Status.COMPLETED, parent.status, "a transient child-start failure must not fail the parent") | ||
| assertEquals("started $CHILDREN children", parent.result.get()) | ||
| assertEquals(2, fault.hits.get(), "the parent is retried and starts the failed child again") | ||
| for (i in 1..CHILDREN) { | ||
| val child = awaitTerminal(childId(parentId, i)) | ||
| assertEquals(WorkflowInstance.Status.COMPLETED, child.status) | ||
| assertEquals(parentId, child.parentWorkflowId) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun schedulerTimeoutWhileSchedulingAChildRetriesTheParent() { | ||
| val parentId = "fan-out-${System.nanoTime()}" | ||
| // The child's instance commits, then inserting its task times out. The retried parent finds the | ||
| // instance already there and schedules it again. | ||
| val fault = Fault(childId(parentId, 2)) { TimeoutException("injected timeout inserting the child's task") } | ||
| start(schedulerFault = fault) | ||
| startParent(parentId) | ||
| assertParentRetriedAndCompleted(parentId, fault) | ||
| } | ||
|
|
||
| @Test | ||
| fun storeTimeoutWhileCreatingAChildRetriesTheParent() { | ||
| val parentId = "fan-out-${System.nanoTime()}" | ||
| val fault = Fault(childId(parentId, 2)) { TimeoutException("injected timeout creating the child's instance") } | ||
| start(storeFault = fault) | ||
| startParent(parentId) | ||
| assertParentRetriedAndCompleted(parentId, fault) | ||
| } | ||
|
|
||
| @Test | ||
| fun retryableErrorWhileCreatingAChildRetriesTheParent() { | ||
| val parentId = "fan-out-${System.nanoTime()}" | ||
| // Already a RetryableError, so it is rethrown unchanged and the executor retries the parent on it. | ||
| val fault = Fault(childId(parentId, 2)) { OptimisticLockingError("injected conflict creating the child's instance") } | ||
| start(storeFault = fault) | ||
| startParent(parentId) | ||
| assertParentRetriedAndCompleted(parentId, fault) | ||
| } | ||
|
|
||
| @Test | ||
| fun invalidChildRequestStillFailsTheParent() { | ||
| val parentId = "fan-out-${System.nanoTime()}" | ||
| val fault = Fault(childId(parentId, 2)) { IllegalArgumentException("injected invalid child request") } | ||
| start(storeFault = fault) | ||
| startParent(parentId) | ||
|
|
||
| val parent = awaitTerminal(parentId) | ||
| assertEquals(WorkflowInstance.Status.ERROR, parent.status) | ||
| assertEquals(1, fault.hits.get(), "an invalid request is not retried") | ||
| } | ||
|
|
||
| @Test | ||
| fun invocationWithoutAParentStillFailsTheParentOnATransientStartFailure() { | ||
| val parentId = "fan-out-${System.nanoTime()}" | ||
| val fault = Fault(childId(parentId, 2)) { TimeoutException("injected timeout creating the child's instance") } | ||
| start(storeFault = fault) | ||
| startParent(parentId, PlainFanOutWorkflow::class.java) | ||
|
|
||
| val parent = awaitTerminal(parentId) | ||
| assertEquals(WorkflowInstance.Status.ERROR, parent.status, "only a child invocation retries the parent") | ||
| assertEquals(1, fault.hits.get()) | ||
| } | ||
|
|
||
| private companion object { | ||
| const val CHILDREN = 3 | ||
|
|
||
| fun childId( | ||
| parentId: String, | ||
| i: Int | ||
| ) = "$parentId-child-$i" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skippererror could also be retryable error? in which case we'd like to treat it as retryable?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and it already is.
RetryableErrorextendsSkipperError, so case 1 rethrows it unchanged. From thereWorkflowExecutor.handleExecutionErrorputs the parent inTRANSIENT_ERRORand reruns it on the error's own delay or strategy. Only errors Skipper doesn't recognize get rewrapped, so aNonRetryableErrorstill fails the parent and aRetryableErrorstill retries it.Done: in b60c9d9 I made this explicit in the KDoc (case 1 now names
RetryableErrorandOptimisticLockingErroras examples). I also addedretryableErrorWhileCreatingAChildRetriesTheParent, which injects anOptimisticLockingErrorfrom the store while starting a child and asserts the parent retries and completes. That test passes with or withoutasChildOf, since this passthrough is existing executor behavior; it pins the behavior so it doesn't change. The unit testtestChildStartClassifiedOrInvalidFailureIsRethrownUnchangedchecks that the same instance is rethrown.