Skip to content

[SEEK-1414] Propagate cancellation to parallel DDL transactions - #2639

Open
willlling wants to merge 2 commits into
masterfrom
fix/seek-1414-ddl-cancel
Open

willlling wants to merge 2 commits into
masterfrom
fix/seek-1414-ddl-cancel

Conversation

@willlling

@willlling willlling commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

修复并行 DDL 收到外层取消信号后,内部事务仍可能提交的问题,使提交准入前生效的取消能够触发回滚并保留原对象和数据。

背景: 在真实复现场景中,元数据行锁保持 6 秒,DDL 预算为 20 秒,用户约在第 2 秒执行 KILL QUERY。旧构建最终返回取消错误 1317,但表已经被删除,说明外层用户会话与内部 SQL 会话没有一致处理取消状态。

根因: 并行 DDL 使用独立的内部 SQL 会话执行事务,但该会话没有完整观察来源用户会话上的 KILL QUERY 状态。因此,外层查询已经被取消后,内部事务仍会继续等待元数据锁,并在锁释放后完成提交。直到控制流程回到外层,会话检查才发现取消并返回错误 1317。此时删除已经生效,所以仅凭返回取消错误不能判断 DDL 已经回滚。

修复与取舍: 修复后的并行 DDL 会创建 CommandCancellationScope,并持有来源用户会话的受管理引用。内部 SQL 和 ObDDLTransController::wait_task_ready() 会调用 check_current(),检查当前调用链是否已经取消。全部提交前 SQL 完成后,admit_commit() 会根据真实事务身份找到对应作用域,并在与 KILL 状态发布共用的会话锁保护下,将 ACTIVE 状态转为 CANCELLED 或 COMMITTING,明确决定本次事务是否可以进入提交。回滚和失败补偿会通过 CommandCancellationMask 暂时屏蔽这一取消检查,以便完成必要清理,但原有超时和资源限制仍然有效。作用域结束时会恢复线程局部状态并归还会话引用。该修复以提交准入为边界:准入前生效的取消会阻止提交,准入后的 KILL 则可能已经来不及撤销提交决定。

转折: Linux 原生边界验证表明,在提交准入前执行 KILL 能保留原表;如果在准入已经返回 COMMITTING 后执行 KILL,则仍可能返回错误 1317,而且 DROP 已经提交。这一实际结果明确了修复能够保证的边界。

验证: 在持锁 6 秒、预算 20 秒、约第 2 秒执行 KILL 的场景中,Linux 旧构建等到锁释放后返回错误 1317,且表已删除;候选构建在 22.494 毫秒内、锁释放之前返回错误 1317,并保留全部对象和数据。Windows 候选构建约在 47 毫秒内返回,且同样保留原表。新增 Windows 验证还确认了有序 CREATE 等待在取消后 16 毫秒内结束、原连接可继续使用,以及 KILL CONNECTION 对应的内核错误 -5066、回滚返回值 0 和独立快照不变。Linux 提交准入后的取消结果仍作为契约边界保留。

影响平台与版本: Linux 和匹配的 Windows 七项修复组合均已实测,八份产品文件与原补丁的对应关系已经核对。精确提交边界的状态验证在 Linux 完成,不能视为 Windows 也完成了相同验证。准确的缺陷引入点尚未确认。具体运行基线和构建对应关系见下方记录。

遗留与验证边界: 当前 master 分支上的修复提交尚未完成构建和运行验证。原七项修复组合在混合负载中的首轮和重试仍然失败,clog 错误 -6325 的根因尚未查清。Windows 的精确提交准入后状态、特定池连接的身份复用,以及全部物理补偿资源的清理尚未完整验证。未经过并行 DDL 辅助执行路径的旧式 DDL,也不能视为本修复已经覆盖。

改动概览

当前分支提交为 268ec176660eda5675c0f793aa7a544a48be00e8。最近一次提交仅删除非必要注释,没有修改可执行代码;既有运行结果仍对应下方列出的历史验证构建。

