📚 Extract database transaction and lock guidance

Keep transaction ownership, after-commit callbacks, and advisory lock
lifetimes in one memory shared by jobs and metrics.

AI-assisted-by: gpt-6.2-sol
This commit is contained in:
Andrey Antukh 2026-11-23 20:05:10 +00:00
parent 10e175d4de
commit 0cffbb4816
4 changed files with 11 additions and 11 deletions

View File

@ -4,7 +4,8 @@ JVM Clojure backend. Start in `app.main` for Integrant wiring, `app.rpc.commands
## Focused memories
- RPC wrapper order, DB transactions and advisory locks, config, HTTP middleware and metrics: `mem:backend/subtleties`.
- RPC wrapper order, config, HTTP middleware and metrics: `mem:backend/subtleties`.
- Transaction ownership, after-commit callbacks and advisory locks: `mem:backend/db-transactions`.
- RPC concurrency limit declarations, acquisition order and queue diagnosis: `mem:backend/rpc-concurrency`.
- Injected component schemas and startup assertion failures: `mem:backend/integrant-schemas`.
- Read-time migrations, fragments and snapshots: `mem:backend/file-data/reads-and-versions`. Partial save and failure handling: `mem:backend/file-data/save`.

View File

@ -0,0 +1,6 @@
# Database transactions and advisory locks
- Start at `app.db/transact!`, `tx-run!` and `own-transaction`. `tx-run!` prefers the connection in the cfg over its pool. Passing the very same connection joins the existing transaction, not a savepoint; use an explicit savepoint for partial rollback. Passing a pool instead starts an independent transaction even inside an existing one. `db/run!` obtains or reuses a connection without starting a transaction.
- Use `db/own-transaction` only when the write must survive rollback of the caller, as with independent job lifecycle transitions. It drops `::db/conn` to force the next `tx-run!` through the pool and `::db/read-only` so a read-only scan does not make the write read-only too. It does not clear an explicit `::db/rollback`.
- `db/after-commit` runs immediately outside the transaction context managed by `app.db`. Inside it, the callback belongs to the transaction that owns the connection, not the outermost call on the stack. Register it inside the transaction that performs the write. A rollback or rollback-only transaction discards its callbacks; an independent transaction drains its own callbacks at commit.
- Transaction advisory locks end with the transaction. Session advisory locks survive both commit and return to the connection pool; release them before returning the connection. The one-key and two-key PostgreSQL lock spaces are disjoint: file updates and setup use the former, audit consumers the latter. See `app.db/session-try-lock` and `mem:backend/audit-consumers`.

View File

@ -3,7 +3,7 @@
## Start in the code
- Definitions and lifecycle: `app.jobs`, `app.main` (`::jobs/defs`). Dispatch, execution and cron: `app.worker.dispatcher`, `app.worker`, `app.worker.cron`. Retention and resources: `app.jobs.gc`, `app.jobs.storage`, `mem:backend/storage`.
- User-facing RPCs: `app.rpc.commands.jobs`; file import/export: `mem:backend/binfile`. Deployment and queue topology: `mem:prod-infra/core`. DB transaction semantics: `mem:backend/subtleties`. Tests: `backend-tests.jobs-test`, `backend-tests.worker-test`, and the tests beside each job.
- User-facing RPCs: `app.rpc.commands.jobs`; file import/export: `mem:backend/binfile`. Deployment and queue topology: `mem:prod-infra/core`. DB transaction ownership: `mem:backend/db-transactions`. Tests: `backend-tests.jobs-test`, `backend-tests.worker-test`, and the tests beside each job.
## Boundaries

View File

@ -17,14 +17,7 @@
## DB helpers
- Most `app.db` helpers accept a pool, connection, or map containing `::db/pool` / `::db/conn`; preserve that convention in shared code.
- `db/tx-run!` uses `next.jdbc.transaction/*nested-tx* :ignore`: nested transaction calls reuse the outer transaction, not a savepoint. Use explicit savepoints when nested rollback semantics matter.
- Whether a call is nested is decided by the CONNECTION, not by an ambient flag: `db/transact!` binds `*after-commit-conn*` around the body, and a transactable that is not identical to that connection is an independent unit of work that owns its own after-commit context. A `tx-run!` that passes a pool while a transaction is open therefore commits on its own and drains its own callbacks, instead of leaving them in a context that may roll back.
- `after-commit` callbacks run on commit only. A transaction marked `::db/rollback` (used by `srepl` and `app.tasks/offload-file-data`) commits nothing, so it runs none of them.
- `db/run!` opens/reuses one connection but does not create a transaction.
- `db/own-transaction` returns a cfg that owns its next transaction: it drops `::db/conn` (so `tx-run!` opens its own) and `::db/read-only` (so a caller running read-only, like the storage scheduler streaming a plan cursor, cannot make the nested write read-only too). Lifecycle writes use it so a rollback of the surrounding work can undo neither the write nor its metric.
- `db/tjson` is Transit JSON for jsonb storage; `db/json` is plain JSON. The old `task` props used Transit, but unified job params and results use JSON with job-def decoders; see `mem:backend/jobs`.
- Advisory locks (`app.db`) come in two independent axes. **Scope**: the transaction ones (`xact-lock!`, `xact-try-lock!`) vanish with the transaction, and the session ones (`session-try-lock`, `session-unlock`, `session-unlock-all`) live until unlocked or the session ends — a pooled connection returned with one keeps it. **Form**: the one-key form takes a uuid or an int (uuids hashed with a zero-UUID seeded siphash) and the two-key form takes a `classid`/`objid` pair; PostgreSQL keys them by `objsubid` (1 vs 2), so the two spaces never conflict even with the same numbers. `update-file` and the setup take the one-key form; the audit consumers take the two-key one with `classid` `730000` and the consumer id as `objid`, which is why a consumer tick can never block a file update, and they clear whatever a borrowed connection carries before taking it.
- Transaction ownership, callbacks and advisory locks: `mem:backend/db-transactions`.
## Workers and cron
@ -57,7 +50,7 @@
- `app.metrics/run!` is safe by default: a recording failure never throws (a metrics bug must not change the behavior of the measured operation). The first failure per metric id logs at `warn`, later ones at `debug`. The `instance` precondition is a plain assert (the backend enables `:backend-asserts`), and the collector lookup sits outside the recording guard, so a missing instance fails hard even when asserts are disabled. `::mtx/metrics` is required by the storage, s3-backend, db-pool and jobs APIs; jobs helpers never silently skip a missing metrics instance. The resolver is `app.metrics/instance` and it raises, unlike `run!`.
- `app.msgbus` (`pub!`, `sub!`, `purge!`) keeps taking the **instance** on purpose: msgbus is what produces it, and `app.rpc.notifications` already shows the caller passing a cfg. `app.jobs` resolves `::mbus/msgbus` from the cfg internally, and `insert-event` keeps its explicit pre-insert check so a profile job never loses its notification.
- `app.db/after-commit` is the boundary for SQL-backed metrics: callbacks are drained by the commit of the transaction that owns the connection they were registered on, and discarded when it rolls back. Because nesting follows the connection, that transaction is the outermost one only for a real nested call.
- Register SQL-backed metrics via `app.db/after-commit` inside the writing transaction; see `mem:backend/db-transactions`.
- `penpot_http_malformed_requests_total` (counter, no labels) counts requests whose body the parser could not read. `app.http.middleware/wrap-parse-request` emits it where it classifies the error, so the instance travels as a middleware argument (`[mw/parse-request (::mtx/metrics cfg)]`) and each service declares the definition with its own prefix (nexus: `nexus_http_malformed_requests_total`). The per-endpoint request counters of nexus do not count these requests: they sit inside the parse middleware, so the failure happens before them.
## Log-derived metrics