AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(merge): reserve merges on their on-disk footprint; drop held _source trees (Refs #2427) (#2462)
xerj-org094f308a157d
diff --git a/engine/crates/xerj-engine/src/governor.rs b/engine/crates/xerj-engine/src/governor.rsindex 6d9c7c6cd..e3b6e8db1 100644--- a/engine/crates/xerj-engine/src/governor.rs+++ b/engine/crates/xerj-engine/src/governor.rs@@ -674,7 +674,21 @@ impl ResourceGovernor {/// for the rule./// `None` means defer: the caller leaves the batch for a later pass.pub fn try_reserve_merge(&self, input_bytes: u64, forced: bool) -> Option<MergeReservation> {- let bytes = crate::merge_budget::merge_working_set_bytes(input_bytes);+ self.try_reserve_merge_with_footprint(input_bytes, 0, forced)+ }++ /// [`Self::try_reserve_merge`] for a batch whose inputs also occupy+ /// `on_disk_bytes` on disk, side-cars included (#2427). The reservation+ /// is `merge_budget::merge_reservation_bytes`: the planned-input+ /// estimate or `on_disk_bytes * MERGE_ON_DISK_WORKING_SET_NUMERATOR`,+ /// whichever is larger. `0` means unknown.+ pub fn try_reserve_merge_with_footprint(+ &self,+ input_bytes: u64,+ on_disk_bytes: u64,+ forced: bool,+ ) -> Option<MergeReservation> {+ let bytes = crate::merge_budget::merge_reservation_bytes(input_bytes, on_disk_bytes);let watermark = self.memory_watermark_bytes();let observed = self.observed_process_memory();let engaged = self.memory_breaker_engaged();@@ -3964,6 +3978,44 @@ mod tests {drop(forced);}+ /// #2427: a batch whose side-cars dwarf its segment file reserves on its+ /// on-disk footprint, so a second merge that the planned estimate would+ /// admit next to it waits.+ #[test]+ fn merge_reservation_uses_the_on_disk_footprint() {+ let mut cfg = Config::default();+ cfg.limits.max_total_memtable_mb = 0;+ cfg.limits.memory_watermark_percent = 95;+ let g = build(&cfg);+ let wm = g.memory_watermark_bytes.load(Ordering::Relaxed);+ assert!(wm > 0, "this test needs a live RSS watermark");+ g.refresh(0, wm / 2 + 1, 0);++ // Planned input small enough that two batches fit by the old rule.+ let planned = wm / 16 / crate::merge_budget::MERGE_WORKING_SET_NUMERATOR;+ // On disk, a footprint whose estimate is over a quarter of the watermark.+ let on_disk = wm / 4 / crate::merge_budget::MERGE_ON_DISK_WORKING_SET_NUMERATOR + 1;+ let first = g+ .try_reserve_merge_with_footprint(planned, on_disk, false)+ .expect("the first merge always starts");+ assert_eq!(+ first.bytes(),+ crate::merge_budget::merge_reservation_bytes(planned, on_disk)+ );+ assert!(first.bytes() > crate::merge_budget::merge_working_set_bytes(planned));+ // The same second batch: 50% + >25% + >25% is past the watermark.+ assert!(g+ .try_reserve_merge_with_footprint(planned, on_disk, false)+ .is_none());+ // By the planned estimate alone it would have been admitted.+ let second = g+ .try_reserve_merge(planned, false)+ .expect("planned-only fits");+ drop(second);+ drop(first);+ assert_eq!(g.merge_reserved_bytes(), 0);+ }+/// #2427: on the 1 GB gate an admitted merge batch grew a 3,072 MB node/// to 8,122 MB while the breaker was engaged. A background batch yields/// once RSS reaches the process limit with the breaker engaged, never indiff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex cd6642ffc..b9fea0b9d 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -666,6 +666,63 @@ mod collection_publication_fail_closed_tests {/// spellings, and a flush that lands them in ONE segment (the 2-core CI/// shape) built the lying column — `terms {b:["true"]}` then returned the/// pre-upgrade document and silently dropped the coerced one.+ /// #2427: the merge builds doc-values from its merged stored JSON one+ /// document at a time instead of holding every `_source` tree. The+ /// columns must match what the held trees produced, ordinals included:+ /// a document with no `_source`, a non-object `_source`, and one that+ /// does not parse as an object each keep their row.+ #[test]+ fn merge_doc_value_sources_matches_held_source_trees() {+ let docs = [+ json!({"_id": "a", "_seq_no": 1, "_source": {"k": "x", "n": 1, "b": true}}),+ json!({"_id": "b", "_seq_no": 2}),+ json!({"_id": "c", "_seq_no": 3, "_source": {"k": "y", "n": 2.5, "arr": [1, 2]}}),+ json!({"_id": "d", "_seq_no": 4, "_source": "not-an-object"}),+ json!({"_id": "e", "_seq_no": 5, "_source": {"k": "z", "msg": "hello"}}),+ ];+ let mut merged = b"[".to_vec();+ for (i, doc) in docs.iter().enumerate() {+ if i > 0 {+ merged.push(b',');+ }+ merged.extend_from_slice(serde_json::to_string(doc).unwrap().as_bytes());+ }+ // A raw element that is not an object takes a row too.+ merged.extend_from_slice(b",42]");++ let mut skip = std::collections::HashSet::new();+ skip.insert("msg".to_string());++ // The pre-#2427 input: one held `_source` tree per document.+ let mut held: Vec<Value> = docs+ .iter()+ .map(|doc| doc.get("_source").cloned().unwrap_or(Value::Null))+ .collect();+ held.push(Value::Null);+ let expected = super::build_doc_value_columns(held.iter().map(Some), &skip);++ let streamed = super::build_doc_value_columns(+ super::merge_doc_value_sources(&merged).expect("array parses"),+ &skip,+ );+ assert_eq!(+ format!("{streamed:?}"),+ format!("{expected:?}"),+ "streamed doc-values must equal the held-tree build"+ );+ assert!(streamed.contains_key("k") && streamed.contains_key("n"));+ assert!(+ !streamed.contains_key("arr"),+ "multi-valued field ships no column"+ );+ assert_eq!(+ super::merge_doc_value_sources(&merged).unwrap().count(),+ docs.len() + 1,+ "one item per merged document, in order"+ );+ assert!(super::merge_doc_value_sources(b"not json").is_err());+ }+#[test]fn build_doc_value_columns_ships_nothing_for_a_type_mixed_field() {let legacy = json!({"b": "true", "n": "5", "ok": "kw"});@@ -14119,7 +14176,16 @@ impl Index {return Some(None);};let input: u64 = metas.iter().map(|meta| meta.size_bytes).sum();- governor.try_reserve_merge(input, forced_merge).map(Some)+ // #2427: reserve on the inputs' whole footprint. `size_bytes` is+ // the segment file alone; the side-cars the merge decodes are+ // usually several times larger (see+ // `merge_budget::MERGE_ON_DISK_WORKING_SET_NUMERATOR`).+ let ids: Vec<xerj_storage::segment::SegmentId> =+ metas.iter().map(|meta| meta.id.clone()).collect();+ let on_disk = self.store.segment_files_bytes(&ids);+ governor+ .try_reserve_merge_with_footprint(input, on_disk, forced_merge)+ .map(Some)};let mut governor_deferred = 0usize;@@ -14344,10 +14410,20 @@ impl Index {// for the doc-values pass. Drops `dv_sources` entirely.// Halves merge working memory and frees the Arc<Value>// immediately after both passes complete.+ // #2427: the third slot used to carry each surviving+ // document's whole `_source` `Value` tree for the+ // doc-values pass, held from the drain loop through the+ // stored encode (which parses the same batch into a+ // second `Vec<Value>` of its own), the FTS build and the+ // doc-values build. Two full Value trees of the batch+ // were live at once after the last yield point. The+ // doc-values columns are now built from `merged_json_buf`+ // one document at a time (`merge_doc_value_sources`), so+ // the slot is empty.let mut fts_input: Vec<(String,HashMap<String, xerj_fts::index::FieldValues>,- Value,+ (),)> = Vec::new();// (seq_no, doc_id) for every surviving doc — kept exactly// aligned with the stored-section byte copy (pushed right@@ -14683,40 +14759,38 @@ impl Index {// addresses a document by. An empty `_source` indexes and// columnises nothing for it, which is the same as the// skip minus the shift.- let doc_value: Value =- serde_json::from_str(&raw_str).unwrap_or_else(|error| {- tracing::warn!(- doc = %id_str,- "merge: surviving doc failed its re-parse, indexing it empty: {error}"- );- Value::Null- });- let source = doc_value.get("_source").cloned().unwrap_or(Value::Null);// The merged FTS/doc-values ordinal is this document's// index in `fts_input`.doc_maps[source_index as usize][source_ordinal as usize] =Some(fts_input.len() as u32);// The re-analysis input. `merge_postings` replays the// inputs' posting lists instead, so it needs no field- // values at all — only the `_source` the doc-values- // builder reads below. An empty `HashMap` allocates- // nothing.+ // values and no per-document parse at all (#2427: the+ // doc-values pass re-reads `merged_json_buf`). An+ // empty `HashMap` allocates nothing. The `Value` the+ // rebuild parses drops at the end of this iteration.let fields = if merge_postings {HashMap::new()} else {- extract_fts_fields_excluding(&source, &excluded_fts_fields_for_task)+ let doc_value: Value =+ serde_json::from_str(&raw_str).unwrap_or_else(|error| {+ tracing::warn!(+ doc = %id_str,+ "merge: surviving doc failed its re-parse, indexing it empty: {error}"+ );+ Value::Null+ });+ let source = doc_value.get("_source").unwrap_or(&Value::Null);+ // #948 attribution: the transient parse, per doc,+ // only when tracing is enabled.+ if crate::ingest_memory::enabled() {+ fts_input_bytes = fts_input_bytes+ .saturating_add(crate::ingest_memory::estimated_json_heap(source));+ }+ extract_fts_fields_excluding(source, &excluded_fts_fields_for_task)};- // #948 attribution: the Value tree the FTS/DV passes- // hold until the end of the batch. The walk is- // per-document but only when tracing is enabled.- if crate::ingest_memory::enabled() {- fts_input_bytes = fts_input_bytes- .saturating_add(crate::ingest_memory::estimated_json_heap(- doc_value.get("_source").unwrap_or(&Value::Null),- ));- }if let Err(error) =- crate::merge_reserve::try_push_merge(&mut fts_input, (id_str, fields, source))+ crate::merge_reserve::try_push_merge(&mut fts_input, (id_str, fields, ())){tracing::error!("{error}");failed_for_task.fetch_add(1, Ordering::Relaxed);@@ -14771,6 +14845,26 @@ impl Index {yielded_for_task.fetch_add(1, Ordering::Relaxed);return None;}+ // Doc-values columns, built here from the merged stored+ // JSON one document at a time (#2427) and written once the+ // merged segment id exists, below. The columns are what+ // survives; the per-document `_source` trees do not.+ // Before, those trees were held in `fts_input` for the+ // whole batch, through the stored encode that parses the+ // same documents into a second `Vec<Value>`: two decoded+ // copies of the batch at once, past the last yield point.+ let dv_timer = std::time::Instant::now();+ let dv_columns = match merge_doc_value_sources(&merged_json_buf) {+ Ok(sources) => Some(build_doc_value_columns(sources, &dv_skip_for_merge)),+ Err(e) => {+ tracing::warn!(+ "merge: merged stored JSON did not re-parse for doc-values, \+ segment ships no doc-values side-car: {e}"+ );+ None+ }+ };+ let dv_build_us = dv_timer.elapsed().as_micros();// Write merged stored section using the columnar v2 codec.let mut writer = match SegmentWriter::new(&segments_dir_for_task, 1, 0, 0) {Ok(w) => w,@@ -15022,14 +15116,10 @@ impl Index {version_repoint_intents.extend(tomb_carry.iter().map(|(seq, id)| (*seq, id.clone(), true)));- // Doc-values side-car — reuse the same `Value`s we- // stashed in fts_input above (M5.22).+ // Doc-values side-car — the columns were built before the+ // stored encode (#2427, see `dv_columns`).let dv_timer = std::time::Instant::now();- {- let columns = build_doc_value_columns(- fts_input.iter().map(|(_, _, v)| Some(v)),- &dv_skip_for_merge,- );+ if let Some(columns) = dv_columns {if !columns.is_empty() {if let Err(e) = write_doc_values_sidecar_at_level(&segments_dir_for_task,@@ -15042,10 +15132,11 @@ impl Index {}}- let dv_us = dv_timer.elapsed().as_micros();- // `fts_input` (raw ids + the per-doc `_source` Value- // trees) survives its last reader here — the batch's- // parsed-side attribution ends with it.+ let dv_us = dv_build_us + dv_timer.elapsed().as_micros();+ // `fts_input` (raw ids + the re-analysis field values;+ // no `_source` trees since #2427) survives its last+ // reader here — the batch's parsed-side attribution ends+ // with it.crate::ingest_memory::observe_checkpoint(crate::ingest_memory::Category::MergeParsed,0,@@ -29353,6 +29444,38 @@ pub fn doc_values_skip_set(schema: &Schema) -> std::collections::HashSet<String>skip}+/// The doc-values input of a merge batch, read back from the batch's+/// merged stored JSON one document at a time (#2427).+///+/// `merged_json` is the `[doc,doc,...]` array the merge byte-copies its+/// survivors into, in merged ordinal order. Each item is that document's+/// `_source` (or `Null` when it has none or does not parse, the same+/// fallback the merge used when it held the trees), and is dropped by+/// [`build_doc_value_columns`] before the next one is parsed. Only one+/// `_source` tree is alive at a time; before #2427 the merge held one per+/// surviving document from the drain loop to the end of the batch, on top+/// of the stored encoder's own `Vec<Value>` of the same documents.+///+/// `Err` means the buffer is not a JSON array (it is built from documents+/// that already parsed as `RawValue`, so this does not happen in practice).+fn merge_doc_value_sources(+ merged_json: &[u8],+) -> std::result::Result<impl Iterator<Item = Option<Value>> + '_, serde_json::Error> {+ #[derive(serde::Deserialize)]+ struct SourceOnly {+ #[serde(rename = "_source", default)]+ source: Value,+ }+ let docs: Vec<&serde_json::value::RawValue> = serde_json::from_slice(merged_json)?;+ Ok(docs.into_iter().map(|raw| {+ Some(+ serde_json::from_str::<SourceOnly>(raw.get())+ .map(|doc| doc.source)+ .unwrap_or(Value::Null),+ )+ }))+}+/// Build the per-segment doc-values columns.////// `skip` names fields whose declared mapping says they must not have@@ -29366,8 +29489,8 @@ pub fn doc_values_skip_set(schema: &Schema) -> std::collections::HashSet<String>/// and 35.6% of the whole index. It is much smaller where `_source` dominates/// (6.97% across the `xc-*` code corpora), so the saving is corpus-dependent and/// must always be quoted as a range.-fn build_doc_value_columns<'a>(- sources: impl Iterator<Item = Option<&'a Value>>,+fn build_doc_value_columns<V: std::borrow::Borrow<Value>>(+ sources: impl Iterator<Item = Option<V>>,skip: &std::collections::HashSet<String>,) -> std::collections::BTreeMap<String, xerj_storage::doc_values::Column> {use std::collections::BTreeMap;@@ -29412,7 +29535,10 @@ fn build_doc_value_columns<'a>(for src in sources {let doc_idx = total_docs;total_docs += 1;- let Some(obj) = src.and_then(|v| v.as_object()) else {+ let Some(obj) = src+ .as_ref()+ .and_then(|v| <V as std::borrow::Borrow<Value>>::borrow(v).as_object())+ else {continue;};for (field, val) in obj {@@ -36319,7 +36445,7 @@ async fn do_flush_shard(let columns = build_doc_value_columns(drained_fts_for_build.iter()- .map(|(_id, _fields, src)| Some(src.as_ref())),+ .map(|(_id, _fields, src)| Some(&**src)),&dv_skip_for_flush,);if !columns.is_empty() {diff --git a/engine/crates/xerj-engine/src/merge_budget.rs b/engine/crates/xerj-engine/src/merge_budget.rsindex 81b34b0b3..845f48c77 100644--- a/engine/crates/xerj-engine/src/merge_budget.rs+++ b/engine/crates/xerj-engine/src/merge_budget.rs@@ -132,6 +132,31 @@ pub fn merge_working_set_bytes(input_bytes: u64) -> u64 {input_bytes.saturating_mul(MERGE_WORKING_SET_NUMERATOR)}+/// Reservation multiplier on a batch's whole on-disk footprint (#2427).+///+/// `SegmentMeta::size_bytes`, which the planner and+/// [`MERGE_WORKING_SET_NUMERATOR`] use, is the segment file alone. A+/// merge also reads every input's FTS, doc-values and ids side-cars+/// (about 60 files per segment on the autoindex corpora), so the planned+/// input under-counts what the batch decodes. On the reduced #2427 repro+/// (1,024 MB cap, `docs` corpus) a 9-input batch (13 MB on disk) ran+/// beside an 8-input batch (23 MB on disk) with only 24 MB reserved in+/// the ledger, and RSS went from about 740 MB to a 1,503 MB peak: about+/// 21x the two batches' combined on-disk bytes. The 3 GB gate analysis+/// puts a merge's working set at about 30x its on-disk input. 32x keeps+/// the reservation at or above both.+pub const MERGE_ON_DISK_WORKING_SET_NUMERATOR: u64 = 32;++/// Bytes one merge batch reserves against the watermark (#2427): the+/// larger of the planned-input estimate ([`merge_working_set_bytes`]) and+/// [`MERGE_ON_DISK_WORKING_SET_NUMERATOR`] × the inputs' on-disk bytes,+/// side-cars included. `on_disk_bytes == 0` (unknown) keeps the planned+/// estimate.+pub fn merge_reservation_bytes(planned_input_bytes: u64, on_disk_bytes: u64) -> u64 {+ merge_working_set_bytes(planned_input_bytes)+ .max(on_disk_bytes.saturating_mul(MERGE_ON_DISK_WORKING_SET_NUMERATOR))+}+/// Process-wide merge admission against the RSS watermark (#2427).////// [`throttle_merge_batches`] plans one index's pass from an RSS snapshot@@ -488,4 +513,28 @@ mod tests {);assert_eq!(merge_working_set_bytes(u64::MAX), u64::MAX);}++ /// #2427: the reservation follows the on-disk footprint, side-cars+ /// included, when that is the larger estimate.+ #[test]+ fn merge_reservation_counts_the_on_disk_footprint() {+ const MIB: u64 = 1024 * 1024;+ // Unknown footprint: the planned estimate, unchanged.+ assert_eq!(+ merge_reservation_bytes(3 * MIB, 0),+ merge_working_set_bytes(3 * MIB)+ );+ // A repro-sized batch: 3 MB planned, 23 MB on disk. 8x planned is+ // 24 MB; the footprint estimate covers the ~700 MB RSS rise measured.+ let reserved = merge_reservation_bytes(3 * MIB, 23 * MIB);+ assert_eq!(reserved, 23 * MIB * MERGE_ON_DISK_WORKING_SET_NUMERATOR);+ assert!(reserved >= 700 * MIB);+ // A footprint smaller than the planned estimate never lowers it.+ assert_eq!(+ merge_reservation_bytes(64 * MIB, MIB),+ merge_working_set_bytes(64 * MIB)+ );+ // Saturates instead of wrapping.+ assert_eq!(merge_reservation_bytes(0, u64::MAX), u64::MAX);+ }}diff --git a/engine/crates/xerj-storage/src/index_store.rs b/engine/crates/xerj-storage/src/index_store.rsindex 416bd3c42..a1bed0451 100644--- a/engine/crates/xerj-storage/src/index_store.rs+++ b/engine/crates/xerj-storage/src/index_store.rs@@ -1694,6 +1694,34 @@ impl IndexStore {crate::bundle::valid_flush_artifact_set(segment_id, &declared) && declared == actual}+ /// Bytes on disk of every local file belonging to `segment_ids`: the+ /// segment file and all its side-cars (FTS, doc-values, ids, ...), matched+ /// by the same `<36-char id>.<suffix>` rule [`Self::delete_segment_files`]+ /// uses. Read-only. `0` when the directory cannot be listed (#2427: merge+ /// admission reserves against this footprint).+ pub fn segment_files_bytes(&self, segment_ids: &[SegmentId]) -> u64 {+ let ids: std::collections::HashSet<&str> = segment_ids.iter().map(|s| s.as_str()).collect();+ if ids.is_empty() {+ return 0;+ }+ let Ok(entries) = std::fs::read_dir(self.data_dir.join("segments")) else {+ return 0;+ };+ let mut bytes = 0u64;+ for entry in entries.flatten() {+ let name = entry.file_name();+ let name_str = name.to_string_lossy();+ if name_str.len() < 37 || !name_str.is_char_boundary(36) {+ continue;+ }+ if !ids.contains(&name_str[..36]) {+ continue;+ }+ bytes = bytes.saturating_add(entry.metadata().map(|m| m.len()).unwrap_or(0));+ }+ bytes+ }+/// Unlink every on-disk file belonging to the given segment ids — the/// primary `.seg` plus all side-cars (`.sidx`, `.ids`, `.dv`,/// `.<field>.post` / `.fst` / `.meta` / `.norms`) and the optional