Skip to content

[Bug]: TN StartReplica can be consumed before acknowledgment in legacy delivery (4.2-dev and main) #30443

Closed

Description

@XuPeng-SH

Problem

The real TN replica-start command generation path is not covered by HAKeeper's existing command redelivery protection. A heartbeat can consume a TN StartReplica command before the TN has acknowledged starting the replica. If the committed heartbeat result is lost, the next heartbeat does not receive that command again.

This is a production control-plane recovery defect, independently reproduced on 4.2-dev e2c99334fd23b9260728695e29befd35ab26b454 and PR #30433 head 0d1637598769ac85c029ede64665906c253925b7. The causal owner files are unchanged between those revisions. Latest main c21a102e74590253dad70d86e0f18646b61457ae also reproduces it in the legacy delivery path, while the new acknowledged-delivery preparing/enabled paths pass the same real-producer lost-response test. See the main verification below.

Root cause

  1. operator.addTnReplica builds StartReplica with ServiceType=TNService and leaves Bootstrapping=false.
  2. bootstrapReplicaCommandStatus only enables acknowledgment-based retention when Bootstrapping=true.
  3. getCommandBatch consequently removes the real TN command on its first committed heartbeat delivery, even when the heartbeat reports no started replica.
  4. If the result never reaches the caller, the target has neither started the replica nor retained a command to receive on the next heartbeat.

The existing TestBootstrapReplicaCommandsRetriedUntilHeartbeatAcknowledges manually constructs a TN command with Bootstrapping=true, so it passes without exercising the real producer contract. This is an accompanying test-coverage gap, rather than evidence that the defect is test-only.

Deterministic white-box reproduction

In pkg/hakeeper/rsm_test.go, add the pkg/hakeeper/operator import and run this diagnostic test. The file already imports testing, sm, pb and require.

func TestTNStartCommandSurvivesLostHeartbeatResponse(t *testing.T) {
    const storeID = "tn-review"
    const shardID uint64 = 2

    op := operator.NewOperator("tnservice", shardID, operator.NoopEpoch,
        operator.AddTnReplica{
            StoreID: storeID, ShardID: shardID,
            ReplicaID: 262146, LogShardID: 1,
        })
    commands := operator.NewController().Dispatch(
        []*operator.Operator{op}, pb.LogState{}, pb.TNState{},
        pb.CNState{}, pb.ProxyState{},
    )
    require.Len(t, commands, 1)
    require.False(t, commands[0].Bootstrapping)

    rsm := NewStateMachine(0, 1).(*stateMachine)
    rsm.state.State = pb.HAKeeperRunning
    _, err := rsm.Update(sm.Entry{Cmd: GetUpdateCommandsCmd(1, commands)})
    require.NoError(t, err)

    hb, err := (&pb.TNStoreHeartbeat{UUID: storeID}).Marshal()
    require.NoError(t, err)
    first, err := rsm.Update(sm.Entry{Cmd: GetTNStoreHeartbeatCmd(hb)})
    require.NoError(t, err)
    var delivered pb.CommandBatch
    require.NoError(t, delivered.Unmarshal(first.Data))
    require.Len(t, delivered.Commands, 1)

    // Discard the committed result: the TN did not observe the command.
    second, err := rsm.Update(sm.Entry{Cmd: GetTNStoreHeartbeatCmd(hb)})
    require.NoError(t, err)
    var retried pb.CommandBatch
    require.NoError(t, retried.Unmarshal(second.Data))
    require.Len(t, retried.Commands, 1,
        "unreported TN replica must remain deliverable after a lost response")
}

The last assertion fails: the second response contains zero commands.

Local verification used Go 1.26.4, Linux/amd64, matching native artifacts and -race -count=1. The diagnostic was supplied through a Go overlay; production sources were unchanged.

Revision/control Result
PR #30433 0d16375987, real producer and discarded first result Reproduced: expected one command, got zero; package 0.119 s
Clean tracked 4.2-dev base e2c99334fd, same diagnostic Same failure; package 0.128 s
Existing test with manually marked Bootstrapping=true Both log/TN cases pass; package 1.137 s

