Explorar o código

docs: measured the 2.17.1 migration on prod data, and my cost model was wrong

An hour ago I committed that history bytes dominate the database's at-rest
encryption migration in both duration and memory. Measured on a copy of
prod's own data directory with 2.17.1, they do not matter to it at all.

190 seconds total, CLEAN and sealed, never OOM-killed. The encryption pass
was 13 SECONDS - 24733 rows scanned, 19 encrypted, 26 history versions.
Compaction was 176 s, and novitax's single 339 MB env was 167 of them,
reclaiming 13 MB. The seal was under a second.

The structural reason, which makes it general rather than a quirk of this
dataset: the migration only rewrites history belonging to ENCRYPTED
collections. Our 114 MB vectorapi_functions.hlog and a consumer's 47 MB
kb_chunks.hlog are on encrypted=false collections, so they are scanned and
left alone - verify reports kb_chunks with history 18648 and
plaintext-required 0. Cost tracks the largest project's data.mdb through
compaction and almost nothing else.

So the whole pruning-and-deferral thread chased a term that does not exist
in this migration, and I drove it: I took an upstream sizing model, found a
114 MB history file, and concluded it was both the duration risk and the OOM
risk. I asked the database for a boot mode and asked a consumer to weigh an
escape hatch on that basis. One measurement first would have been cheaper
than either, and I had the backup copy in hand before I asked.

Capping our retention is still worth doing for disk and unbounded growth.
It is now decoupled from any window.

Also recorded, because the headline number misleads: plan with ANONYMOUS
memory. anon was 364 MiB while the process VmHWM reached 1122 MiB, above the
900 MiB cgroup cap, with zero OOM kills - the LMDB mmap is file-backed, so
resident mapped pages raise the process high-water mark while the cgroup
reclaims them as cache instead of killing. Both numbers come off one run and
only 364 MB is the one to plan with.
fszontagh hai 1 mes
pai
achega
386d2fa179
Modificáronse 1 ficheiros con 3 adicións e 1 borrados
  1. 3 1
      CLAUDE.md

+ 3 - 1
CLAUDE.md

@@ -157,7 +157,9 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
 - **"The dev host cannot build the C++" was a misdiagnosis - it was a stale `protoc`.** The host builds everything natively; use `scripts/host-build.sh`. The old symptom was real: generated protos wanted `google/protobuf/map_field_inl.h`, a header protobuf 35 removed. But nothing in the database's tree includes that header - an **old protoc emits the include**. The host had libprotobuf 35.1 and gRPC 1.83 from the distro, and a hand-dropped static **protoc 25.2 in `~/.local/bin`** shadowing everything, while the distro's compiler package (`protobuf-35.1`, separate from `protobuf-devel`, which ships no protoc) was never installed. `find_program(PROTOC protoc REQUIRED)` took the 25.2 binary from PATH, generated code for a protobuf 25 runtime and compiled it against 35 headers. With a matching protoc the client and all of vectorapi compile and link natively. `host-build.sh` therefore picks protoc **by version rather than PATH order** and refuses to run on a mismatch, because the failure otherwise surfaces as a missing header deep in generated code rather than as a version complaint.
 - **`encrypted_field` (422) is its own slug, not `validation`.** Database 2.17.0 encrypts any field named `password`, `secret`, `token`, `api_key`, `apiKey` or `credentials` at ANY depth in EVERY collection, and then REFUSES a filter, sort, index, unique constraint, relation or increment on one. It needs its own slug because the remedy is unlike every other validation failure: the request is well formed, the field name is correct, and no correction of the body helps - the caller must stop querying that field, storing a non-sensitive duplicate (a hash, or a boolean "set") or renaming it if it was never a credential. Folding it into `validation` would send them hunting a typo. Note what it REPLACES is worse than itself: before 2.17.0 a top-level field of those names was plaintext and filterable, and a NESTED one compared ciphertext and **silently matched nothing** - so the refusal turns a silently empty result into a visible error. **Our `/documents` filter grammar, `?sort=` and the index route all accept ANY field name with no validation, so a TENANT can trigger this; we cannot.** Our own tests use `password` only as a `ctx.mail` option key, never as a stored field being queried.
 - **WE CREATE EVERY COLLECTION WITH UNLIMITED VERSION HISTORY, and most of prod's history is therefore our own operational churn.** `createCollection(..., maxVersions = 0, ...)` and 0 means "keep unlimited history", not "keep none" - so every code push to a function has appended a full copy of the script forever. Measured on prod: `justice-pa:vectorapi_functions.hlog` 114 MB (the function REGISTRY, a handful of rows), `default:vectorapi_function_jobs.hlog` 14 MB, `justice-pa:vectorapi_function_runs.hlog` 4.7 MB - about 133 MB of the box's 192 MB of history, roughly 69%, none of it data anyone would ever read. A consumer's 47 MB of `kb_chunks` history is retention of the same kind on a collection that has not changed size in days.
-  **It is not just disk: history bytes dominate the database's at-rest encryption migration**, in both duration and its memory pass, so our retention default sets the length of a window three consumers have to approve.
+  **IT IS A DISK AND GROWTH PROBLEM ONLY. I claimed history bytes dominate the at-rest encryption migration, and measured on prod's own data they do not matter to it at all.** That claim was inherited from the database team's sizing model and amplified by me, and one measurement would have settled it before I asked them for a boot mode and asked a consumer to weigh an escape hatch. Measured with 2.17.1 on a copy of prod's data directory (1.6 GB, 24753 docs, 65 collections, prod's own config, 900 MB cap): **190 seconds total**, of which the ENCRYPTION pass was **13 seconds** (24733 rows scanned, 19 encrypted, 26 history versions) and **compaction was 176 seconds, 167 of them novitax's single 339 MB env**. The seal was under a second.
+  **The structural reason, which is what makes it general rather than a quirk of this dataset: the migration only rewrites history belonging to ENCRYPTED collections.** Our 114 MB `vectorapi_functions.hlog` and the consumer's 47 MB `kb_chunks.hlog` sit on `encrypted=false` collections, so they are scanned and left alone - `encryption-verify` reports `novitax:kb_chunks` with `history 18648, plaintext-required 0`. So the migration's cost tracks **the largest project's `data.mdb`, through compaction**, and almost nothing else. An operator with a huge history and a small env would read the old model and expect the opposite of what happens.
+  **Memory: plan with ANONYMOUS memory, not RSS, and expect RSS to look alarming.** Same run: `anon` 364 MiB, page cache 295 MiB, cgroup `memory.peak` pinned at the 900 MiB cap, and the process's `VmHWM` **1122 MiB - above the cgroup limit** - with `oom_kill 0`. That is not a contradiction: the LMDB mmap is file-backed, so resident mapped pages count toward the process high-water mark while the cgroup treats them as reclaimable cache and evicts rather than killing. "It needed 1.1 GB" and "it needed 364 MB" are both readable off one run and only the second is the planning number. Anyone watching RSS cross 1 GB on a 2 GB box mid-credential-migration will reach for the kill switch; the page-cache explanation is not something they will derive at that moment.
   **PRUNING IS NOT BACKGROUND HYGIENE. It takes a GLOBAL lock.** From database 2.17.1, `createCollection(existing, maxVersions = N)` with a lower N rewrites that collection's history keeping the newest N per document - but it runs synchronously while holding the store's global collection lock exclusively AND the global history-append lock, so essentially every operation that resolves a collection waits, **in every collection**, for its whole duration. Expect seconds to tens of seconds on a 100 MB file (unmeasured). So: never loop it under live traffic, give it a quiet moment or put it inside a window, and prune the big one alone. Moving it out of that lock is an upstream follow-up and is NOT in 2.17.1.
   **And it cannot be done BEFORE the encryption migration.** There is no mode that boots 2.17.1 without migrating - it runs before the listener opens - so "prune first to shorten the migration" is unreachable. Just as well: the prune path has no test against an unmigrated store (its test runs on a fresh 2.17.1 store and asserts only counts, not that surviving versions decrypt), so the escape hatch would have exercised new code against old on-disk format in a configuration nobody upgrades into. **The consumer who would have gained most from it was the one who decomposed the question correctly - "can it be deferred" is the easy half, "is the prune path safe against an unmigrated store" is the half that bites - and said they would rather have the long window.** Worth remembering as a pattern: when an optimisation needs a state that only the optimisation produces, the question is never whether you can get into that state.
   **What disabling versioning costs**, confirmed from their code rather than assumed, because the plan is to turn it OFF for the queue and the run log and keep a small N (5-10) on the registry: version history and `restoreVersion`/`restoreToDate` stop working for new writes, and **a DELETED row can no longer be restored**, because restore-after-delete reads the pre-delete state from history. That is why the REGISTRY keeps versions - an accidentally deleted function is exactly the row worth recovering - while a claimed-and-deleted queue row has no reader. It does NOT touch the WAL, replication, the event stream (actor included) or `_version`, which still increments; none of them read history, and recovery is snapshots plus WAL.