Skip to content

armgit / xerj

Public Imported from GitHub
0

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.

fix(autoindex): measure bulk back-pressure patience since the last progress

Christian M. Todie9b12ff19067f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d04e93123..e7efe2b14 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -79,6 +79,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `autoindex` bulk back-pressure patience is now measured since the last
+ progress, not since the first round. A window the server drains a few
+ records at a time (per-item 429 `resource_exhausted` on an embedding
+ deadline or admission refusal) used to abort with `bulk backend failed`
+ once the rounds together passed the 600 s `THROTTLE_PATIENCE`, even though
+ every round settled records. Each round that settles at least one item now
+ restarts the patience clock and the backoff; rounds that settle nothing
+ still count, so a server that answers 429 forever fails at the same limits
+ as before.
+
- The parent memory breaker no longer flaps, and background merges and flush
finalizes are bounded while it is engaged (#2427). A 3,000 MB autoindex run
engaged the breaker 20 times in 16 minutes, often releasing 100-500 ms
diff --git a/engine/crates/xerj-autoindex/src/esclient.rs b/engine/crates/xerj-autoindex/src/esclient.rs
index 4222727b9..95d3aa853 100644
--- a/engine/crates/xerj-autoindex/src/esclient.rs
+++ b/engine/crates/xerj-autoindex/src/esclient.rs
@@ -1908,7 +1908,9 @@ impl Es {
/// 429/5xx retries the WHOLE request (`retry_loop`). A 200 whose items
/// carry `status: 429` re-offers ONLY those items, after a backoff, for up
/// to `throttle_patience` — the memory breaker's shape, see
- /// [`THROTTLE_PATIENCE`]. A parent memory breaker that has not accepted a
+ /// [`THROTTLE_PATIENCE`]. The patience clock restarts whenever a round
+ /// settles at least one item, so it bounds time without progress, not the
+ /// total time a draining window takes. A parent memory breaker that has not accepted a
/// single item of this bulk stops at [`ZERO_PROGRESS_PATIENCE`] instead
/// (#2238). Records the server took are never sent twice, and a record it
/// refused for a reason waiting cannot fix (a per-item 400, 5xx, or a
@@ -1923,7 +1925,7 @@ impl Es {
// applies, even if later rounds are all 429: a drain in progress
// (#944 held 57 s) must be allowed to finish.
let mut accepted_any = false;
- let episode = Instant::now();
+ let mut episode = Instant::now();
let mut delay = self.retry_initial_delay;
let mut last_notice: Option<Instant> = None;
loop {
@@ -1941,6 +1943,13 @@ impl Es {
}
if round.items > round.throttled.len() {
accepted_any = true;
+ // Patience is measured since the last progress, not since the
+ // first round: a window that settles at least one record per
+ // round is draining, and each such round restarts the clock
+ // and the backoff. Only rounds that settle nothing accumulate
+ // toward the limit below.
+ episode = Instant::now();
+ delay = self.retry_initial_delay;
}
// Only per-item 429s stand between this bulk and a clean
// outcome. Wait while patience lasts; after that the caller gets
@@ -4424,6 +4433,128 @@ mod tests {
assert!(outcome.throttled_out >= 1, "{outcome:?}");
}
+ /// A window the server drains one record per round (the rest 429, as an
+ /// embedding deadline slice does) must finish even when the rounds
+ /// together outlast the patience: the clock is time since the last
+ /// progress, not since the first round.
+ #[test]
+ fn patience_restarts_whenever_a_round_settles_an_item() {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let address = listener.local_addr().unwrap();
+ listener.set_nonblocking(true).unwrap();
+ let stop = Arc::new(AtomicBool::new(false));
+ let server_stop = Arc::clone(&stop);
+ let rounds = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let server_rounds = Arc::clone(&rounds);
+ let server = std::thread::spawn(move || loop {
+ match listener.accept() {
+ Ok((mut stream, _)) => {
+ stream.set_nonblocking(false).unwrap();
+ let request = read_request(&mut stream);
+ let actions = request_body(&request)
+ .split(|b| *b == b'\n')
+ .filter(|line| !line.is_empty())
+ .count()
+ / 2;
+ server_rounds.fetch_add(1, Ordering::SeqCst);
+ std::thread::sleep(Duration::from_millis(60));
+ let mut statuses = vec![429u16; actions];
+ statuses[0] = 201;
+ items_answered(&mut stream, &statuses);
+ }
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ if server_stop.load(Ordering::SeqCst) {
+ return;
+ }
+ std::thread::sleep(Duration::from_millis(2));
+ }
+ Err(error) => panic!("accept: {error}"),
+ }
+ });
+ let es = client(address, 4).with_throttle_patience(Duration::from_millis(200));
+ let ids = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
+ let body: Vec<u8> = ids.iter().flat_map(|id| pair(id)).collect();
+ let started = Instant::now();
+ let outcome = es.bulk(body).unwrap();
+ let elapsed = started.elapsed();
+ stop.store(true, Ordering::SeqCst);
+ server.join().unwrap();
+ assert_eq!(outcome.server_errors, 0, "{outcome:?}");
+ assert_eq!(outcome.throttled_out, 0, "{outcome:?}");
+ assert!(outcome.exhausted_after.is_none(), "{outcome:?}");
+ assert_eq!(
+ rounds.load(Ordering::SeqCst),
+ 10,
+ "one record settled per round"
+ );
+ assert!(
+ elapsed > Duration::from_millis(200),
+ "the test is only meaningful if the whole window outlasts the patience: {elapsed:?}"
+ );
+ }
+
+ /// The other half: progress restarts the clock, silence does not. After
+ /// three rounds that each settle a record, a server answering 429 for
+ /// everything still ends the window at the patience, measured from the
+ /// last round that settled anything.
+ #[test]
+ fn patience_still_ends_a_window_that_stops_making_progress() {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let address = listener.local_addr().unwrap();
+ listener.set_nonblocking(true).unwrap();
+ let stop = Arc::new(AtomicBool::new(false));
+ let server_stop = Arc::clone(&stop);
+ let server = std::thread::spawn(move || {
+ let mut round = 0usize;
+ loop {
+ match listener.accept() {
+ Ok((mut stream, _)) => {
+ stream.set_nonblocking(false).unwrap();
+ let request = read_request(&mut stream);
+ let actions = request_body(&request)
+ .split(|b| *b == b'\n')
+ .filter(|line| !line.is_empty())
+ .count()
+ / 2;
+ round += 1;
+ let mut statuses = vec![429u16; actions];
+ if round <= 3 {
+ statuses[0] = 201;
+ }
+ items_answered(&mut stream, &statuses);
+ }
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ if server_stop.load(Ordering::SeqCst) {
+ return;
+ }
+ std::thread::sleep(Duration::from_millis(2));
+ }
+ Err(error) => panic!("accept: {error}"),
+ }
+ }
+ });
+ let es = client(address, 4).with_throttle_patience(Duration::from_millis(300));
+ let body: Vec<u8> = ["a", "b", "c", "d", "e"]
+ .iter()
+ .flat_map(|id| pair(id))
+ .collect();
+ let started = Instant::now();
+ let outcome = es.bulk(body).unwrap();
+ let elapsed = started.elapsed();
+ stop.store(true, Ordering::SeqCst);
+ server.join().unwrap();
+ assert_eq!(
+ outcome.exhausted_after,
+ Some(Duration::from_millis(300)),
+ "{outcome:?}"
+ );
+ assert_eq!(outcome.throttled_out, 2, "{outcome:?}");
+ assert!(
+ elapsed >= Duration::from_millis(300) && elapsed < Duration::from_secs(3),
+ "stopped at the patience after the last progress, took {elapsed:?}"
+ );
+ }
+
#[test]
fn backpressure_exhausted_names_the_process_memory_cap_once() {
let error = super::backpressure_exhausted(4, Duration::from_secs(90), BREAKER);