旧修复提交 36899ebe6e831fa0aa9fa699fdfb609dca41e498 已无冲突迁移为 688c50e2601eb83a4710af0bc9f32c538ec22648。该修复提交直接基于 master 00f88ac89b472e4979c37c22036dc2cecdc25e2e;随后新增的提交仅清理注释。git patch-id --stable 两侧均为 abb7e5c646074d6ee8ecded745645dda8b9c7f53,git diff --check 通过。补丁等价检查只能确认两次修复的差异相同,不能证明其在新 master 环境中的运行行为也相同。旧提交映射和 Git bundle 已保存。

  • src/observer/ob_inner_sql_connection.cpp
  • src/query/api/query/session/ob_command_cancellation.h
  • src/query/query_build_defs.bzl
  • src/rootserver/ob_ddl_service.cpp
  • src/rootserver/parallel_ddl/ob_ddl_helper.cpp
  • src/share/schema/ob_ddl_trans_controller.cpp
  • src/share/schema/ob_ddl_trans_controller.h
  • src/sql/session/ob_sql_session_mgr.cpp

既有运行验证使用旧 win 基线 a9a24a3113e52e5c2be90f49ca7af8d0255b0b41 或相应的组合构建。具体构建 SHA、二进制身份、必要适配及前后对照范围见折叠记录。迁移到 master 时已完成静态检查和补丁校验,但新基线的运行验证仍待补充。

合入理由

真实锁场景中,旧构建在取消后仍删表,而候选构建在提交准入前取消时能够回滚并保留对象,这一对照支持本次修复。合入前仍需评估提交边界、池连接身份复用和物理补偿资源的验证范围,并补充当前 master 基线验证。原混合负载失败及未查清的 clog 错误继续保留,不能因此宣称全部 DDL 问题已经解决。

当前 PR 为 Ready for review,目标分支为 master。是否合入仍需结合实际审核、新基线验证和上述风险判断。

Related links

展开详细复现与验证记录

真实6秒行锁、20秒预算、约2秒KILL:Linux旧包等到放锁后1317且删表,候选22.494ms放锁前1317且对象/数据完整;Windows候选约47ms并保表。新增Windows有序CREATE等待取消16ms、原连接复用、KILL CONNECTION内核-5066/rollback ret0及独立快照相同已核。Linux精确准入后KILL仍可1317且已提交,作为契约边界保留。

上述为既有实测,本次master迁移新增检查限于补丁等价、提交/文件范围、空白错误及身份。旧运行制品与当前master提交存在基线差异,未将既有PASS标记为新head实测PASS。基线验证报告中的完整SHA和组合关系保留在下方。

展开迁移前的真实验证结果与失败记录

2026-11-16 新增同一七修复 Windows 组合的有序等待及连接取消:有序第二 CREATE 在 KILL 后约 16 ms、放锁前返回 1317,第一 CREATE 放锁后成功,原第二客户端可继续 CREATE/DROP/SELECT 1。KILL CONNECTION 的客户端 2013 保留为原 OBSERVED;另核服务端同 trace 的元数据锁查询 -5066、内部事务 rollback tx(ret=0) 和 parallel DROP 中断均发生在放锁前,独立四项对象/DDL/数据快照相同。客户端断连时间不当作服务端回滚耗时上界。首个连接探针在 DROP 前因标识符过长返回 1059,保留后仅缩短唯一名称补跑,不算产品失败或混合重试。本轮 Windows 原始结果、21 运行文件/PDB/27 源码及精确清理均已独立核收。

真实产品进程均为 2 CPU / 4 GiB,原6秒元数据行锁、20秒DDL预算、约2秒发送KILL,未修改原预算或断言:

  • Linux:对照包KILL后仍等待4.009秒并删表;候选22.494毫秒、放锁前返回1317,持锁者退出后独立读回原表、LOB对象、建表定义和完整数据均不变。正常6秒等待、不存在目标CREATE、末端schema-version锁、同客户端复用及KILL CONNECTION后的独立对象保留均有实际结果。Linux SIGUSR1正常退出。
  • Windows:新七修复组合 a76feb06 / EXE 784e46e8、PDB/GUID匹配、全21运行文件校验,八份本修复源码与Linux已跑版本一致。旧六修复包KILL后仍等待4.031秒、1317且删表;候选约47毫秒、放锁前1317且原对象/数据完全不变。同包不取消6.078秒后成功,原5秒预算返回4012。末端锁约16毫秒、过程 INSERT; COMMIT; DROP 约31毫秒且已提交标记保留、同客户端取消后CREATE/INSERT/SELECT读回(1,42)/DROP均完成。
  • Windows五次运行收齐80次健康读、原1000秒参数独立恢复、独立查询确认测试库不存在、精确进程和端口清理;退出为TerminateProcess/native1,不是正常SIGUSR1。独立源码、入口与原始结果审核已完成,非合入批准。
  • Linux原生提交边界(状态验证):绑定同一次调用的来源会话和真实事务,在准入前发真实KILL,析构时为CANCELLED/-5065且原表完整保留;准入返回0/COMMITTING后发KILL,最终1317但DROP已提交。两侧正确作用域析构后TLS均为空。after唯一R1完整driver/controller/GDB退出0;初次before/after及before R1共三次诊断driver1分别保留(断点条件、TLS类型、同步MI收尾),controller均正常收齐。before R1业务状态齐,但不是无异常整轮通过。调试停顿不作性能证据。
  • 保留失败:Linux首个5秒预算项返回1205而非原4012,原表保留,唯一同输入R1为4012;Windows旧包对照同预算也返回1205,未重试或归因新修复。既有错误分类源码未改。Linux有序等待首次夹具重复close失败及只修关闭动作的R1分别保留;两次过程体前的系统包等待不计内部DDL覆盖。

匹配七修复Windows原WIN09完整混合入口:FAIL。 同一 a76feb06/784e46e8 包、6CPU/30GiB、原JAR及16类39线程,初始化464.647秒后实际启动全部工作线程;正式阶段约98秒、进度30/100时,2条独立clog异常和1条Timeout触发原transient≥3规则,未完成原300秒。首错已匹配服务端同连接/事务/trace的 -6325,尚不能据此确认物理磁盘根因。转发relay error与2次peer reset保留;失败后健康检查SSH连接超时,健康/数据一致性未证。原生PID/端口、四SSH及采集命令已核清,退出仍为受控TerminateProcess。此结果不覆盖旧四修复70/100/32clog失败,也不能按不同随机运行的进度差异归因性能回退。

正式Windows构建成功。Linux验证组合还含既有线程池、冷包、历史时钟修复及构建适配;Windows组合含此前六项修复。本分支仅八个必要产品文件,无测试或验证脚本。

展开实际复现前置与验证顺序

在独占2CPU/4GiB真实实例,创建带主键及LONGTEXT的一行表,关闭该连接recyclebin。设置DDL预算20s,持锁连接BEGIN后执行下面真实core查询,取得锁后保持6秒并ROLLBACK。第二连接DROP该表,约2秒后第三连接KILL QUERY目标连接号,最后由新连接读回主表/辅助对象、SHOW CREATE及完整标记行。对照包1317后表已删除;候选准入前取消时放锁前1317且所有快照相同。

SELECT row_id,column_name,column_value
FROM oceanbase.__all_core_table
WHERE table_name='__all_global_stat'
ORDER BY row_id,column_name FOR UPDATE;

结束恢复原DDL预算、清理唯一schema,核健康、进程与端口;参数/原始SQL/trace及清理收据均由既有报告引用。提交准入后的取消需要原生状态观测,不能由此SQL前置直接宣称覆盖。

历史风险与证据范围

展开既有风险、证据链接及历史验证范围

评审重点包括原混合稳定性仍失败、特定内部池连接身份复用、物理补偿资源和 Windows 精确提交准入后边界。准入前取消和连接取消已有匹配实测,准入后的取消仍可能发生在提交决定之后。

已提交正式评审。尚缺原混合稳定性完整通过、特定内部池连接身份复用及全部物理补偿资源证明;Linux原生准入边界的观测不替代Windows精确边界验证;不能把窄场景通过写成全局取消保证。Windows 的 KILL CONNECTION 和有序等待扩展已实际执行并独立核收;精确提交准入后的 Windows 边界仍未执行。

旧式未进入并行helper的DDL不获得独立取消作用域;保留的外部事务接口未来需要真实外层事务持有作用域至结束。无持久化格式、协议或升级步骤变化。根因、引用生命周期及初始验证边界见提交说明,Windows新增结果以上述实际验证为准。

发布说明

修复并行DDL在收到取消后仍等待内部行锁并提交的问题;提交准入后的取消仍可能晚于事务提交决定。

@github-actions github-actions Bot added the community Contribution from community. label Nov 13, 2026
@willlling
willlling marked this pull request as ready for review November 16, 2026 03:23
The originating user session and synchronous inner SQL session have separate
cancellation states. KILL QUERY publishes QUERY_KILLED on the former, while
parallel DDL can keep retrying an inner row lock and eventually commit. The
outer request then reports 1317 despite an independently observed DROP. The
separation exists in the initial repository import 2c524b5; available history
does not establish an introducing upstream PR. This is shared production code,
not a Windows-only conditional path; runtime evidence here is Linux x86_64.

A real 6-second __all_core_table global-stat row lock, 20-second DDL budget and
KILL QUERY after 2 seconds reproduce this on the matched control: DROP returns
1317 after 6.012058 seconds (4.008657 seconds after KILL) and the table and LOB
metadata are gone. A GDB snapshot confirms the outer QUERY_KILLED state while
the inner SQL retries -6005 in select_ddl_epoch_for_update at precommit. The
all-stop diagnostic affects latency and is used only as state evidence.

Give the actual ObDDLHelper transaction a synchronous CommandCancellationScope.
Its session-manager reference pins the originating user session; nested lexical
scopes borrow that reference. Read the existing authoritative termination state
under the same thread-data lock used by KILL. Check it before inner SQL attempts
and inside the existing bounded DDL readiness wait. No session pointer is
published cross-thread, no interrupt registry delivery or new polling worker is
introduced, and no timeout, retry budget or session-wide state is cleared.

At the end of all precommit SQL, admit only the matching transaction under the
source session lock. Cancellation published before admission prevents commit;
admission first moves that scope to COMMITTING. A later KILL can still be too
late, and the outer response may be 1317 despite committed effects. The change
does not promise universal 1317-implies-rollback semantics. Mandatory base end,
rollback and failed-DDL compensation mask only this cancellation capability;
existing resource, timeout and transaction checks remain active. Destruction
restores the previous TLS scope before releasing the session reference. No
state survives on pooled inner sessions/connections. Current production helpers
own their transactions; the retained external-transaction interface would need
its real enclosing owner to maintain a scope through its final end.

Validation used a frozen Linux ELF with the exact eight product-source hashes,
plus previously validated pool/cold/history fixes and existing Linux build
adapters. The public branch contains only these eight product files. Formal
build succeeded. Original 6-second DROP/KILL now returns 1317 before lock release
in 22.494 ms after KILL, with identical table IDs, LOB auxiliaries, CREATE text
and payload after the holder exits. Normal uncancelled 6-second DROP succeeds.
The first 5-second budget run returned 1205 at 4.998547 seconds instead of the
original 4012 assertion: retain that FAIL. Native logs show transaction expiry
9.394 ms before the inner query deadline and existing timeout classification;
that classification code is unchanged. One same-input R1 returns 4012 at
5.005806 seconds, preserving the table; no assertion/error normalization changed.

Additional real runs cover late normal_schema_version precommit lock, absent
CREATE cancellation, same-client CREATE/INSERT/SELECT/DROP after interruption,
KILL CONNECTION with later independent metadata retention, and ordered DDL
registration/cancellation before its predecessor finishes. The first ordered
fixture completed product assertions but failed duplicate client close; the
fixed-close-only R1 passes and the original fixture failure is retained. Two
cold nested calls stopped before the body and are not nested-DDL coverage. A
separate explicit read-only package-ready preparation then runs a real procedure
INSERT; COMMIT; DROP under the held lock: 1317 in 6.615 ms after KILL, original
table unchanged and committed marker retained. Owned Linux instances complete
normal SIGUSR1 exit and log/health collection. Static and result reviews are
recorded separately, without merge approval.

Still unverified: deterministic cancellation strictly after final admission,
specific pooled-connection identity reuse, every compensation/storage resource,
matching Windows artifact/build and original mixed workload. Legacy DDL paths
without the parallel helper do not gain an independent owner scope. Keep the
change Draft pending these acceptance boundaries; no protocol or persisted
format changes, no product tests/scripts included.

Master-target delivery

Retarget this existing fix PR to the fetched master 00f88ac.
The original fix commit was 36899eb on the win baseline.
Cherry-pick applies without conflicts and retains the same stable patch-id.
The seven PR branches each contain their own product fix only.
Existing Linux and Windows runtime evidence belongs to the original
win-based artifacts described above. No runtime verification of the
new master-based commit is claimed by this branch migration.
@willlling
willlling force-pushed the fix/seek-1414-ddl-cancel branch from 36899eb to 688c50e Compare November 16, 2026 03:42
@willlling
willlling changed the base branch from windows-mysqltest-fix to master November 16, 2026 03:43
Remove 3 full-line explanatory comments that repeat the adjacent code
or the public API contract. Keep required copyright, lifecycle, lock-order,
cancellation/rollback and workflow security documentation where present.

Only complete standalone comment lines are deleted. No statements, strings,
preprocessor directives, workflow keys or executable script are changed.
Verified the diff contains only these deletions, no added lines and no line
continuations; git diff --check passes. Existing functional evidence belongs
to the previous product tree; this cleanup does not claim a new runtime test.

This branch has not been deployed

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

Labels

community Contribution from community.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant