Skip to content

Git designed for AI agents.

docs(storage): state the shadow budget requirement and the SQLite limit

Federico Eliad1e61d361efb
diff --git a/doc/graft/PLAN7.md b/doc/graft/PLAN7.md
index c80c76c..9c73b61 100644
--- a/doc/graft/PLAN7.md
+++ b/doc/graft/PLAN7.md
@@ -243,6 +243,69 @@ WP76 audits the syncer's paths (the single-flight retry rounds, `fetch`,
tests that a round slower than the bound is cancelled. Verify's dry hydration runs
under a deadline of `W` from its start, tested the same way.
+### SHADOW BUDGET requirement (WP73b, binding; charter case B)
+
+Found in WP73b: `TestStorageShadowModeBenchmarkHasNoDivergence` failed under
+machine load with `select repository: context deadline exceeded`. Traced (pool
+waits 0, SQLite lock waits under 50 ms; the failure reproduced byte for byte
+with a 150 ms budget): Preparing's git work used up the shadow budget, and
+Prepared failed at its first context-aware call, which was the `SELECT` of
+`GetRepository`. The design behaved as specified; the test asserted more than
+the design guarantees.
+
+* **Old (implicit) requirement, asserted by the test:** in shadow mode every
+ reference transaction is recorded in the database, under any load.
+* **Requirement (RULE 4, stated):** shadow mode never fails or noticeably delays
+ a git write: the storage-core work of one reference transaction, the Preparing
+ and Prepared phases together, runs under `shadow_timeout`. A transaction whose
+ work exceeds it is **not recorded**: the failure is counted in
+ `graft_storage_shadow_errors_total` and logged at error level, and the git write
+ proceeds. The database misses that write until the next write of the same
+ reference, which is recorded as a forced changeset (counted in
+ `graft_storage_shadow_divergences_total` and logged); meanwhile `graft storage
+ verify` reports the reference as diverging. Recording every write is
+ guaranteed only while the shadow work of every transaction fits the budget.
+* **Operator rule:** switch to authoritative mode only with
+ `graft_storage_shadow_errors_total`, `graft_storage_shadow_divergences_total`
+ and `graft_storage_untracked_writes_total` at zero and `graft storage verify
+ --connectivity` consistent on every repository.
+* **Tests:** the mirroring test runs with a non-binding budget, because it tests
+ the correctness of the mirroring and whether a load fits 5 s depends on the
+ machine; `TestStorageShadowModeBudgetOverrun` tests the budget path
+ deterministically, for an overrun in Preparing and one in Prepared (git write
+ succeeds, error logged, nothing recorded, verify reports the divergence, the next
+ write is forced and logged), and the voter's unit tests check the counter.
+
+### SQLite write admission (WP73b, binding)
+
+Found in WP73b: `TestCommitsOnOneReferenceAreDense/sqlite` (32 writers on one
+reference) failed under load with `database is locked`. Traced on two cores: the
+write lock is busy all the time (summed holds at least the wall time; mean hold
+9 ms), but SQLite's busy handler keeps no queue, so the lock goes to whichever
+waiter retries first: mean wait 0.16 s, longest 3.05 s where a fair queue bounds
+it near 0.3 s, and under heavier load the tail crossed the 10 s busy timeout.
+Postgres queues its row-lock waiters and is not affected.
+
+* Every SQLite `Store` admits its write transactions one at a time, first come
+ first served (`sqlstore.writeQueue`), before `BEGIN IMMEDIATE`; the admission is
+ handed directly to the next waiter, so no newcomer overtakes it, and a writer
+ whose context ends leaves the queue at once.
+* **One admission covers one attempt** of `withTx`: it is taken right before
+ `BEGIN IMMEDIATE` and released when that attempt committed or rolled back. A
+ writer never holds it while `retryTx` sleeps before its next attempt, so the
+ writers behind it never wait for a lock nobody uses. No transaction function
+ starts another write transaction, and the admission is always taken before
+ SQLite's lock, so the two cannot wait for each other (LOCK ORDER).
+* Read transactions (the query-only pool) and plain reads need no admission.
+* **Limit:** the fairness is per process. Several processes on one SQLite file
+ (several nodes on one host) still meet in SQLite's busy handler; a multi-node
+ deployment uses Postgres, SQLite is for one node or for tests (STORAGE.md).
+* **Tests:** writers are admitted in arrival order; a writer whose context ends
+ leaves the queue and writes nothing; the hand-over never loses the admission when
+ it races a cancellation; the dense test and the LOCK ORDER `-race` test; and the
+ dense test's wait distribution before and after, measured with the WP73b tracing
+ under the same pinning.
+
### Liveness is per key (change G)
Liveness is decided per key from the rows that reference it: a key is live when
diff --git a/doc/graft/STORAGE.md b/doc/graft/STORAGE.md
index ee69b53..540a683 100644
--- a/doc/graft/STORAGE.md
+++ b/doc/graft/STORAGE.md
@@ -118,6 +118,21 @@ that landed while the repository's import was not complete) shows up as a
divergence until a later write records the reference as a forced changeset;
its objects are uploaded with the pack of the next write that builds on them.
+Shadow mode never fails or noticeably delays a git write: the storage-core work
+of one reference transaction (building and uploading its pack, then recording
+it in the database) runs under `shadow_timeout`. A transaction whose work takes
+longer is **not recorded**: the failure is counted in
+`graft_storage_shadow_errors_total` and logged at error level, and the git write
+proceeds. The database misses that write until the next write of the same
+reference, which is recorded as a forced changeset (counted in
+`graft_storage_shadow_divergences_total`); until then `graft storage verify`
+reports the reference as diverging. Recording every write is therefore
+guaranteed only while the shadow work of every transaction fits the budget; on a
+heavily loaded machine some writes may not fit. Switch to authoritative mode only
+when those counters and `graft_storage_untracked_writes_total` are at zero and
+`graft storage verify --connectivity` is consistent on every repository (see
+[Switching](#switching-from-shadow-to-authoritative)).
+
Authoritative mode serves reftable repositories only. Repositories created
through `CreateRepository` are created with the reftable backend, and the syncer
treats a local copy that is not reftable as a stale cache and hydrates a
@@ -170,9 +185,9 @@ environment variables and `.pgpass`, and buckets use the standard environment
of their cloud SDK (`AWS_*`, `GOOGLE_APPLICATION_CREDENTIALS`, `AZURE_*`).
All nodes of a deployment use the same `database` and `bucket` and different
-`node_id` values. SQLite works for one node, or for several nodes on one host
-(the tests run several servers on one SQLite file); a deployment with nodes on
-several hosts needs Postgres and a shared bucket. The database schema is
+`node_id` values. SQLite is for one node (the tests also run several servers
+on one SQLite file, at the cost described in [Limits](#limits)); a deployment
+with more than one node needs Postgres and a shared bucket. The database schema is
created and migrated at startup.
Startup fails when the mode is unknown, authoritative mode is set without
@@ -523,6 +538,12 @@ show:
the lock is taken), and git waits up to 10 s for it, so concurrent writes
queue instead of failing; a write that waits longer than 10 s still fails with
"reference is already locked". See [Benchmark](#benchmark).
+- **SQLite serves one node.** SQLite has a single write lock. The writes of
+ one gitaly process take it in the order they arrive, but several processes on
+ one SQLite file (several nodes on one host) meet in SQLite's own lock wait,
+ which keeps no queue: under steady contention an unlucky write can wait past
+ the 10 s busy timeout and fail. A deployment with more than one node uses
+ Postgres; SQLite is for one node, or for tests.
- **Some mutators are refused in authoritative mode**, see
[RPCs in authoritative mode](#rpcs-in-authoritative-mode).
- **Switching back from authoritative mode** has no export of the changelog