Skip to content

fix(disttae): preserve exact PK reads across transferred deletes - #32668

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:fix/exact-pk-visible-history-32660
Nov 14, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:fix/exact-pk-visible-history-32660

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #32660

What this PR does / why we need it:

logtailreplay.Exact treats three historical candidates as sufficient to prove
absence. Transferred deletes of distinct older physical rows can consume this
budget before the current insertion is inspected. Ordinary complete-PK SELECT
and SELECT FOR UPDATE can silently return zero while prefix/full scans find the row.

This changes only Exact's iterator-local termination rules:

  • Check the key boundary before skipping the opposite insert/delete stream.
  • Do not limit the number of preceding same-key transferred deletes.
  • For the unique PK data stream, inspect the newest insertion at/before the
    snapshot, then finish. Existing RowID/version validation still decides whether
    that insertion is visible; an older logical version cannot resurrect when the
    newest insertion is deleted. This avoids scanning a hot key's entire update history.
  • Keep all matching physical rows eligible for delete iteration. Nonunique
    cluster-key equality uses ExactNonUnique and is unchanged.

No TN, Flush, lock protocol, persistence format, allocator retry/FATAL, or shared
state changes. The fixed candidate count dates to #19658 (2024), not a newly
identified recent introducing commit.

Validation

Pinned upstream base: 6c7457903d9e6e259b6b51e749fecc0ff8a59ea5.
Linux host 50, Go 1.27.1, matching native build, GOMAXPROCS=2, build -p=1.

  • Baseline handler-fed regression: 0/1/2 preceding deletes pass; 3/8 fail.
  • Baseline real TAE -> logtail -> SQL: 14/16 queries correct. After the third
    controlled Flush commit, complete-PK SELECT and FOR UPDATE return no row with
    no error, while prefix/full return (3,1) in the same fixed SI transaction.
  • Patched identical SQL oracle: 16/16 correct, test body 0.55s.
  • Full pkg/vm/engine/disttae/logtailreplay and pkg/vm/engine/test: PASS
    (0.594s / 171.447s). Three existing skipped tests remain skipped, not credited.
  • Repository-pinned golangci-lint 2.14.0 with unchanged .golangci.yml, both
    affected packages: PASS, 0 issues. Final scope-aware self-review passed.
  • Controls cover historical snapshots, true deletion, reinsertion, delete
    iteration by physical block, missing-key boundaries and bounded hot-key work.
  • Three-sample microbenchmark: 10000-version live lookup median 948 -> 682 ns;
    deleted lookup 697 -> 679 ns; one-version lookup approximately unchanged.
    A 1024-transfer-delete prefix costs about 10.7 us with constant 794 B / 10
    allocations per lookup. These are microbenchmarks, not a full-workload guarantee.

Evidence and risk boundary

The SQL fixture reuses EnginePack and real Flush tasks but deliberately holds
three commits and passes no source tombstone objects. It bypasses automatic
flusher admission/worker ordering; real-service reachability and frequency of
that exact history are not established. Main's normal flusher has additional
oldest-first/tombstone/scope constraints. No production scheduler is changed.

The in-process fixture uses test RPC/lock adapters and fixed SI, not a distributed
lock or frontend-protocol reproduction. Ordinary client BVT cannot deterministically
hold these Flush commits. The added test costs less than one second and uses
only four versions of one compound key, with no sleeps or production test hooks.

This does not establish the cause of #32515, durable data loss, or duplicate
allocator IDs. Same-key skipped-delete work is necessarily linear in the retained
prefix; there is no new per-entry allocation/I/O/wait or cancellation interface.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify mergify Bot added the kind/bug Something isn't working label Nov 13, 2026
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Nov 13, 2026
@mergify

mergify Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复核结论:基于 abe333824c4787f0ee0038bb84a5e279f50ea077,未发现需要阻断的代码问题,代码层面建议通过。该结论不代表 CI 已全部通过,也不代表 #32515 的生产根因已经闭环。

设计与正确性

完整检查了 3 个文件及相关调用链:

  • Exact:先判键边界,再区分 INSERT/DELETE;移除“3 条候选足以证明不存在”的假设,修复点位于原有候选遍历职责内。可见性仍交给 isPKItemValid,没有重复建设版本选择、持久化或恢复机制。
  • 唯一 PK 的数据迭代:按 key / commit TS 降序定位快照内最新 INSERT,仅检查该候选。核对了 workspace 中同事务插入再删除的消除路径、RowID 可见性判断、以及物理行清理同步删除主键索引的路径,没有发现需要回退到更旧 INSERT 才能读到合法当前行的新增反例。真实删除不能使旧逻辑版本复活。
  • 删除迭代不受“一个 INSERT 后结束”限制,仍按物理 BlockID/RowID 验证;非唯一 cluster-key equality 通过 ExactNonUnique,没有误用唯一 PK 的提前结束规则。普通 datasource、materialized snapshot datasource 和多条件 union 的迭代/关闭契约也保持兼容。
  • 两组新增测试分别覆盖 handler 输入下的历史版本/转移删除,以及真实 TAE → logtail → SQL 的受控 Flush 时序。复用 EnginePack、四个版本、固定 SI、同一阶段四种查询比较,没有新增生产测试钩子或靠 sleep 碰时序。

性能与 unhappy path

维度 复核结果
Q1 清理 迭代器沿用原 Close/Release 所有权;新增测试的结果、packer、事务及 EnginePack 均有相应清理路径,未发现本次新增的确定性泄漏。
Q2 等待/退出 生产改动没有增加锁、RPC、I/O 或重试。扫描单向推进,到键边界/树尾/最新 INSERT 终止;测试使用现有超时 context,未提交的 Flush 注册回滚。
Q3 增长 新状态只是每个迭代器的 finished 标记,没有新增队列、缓存或逐候选分配。唯一键普通更新历史不再继续遍历旧 INSERT。

必须保留的性能边界:若同键最新 INSERT 前确实积累很多转移 DELETE,时间成本是 O(该前缀长度),不是恒定时间,也没有新增逐条取消检查。不能用固定候选数重新换取错误的空结果。PR 报告的 1024 条前缀微基准有参考价值,但不是线上尾延迟保证;本次未发现足以据此阻断的实际性能回归。

验证与证据边界

  • 本轮独立完成全量 diff、调用方/清理链检查、正确 Go 工具链的 gofmt 检查及 git diff --check,均无问题;没有另起大规模 UT/基准。
  • 复用 PR/Issue 已记录的 Linux 定向红绿、两个相关包及微基准报告,不将其表述为本轮重新执行。验证基线 6c7457903d 到当前合入的 base b6f5f597b8 只改了 .github/mergify.yml,相关生产/测试/依赖语义未变。
  • 查询时 shared build 已通过,UT、coverage、SCA、两项 multi-CN BVT 仍在运行;pending/skipped 不计通过。
  • 当前没有待处理的人审 inline comment。受控测试绕过自动 flusher 的准入/调度,且未传 source tombstone objects;正常生产调度下的触发路径与频率仍未知。此限制已明确披露,不把它当成新的 blocker,也不据此承诺之前生产事故或所有精确点查问题已解决。

当前 GitHub 身份与 PR 作者相同,因此提交 COMMENT 记录无阻断建议,不冒充独立 APPROVE;合并仍需相应 CI 和仓库要求的独立审批。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独立 deep review:基于 abe333824c4787f0ee0038bb84a5e279f50ea077,对照 merge-base b6f5f597b8923b0f6d762e3b18243f39e49c5840 完整审查三个文件及调用链,未发现需要阻断合并的正确性、设计、工程质量或性能问题,APPROVE。用户给出的 #32660 是本 PR 关联的 issue。没有使用 subagent,CI 失败未作为本次判定依据。

设计与实现:修复落在原有 Exact 候选遍历职责内,保留 isPKItemValid 对 RowID/version 可见性的裁决。固定候选数不能证明不存在;唯一 PK 数据流在检查快照内最新 INSERT 后结束,避免旧版本复活,也避免继续扫描整个更新历史。键边界先于 insert/delete 类型过滤,缺失键不会越界扫描相邻键。删除迭代继续检查所有相关物理 RowID;非唯一 cluster-key 使用 ExactNonUnique;普通、materialized snapshot 和 union reader 的调用、关闭契约没有变化。没有引入重复版本选择器、共享状态机、存储路径、后台任务或协议/格式兼容分支。

本轮在 Linux 57、Go 1.27.1 上实际验证,原生库与目标源码输入一致并通过仓库 provenance 校验,使用仓库 CGo wrapper、-count=1:

  • 相同 handler 回归与仅替换 base rows_iter.go 的负对照:旧算法在 0/1/2 条前置转移删除时通过、3/8 条时漏行;当前实现全部通过。
  • logtailreplay 全包:588 个测试/子测试通过,1 个已有 skip 未计入通过,包耗时 0.503s。原有非唯一 cluster、union、同时间写删等控制也通过。
  • TestExactPrimaryKeyPendingFlushes:真实 TAE -> Flush/transfer -> logtail -> SQL 的四阶段 16 次查询全部通过,测试体 0.52s。完整 PK SELECT、FOR UPDATE、前缀和全扫保持相同固定 SI 快照和结果。
  • 独立临时 overlay 探针:1/2/3/4/8/32/128 个历史版本,乱序到达的旧物理行转移删除、各历史快照、COW 状态副本、真实 HandleObjectEntry GC、真删除后重插,以及同提交时间 INSERT/DELETE 两种到达顺序;Exact/IN/PREFIX_EQ 与独立全扫 oracle 一致。探针没有修改提交内容。
  • gofmt、git diff --check、上述 go test 默认 vet 均通过。作者提供的完整 engine/test 包和固定版本 golangci 报告仅作为补充证据,不表述为本轮重跑。

性能:三次交替短采样中,10000 版本存活键点查中位数 727.8 -> 509.4 ns(约 -30%),1032 B/12 次分配 -> 794 B/10 次。首次短样本出现单版本约 +7% 信号,随后以相同 GOMAXPROCS=4、500ms 的交替采样复核,中位数 326.7 -> 315.2 ns,样本波动重叠,没有复现稳定退化。1024 条前置转移删除约 8.0us,仍为 794 B/10 次分配;扫描成本随同键前缀长度线性增长。共享机器微基准不能替代部署负载/尾延迟保证。

unhappy path 结论
Q1 资源 迭代器沿用 Close/Release;测试结果和 packer 释放,未完成事务通过 cleanup 回滚后再关闭 EnginePack。未发现新增确定性泄漏或双重清理。
Q2 终止 扫描在不可变的已捕获状态中单向推进,到键边界/树尾/最新 INSERT 结束,没有新增锁、RPC、I/O 或等待边。长前缀内仍无逐条 ctx 检查,成本是有限的 CPU 扫描,不宣称恒定取消延迟。
Q3 增长 finished 为迭代器私有标记,替代旧计数,无新容器、持久引用或逐候选分配。历史保留/GC 机制未改变。

测试增量主要由轻量 handler 场景和一个复用 EnginePack 的四版本 SQL 场景构成;两者分别证明遍历边界与真实 SQL 消费路径,未发现用重复重型 fixture、sleep、弱化断言来增加覆盖数量的问题。文档边界也已审阅。

已有讨论:本 PR 没有人审 inline/reply 待处理;#32660 的“main 也需要实际复现”已由后续 main 红绿报告及本轮 main 执行补齐。正常 flusher 的 oldest-first/tombstone/对象 scope 约束已核对,但 fixture 刻意控制三个 Flush 提交且不传 source tombstone objects,正常生产调度下该历史的到达路径与频率仍未知。这是已披露的证据边界;本结论针对 #32660 的读取算法缺口,不宣称 #32515 事故根因或所有 PK 点查问题已解决。

@mergify mergify Bot added the queued label Nov 13, 2026
@mergify

mergify Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request merged manually

42 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Fri 13 Nov, 23:24 · rule main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue Sat 14 Nov, 00:06 · abe3338

Reason

Pull request #32668 has been merged manually at 0e8d3ca

Hint

You were too fast!

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot removed the queued label Nov 14, 2026
mergify Bot pushed a commit that referenced this pull request Nov 14, 2026
…letes (#32692)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Backport of #32668 to `4.2-dev`, addressing #32660 on this release branch.

## What this PR does / why we need it:

Cherry-pick `50bb07caae467c08b951154757933323e5c607fa` with `-x`, onto
`4.2-dev@2397892`. There were no conflicts
and no branch-specific source or test adaptations.

`logtailreplay.Exact` must not infer that a unique PK is absent after inspecting
only three historical candidates. Multiple transferred deletes can precede the
live insertion and consume that budget. The old implementation returns an empty
SQL result without an error, while prefix/full scans find the same row.

The patch changes only iterator-local termination:

- Check the key boundary before skipping entries from the opposite stream.
- Skip all preceding same-key transferred deletes, with no arbitrary count cap.
- For unique-PK data iteration, validate only the newest insertion at/before the
  snapshot using the existing RowID/version visibility logic, then finish.
- Keep delete iteration eligible to visit every matching physical RowID.

No TN, Flush scheduler, lock protocol, persistence format, allocator/FATAL, shared
state or retry changes. No main-only cluster-key/range changes are included.

### Branch-specific validation

Validation runs on host 50 in an isolated worktree with a fresh 4.2 native build,
Go 1.26.4 linux/amd64, GOMAXPROCS=2 and build parallelism 1. Main CI evidence is
not substituted for branch-specific checks.

Baseline retains the exact cherry-picked tests and reverses only the production
function change:

- Handler-fed history: 0/1/2 transferred deletes pass; 3/8 fail with no row.
- Real TAE -> Flush/transfer -> logtail -> SQL: 14/16 checks correct. After the
  third controlled Flush commit, exact SELECT and FOR UPDATE return no rows;
  prefix/full return `(3,1)` in the same fixed SI transaction, with no SQL error.

Patched candidate:

- Identical SQL oracle: **16/16 checks pass**, body 0.53s.
- Full `pkg/vm/engine/disttae/logtailreplay`: **PASS**, 0.278s.
- Full `pkg/vm/engine/test`: **PASS**, 155.151s.
- Four existing skipped tests remain skipped, not credited as passes.
- Three-sample microbench medians (ns/op):

  | History | Original | Patched |
  | --- | ---: | ---: |
  | 1 version, live | 409.4 | 419.6 |
  | 1 version, deleted | 440.0 | 424.4 |
  | 100 versions, live | 735.9 | 506.3 |
  | 100 versions, deleted | 543.0 | 513.9 |
  | 10000 versions, live | 972.8 | 670.1 |
  | 10000 versions, deleted | 708.2 | 684.2 |

- 1024 preceding transferred deletes: 10.998 us median, fixed 810 B /
  10 allocations per lookup. The one-version live median is +2.5% in this short
  shared-host test; this is not a full-workload performance guarantee.
- Full two-package golangci-lint from the branch-pinned v2.6.2 source, built with
  Go1.26.4 and using unchanged `.golangci.yml`: **PASS, 0 issues**. No new filters
  or exclusions. An initial Go1.27-built linter produced a formatter mismatch
  on an unchanged file; it was corrected by matching the toolchain, not by
  changing unrelated code.
- Final scope-aware self-review, branch-native gofmt and `git diff --check` pass.
- GitHub CI for this backport is separate and must still pass before merging.

### Evidence boundaries

The SQL fixture holds three real Flush commits and passes no source tombstone
objects. It bypasses automatic flusher admission/order and uses EnginePack's
test RPC/lock adapters. Normal automatic-scheduler reachability/frequency is not
proved. This is not a production multi-CN reproduction, proof of durable loss,
or a claim that #32515 has been diagnosed/fixed.

Skipped-delete work is linear in the same-key prefix. There is no new per-entry
allocation/I/O/wait, and ordinary update history terminates after the newest
insertion. Microbenchmarks do not guarantee production tail latency.


Approved-by: XuPeng-SH <[email protected]>

This branch was successfully deployed

No deployments
ci — 92236214 Deployed Nov 14, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Exact PK iterator misses a live row beyond three candidates (controlled SQL reproduction)

3 participants