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.

docs(#202): correct the claims this change makes, and add the CHANGELOG entry

xerj-org2d8d5c1d6106
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9160e83f7..623d09861 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,73 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- **An index whose metadata will not parse is refused instead of silently
+ reopened with an empty mapping**
+ ([#202](https://github.com/xerj-org/xerj/issues/202)). `Index::open` treated
+ "this file is not there" and "this file does not parse" as the same thing:
+ `load_schema`/`load_settings` mapped ENOENT, EACCES, EIO and malformed JSON
+ alike to one anonymous error, and the caller answered all of them with a
+ fresh dynamic mapping. Measured on a field deliberately mapped `keyword`:
+ after `schema.json` was truncated to half its bytes the index still opened
+ `Ok` with `field_count = 0`, the field came back `None`, and one further
+ document re-inferred it as `long` — a mapping silently replaced by a
+ different one, with nothing in any response saying so. Absent stays absent
+ (indices predating create-time schema persistence legitimately have no
+ `schema.json`); present-but-unparseable now fails the open with an error
+ naming the file. `es_mapping.json` — the full-fidelity mapping behind
+ `GET /_mapping` — was logged-and-ignored on a parse failure and now fails the
+ index too, on the boot scan, on snapshot restore and on a retry.
+
+ **Visible on upgrade:** a node whose data dir already holds a corrupt sidecar
+ now boots **red** with that index unserved, where it previously came up green
+ with an empty mapping. It joins the failed set
+ [#206](https://github.com/xerj-org/xerj/issues/206) introduced, so every
+ surface that set feeds already reports it — measured on a two-index node with
+ one truncated `schema.json`:
+
+ ```
+ GET /_cluster/health -> red, unassigned_primary_shards: 1
+ GET /_cluster/health?level=indices
+ -> victim red, unassigned_info.details = the
+ absolute path of schema.json and the parse error
+ GET /_cat/indices -> "red open victim … 0 0 0b 0b 0b"
+ GET /_cluster/indices/failed -> the reason, plus the retry and delete calls
+ PUT /victim, POST /victim/_doc, POST /victim/_search
+ -> 503 no_shard_available_action_exception
+ carrying that same reason
+ GET /health/ready -> 200 "ready (degraded): 1 of 2 indices
+ serving, 1 failed to open"
+ POST /healthy/_search -> 200
+ ```
+
+ Recovery is the three doors #206 added, both verified end to end here: repair
+ the file and `POST /_cluster/indices/failed/{index}/_retry` (503 while it is
+ still torn, `200 {"reopened": true}` once it is not, health back to green), or
+ restore the index from a snapshot, or `DELETE /{index}`. The node stays in
+ kubelet rotation as long as any index is still serving.
+
+- **Concurrent sidecar writes can no longer manufacture the torn file the reader
+ now refuses.** `write_file_atomic` staged every write in
+ `path.with_extension("tmp")` — one shared name for all writers of that file —
+ so two concurrent writers of one sidecar (`PUT /{index}/_settings` racing
+ another settings update, two API-key mints for `api_keys.json`) both opened it
+ `O_TRUNC`, interleaved their bytes, and each renamed it into place. The rename
+ is atomic; the content being renamed was not one writer's. Measured with the
+ old shared name, 4 threads × two different-length settings bodies × 200
+ rounds, over four runs: **86–294 of 800 writes failed with ENOENT** (the loser
+ renaming a file the winner had already moved) and, with those errors swallowed
+ as the callers did, **1–16 of 200 rounds left an unparseable `settings.json`**.
+ The counts are race-dependent and vary with machine load; what does not vary is
+ that both are non-zero on every run with the shared name and **0 and 0** on
+ every run with a unique staging name. Each write now stages
+ in its own sibling file (`<file>.tmp.<pid>.<seq>`) in the target's directory
+ and removes its own debris on failure — which for `api_keys.json` is key
+ material. `update_settings` also writes `settings.json` through
+ `write_file_atomic` rather than a plain `fs::write`; it was the last
+ non-atomic sidecar writer left.
+
## [1.0.0-rc.14] - 2026-08-10
### Changed
diff --git a/engine/crates/xerj-api/tests/corrupt_index_http.rs b/engine/crates/xerj-api/tests/corrupt_index_http.rs
index 4608c0cd8..5c615592c 100644
--- a/engine/crates/xerj-api/tests/corrupt_index_http.rs
+++ b/engine/crates/xerj-api/tests/corrupt_index_http.rs
@@ -1,31 +1,27 @@
-//! Issue #202, from the outside — the recovery story for a failed index has to
-//! exist on the surface operators actually use.
+//! Issue #202, from the outside — a torn `schema.json` has to reach the
+//! recovery surfaces, not just the engine.
//!
//! Refusing to open an index whose metadata is unparseable is only useful if
//! the ES-compat API (`:9200`, the port every ES client, Kibana and monitoring
//! dashboard talks to) then tells the truth about it and offers a way out. The
//! engine-level tests in `xerj-engine/tests/corrupt_index_metadata.rs` call
//! `engine.delete_index` / `engine.health()` directly and therefore cannot see
-//! the HTTP layer at all. Measured over `build_es_compat_router` with one torn
-//! `schema.json`, that layer was wrong in three ways:
+//! the HTTP layer at all.
//!
-//! 1. `DELETE /{index}` answered `404 index_not_found_exception` and left the
-//! directory on disk — the handler resolves names from
-//! `engine.list_indices()`, which iterates `indices` only, so a failed index
-//! never reaches `engine.delete_index`. Combined with the create gate the
-//! name was unopenable, uncreatable *and* undeletable on `:9200`.
-//! 2. `DELETE /_all` answered `200 {"acknowledged": true}` while the corrupt
-//! index survived — the wildcard branch expands over the same list.
-//! 3. `GET /_cluster/health` answered `status: "green"` while
-//! `engine.health()` answered `"red"`, because it too is built from
-//! `list_indices()`.
+//! **The HTTP behaviour asserted here is not this change's work.** Issue #206
+//! built it — failed indices in `_cat/indices` and `_cluster/state`, a red
+//! `_cluster/health` that names them, `DELETE` reaching a failed index through
+//! literal, `_all` and wildcard forms, `GET /_cluster/indices/failed`, the
+//! retry endpoint, and a readiness probe scoped to "this node has nothing to
+//! serve" rather than "cluster is red". What #202 adds is a *new way in*: an
+//! unparseable sidecar is now one of the conditions that puts an index in that
+//! failed set. This file pins the join between the two, so a change to either
+//! side that stops a corrupt-metadata index from being visible, deletable and
+//! survivable on `:9200` fails here rather than in production.
//!
-//! And one consequence that had to be decided rather than inherited: readiness
-//! was `engine.health() == "red"`, which after #202 means "any index failed",
-//! so a single unparseable sidecar would have taken the whole node out of
-//! kubelet rotation permanently while its other indices kept serving. Readiness
-//! is a traffic gate, not an alarm channel, so it is now scoped to "this node
-//! has nothing to serve". Both halves of that are pinned below.
+//! Every case below quarantines an index by truncating its `schema.json` —
+//! the #202 trigger — and then asserts the #206 contract over
+//! `build_es_compat_router`.
use axum::body::Body;
use axum::http::{Request, StatusCode};
@@ -107,11 +103,12 @@ async fn delete(app: &axum::Router, path: &str) -> (StatusCode, Value) {
.await
}
-// ── must-fix 1: DELETE /{index} is the documented escape hatch ────────────────
+// ── DELETE /{index} is the documented escape hatch ───────────────────────────
-/// The PR body's recovery story ("restore the file, restore from snapshot, or
-/// DELETE the index") has to hold on the ES-compat port. Pre-fix this answered
-/// 404 and left the directory on disk.
+/// The recovery story for a corrupt sidecar ("restore the file and retry,
+/// restore from a snapshot, or DELETE the index") has to hold on the ES-compat
+/// port for an index quarantined by #202, not only for the #206 triggers the
+/// delete path was built against.
#[tokio::test]
async fn delete_removes_a_failed_index_over_the_es_api() {
let dir = tempfile::tempdir().unwrap();
@@ -172,11 +169,11 @@ async fn health_recovers_after_the_failed_index_is_deleted() {
assert!(dir.path().join("healthy").exists());
}
-// ── must-fix 2: DELETE /_all must not acknowledge a no-op ─────────────────────
+// ── DELETE /_all must not acknowledge a no-op ────────────────────────────────
-/// `DELETE /_all` expanded only over `list_indices()`, so it answered
-/// `200 {"acknowledged": true}` with the corrupt index still on disk — the
-/// accepted-and-ignored class tracked in #204.
+/// A wildcard expansion that skipped the failed index would answer
+/// `200 {"acknowledged": true}` with the corrupt directory still on disk — the
+/// accepted-and-ignored class tracked in #204. Pinned for a #202 failure.
#[tokio::test]
async fn delete_all_actually_removes_a_failed_index() {
let dir = tempfile::tempdir().unwrap();
@@ -216,11 +213,12 @@ async fn delete_wildcard_resolves_a_failed_index() {
);
}
-// ── must-fix 3: GET /_cluster/health is the surface monitoring polls ──────────
+// ── GET /_cluster/health is the surface monitoring polls ─────────────────────
-/// The PR's headline claim is "the operator sees red". `engine.health()` said
-/// red; `GET /_cluster/health` — what every ES client and dashboard actually
-/// polls — said green, because it is built from `list_indices()`.
+/// #202's user-visible promise is "the operator sees red". `engine.health()`
+/// answering red is not enough: the claim only holds if `GET /_cluster/health`
+/// — what every ES client and dashboard actually polls — reports it too, for an
+/// index that failed on a torn sidecar.
#[tokio::test]
async fn cluster_health_reports_red_for_a_failed_index() {
let dir = tempfile::tempdir().unwrap();
@@ -297,15 +295,17 @@ async fn per_index_health_is_scoped_to_the_selector() {
);
}
-// ── must-fix 4: readiness is a traffic gate, not an alarm channel ────────────
+// ── readiness is a traffic gate, not an alarm channel ───────────────────────
/// `native::readiness` is mounted on BOTH routers (`router.rs:113` and
-/// `router.rs:225`) and used to be `health() == "red"`. After #202 red means
-/// "any index failed", which is permanent — so one unparseable sidecar out of
-/// N indices would have removed the pod from kubelet rotation for good while
-/// the other N-1 answered perfectly, and would have bricked `xerj brain`, which
-/// gates on this probe. A node that is still serving must stay in rotation; the
-/// alarm belongs on `_cluster/health`, asserted here alongside it.
+/// `router.rs:225`) and was rescoped by #206 away from `health() == "red"`.
+/// #202 is what makes that rescope load-bearing: red now also means "an index
+/// has an unparseable sidecar", which is permanent, so under the old predicate
+/// one torn `schema.json` out of N indices would have removed the pod from
+/// kubelet rotation for good while the other N-1 answered perfectly — and would
+/// have bricked `xerj brain`, which gates on this probe. A node that is still
+/// serving must stay in rotation; the alarm belongs on `_cluster/health`,
+/// asserted here alongside it.
#[tokio::test]
async fn a_partially_failed_node_stays_in_rotation_and_keeps_serving() {
let dir = tempfile::tempdir().unwrap();
@@ -376,9 +376,10 @@ async fn an_empty_node_is_ready() {
// ── the create gate, over HTTP ───────────────────────────────────────────────
-/// `PUT /{index}` and `_bulk` auto-create used to run `Index::create` over the
-/// corrupt directory. They are now refused — and the refusal must name the file
-/// and the way out rather than being an opaque 500.
+/// `PUT /{index}` and `_bulk` auto-create used to run `Index::create` over a
+/// corrupt directory, overwriting `schema.json` with an empty mapping and
+/// destroying the evidence. They are refused — and the refusal must name the
+/// file and the way out rather than being an opaque 500.
#[tokio::test]
async fn create_over_a_failed_index_is_refused_over_http() {
let dir = tempfile::tempdir().unwrap();
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index bc6e64cc5..dec68c149 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -25473,12 +25473,13 @@ static TMP_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new
/// every writer of that file, so two concurrent `write_file_atomic` calls on one
/// sidecar both opened it `O_TRUNC` and interleaved their bytes before either
/// renamed. Measured on this tree with the old shared name, 4 threads writing
-/// two different-length settings bodies for 200 rounds: 294 of the 800 writes
-/// failed outright with ENOENT (the loser renaming a file the winner had
-/// already moved), and with those errors swallowed — which is what the callers
-/// did — 16 of the 200 rounds left a `settings.json` that does not parse. That
-/// is exactly the file `Index::open` now refuses (#202), so the writer must not
-/// be able to manufacture it.
+/// two different-length settings bodies for 200 rounds, four runs: 86–294 of
+/// the 800 writes failed outright with ENOENT (the loser renaming a file the
+/// winner had already moved), and with those errors swallowed — which is what
+/// the callers did — 1–16 of the 200 rounds left a `settings.json` that does
+/// not parse. The counts move with machine load; both being non-zero on every
+/// run does not. That torn file is exactly what `Index::open` now refuses
+/// (#202), so the writer must not be able to manufacture it.
///
/// Prior art: tantivy stages atomic writes in a *unique* temp file created in
/// the destination's own directory and then persists it over the target
diff --git a/engine/crates/xerj-engine/tests/corrupt_index_metadata.rs b/engine/crates/xerj-engine/tests/corrupt_index_metadata.rs
index 3dca1da44..aa2a45b7e 100644
--- a/engine/crates/xerj-engine/tests/corrupt_index_metadata.rs
+++ b/engine/crates/xerj-engine/tests/corrupt_index_metadata.rs
@@ -336,11 +336,13 @@ async fn a_failed_index_can_still_be_deleted() {
/// all writers of that path. Two concurrent settings writes (`PUT /_settings`
/// racing another `PUT /_settings`, or an `index.blocks` update) therefore both
/// opened it `O_TRUNC` and interleaved their bytes before either renamed, so
-/// the *complete* file that landed was a mix of two bodies. Pre-fix, this test
-/// reports 294 of 800 writes failing with ENOENT (the loser renaming a file the
-/// winner had already moved) and — with those errors swallowed, which is what
-/// the callers do — 16 of 200 rounds leaving an unparseable file. After the
-/// fix: no failed writes, no torn file, no debris.
+/// the *complete* file that landed was a mix of two bodies. Run against the old
+/// shared name (four runs, two boxes) this loop reports 86–294 of 800 writes
+/// failing with ENOENT — the loser renaming a file the winner had already moved
+/// — and, with those errors swallowed as the callers do, 1–16 of 200 rounds
+/// leaving an unparseable file. The counts are race-dependent; both being
+/// non-zero is not. After the fix: no failed writes, no torn file, no debris,
+/// which is what the assertions below pin.
///
/// Before #202 that torn file was "only" silently swapped for an empty mapping.
/// Now it refuses the open, so a lost race would brick the index — which is why