8000 api: a no-match by-query run must not flush the index (#1222) by xerj-org · Pull Request #1227 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

api: a no-match by-query run must not flush the index (#1222) - #1227

Merged
xerj-org merged 1 commit into
mainfrom
fix/byquery-noop-no-flush
Oct 7, 2026
Merged

xerj-org merged 1 commit into
mainfrom
fix/byquery-noop-no-flush

Conversation

@xerj-org
@xerj-org xerj-org commented Oct 7, 2026
Copy link
Copy Markdown
Owner

What

run_delete_by_query and run_update_by_query flush the member index FIRST, unconditionally — the #1019 paging precondition (_id-keyset paging is only correct over on-disk segments). A run whose query matches nothing still paid that flush. This PR gates it: a size:0 probe through the ordinary search path (active memtable + segments) runs first; zero matches returns the zero response without flushing. A matching doc that was never flushed still counts and takes the flush arm exactly as before. Probe errors propagate; a script resource limit during the probe refuses the run, matching the fail-closed per-batch check.

Corrected root cause of the #1222 segment storm

The issue blames the #873 idle-flush fingerprint loop. That mechanism is already dead — Index::flush() clears the idle-probe pair at entry (with the PR #878 regression test for exactly the write→age→flush→same-shape-write sequence) — and the node evidence rules the idle path out entirely: the corpus-builder node runs flush_idle_secs = 0, a single write under that config provably sits in the memtable (reproduced: 47 s, zero segments), and the 1-doc segment storm continued anyway.

The real mechanism is this PR's target. The corpus apply loop fires one defensive no-match delete_by_query per group op (#1224: 402,744 of them on cve-records). Per op: bulk writes 1 doc (per-file group), the next op's delete flushes it straight back out into a 1-doc segment (~121 files on the cvelistV5 dynamic mapping), which the merge scheduler then rewrites. That is the measured "6,848 one-doc segments for ~3,005 writes" — a flush per write with no idle path involved. Full corrected diagnosis posted on the issue.

This also reframes most of #1226: the 5–16 s delete_by_query/_refresh latencies are this flush (segment write + fsync + merge contention under concurrent ops), not a refresh-specific bottleneck.

Verification

  • Fail-before: gate forced to always report a match → the new no-match test fails with the memtable drained, the exact production behavior.
  • Live A/B on a throwaway node, 1 doc written with refresh=false:
    • old binary: no-match delete → 1-doc segment created
    • this branch: no-match delete → 0 segments; matching delete → total 1 / deleted 1 / batches 1, doc gone
  • no_match_by_query_does_not_flush (delete + update twins) and matching_delete_by_query_still_flushes_and_purges in es_compat.rs.
  • Crate suite: xerj-api 811 passed / 0 failed.
  • ES-YAML conformance gate on this branch: 1385 passed / 0 failed / 4 skipped.

Server-side twin of the client gate merged in #1225 (which stops our own client from sending the no-op deletes; this makes the server tolerate any ES-native client that still does). _reindex's flush-first is left unchanged — not in the apply loop's path.

Refs #1222 (segment-storm half), #1224 (server half), #1226 (likely explains most of the measured refresh cost).

Corrected root cause, superseding the idle-flush fingerprint theory in
the issue: the fingerprint loop is already dead (Index::flush clears the
idle-probe pair at entry, with the PR #878 regression test for exactly
the write->age->flush->same-shape-write sequence), and the node evidence
rules the idle path out entirely — the corpus-builder node runs
flush_idle_secs = 0, a single write under that config provably sits in
the memtable (reproduced: 47 s, zero segments), and the 1-doc segment
storm continued anyway.

The real mechanism: run_delete_by_query flushes the member index FIRST,
unconditionally, before it even parses the query (#1019: _id-keyset
paging is only correct over on-disk segments). A delete whose query
matches nothing still pays that flush, and the corpus apply loop fires
one defensive no-match delete per group op — #1224's cve-records build
issued 402,744 of them. Per op: bulk writes 1 doc (per-file group),
the next op's delete flushes it straight back out into a 1-doc segment
(~121 files on the cvelistV5 dynamic mapping) which the merge scheduler
then rewrites. That is the measured "6,848 one-doc segments for ~3,005
writes" — a flush per write with no idle path involved. It also reframes
most of #1226: the 5-16 s delete_by_query/_refresh latencies are this
flush (segment write + fsync + merge contention), not a refresh-specific
bottleneck.

Fix (server-side twin of the client gate in #1225): probe first with a
size:0 search through the ordinary search path — active memtable plus
segments, so a matching doc that was never flushed still counts and the
run proceeds to the flush arm exactly as before. Zero matches returns
the zero response (total 0 / batches 0 — the same shape both arms below
already return for a no-match run over a flushed index) without
flushing. A probe error propagates; a script resource limit during the
probe refuses the whole run, matching the fail-closed per-batch check.
Applied to _update_by_query's runner too (identical shape). _reindex's
flush-first is left as is: it is not in the apply loop's path.

Tests (fail-before verified by forcing the gate to always report a
match: the new no-match test fails with the memtable drained — the
exact production behavior):
- no_match_by_query_does_not_flush: a no-match delete AND update leave
  the just-written document in the memtable.
- matching_delete_by_query_still_flushes_and_purges: an unflushed
  matching doc is visible to the probe, takes the flush arm, is purged;
  the non-matching doc survives.

Live A/B on a throwaway node: PUT 1 doc -> 0 segments; no-match
delete -> 0 segments (old binary: 1-doc segment, 121 files); matching
delete -> total 1 / deleted 1 / doc gone.

Crate suite: xerj-api 811 passed / 0 failed. ES-YAML gate on this
branch: see PR.

Files:
- crates/xerj-api/src/es_compat.rs: by_query_matches_anything gate +
  call sites in run_delete_by_query / run_update_by_query + tests
@cla-bot cla-bot Bot added the cla-signed label Oct 7, 2026
@xerj-org
xerj-org merged commit cb91a67 into main Oct 7, 2026
22 checks passed
thomas-villani pushed a commit to thomas-villani/xerj that referenced this pull request Oct 9, 2026
Workspace version + Cargo.lock (17 member entries). Release window:
xerj-org#1219, xerj-org#1223, xerj-org#1225, xerj-org#1227, xerj-org#1228.
thomas-villani pushed a commit to thomas-villani/xerj that referenced this pull request Oct 9, 2026
Window: xerj-org#1219 (Vinz2168 field report, closes the xerj-org#1091 tracker it
diagnoses), xerj-org#1223 (kNN exact-scan capture bracket, xerj-org#1220), xerj-org#1227
(no-match by-query must not flush, xerj-org#1222), xerj-org#1225 (visibility probe
gates the per-group delete), xerj-org#1228 (O(1) journal state writes, the
xerj-org#1224 client-CPU leg).

Release-notes gate: PASS locally (release-notes-gate.sh against
origin/main) after the gate itself caught the xerj-org#1224 claims-it-whole
drift in the first draft - the entry now says the tracker closes on
the completed re-measure, not on the legs.
thomas-villani pushed a commit to thomas-villani/xerj that referenced this pull request Oct 9, 2026
- xerj-org#1220 closed by xerj-org#1223, xerj-org#1222 closed by xerj-org#1227, xerj-org#1091 closed by the
  xerj-org#1219 field report: moved to the closed-inside-window record (all
  three verified closed live before de-linking).
- xerj-org#1226 added OPEN: the 5-16s refresh/by-query latencies, reframed by
  xerj-org#1227 (the no-match flush the callers of which are now gone) - what
  remains is the flush cost itself under concurrent bulk load.
- Count eleven -> ten, all verified open live.
neoneye pushed a commit to agent-memory-atlas-archive/xerj-org--xerj that referenced this pull request Oct 10, 2026
xerj-org#1224's original report measured ~0.66 s of client CPU per file during
cve-records apply. The client no-op delete gate (xerj-org#1225) and the
server-side no-flush gate (xerj-org#1227) removed the delete_by_query storm, but
the re-measure still ran at 1.18 items/s with the client pinning one
core in pure user time (82% of a core, 0 kernel ticks) at 5.7 GB RSS.
The original per-file CPU claim was right for a reason nothing had
profiled yet: the parked delete stacks of the first gdb probe masked it.

Root cause: Journal::sync_operation_state runs twice per replayed
operation (Started + Committed), and each call cloned the ENTIRE
PendingSync - desired GenerationManifest with all 402,695 groups, the
402,744-entry operations Vec, and the growing operation_states map -
before appending one small durable line. Two full deep clones per file,
plus three more quadratics hiding behind them:

- PendingSync::apply_operation_state validated membership with a linear
  operations.iter().any() per state event, both live and again on every
  journal load-replay (2 events per operation).
- EsSyncBackend::apply_shared resolved each operation's group with
  base/desired groups.iter().find() over every group, per operation.
- replay_prepared resolved each content_id with a linear scan of the
  snapshot's 402k files, per upsert.

Fix, all client-side, event format and semantics unchanged:

- sync_operation_state: validate (pure) -> durable append -> record in
  place. The journal line is the transaction boundary; a crash between
  append and record loses nothing because load replay rebuilds the map
  from the file, and a failed append returns before any mutation, the
  same guarantee the clone-on-write provided.
- PendingSync carries a lazily-built, never-serialized OperationIdIndex
  (HashSet + len guard) so membership is O(1); apply_operation_state is
  now validate+record so load replay shares the fast path.
- EsSyncBackend builds a ReplayLookup (group_id and content_id to
  position maps for base, desired and snapshot) once per
  replay_operations call. Hits are verified against the slice they
  answer from and length-guarded, so a stale or absent cache falls back
  to the exact scan it replaces.

Pattern reference (approach, not code): redb's InMemoryState - the
durable structure is the truth, in-memory state is a derived cache
updated after the durable commit and rebuilt on load
(redb src/tree_store/page_store/page_manager.rs:252, Apache-2.0/MIT).

Measured, cve-records (402,695 files, 1.9 GB prepared) against the
isolated rc.89-gate node on :19285: apply phase 1.18 items/s before
this change (0.72 s client CPU per file); after-numbers land on xerj-org#1224
as the resumed run reaches the apply phase.

Tests: operation_state_split_semantics_and_membership_index_hold pins
the validate/record split, the pre-split transition rules, the
clone-carried index, and the len guard against stale-shape answers.
Full xerj-autoindex lib suite 1299 passed / 0 failed.

Files:
- engine/crates/xerj-autoindex/src/state.rs :: sync_operation_state
- engine/crates/xerj-autoindex/src/sync.rs :: PendingSync,
  OperationIdIndex, validate/record_operation_state
- engine/crates/xerj-autoindex/src/sync_executor.rs :: ReplayLookup,
  EsSyncBackend::{apply_shared, replay_prepared, replay_metadata,
  replay_operations}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0