You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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+1lastID=firstID+batchSize-1
lastID is therefore inclusive, but the cache-consumption check treats equality as exhausted:
ifnextID!=lastID {
returnnextID
}
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.
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 边界/唯一性断言。
临时最小修正对照
临时修改:
将本地缓存判断改为包含式边界:nextID != 0 && nextID <= lastID;
在 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
未再出现旧代码中“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 的验证结论。
Activity
Ariznawlll commentedon Jul 20, 2026
最新 main 白盒验证
基线
main862d9d30c2cd7a55995fd0bae0951af0809f5752perf(plan): prune right SINGLE probe scans with runtime filters (#25865)测试过程与用例
新增本地审计测试
pkg/logservice/hakeeper_id_batch_audit_test.go,用一个共享的假 HAKeeper 分配器和多个独立managedHAKeeperClient验证批次所有权。用例一:
batch=1的跨客户端唯一性locker、batch=1,HAKeeper 返回1;2;2;实际结果:两个 client 都返回
2,确定性违反全局唯一性。用例二:完整消费已分配批次
让 HAKeeper 为 keyed/shared 路径分配 batch=
3,连续调用三次,期望只发一次 RPC 并依次得到[1,2,3]。实际 keyed 路径返回
[1,2,4]:ID3被跳过,并提前发起第二次 HAKeeper RPC。shared 路径使用相同边界判断,根因一致。用例三:拒绝零批次
调用
AllocateIDByKeyWithBatch(ctx, "zero", 0),期望客户端直接返回错误且 HAKeeper RPC 调用次数为 0。实际结果:返回 nil error 并发送请求。HAKeeper 状态机执行
allocIDCmd.Batch - 1,零批次会发生uint64下溢。原始代码执行命令
原始代码结果
9/9 FAIL;3/3 FAIL,未报告额外数据竞争;临时最小修正对照
临时修改:
nextID != 0 && nextID <= lastID;AllocateIDByKeyWithBatch入口拒绝batchSize == 0。执行:
结果:
PASS,-count=100;PASS,-count=50。曾额外尝试运行 logservice 全包集成测试,但本机 Dragonboat/gossip 测试出现动态端口
address already in use,该环境结果不作为本 bug 的判断依据,也不宣称全包通过。清理
验证完成后已撤销临时产品代码修正;白盒测试只保存在本地审计目录,未提交、未推送。
LeftHandCold commentedon Jul 22, 2026
fixed
Ariznawlll commentedon Jul 22, 2026
已在包含修复的最新
main上完成复测和扩展压力验证,结论:#25887 已修复。测试版本
mainmaincommit:e15943929885c2c5c83daed90f0f8caeb9a1f0d0647cbe620cefca8249b98fc0e62e327610b6bdf3修复点核对
代码已经统一采用包含式批次边界:
shared 和 keyed 两条缓存路径均使用相同判断;
AllocateIDByKeyWithBatch(..., 0)也会在发起 HAKeeper RPC 前返回ErrInvalidInput。一、研发新增回归测试
执行并重复验证以下测试:
TestAllocateIDConsumesEntireBatch[1,2,3];TestAllocateBatchOneIDsRemainUniqueAcrossClients[1,2,3];TestAllocateIDByKeyRejectsZeroBatchBeforeRPCErrInvalidInput;batch-1无符号下溢。二、扩展批次边界测试
额外编写本地白盒审计测试,分别覆盖 shared 和 keyed 路径的 batch:
每个组合连续分配
batch*4+3个 ID,并逐个断言:1,2,3...N;ceil(N/batch),不能提前申请下一批。8 组 shared/keyed × batch 组合全部通过。
三、多客户端 batch-one 唯一性压力验证
构造 10 个独立
managedHAKeeperClient,对同一个 key=locker交错申请 100 次、batch=1:执行命令与结果
-race连续 50 轮全部通过;全包测试说明
补跑
go test ./pkg/logservice -count=1时,TestNewServiceClosesStoreOnReplicaStartFailure和TestTestClient因本机另一个mo-service已占用固定端口32001/32002而失败,错误为bind: address already in use。该失败发生在测试服务启动阶段,与本 issue 的 ID 批次边界、唯一性或零批次校验无关,因此不计入本 bug 的验证结论。最终结论
原始三个缺陷均已修复:
扩展批次和多客户端压力测试均稳定通过,建议关闭 #25887。
该问题位于内部 HAKeeper 客户端缓存/RPC 路径,没有稳定的 SQL 黑盒入口,不建议加入 motr;现有 logservice UT 和 race 回归是更直接、可靠的覆盖层。