Skip to content

[Bug]: Serial HAKeeper heartbeat RPC can starve schedule-command delivery #26566

Closed

Description

@XuPeng-SH
  • I have checked the existing issues.

Branch Name

main

Commit ID

ffad68fdb75f05d7356b4e08a8013e26349a8968

Other Environment Information

Actual Behavior

TN and CN obtain HAKeeper schedule commands from heartbeat responses, and their heartbeat loops execute requests serially. In the TN path:

heartbeatTask
  -> s.heartbeat(ctx)                         // synchronous
     -> SendTNHeartbeat(ctxWithTimeout, hb)   // may block until RPC deadline
     -> handleCommands(response.Commands)     // only reachable after the RPC returns

Consequently, while one heartbeat RPC is blocked, the service cannot send the next heartbeat and cannot receive or apply schedule commands queued after that request, such as D/Start. Command-delivery progress is therefore coupled to the configured per-RPC heartbeat deadline.

This coupling caused a concrete CI failure when WithTesting() unintentionally changed the heartbeat RPC deadline from the service default of 3 seconds to 30 seconds:

12:57:10 HAKeeper entered Running and detected the TN log shard
12:57:11 HAKeeper scheduled D/Start for 10001-tn
          no subsequent TN "received/applying D/Start" record
12:57:35 all CNs timed out in waitAnyShardReadyLocked

The failed process had successfully bound and started HAKeeper; there was no EADDRINUSE. Normal UT passed while the coverage job exposed the longer startup window.

The test-only regression is addressed in PR #26546 by separating HAKeeper store-liveness tolerance from the heartbeat RPC deadline. The production implementation still has the structural weakness: a large explicit heartbeat timeout, a transport that is slow to observe cancellation, or a tight startup/recovery progress budget can starve control commands for the duration of the in-flight request.

This is a liveness/configuration defect rather than evidence of data corruption. With the current default 3-second deadline, impact is normally bounded to a few seconds, but the architecture does not independently guarantee schedule-command progress.

Expected Behavior

Control-plane progress must have an explicit, locally enforceable upper bound:

  1. A slow or unavailable heartbeat RPC must not starve schedule-command delivery beyond the documented control-plane progress budget.
  2. HAKeeper store-liveness/eviction timeouts and per-heartbeat RPC deadlines must remain independent concepts.
  3. Configuration must reject or clearly warn about heartbeat deadlines that violate startup, recovery, or scheduling progress invariants.
  4. Shutdown and cancellation must terminate blocked heartbeat/control-command work without leaking goroutines or allowing stale responses to affect a restarted service generation.

Steps to Reproduce

Use a deterministic fake HAKeeper client; no time.Sleep-based scheduling is required:

  1. Start a TN heartbeat loop with an injected SendTNHeartbeat implementation.
  2. Use a channel/barrier to prove the first heartbeat request has entered the fake client, then block that request until its context deadline or an explicit release signal.
  3. After the first request is in flight, queue a D/Start schedule command in the fake HAKeeper response source.
  4. Observe that TN cannot issue another heartbeat and cannot invoke handleCommands for D/Start while the first request remains blocked.
  5. Repeat for CN schedule-command handling.
  6. Set a large explicit heartbeat timeout and verify command latency grows with that timeout.
  7. Cancel/close during the blocked phase and verify bounded termination, exactly-once cleanup, and that an old response cannot mutate a restarted generation.

Additional information

Relevant code paths

  • pkg/tnservice/store_heartbeat.go: heartbeatTask, heartbeat, and handleCommands
  • pkg/cnservice/server_heartbeat.go: serial heartbeat request and command handling
  • pkg/tnservice/cfg.go: default per-RPC heartbeat timeout (3 seconds)
  • pkg/cnservice/types.go: default per-RPC heartbeat timeout (3 seconds)
  • pkg/embed/options.go and pkg/embed/cluster.go: embedded-cluster timeout overrides

First-principles invariant

schedule-command progress
  must not depend on
completion latency of an unrelated, already in-flight heartbeat request
  beyond a documented and enforced local bound

Current wait-for graph:

D/Start application
  -> next successful heartbeat response
  -> serial heartbeat loop becomes available
  -> current SendTNHeartbeat/SendCNHeartbeat returns
  -> remote response, transport cancellation, or RPC deadline

Suggested solution space

  • Define a control-plane progress budget and validate the heartbeat RPC deadline against it.
  • Decouple schedule-command polling/delivery from a potentially slow heartbeat request, or otherwise guarantee bounded retry without creating unbounded concurrent heartbeat RPCs.
  • Preserve at-most-one application of each command and prevent late responses from an old request/service generation from applying stale commands.
  • Add metrics/logs for heartbeat in-flight duration, deadline expiry, command queue age, and time from HAKeeper scheduling to service application.

Acceptance criteria

  • A deterministic fault-injection test proves that one blocked heartbeat cannot delay a queued schedule command beyond the declared bound.
  • Tests cover TN and CN, success, timeout, cancellation, shutdown, and restart/generation boundaries.
  • No unbounded goroutine/request fan-out is introduced; in-flight work and retained responses remain bounded.
  • Store-liveness timeout and RPC deadline remain independently configurable and documented.
  • Existing default and explicit configurations either satisfy the progress invariant or fail validation with an actionable error.
  • Startup/recovery behavior is validated under -race without reducing existing test coverage or relying on sleeps/retries as the oracle.

Activity

  1. added
    kind/bug
    on Aug 1, 2026
  2. added
    severity/s0
    and removed on Aug 6, 2026
  3. added a commit that references this issue on Aug 7, 2026
    ea78047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions