History: migrate engine.Engine to common.DaemonV2 so shard engine shutdown is bounded by a context
Description
Background
common.Daemon (Start() / Stop()) is deprecated in favor of the context-aware common.DaemonV2 (Start(ctx) error / Stop(ctx) error). The per-shard history engine (engine.Engine) still embeds common.Daemon. Because of that, nothing above the engine hands it a lifecycle context, and engine shutdown has no deadline.
Goal
Migrate engine.Engine to common.DaemonV2, so that the shard controller creates the context and passes it down:
- The shard controller derives a context per shard when it starts and stops that shard's engine (
historyShardsItem.getOrCreateEngine/stopEngineinservice/history/shard/controller.go) - The history service's shutdown drain budget (
HistoryShutdownDrainDuration, already used byservice/history/service.go) is passed down to engine shutdown, soStop(ctx)is bounded by that budget rather than running unbounded - The engine passes this context to every sub-component that implements
common.DaemonV2 - Every engine sub-component still on
common.Daemon(queue processors, replication DLQ handler, replication metrics emitter, etc.) is migrated tocommon.DaemonV2so it receives the same context
With this change, the lifecycle context comes from the shard, not from a context created locally in the engine.
Blast radius
service/history/engine/interface.go—Engineembedscommon.DaemonV2instead ofcommon.Daemonservice/history/engine/interface_mock.go— regenerated mockservice/history/engine/engineimpl/history_engine.go—Start(ctx)/Stop(ctx), passing ctx to sub-componentsservice/history/shard/controller.go— derives the per-shard context and handles errors returned byStart/Stop- Every test that sets expectations on
Engine.Start()/Engine.Stop()via the mock, plus the engine's own tests - Each engine sub-component migrated off
common.Daemon, with its mock and tests
Suggested approach (not a mandate)
- Migrate the engine and controller first; sub-component migrations can land as separate follow-up PRs, each wiring in the ctx as it moves to
DaemonV2 - Keep
Start(ctx)non-blocking, as it is today Stop(ctx)returnsctx.Err()if sub-components don't finish before the deadline; the controller logs it and still marks the shard item as stopped, so an engine that is slow to stop cannot wedge the shard
Acceptance criteria
engine.Engineimplementscommon.DaemonV2; mock regenerated viamake go-generate- The shard controller passes a per-shard context to
Startand a deadline-bounded context toStop, derived from the shutdown drain duration - The history engine passes that context to all of its
DaemonV2sub-components - All engine sub-components still on
common.Daemonare migrated toDaemonV2and receive the context - Tests cover
Stop(ctx)returning on deadline expiry and the controller's handling ofStart/Stoperrors make prandmake testpass
Is this a breaking change?
- Yes
- No
If yes, explain why:
N/A — engine.Engine and its sub-components are internal server interfaces; no API, config, or persistence change.
Scope of the feature (server, specific client, all clients)
Server — history service only.
Source: cadence-workflow/cadence