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.

Merge remote-tracking branch 'upstream/main' into fix/cat-format-json-and-data-stream-wildcard

Vincenzo Lombardo68597801d855
diff --cc engine/crates/xerj-api/src/es_compat.rs
index 9e8708d30,3a706c163..bdc201338
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@@ -526,17 -630,17 +652,22 @@@ pub async fn cat_indices
pub async fn cat_indices_pattern(
State(state): State<AppState>,
Path(pattern): Path<String>,
+ Query(params): Query<CatFormatParams>,
) -> impl IntoResponse {
- cat_indices_inner(state, Some(pattern)).await
+ cat_indices_inner(state, Some(pattern), params).await
}
-async fn cat_indices_inner(state: AppState, pattern: Option<String>) -> axum::response::Response {
+async fn cat_indices_inner(
+ state: AppState,
+ pattern: Option<String>,
+ params: CatFormatParams,
+) -> axum::response::Response {
let indices = state.engine.list_indices().await;
+ // Indices whose directory exists but refused to open. They are listed as
+ // `red` rather than omitted: an index the operator can see is an index the
+ // operator can delete or retry, and omitting them is what made a failed
+ // index invisible to every dashboard (issue #206).
+ let failed = state.engine.list_failed_indices();
// Narrow to the path pattern when present (`/_cat/indices/{pattern}`):
// a comma-separated list of concrete names and/or `*` globs. ES rules:
@@@ -593,33 -704,32 +727,75 @@@
));
}
+ // Failed indices, after the healthy ones. `red` is the status column and
+ // the shard is unassigned, so `pri` is 1 and every stat column is 0 —
+ // nothing about the contents can be read, and guessing a doc count here
- // would be a claim we cannot back.
++ // would be a claim we cannot back. Selected via the same pattern rules
++ // as the healthy set above (issue #206: a failed index is still an
++ // index the operator can see and delete, not silently omitted).
+ let selected_failed: Vec<&xerj_engine::engine::FailedIndex> = match pattern.as_deref() {
+ None => failed.iter().collect(),
+ Some(pat) => failed
+ .iter()
+ .filter(|f| {
+ pat.split(',')
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ .any(|p| {
+ p == "_all" || p == "*" || f.name == p || glob_match_simple(p, &f.name)
+ })
+ })
+ .collect(),
+ };
- for f in selected_failed {
- lines.push(format!(
- "red open {} {} 1 0 0 0 0b 0b 0b",
- f.name,
- stable_index_uuid(&f.name),
- ));
++ let failed_rows: Vec<(String, String)> = selected_failed
++ .iter()
++ .map(|f| (f.name.clone(), stable_index_uuid(&f.name)))
++ .collect();
++
+ if params.format.as_deref() == Some("json") {
- let arr: Vec<Value> = rows
++ let mut arr: Vec<Value> = rows
+ .iter()
+ .map(|(name, uuid, doc_count, hsize)| {
+ json!({
+ "health": "green",
+ "status": "open",
+ "index": name,
+ "uuid": uuid,
+ "pri": "1",
+ "rep": "0",
+ "docs.count": doc_count.to_string(),
+ "docs.deleted": "0",
+ "store.size": hsize,
+ "pri.store.size": hsize,
+ })
+ })
+ .collect();
++ arr.extend(failed_rows.iter().map(|(name, uuid)| {
++ json!({
++ "health": "red",
++ "status": "open",
++ "index": name,
++ "uuid": uuid,
++ "pri": "1",
++ "rep": "0",
++ "docs.count": "0",
++ "docs.deleted": "0",
++ "store.size": "0b",
++ "pri.store.size": "0b",
++ })
++ }));
+ return Json(arr).into_response();
}
- let lines: Vec<String> = rows
++ let mut lines: Vec<String> = rows
+ .iter()
+ .map(|(name, uuid, doc_count, hsize)| {
+ format!("green open {name} {uuid} 1 0 {doc_count} 0 {hsize} {hsize} {hsize}")
+ })
+ .collect();
++ for (name, uuid) in &failed_rows {
++ lines.push(format!("red open {name} {uuid} 1 0 0 0 0b 0b 0b"));
++ }
// ES returns an empty body (not a bare newline) when nothing matches.
let body = if lines.is_empty() {
String::new()
@@@ -15990,10 -16097,15 +16239,18 @@@ async fn cat_ann_inner
// GET /_cat/health
// ─────────────────────────────────────────────────────────────────────────────
+ /// `GET /_cat/health`.
+ ///
+ /// The status column used to be the literal string `green`, so the one
+ /// endpoint an operator's existing dashboard points at was the one endpoint
+ /// that could not report a broken node — `/v1/health`, `/v1/cluster/health`
+ /// and `/health/ready` all knew, and `_cat/health` said everything was fine
+ /// (issue #206). It now reports the same status `_cluster/health` computes,
+ /// from the same inputs.
-pub async fn cat_health(State(state): State<AppState>) -> impl IntoResponse {
+pub async fn cat_health(
+ State(state): State<AppState>,
+ Query(params): Query<CatFormatParams>,
+) -> impl IntoResponse {
let health = state.engine.health().await;
// epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
let epoch = std::time::SystemTime::now()
@@@ -16003,28 -16115,21 +16260,42 @@@
let now = Utc::now();
let ts = now.format("%H:%M:%S").to_string();
let shards = health.index_count as u32;
+ let unassigned = state.engine.list_failed_indices().len() as u32;
+ // Mirror `_cluster/health`: an unopenable index is an unassigned primary
+ // (red); an unassigned replica is yellow. The engine's own "yellow" means
+ // "unflushed memtable", which is a durability nuance rather than an ES
+ // shard state, so it deliberately does not colour this column.
+ let status = if unassigned > 0 { "red" } else { "green" };
+ let total = shards + unassigned;
+ let pct = if total == 0 {
+ 100.0
+ } else {
+ (shards as f64) / (total as f64) * 100.0
+ };
+
+ if params.format.as_deref() == Some("json") {
+ return Json(vec![json!({
+ "epoch": epoch.to_string(),
+ "timestamp": ts,
+ "cluster": "xerj",
- "status": "green",
++ "status": status,
+ "node.total": "1",
+ "node.data": "1",
+ "shards": shards.to_string(),
+ "pri": shards.to_string(),
+ "relo": "0",
+ "init": "0",
- "unassign": "0",
++ "unassign": unassigned.to_string(),
+ "pending_tasks": "0",
+ "max_task_wait_time": "-",
- "active_shards_percent": "100.0%",
++ "active_shards_percent": format!("{pct:.1}%"),
+ })])
+ .into_response();
+ }
+
- let body = format!("{epoch} {ts} xerj green 1 1 {shards} {shards} 0 0 0 0 - 100.0%\n");
+ let body = format!(
+ "{epoch} {ts} xerj {status} 1 1 {shards} {shards} 0 0 {unassigned} 0 - {pct:.1}%\n"
+ );
(
StatusCode::OK,
[(
@@@ -21451,34 -21498,17 +21722,58 @@@ pub async fn cat_shards
.get_index(&info.name)
.map(|idx| dir_size_bytes(idx.data_dir()))
.unwrap_or(0);
- lines.push(format!(
- "{} 0 p STARTED {} {}b 127.0.0.1 xerj-node-1",
- info.name, info.doc_count, store_bytes,
- ));
+ rows.push((info.name.clone(), info.doc_count, store_bytes));
}
+ // `_cat/shards` is the call an operator makes straight after seeing a red
+ // cluster, so an index whose primary could not be opened has to be in it.
+ // ES prints UNASSIGNED rows with no node and empty stats.
- for f in state.engine.list_failed_indices() {
- lines.push(format!("{} 0 p UNASSIGNED 0 0b - -", f.name));
++ let failed_names: Vec<String> = state
++ .engine
++ .list_failed_indices()
++ .into_iter()
++ .map(|f| f.name)
++ .collect();
+
+ if params.format.as_deref() == Some("json") {
- let arr: Vec<Value> = rows
++ let mut arr: Vec<Value> = rows
+ .iter()
+ .map(|(name, docs, store_bytes)| {
+ json!({
+ "index": name,
+ "shard": "0",
+ "prirep": "p",
+ "state": "STARTED",
+ "docs": docs.to_string(),
+ "store": format!("{store_bytes}b"),
+ "ip": "127.0.0.1",
+ "node": "xerj-node-1",
+ })
+ })
+ .collect();
++ arr.extend(failed_names.iter().map(|name| {
++ json!({
++ "index": name,
++ "shard": "0",
++ "prirep": "p",
++ "state": "UNASSIGNED",
++ "docs": "0",
++ "store": "0b",
++ "ip": "-",
++ "node": "-",
++ })
++ }));
+ return Json(arr).into_response();
+ }
+
- let lines: Vec<String> = rows
++ let mut lines: Vec<String> = rows
+ .iter()
+ .map(|(name, docs, store_bytes)| {
+ format!("{name} 0 p STARTED {docs} {store_bytes}b 127.0.0.1 xerj-node-1")
+ })
+ .collect();
++ for name in &failed_names {
++ lines.push(format!("{name} 0 p UNASSIGNED 0 0b - -"));
+ }
let body = if lines.is_empty() {
String::new()
} else {