Skip to content

[Bug]: BranchHashmap ItemCount remains stale after PopByVectorsStream callback error #26081

Closed

Description

@Ariznawlll

Description

BranchHashmap.PopByVectorsStream removes matching in-memory entries before invoking its callback. If the callback returns an error, the method returns immediately without applying removedTotal to shard.items.

The removed row is no longer returned by GetByVectors, but ItemCount() still includes it. This leaves the hashmap's public count inconsistent with its actual contents after an error in a real Data Branch callback such as row decoding or batch construction.

Tested on main commit 2eb7f523e5a10f352d3e258183ed8440510388de.

Reproduction

  1. Create a one-shard BranchHashmap.
  2. Insert one row through PutByVectors and verify ItemCount() == 1.
  3. Call PopByVectorsStream for that key with a callback that returns an error.
  4. Query the key with GetByVectors and read ItemCount().

This was reproduced in 3 consecutive runs with a focused unit test.

Actual result

  • PopByVectorsStream returns the callback error.
  • GetByVectors reports that the row no longer exists.
  • ItemCount() incorrectly remains 1.
  • The returned removed count is 0, even though the row has already been removed.

Expected result

The bookkeeping and returned removal count should remain consistent with the entries already removed before the callback error is returned, or the removal should be rolled back.

Code path

pkg/frontend/databranchutils/branch_hashmap.go, PopByVectorsStream: the callback error branch returns before atomic.AddInt64(&shard.items, -removedTotal) and before adding removedTotal to totalRemoved.

Activity

  1. added this to the MO-202607 milestone on Jul 23, 2026
  2. added
    kind/bug
    on Jul 23, 2026
  3. added theissue type on Jul 23, 2026
  4. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充同键多行场景:插入 key=1 的两行记录后,调用 PopByVectorsStream(..., removeAll=true, callback),并让 callback 在收到第一行时返回错误。

    连续 3 轮结果一致:

    • callback 只收到 1 行;
    • PopByVectorsStream 返回的 removed 数为 0;
    • 随后 GetByVectors(key=1) 已无剩余行,说明两行都已在回调前移除。

    因此该路径除计数未汇总外,还会出现“已移除但未交付给 callback”的记录。建议在回调错误时同步已完成的计数,并明确采用逐行移除/交付或可恢复的处理方式,避免多行结果半途丢失。

  5. added a commit that references this issue on Jul 28, 2026
    6e79915
  6. VioletQwQ-0 commented on Jul 29, 2026

    @VioletQwQ-0
    Collaborator

    PR #26116 merged as 6e79915d1e3eeaee56b3ebe4a66989fca75f3c3d (reviewed head a4beba2b7692de59ba3d0dc2ede8e5cd57dbe017). The repaired in-memory/spill truth table, race tests, and required CI passed.

    QA handoff on main f6dab28046d70412cec132f0068840896852101c:

    • For in-memory and spilled storage, insert multiple rows with the same key and fail the callback after the first delivered row.
    • Verify callback count, returned removal count, ItemCount, and retained later rows agree at the same per-row boundary.
    • Verify retry can retrieve the rows that were never delivered.

    Pass: only delivered rows are removed/accounted and undelivered rows remain retrievable in both backends. Fail: silent row loss, stale ItemCount, inconsistent counts, or backend-dependent behavior.

  7. VioletQwQ-0 commented on Jul 29, 2026

    @VioletQwQ-0
    Collaborator

    PR #26116 merged as 6e79915d1e3eeaee56b3ebe4a66989fca75f3c3d (reviewed head a4beba2b7692de59ba3d0dc2ede8e5cd57dbe017). The repaired in-memory/spill truth table, race tests, and required CI passed.

    QA handoff on main f6dab28046d70412cec132f0068840896852101c:

    • For in-memory and spilled storage, insert multiple rows with the same key and fail the callback after the first delivered row.
    • Verify callback count, returned removal count, ItemCount, and retained later rows agree at the same per-row boundary.
    • Verify retry can retrieve the rows that were never delivered.

    Pass: only delivered rows are removed/accounted and undelivered rows remain retrievable in both backends. Fail: silent row loss, stale ItemCount, inconsistent counts, or backend-dependent behavior.

  8. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成专项验证。

    测试版本:52ea9f874f492c368a20efef16b46ab8ced2de37

    测试过程:

    1. 分别使用内存存储和 spilled 存储创建 BranchHashmap。
    2. 插入多个相同 key 的数据行。
    3. 在 callback 已接收第一行后主动返回错误。
    4. 检查 callback 收到的行数、返回的 removal count、ItemCount 和剩余数据。
    5. 对同一个 key 重试读取,确认从未交付的行仍可取回。
    6. 普通模式及 -race 模式各连续执行 3 轮。

    测试结果:

    • 两种后端都只删除/计数已经成功交付给 callback 的行。
    • callback 返回错误后,ItemCount、removal count 和实际剩余行数一致。
    • 未交付的后续行没有静默丢失,重试可以完整取回。
    • 内存和 spill 行为一致。
    • 普通及 -race 各 3 轮全部通过,未发现数据竞争。

    结论:该问题在上述最新 main commit 上验证通过。

  9. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在 main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c 补充完成黑盒和白盒验证。

    黑盒验证:

    • MOTR 场景:issue_26081_data_branch_hashmap_bulk.py
    • 基表创建 50,000 行。
    • branch 上执行 10,000 行 UPDATE、10,000 行 DELETE、10,000 行 INSERT。
    • DIFF summary 必须分别得到精确的 10000/10000/10000。
    • 使用独立 key 表选择 2,500 个更新、2,500 个删除和 2,500 个新增键执行 PICK,并验证选中/未选中边界。
    • 执行全量 MERGE,检查 count、sum、变化区间以及源表不变性。

    结果:场景通过;本轮 Data Branch 全集 13/13 case、280/280 条 SQL/断言通过。此前该 bulk 场景连续执行 3/3 也全部通过。

    白盒验证:

    go test ./pkg/frontend/databranchutils \
      -run '^(TestBranchHashmapPopByVectorsStream(InMemory|Spilled)(CallbackError)?)$' \
      -count=1
    ok github.com/matrixorigin/matrixone/pkg/frontend/databranchutils
    

    白盒分别覆盖 memory/spill 正常路径和 callback error 注入,验证已经移除的行会同步更新 ItemCount 和返回计数。黑盒场景不伪造内部 callback failure,只验证公开数据面的大批量计数与结果正确性。

    Unhappy path / 错误注入:

    • memory backend 的 callback 在首批返回 error,检查 error 原样传播、callback 不再继续、已删除行的 ItemCount 正确扣减。
    • spill backend 执行同样的 callback-error 注入,验证磁盘路径计数一致。
    • 黑盒中混合 UPDATE/DELETE/INSERT 后做 selective PICK,检查选中删除键确实消失、未选中边界键仍保留。
    • full MERGE 后通过 count/sum/range 三组断言识别漏行、重复行和 stale-count,而不是只检查语句成功。

    结论:原 callback-error 计数路径及公开 bulk consumer 路径均验证通过,黑盒回归已加入 motr#69。

  10. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Unhappy Path 专项验证

    测试版本:main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c

    白盒错误注入:

    1. memory backend 在首批 callback 中返回 error。
    2. spill backend 执行相同 callback-error 注入。
    3. 检查原始 error 传播、callback 不再继续。
    4. 检查已删除行同步扣减 ItemCount 和 returned removed count。
    TestBranchHashmapPopByVectorsStreamInMemoryCallbackError
    TestBranchHashmapPopByVectorsStreamSpilledCallbackError
    

    白盒测试返回 ok。

    黑盒数据异常组合:

    • 50,000 行基表;
    • 10,000 UPDATE、10,000 DELETE、10,000 INSERT;
    • selective PICK 同时选择更新、删除和新增 key,并检查未选择边界;
    • MERGE 后使用 count/sum/range 检测漏行、重复行和 stale-count。

    结果:内部 callback-error 计数一致;黑盒 bulk 场景通过,并曾连续执行 3/3 通过。回归见 motr#69。

  11. added 3 commits that reference this issue on Sep 17, 2026
    d8f16b8
    0c4ad8d
    d73acee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions