feat: Drain history task DLQ by re-injecting tasks
PR 2 โ #8286: feat: Drain history task DLQ by re-injecting tasks
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
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. ๐ 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 ๐ service/history/shard/context.go:1601-1606
In ๐ service/history/taskdlq/processor.go:268-277 ๐ service/history/shard/context.go:1337-1351 ๐ service/history/shard/context.go:1435-1441
In Fresh transfer IDs come from The existing unit tests do not catch this because the Fix: capture the original task key before calling ๐ 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 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 the behavior for this request:Code Review ๐ Approved with suggestions 3 resolved / 4 findings
๐ก Quality: Pure-transfer reinjection now always does a domain lookup
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
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
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.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.MockTaskReinjector does not mutate the tasks, so GetTaskKey() still returns the original ID in tests but not in production.ReinjectHistoryTasks.โ
Bug: Reinjection now allocates IDs for replication tasks
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
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
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. ๐ 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 ๐ service/history/shard/context.go:1601-1606
In ๐ service/history/taskdlq/processor.go:268-277 ๐ service/history/shard/context.go:1337-1351 ๐ service/history/shard/context.go:1435-1441
In Fresh transfer IDs come from The existing unit tests do not catch this because the Fix: capture the original task key before calling ๐ 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 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 the behavior for this request:Code Review ๐ Approved with suggestions 3 resolved / 4 findings
๐ก Quality: Pure-transfer reinjection now always does a domain lookup
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
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
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.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.MockTaskReinjector does not mutate the tasks, so GetTaskKey() still returns the original ID in tests but not in production.ReinjectHistoryTasks.โ
Bug: Reinjection now allocates IDs for replication tasks
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
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