Skip to content

fix(fulltext2): rebuild + refresh cache so COPY ALTER leaves the index queryable (#28837) - #28879

Merged
XuPeng-SH merged 11 commits into
matrixorigin:mainfrom
cpegeric:bug_28837
Sep 16, 2026
Merged

XuPeng-SH merged 11 commits into
matrixorigin:mainfrom
cpegeric:bug_28837

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #28837

What this PR does / why we need it:

Fixes #28837 — an unrelated COPY ALTER (e.g. ADD COLUMN) left an unaffected FULLTEXT2 index returning empty from
MATCH for minutes, until a manual reindex or a CN restart.

Root cause

Why the index is rebuilt from source, not copied

A fulltext2 index — like every async index — is populated from CDC, not
synchronously with the base-table DML: the index table always trails the base
table by however far the CDC consumer has progressed. At the instant ALTER TABLE runs, that CDC may not have caught up, so the index table is left
behind
— it does not reflect the base table's committed state. Cloning such an
index into the replacement table would therefore copy a stale, partial snapshot
(and the copy point has no clean watermark to resume CDC from consistently),
producing an index that silently omits whatever the CDC had not yet applied.
That is exactly why cloneUnaffectedIndexes marks these indexes SkipWholeIndex
and the replacement is rebuilt from source (REINDEX FORCE_SYNC) rather than
copied. It also means the issue's stated Expected behavior — that the COPY
ALTER carries the existing index over intact — is not valid for an async index:
there is no consistent index snapshot to carry over, so a rebuild from source is
the only correct outcome. The bug this PR fixes is not "the index wasn't copied"
(it correctly isn't) but that after skipping the copy, the replacement was never
rebuilt (no tag=0 base) and the stale warm cache kept serving empty.

Two independent causes, both required for the bug:

  1. Base-less rebuild. cloneUnaffectedIndexes marks fulltext2 SkipWholeIndex, so a COPY ALTER clones the table to a new
    id with empty index tables that are rebuilt from the CDC log. But RunFulltext2 appends only a tag=1 cdc_tail, and a
    fulltext2 tag=0 base is written only by buildFromSource — so the replacement index had no base and nothing to serve.

  2. Stale warm cache. The querying CN kept its doc-less warm cache across the CDC flush (the [Performance]: keep FULLTEXT2 cache read-only across CDC updates #28005 keep-warm
    optimization), serving false negatives until the ~10-minute IsStale sweep.

Fix (scoped to fulltext2)

  • New plugin hook AlterCopyInitSQL (pkg/indexplugin/compile/hooks.go): fulltext2 returns ALTER TABLE … ALTER REINDEX … FULLTEXT2 FORCE_SYNC, run post-commit by the CDC's first iteration to rebuild a real tag=0 base. Every other algorithm
    returns (false, "") because its consumer already rebuilds from the ts=0 replay (hnsw/cagra/ivfpq via RunHnsw/RunCuvs,
    ivfflat entries, classic row-based fulltext). Wired into alter.go's unaffected-index ISCP block, mirroring RestoreInitSQL —
    including its initSQL=="" ⇒ startFromNow=false guard so a no-rebuild path can never arm the tail from now and drop
    pre-existing rows.

  • New cache.RemoveIdle(key, reason) (pkg/vectorindex/cache/cache.go): evicts a cached index only when no search is in
    flight
    (wraps the existing evictIdleEntry/tryClaimIdle); a busy entry stays warm and refreshes at the next idle moment —
    no reader thrash, and [Performance]: keep FULLTEXT2 cache read-only across CDC updates #28005's keep-warm is left intact. RunFulltext2 calls it (via fulltext2.EvictIdleCache) after a
    non-empty tail commit, so the idle doc-less cache is refreshed promptly instead of after the ~10-min sweep.

Scope

fulltext2 only. cagra/ivfpq are also base-less on COPY ALTER (same shape) but work today via idxcron; converting them to
REINDEX, and changing hnsw/cagra/ivfpq's forcible Remove to RemoveIdle, are deferred follow-ups.

Testing

  • Regression BVT — test/distributed/cases/pessimistic_transaction/fulltext2/fulltext2_copy_alter.sql: force-builds a
    FULLTEXT2 index, performs an unrelated COPY ALTER, then @wait_expects on the replacement index's durable tag=0 base
    (never polls MATCH, which can pin a stale per-CN cache) before comparing MATCH against a LIKE base-table oracle. Pre-fix
    the tag=0 base never appears → the case times out and fails; post-fix it converges in ~1s. Validated in mo-tester run mode:
    25/25 (100%).
  • Unit tests — RemoveIdle busy-vs-idle + snapshot-preservation (-race -count=40, full cache package -race); rewritten
    [Performance]: keep FULLTEXT2 cache read-only across CDC updates #28005 consumer contract test (-race -count=20); AlterCopyInitSQL REINDEX + fail-closed-on-empty-defs; plugin (false,"")
    stubs.
  • Live-validated end-to-end on a two-CN cluster: replacement base + MATCH == oracle converge in ~2.5s.

…x queryable (matrixorigin#28837)

An unrelated COPY ALTER (e.g. ADD COLUMN) left an unaffected FULLTEXT2 index
returning empty for minutes until a reindex/restart. Two causes, both fixed:

1. Base-less rebuild. cloneUnaffectedIndexes marks fulltext2 SkipWholeIndex, so
   the ALTER clones the table to a new id with empty index tables rebuilt from
   the CDC log -- but RunFulltext2 appends only a tag=1 cdc_tail and a fulltext2
   base is written ONLY by buildFromSource, so the replacement had no base.
   New plugin hook AlterCopyInitSQL: fulltext2 returns an ALTER REINDEX ...
   FULLTEXT2 FORCE_SYNC seed (run post-commit by the CDC's first iteration);
   every other algo returns (false, "") because its consumer rebuilds from the
   ts=0 replay. Wired into alter.go's unaffected-index ISCP block, mirroring
   RestoreInitSQL, including its initSQL=="" => startFromNow=false guard.

2. Stale warm cache. The querying CN kept its doc-less warm cache across the CDC
   flush (the matrixorigin#28005 keep-warm optimization), serving false negatives until the
   ~10-min IsStale sweep. New cache.RemoveIdle evicts a cached index only when no
   search is in flight (wraps evictIdleEntry/tryClaimIdle); a busy entry stays
   warm and refreshes later -- no reader thrash. RunFulltext2 calls it (via
   fulltext2.EvictIdleCache) after a non-empty tail commit.

Scope: fulltext2 only. cagra/ivfpq (also base-less) REINDEX and converting
hnsw/cagra/ivfpq forcible Remove to RemoveIdle are deferred follow-ups.

Regression: a two-CN-safe BVT (pessimistic_transaction/fulltext2/
fulltext2_copy_alter.sql) force-builds FULLTEXT2, performs an unrelated COPY
ALTER, and @wait_expect's on the REPLACEMENT index's durable tag=0 base (never
polls MATCH, which can pin a stale per-CN cache) before comparing MATCH to a
LIKE base-table oracle. Pre-fix the tag=0 base never appears, so the case times
out and fails; post-fix it converges in ~1s.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@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 →

…ixorigin#28837)

The coverage gate scores newly-added lines; the copy-alter CDC-seed logic read
low because it only executes on the copy-alter path (BVT, which truncates for
fulltext2 in CI). Move the testable core out and add CPU unit tests:

- Extract alterCopyCdcSeed (idxDefs build + AlterCopyInitSQL call + the
  initSQL=="" ts=0-replay guard) from alterTableCopy into a package helper and
  cover it with TestAlterCopyCdcSeed (rebuild passthrough, empty-InitSQL guard,
  (false,"") algo, error propagation, index-name filter) via a fake Hooks.
- Add TestAlterCopyInitSQL for cagra/ivfpq/ivfflat/classic-fulltext plugin
  compile (0% -> 100%); hnsw and fulltext2 already had one.
- Revert incidental churn (idxcronCctx var rename, expanded logutil line) back to
  origin/main so those non-new lines leave the diff.

No behavior change: alterCopyCdcSeed is the same dispatch previously inlined, and
the guard mirrors RestoreInitSQL (ddl.go).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Sep 14, 2026

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

GPT-6 medium deep re-review APPROVE at exact head d79dc6f\n\nAll prior findings are fixed: identifier escaping, CDC watermark/replay snapshot handling, stale empty-generation eviction, the empty-generation destruction race (both search paths capture under the entry read lock), and the initialization-window regression. Exact-head affected-package race/UT coverage passed, and the multi-CN regression passed 33/33 with no ignored statements. No concrete blockers remain.

@cpegeric
cpegeric requested a review from aptend September 15, 2026 15:30

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

Reviewed d79dc6f against c61ed37. No confirmed blocker.

The fulltext2 COPY ALTER seed builds a real base via escaped REINDEX FORCE_SYNC SQL; other plugins retain their established replay path and empty InitSQL forces startFromNow=false. Cache refresh happens after commit, idle removal preserves busy readers/snapshot generations, and a base-less/tail-less load is served but not retained. Both Search and SearchInto capture empty-generation state under the entry read lock, closing the destruction race.

Read the 25-file production/test change, hook dispatch and deterministic cache/commit-boundary controls, plus MATCH-versus-base-table BVT oracles. Reused green exact-head CI and existing exact-head race/33-of-33 multi-CN evidence; no fresh native/GPU/cluster run. Cross-CN nonempty-cache freshness remains eventual, not an instantaneous global invalidation guarantee.

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

Deep re-review at exact head d79dc6f87b6d1b501ef5147e3fd684737544e70c, covering the full 25-file diff, all prior reviews/replies/resolved threads, and the increment since my previous reviewed head 8c89a3711e1ecfac23d76db09a5879d2ca30211c. The previous synchronization blocker is closed: both Search paths now capture the fulltext2 empty-generation state while the cache entry read lock is held, and the deterministic reload test proves an empty prewarm is not retained. I also revalidated the init transaction: FORCE_SYNC rebuild drops/recreates the CDC generation in the same transaction, so the new job starts at the rebuild snapshot rather than replaying copied rows. Focused exact-head cache, race, compile-hook, plugin, and ISCP tests passed with the required CGO/thirdparties setup; exact-head CI remains green. No blocking issue found.

@mergify mergify Bot added the queued label Sep 16, 2026
@mergify

mergify Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-16 03:44 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-16 04:27 UTC · at cd18d24d8ee112e9234f6e1e0cf6f3d29932c5c0

This pull request spent 42 minutes 36 seconds in the queue, with no time running CI.

Reason

Pull request #28879 has been merged manually at d5c4e8b

Hint

You were too fast!

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
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 size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: COPY ALTER leaves an unaffected FULLTEXT2 index empty

6 participants