Class WorkflowEngine
- All Implemented Interfaces:
WorkflowEngineCheckpointingSupport.CheckpointLatchCoordinator,MessageHandler,EventHandler
- Since:
- 5.4.0
- Author:
- Allard Buijze, Simon Zambrovski, Stefan Dragisic, Steven van Beelen
-
Constructor Summary
ConstructorsConstructorDescriptionWorkflowEngine(WorkflowConfigurationRegistry<?> workflowConfigurationRegistry, WorkflowExecutionRepository workflowExecutionRepository, WorkflowCancellationService workflowCancellationService, WorkflowStore workflowStore, UnitOfWorkFactory unitOfWorkFactory) Creates a new workflow engine. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCheckpointLatch(Segment segment, Runnable latch) Adds a checkpoint latch across the current set ofWorkflowExecutionsowned by the givensegmentthat are still performing tasks.handle(EventMessage event, ProcessingContext context) Handles the giveneventwithin the givencontext.booleanhasUnsafeCheckpointWork(Segment segment) Returns whether anyWorkflowExecutionsowned by the givensegmentstill makes checkpoint advancement unsafe.booleanholdsCheckpoint(Segment segment) releaseWorkflowsFor(Segment segment) Drops the workflow executions owned by a segment this node no longer holds.restoreWorkflowsFor(Segment segment, @Nullable TrackingToken claimedFrom, ProcessingContext sourcingContext, ProcessingContext executionContext) Restores and starts the workflow executions owned by a segment this node just claimed.Returns the sequencing policy derived from this engine's workflow definitions, which delivers each event to the segment owning the workflow instance it affects.voidsetCheckpointingSupport(WorkflowEngineCheckpointingSupport checkpointingSupport) Sets the checkpointing support required by this engine.voidshutdown()Shuts downs the engine and removes all running workflow executions.start(@Nullable TrackingToken latestToken) Records the stream position that restored segments must reach before their workflow bodies start.Retrieve all workflow executions.
-
Constructor Details
-
WorkflowEngine
@Internal public WorkflowEngine(WorkflowConfigurationRegistry<?> workflowConfigurationRegistry, WorkflowExecutionRepository workflowExecutionRepository, WorkflowCancellationService workflowCancellationService, WorkflowStore workflowStore, UnitOfWorkFactory unitOfWorkFactory) Creates a new workflow engine. Be sure to follow-up construction of aWorkflowEnginewith an invocation ofsetCheckpointingSupport(WorkflowEngineCheckpointingSupport), as otherwise checkpointing support is unavailable.- Parameters:
workflowConfigurationRegistry- registry containing aworkflow configurationworkflowExecutionRepository- repository dedicated towardsWorkflowExecutionstorage and retrievalworkflowCancellationService- workflow cancellation serviceworkflowStore- dedicated store for workflow information, likeRunningWorkflowsandWorkflowStateunitOfWorkFactory- a unit of work factory dedicated to construct a unit of work duringof this engineinvalid reference
#start(TrackingToken, boolean)
-
-
Method Details
-
setCheckpointingSupport
@Internal public void setCheckpointingSupport(WorkflowEngineCheckpointingSupport checkpointingSupport) Sets the checkpointing support required by this engine.The component is set outside the
WorkflowEngine(WorkflowConfigurationRegistry, WorkflowExecutionRepository, WorkflowCancellationService, WorkflowStore, UnitOfWorkFactory), because they require thisWorkflowEngineitself to function. Hence, a cyclic dependency would exist upon start-up if completion otherwise.- Parameters:
checkpointingSupport- provides checkpointing support to thisWorkflowEngine
-
handle
Description copied from interface:EventHandler- Specified by:
handlein interfaceEventHandler- Parameters:
event- The event to handle.context- The context to the giveneventis handled in.- Returns:
- An
empty streamcontaining nothing.
-
hasUnsafeCheckpointWork
Description copied from interface:WorkflowEngineCheckpointingSupport.CheckpointLatchCoordinatorReturns whether anyWorkflowExecutionsowned by the givensegmentstill makes checkpoint advancement unsafe.- Specified by:
hasUnsafeCheckpointWorkin interfaceWorkflowEngineCheckpointingSupport.CheckpointLatchCoordinator- Parameters:
segment- the segment whose checkpoint is being advanced- Returns:
truewhen checkpoint advancement must wait,falseotherwise
-
holdsCheckpoint
- Specified by:
holdsCheckpointin interfaceWorkflowEngineCheckpointingSupport.CheckpointLatchCoordinator
-
addCheckpointLatch
Adds a checkpoint latch across the current set ofWorkflowExecutionsowned by the givensegmentthat are still performing tasks.The given
latchis attached to allWorkflowExecutionsthat still have tasks to perform. Or in other terms, executions that are "unsafe" to checkpoint on. When no execution is unsafe at the moment the latch is attached, the latch runs straight away so safety is re-checked, covering a workflow that appended work concurrently.- Specified by:
addCheckpointLatchin interfaceWorkflowEngineCheckpointingSupport.CheckpointLatchCoordinator- Parameters:
segment- the segment whose checkpoint is being advancedlatch- the latch to invoke after all unsafeWorkflowExecutionshave reached it
-
workflowExecutions
Retrieve all workflow executions.- Returns:
- set of currently running workflow executions.
-
start
Records the stream position that restored segments must reach before their workflow bodies start.- Parameters:
latestToken- latest token observed while starting the processor- Returns:
- a completed future after startup validation
-
restoreWorkflowsFor
public CompletableFuture<Void> restoreWorkflowsFor(Segment segment, @Nullable TrackingToken claimedFrom, ProcessingContext sourcingContext, ProcessingContext executionContext) Restores and starts the workflow executions owned by a segment this node just claimed.Restoration is per segment rather than per node: a node materializes only the instances of the segments it holds, so a segment migrating between nodes carries its instances with it. When another node dies, the coordinator hands its segments to a surviving node and this callback rebuilds their instances there, without restarting anything.
The instances are materialized from their current durable state and started immediately.
- Parameters:
segment- the segment that was claimedclaimedFrom- the stored position the segment resumes from, ornullwhen it has consumed nothing. This is what tells a segment sitting at the end of the stream from one far behind, on a node that never held the segment as well as on one that didsourcingContext- short-lived context used to load durable workflow stateexecutionContext- independent context used as the parent of the restored workflow executions. ReusingsourcingContexthere is invalid because restored workflow bodies run asynchronously and can outlive the claim callback. If such a body appends an event after the sourcing unit of work has enteredCOMMIT, Axon can no longer register the requiredPREPARE_COMMIThandler, and the workflow cannot persist its resumed, timed-out, or terminal state- Returns:
- a future that completes once the segment's executions are restored and, unless the segment is still
replaying, started. It completes exceptionally when loading durable state does not finish within
DEFAULT_RESTORE_TIMEOUT, failing the claim rather than holding the segment
-
releaseWorkflowsFor
Drops the workflow executions owned by a segment this node no longer holds.In-flight steps are stopped rather than cancelled, exactly as on
shutdown(): no cancellation events are emitted, so the node claiming the segment next resumes each instance from its persisted state.- Parameters:
segment- the segment that was released- Returns:
- a future completed after this engine has requested every released execution to stop
-
shutdown
public void shutdown()Shuts downs the engine and removes all running workflow executions.Before clearing the repository, all in-flight step futures are interrupted so that workflow driver threads parked in
sleepAsync/waitForEvent/ asyncexecutecan exit. This is an interrupt, not a cancellation: no<Step>Cancelled/<Workflow>Cancelledevents are emitted, so the persisted event stream still reflects the most recent<Step>Startedand the step resumes on the next app start. Without this, a graceful shutdown can hang because the workflow executor (e.g. a virtual-thread-per-task executor) blocks onclose()waiting for those threads to terminate. See issue #125.A workflow driver that exits after this call skips its termination handler, so it touches no engine state and requests no checkpoint.
-
segmentedRouting
Returns the sequencing policy derived from this engine's workflow definitions, which delivers each event to the segment owning the workflow instance it affects.- Returns:
- the sequencing policy of this engine.
-