forked from microsoft/durabletask-go
-
Notifications
You must be signed in to change notification settings - Fork 17
wf history ctx propagation #85
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
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8809e44
feat: workflow history/context propagation
cicoyle a6cd3fa
fix lint
cicoyle 5c0326a
add test
cicoyle babc5e9
PR feedback
cicoyle dd60212
NO_HISTORY_PROPAGATION
cicoyle 6e53de2
update submodule
cicoyle c5bcae3
address PR feedback - logic is in dapr, add comment about it
cicoyle af33f67
support CAN + rerun
cicoyle 38944ed
proto name updates and rm mtls dep
cicoyle 65b6243
PR feedback, return err
cicoyle 2dbd830
use latest durabletask-protobuf
cicoyle 27e7be6
update submodule
cicoyle ff8192a
PR feedback and add test to not set any defaults
cicoyle c711268
fix typo
cicoyle aed7f0d
Update api/propagation.go
cicoyle 339f956
fix build
cicoyle 7938cf0
fix build
cicoyle 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| [submodule "submodules/durabletask-protobuf"] | ||
| path = submodules/durabletask-protobuf | ||
| url = https://github.com/dapr/durabletask-protobuf | ||
| branch = main | ||
| ; I will update this once my durabletask-protobuf PR is merged | ||
| url = https://github.com/cicoyle/durabletask-protobuf | ||
| branch = feature-wf-ctx-propagation | ||
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,341 @@ | ||
| /* | ||
| Copyright 2026 The Dapr Authors | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "github.com/dapr/durabletask-go/api/protos" | ||
| "google.golang.org/protobuf/types/known/wrapperspb" | ||
| ) | ||
|
|
||
| // PropagationOption is a functional option for configuring history propagation. | ||
| // Pass one of PropagateOwnHistory() or PropagateLineage(). | ||
| type PropagationOption func(*protos.HistoryPropagationScope) | ||
|
|
||
| // PropagateOwnHistory configures propagation to include the caller's own | ||
| // history events (activities, child workflows, timers, etc.). The child only | ||
| // sees events from the immediate parent, not the ancestor chain. | ||
| func PropagateOwnHistory() PropagationOption { | ||
| return func(s *protos.HistoryPropagationScope) { | ||
| *s = protos.HistoryPropagationScope_HISTORY_PROPAGATION_SCOPE_OWN_HISTORY | ||
| } | ||
| } | ||
|
|
||
| // PropagateLineage configures propagation to include the caller's own history | ||
| // events AND the full ancestor chain. Any propagated history this workflow | ||
| // received from its own parent is forwarded to the child, preserving the full | ||
| // chain of custody. | ||
| func PropagateLineage() PropagationOption { | ||
| return func(s *protos.HistoryPropagationScope) { | ||
| *s = protos.HistoryPropagationScope_HISTORY_PROPAGATION_SCOPE_LINEAGE | ||
| } | ||
| } | ||
|
|
||
| // NewHistoryPropagationScope creates a HistoryPropagationScope from the given option. | ||
| // A nil option is treated as NO_HISTORY_PROPAGATION. | ||
| func NewHistoryPropagationScope(opt PropagationOption) protos.HistoryPropagationScope { | ||
| var scope protos.HistoryPropagationScope | ||
| if opt != nil { | ||
| opt(&scope) | ||
| } | ||
| return scope | ||
| } | ||
|
|
||
| // historyChunk represents a contiguous range of events produced by a single | ||
| // workflow instance | ||
| type historyChunk struct { | ||
| appID string | ||
| startEventIndex int | ||
| eventCount int | ||
| instanceID string | ||
| workflowName string | ||
| } | ||
|
|
||
| // PropagatedHistory is the history propagated from a parent workflow to a | ||
| // child wf or activity | ||
| type PropagatedHistory struct { | ||
| events []*protos.HistoryEvent | ||
| scope protos.HistoryPropagationScope | ||
| chunks []historyChunk | ||
| } | ||
|
|
||
| // Events returns all propagated history events. | ||
| func (ph *PropagatedHistory) Events() []*protos.HistoryEvent { | ||
| return ph.events | ||
| } | ||
|
|
||
| // Scope returns the propagation scope used to produce this history. | ||
| func (ph *PropagatedHistory) Scope() protos.HistoryPropagationScope { | ||
| return ph.scope | ||
| } | ||
|
|
||
| // GetAppIDs returns the ordered, deduplicated list of app IDs in the history chain. | ||
| func (ph *PropagatedHistory) GetAppIDs() []string { | ||
| seen := make(map[string]bool) | ||
| var ids []string | ||
| for _, chunk := range ph.chunks { | ||
| if !seen[chunk.appID] { | ||
| seen[chunk.appID] = true | ||
| ids = append(ids, chunk.appID) | ||
| } | ||
| } | ||
| return ids | ||
| } | ||
|
|
||
| // GetEventsByAppID returns only the events produced by the given app. | ||
| func (ph *PropagatedHistory) GetEventsByAppID(appID string) []*protos.HistoryEvent { | ||
| var events []*protos.HistoryEvent | ||
| for _, chunk := range ph.chunks { | ||
| if chunk.appID == appID { | ||
| events = append(events, ph.chunkEvents(chunk)...) | ||
| } | ||
| } | ||
| return events | ||
| } | ||
|
|
||
| // GetEventsByInstanceID returns only the events produced by the given workflow instance. | ||
| func (ph *PropagatedHistory) GetEventsByInstanceID(instanceID string) []*protos.HistoryEvent { | ||
| var events []*protos.HistoryEvent | ||
| for _, chunk := range ph.chunks { | ||
| if chunk.instanceID == instanceID { | ||
| events = append(events, ph.chunkEvents(chunk)...) | ||
| } | ||
| } | ||
| return events | ||
| } | ||
|
|
||
| // GetEventsByWorkflowName returns only the events produced by workflows with the given name. | ||
| func (ph *PropagatedHistory) GetEventsByWorkflowName(name string) []*protos.HistoryEvent { | ||
| var events []*protos.HistoryEvent | ||
| for _, chunk := range ph.chunks { | ||
| if chunk.workflowName == name { | ||
| events = append(events, ph.chunkEvents(chunk)...) | ||
| } | ||
| } | ||
| return events | ||
| } | ||
|
|
||
| // chunkEvents returns the slice of events for a given chunk, with bounds | ||
| // checking against malformed or forged propagated history | ||
| func (ph *PropagatedHistory) chunkEvents(chunk historyChunk) []*protos.HistoryEvent { | ||
| start := chunk.startEventIndex | ||
| if start < 0 || start > len(ph.events) { | ||
| return nil | ||
| } | ||
| end := start + chunk.eventCount | ||
| if end > len(ph.events) { | ||
| end = len(ph.events) | ||
| } | ||
| if end < start { | ||
| return nil | ||
| } | ||
| return ph.events[start:end] | ||
|
cicoyle marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // WorkflowResult is a scoped view of a single workflow's chunk in propagated history. | ||
| // Use GetActivityByName or GetChildWorkflowByName to query specific items. | ||
| type WorkflowResult struct { | ||
| Found bool | ||
| InstanceID string | ||
| AppID string | ||
| Name string // wf name | ||
| events []*protos.HistoryEvent | ||
| } | ||
|
|
||
| // ActivityResult holds the status and data of a named activity. | ||
| type ActivityResult struct { | ||
| Name string // activity name | ||
| Started bool | ||
| Completed bool | ||
| Failed bool | ||
| Input *wrapperspb.StringValue | ||
| Output *wrapperspb.StringValue | ||
| Error *protos.TaskFailureDetails | ||
| } | ||
|
|
||
| // ChildWorkflowResult holds the status and data of a named child workflow. | ||
| type ChildWorkflowResult struct { | ||
| Name string // child wf name | ||
| Started bool | ||
| Completed bool | ||
| Failed bool | ||
| Output *wrapperspb.StringValue | ||
| Error *protos.TaskFailureDetails | ||
| } | ||
|
|
||
| // makeWorkflowResult creates a WorkflowResult from a chunk. | ||
| func (ph *PropagatedHistory) makeWorkflowResult(chunk historyChunk) WorkflowResult { | ||
| return WorkflowResult{ | ||
| Found: true, | ||
| InstanceID: chunk.instanceID, | ||
| AppID: chunk.appID, | ||
| Name: chunk.workflowName, | ||
| events: ph.chunkEvents(chunk), | ||
| } | ||
| } | ||
|
|
||
| // GetWorkflows returns all workflow results in the propagated history chain, | ||
| // in execution order (ancestor first, then own) | ||
| func (ph *PropagatedHistory) GetWorkflows() []WorkflowResult { | ||
| results := make([]WorkflowResult, 0, len(ph.chunks)) | ||
| for _, chunk := range ph.chunks { | ||
| results = append(results, ph.makeWorkflowResult(chunk)) | ||
| } | ||
| return results | ||
| } | ||
|
|
||
| // GetWorkflowByName returns the last workflow result matching the given name. | ||
| // Returns the most recent instance, which is the final outcome after retries. | ||
| // Returns a zero WorkflowResult if not found | ||
| func (ph *PropagatedHistory) GetWorkflowByName(name string) WorkflowResult { | ||
| all := ph.GetWorkflowsByName(name) | ||
| if len(all) == 0 { | ||
| return WorkflowResult{} | ||
| } | ||
| return all[len(all)-1] | ||
| } | ||
|
|
||
| // GetWorkflowsByName returns all workflow results matching the given name, | ||
| // in execution order. Returns nil if none found. | ||
| func (ph *PropagatedHistory) GetWorkflowsByName(name string) []WorkflowResult { | ||
| var results []WorkflowResult | ||
| for _, chunk := range ph.chunks { | ||
| if chunk.workflowName == name { | ||
| results = append(results, ph.makeWorkflowResult(chunk)) | ||
| } | ||
| } | ||
| return results | ||
| } | ||
|
|
||
| // resolveActivity builds an ActivityResult for a single TaskScheduled history | ||
| // event and its matching TaskCompleted/TaskFailed. Matching is done by the | ||
| // scheduling event's ID (TaskCompletedEvent.taskScheduledId / | ||
| // TaskFailedEvent.taskScheduledId), NOT by taskExecutionId, because SDK | ||
| // retries reuse the same taskExecutionId | ||
| func resolveActivity(events []*protos.HistoryEvent, scheduleEvent *protos.HistoryEvent) ActivityResult { | ||
| ts := scheduleEvent.GetTaskScheduled() | ||
| result := ActivityResult{ | ||
| Name: ts.GetName(), | ||
| Started: true, | ||
| Input: ts.GetInput(), | ||
| } | ||
| scheduleID := scheduleEvent.GetEventId() | ||
| for _, e := range events { | ||
| if tc := e.GetTaskCompleted(); tc != nil && tc.GetTaskScheduledId() == scheduleID { | ||
| result.Completed = true | ||
| result.Output = tc.GetResult() | ||
| } | ||
| if tf := e.GetTaskFailed(); tf != nil && tf.GetTaskScheduledId() == scheduleID { | ||
| result.Failed = true | ||
| result.Error = tf.GetFailureDetails() | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // GetActivityByName returns the last activity result matching the given name. | ||
| // Returns the most recent invocation, which is the final outcome after retries. | ||
| // Returns a zero ActivityResult if not found or if the wf was not found. | ||
| func (wr WorkflowResult) GetActivityByName(name string) ActivityResult { | ||
| all := wr.GetActivitiesByName(name) | ||
| if len(all) == 0 { | ||
| return ActivityResult{Name: name} | ||
|
cicoyle marked this conversation as resolved.
Outdated
|
||
| } | ||
| return all[len(all)-1] | ||
| } | ||
|
|
||
| // GetActivitiesByName returns all activity results matching the given name, | ||
| // in execution order. Useful when the same activity is called multiple times | ||
| // (ex retries/loops). Returns nil if none found. | ||
| func (wr WorkflowResult) GetActivitiesByName(name string) []ActivityResult { | ||
|
cicoyle marked this conversation as resolved.
Outdated
|
||
| if !wr.Found { | ||
| return nil | ||
| } | ||
| var results []ActivityResult | ||
| for _, e := range wr.events { | ||
| if ts := e.GetTaskScheduled(); ts != nil && ts.GetName() == name { | ||
| results = append(results, resolveActivity(wr.events, e)) | ||
| } | ||
| } | ||
| return results | ||
| } | ||
|
|
||
| // resolveChildWorkflow builds a ChildWorkflowResult for a single | ||
| // ChildWorkflowInstanceCreated event and its matching Completed/Failed. | ||
| func resolveChildWorkflow(events []*protos.HistoryEvent, eventID int32) ChildWorkflowResult { | ||
| result := ChildWorkflowResult{Started: true} | ||
| for _, e := range events { | ||
| if cw := e.GetChildWorkflowInstanceCreated(); cw != nil && e.GetEventId() == eventID { | ||
| result.Name = cw.GetName() | ||
| } | ||
| if cc := e.GetChildWorkflowInstanceCompleted(); cc != nil && cc.GetTaskScheduledId() == eventID { | ||
| result.Completed = true | ||
| result.Output = cc.GetResult() | ||
| } | ||
| if cf := e.GetChildWorkflowInstanceFailed(); cf != nil && cf.GetTaskScheduledId() == eventID { | ||
| result.Failed = true | ||
| result.Error = cf.GetFailureDetails() | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // GetChildWorkflowByName returns the last child workflow result matching the given name. | ||
| // Returns the most recent invocation, which is the final outcome after retries. | ||
| // Returns a zero ChildWorkflowResult if not found or if the wf was not found. | ||
| func (wr WorkflowResult) GetChildWorkflowByName(name string) ChildWorkflowResult { | ||
| all := wr.GetChildWorkflowsByName(name) | ||
| if len(all) == 0 { | ||
| return ChildWorkflowResult{Name: name} | ||
| } | ||
| return all[len(all)-1] | ||
| } | ||
|
|
||
| // GetChildWorkflowsByName returns all child workflow results matching the given name, | ||
| // in execution order. Returns nil if none found. | ||
| func (wr WorkflowResult) GetChildWorkflowsByName(name string) []ChildWorkflowResult { | ||
| if !wr.Found { | ||
| return nil | ||
| } | ||
| var results []ChildWorkflowResult | ||
| for _, e := range wr.events { | ||
| if cw := e.GetChildWorkflowInstanceCreated(); cw != nil && cw.GetName() == name { | ||
| results = append(results, resolveChildWorkflow(wr.events, e.GetEventId())) | ||
| } | ||
| } | ||
| return results | ||
| } | ||
|
|
||
| // PropagatedHistoryFromProto converts a proto PropagatedHistory to the Go type. | ||
| func PropagatedHistoryFromProto(ph *protos.PropagatedHistory) *PropagatedHistory { | ||
| if ph == nil { | ||
| return nil | ||
| } | ||
| chunks := make([]historyChunk, len(ph.GetChunks())) | ||
| for i, c := range ph.GetChunks() { | ||
| chunks[i] = historyChunk{ | ||
| appID: c.GetAppId(), | ||
| startEventIndex: int(c.GetStartEventIndex()), | ||
| eventCount: int(c.GetEventCount()), | ||
| instanceID: c.GetInstanceId(), | ||
| workflowName: c.GetWorkflowName(), | ||
| } | ||
| } | ||
| return &PropagatedHistory{ | ||
| events: ph.GetEvents(), | ||
| scope: ph.GetScope(), | ||
| chunks: chunks, | ||
| } | ||
| } | ||
|
cicoyle marked this conversation as resolved.
Outdated
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.