[Improvement][Master] Initialize WorkflowGraph/WorkflowExecutionGraph when `WorkflowStartLifecycleEvent` fired.
Search before asking
- [x] I had searched in the issues and found no similar feature requirement.
Description
It's better not initialize WorkflowGraph/WorkflowExecutionGraph when handle command, and initialize these when start event fired. The advantage is that we can reduce transaction time when processing commands, and once initialize graph failed, we can trigger a workflow instance failure event.
Are you willing to submit a PR?
- [x] Yes I am willing to submit a PR!
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
hey @SbloodyS & @ruanwenjun can you assign this issue to me i want to work on this........
hey @ruanwenjun thank you for assigning ill working on it.....
hey @ruanwenjun can you review this and give feedback : https://github.com/apache/dolphinscheduler/pull/17953
@GaneshPatil7517 Please provide a design before you submit a PR. This issue involves code in the core module and is not a good first issue. Currently, I see that you've only made some simple code changes. Do you understand what these code changes mean? You may need to familiarize with the architecture and processes first; otherwise, the code is quite dangerous.
Hi @ruanwenjun, thank you for the feedback. I apologize for not providing a design document upfront.
Here's my understanding of the architecture and the proposed changes:
Problem Currently, WorkflowExecutionGraph is created inside handleCommand() which runs within a database transaction. This increases transaction time and lock contention.
Current Flow handleCommand() [@Transactional] → assembleWorkflowInstance() → assembleWorkflowGraph() → createWorkflowExecutionGraphAssembler() ← Graph created HERE (inside tx) → triggerWorkflow()
Proposed Flow handleCommand() [@Transactional] → assembleWorkflowInstance() → assembleWorkflowGraph() → triggerWorkflow() (no graph yet)
WorkflowRunningStateAction.onStartEvent() [OUTSIDE tx] → WorkflowExecutionGraphFactory.createGraph() ← Graph created HERE → context.setWorkflowExecutionGraph(graph)
Key Changes WorkflowExecutionGraphFactory - Centralizes graph creation for all command types (START_PROCESS, REPEAT_RUNNING, START_FAILURE_TASK_PROCESS, RECOVER_TOLERANCE_FAULT_PROCESS, RECOVER_SERIAL_WAIT) Removed assembler pattern - Per your earlier feedback that "command handlers should not be concerned with how instances are initialized" Deferred initialization - Graph is created when WorkflowStartLifecycleEvent fires, after the transaction Please let me know if you'd like me to elaborate on any part of the design or if this approach needs adjustment.
Would you like me to modify the design document or create a different format for posting on the PR?> handleCommand() [@Transactional]
→ assembleWorkflowInstance() → assembleWorkflowGraph() → createWorkflowExecutionGraphAssembler() ← Graph created HERE (inside tx) → triggerWorkflow()
Proposed Flow
handleCommand() [@Transactional] → assembleWorkflowInstance() → assembleWorkflowGraph() → triggerWorkflow() (no graph yet)
WorkflowRunningStateAction.onStartEvent() [OUTSIDE tx] → WorkflowExecutionGraphFactory.createGraph() ← Graph created HERE → context.setWorkflowExecutionGraph(graph)
Key Changes
- WorkflowExecutionGraphFactory - Centralizes graph creation for all command types (START_PROCESS, REPEAT_RUNNING, START_FAILURE_TASK_PROCESS, RECOVER_TOLERANCE_FAULT_PROCESS, RECOVER_SERIAL_WAIT)
- Removed assembler pattern - Per your earlier feedback that "command handlers should not be concerned with how instances are initialized"
- Deferred initialization - Graph is created when WorkflowStartLifecycleEvent fires, after the transaction
Please let me know if you'd like me to elaborate on any part of the design or if this approach needs adjustment.
@GaneshPatil7517 Yes, the current process is indeed as you described. Please tell me honestly whether this was generated by AI—it's important.
In the current implementation, moving the WorkflowExecutionGraph to post-processing is risky. The graph manages task instance states during construction, which is tightly coupled with the workflow instance state. Shifting it to post-processing may lead to state inconsistencies during failover, where task instance states and the workflow instance state can diverge.
Hi @ruanwenjun,
I want to be transparent with you - yes, I am using AI assistance (GitHub Copilot) to help with this contribution. I am learning the codebase and using it as a tool to understand the architecture and implement changes. I should have disclosed this upfront, and I apologize.
Regarding your technical concern - you're absolutely right. I hadn't fully considered the failover scenario:
The Risk:
- handleCommand() commits transaction → WorkflowInstance persisted
- Master crashes before onStartEvent() executes
- New Master picks up workflow for failover
- WorkflowExecutionGraph was never created → State inconsistency
The current design where graph creation happens inside the transaction ensures atomicity - either both the WorkflowInstance AND the graph state are persisted, or neither.
Given this, I have a few questions: 1.Should this issue be closed as "won't fix" since the current design is intentional for consistency? 2. Or is there an alternative approach that could reduce transaction time while maintaining consistency (perhaps caching/optimizing the graph creation rather than deferring it)? I appreciate you taking the time to explain this. It helps me understand why the current architecture is designed this way.
@GaneshPatil7517 This issue deserves optimization, as the current transaction locking mechanism exist performance impact. However, addressing it is non-trivial and would require a series of changes across the lifecycle. I’m still working through the design and haven’t reached a solid solution yet, so I haven’t submitted a PR at this stage..
Thanks for the clarification, @ruanwenjun It's reassuring to know the optimization goal is valid.
I understand this requires careful design work. I'd be happy to:
Collaborate on the design if you'd like another perspective Wait for your design direction and then help with implementation Please let me know how I can best contribute. I'm willing to iterate on whatever approach you think is safest for the failover scenarios.
Hi @ruanwenjun, I checked the history of this issue and the closed PR #17953. I agree that simply moving WorkflowGraph / WorkflowExecutionGraph creation into WorkflowStartLifecycleEvent is unsafe, especially if the master crashes between command handling and the start event.
Here is a different design direction that avoids keeping graph construction inside the command transaction, but also avoids lazy initialization without a durable lifecycle boundary.
Proposed direction
Introduce an explicit workflow execution bootstrap phase between command persistence and workflow start.
1. Keep command handling focused on durable workflow metadata
The command handler should create/update the durable workflow instance and command-related metadata inside the transaction, but should not construct the in-memory WorkflowExecutionGraph while the command transaction is held.
The command transaction should leave the workflow in a durable state that means roughly: "workflow instance created, execution bootstrap still pending".
This could be represented by either:
- a new workflow instance state / flag such as
INITIALIZINGorBOOTSTRAPPING; or - an existing state plus a durable bootstrap marker, if adding a new state is too invasive.
2. Add a bootstrapper before WorkflowStartLifecycleEvent
After the command transaction commits, a dedicated WorkflowExecutionBootstrapper can:
- assemble
WorkflowGraphandWorkflowExecutionGraphoutside the command transaction; - initialize task execution state in one well-defined place;
- attach the initialized graph to
WorkflowExecuteContext; - only then publish
WorkflowStartLifecycleEvent.
So WorkflowStartLifecycleEvent would still assume that the graph is ready. It would not become the component that constructs the graph.
3. Make bootstrap idempotent for failover
The key invariant should be: if the master crashes after the command transaction but before the start event, the new master can detect a bootstrap-pending workflow instance and safely run the bootstrapper again.
Possible rules:
- if no task instances / execution graph have been materialized yet, reconstruct from workflow definition and bootstrap normally;
- if task instances were already materialized, rebuild the in-memory graph from persisted task-instance state instead of creating duplicates;
- bootstrap should be guarded so repeated attempts do not create duplicate task instances or inconsistent edges.
4. Add a separate failure path for bootstrap failures
If graph construction fails before any task execution graph exists, the current normal failed-event path may not be sufficient because WorkflowRunningStateAction.onFailedEvent expects a failed task chain in the graph.
A safer design may need a separate bootstrap failure transition, for example:
- mark the workflow instance as failed;
- move or clean the related command if needed;
- record the bootstrap error;
- avoid requiring
WorkflowExecutionGraph.isExistFailureTaskExecutionChain()when the graph was never initialized.
5. Suggested staged implementation
- First PR: add regression/integration tests that describe the crash/failover boundary and graph-initialization-failure behavior, without changing the production flow.
- Second PR: introduce the bootstrap state/marker and
WorkflowExecutionBootstrapper, but keep existing command types functionally equivalent. - Third PR: move graph construction for the normal run-workflow path out of the command transaction.
- Later PRs: migrate rerun/recover/failover command paths one by one.
Questions
- Is a new workflow instance state/marker for bootstrap-pending acceptable, or should this be modeled with existing states only?
- Should
WorkflowStartLifecycleEventcontinue to require a fully initializedWorkflowExecutionGraph, with initialization handled just before publishing the event? - What persisted invariants should hold across these three boundaries: command transaction committed, bootstrap completed, start event published?
- Would a first PR that only adds failover/bootstrap regression tests be useful before implementation?
I do not want to repeat the unsafe approach from #17953, so I would like to align on these lifecycle/failover invariants before preparing code.