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.

8.1 KB
Raw Blame

Autotuner — zero-config perf via on-host measurement

Date: 2026-04-25 Status: design notes (not yet implemented) Origin: Pain point exposed by the 2026-04-25 ingest perf regression investigation — the engine has 6+ knobs (flush_size_mb, wal_max_size_mb, ingest_shards, flush_workers, batch size, zstd level…) and the wrong combination cuts sustained ingest from 1.55 M docs/s to 21 K docs/s. "Tune your config" defeats the entire pitch: "no JVM, no tuning."

Pitch

xerj --autotune runs a 30-90 s self-calibration once on first boot (or on demand), measures the host's CPU, memory, disk-I/O, and codec throughput, and writes a xerj.autotuned.toml next to the data dir. Subsequent boots load it and skip the calibration. The user never opens a tuning guide. The product looks like SQLite, not Postgres.

The pitch line for the README:

xerj finds its own settings. No jvm.options. No elasticsearch.yml. No "tune your shard count." Run xerj --autotune once, get the same 1 M+ docs/s on a laptop and on a 64-core box.

What gets measured

Probe Output Used to set
CPU cores (logical, physical, avx2/avx512) n_cpu ingest_shards, flush_workers
L3 cache size (/proc/cpuinfo) l3_kb batch_bytes (so a batch fits in L3)
Available RAM (/proc/meminfo) ram_gb flush_size_mb, wal_max_size_mb, memtable_cap
Free disk on data_dir disk_gb wal_max_size_mb, retention defaults
Disk write throughput (5 s sequential write to data_dir) disk_write_mb_s upper bound on sustained ingest, governs flush sizing
Disk fsync latency (10 × 4 KB fsync) fsync_us WAL group-commit window
Zstd encode throughput (1 MB random text @ levels 1, 3, 6, 9, 19) zstd_mb_s_l3, zstd_mb_s_l9, … Pick the highest level whose throughput per core × flush_workers ≥ disk_write_mb_s
Memcpy bandwidth (1 GB stream copy) memcpy_gb_s sanity bound; flag if memory channel is contended
Wall-clock burst ingest (10 s, 50 K-doc batches, default config) burst_docs_s cross-check that the predicted config actually delivers

The whole probe should fit in ~30 seconds. Heavier verification (see "Adaptive mode" below) takes longer when the user opts in.

What gets set

# Auto-generated by xerj --autotune on 2026-04-25T22:00:00Z
# Host: 32 logical cores, 119 GB RAM, NVMe (1.2 GB/s sequential)
# Re-run `xerj --autotune` if hardware changes.

[engine]
ingest_shards   = 32          # = n_cpu
flush_workers   = 8           # = n_cpu / 4 (leaves CPU for ingest)
batch_bytes     = 4_194_304   # 4 MiB — fits in L3 with headroom
memtable_cap_mb = 4096        # = ram_gb × 0.05 (cap at 5 % of RAM)

[storage]
flush_size_mb        = 1024   # ≈ 1 s of disk_write at this host's NVMe
flush_zstd_level     = 3      # picked via probe
merge_zstd_level     = 19     # merge is out-of-band, fine to be slow
wal_max_size_mb      = 4096   # = 4 × flush_size_mb (rotation cadence)
wal_group_commit_us  = 200    # = max(100, fsync_us)

The choice rules are explicit, auditable, and shown in --autotune --explain:

$ xerj --autotune --explain
[autotune] CPU      : 32 logical / 16 physical cores, AVX2  → ingest_shards=32, flush_workers=8
[autotune] RAM      : 119 GB available                        → memtable_cap=5GB (5% of RAM)
[autotune] Disk     : NVMe @ 1.18 GB/s seq write              → flush_size=1GB (1s of writes)
[autotune] fsync    : p99=180us                               → wal_group_commit=200us
[autotune] zstd     : L3=265 MB/s, L9=49, L19=22 MB/s/core    → flush=L3 (need 1.18 GB/s drain × 8 workers)
                                                              → merge=L19 (out-of-band, fine)
[autotune] burst    : 50K-batch sustained at 1.42 M docs/s    → config validates
[autotune] wrote    : /var/lib/xerj/xerj.autotuned.toml

Picking zstd level — the rule

This is the rule that would have prevented the 2026-04-25 regression without anyone needing to know about it:

required_drain_mb_s = disk_write_mb_s
encode_budget_per_worker = required_drain_mb_s / flush_workers
flush_zstd_level = highest level L such that
    zstd_encode_mb_s(L) >= encode_budget_per_worker × 1.5  # 1.5× safety margin

On the test box: 1180 / 8 = 148 MB/s/worker. zstd-3 measures at ~265 MB/s/core → comfortably > 148 × 1.5 = 222. zstd-9 at ~49 MB/s/core fails the safety margin. zstd-19 at ~22 fails by 7×. → autotune picks level 3 automatically. Level 19 is the merge-time setting, paid out-of-band where slow encode is fine.

Adaptive mode (opt-in)

The 30-second probe is fast but not exhaustive. For users who want the absolute peak (and have 5-10 minutes), --autotune --deep:

  1. Start with the heuristic config (above).
  2. Synthesise a 1 GB NDJSON corpus on tmpfs (or use a user-provided sample file).
  3. Run xerj index against it, measure sustained docs/s, p99 batch latency, RSS peak, error count.
  4. Bisect on flush_size_mb (halve / double) until docs/s plateaus.
  5. Bisect on flush_workers (halve / double) until p99 latency stops improving.
  6. Bisect on batch_bytes (halve / double) until docs/s peaks.
  7. Write the resulting config + a JSON report of all the runs.

This is essentially what an SE does manually today, but the engine does it for them. Total wall time: 5-10 minutes. Output: a config that beats anything a human would write because it tried 30+ combos.

Continuous tuning (v1+)

Phase 3 — the engine watches itself in production and adjusts:

  • If index_batch p99 latency > 100 ms for 5 minutes → grow memtable_cap or flush_workers (within RAM/CPU budget).
  • If flush p99 > 5 s for 5 minutes → grow flush_size_mb (more docs per flush amortises encode CPU) or drop flush_zstd_level.
  • If disk_free < 20 % → tighten retention or grow merge_zstd_level (trade CPU for disk; merge is out-of-band).
  • All adjustments logged to _audit/autotune.jsonl so an operator can roll back.

What this avoids

The competitor surface area we're explicitly NOT competing with:

  • Elasticsearch: requires a dedicated SRE who understands jvm.options, elasticsearch.yml, shard sizing, heap limits, circuit-breaker thresholds. ES has 3,000+ settings.
  • OpenSearch: same surface, plus AWS-specific deployment patterns.
  • ClickHouse: requires understanding of MergeTree settings, primary-key cardinality, ZooKeeper quorum.

xerj's pitch is "single binary, no config." Autotune is the pitch.

Implementation plan

v0.7.x — heuristic autotune

  • New flag: xerj --autotune (also runs implicitly on first boot if xerj.autotuned.toml is absent and data_dir is empty).
  • Probe module under crates/xerj-server/src/autotune/ (hardware.rs, zstd.rs, disk.rs, apply.rs).
  • Choice rules in plain Rust — no ML, no policy DSL. Auditable.
  • Writes xerj.autotuned.toml to data_dir parent, layered under any user --config (user wins on every key).

v0.8 — deep autotune

  • Synthesise corpus, run end-to-end ingest probe, bisect knobs.
  • Reuses the existing CLI ingest path (xerj index).
  • Reports per-run metrics + the chosen config to data_dir/_autotune/<timestamp>/.

v1 — continuous

  • New per-index metric: tuning_pressure (rolling p99 of time_in_back_pressure_ms).
  • A tuning_loop background task adjusts knobs within configured caps, emits audit events.
  • Disabled by default; opt-in via [tuning] enabled = true.

Files (when implemented)

  • crates/xerj-server/src/autotune/mod.rs
  • crates/xerj-server/src/autotune/probe.rs
  • crates/xerj-server/src/autotune/zstd_bench.rs
  • crates/xerj-server/src/autotune/disk_bench.rs
  • crates/xerj-server/src/autotune/apply.rs

Cross-references

  • 2026-04-25T21-50-00_ingest_perf_regression_zstd19.md — the case study that motivates this. The Zstd-19 regression would have been caught at autotune time because the encode-throughput probe would have eliminated level 19 on this host's disk-throughput budget.
  • releases/v0.1.0/reports/PATH_TO_100_PCT_v0.6.0_to_v1.0.md — slot this into the v0.7.x → v0.8 milestones as P1 (gates the "no manual tuning" claim in the README).