Impact and incident attribution

A transient response loss can leave the original TN startup attempt without its startup command. This can delay replica availability and exceed startup/readiness deadlines. The reproduction does not establish that all later operator rescheduling or recovery paths fail forever.

The coverage failure on #30433 is consistent with this mechanism:

  • 2026-10-16 22:47:26 UTC: HAKeeper elected its leader.
  • 22:47:31: log shard elected its leader; HAKeeper entered Running.
  • 22:47:37: HAKeeper enqueued D/Start for TN 10005-tn, shard 2, replica 262146.
  • Until 22:48:03: the CN kept observing no ready TN shard, then the 30-second readiness deadline expired.

The specific CI heartbeat-response loss is not proven. The retained bundle has no per-heartbeat proposal/result trace or failure-time goroutine dump. This issue tracks the independently reproduced delivery defect. The separate debugUpdateCNLabel timeout in the race UT job remains a distinct investigation.

Fix and verification requirements

  • Align the real TN command producer and HAKeeper's acknowledgment/redelivery owner. Review the meaning of Bootstrapping before changing it globally; preserve normal operator and HAKeeper state transitions.
  • Exercise commands generated by the real controller, rather than relying only on hand-constructed commands with a different flag.
  • Verify a committed-but-unobserved heartbeat result, repeated heartbeats with no replica report, and retirement only after the matching shard/replica report.
  • Preserve idempotent start handling, log-service behavior, and cancellation/restart behavior. Increasing readiness timeouts alone does not repair command consumption before acknowledgment.

Latest main verification

Verified on main c21a102e74590253dad70d86e0f18646b61457ae, Go 1.27.1/Linux amd64 with -race. The diagnostic uses the real operator.Controller TN command, and the heartbeat advertises CommandDeliveryAckSupported=true, as the current TN does. It discards the first committed response, then retries without acknowledging or reporting the replica.

Replicated command-delivery state Result
Legacy: neither Preparing nor Enabled Reproduces: second heartbeat returns zero commands
Preparing Retains and redelivers the same command; matching ACK retires it
Enabled Retains and redelivers the same command; matching ACK retires it

All three scenarios were executed in one race run (package 0.110 s; expected failing legacy assertion, two passing controls). They are controlled RSM scenarios, not a full mixed-version deployment experiment.

Main's handleTNHeartbeat chooses getCommandBatchWithAck during Preparing/Enabled for capable TNs, avoiding this loss. Before Preparing, it still chooses the destructive legacy path. store.tryEnableCommandDelivery deliberately waits for capability/admission barriers across HAKeeper members and current service targets, so incomplete upgrades can keep that legacy path reachable. An activated modern cluster must not be classified as affected on the strength of the legacy reproduction alone.

The confirmed scope is therefore 4.2-dev plus main's pre-activation legacy compatibility path, with protection already present in main's new acknowledged-delivery paths. The specific #30433 coverage incident attribution remains unproven as stated above.

