Skip to content

[SEEK-1414] Preserve initial stats gather after cold package schema refresh - #2635

Open
willlling wants to merge 2 commits into
masterfrom
fix/seek-1414-cold-stats-retry
Open

willlling wants to merge 2 commits into
masterfrom
fix/seek-1414-cold-stats-retry

Conversation

@willlling

@willlling willlling commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

修复冷启动等待系统包后,首次直接调用统计过程却被误判为重复执行而返回错误 5935 的问题,同时保留对已经执行过或来源不明的重试的保护。

背景: 在全新数据目录、2 CPU / 4 GiB、没有预先加载系统包的环境中,用户在默认 10 秒预算内首次调用 DBMS_STATS.GATHER_SCHEMA_STATS,就可能遇到这一错误。

根因: ObCallProcedureResolver::resolve() 等待系统包加载时,可能在解析过程名和实参之前返回 OB_SCHEMA_EAGAIN。通用重试逻辑会因此将会话标记为 IN_RETRY,即当前请求正在重试。但是,gather_schema_stats() 只看到这一标志,无法区分“过程尚未执行,只是在等待系统包”和“过程已经执行后再次重试”,于是拒绝了第一次真正进入统计过程的调用,并返回错误 5935。此外,拒绝之前仍会发生既有的隐式提交,因此收到错误也不能说明之前的事务没有提交。

修复与取舍: 修复后的 ObSqlCtx 会明确记录本次重试是否来自过程执行前的系统包加载等待,ObQueryRetryInfo 则分别累计全部重试次数和这类包加载重试次数。只有两个计数相等且大于零,并且真实 PL 调用栈表明当前是顶层直接 CALL 时,代码才允许进入统计过程。这样可以让尚未执行过的首次调用继续,又不会放开已经执行过或来源不明的重试。只要发生一次其他来源的重试,两个计数就会不同,后续包加载重试也不能将其覆盖。执行前来源标志会在每次尝试时重置,重试计数则会在请求边界重置;嵌套调用、预处理、动态 SQL 和多语句请求不适用这一例外。

验证: Linux 在没有预热的首次 CALL 中,基线耗时 4.267 秒后返回错误 5935,候选构建耗时 4.204 秒后成功;两张表各新增一条统计历史,记录的行数分别为 2 和 0。Windows 旧组合构建在 4.844 秒后返回错误 5935,新组合构建在 4.907 秒后成功。嵌套调用仍被拒绝并返回错误 5935,且既有隐式提交产生的标记数据仍然保留。100000 微秒的短预算和包等待后执行 KILL 的场景均未新增统计历史。

影响平台与版本: Linux 的旧 win 基线和 Windows 组合候选均实测覆盖了冷启动系统包场景。相关 SQL 入口共用源码,但其他发布版本没有逐一实测,准确的缺陷引入提交和 PR 也尚未确认。具体运行基线和构建对应关系见下方记录。

遗留与验证边界: 当前 master 分支上的修复提交尚未完成构建和运行验证,原完整 Windows 场景也尚未完成验收。对于先发生未知来源重试、再遇到包加载重试,以及执行后故障、预处理和动态 SQL 等情况,目前完成的是源码审查,不能记为故障注入验证通过。每张表只新增一条历史记录,也不能单独证明所有原生入口都只执行了一次。

改动概览

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

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

  • src/observer/mysql/obmp_query.cpp
  • src/pl/sys_package/ob_dbms_stats.cpp
  • src/sql/ob_query_retry_ctrl.cpp
  • src/sql/ob_sql_context.cpp
  • src/sql/ob_sql_context.h
  • src/sql/resolver/cmd/ob_call_procedure_resolver.cpp

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

合入理由

冷启动首次调用的前后对照,以及嵌套调用保护、短预算和取消场景,支持本补丁只对明确的执行前包加载重试放行。合入前仍需结合当前 master 基线、完整 Windows 场景和未实跑的重试边界进行判断,不能将源码审查结论替代运行验证。

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

Related links

展开详细复现与验证记录

Linux无预热首次CALL基线4.267秒5935,候选4.204秒成功,两表历史各新增1条/行数2和0;Windows旧组合4.844秒5935,新组合4.907秒成功。嵌套仍5935且已提交marker保留,100000微秒预算超时和包等待后KILL均保持无统计历史新增。

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

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

Linux x86_64,正式 build.py 构建通过。验证包组合了 #2633 的线程池修复和本 PR 完全相同的六文件补丁,另有既存构建适配;本 PR 本身只含六个产品文件。组合避免独立的启动死锁,未将线程池改动重复纳入本 PR。

  • 同输入、无预热首次冷 CALL:基线 4.267 秒后 5935;候选 4.204 秒成功,每表历史各增加一条,独立读回行数分别为 2/0。原单连接、两表统计探针的三轮 CALL 也完成。
  • 外层先 INSERT、再嵌套调用统计:基线和候选均保留已提交行、返回 5935、历史增量为零。原生采样确认直接调用 total/safe=1/1、栈深 1、top_call=true;嵌套为 1/1、栈深 2、top_call=false。调试器暂停样本不作为耗时证据。
  • 无调试器控制:100000 微秒预算约 101 毫秒返回 4012并恢复原值;确认等待包后 KILL QUERY,约 103 毫秒返回 1317;均无统计历史写入。
  • 选定 11 次基线/候选运行均完成健康检查、独占 schema 清理及 Linux SIGUSR1 退出 0,日志读取线程已收齐且无记录丢失。独立源码、生命周期及证据审查未发现可行动 Blocker/Major;这不代表合入批准。

Windows x86_64 匹配六修复组合 f33ae2df / EXE SHA256 247d0ef5 已完成同一份真实探针:旧四修复包首次冷 CALL 4.844 秒返回 5935;候选 4.907 秒成功,两表历史各新增 1 条,row_cnt 独立读回 2/0。每个场景均为全新实例、无包预热,日志确认测量 CALL 实际等待系统包。

嵌套场景 4.859 秒仍返回 5935,已提交 marker 保留且历史增量 0;100000 微秒预算 0.110 秒返回 4012 并恢复原值;观察到真实包等待后 KILL QUERY,0.110 秒返回 1317、历史增量 0。全部原断言和预算保留,健康、schema 清理和 PID/端口收据均核收,独立证据审核未发现新增 Blocker/Major。Windows 使用 TerminateProcess/退出1,未验证正常 SIGUSR1。完整原 Windows 场景仍待验收,提交正式评审,相关验收边界保留。

展开冷启动实际输入

在2CPU/4GiB、全新数据目录、无系统包预热的实例,保持默认10秒查询预算,执行下列等价SQL。需日志确认首次CALL确实等待系统包。分别在基线和匹配候选的新实例执行,独立读回统计历史增量、行数和marker,保留嵌套5935、100000微秒预算和包等待后KILL控制。

CREATE DATABASE cold_stats;
CREATE TABLE cold_stats.t(id INT PRIMARY KEY, v INT);
INSERT INTO cold_stats.t VALUES(1,11),(2,22);
CREATE TABLE cold_stats.marker(id INT PRIMARY KEY);
CALL DBMS_STATS.GATHER_SCHEMA_STATS('cold_stats',degree=>1,no_invalidate=>FALSE,cascade=>TRUE);

历史风险与证据范围

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

评审重点包括原完整 Windows 场景的验收,以及仅完成源码检查的未知重试、执行后故障、预处理和动态 SQL 路径。已通过的冷调用、嵌套保护、短预算和取消结果保持各自实测范围。

影响平台与版本: Linux 实测基于 win a9a24a311 的原 SQL/统计代码及匹配组合候选;源码入口为平台共用,Windows 已完成上述匹配组合包的窄验证,仍未完成原完整场景验收,提交正式评审,相关验收边界保留。实际制品 SHA 和完整工程分析见本 PR commit message。

未知重试再遇包加载重试、执行后队列故障、预处理/动态 SQL 等边界完成源码审查,未计作故障注入实测通过。统计历史增量一条不等于证明 native 入口只执行一次。原 WIN08 的 1062 历史键冲突由独立 #2637 处理,其原完整场景、WIN09 SQL 取消副作用及 Windows 整体验收不以本结果结清。

不改变协议、存储格式或参数预算;使用现有 reserved bit,session 重试信息增加一个内部计数。产品提交无测试、脚本或构建适配。

发布说明

修复冷启动等待系统包后,首次直接收集 schema 统计被错误拒绝的问题。

@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
…retries

A direct first GATHER_SCHEMA_STATS during startup waits in
ObCallProcedureResolver::resolve for asynchronous system packages. The helper
returns OB_SCHEMA_EAGAIN to reacquire the schema guard before even resolving
the procedure name or its arguments. The generic retry controller nevertheless
sets session IN_RETRY. The first native stats entry then returns error 5935
although it has not yet backed up or gathered statistics. The existing implicit
commit before that rejection is real and is not changed by this fix.

Track that precise pre-resolution origin in the existing statement SqlCtx.
Each MpQuery attempt clears it, and only the CALL package-ready EAGAIN boundary
can set it. Exclude inner, PL, nested, prepared/dynamic, text-PS and multi/batched
contexts. The retry controller increments total and marked counts together for
this origin; every unknown retry increments total only. Equality of initialized,
nonzero counts therefore cannot be restored by a later marked retry after an
unknown retry. The existing request owner/reset clears both counts, whereas
per-attempt clear retains the cumulative history. No session retry flag is
cleared, no context ownership is replaced, and borrowed inner sessions do not
receive an invented save/restore that could erase unknown execution.

GATHER_SCHEMA_STATS alone may enter its existing body when that cumulative proof
holds and the actual PL stack has exactly one current top-call state. A nested
routine can reuse the outer SqlCtx without incrementing session nested_count;
the real PL stack is therefore necessary. Other stats retry guards, implicit
commit ordering, schema refresh, deadlines and cancellation remain in place.
No new thread, queue, global state, protocol field or persistence format is used.
The SqlCtx flag consumes an existing reserved bit. RetryInfo adds one int64 count
owned and accessed by its existing session/request path.

Linux x86_64 validation used a frozen package e80a8d685a3f8858c55c50fc2c463648925769c8cb6436214f25aa7f5fa2816d,
composed from pool-fixed 76cb5d6
plus these exact six source hashes and the previously recorded build-only
adapters. The independent pool fix avoids a separately confirmed bootstrap
shutdown deadlock; it is not included in this six-file product commit.
The baseline pool package 4a5d65597c1002b01cba2d01140eee8aae0f2d07dcca6de69ad0e4d71c56a95c
contains identical unmodified stats/SQL source from win a9a24a3.
Formal build.py build passed with six jobs, 32GiB scheduler budget, private
verified caches and Linux sandbox intact (1156.380 seconds).

Same real fresh-directory, no-package-warmup input: baseline first direct CALL
returned 5935 after 4.267 seconds; candidate completed in 4.204 seconds with
one history-row increment per table and exact independently read row counts
2 and 0. This history result is not claimed as a native-entry-once proof.
The original single-worker/two-table statistics probe completed all three
CALL waves on another fresh candidate instance with unchanged parameters.
Its absence of 1062 within this bounded input does not establish a 1062 fix.

A real cold outer procedure inserted a marker before nested gather. Both
baseline and candidate preserved that committed row, returned 5935 and added
zero stats-history rows; candidate elapsed 4.059 seconds. GDB native-entry
inspection observed candidate total=marked=1 and stack-count=1/top_call=true
for direct CALL, versus total=marked=1 and stack-count=2/top_call=false for
nested CALL. Debugger runs detached normally; their paused durations are
excluded from latency conclusions.

Uninstrumented package-wait controls kept the existing mechanism: a separately
recorded 100000us session budget returned 4012 in 0.101 seconds and was restored
to its original 10000000us; KILL QUERY after observing the actual package wait
returned 1317 in 0.103 seconds. Neither added history. All selected eleven
baseline/candidate runs retained health/owned-schema cleanup and native
SIGUSR1 exit zero with joined log readers and no recorded capture loss.
Independent source/owner/native-evidence review found no actionable
Blocker/Major in the final product diff; this is not a merge approval. Product-only diff: six existing C++ files; no tests, scripts, logs,
build adapters or local validation helpers are part of this commit.

Known limits: Linux reproduction and matching validation do not establish a
Windows pass. Unknown-then-marked history and execution-error packet/queue
fault paths are source-audited, not fault-injected product runs. The existing
WIN08 duplicate history-key 1062 and WIN09 SQL cancellation side effects are
separate unresolved problems. This change does not claim to repair them.

Master-target delivery

Retarget this existing fix PR to the fetched master 00f88ac.
The original fix commit was 51015e6 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-cold-stats-retry branch from 51015e6 to 6b62853 Compare November 16, 2026 03:42
@willlling
willlling changed the base branch from windows-mysqltest-fix to master November 16, 2026 03:43
Remove 4 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.
footka added a commit that referenced this pull request Nov 20, 2026
…2717)

### 任务描述

SEEK-1414,与 #2635 处理同一个冷启动问题。全新数据目录首次连上主库后立即执行下面的 SQL,原版可能在执行阶段返回 5935。

```sql
CALL DBMS_STATS.GATHER_SCHEMA_STATS('test', degree => 1);
```

### 根因

原版每 3 秒运行一次建包任务。首次回调创建管理任务,后续回调才执行建包。直接 CALL 的解析器会等待全局内存标记,等到包就绪后返回
OB_SCHEMA_EAGAIN。SQL 重试将请求标为 IN_RETRY,导致 DBMS_STATS 在第一次实际进入统计过程时就拒绝执行。

### 解决方案

主库首次部署和已有目录重启走同一个元数据就绪入口。schema 和时区检查成功后,只提交异步任务,建包与 SQL
服务启动可以并行。只读备库靠日志回放获得包。在线备升主沿用本地日志服务的激活回调。

删除配置项 `_enable_async_load_sys_package` 和同步等待接口。普通系统包统一异步安装;bootstrap
原有的特殊系统包加载顺序保持不变。建包 timer 首次立即执行,在同一回调创建并执行管理任务,安装失败时按 3
秒间隔重试,成功后取消。重启复用已有管理任务记录。

删除 SQL 解析器的全局就绪等待。包不存在或只有声明时,CALL 和包函数返回原有解析错误,稍后可以重新发起请求。

### 验证

Linux,2 CPU、4 GiB。使用本机依赖缓存,以 Bazel `-j32` 构建完整 seekdb,启动日志版本为本 PR head
3798ce9。git diff --check 通过。本轮使用两个独立实例实际执行 SQL,未运行整套 mysqltest。

| 场景 | 原版 1e45cf4 | 本 PR 3798ce9 |
| --- | --- | --- |
| 全新目录,SELECT 1 成功即 CALL | 首次 SELECT 1 用时 2.64 秒,首次 CALL 返回 5935 | 首次
SELECT 1 用时 2.44 秒,首次 CALL 早于建包,返回原生 1049,26 条包声明和包体全部可见后再次 CALL 成功 |
| 已有目录重启 | 未重复安装 | 26 条记录可见,CALL 成功;日志显示安装计数未增加,timer 完成检查后退出 |
| 只读备库 | 通过回放获得 26 条记录 | 通过回放获得 26 条记录,未启动建包 |
| 在线升主 | 首次 CALL 报 5935,后台重新安装 13 个包 | 切主后首次 CALL 成功,后台重新安装 13 个包后取消
timer |

同一用例在原版和候选上均观察到新主库本地 LOAD_MYSQL_SYS_PACKAGE 任务计数为
0,即使包目录已回放完整,后台也会再执行一次安装。候选另行核对了已删除配置在参数虚表中的行数为 0。表内启动耗时为单轮观测。

此前以 1e45cf4 为原版基线,Linux 2 CPU、4 GiB、新数据目录,SELECT 1 首次成功后执行上述 CALL,5
轮均在 4.37–4.57 秒后报 5935。此前在沿用相同 SQL 解析路径的草稿 6a52bba 上,注入一次中断并确认目录中只有
DBMS_STATS 声明。CALL 和包函数均报 5559;补齐包体后 CALL 成功,函数返回 31。

### 影响范围

首次部署、重启和角色切换时普通系统包的安装,以及包相关 SQL 的名称解析。删除隐藏参数后,相应更新参数目录的 mysqltest
期望列表。Windows 和升主中断后的恢复未实测。

### 合入理由

普通包的安装离开对外服务的关键路径。已可见的包依照 schema 直接解析,提前到达的请求沿用已有错误码,避免原有等待制造的 5935。

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