feat: Allow shards to create history tasks
๐ Stacked PR (1/3). Previous: none (base
master) ยท Next: #8286
What changed?
Add ShardContext.ReinjectHistoryTasks([]persistence.Task), which writes history tasks (transfer/timer) directly into the executions table under the shard lock, advancing the shard's max read level. Plumbs a new create-history-tasks path down through ExecutionManager โ nosql/sql execution stores โ nosqlplugin interfaces, with the generated mocks and persistence wrappers (errorinjectors/metered/ratelimited) regenerated.
Why?
https://github.com/cadence-workflow/cadence/issues/6697
Draining the history task DLQ requires putting tasks back onto a shard's queues so the normal processors pick them up. Shards previously had no way to create raw history tasks outside of the workflow mutation path. This adds a minimal, lock-safe entry point that respects the shard's read levels, which the DLQ processor (next PR) builds on.
How did you test it?
go test -race ./service/history/shard/... \
./common/persistence/nosql/... ./common/persistence/...
Potential risks
Touches core persistence interfaces and execution-store write paths; widely regenerated mocks/wrappers. Tasks are written under the shard lock and advance max read level โ incorrect task keys could affect read-level accounting. No schema change.
Release notes
N/A โ internal plumbing, not user-facing on its own.
Documentation Changes
N/A
Enables history task reinjection by adding ๐ service/history/shard/context.go:1546-1560
ReinjectHistoryTasks groups all input tasks by category into ๐ service/history/shard/context.go:1561-1575
In ๐ service/history/shard/context.go:1552-1566
ReinjectHistoryTasks holds the shard write lock (s.Lock()/defer s.Unlock()) for the entire duration, including the blocking executionManager.CreateHistoryTasks network/DB call. This mirrors the existing ReplicateFailoverMarkers pattern so it is consistent, but be aware that under a slow or hanging persistence backend this serializes and blocks all other shard operations that need the write lock for the full request latency. If this path can be invoked with large task batches or under DB latency, consider whether the contention is acceptable. Not a regression versus existing code, flagged for awareness. 2 rules not applicable. Show all rules by commenting Auto-apply is off โ Gitar will not commit updates to this branch. Comment with these commands to change:Code Review โ
Approved 3 resolved / 3 findings
ReinjectHistoryTasks to the shard context, allowing DLQ tasks to be moved back into execution storage. Resolves lock management concerns by deferring domain lookups and ensuring proper task category handling.โ
3 resolved
โ
Edge Case: ReinjectHistoryTasks forwards unhandled task categories without IDs
tasksByCategory, but only allocates task IDs for HistoryTaskCategoryTransfer and HistoryTaskCategoryTimer. Any task of another category (e.g. replication or a future immediate category) is still included in the CreateHistoryTasksRequest.TasksByCategory map and sent to the DB with an unallocated taskID (0). Today this is caught because the persistence layer (nosqlExecutionStore.CreateHistoryTasks) rejects unsupported categories with a BadRequestError, so the write fails loudly rather than inserting a zero-ID task. The shard method, however, depends entirely on that downstream guard and the code comment ("Only transfer + timer are produced by the standby DLQ write path") rather than enforcing the invariant itself. Consider validating the categories up front in ReinjectHistoryTasks (and/or skipping non transfer/timer tasks) so the contract is explicit at the shard layer and not silently dependent on the store implementation.โ
Performance: GetDomainByID called while holding shard lock in ReinjectHistoryTasks
ReinjectHistoryTasks the domain cache lookup s.GetDomainCache().GetDomainByID(key.domainID) (lines 1570-1577) now runs inside the shard lock (s.Lock() at 1561). Every other execution-table write path in this file โ CreateWorkflowExecution, UpdateWorkflowExecution, ConflictResolveWorkflowExecution โ deliberately performs the domain cache lookup before taking the shard lock, each carrying the explicit comment // do not try to get domain cache within shard lock. A domain cache miss can trigger a synchronous, potentially DB-backed resolution; performing it under the shard lock serializes all other shard operations behind it and risks lock contention (and, in the worst case, lock-ordering issues) that the established pattern was written to avoid. Consider resolving the distinct domain IDs into the domainEntries map before acquiring the lock (the set of domain IDs is already known from tasksByExecution), then only allocate IDs under the lock.โ
Quality: Lock held across CreateHistoryTasks DB call in ReinjectHistoryTasks
Rules โ
All requirements met
Repository Rules
gitar display:verbose.Options
Display: compact โ Showing less information.
Auto-apply
Compact
gitar auto-apply:on
gitar display:verbose
Was this helpful? React with ๐ / ๐ | Gitar
Enables history task reinjection by adding ๐ service/history/shard/context.go:1546-1560
ReinjectHistoryTasks groups all input tasks by category into ๐ service/history/shard/context.go:1561-1575
In ๐ service/history/shard/context.go:1552-1566
ReinjectHistoryTasks holds the shard write lock (s.Lock()/defer s.Unlock()) for the entire duration, including the blocking executionManager.CreateHistoryTasks network/DB call. This mirrors the existing ReplicateFailoverMarkers pattern so it is consistent, but be aware that under a slow or hanging persistence backend this serializes and blocks all other shard operations that need the write lock for the full request latency. If this path can be invoked with large task batches or under DB latency, consider whether the contention is acceptable. Not a regression versus existing code, flagged for awareness. 2 rules not applicable. Show all rules by commenting Auto-apply is off โ Gitar will not commit updates to this branch. Comment with these commands to change:Code Review โ
Approved 3 resolved / 3 findings
ReinjectHistoryTasks to the shard context, allowing DLQ tasks to be moved back into execution storage. Resolves lock management concerns by deferring domain lookups and ensuring proper task category handling.โ
3 resolved
โ
Edge Case: ReinjectHistoryTasks forwards unhandled task categories without IDs
tasksByCategory, but only allocates task IDs for HistoryTaskCategoryTransfer and HistoryTaskCategoryTimer. Any task of another category (e.g. replication or a future immediate category) is still included in the CreateHistoryTasksRequest.TasksByCategory map and sent to the DB with an unallocated taskID (0). Today this is caught because the persistence layer (nosqlExecutionStore.CreateHistoryTasks) rejects unsupported categories with a BadRequestError, so the write fails loudly rather than inserting a zero-ID task. The shard method, however, depends entirely on that downstream guard and the code comment ("Only transfer + timer are produced by the standby DLQ write path") rather than enforcing the invariant itself. Consider validating the categories up front in ReinjectHistoryTasks (and/or skipping non transfer/timer tasks) so the contract is explicit at the shard layer and not silently dependent on the store implementation.โ
Performance: GetDomainByID called while holding shard lock in ReinjectHistoryTasks
ReinjectHistoryTasks the domain cache lookup s.GetDomainCache().GetDomainByID(key.domainID) (lines 1570-1577) now runs inside the shard lock (s.Lock() at 1561). Every other execution-table write path in this file โ CreateWorkflowExecution, UpdateWorkflowExecution, ConflictResolveWorkflowExecution โ deliberately performs the domain cache lookup before taking the shard lock, each carrying the explicit comment // do not try to get domain cache within shard lock. A domain cache miss can trigger a synchronous, potentially DB-backed resolution; performing it under the shard lock serializes all other shard operations behind it and risks lock contention (and, in the worst case, lock-ordering issues) that the established pattern was written to avoid. Consider resolving the distinct domain IDs into the domainEntries map before acquiring the lock (the set of domain IDs is already known from tasksByExecution), then only allocate IDs under the lock.โ
Quality: Lock held across CreateHistoryTasks DB call in ReinjectHistoryTasks
Rules โ
All requirements met
Repository Rules
gitar display:verbose.Options
Display: compact โ Showing less information.
Auto-apply
Compact
gitar auto-apply:on
gitar display:verbose
Was this helpful? React with ๐ / ๐ | Gitar