Activity

  1. self-assigned this
    on Oct 16, 2026
  2. added
    kind/bug
    on Oct 16, 2026
  3. changed the title [-][Bug]: TN StartReplica commands bypass heartbeat redelivery protection on 4.2-dev[/-] [+][Bug]: TN StartReplica can be consumed before acknowledgment in legacy delivery (4.2-dev and main)[/+] on Oct 16, 2026
  4. XuPeng-SH commented on Oct 17, 2026

    @XuPeng-SH
    ContributorAuthor

    4.2-dev fix: #30444, commit 505f6ddaa958927034c78b0a9d0537d255ebba36.

    The fix closes both loss boundaries in the existing RSM owner: real TN StartReplica commands survive committed-but-unobserved heartbeats and unrelated/empty subsequent scheduling batches. Supersession, local stop/remove and shutdown revoke stale starts; no ACK protocol or schema backport is required.

    Clarification to the original exact-replica retirement wording: an exact target report confirms the requested replica. A fresh report of the same local shard under another replica ID cancels obsolete intent, because the current TN receiver keys replicas by shard and cannot replace an existing shard by replica ID; its checker accepts one working replica. Another target/shard report does not retire the start.

    Local full HAKeeper/operator/TN-checker UT and race checks passed, as did focused TN/logservice consumer race tests, 100 repetitions of duplicate TN startup under race, incremental configured SCA (0 issues), vet and molint. gpt-6.1-sol/xhigh produced the design and an independent final PASS review.

    This issue stays open for main's pre-activation legacy path. This fix does not establish the cause of the specific #30433 startup timeout or resolve its independent CN-label Raft timeout. CI has not been awaited or rerun.

  5. XuPeng-SH commented on Oct 17, 2026

    @XuPeng-SH
    ContributorAuthor

    Main fix submitted as #30445 (551cf104d8), alongside 4.2-dev #30444.

    Main's pre-activation legacy path now retains real TN starts across committed-but-unobserved responses and unrelated accepted scheduling updates, with bounded supersession/removal/shutdown cleanup. Its existing preparing/enabled ACK completion and stable delivery identities remain separate and unchanged.

    Verification includes full HAKeeper/operator/TN-checker normal/race tests, TN normal tests and eleven focused consumer race controls, 100 repeated duplicate-start race runs, real activation/snapshot/rollover/stale-ACK handoff, and an existing real service fixture extended to prove zero-ID legacy polls cannot consume work before activation. Incremental configured SCA, vet and molint passed; independent gpt-6.1-sol/xhigh final review PASS.

    Both PRs remain pending merge/CI; this comment does not claim the issue is already resolved on either branch. Mixed old/new HAKeeper semantic co-application is not validated; version-coherent rollout and downgrade limitations are explicit in the main PR. The earlier specific CI incident attribution remains unproven.

  6. XuPeng-SH commented on Oct 17, 2026

    @XuPeng-SH
    ContributorAuthor

    继续实测验证(2026-10-17):

    4.2-dev ac27db31696c0394ab1b2787b7b0a8f4933f088f(Go 1.26.4):checkers/coordinator、TN checker、Log checker、operator 完整普通与 race 测试均通过;真实 Coordinator → RSM 丢响应/批覆盖/重试与报告收敛测试实际执行通过。历史 RSM 重放和 bootstrap 重投对照也通过。LogService 重复 start/bootstrap/add/nonvoting-add/remove 及 TN 重复/不同 ID start 的 receiver 回归,普通与 race 均通过。均为 Linux/amd64、匹配分支 CGo 产物。

    main a322466757b9b0165c59fbda565608653d923b2a(Go 1.27.1):用真实 operator producer 生成 TN StartReplica 后,丢弃首个已提交的心跳响应;-race 诊断再次确认 legacy 模式首次返回 1 条、第二次返回 0 条,仍失败。Preparing 和 Enabled 两个对照均连续返回 1 条,通过。该诊断不把 Coordinator 周期重试与 RSM 内保留混为一谈。

    结论:#30444 的 4.2 修复已验证;main 对应 #30445 仍未合并,整个 #30443 的报告范围尚未全部解决。保留 @XuPeng-SH 开发指派,不整体转测。仍使用 #30443 跟踪,没有为同一缺口新建重复 issue。

  7. XuPeng-SH commented on Oct 17, 2026

    @XuPeng-SH
    ContributorAuthor

    继续独立实测验证(本轮截止 2026-10-17T17:51:03Z),#30445 合并后,#30443 的已确认功能范围在 main 与 4.2-dev 均通过,转 @Ariznawlll 测试。

    环境:Linux/amd64 CPU/CGo;main d304047724bea6104c5c2be496107928034b0778、Go 1.27.1;4.2-dev ac27db31696c0394ab1b2787b7b0a8f4933f088f、Go 1.26.4。隔离 worktree/Go overlay,仓库原工作树与既有服务未修改。通过仓库 mo-cgo-test 实际编译、链接并执行测试;未变化的 native 输入/产物由 wrapper 校验后复用。

    独立复现 TestAudit1751LostTNResponseRecovery:

    • 使用真实 Coordinator → operator producer → RSM → TN heartbeat;丢弃首个已提交响应,随后连续 32 个调度周期丢弃重发响应,再用不相关 CN 批覆盖,下一健康检查仍恢复原 TN 启动命令。
    • 每次命令/ReplicaID 相同,controller 始终只有一个 pending operator,没有额外消耗 replica ID;实际目标 shard/replica 报告后停止重试、移除 operator。
    • main 和 4.2 均实际运行普通/race,通过;相同诊断在干净修复前 main b111224b0c624773389b390cd3cf1223126b6ee2 失败(首个重试返回 nil),为有效负对照。

    相关回归:

    • main 的 checkers、TN checker、Log checker、operator 四个完整归属包普通/race 各 114 个测试/子测试通过;4.2 相同四包各 97 个通过,零失败/跳过。
    • RSM TN/命令交付/历史重放控制:main 普通/race 各 18 个通过;4.2 各 9 个通过。main Preparing/Enabled 的 ACK identity、stale ACK、coalescing 与 legacy snapshot convergence 均通过。
    • main 13 项精确 TN/Log/operator 回归分别在单进程 -race -count=100 下通过,共 5,200 个测试/子测试通过事件。覆盖目标失效、不同 ID 报告、取消/teardown、重建、Log 候选失效/存储 incarnation、混合批次和 pointer ownership。
    • main 实际 TN/LogService 接收端普通/race 各 29 个通过事件,覆盖重复 start、bootstrap/add/nonvoting-add/remove、命令轮询、激活 capability/admission、停止创建/关闭等控制。
    • main 真实嵌入式服务的 MySQL REPLACE SELECT 锁等待/恢复普通/race 通过(test: add regression guard for pipeline terminal loss under lock wait backpressure #30369):完整写入 16,384 行,锁释放,后续同键 writer 不阻塞。它是公开 SQL/服务启动消费者控制,不是网络丢包注入。

    命令(每次均非 cached、非空选择):mo-cgo-test -p 1/2 -count=1 -short -tags matrixone_test -timeout=8m/12m -json [-race] [-overlay ...] ./pkg/hakeeper/checkers ./pkg/hakeeper/checkers/tnservice ./pkg/hakeeper/checkers/logservice ./pkg/hakeeper/operator;RSM、tnservice/logservice 和 SQL 的精确选择、原始 JSONL/stderr/退出码及 SHA 已保存在本轮审计记录。压力命令每次只指定一个精确测试名。

    机制澄清:修复是 Coordinator 对未完成 TN operator 的周期重发;legacy RSM 仍保留历史消费语义,并非承诺“第二次原始心跳在没有调度检查时也一定保留命令”。这符合当前 #30444/#30445 的功能闭环。需要健康检查继续运行、目标可用并最终交付;不是 exactly-once。未验证现场 CI 的具体丢响应原因、真实网络故障注入、混合旧新二进制滚动升级、GPU 或全仓库 CI;不把这些限制称为通过。

  8. Ariznawlll commented on Oct 17, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main,commit:429c27761be90c3c865685700cc208e194e61be6(修复 PR #30445 的 merge commit d304047724bea6104c5c2be496107928034b0778 已进入该 main)。
    • 分支:官方 4.2-dev,commit:ac27db31696c0394ab1b2787b7b0a8f4933f088f(修复 PR #30444 已合入)。
    • 部署方式:两套独立 Git worktree,macOS/arm64、本地原生 CGo 依赖重新构建;main 使用 Go 1.27.1,4.2-dev 使用 Go 1.26.5。测试拓扑为真实 Coordinator/operator → HAKeeper RSM → TN 心跳的进程内确定性故障注入,并补 TN/LogService 接收端测试;非生产集群。
    • 测试日期:2026-10-18(北京时间)。

    原问题验证

    两条分支均执行真实 Coordinator 生成的 TN StartReplica(Bootstrapping=false),提交首个心跳但丢弃响应,再在无副本报告情况下继续调度/心跳。TestTNStartRescheduledAfterLegacyCommandLoss 的 lost_response 和 overwritten_batch 两个子场景在 -race -count=3 均通过;该原问题路径另以 -race -count=20 通过(每轮两个子场景)。实际返回为首次生成 1 条启动命令,旧版 RSM 消费后心跳暂为 0 条,但 Coordinator 后续调度每次重新生成与原命令相同的 1 条命令;没有新增 replica ID,目标 shard/replica 报告后停止重试并清除待处理 operator。该修复契约是周期性调度重发,不是旧版 RSM 无调度时对原始心跳的重投。

    Happy Path 覆盖

    1. main 的 TestTNStartRescheduledAfterLegacyCommandLoss、TestTNStartReconcilesReportsAndTargets、TestTNStartReservationsAndPartialReports、TestTNStartRebuildAndTeardown、TestTNStartRetriesPreserveAcknowledgedDelivery、TestTNRetryPreservesPendingLogStart 和 TestTNRetryDoesNotResurrectObsoleteRemoval,-race -count=3 全通过。覆盖真实目标报告后的完成、部分报告、重建、Preparing/Enabled ACK 与并存 Log 待处理命令。
    2. 4.2-dev 上相应六个 Coordinator/TN/Log 路径(不含 main 专属 ACK 测试)-race -count=3 全通过。
    3. main 的 RSM 快照重放、TN 启动/心跳就绪及 LogService 启动/添加/停止/关闭接收端定向测试均 -race -count=3 通过;4.2-dev 相同 RSM/TN/LogService 对照 -race -count=3 通过。

    Unhappy Path 覆盖

    1. 首个已提交响应丢失、后续不相关 CN 批覆盖:两条分支均可重新取得原 TN 启动命令,-race -count=3,额外原问题场景 20 轮通过。
    2. 不同 replica ID 的同 shard 报告、错误 shard 报告、目标失效/缺失、其他目标已有副本、取消与连续取消、重复 operator:TestTNStartReconcilesReportsAndTargets 在两条分支 -race -count=3 通过;应取消者没有陈旧命令复活,应保留者保持待处理状态。
    3. Coordinator 重建和 teardown、Log 待处理启动与过期删除并存:两条分支 -race -count=3 通过;重建产生新意图,清理后旧意图不复活。

    白盒验证

    本问题是 HAKeeper 内部命令调度/心跳响应所有权路径,公共 SQL 无法精确控制“已提交但调用方未观察到心跳响应”。使用生产 Coordinator、operator 和 RSM 调用的确定性注入及 -race 验证;没有将手工设定 Bootstrapping=true 的旧测试误作原问题覆盖。

    回归测试

    回归已随产品修复进入上述两个官方分支:main 真实命令/丢响应测试、main ACK 对照、4.2-dev 对应测试。这是内部控制面故障/并发测试,不硬塞到 MOTR 的 SQL golden。PR 的 UT、静态检查与 Required 汇总成功;部分 TKE 构建/BVT 检查失败或被跳过,本轮未将它们算作集成通过,也未重跑共享 CI。

    测试证据

    • 独立隔离 worktree 运行 go test -race -run '^TestTNStartRescheduledAfterLegacyCommandLoss$' -count=20 -timeout=8m ./pkg/hakeeper/checkers,main 与 4.2-dev 输出均为 ok,无 race 报告;其余上述定向组均用 -race -count=3 实际运行,退出码 0。
    • 上述三个固定 commit 的回归源文件、修复 #30444 / #30445 及其 GitHub Actions 记录可复查。
    • 未覆盖:真实网络故障注入、混合旧新 HAKeeper 二进制滚动升级,以及 [4.2-dev] Fix SQL server file access isolation (#30185) #30433 原 CI 现场的具体丢包归因;本结论只针对 issue 中独立确认的命令调度缺陷和两条版本线的修复,不把这些未验证项写为通过。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions