cadence icon indicating copy to clipboard operation
cadence copied to clipboard

feat: Drain history task DLQ by re-injecting tasks

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

PR 2 โ€” #8286: feat: Drain history task DLQ by re-injecting tasks

๐Ÿ“š Stacked PR (2/3). Previous: #8273 ยท Next: #8287

What changed?

https://github.com/cadence-workflow/cadence/issues/6697

Rework the DLQ Processor to drain via re-injection instead of per-task executors: replace the executors map[int]TaskExecutor with a single TaskReinjector, and have processAckLevel page tasks from the current ack level to the shard's max read level and re-inject them (using ReinjectHistoryTasks from #8273), advancing the ack level on success. Adds a metrics.Client to the processor and wires the reinjector through history_engine.go.

Why?

The original design required a bespoke executor per task category to replay DLQ entries, duplicating live task-processing logic. Re-injecting tasks back into the executions table lets the existing shard processors handle them through their normal path, so the DLQ drain stays correct as task processing evolves and needs no parallel executor implementations.

How did you test it?

go test -race ./service/history/taskdlq/... \
  ./common/persistence -run DLQ

Potential risks

Modifies core DLQ drain logic and the processor's public constructor signature. Re-injected tasks re-enter live queues, so a bug could replay or duplicate tasks. No schema change; no IDL change.

Release notes

N/A โ€” depends on the rest of the stack to be user-visible.

Documentation Changes

N/A

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

Code Review ๐Ÿ‘ Approved with suggestions 3 resolved / 4 findings

Replaces task-specific executors with a centralized TaskReinjector to drain the DLQ via task re-injection, improving maintainability and reuse of live processing logic. Consider optimizing the pure-transfer reinjection to avoid unnecessary domain cache lookups.

๐Ÿ’ก Quality: Pure-transfer reinjection now always does a domain lookup

๐Ÿ“„ service/history/shard/context.go:1569-1578 ๐Ÿ“„ service/history/shard/context_test.go:538 ๐Ÿ“„ service/history/shard/context_test.go:574 ๐Ÿ“„ service/history/shard/context_test.go:588 ๐Ÿ“„ service/history/shard/context_test.go:598

Previously, reinjecting transfer-only tasks did not require a domain-cache lookup (transfer IDs are shard-global). The refactor groups by execution and unconditionally calls GetDomainByID for every distinct domain before allocating IDs, even when only transfer tasks are present (confirmed by the test changes that now add GetDomainByID expectations to the transfer-only and error cases). This adds a domain-cache dependency/round trip to the transfer path and means reinjection now fails if the domain entry cannot be resolved, even for tasks that don't need it. This is acceptable if all reinjected batches realistically include timer tasks, but worth confirming the added coupling is intended.

โœ… 3 resolved
โœ… Quality: Misplaced errors.As explanatory comment in ReinjectHistoryTasks

๐Ÿ“„ service/history/shard/context.go:1601-1606 In contextImpl.ReinjectHistoryTasks, the comment explaining why errors.As is used is placed directly above the if err == nil { branch rather than above the else if errors.As(...) branch it describes. This is misleading on a first read. Consider moving the comment to sit above the errors.As check, or just before the whole if/else chain.

โœ… Bug: DLQ ack level captured from mutated task key after reinjection

๐Ÿ“„ service/history/taskdlq/processor.go:268-277 ๐Ÿ“„ service/history/shard/context.go:1337-1351 ๐Ÿ“„ service/history/shard/context.go:1435-1441 In processAckLevel, the ack-level cursor is read after the tasks have been reinjected:

if err := p.reinjector.ReinjectHistoryTasks(ctx, resp.Tasks); err != nil { ... }
k := resp.Tasks[len(resp.Tasks)-1].GetTaskKey()
lastGoodKey = &k

ReinjectHistoryTasks (shard contextImpl.ReinjectHistoryTasks โ†’ allocateTransferIDsLocked / allocateTimerIDsLocked) mutates the task objects in place: it calls task.SetTaskID(freshID) and, for timers, task.SetVisibilityTimestamp(bumpedTS). Since GetTaskKey() for a transfer task returns NewImmediateTaskKey(d.TaskID) reading the same mutable TaskData.TaskID, lastGoodKey ends up being the freshly allocated shard-global ID, not the original DLQ row position.

Fresh transfer IDs come from generateTaskIDLocked() and are monotonically increasing shard-global values, so they are far larger than the original DLQ task IDs. Consequences:

  • UpdateHistoryDLQAckLevel advances the DLQ read cursor far past the real position, so on the next periodic run any genuinely unprocessed DLQ rows (original IDs below the bumped cursor) are silently skipped.
  • DeleteHistoryDLQTasks deletes with ExclusiveMaxTaskKey = newKey.Next() (the huge value), deleting all DLQ rows for the partition โ€” including rows that were never reinjected (e.g. rows that arrived during processing or fell outside the processed pages). This is silent DLQ data loss.

The existing unit tests do not catch this because the MockTaskReinjector does not mutate the tasks, so GetTaskKey() still returns the original ID in tests but not in production.

Fix: capture the original task key before calling ReinjectHistoryTasks.

โœ… Bug: Reinjection now allocates IDs for replication tasks

๐Ÿ“„ service/history/shard/context.go:1546-1560 The previous implementation deliberately handled only the transfer and timer categories (comment: "Only transfer + timer are produced by the standby DLQ write path."). The refactor replaces that explicit handling with the generic allocateTaskIDsLocked, which iterates over every category present in the per-execution map. For the replication category it allocates IDs (deferred to last) and the tasks remain in executionTasks, which is then merged into tasksByCategory and written via CreateHistoryTasks. So if ReinjectHistoryTasks is ever passed a replication task, it will now be inserted into the executions table โ€” the exact behavior the stack's earlier "fix: Prevent inserting replication tasks" commit was trying to avoid. If the upstream processor reliably filters replication tasks before calling this, this is defensive-only; otherwise it can re-inject replication tasks. Consider explicitly skipping the replication category here (or asserting it is absent) so reinjection cannot write replication tasks even if a caller passes one.

๐Ÿค– Prompt for agents
Code Review: Replaces task-specific executors with a centralized TaskReinjector to drain the DLQ via task re-injection, improving maintainability and reuse of live processing logic. Consider optimizing the pure-transfer reinjection to avoid unnecessary domain cache lookups.

1. ๐Ÿ’ก Quality: Pure-transfer reinjection now always does a domain lookup
   Files: service/history/shard/context.go:1569-1578, service/history/shard/context_test.go:538, service/history/shard/context_test.go:574, service/history/shard/context_test.go:588, service/history/shard/context_test.go:598

   Previously, reinjecting transfer-only tasks did not require a domain-cache lookup (transfer IDs are shard-global). The refactor groups by execution and unconditionally calls `GetDomainByID` for every distinct domain before allocating IDs, even when only transfer tasks are present (confirmed by the test changes that now add `GetDomainByID` expectations to the transfer-only and error cases). This adds a domain-cache dependency/round trip to the transfer path and means reinjection now fails if the domain entry cannot be resolved, even for tasks that don't need it. This is acceptable if all reinjected batches realistically include timer tasks, but worth confirming the added coupling is intended.

Rules โœ… All requirements met

Repository Rules

โœ… GitHub Issue Linking Requirement: The PR description contains a valid link to a cadence-workflow issue (https://github.com/cadence-workflow/cadence/issues/6697), satisfying the requirement.
โœ… PR Description Quality Standards: The PR description covers all required sections, provides clear technical rationale, links the relevant issue, includes specific test commands, and addresses potential risks and release notes appropriately.

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 the behavior for this request:

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

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

gitar-bot avatar Jul 01 '26 19:07 gitar-bot

Code Review ๐Ÿ‘ Approved with suggestions 3 resolved / 4 findings

Replaces task-specific executors with a centralized TaskReinjector to drain the DLQ via task re-injection, improving maintainability and reuse of live processing logic. Consider optimizing the pure-transfer reinjection to avoid unnecessary domain cache lookups.

๐Ÿ’ก Quality: Pure-transfer reinjection now always does a domain lookup

๐Ÿ“„ service/history/shard/context.go:1569-1578 ๐Ÿ“„ service/history/shard/context_test.go:538 ๐Ÿ“„ service/history/shard/context_test.go:574 ๐Ÿ“„ service/history/shard/context_test.go:588 ๐Ÿ“„ service/history/shard/context_test.go:598

Previously, reinjecting transfer-only tasks did not require a domain-cache lookup (transfer IDs are shard-global). The refactor groups by execution and unconditionally calls GetDomainByID for every distinct domain before allocating IDs, even when only transfer tasks are present (confirmed by the test changes that now add GetDomainByID expectations to the transfer-only and error cases). This adds a domain-cache dependency/round trip to the transfer path and means reinjection now fails if the domain entry cannot be resolved, even for tasks that don't need it. This is acceptable if all reinjected batches realistically include timer tasks, but worth confirming the added coupling is intended.

โœ… 3 resolved
โœ… Quality: Misplaced errors.As explanatory comment in ReinjectHistoryTasks

๐Ÿ“„ service/history/shard/context.go:1601-1606 In contextImpl.ReinjectHistoryTasks, the comment explaining why errors.As is used is placed directly above the if err == nil { branch rather than above the else if errors.As(...) branch it describes. This is misleading on a first read. Consider moving the comment to sit above the errors.As check, or just before the whole if/else chain.

โœ… Bug: DLQ ack level captured from mutated task key after reinjection

๐Ÿ“„ service/history/taskdlq/processor.go:268-277 ๐Ÿ“„ service/history/shard/context.go:1337-1351 ๐Ÿ“„ service/history/shard/context.go:1435-1441 In processAckLevel, the ack-level cursor is read after the tasks have been reinjected:

if err := p.reinjector.ReinjectHistoryTasks(ctx, resp.Tasks); err != nil { ... }
k := resp.Tasks[len(resp.Tasks)-1].GetTaskKey()
lastGoodKey = &k

ReinjectHistoryTasks (shard contextImpl.ReinjectHistoryTasks โ†’ allocateTransferIDsLocked / allocateTimerIDsLocked) mutates the task objects in place: it calls task.SetTaskID(freshID) and, for timers, task.SetVisibilityTimestamp(bumpedTS). Since GetTaskKey() for a transfer task returns NewImmediateTaskKey(d.TaskID) reading the same mutable TaskData.TaskID, lastGoodKey ends up being the freshly allocated shard-global ID, not the original DLQ row position.

Fresh transfer IDs come from generateTaskIDLocked() and are monotonically increasing shard-global values, so they are far larger than the original DLQ task IDs. Consequences:

  • UpdateHistoryDLQAckLevel advances the DLQ read cursor far past the real position, so on the next periodic run any genuinely unprocessed DLQ rows (original IDs below the bumped cursor) are silently skipped.
  • DeleteHistoryDLQTasks deletes with ExclusiveMaxTaskKey = newKey.Next() (the huge value), deleting all DLQ rows for the partition โ€” including rows that were never reinjected (e.g. rows that arrived during processing or fell outside the processed pages). This is silent DLQ data loss.

The existing unit tests do not catch this because the MockTaskReinjector does not mutate the tasks, so GetTaskKey() still returns the original ID in tests but not in production.

Fix: capture the original task key before calling ReinjectHistoryTasks.

โœ… Bug: Reinjection now allocates IDs for replication tasks

๐Ÿ“„ service/history/shard/context.go:1546-1560 The previous implementation deliberately handled only the transfer and timer categories (comment: "Only transfer + timer are produced by the standby DLQ write path."). The refactor replaces that explicit handling with the generic allocateTaskIDsLocked, which iterates over every category present in the per-execution map. For the replication category it allocates IDs (deferred to last) and the tasks remain in executionTasks, which is then merged into tasksByCategory and written via CreateHistoryTasks. So if ReinjectHistoryTasks is ever passed a replication task, it will now be inserted into the executions table โ€” the exact behavior the stack's earlier "fix: Prevent inserting replication tasks" commit was trying to avoid. If the upstream processor reliably filters replication tasks before calling this, this is defensive-only; otherwise it can re-inject replication tasks. Consider explicitly skipping the replication category here (or asserting it is absent) so reinjection cannot write replication tasks even if a caller passes one.

๐Ÿค– Prompt for agents
Code Review: Replaces task-specific executors with a centralized TaskReinjector to drain the DLQ via task re-injection, improving maintainability and reuse of live processing logic. Consider optimizing the pure-transfer reinjection to avoid unnecessary domain cache lookups.

1. ๐Ÿ’ก Quality: Pure-transfer reinjection now always does a domain lookup
   Files: service/history/shard/context.go:1569-1578, service/history/shard/context_test.go:538, service/history/shard/context_test.go:574, service/history/shard/context_test.go:588, service/history/shard/context_test.go:598

   Previously, reinjecting transfer-only tasks did not require a domain-cache lookup (transfer IDs are shard-global). The refactor groups by execution and unconditionally calls `GetDomainByID` for every distinct domain before allocating IDs, even when only transfer tasks are present (confirmed by the test changes that now add `GetDomainByID` expectations to the transfer-only and error cases). This adds a domain-cache dependency/round trip to the transfer path and means reinjection now fails if the domain entry cannot be resolved, even for tasks that don't need it. This is acceptable if all reinjected batches realistically include timer tasks, but worth confirming the added coupling is intended.

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), which satisfies the GitHub Issue Linking Requirement.
โœ… PR Description Quality Standards: The PR description follows the required structure and guidance, providing a clear 'why' for the architectural change, concrete test commands, and addressing the required sections adequately.

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 the behavior for this request:

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

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

gitar-bot avatar Jul 06 '26 20:07 gitar-bot