Parcourir la source

docs: prod database OOM crash-loop - a headroom measurement with no re-measurement trigger

The hourly seal snapshot outgrew the box through ordinary data growth, so
the database was OOM-killed once an hour from 2026-10-17 00:37 and could
not boot at all by the 19th. Restored by adding 4 GB of swap.

Labels the 2026-10-12 'the snapshot survives every hour' measurement as
superseded rather than rewriting it - it was accurate when taken, and the
mechanism it describes is why it stopped holding.

Three lessons kept: an OOM kill point is a LOWER BOUND on the requirement
and never the requirement; a crash that restarts cleanly is a silent
crash, so NRestarts distinguishes 'up' from 'not failing'; and a headroom
measurement needs a re-measurement trigger tied to a quantity that moves,
not just a date.
fszontagh il y a 1 mois
Parent
commit
7cc1c5e656
1 fichiers modifiés avec 15 ajouts et 3 suppressions
  1. 15 3
      CLAUDE.md

+ 15 - 3
CLAUDE.md

@@ -212,7 +212,7 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
 - **DB v2.3.1: `dropProject` does not physically purge collection data.** A recycled project name is otherwise blocked by stale collections/registry → the project-delete handler drops the registry + collections first, and collection-create drops invisible orphans (after the registry "exists" check).
 - **WE strip `id` from a document body, not the database client.** This note used to say the client did it, and that is measured as false on client 2.12.1: inserting `{"id":"body-id-value"}` with an explicit document id stores the body field verbatim and it reads back alongside the real `_id` metadata. The stripping is ours, in `documentBody()` (`documents_util.hpp`), which erases `id` and then calls `stripDbMetadata()` to drop the six reserved keys. Keep doing it: the database stores a reserved key as an ordinary user field while every read overwrites it with the real metadata, so a body carrying `_version` would put an invisible shadow copy on disk that no read ever shows and no filter can reach. Our layer is what keeps stored documents clean (the database is fixing this server-side in 2.13.0, for every other client). API keys store their stable id as `key_id` in the body (exposed as `id` externally) - that predates this and the stored rows are not worth migrating, but it was never forced by the client.
 - **Debian 13 (trixie) gRPC cmake config is broken** unless `protobuf-compiler-grpc` + `libgrpc++-dev` + `libprotobuf-dev` are installed - `find_package(gRPC CONFIG)` (used transitively by `smartbotic-db-clientConfig.cmake`) fails on a `gRPCTargets.cmake` referencing missing plugin files. The Docker base installs them.
-- **THE 2.17.x MIGRATION'S MEMORY PEAK SCALES WITH THE DATASET, AND IT OOM-KILLS. Measure it against the box you are deploying to, on the data you are deploying over.** Measured on 2.17.2 with the PUBLISHED debs in a Debian 13 container (not an upstream image - prod installs the deb), prod's own config, over a copy of prod's data directory: **204 s, cgroup `memory.peak` 1092 MiB, process `VmHWM` 1212 MiB**, ending `CLEAN` / `sealed: yes`. Under a **900 MiB cap it was OOM-KILLED** during compaction. Prod is **1973 MB with NO SWAP**, and the running database holds ~757 MB RSS which is freed when it restarts, so the margin is real but not generous.
+- **THE 2.17.x MIGRATION'S MEMORY PEAK SCALES WITH THE DATASET, AND IT OOM-KILLS. Measure it against the box you are deploying to, on the data you are deploying over.** Measured on 2.17.2 with the PUBLISHED debs in a Debian 13 container (not an upstream image - prod installs the deb), prod's own config, over a copy of prod's data directory: **204 s, cgroup `memory.peak` 1092 MiB, process `VmHWM` 1212 MiB**, ending `CLEAN` / `sealed: yes`. Under a **900 MiB cap it was OOM-KILLED** during compaction. Prod WAS **1973 MB with NO SWAP** when this was measured; it now has a 4 GB `/swapfile` (added 2026-10-19, see the crash-loop note below), and the running database holds ~757 MB RSS which is freed when it restarts. The margin was real but not generous, and it ran out.
   **The same run on 2.17.1 OOM-kills identically, so this is NOT a version regression - it is the DATA.** Friday's copy held 24733 rows and peaked at 364 MiB anonymous; today's held 33321 rows, 35% more, and peaks near 1.1 GB. **A migration memory figure is therefore a measurement of one corpus on one day, exactly like a search baseline**, and the earlier comfortable number would have been carried straight into a window had the second run not been made. An OOM is RECOVERABLE - the migration re-runs at the next boot and skips completed work - so the failure mode is a longer outage rather than lost credentials, which is worth telling consumers when asking for the window.
 - **THE MIGRATION'S ANONYMOUS PEAK IS THE 5-SECOND SEAL SNAPSHOT, NOT THE MINUTES OF COMPACTION - so size the box against the STORE, not against the biggest env.** Measured upstream by phase, sampling `memory.stat` every second against the log (22.5k docs, a 626 MB and a 262 MB env):
 
@@ -224,8 +224,20 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
 
   The snapshot serialises the whole in-memory store into one buffer, compresses into a second, writes, then verifies by reading back and fully decompressing - so the peak is roughly **working set + one serialised copy of the store**, about +400 MB there. **Compaction never raised anonymous memory at all**, even on a 626 MB env, because `mdb_env_copy2` streams the mmapped env with small buffers; its cost is reclaimable page cache. Two consequences that inverted our planning: a large ENV does not threaten memory (so dropping a big collection would not have helped), and the thing that CAN kill the boot is identical work on every attempt, so under a cap it cannot fit it **loops at the same point** rather than retrying into eventual success. Progress is not persisted per project either.
   **Our own 1092 MiB figure was a cgroup peak and therefore INCLUDED page cache; only the anonymous part has to fit.** Quoting it as the memory requirement overstates it - the same measure-the-wrong-quantity error as reading `VmHWM` and forgetting the LMDB mmap is file-backed.
-  **MEASURED ON PROD, 2026-10-12: its own hourly snapshot peaks at 1051 MiB RSS against a 743 MiB steady state (+307 MB), and it survives that EVERY HOUR with vectorapi still running.** Sampled at 1 Hz across the 13:30 UTC snapshot: 761 MB steady, 950 at 13:30:19, 1034 at 13:30:20, 1051 at 13:30:23, with "Created snapshot at sequence 206689" logged at 13:30:25. Box is 1973 MB, other services ~264 MB (journald 110, qdrant 82, dockerd 54, nginx 18), so peak plus others is ~1315 MB and the margin is **~658 MB** with vectorapi stopped and the old process's memory already freed. **So the seal snapshot fits, and the strongest part of that evidence is not the arithmetic - it is that the same code path on the same store already runs hourly and survives.** It also fully explains the container OOM at a 900 MiB cap: the cgroup counted anon plus page cache, and ~724 MiB of steady-state total plus a ~307 MB snapshot spike simply exceeds 900.
-  **The safest pre-flight number is the running server's own periodic snapshot.** `snapshot_interval_sec` is 3600 on prod, so it performs this exact serialise-compress-verify hourly and survives it; sampling its RSS across one ("Created snapshot at sequence ...") gives the peak the first boot has to reproduce, on the same store, same code and same config. A number from the thing itself beats any number from a container.
+  **SUPERSEDED BY GROWTH, 2026-10-19 - the measurement below was ACCURATE when taken and the claim it supports is now FALSE. Read the crash-loop note that follows before using any number here.** The headroom was real on 2026-10-12 and the store outgrew it; the mechanism this paragraph describes is still correct, which is exactly why it stopped fitting.
+  **MEASURED ON PROD, 2026-10-12: its own hourly snapshot peaks at 1051 MiB RSS against a 743 MiB steady state (+307 MB), and it survived that EVERY HOUR with vectorapi still running.** Sampled at 1 Hz across the 13:30 UTC snapshot: 761 MB steady, 950 at 13:30:19, 1034 at 13:30:20, 1051 at 13:30:23, with "Created snapshot at sequence 206689" logged at 13:30:25. Box is 1973 MB, other services ~264 MB (journald 110, qdrant 82, dockerd 54, nginx 18), so peak plus others is ~1315 MB and the margin is **~658 MB** with vectorapi stopped and the old process's memory already freed. **So the seal snapshot fits, and the strongest part of that evidence is not the arithmetic - it is that the same code path on the same store already runs hourly and survives.** It also fully explains the container OOM at a 900 MiB cap: the cgroup counted anon plus page cache, and ~724 MiB of steady-state total plus a ~307 MB snapshot spike simply exceeds 900.
+  **The safest pre-flight number is the running server's own periodic snapshot - but it is a MEASUREMENT WITH A SHELF LIFE, not a property of the deployment, and nothing announces its expiry.** `snapshot_interval_sec` is 3600 on prod, so it performs this exact serialise-compress-verify hourly and survives it; sampling its RSS across one ("Created snapshot at sequence ...") gives the peak the first boot has to reproduce, on the same store, same code and same config. A number from the thing itself beats any number from a container.
+
+  **AND IT EXPIRED, AND THE EXPIRY WAS INVISIBLE FOR THREE DAYS. PROD'S DATABASE SPENT 2026-10-17 AND 2026-10-18 BEING OOM-KILLED ONCE AN HOUR, RECOVERING EACH TIME, UNTIL THE MORNING OF THE 19TH WHEN IT COULD NO LONGER BOOT AT ALL.** The sequence is worth knowing in full because every step made the next one worse:
+  1. **The hourly seal snapshot's anonymous peak outgrew the box.** The serialised snapshot is ~370-399 MB now, and the path is working set + one serialised copy + a compressed copy + a verify read-back, so the peak tracks STORE SIZE. Doc count went 33,321 (early October) to 56,590 (Oct 16) to 65,021 (Oct 19).
+  2. **Every hourly snapshot was therefore killed.** systemd `oom-kill` results run hourly from 2026-10-17 00:37, 48 of them across Oct 17-18 - exactly 24 a day, at :37/:38.
+  3. **A snapshot that never completes never truncates the WAL**, so it grew to 139 MB (a rotated 105 MB file plus a 34 MB current).
+  4. **Recovery cost scales with the WAL, so each boot was dearer than the last.** On 2026-10-19 it crossed the point where BOOT did not fit: the restart counter went 69 at 08:51, 70 at 09:51, 71 at 10:51 - hourly - and then 72 and 73 inside 10:52, continuous. Boot died ~3 s after "Re-mirrored 12045 document(s) to LMDB after WAL replay", at `anon-rss:1513544kB`.
+  Last snapshot before the gap: Oct 16 14:35, sequence 337213. Next: Oct 19 11:39, sequence 406829, after the fix. **~69 hours with none.**
+  **FIXED BY ADDING SWAP, which is a floor under the symptom and not a cure.** 4 GB `/swapfile` plus an `/etc/fstab` line so it survives a reboot (`/etc/fstab` is owned by no package - `dpkg -S` first, per the conffile trap above; backed up to `/etc/fstab.bak-20261019`). Boot then peaked at ~1514 MB RSS **and 818 MB of swap**, settling at 847 MB in use, so the anonymous demand is well ABOVE the 1973 MB box rather than marginally over it. The snapshot peak still scales with the store, and swapping a 400 MB serialise is slow; the structural fix is the database's.
+  **THE KILL POINT IS A LOWER BOUND ON THE REQUIREMENT, NEVER THE REQUIREMENT.** Without swap the process died at 1.51 GB anon, and it is tempting to size against that. The kernel STOPPED it there - it never revealed its own peak. Only once swap gave it room did the real figure appear, and it was ~2.3 GB of anonymous demand. Sizing against an OOM kill point under-provisions by however much the process still had left to allocate, which is unknowable from the kill.
+  **WHY NOBODY SAW IT, AND THIS IS THE GENERALISABLE PART: A CRASH THAT RESTARTS CLEANLY IS A SILENT CRASH.** systemd brought it back each hour, so the observable was a ~40 s outage once an hour inside consumers' retry budgets - and the database logs it only as `status=9/KILL`, with no line saying a snapshot FAILED. Worse, the run log lives in the database, so during each kill it goes QUIET rather than recording failures: the absence-is-not-information trap this file already records, now in its most expensive form. A consumer reported it only on the day it became permanent. **So "the service is up" and "the service has not been failing" are different claims, and `NRestarts` is the cheap way to tell them apart** - 183 here, which was visible all along and which nobody read. Check it when closing any window, next to `apt-mark showhold`; it is the same class of signal as the `ii`/`hi` column this file has now been read past twice.
+  **The pre-flight rule that follows: a headroom measurement needs a RE-MEASUREMENT TRIGGER, not just a date.** "The snapshot survives hourly" was true, dated, and silently became false through ordinary growth - no deploy, no config change, nothing to review. Tie it to a quantity that moves: snapshot file size against free memory, or doc count against the figure the headroom was measured at. A dated measurement with no trigger is a claim that will be believed long after it stops being true, which is precisely the failure this file's maintenance rule exists to prevent - and here the stale claim was MINE, in this file, and it read as reassuring right up to the outage.
 - **A SECOND RESTART IS NOT NEEDED ON 2.17.2, measured rather than reasoned.** Our `KeyStore`/`SettingsStore` bootstraps DO perform the online upgrade after the migration, and the log says so - but with **0 rows and 0 history versions rewritten**, because the collections are already ciphertext through the bare-alias merge. `encryption-verify` then reports `sealed: yes`, `CLEAN`, exit 0. So the window is TWO steps and ONE compaction (upgrade, then start vectorapi), with verify as the branch: plain `CLEAN` means done, `CLEAN (UNSEALED: ...)` means take the reseal inside the window. On 2.17.1 that same flag move requested a reseal that compacted every project, which is where the obsolete three-step plan came from.
   Note the upstream log line still says "old plaintext copies are removed at the next restart" even when it rewrote NOTHING, which contradicts verify's `sealed: yes` and would have an operator restart for no reason. Trust verify.
 - **MOVING THE CLIENT PACKAGE UNDER AN EXISTING BINARY IS AN ABI QUESTION, AND IT IS CHEAP TO TEST.** Every 2.x client shares one SONAME, so apt will happily put a 2.17.2 `libsmartbotic-db-client.so.2` under a server binary compiled against 2.14.0 headers - and the documented failure mode is that it installs fine and segfaults later. Verified before deploying rather than hoped: a Debian 13 container holding the real `smartbotic-vectorapi 0.12.0-1` deb plus `libsmartbotic-db-client 2.17.2-1`, against a 2.17.2 server, answered **21 of 21 calls** correctly - including `POST /indexes` with `unique`, which exercises `CreateIndexResult`, the by-value struct whose SIZE is the thing that would break, and a 409 duplicate refusal, which proves `duplicateExamples` crossed the boundary intact. `/api/v1/version` reported `db_client: 2.17.2` so the new library really was loaded. Repeat this whenever `DB_CLIENT_VERSION` and the deployed client diverge; it takes minutes and the alternative is a restart days later.