Skip to content

[Bug]: HAKeeper batch-one ID cache returns duplicate globally allocated IDs #25887

Closed

Description

@Ariznawlll

Summary

managedHAKeeperClient uses an inconsistent boundary convention for locally cached ID batches. With batch=1, one client starts returning IDs that HAKeeper never allocated to it, and another client can receive the same ID from HAKeeper. This violates the global uniqueness contract of the allocation API.

Affected revision

  • Branch: latest main
  • Commit: 862d9d30c2cd7a55995fd0bae0951af0809f5752
  • Commit date: 2026-07-20T04:31:19Z

Duplicate check

Searched open and closed issues for HAKeeper allocation batches, duplicate/global IDs, AllocateIDByKeyWithBatch, lastID off-by-one, and zero-batch underflow. #25868 concerns caller timeouts resetting the shared client and is unrelated.

Root cause

After HAKeeper allocates [firstID, firstID+batchSize-1], the client stores:

nextID = firstID + 1
lastID = firstID + batchSize - 1

lastID is therefore inclusive, but the cache-consumption check treats equality as exhausted:

if nextID != lastID {
    return nextID
}

Consequences:

  • batch > 1: the final ID in every batch is skipped;
  • batch = 1: nextID starts above lastID and remains unequal, so the client generates an unbounded local sequence without contacting HAKeeper again;
  • another client can then receive one of those same IDs from HAKeeper;
  • batch = 0 is accepted by AllocateIDByKeyWithBatch, while HAKeeper computes batch-1, causing unsigned underflow in the state machine.

The same inclusive/exclusive mismatch exists in both shared and keyed allocation caches.

Production path

pkg/cnservice/server.go calls:

l.hakeeperClient.AllocateIDByKeyWithBatch(ctx, key, 1)

for the CN locker path. The public HAKeeper client API also promises globally unique IDs to callers.

Expected behavior

  • Every ID in an allocated batch is returned exactly once.
  • Independent clients never return the same globally allocated ID.
  • A zero-sized batch is rejected before any HAKeeper request is sent.

Detailed white-box reproduction commands and results are provided in a Chinese comment.

Activity

  1. added this to the MO-202607 milestone on Jul 20, 2026
  2. Ariznawlll commented on Jul 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 白盒验证

    基线

    • 分支:最新 main
    • Commit:862d9d30c2cd7a55995fd0bae0951af0809f5752
    • Commit 标题:perf(plan): prune right SINGLE probe scans with runtime filters (#25865)
    • 验证日期:2026-07-20
    • 本测试不涉及事务或 2PC。

    测试过程与用例

    新增本地审计测试 pkg/logservice/hakeeper_id_batch_audit_test.go,用一个共享的假 HAKeeper 分配器和多个独立 managedHAKeeperClient 验证批次所有权。

    用例一:batch=1 的跨客户端唯一性

    1. client A 请求 key=locker、batch=1,HAKeeper 返回 1;
    2. client A 再次请求同一 key,当前实现未访问 HAKeeper,直接在本地返回 2;
    3. client B 第一次请求同一 key,HAKeeper 的下一个合法 ID 也是 2;
    4. 断言 client A 与 client B 不得返回相同 ID。

    实际结果:两个 client 都返回 2,确定性违反全局唯一性。

    用例二:完整消费已分配批次

    让 HAKeeper 为 keyed/shared 路径分配 batch=3,连续调用三次,期望只发一次 RPC 并依次得到 [1,2,3]。

    实际 keyed 路径返回 [1,2,4]:ID 3 被跳过,并提前发起第二次 HAKeeper RPC。shared 路径使用相同边界判断,根因一致。

    用例三:拒绝零批次

    调用 AllocateIDByKeyWithBatch(ctx, "zero", 0),期望客户端直接返回错误且 HAKeeper RPC 调用次数为 0。

    实际结果:返回 nil error 并发送请求。HAKeeper 状态机执行 allocIDCmd.Batch - 1,零批次会发生 uint64 下溢。

    原始代码执行命令

    go test ./pkg/logservice \
      -run '^TestAuditHAKeeper(BatchOneIDsRemainGloballyUnique|ClientConsumesEntireAllocatedBatch|RejectsZeroBatch)$' \
      -count=3
    
    go test -race ./pkg/logservice \
      -run '^TestAuditHAKeeper(BatchOneIDsRemainGloballyUnique|ClientConsumesEntireAllocatedBatch|RejectsZeroBatch)$' \
      -count=1

    原始代码结果

    TestAuditHAKeeperBatchOneIDsRemainGloballyUnique:
        Should not be: 0x2
        independent clients must never return the same globally allocated ID
    
    TestAuditHAKeeperClientConsumesEntireAllocatedBatch:
        expected: []uint64{1, 2, 3}
        actual:   []uint64{1, 2, 4}
    
    TestAuditHAKeeperRejectsZeroBatch:
        An error is expected but got nil
    
    • 普通模式:3 个用例连续 3 轮全部失败,9/9 FAIL;
    • race 模式:3/3 FAIL,未报告额外数据竞争;
    • 失败均为确定性的 ID 边界/唯一性断言。

    临时最小修正对照

    临时修改:

    1. 将本地缓存判断改为包含式边界:nextID != 0 && nextID <= lastID;
    2. 在 AllocateIDByKeyWithBatch 入口拒绝 batchSize == 0。

    执行:

    go test ./pkg/logservice \
      -run '^TestAuditHAKeeper(BatchOneIDsRemainGloballyUnique|ClientConsumesEntireAllocatedBatch|RejectsZeroBatch)$' \
      -count=100
    
    go test -race ./pkg/logservice \
      -run '^TestAuditHAKeeper(BatchOneIDsRemainGloballyUnique|ClientConsumesEntireAllocatedBatch|RejectsZeroBatch)$' \
      -count=50

    结果:

    • 普通聚焦测试:PASS,-count=100;
    • race 聚焦测试:PASS,-count=50。

    曾额外尝试运行 logservice 全包集成测试,但本机 Dragonboat/gossip 测试出现动态端口 address already in use,该环境结果不作为本 bug 的判断依据,也不宣称全包通过。

    清理

    验证完成后已撤销临时产品代码修正;白盒测试只保存在本地审计目录,未提交、未推送。

  3. added
    kind/bug
    on Jul 20, 2026
  4. added theissue type on Jul 20, 2026
  5. added
    severity/s0
    and removed on Jul 20, 2026
  6. LeftHandCold commented on Jul 22, 2026

    @LeftHandCold
    Collaborator

    fixed

  7. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在包含修复的最新 main 上完成复测和扩展压力验证,结论:#25887 已修复。

    测试版本

    • MatrixOne 分支:main
    • 当前 main commit:e15943929885c2c5c83daed90f0f8caeb9a1f0d0
    • 修复 PR:PR #25911
    • 修复 commit:647cbe620cefca8249b98fc0e62e327610b6bdf3

    修复点核对

    代码已经统一采用包含式批次边界:

    nextID != 0 && nextID <= lastID

    shared 和 keyed 两条缓存路径均使用相同判断;AllocateIDByKeyWithBatch(..., 0) 也会在发起 HAKeeper RPC 前返回 ErrInvalidInput。

    一、研发新增回归测试

    执行并重复验证以下测试:

    • TestAllocateIDConsumesEntireBatch
      • shared/keyed 的 batch=3 均返回 [1,2,3];
      • 每个完整批次只请求一次 HAKeeper,不再跳过末尾 ID。
    • TestAllocateBatchOneIDsRemainUniqueAcrossClients
      • client A 连续两次和 client B 一次分配得到 [1,2,3];
      • batch=1 时每次耗尽批次后都会重新请求 HAKeeper,不再本地越界生成 ID。
    • TestAllocateIDByKeyRejectsZeroBatchBeforeRPC
      • batch=0 返回 ErrInvalidInput;
      • HAKeeper RPC 调用次数为 0,不再触发 batch-1 无符号下溢。

    二、扩展批次边界测试

    额外编写本地白盒审计测试,分别覆盖 shared 和 keyed 路径的 batch:

    1, 2, 3, 7
    

    每个组合连续分配 batch*4+3 个 ID,并逐个断言:

    1. 返回值必须严格为 1,2,3...N;
    2. 不允许跳过批次最后一个 ID;
    3. 不允许返回超出 HAKeeper 已分配范围的 ID;
    4. RPC 次数必须等于 ceil(N/batch),不能提前申请下一批。

    8 组 shared/keyed × batch 组合全部通过。

    三、多客户端 batch-one 唯一性压力验证

    构造 10 个独立 managedHAKeeperClient,对同一个 key=locker 交错申请 100 次、batch=1:

    • 共得到 100 个不同 ID;
    • ID 无重复、无跳号;
    • HAKeeper RPC 调用次数为 100;
    • 未再出现旧代码中“client A 本地返回 2、client B 又从 HAKeeper 获得 2”的全局重复。

    执行命令与结果

    go test ./pkg/logservice \
      -run '^(TestAllocateIDConsumesEntireBatch|TestAllocateBatchOneIDsRemainUniqueAcrossClients|TestAllocateIDByKeyRejectsZeroBatchBeforeRPC|TestIssue25887ExtendedBatchBoundaries|TestIssue25887BatchOneUniqueAcrossManyClients)$' \
      -count=100
    
    ok github.com/matrixorigin/matrixone/pkg/logservice
    
    go test -race ./pkg/logservice \
      -run '^(TestAllocateIDConsumesEntireBatch|TestAllocateBatchOneIDsRemainUniqueAcrossClients|TestAllocateIDByKeyRejectsZeroBatchBeforeRPC|TestIssue25887ExtendedBatchBoundaries|TestIssue25887BatchOneUniqueAcrossManyClients)$' \
      -count=50
    
    ok github.com/matrixorigin/matrixone/pkg/logservice
    
    • 普通模式连续 100 轮全部通过;
    • -race 连续 50 轮全部通过;
    • 未发现 data race。

    全包测试说明

    补跑 go test ./pkg/logservice -count=1 时,TestNewServiceClosesStoreOnReplicaStartFailure 和 TestTestClient 因本机另一个 mo-service 已占用固定端口 32001/32002 而失败,错误为 bind: address already in use。该失败发生在测试服务启动阶段,与本 issue 的 ID 批次边界、唯一性或零批次校验无关,因此不计入本 bug 的验证结论。

    最终结论

    原始三个缺陷均已修复:

    1. batch > 1 不再跳过最后一个 ID;
    2. batch = 1 不再越界生成未分配 ID,多客户端不再返回重复 ID;
    3. batch = 0 在客户端入口被拒绝,不再发送 RPC 或触发下溢。

    扩展批次和多客户端压力测试均稳定通过,建议关闭 #25887。

    该问题位于内部 HAKeeper 客户端缓存/RPC 路径,没有稳定的 SQL 黑盒入口,不建议加入 motr;现有 logservice UT 和 race 回归是更直接、可靠的覆盖层。

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions