cadence icon indicating copy to clipboard operation
cadence copied to clipboard

feat: Allow shards to create history tasks

Open c-warren opened this issue 5 months ago โ€ข 1 comments

๐Ÿ“š 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

c-warren avatar Jun 23 '26 20:06 c-warren

Code Review โœ… Approved 3 resolved / 3 findings

Enables history task reinjection by adding 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

๐Ÿ“„ service/history/shard/context.go:1546-1560 ReinjectHistoryTasks groups all input tasks by category into 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

๐Ÿ“„ service/history/shard/context.go:1561-1575 In 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

๐Ÿ“„ 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.

Rules โœ… All requirements met

Repository Rules

โœ… GitHub Issue Linking Requirement: The PR description includes a valid issue link (https://github.com/cadence-workflow/cadence/issues/6697).
โœ… PR Description Quality Standards: The PR description follows the required structure and provides detailed information on what changed, why, and how it was tested.

2 rules not applicable. Show all rules by commenting gitar display:verbose.

Options

Auto-apply is off โ†’ Gitar will not commit updates to this branch.
Display: compact โ†’ Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with ๐Ÿ‘ / ๐Ÿ‘Ž | Gitar

gitar-bot avatar Jun 30 '26 16:06 gitar-bot

Code Review โœ… Approved 3 resolved / 3 findings

Enables history task reinjection by adding 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

๐Ÿ“„ service/history/shard/context.go:1546-1560 ReinjectHistoryTasks groups all input tasks by category into 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

๐Ÿ“„ service/history/shard/context.go:1561-1575 In 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

๐Ÿ“„ 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.

Rules โœ… All requirements met

Repository Rules

โœ… GitHub Issue Linking Requirement: The PR contains a link to a cadence-workflow organization issue, satisfying the requirement.
โœ… PR Description Quality Standards: The PR description strictly follows the template structure and provides substantive, high-quality content for all required sections, clearly articulating the 'why' and 'how'.

2 rules not applicable. Show all rules by commenting gitar display:verbose.

Options

Auto-apply is off โ†’ Gitar will not commit updates to this branch.
Display: compact โ†’ Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with ๐Ÿ‘ / ๐Ÿ‘Ž | Gitar

gitar-bot avatar Jun 30 '26 16:06 gitar-bot