Skip to content

[4.2-dev] fix(disttae): preserve RowEntry snapshot isolation during replay - #32616

Merged
XuPeng-SH merged 2 commits into
4.2-devfrom
fix/4.2-rowentry-snapshot-cow
Nov 12, 2026
Merged

XuPeng-SH merged 2 commits into
4.2-devfrom
fix/4.2-rowentry-snapshot-cow

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:

issue #32613

What this PR does / why we need it:

Backport only the existing-RowEntry copy-on-write correction from main #30617 (b58d92746d619a9313fe48590eb1d491f5fe8056) to 4.2-dev. Base: 51c0af67a207f7a30dddb4bc385c3466ae5696de.

PartitionState.Copy() copies B-tree nodes, not the *RowEntry elements they reference. Both row handlers previously mutated an already-published entry when replay matched its (BlockID, RowID, Time). The unpublished mutation therefore escaped into old snapshots and raced their readers.

  • Copy an existing entry before changing its Batch/Offset/primary-key bytes or deletion flag in both INSERT and DELETE replay.
  • Preserve physical version identity, RowEntry ID, primary-index cardinality, and existing delete-wins behavior. The new-key path is unchanged.
  • Correct the misleading value-type comment on the pointer-valued tree.
  • Add a two-row regression covering duplicate INSERT/DELETE and both same-version INSERT/DELETE orders with/without payload retention, before/after actual Partition publication, row/PK iterators, and a concurrent old-entry reader.

This is not a full cherry-pick: no composite CLUSTER BY feature, batch-compaction change, new lock/cache/state machine, public API, persisted format, or TN protocol change is included. The incident's duplicate-delivery/overlap precondition remains unproven; this PR fixes the demonstrated replay ownership defect, not a claimed incident root cause.

Validation

macOS arm64, Go 1.26.4, native libraries freshly built from this 4.2 base (make -j2 cgo). Go validation uses GOMAXPROCS=2, GOMEMLIMIT=3GiB, -p=1, explicit TMPDIR, and the repository CGo test wrapper.

Check Result
Same new test on unchanged production baseline All eight subcases FAIL; old entry fields/identity are changed
Same baseline under -race FAIL with DATA RACE in both row replay handlers versus the retained-entry reader
Fixed focused regression PASS, all eight subcases
Fixed focused -race -count=100 PASS (single-run test-body measurement 0.01 s; adaptive count capped at 100)
Complete logtailreplay owning package, normal PASS
Complete logtailreplay owning package, race PASS
Scoped go vet, repository molint, configured golangci-lint v2.6.2 built with Go 1.26.4 PASS; incremental golangci-lint reports 0 issues
Go 1.26.4 gofmt and git diff --check PASS

Tested with:

.agents/skills/mo-dev/scripts/mo-cgo-test -p=1 -v -count=1 -timeout=120s \
  -run '^TestPartitionStateRowEntrySnapshotIsolation$' ./pkg/vm/engine/disttae/logtailreplay
.agents/skills/mo-dev/scripts/mo-cgo-test -p=1 -race -count=100 -timeout=120s \
  -run '^TestPartitionStateRowEntrySnapshotIsolation$' ./pkg/vm/engine/disttae/logtailreplay
.agents/skills/mo-dev/scripts/mo-cgo-test -p=1 -count=1 -timeout=180s ./pkg/vm/engine/disttae/logtailreplay
.agents/skills/mo-dev/scripts/mo-cgo-test -p=1 -race -count=1 -timeout=240s ./pkg/vm/engine/disttae/logtailreplay

No new SQL BVT: SQL cannot directly request replay of an identical physical row version with a different batch position. The regression exercises the real replay handlers, publication, scan and PK iterator contracts without sleeps, a cluster, or a probabilistic SQL reproducer. No end-to-end incident reproduction is claimed.

Cost and self-review

Existing one-row repeated-replay benchmarks (-benchtime=100ms -count=3, same toolchain) show exactly +112 B and +1 allocation per existing-key replay for both handlers (INSERT: 1056→1168 B/op; DELETE: 1392→1504 B/op). No payload copy or new-key allocation is added. This is a local allocation check, not a claim about production throughput.

Ownership audit Result
Q1 Replacement and old entries are Go-managed; existing state/reader reachability owns retention. No new manual free or double-cleanup path.
Q2 No new production waits, locks, or goroutines. The finite test reader is joined before assertions/fixture cleanup.
Q3 One fixed-size replacement per existing-key hit; old entries become collectible when snapshots release them. No new history list/registry.

Focused bug fix: no feature design document needed. Both handler branches and their row/PK readers have been reviewed; compatibility, delete precedence and identity are preserved. No merge or release is requested by this PR.

Backport the RowEntry COW correction from main #30617 without the CLUSTER BY feature. Preserve version IDs and delete precedence; cover retained snapshots, reordered replay batches and concurrent readers.

Related to issue #32613.
@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 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@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.

复审结论:当前 revision 未发现需要阻断的代码问题,支持这个最小范围的 4.2 回补。当前账号也是 PR 作者,因此这里只提交 COMMENT,不作为独立 reviewer 的 APPROVE,也不替代 CI/仓库合并门禁。

核对范围:4.2-dev 51c0af67a207 → 598b782e3d2c,完整阅读两个变更文件,并追踪 Partition 发布、普通行扫描、主键迭代、数据源/CDC batch 消费以及行/索引 GC。

设计与正确性

  • 修复放在已有的两个行回放写入口:命中已有物理版本后先复制 RowEntry,再通过原来的 B-tree Set/Partition 发布路径替换。没有新增状态机、读者锁或另一套回放逻辑;对应 main #30617 的两处 COW 修复,不携带其 CLUSTER BY 功能。
  • 浅复制在这里足够:后续写入替换 Batch 指针、Offset、Deleted 和主键字节切片,并不原地修改旧 batch payload/旧 key 字节。已检查的扫描和 CDC 消费路径将数据写入自己的输出 batch。无需深复制整个 payload。
  • 保留 RowEntry.ID、物理键和时间戳,不改变主键索引的版本匹配及 GC 查找;仍保留原有 delete-wins 语义,重复插入不会复活同版本删除。新版本首次插入路径不变。
  • 发布前修改不再泄漏给旧快照;即使新状态最终没有发布,旧行条目也不受影响。

Unhappy path 与性能

  • Q1:新条目和旧条目均由保留它们的树/读者引用及 Go GC 管理;没有引入手动 Free、引用计数或双重清理责任。
  • Q2:没有新增生产锁、channel、后台任务或等待链。测试中的有限读者在断言和资源清理前已收回。
  • Q3:是原键替换,不是追加新版本历史;旧对象随快照释放后可回收,没有新增常驻 registry。
  • 代价不是零:已有 benchmark 的对照结果为每次命中已有版本增加 1 次分配、112 B;不复制 batch payload,不增加编码和树查找。旧快照会继续保留其原 batch,因此保留内存不能简单理解为只有 112 B,这属于快照隔离应有的生命周期成本。微基准不等同于生产吞吐保证。

验证证据与边界

本次核对并复用同一代码 revision 的本地证据,没有重复启动大规模 UT:

  • 相同新测试在修复前 8 个场景全部失败;race 报告直接指向 HandleRowsInsert/Delete 与保留快照读者的冲突。
  • 修复后 8 个场景通过,覆盖重复插入/删除、同版本插删两种顺序、noData 两种模式、batch 重排、发布前后、真实行扫描和主键迭代。
  • owning logtailreplay 包普通/race 测试通过;定向 -race -count=100 通过;scoped vet/molint/增量 lint 及 diff 检查通过。以上本地运行平台为 Darwin arm64。
  • 查询时 GitHub Linux UT、coverage、SCA/shared build 等仍在运行,不能视为已经通过;MongoDB E2E 已通过。

该证据确认的是共享 RowEntry 的 COW/并发缺陷及本补丁的修复效果,不证明某次生产事故发生了重复回放,也不能据此承诺此前的连续空结果事故已定位或修复。

@XuPeng-SH

XuPeng-SH commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 598b782e3d2c03f3044c2ad05d51eea3012d2c84

Review completed. Read the assessment.

@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.

mo-bot review · mo-bot-local · review

Commit: 598b782e3d2c03f3044c2ad05d51eea3012d2c84

Assessment: approve

The narrow copy-on-write fix preserves published RowEntry values, identity and delete precedence. It reuses existing replacement/publication paths without copying immutable payloads or adding synchronization.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • Reviewed both changed files, all four discussion entries, linked issue and affected publication/reader/CDC/GC consumers.

  • Unchanged regression fails all eight baseline cases; fixed focused race passes all eight. Adaptive 100-repeat race check passes 100 roots and 800 subcases.

  • Normal and race owning-package runs each pass 146 root tests, with one existing skip.

  • Allocation controls confirm +112 B and one allocation per existing-key replay. Build, vet, formatting and diff checks pass.

  • Final audit verified clean source and correlated 15 execution receipts with retained logs.

Limitations:

  • TestCountTombstoneRowsWithRealObject remains skipped.

  • Validation covers the replay component; production duplicate delivery and incident root cause remain unproven.

  • Allocation measurements do not establish production throughput or total retained memory; old snapshots retain their original payloads.

This branch was successfully deployed

No deployments
ci — f1e3c26a Deployed Nov 12, 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 kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants