#8577·cadence

History: migrate engine.Engine to common.DaemonV2 so shard engine shutdown is bounded by a context

Author: c-warrenCreated Sep 25, 2026Updated Sep 25, 2026
Labelstriage/needs-infokind/feature

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 / stopEngine in service/history/shard/controller.go)
  • The history service's shutdown drain budget (HistoryShutdownDrainDuration, already used by service/history/service.go) is passed down to engine shutdown, so Stop(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 to common.DaemonV2 so 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 — Engine embeds common.DaemonV2 instead of common.Daemon
  • service/history/engine/interface_mock.go — regenerated mock
  • service/history/engine/engineimpl/history_engine.go — Start(ctx) / Stop(ctx), passing ctx to sub-components
  • service/history/shard/controller.go — derives the per-shard context and handles errors returned by Start/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) returns ctx.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.Engine implements common.DaemonV2; mock regenerated via make go-generate
  • The shard controller passes a per-shard context to Start and a deadline-bounded context to Stop, derived from the shutdown drain duration
  • The history engine passes that context to all of its DaemonV2 sub-components
  • All engine sub-components still on common.Daemon are migrated to DaemonV2 and receive the context
  • Tests cover Stop(ctx) returning on deadline expiry and the controller's handling of Start/Stop errors
  • make pr and make test pass

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