perf(engine): concurrent Kibana dashboard bursts stalled the scan path - #48
Merged
xerj-org merged 2 commits intoJul 27, 2026
Merged
Conversation
Two independent, compounding costs in the brute-force scan path (`scan_stored_section_into` / its `search_inner` caller), both specifically hit by a real Kibana dashboard firing several panel queries at once -- the exact scenario a user reported as "xerj feels slow when Kibana sends multiple parallel requests": 1. `scan_stored_section_into` unconditionally deep-cloned EVERY scanned doc's `_source` to splice `_id` in, even though that injection is only ever needed for a deeply-nested `Ids` clause (a top-level `Ids` is already handled separately, without this). Added `query_needs_id_injection()` -- an exhaustive match over every `QueryNode` variant (the compiler rejects the build if a future variant that can nest a sub-query isn't handled, so a missed arm can only ever cost the pre-fix clone, never a silently wrong match) -- computed once per scan, not per doc. The common case (`term`/`match`/`match_phrase`/`query_string`/`bool` with no nested `ids`) now matches directly against the borrowed source, no clone. 2. The raw-fallback decode branch (reached whenever neither `decoded_stored_cache` nor `stored_slices_cache` is warm -- i.e. every FIRST query against a segment, most visibly right after a restart) opened and decompressed the segment with no single-flight protection. N concurrent requests racing the same cold segment each independently paid the full open+decompress -- measured ~4.5s/request *uniformly* across a real 24-way concurrent burst (every request finishing in the same narrow window is the signature of N-way redundant work, not per-request cost). Wrapped the decode in the same per-segment mutex (`stored_slices_build_locks`) the sorted path's `stored_slices_for` already uses for this exact reason, with a cache re-check after acquiring the lock so only the first waiter actually decodes. Verified against a real Kibana OSS 7.10.2 instance and real eCommerce sample data: a 24-concurrent-request burst of `query_string` wildcard + `match_phrase` filters, fired immediately after a fresh restart (worst case: fully cold caches), went from ~4.5s per request (uniform, zero variance -- 32 `slow_query` warnings logged) to 100-165ms per request (zero `slow_query` warnings) -- a ~30-45x improvement on the cold-start case Kibana dashboards hit on every restart. A warm-cache repeat of the same burst (i.e. the second and every subsequent dashboard load) completes in 1-5ms. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016XJaZygeuRfZfUg2B8tPKU
…resolve index.rs conflict: keep both query_needs_id_injection and geo_coord)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two independent, compounding costs in the brute-force scan path, both specifically hit by a real Kibana dashboard firing several panel queries at once — this is what a user reported as "xerj feels slow when Kibana sends multiple parallel requests," one of the last blockers before trusting xerj as a real Kibana/OSD backend.
Bug 1 — unconditional per-doc
_sourceclonescan_stored_section_into(the brute-force query-matching loop) deep-cloned every scanned document's_sourceto splice_idin, even though that injection is only ever needed for a deeply-nestedIdsclause (e.g.function_score → ids) — a top-levelIdsquery is already handled separately, without this clone at all.Fix:
query_needs_id_injection()— an exhaustive match over everyQueryNodevariant, computed once per scan (not per doc). The match has no wildcard arm on purpose: adding a futureQueryNodevariant that can nest a sub-query without updating this function is a compile error, so a missed arm can only ever cost the pre-fix clone again, never silently produce a wrong match. The common case (term/match/match_phrase/query_string/boolwith no nestedids— the overwhelming majority of real queries) now matches directly against the borrowed source, no clone at all.Bug 2 — cold segment decode has no single-flight protection
The raw-fallback decode branch (reached whenever neither
decoded_stored_cachenorstored_slices_cacheis warm for a segment — i.e. every first query against it, most visibly right after a restart) opens and decompresses the segment with no single-flight protection. N concurrent requests racing the same cold segment each independently pay the full open+decompress.Measured ~4.5s per request, uniformly, across a real 24-way concurrent burst — every request finishing in the same narrow time window is the signature of N-way redundant work, not N independent per-request costs.
Fix: wrapped the decode in the same per-segment mutex (
stored_slices_build_locks) the sorted path'sstored_slices_foralready uses for this exact reason (see its own doc comment: "up to 64 in-flight queries race the same cold segment... one ~30ms decode became a ~1s 64-query stall episode") — with a cache re-check after acquiring the lock so only the first waiter actually decodes; everyone else gets the freshly-published result.Test plan
cargo build --release -p xerj-engine -p xerj-api -p xerj-servercargo fmt --check/cargo clippy --no-deps -- -D warnings— cleancargo test --release -p xerj-engine --lib— 156/156 passedids-query failures given bug 1 touches that logic)query_stringwildcard +match_phrasefilters, fired immediately after a fresh restart (worst case — fully cold caches):slow_querywarnings loggedslow_querywarnings — a ~30–45x improvement on the cold-start case every Kibana dashboard hits after a restart🤖 Generated with Claude Code