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(wal-tap): range-check every [wal_tap] knob at boot, not only in PUT (#320)

xerj-org7ca66f832395
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 08ea34075..c46c5dcfb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -129,14 +129,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
document was already at the target. The field is now
`consecutive_unapplied_polls` and rises by at most one per poll; three
unapplied polls in a row still reports the stall.
- - `wal_tap.max_retry_backoff_secs` is range-checked (`1..=86400`) by `PUT`
- the way `poll_interval_ms` already was, and every multiply in the backoff
- computation saturates. `Config::validate` does not bound the field, so
- `xerj.toml` can put any `u64` there, and `secs.max(1) * 1000` overflowed
- above ~1.8e16: a panic under `cargo test`'s overflow-checks that killed the
- spawned tap task silently, and under a release profile a *wrapped* cap that
- can be arbitrarily small — the value `18_446_744_073_709_552` wrapped to a
- 384 ms ceiling, i.e. a retry storm aimed at an already-failing target.
+ - Every multiply in the retry-backoff computation saturates.
+ `max_retry_backoff_secs.max(1) * 1000` overflowed above ~1.8e16: a panic
+ under `cargo test`'s overflow-checks that killed the spawned tap task
+ silently, and under a release profile a *wrapped* cap that can be
+ arbitrarily small — the value `18_446_744_073_709_552` wrapped to a 384 ms
+ ceiling, i.e. a retry storm aimed at an already-failing target.
+ - **Every `[wal_tap]` numeric knob is range-checked at boot as well as by
+ `PUT`,** through one shared `WalTapConfig::check_limits`. The config file
+ was the way around the API's validation: `PUT
+ {"min_retained_generations": 100}` is a `400` because the knob costs
+ `n × storage.wal_max_size_mb` per WAL shard per index, while `xerj.toml`
+ took the same `100` in silence and the node then held 100 rotated
+ generations per shard forever. Bounds are `poll_interval_ms 50..=60000`,
+ `max_retry_backoff_secs 1..=86400`, `min_retained_generations <= 64`, and
+ `max_batch_docs` / `max_batch_bytes` / `request_timeout_secs` at least 1.
+ Same precedent as `compression.block_size_docs` (#318): an out-of-range
+ value is a typo the operator should hear about at boot, not as a disk-full
+ page later.
- XERJ's own `.xerj*` system indices are never shipped, whatever the allowlist
says, and a wildcard never expands to a hidden index.
- There is no backfill. A new index is shipped whole; an established one ships
diff --git a/engine/crates/xerj-api/src/wal_tap_api.rs b/engine/crates/xerj-api/src/wal_tap_api.rs
index 7f8fb2644..c81f8ac88 100644
--- a/engine/crates/xerj-api/src/wal_tap_api.rs
+++ b/engine/crates/xerj-api/src/wal_tap_api.rs
@@ -160,58 +160,34 @@ pub async fn put_wal_tap(
config.indices = v;
}
if let Some(v) = patch.poll_interval_ms {
- if !(50..=60_000).contains(&v) {
- return bad_request("wal_tap.poll_interval_ms must be between 50 and 60000");
- }
config.poll_interval_ms = v;
}
if let Some(v) = patch.max_batch_docs {
- if v == 0 {
- return bad_request("wal_tap.max_batch_docs must be at least 1");
- }
config.max_batch_docs = v;
}
if let Some(v) = patch.max_batch_bytes {
- if v == 0 {
- return bad_request("wal_tap.max_batch_bytes must be at least 1");
- }
config.max_batch_bytes = v;
}
if let Some(v) = patch.request_timeout_secs {
- if v == 0 {
- return bad_request("wal_tap.request_timeout_secs must be at least 1");
- }
config.request_timeout_secs = v;
}
if let Some(v) = patch.max_retry_backoff_secs {
- // Range-checked like `poll_interval_ms` above, not merely `!= 0`.
- // `WalTap::arm_backoff` now saturates so no value here can overflow
- // it, but a cap of "several thousand years" is not a backoff — it is a
- // tap that stops retrying after its first failed send and reports
- // nothing beyond `last_error`. One day is already far past any
- // recovery window a `_bulk` target has.
- if !(1..=86_400).contains(&v) {
- return bad_request(
- "wal_tap.max_retry_backoff_secs must be between 1 and 86400 (one day)",
- );
- }
config.max_retry_backoff_secs = v;
}
if let Some(v) = patch.min_retained_generations {
- // Bounded on purpose: this knob costs `v * storage.wal_max_size_mb`
- // per WAL shard per index whether or not a tap is running, so an
- // operator typing an extra digit must not be able to fill the disk
- // quietly.
- if v > 64 {
- return bad_request(
- "wal_tap.min_retained_generations must be at most 64: it holds that many \
- rotated WAL files per shard per index, costing up to \
- n * storage.wal_max_size_mb of disk each",
- );
- }
config.min_retained_generations = v;
}
+ // One range check for the numeric knobs, shared verbatim with
+ // `Config::validate` so the config file cannot be used to get a value past
+ // the bound this endpoint enforces. It used to be six inline checks here
+ // and none at all at boot: `PUT {"min_retained_generations": 100}` was a
+ // `400` because the knob costs `n × storage.wal_max_size_mb` per WAL shard
+ // per index, while `xerj.toml` took the same 100 in silence.
+ if let Err(reason) = config.check_limits() {
+ return bad_request(&reason);
+ }
+
if config.enabled && config.target_url.is_empty() {
return bad_request("wal_tap.enabled requires a target_url");
}
diff --git a/engine/crates/xerj-common/src/config.rs b/engine/crates/xerj-common/src/config.rs
index 64911a48e..52fdc3bbe 100644
--- a/engine/crates/xerj-common/src/config.rs
+++ b/engine/crates/xerj-common/src/config.rs
@@ -191,6 +191,21 @@ impl Config {
if let Err(reason) = WalTapConfig::check_target_url(&self.wal_tap.target_url) {
return Err(XerjError::config(reason));
}
+ // …and the numeric knobs, with the same bounds `PUT /_xerj/wal_tap`
+ // enforces. Without this the file was the way *around* the API's
+ // validation: `PUT {"min_retained_generations": 100}` is a `400`
+ // because the knob costs `n × storage.wal_max_size_mb` per WAL shard
+ // per index, but `xerj.toml` took it in silence and the node held 100
+ // rotated generations per shard forever. Same for
+ // `max_retry_backoff_secs`, where a value above `u64::MAX / 1000` used
+ // to reach an unchecked `* 1000` inside `WalTap::arm_backoff`.
+ //
+ // Same reasoning as `compression.block_size_docs` below: an
+ // out-of-range value is a typo the operator wants to hear about now,
+ // at boot, not as a disk-full page later.
+ if let Err(reason) = self.wal_tap.check_limits() {
+ return Err(XerjError::config(reason));
+ }
// Compression: block_size_docs is documented as 16–4096 and was
// accepted at any value (#318 got `999999` past startup in silence).
@@ -1850,6 +1865,62 @@ impl Default for WalTapConfig {
}
impl WalTapConfig {
+ /// Largest `min_retained_generations` this node accepts.
+ ///
+ /// The knob costs `n × storage.wal_max_size_mb` of disk per WAL shard per
+ /// index whether or not a tap is running, so an operator who types an
+ /// extra digit must not be able to fill the disk quietly.
+ pub const MAX_RETAINED_GENERATIONS: u64 = 64;
+ /// Accepted range for `poll_interval_ms`.
+ pub const POLL_INTERVAL_MS_RANGE: std::ops::RangeInclusive<u64> = 50..=60_000;
+ /// Accepted range for `max_retry_backoff_secs` — one day is already far
+ /// past any recovery window a `_bulk` target has, and the upper bound also
+ /// keeps `secs × 1000` nowhere near `u64::MAX`.
+ pub const MAX_RETRY_BACKOFF_SECS_RANGE: std::ops::RangeInclusive<u64> = 1..=86_400;
+
+ /// Range-check every numeric knob, returning the operator message.
+ ///
+ /// Shared by `Config::validate` (the config file, at boot) and
+ /// `PUT /_xerj/wal_tap` (the API), so the file cannot be used to get a
+ /// value past the bound the API enforces.
+ pub fn check_limits(&self) -> Result<(), String> {
+ if !Self::POLL_INTERVAL_MS_RANGE.contains(&self.poll_interval_ms) {
+ return Err(format!(
+ "wal_tap.poll_interval_ms must be between {} and {}, got {}",
+ Self::POLL_INTERVAL_MS_RANGE.start(),
+ Self::POLL_INTERVAL_MS_RANGE.end(),
+ self.poll_interval_ms
+ ));
+ }
+ if !Self::MAX_RETRY_BACKOFF_SECS_RANGE.contains(&self.max_retry_backoff_secs) {
+ return Err(format!(
+ "wal_tap.max_retry_backoff_secs must be between {} and {} (one day), got {}",
+ Self::MAX_RETRY_BACKOFF_SECS_RANGE.start(),
+ Self::MAX_RETRY_BACKOFF_SECS_RANGE.end(),
+ self.max_retry_backoff_secs
+ ));
+ }
+ if self.min_retained_generations > Self::MAX_RETAINED_GENERATIONS {
+ return Err(format!(
+ "wal_tap.min_retained_generations must be at most {}: it holds that many \
+ rotated WAL files per shard per index, costing up to \
+ n * storage.wal_max_size_mb of disk each. Got {}",
+ Self::MAX_RETAINED_GENERATIONS,
+ self.min_retained_generations
+ ));
+ }
+ if self.max_batch_docs == 0 {
+ return Err("wal_tap.max_batch_docs must be at least 1".into());
+ }
+ if self.max_batch_bytes == 0 {
+ return Err("wal_tap.max_batch_bytes must be at least 1".into());
+ }
+ if self.request_timeout_secs == 0 {
+ return Err("wal_tap.request_timeout_secs must be at least 1".into());
+ }
+ Ok(())
+ }
+
/// Reject a `target_url` this node must not accept, returning the operator
/// message for a `400`.
///
@@ -1986,6 +2057,63 @@ mod tests {
.expect("default config should be valid");
}
+ /// #320 — the config file must not be the way around the bounds
+ /// `PUT /_xerj/wal_tap` enforces.
+ ///
+ /// `min_retained_generations` is refused above 64 by the API because the
+ /// knob costs `n × storage.wal_max_size_mb` per WAL shard per index
+ /// whether or not a tap is running; `xerj.toml` took any `u64` in silence
+ /// and the node then held that many rotated generations per shard forever.
+ /// `max_retry_backoff_secs` is the same shape and worse: values above
+ /// `u64::MAX / 1000` used to reach an unchecked `* 1000` inside
+ /// `WalTap::arm_backoff`.
+ ///
+ /// Same precedent as `compression.block_size_docs` (#318): an
+ /// out-of-range value is a typo the operator wants to hear about at boot,
+ /// not as a disk-full page later.
+ #[test]
+ fn wal_tap_numeric_knobs_are_range_checked_in_the_config_file_too() {
+ let bad = [
+ ("min_retained_generations = 100", "min_retained_generations"),
+ ("max_retry_backoff_secs = 0", "max_retry_backoff_secs"),
+ (
+ "max_retry_backoff_secs = 18446744073709552",
+ "max_retry_backoff_secs",
+ ),
+ ("poll_interval_ms = 10", "poll_interval_ms"),
+ ("poll_interval_ms = 60001", "poll_interval_ms"),
+ ("max_batch_docs = 0", "max_batch_docs"),
+ ("max_batch_bytes = 0", "max_batch_bytes"),
+ ("request_timeout_secs = 0", "request_timeout_secs"),
+ ];
+ for (line, field) in bad {
+ // `from_toml_str` validates, so a bad file is refused at load —
+ // the node never boots with it.
+ let toml = format!("[wal_tap]\n{line}\n");
+ let err = Config::from_toml_str(&toml)
+ .err()
+ .unwrap_or_else(|| panic!("[wal_tap] {line} must be refused at boot"));
+ assert!(
+ err.to_string().contains(field),
+ "the error must name the field the operator typed ({field}): {err}"
+ );
+ }
+
+ // The bounds themselves are accepted, so this is a range check and not
+ // an accidental ban.
+ for line in [
+ "min_retained_generations = 64",
+ "max_retry_backoff_secs = 86400",
+ "max_retry_backoff_secs = 1",
+ "poll_interval_ms = 50",
+ "poll_interval_ms = 60000",
+ ] {
+ let toml = format!("[wal_tap]\n{line}\n");
+ Config::from_toml_str(&toml)
+ .unwrap_or_else(|e| panic!("[wal_tap] {line} is in range but was refused: {e}"));
+ }
+ }
+
#[test]
fn parse_minimal_toml() {
let cfg = Config::from_toml_str(