Skip to content

[Subtask]: [W1] 修复 Vector.Shuffle 的 data lease 泄漏与 sorted 标记失效 #32016

Closed

Description

@iamlinjunhong

Parent Issue

#31871

对应 06-vector/vector-field-plan.md(草案 v3)的 W1:Shuffle 替换 data 后遗留旧 lease;重排后 sorted 未清除。

负责人:@iamlinjunhong。优先级:severity/s0,按负责人明确要求列为当前工作。W1 无前置依赖;后续 W3(所有权统一)及 W7b(其他性质标记失效入口)以此为基础。

Detail of Subtask

只修复 Shuffle 的两个行为问题,不扩展为 Vector 所有权整体重构。

已有证据

讲义与规划的基线为 main@d39d8a40343ccd40bf665036388df7a40ee867f8,以下实测为文档记录,本次建 issue 未重新执行;开工时须重新固定最新 main 并复核。

  1. 新 data 泄漏:shuffleFixedNoTypeCheck 分配新 data 并替换 v.data,但旧 dataLease 仍保留。Free 优先释放旧 lease 并清空当前 data,遗漏新分配的 mpool 内存。4 行 borrowed int64 执行 Shuffle([3,1]) 再 Free,旧 lease 释放一次,但 mpool 仍剩 16 字节。
  2. sorted 失效未维护:sorted 的 [1,2,3] 经 Shuffle([2,0]) 变成 [3,1],仍保留 sorted;直接调用 BETWEEN 的有序快路径,BETWEEN 1 AND 2 得到错误的 [true,true],清除标记后为 [false,true]。

两项均为组件 API 层面的证据,尚未证明生产 SQL 可达性,不宣称已确认生产事故或历史首坏提交。

源码参考:Shuffle、shuffleFixedNoTypeCheck、Free、releaseBorrowedData、BETWEEN。

验收标准

  • 4 行借用 int64 → Shuffle([3,1]) → Free:值正确,旧 lease 持有责任恰好释放一次,最终 mpool 余额归零;存在其他 retained view 时仍可读。
  • 覆盖自有内存、legacy alias 和变长列对照;变长列只替换描述符,不复制或提前释放仍共享的 area。
  • data 分配失败、NULL/grouping bitmap 重映射分配失败时,原 owner 与原数据保持有效,新分配回收,无泄漏或重复释放。
  • sorted 向量经破坏顺序的 Shuffle 后不再标记 sorted,并通过真实 BETWEEN 消费者验证上述结果为 [false,true];失败时标记与保留的数据一致。
  • 核对 Shuffle 调用方(文档基线为 21 处),明确是否有依赖升序选择保留 sorted 的快路径及其处理。
  • 同环境微基准对照证明普通独占路径无新增分配、引用计数/原子操作或实质性能回退。
  • 相关回归与失败路径测试通过;记录当前基线、命令和结果,供 W3/W7b 复用。

Describe implementation you've considered

复用或增加一个小型“替换 data 并退役旧 owner”的内部入口:所有可能失败的分配和重映射完成后,发布新 data 的独占状态;旧 owner 为 lease 时释放本向量的持有责任,为自有内存时通过原有 mpool/记账机制释放,为 legacy alias 时不释放。Shuffle 使用该入口,成功后维护 sorted。

不采用“先完整物化再 Shuffle 复制”。不删除字段、不改公开函数签名、不增加新的所有权状态机;Shrink/原地排序的可写性属于 W2,完整所有权收敛属于 W3,Shuffle 之外的 sorted/areaDisjoint 盘点属于 W7b。一个 PR 只完成本工作项。

Additional information

来源:06-vector/vector-field-plan.md 的 W1、第 11 讲《案例:Shuffle 漏掉的 16 字节》、第 16/17 讲的性质标记和范围约束。

旧任务 #31883 已关闭;相关 PR #31932 也已关闭且未合并。本 issue 按当前 W1 规划重新明确局部修复范围,覆盖泄漏与 sorted 两项,不沿用旧任务的广泛重构范围。旧 PR 中的局部替换思路可供参考,但不视为当前 main 已修复或验证通过的证据。

Activity

  1. self-assigned this
    on Nov 2, 2026
  2. added
    severity/s0
    on Nov 2, 2026
  3. XuPeng-SH commented on Nov 2, 2026

    @XuPeng-SH
    Contributor

    QA handoff for the scoped W1 fix (not acceptance).

    • Merged PR: Fix vector shuffle ownership and sorted metadata #32020, main commit cd40f4e15ca44b58c5e81ace48071ee54c483b4f (2026-11-02 18:24 UTC), final PR head 08968d088eb211aaa9418ab358e9063a1e0debad.
    • Scope: retire the previous data lease correctly when Shuffle installs replacement data; preserve retained views/shared varlen area; prepare fallible metadata/bitmap changes before publication; invalidate sorted metadata for non-monotonic selections. This issue is the W1 child, not completion of parent [Refactoring]: 收敛 Vector 所有权与语义状态,保留共享收益并保护普通执行路径 #31871 or W2/W3/W7b.
    • Evidence reviewed remotely: author reports vector/mergeutil suites, the real BETWEEN consumer regression, vector race tests and vet/lint passing at e70a3b3a. The later review reports 343 vector tests without skips and no confirmed actionable findings. The earlier ordinary-path slowdown was addressed by the uniform-metadata fast path; the later review's large-int64 performance result remains inconclusive (+3.30% median, paired interval -2.14% to +17.88%), not proof of equivalence or a current verified regression. Final PR-head checks now show CI Required, UT, SCA, coverage, shared build and COMPOSE/PROXY BVT success; upgrade and several other suites were skipped.
    • No local test was run by this handoff pass. Final merged-main reproduction, production SQL reachability and end-to-end performance remain unverified. Please retest borrowed/owned/legacy-alias vectors, retained-view lifetime and mpool zero balance; shared varlen area; data/NULL/grouping/sidecar allocation-failure rollback; ascending versus disordered selections and real BETWEEN results; uniform/mixed metadata and zero-allocation buffered controls. Recheck large-int64 throughput with controlled paired measurements, including sorted-consumer tradeoffs.
    • No current applicable verified counterexample or unfinished development follow-up was found after reviewing the issue and PR discussion. Handing this issue to @Ariznawlll for QA; leave it open until QA decides the outcome.
  4. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。原有的 lease 泄漏及 sorted 正确性问题在当前 main 的覆盖路径中未再复现,但升序选择的 Shuffle 组件微基准出现重复的时延回退,尚未满足本 issue 对普通路径性能的验收要求。该测量不是端到端查询或生产性能结论。

    关键证据

    • 当前版本:官方 main@d61a7206c16b30baa4efc525eb6067c4364035aa;对照为修复前 b86ddaf607e6502dea4e84f4ce3ea446b2a7c649。本机 Apple M3/macOS arm64、Go 1.27.1、GOMAXPROCS=2;运行时机器还有其他工作负载,未做 CPU 独占或亲和性绑定。
    • **Happy case:**主库 vector 整包普通及 -race 检查通过;借用 data/retained view、变长共享 area、legacy alias、失败注入和分配回滚等 Shuffle 覆盖三轮。修复前借用 int64 Shuffle 后 Free 仍留 16 字节,三轮复现;当前对应检查通过。真实 BETWEEN 消费者在修复前把 [false,true] 错算为 [true,true],当前两种 Shuffle 路径均三轮通过。
    • **性能复测:**使用当前主库 pkg/container/vector/shuffle_sorted_test.go 的 BenchmarkShuffle;修复前树仅加入同一 benchmark 测试代码,不修改产品代码。对 n=8192/varlen=false/buffered=true/ascending,两版以 ABBA/BAAB/ABBA 顺序交替各运行 6 次,每次 1 秒:基线 [6116,5598,6535,6032,5511,5446] ns/op,中位数 5815;当前 [8280,8068,9926,8546,7994,7719] ns/op,中位数 8174,约 +40.6%。两版均为 0 B/op、0 allocs/op。在先前独立三轮采样中,同一升序场景中位数 5852 → 7882 ns/op,方向一致;降序对照中位数 5252 → 5381 ns/op。非缓冲普通路径两版均为 1 次分配。
    • **Unhappy case / 白盒:**分配失败时保留原 owner 和数据;并发 lease 生命周期及 race 检查通过。源码中 preserveSortedAfterShuffle 对原本标记 sorted 的向量遍历选择下标;它与升序场景中额外成本相符,但本次没有 profile,不能断言全部差异的归因。基准入口见 BenchmarkShuffle,可用 go test ./pkg/container/vector -run '^$' -bench '^BenchmarkShuffle/n=8192/varlen=false/buffered=true/ascending$' -benchmem -benchtime=1s 复测。

    待解决点

    • 请研发在安静、可控的相同软硬件环境复现升序与降序对照,并确认 sorted 保留策略的成本及可接受性;如确认为回退,请修复后复测正确性、所有权/失败回滚和性能。当前结果不推断生产 SQL 影响,也不能把修复前原有错误当成当前 main 正确性失败。
  5. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。最新 main 中借用内存释放、排序标记和失败回滚的本轮组件用例通过,但升序 Shuffle 的微基准相对修复前仍有重复可见的耗时增加,未满足本 issue 的普通路径性能验收;这不是端到端 SQL 或生产环境回退结论。

    关键证据

    • 当前版本:官方 main@dd2499cdcdf2722848851d4debca6cd6c66b4751;修复前对照 b86ddaf607e6502dea4e84f4ce3ea446b2a7c649。macOS / Apple Silicon,Go 1.27.1,GOMAXPROCS=2;机器未做 CPU 独占或亲和性绑定。当前 main 的 vector 相关源码相对此前测试的 d61a7206c16b30baa4efc525eb6067c4364035aa 无变更。
    • Happy / Unhappy case:最新 main 的 TestShuffleBorrowedDataRetiresOwner、TestShuffleVarlenSharedLeaseAndLegacyAlias、TestShuffleBorrowedLeaseLifecycle、TestShuffleSorted、借用数据及二进制字符串分配失败回滚用例各执行 3 次,通过。此前 完整验收评论 覆盖真实 BETWEEN 消费者和 race;本次不把先前结果当成新跑的测试。
    • 同一 BenchmarkShuffle/n=8192/varlen=false/buffered=true,两版按 ABBAABBA 交替运行各 4 次,每次每个方向 1 秒。升序修复前 [5480,5284,5408,5321] ns/op,中位数 5364.5;当前 [7562,8771,7517,7603] ns/op,中位数 7582.5,约 +41.3%。降序对照中位数 5138.5 → 5061.5 ns/op。两方向两版均为 0 B/op、0 allocs/op;方向与此前 6 轮对照一致。基准代码见 BenchmarkShuffle。

    待解决点

    • 请研发在安静、可控环境复核升序/降序和 sorted 消费者整体开销,确认选择向量顺序检查的成本及验收标准;如需优化,修复后重新核对借用/自有/变长数据、失败回滚、真实 BETWEEN 及性能。当前未取得性能 profile,不把全部差异归因于某个函数。
  6. removed their assignment
    on Nov 3, 2026
  7. XuPeng-SH commented on Nov 4, 2026

    @XuPeng-SH
    Contributor

    QA re-handoff for W1 follow-up #32168, merged into main as 4bd71af013ab3bcd7480cbea0b21a6efe4e2966d. It folds ordering proof into gather and skips identity copy-back only when actual data and scratch do not overlap, preserving lease, sorted metadata and failure atomicity. The earlier ascending-Shuffle failure on dd2499c is the specific cost addressed by this later merge, not a test result for the new version.

    Author reports vector/function tests, focused mergeutil tests, alias/byte-overlap boundaries and vet/lint passing. The shared-host 8192-row ascending component comparison improved 30.52%, not an end-to-end SQL result. A +0.72% small descending control signal remains inconclusive. Review used extracted-function probes and frozen-head CI without a new race run; reviewed head fec2852 differs from final head 2187126. This handoff run checked remote evidence only and ran no local tests or performance measurements.

    Please verify borrowed/owned/legacy aliases, shared variable-length area, retained views, mpool cleanup, allocation rollback, real BETWEEN consumers, ascending/descending and identity overlap. Remeasure the original 8192-row case and small controls in a quiet paired setup. Nonidentity scratch/data overlap is not supported in-place gather. This handoff does not complete parent #31871 or W2/W3/W7b, and the issue remains open pending QA acceptance.

  8. slyunnn commented on Nov 5, 2026

    @slyunnn

    Regression and extended verification for #32016 (2026-11-05)

    Verification Environment

    • Frozen latest origin/main at test start, 3820136ff2bdeb0b7f3136fd4a66a00b8d571f55, fix(plan): intersect BETWEEN selectivity with column domain (#32224). The verification worktree was clean and matched that commit.
    • Rebuilt the native service from this commit with Go 1.27.1, macOS arm64, Apple M2 Pro. Build log, binary version and runtime git_version() identified 3820136ff2.
    • Fresh topology of 2 CN / 1 TN / 1 Log service / Proxy, local disk file service, service GOMAXPROCS=4. Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001 and CN2 127.0.0.1:16002 all connected and executed the SQL consumer smoke checks through PyMySQL text protocol.
    • Component tests exercised the Go API directly. Benchmarks used Go 1.27.1, GOMAXPROCS=2 and separately compiled test binaries. The historical comparison used clean b86ddaf607e6502dea4e84f4ce3ea446b2a7c649 with the identical current BenchmarkShuffle supplied through an external Go overlay. Product files remained clean.

    What Was Covered

    • OK — Original borrowed int64 vector containing 4 rows followed by Shuffle([3,1]) produced [4,2], retired this vector's old lease responsibility, preserved the retained view's [1,2,3,4], released the underlying lease exactly once after the final owner and returned mpool usage to zero. TestShuffleBorrowedDataRetiresOwner passed.
    • OK — Owned storage, borrowed storage, legacy aliases, shared variable-length area, retained-view release order, buffered and unbuffered paths passed. Shared variable-length payload stayed readable until its remaining owner released it.
    • OK — Data, NULL/grouping bitmap, string metadata/sidecar and borrowed-area allocation failures passed rollback assertions in the complete vector suite. Failed operations preserved valid original data/ownership and released prepared allocations.
    • OK — Empty, singleton, increasing, repeated, gapped, identity and inverted selections passed sorted-property checks. Identity scratch aliases, offset/last-byte overlap, adjacent ranges, zero-length scratch and scratch growth passed their supported boundary tests.
    • OK — Real betweenImpl consumer checks passed after destructive reordering, including the original [false,true] result. Buffered variants and integer BETWEEN / variable-length PrefixEq controls matched independent scalar expectations. The full function package passed.
    • OK — Full vector race tests passed. The exact TestShuffleBorrowedLeaseLifecycle race test then passed 100 repetitions, covering retained-view release interleavings and reuse.
    • OK — The complete mergeutil consumer suite passed. Caller review found 20 non-random .Shuffle / .ShuffleWithBuf call expressions including forwarding wrappers. Monotonic selections preserve the existing sorted proof. In mergeutil, callers explicitly reestablish sorted status for the sort-key column and clear it for other columns after arbitrary sorting permutations.
    • OK — At each SQL endpoint, an 8,193-row descending sort/limit followed by BETWEEN returned (8193,false), (8192,true), (8191,true) and the filtered query returned IDs 8191,8192. Version checks, row counts and cleanup passed.
    • OK — Same-host alternating benchmark measurements completed 6 samples per version for each fixed buffered ascending/descending case at 64 and 8,192 elements. Each sample used 1s per case. All key measurements reported 0 B/op, 0 allocs/op.
    • OK — The full 24-cell allocation matrix per version covered fixed/variable-length data, buffered/unbuffered operation, both sizes and unsorted/ascending/descending states. Allocation bytes and counts matched exactly between versions. These 100ms samples were used for allocation checks only.
    • Not covered — Production SQL reachability of the exact borrowed-lease defect remains unestablished, as stated in the original issue. The SQL smoke checks do not establish that path. Live MySQL differential execution, end-to-end performance, CPU-exclusive/affinity-pinned benchmarking, TKE and a single SQL query spanning both CNs were outside this component acceptance.

    Findings

    • Original W1 fix, PR Fix vector shuffle ownership and sorted metadata #32020, merged as cd40f4e15ca44b58c5e81ace48071ee54c483b4f, Fix vector shuffle ownership and sorted metadata. Original fix
    • Follow-up PR Optimize Shuffle ordering proof and identity copy #32168 merged as 4bd71af013ab3bcd7480cbea0b21a6efe4e2966d, Optimize Shuffle ordering proof and identity copy. It folds ordering proof into the gather and avoids identity copy-back only for non-overlapping data and scratch. Both commits are ancestors of the tested main. Performance follow-up
    • The previous approximately 40% ascending component slowdown was not reproduced after the follow-up. With the issue's original benchmark fixture, the 8,192-element ascending median changed from 4875.5 ns/op to 4749 ns/op, about -2.59%; descending changed from 4871.5 to 4846.5 ns/op. The 64-element ascending and descending medians changed from 84.74 to 49.53 ns/op and 84.465 to 56.865 ns/op respectively.
    • Measurements alternated versions in ABBA / BAAB / ABBA order. All task-owned builds, other tests and the test service had ended before the key timed samples. The desktop remained a shared OS host with background activity and no affinity pinning. These observations establish the scoped component result on this host, with no production throughput claim or attribution of every version difference to a single function.
    • The already documented general non-identity scratch/data overlap remains outside supported independent-scratch gather behavior. Identity overlap was directly tested. This acceptance covers W1 only; parent [Refactoring]: 收敛 Vector 所有权与语义状态,保留共享收益并保护普通执行路径 #31871 and W2/W3/W7b retain their own scope.

    MOTR / BVT Coverage

    • Reused existing component tests in shuffle_ownership_regression_test.go, shuffle_sorted_test.go, allocation_account_test.go and the actual consumers in shuffle_between_regression_test.go. Complete vector, function and mergeutil suites passed with -count=1, along with full vector -race and the focused lifecycle repetitions. These runs reported no skipped tests.
    • Exact ownership and allocation-failure states are directly observable at the component API. Existing tests supply maintained coverage for those contracts. This run added no duplicate MOTR case or commit, and made no weekly MOTR PR change.
    • Native setup initially rejected a premature historical benchmark compile while its required native library was still building. After that build completed, benchmark compilation and all recorded benchmark runs succeeded. This was a setup ordering error, with no SQL or product assertion failure.
    • Independent compilation/preparation and component checks used bounded parallelism. Timed key benchmark runs were sequential, isolated from task-owned service/test work. The allocation-only matrix and the final focused race repetitions could overlap because timing from that matrix was outside the performance claim.

    Final Conclusion

    Fixed for the scoped W1 issue #32016 on 3820136ff2bdeb0b7f3136fd4a66a00b8d571f55. Original ownership and sorted-consumer assertions, supported overlap boundaries, allocation-failure atomicity, race checks and the previously blocking ascending microbenchmark passed this QA run.

    Large-data regression is not required for this component contract. Required size-sensitive coverage was completed at 64 and 8,192 elements using the reported benchmark boundary; the ownership and sorted defects have deterministic witnesses of 4 and 3 rows. The three-endpoint SQL smoke used 8,193 rows. Arbitrary data enlargement or a TKE throughput workload would not strengthen these scoped component assertions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/subtaskseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions