Skip to content

[Bug]: IVF/HNSW enable APIs set flags on one pooled connection only #31398

Closed

Description

@Ariznawlll

DRAFT 20 (SDK, 2026-10-27) — to be filed

Title: [Bug]: IVFConfig/HNSWConfig enable-flags run SET on one pooled connection only — "enabled" is per-connection and lost on recycle

Description

IVFConfig.enable_ivf_indexing()/disable/.../configure_ivf, HNSWConfig.enable_hnsw_indexing()... (sqlalchemy_ext/ivf_config.py, hnsw_config.py) implement the documented "enable X indexing" APIs as:

with self.engine.begin() as conn:
    _exec_sql_safe(conn, "SET experimental_ivf_index = 1")   # session-scope SET

SET ... in MatrixOne/MySQL semantics is a session variable — but engine.begin() checks out one pooled connection, sets the flag, and returns it to the pool. So:

  1. Only one pool connection carries the flag. With pool_size 5 + overflow (the SDK's own defaults: pool_size=5, max_overflow=10), the other 4-14 connections never get the SET. The very next query may land on a different connection — experimental features (IVF/HNSW index usage) silently off, then on, intermittently per query.
  2. Recycle invalidates it entirely: pool_recycle=3600 (SDK default) or any pool_pre_ping invalidation replaces that connection — after recycle no connection has the flag, while configure_ivf() long returned True and get_ivf_status() (which runs SHOW VARIABLES on yet another fresh connection!) reports... experimental_ivf_index = off. The status API and the enable API talk to different connections, so they can disagree within the same second.
  3. vector_manager._build_setup_sql(["SET experimental_ivf_index=1", "SET probe_limit=N"]) has the same shape — its SETUP statements only bind to whichever pooled connection the caller later does NOT guarantee (depends on the executor path taking the same connection).

Correct approaches exist in the SDK itself: ConnectionHook (connection_hooks.py) exists precisely to run SET-on-connect for every pooled connection — the IVF/HNSWConfig helpers bypass it instead of composing with it. (ConnectionHook has its own id()-set bug filed today — #31396 — but its architecture is the right one.)

Secondary defect in the same module: _exec_sql_safe does sql.replace('%', '%%') only on the exec_driver_sql branch, not on the text() branch — two execution paths with different escaping for identical input; a LIKE containing % round-trips differently depending on which driver-connection facade a caller's SQLAlchemy version provides. And set_probe_limit interpolates limit unvalidated into SET (minor, but should be int()-asserted).

Reproduction

Conceptual (needs server, no assertion dependency):

cfg = IVFConfig(engine)
assert cfg.enable_ivf_indexing()           # True — flagged ONE pooled connection
cfg.get_ivf_status()                        # runs on ANOTHER connection → often reports ivf_enabled=False
# and a vector search executed right after may or may not use the IVF index
# depending on which pooled connection the driver picks — flaky by construction

Impact

  • "Enable IVF/HNSW" is the primary documented step for every vector-index workflow; with default pools it is ineffective for 80%+ connections immediately and 100% ineffective after one recycle. Symptoms are the worst kind: intermittent index usage — slow queries sometimes, EXPLAIN plans that flip, search-quality variance.
  • get_ivf_status() contradicting a successful enable_*() confuses every diagnosing engineer ("I enabled it!").
  • The module overlaps with (and contradicts) the ConnectionHook mechanism; two competing enable paths with different behavior semantics confuse users about which to call.

Expected behavior

  1. Enable/disable APIs should schedule the flag on every current and future pool connection — either reuse ConnectionHook (attach an action for the flag) or execute SET via an event-listener on connect; or store the desired state and have the pool's do_connect callback apply it.
  2. get_ivf_status() should read from the same connection the SET ran on (or aggregate all pooled connections), not a random one.
  3. Unify the %-escaping between the two _exec_sql_safe branches (escape neither, or escape both — pick per actual driver semantics).
  4. Regression: enable → exhaust all pool connections → assert each received the SET (stub executor or event spy); after engine.dispose(), re-enable and re-assert.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。补充真实 MatrixOne 连接池验证:IVFConfig 返回启用成功,但另一条已借出的物理连接仍为关闭;连接池 dispose 后新连接也恢复关闭。本轮补充原问题,不新提重复 issue。

    关键证据

    • 版本:官方 main@c6683bd2f531dcaa80e153c0924dac619c49dc9d;SDK 与隔离本地服务构建一致,Python 3.14 / SQLAlchemy 2.1.6;3 个新建库验证,每库包含真实 VECF32(2) 数据、NULL 行和已创建的 IVFFLAT 索引。
    • 保持物理连接 A 借出且 experimental_ivf_index=0,调用同引擎 IVFConfig.enable_ivf_indexing() 返回 True;在 A 上直接重读仍为 0,另借物理连接 B 为 1。各轮均记录 CONNECTION_ID 证明是两条不同连接。
    • 在 A 上直接执行 SET 后立即读取为 1,说明变量本身可用;归还连接后 engine.dispose(),再次 get_ivf_status() 返回 ivf_enabled=False、无错误。
    • 三轮完整重读向量数据均保持 (1,[0,0]),(2,[1,1]),(3,NULL),同连接 SELECT 1 正常。源码 IVFConfig.enable_ivf_indexing 临时借用一条连接 SET 后归还,与实测一致。

    待解决点

    • 本轮证明的是配置生命周期,不把连接池默认选择写成“80% 查询未用索引”等未经测量的比例,也未据此宣称实际查询计划或召回率受损。需明确 API 是单连接配置还是整个客户端连接池配置,并覆盖新增/回收连接;HNSW 与实际查询计划仍需独立验证。
  5. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。HNSW 也已补充真实连接池与建索引验证:无连接参数的启用调用返回 True,另一条已借出的连接仍因开关未启用而无法建索引;明确传入该连接后创建正常。补充原 issue,不另提重复问题。

    关键证据

    • 版本:官方 main@c6683bd2f531dcaa80e153c0924dac619c49dc9d,SDK 与本地隔离服务构建一致,Python 3.14 / SQLAlchemy 2.1.6;3 组独立库复现。
    • 连接 A 显式设置 experimental_hnsw_index=0 并保持借出;同引擎 HNSWConfig.enable_hnsw_indexing() 返回 True。A 执行 CREATE INDEX ix_setup USING hnsw ON hnsw_setup_control(v) op_type 'vector_l2_ops' 报 20101: experimental_hnsw_index is not enabled。SHOW INDEX 仍仅 PRIMARY,已有两行向量未变。
    • 调用 enable_hnsw_indexing(A) 后,在 A 上重试同一 DDL 成功,索引为 PRIMARY、ix_setup;完整数据未变且 SELECT 1 正常。另一个真实 1000 行表也验证了 A/B 开关分别为 0/1、dispose 后新连接状态为 False。
    • 需要纠正影响范围推断:本版已有 HNSW 索引的查询,在 A 开关为 0 与 B 为 1 时都生成 Vector Index Scan,普通 Top-10 均与无索引对照一致。因此本轮证明建索引配置不一致,不能据开关差异推断“某比例查询不走索引”。HNSWConfig 的无参分支另借连接;带参分支直接作用于指定连接。

    待解决点

    • 明确无参配置 API 的连接池合同,覆盖已有、并发借出和回收后的连接;保留显式连接控制。应把 DDL 开关与查询改写策略分开验收,不复用未经测量的查询比例或召回推断。本轮未新增仓库用例或 PR。
  6. added a commit that references this issue on Oct 30, 2026
    bbfe556
  7. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31774 (main bbfe55669a1440424914d45a283a1d0e36f722fa): please verify per-physical-connection IVF/HNSW pool settings, concurrent checked-out connections and reconnect. This isolated QA run inspected the issue and merged source but did not run tests or a live MatrixOne service at this SHA because resource headroom under the existing guard was insufficient. Earlier QA counterexamples and PR author-reported validation remain scoped to their recorded versions and are not results of this QA run.

  8. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。IVF/HNSW 设置在连接池重新借用和重建后生效,但 get_ivf_status() 仍把已启用状态报告为 False,与 issue 的状态一致性要求不符。

    关键证据

    • 官方 main@a54850983d9cddaacc97e38a84515ec724c937b7;修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 合并提交 bbfe55669a1440424914d45a283a1d0e36f722fa 为祖先。SDK 和隔离 1 CN / 1 TN / 1 LogService 二进制对应同一 SHA,Python 3.12、SQLAlchemy 2.0.43。
    • 禁用后借出连接 A,再启用 IVF/HNSW:已借出的 A 保持原状态;归还后 10 轮同时借出两条不同物理连接,两者均读取到 IVF/HNSW 开关为 1;dispose() 后新连接同为 1,禁用后为 0,3/3。使用 BIGINT 主键表和启用的 HNSW 连接实际创建索引成功,行数保持 3;% 在两种执行分支均原样返回,非法 probe_limit 被拒绝。MOTR #298 覆盖上述通过部分,定向 3/3。
    • 同一池启用 IVF 后,get_ivf_status() 返回 ivf_enabled=False 且 error=None;直接在池连接执行 SHOW VARIABLES LIKE 'experimental_ivf_index' 返回字符串 ON,同一连接 SELECT @@experimental_ivf_index 为 1,3/3。当前 ivf_config.py 用 ivf_setting[1] == '1' 判断,不能识别返回的 ON;这不是连接未启用。此前 HNSW 建索引错误经核实是测试表使用 INT 而非要求的 BIGINT 主键,已修正,不归为产品失败。

    待解决点

    • 状态 API 需要正确识别实际服务返回值,并覆盖开启、关闭及重连后的状态;未将错误 False 固化为成功 golden。未跑完整 MOTR suite 或多 CN;(aoe): fix api imcompatitable issue #298 尚未合入。
  9. XuPeng-SH commented on Oct 31, 2026

    @XuPeng-SH
    Contributor

    新修复转 QA:#31953 已合入 main,merge SHA 569d521cfae73ba80c42f58366c172ce4ef84ed7。

    旧连接池修复已在此前 QA 记录通过;本次修复剩余 ON/OFF 与 1/0 状态解码并拒绝未知表示。请验证启停、并发借还、dispose 重连后的 IVF/HNSW 状态一致性;不要把 DDL 开关等同于查询计划或召回率。

    验证证据:作者记录离线多 SQLAlchemy 矩阵、187 个 native 合同、19 个全文 SQL 检查;当前 head 独立 reviewer 记录 315 个 focused Python tests、11 个 SDK online cases 及三个 Go owner package 通过。上述均为 PR 作者/评审者报告,不是本轮独立本地实测。本轮读取 issue 完整评论、修复范围及远端合并状态,未运行服务或测试;未发现适用于新合并版本的已验证反例或未完成的开发 follow-up。转 @Ariznawlll 继续验证,不代表验收通过。完整 BVT、多 CN、长稳和生产环境验证不在本轮证据范围内。

  10. Ariznawlll commented on Nov 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。IVF/HNSW 开关在连接池借还及重建后保持一致,IVF 状态接口正确识别开启、关闭与未知值。

    关键证据

    • 版本和环境:官方 main@35202093259793e48dcd7abc8ff418f07190f746;修复 fix(python): restore SDK SQL and execution contracts #31953 的 merge 569d521cfae73ba80c42f58366c172ce4ef84ed7 在该 SHA 的祖先链。隔离本地单 CN / TN / LogService;服务 git_version()、二进制构建记录 SHA 一致,vcs.modified=false。Python 3.12,分别使用 SQLAlchemy 1.4.54 与 2.1.6。
    • Happy Path:分别禁用、启用 IVF/HNSW;启用前已借出连接保持原值,归还后的两条不同物理连接均读取到两个开关为 1。每轮另用两个线程同时持有不同连接并读回开关,重复 10 次;dispose() 重建连接池后开关仍为 1,get_ivf_status() 返回 ivf_enabled=True 且无错误。禁用后返回 False,HNSW 在启用的连接上真实建索引,源表保持 3 行。两个 SQLAlchemy 版本各独立重复 3 轮,全部通过。
    • Unhappy Path:probe_limit 为 0、负数、布尔值和非法字符串均抛 ValueError;未知 IVF 状态值在 SDK 定向测试中返回错误而非错误地报告已启用或关闭。SQLAlchemy 1.4.54 / 2.1.6 的状态 ON、off、1、0、bytes 和未知值定向用例各 3 轮通过;未知值由模拟结果构造,不宣称服务返回过该值。异常相关 SDK 测试共每轮 8 项,包含依赖诊断,不能全部归为本 issue。
    • 清理:每轮删除独立测试库,结束后查询 qa_pool_ 前缀测试库数量为 0。MOTR #298 已扩展连接池回归,两种 SQLAlchemy 版本经 MOTR runner 各 3/3;SDK 回归需要显式启用,默认跳过。
    • MOTR 离线 CI通过;它不运行默认跳过的 SDK 实服回归,实服结果来自上述本地测试。

    待解决点

    • 本次为单 CN、本地连接池设置及索引建表验证,未覆盖多 CN、查询计划、召回率或性能;这些不在本 issue 的连接池状态验收结论内。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions