How does workflow replays interact with the Temporal.sleep() function?

I have a question about the following scenario. Suppose I have

class MyWorkflow() {
    activityA()
    Workflow.sleep(Duration.of(7 days))
    activityB()
}

If activity A is completed successfully, and somehow does the Temporal workflow dies 3 days into the workflow.sleep(), on replay will the workflow resume from the start of the sleep function? Or within the exact 3 days in time of when the workflow died? From my understanding, the event history keeps track of which activities have been completed (so it will at least resume from the start of the sleep function), but not sure if it the duration of the sleep function is captured.

When your worker runs Workflow.sleep, it completes workflow task and sends workflow task completion request to server which includes a start timer command. Server then adds TimerStarted event to event history which would have the sleep duration of 7 days, and create a durable timer for it.
When your worker comes back up after 3 days it would not replay event history of this execution until it receives a workflow task for it.
When server timer fires after 7 days, server then adds TimerFired event to event history and creates a workflow task that includes this timer fired event. This task can then be dispatched to your worker which at this point would replay this execution, so run your workflow code from beginning, and compare it to event history. So it would run your workflow code again up to Workflow.sleep (note it would not re-execute activityA since its completion is already recorded in history), then it would look at new events in workflow task received and see that there is a TimerFired event, and unblock Workflow.sleep, then continue to next line and schedule activityB.

So,

will the workflow resume from the start of the sleep function?

no

but not sure if it the duration of the sleep function is captured.

yes it is captured, so on replay you would not start sleep timer again

Note that the implementation uses replay to recover state. So during replay, the whole function is reexecuted. But when developing your application, you don’t need to think about this (besides ensuring determinism). From a logical point of view, the workflow function is executed exactly once.

1 Like