mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
📚 Extract RPC concurrency limits guidance
Document limiter declarations, acquisition order, and queue diagnosis in one focused memory. Keep the original section as a pointer. AI-assisted-by: gpt-6.2-sol
This commit is contained in:
parent
570a5e67bc
commit
ed7d22e5b8
@ -5,6 +5,7 @@ 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 concurrency limit declarations, acquisition order and queue diagnosis: `mem:backend/rpc-concurrency`.
|
||||
- Read-time migrations, fragments and snapshots: `mem:backend/file-data/reads-and-versions`. Partial save and failure handling: `mem:backend/file-data/save`.
|
||||
- Job definitions, submission and lifecycle transaction boundaries, dispatch/recovery, user-facing jobs, and metrics: `mem:backend/jobs`.
|
||||
- File import/export format compatibility, jobs, permissions and cancellation: `mem:backend/binfile`.
|
||||
|
||||
7
.serena/memories/backend/rpc-concurrency.md
Normal file
7
.serena/memories/backend/rpc-concurrency.md
Normal file
@ -0,0 +1,7 @@
|
||||
# RPC concurrency limits
|
||||
|
||||
- Start at `app.rpc.climit/wrap` and `backend/resources/climit.edn`. A plain `::climit/id` keyword shares one limiter across calls in the process; `[id key-fn]` declares one keyed limit; `[[id key-fn] ...]` declares several. A keyword in the key position looks up a param. `::climit/key-fn` metadata is not read, so putting it beside a bare ID silently makes the limiter shared.
|
||||
- Acquire command limits before root limits, and the per-profile limit before its global counterpart. Otherwise callers waiting on heavy work can hold root permits and block unrelated RPCs, or commands acquiring shared limits in opposite orders can deadlock. Declare limits at the RPC boundary, not inside handlers; use shared helpers rather than invoking another wrapped RPC.
|
||||
- Give every declared ID a config entry with a bounded queue and permit wait timeout. Keep the deployed config aligned with shipped declarations: a missing entry skips that limit, and root limits do not bound waiters held outside them. A permit timeout bounds acquisition, not the running handler: a stalled remote image body read can still hold both image permits. See `rpc_climit_test.clj` for declaration order and config coverage and `climit_test.clj` for wait bounds.
|
||||
- `penpot_rpc_climit_waiting` is a gauge of callers waiting for a permit, summed over keys for each limiter name and instance. Do not replace it with the last active key's queue size. Pair each increment with one decrement on acquisition or failure; concurrent publications can arrive out of order, so the component sampler republishes the current total.
|
||||
- Count rejections with `penpot_rpc_climit_rejected_total` (`queue-full` vs `timeout`), not log lines: an isolated timeout can log at `TRACE`. Use `app.rpc.climit` logs to identify a key, not metric labels. When waiting stays high and `penpot_rpc_climit_timing_count` stops increasing on one instance, check thread dumps for parked permit waiters before blaming CPU saturation.
|
||||
@ -13,15 +13,7 @@
|
||||
|
||||
## Concurrency limiter (CLIMIT)
|
||||
|
||||
- `::climit/id` is either a plain keyword, which gives one shared limiter per process, or the vector form `[[id key-fn]]`, which gives one limiter per key. `climit/wrap` takes the key only from that positional `key-fn` (`climit/get-limits` maps a plain keyword to a single-element vector), so `::climit/key-fn` is metadata that nothing reads: a command declaring `::climit/id :x/by-profile` next to it ends up with a single shared limiter, and the missing key is visible in the `app.rpc.climit` log lines, whose `id` carries no key suffix. That is what happened to `push-audit-events`, the last user of the dead metadata, from `a5c6d78ee5` (2024-01-27, "fix some fundamental bugs on climit module", which moved the key to the vector form without updating it) until it was fixed; nothing declares `::climit/key-fn` any more, so keep using the vector form for anything that must be per profile.
|
||||
- A gauge written by whichever limiter of a name ran last cannot show a queue: every per-profile limiter of the name collapses into one series, so a healthy profile writing 0 overwrites the 15 of the one that is queueing (measured in production: 4 of 1441 samples). `penpot_rpc_climit_waiting` replaces it and is accumulated: one counter per name and instance, up when a call starts waiting for a permit, down when it stops, so a call that enters and never leaves keeps the value above zero for ever. `penpot_rpc_climit_rejected_total` counts rejections by name and `reason`: `queue-full` when the error carries `:size`, `timeout` when it carries `:timeout` (both carry `:code :capacity-limit-reached`, so the timeout field is the discriminator). The two gauges that could not tell the truth (`penpot_rpc_climit_queue`, `penpot_rpc_climit_permits`) were deleted, and so was `penpot_http_worker_busy_threads`, which reads 0 on an instance with 72 blocked threads: `penpot_http_worker_pool_size` is the signal for retained threads. A stuck limiter also shows up in `penpot_rpc_climit_timing_count` (the acquired path), which is a counter: compare the same limiter name across instances and the stuck one reads 0 acquisitions while its peer reads thousands. In thread dumps it reads as threads parked in `psm/acquire` whose CPU time does not grow between two dumps, on an instance that is otherwise idle.
|
||||
- The waiting counters are a field of the `ConcurrencyLimiter` component (a `ConcurrentHashMap` of name to `AtomicLong`), so they start at zero, die with the component, and the sampler that closes over that map republishes only its own names every 15 s; the sampler stops with the component in `halt-key!`. `impl-invoke` pairs one decrement with one increment through a per-call flag: the flag turns a second call into a no-op and the `finally` guarantees that there is one. The increment sits inside the `try` and the decrement lives only in the acquired callback and the `finally`, so a handler that throws, a rejection and a throwable from the publishing path all leave the count where it was and the thread without the limiter marked as held (a leftover mark would skip that limiter for that key for ever). Concurrent `inc`/`dec` publications of two threads can land out of order and the value can read low for a moment; the republish corrects it.
|
||||
- The `app.rpc.climit` log lines carry `queue`, `queue-max`, `permits`, `elapsed`, `code` and `reason`, and their `id` carries the key (`name/<profile or key>`). That is the only place where the profile behind a queue is visible: a pile-up is chased there, on demand, not in the metrics. The `queue` of the line is the calls inside the limiter minus its permits, so it reads 0 for a lone caller that times out: that line goes out at TRACE instead of WARN, and the rejection count has to come from the metric.
|
||||
- Nothing in the limiter path has a timeout unless the config sets one, and `psm/acquire` then calls `Semaphore.acquire`, which parks the calling thread forever. A parked thread still counts as in-system for every outer limiter of its chain, so the outermost one (the method's own by-profile limiter) stops granting permits and answers 429 to everything. A limiter without `:queue` gets an unlimited queue and therefore never rejects: its pile-up is silent, with no 429 and no log line except the WARN `enqueued`/`acquired` lines of the keyed id.
|
||||
- Deadlock shape between a command's own limits and the root ones (fixed): a command declaring its own limits held `process-image/by-profile` before `root/by-profile`, while a command with no limits of its own takes the roots first and then re-enters with the same keyed limits from inside its handler (`create-file-media-object-from-url`, `upload-photo`) and waits for them holding a root permit. Two calls of one profile then held the same two limiters inverted and parked for good: 69 threads at 0 CPU, an audit limiter with 0 acquisitions in 24h and 100% 429s, on an instance that never served a 204. Both commands declare their limits now, so the shape cannot happen any more: the root limits are taken last and every path takes the limiters in the same order.
|
||||
- `climit/wrap` takes the command's own limits before the root ones: the root limiters are appended to the sequence first and the last element of the sequence is the outermost one, so `root/global`/`root/by-profile` end up innermost. A request waiting for its own limits no longer holds the root permits of its profile, which is what kept a pile-up of heavy requests from blocking the other commands of that profile. Every acquisition goes through that wrapper, so every path takes the limiters in the same order: there is no way to enter the limiter from inside a handler, and a command that calls another command internally would take the limiters twice (the second call waits for a permit the first one holds and answers 429 when the timeout runs out). The key of a limit is a function of the params (a keyword is looked up in them, a function is applied to them, any other value is the key itself). Commands declare the by-profile limit before the global one, which makes it the outermost of the pair, and a test fails when the relation between all the limits the commands declare cannot be sorted: two commands that take the same limits in opposite order, or a ring of three, leave the paths of one profile holding them in opposite order.
|
||||
- The two commands that used to enter climit from inside their handler declare their limits now, so the whole command runs inside them: the download of an external image takes a `process-image` permit, and that limit has one permit per profile, so a slow download holds the image processing of that profile for as long as it takes. It takes the global permit of that limit too (8 permits, queue 20), so eight slow downloads of eight different profiles leave the image processing of every profile waiting. The copy of the body has no timeout (the 30 s of the request covers the connection and the headers), so a server that stops sending keeps the permits until the operating system gives up. The storage write of the profile photo runs inside its limit too.
|
||||
- The shipped `backend/resources/climit.edn` sets a timeout and a queue on every limiter, 60s for operations that can be slow and 30s otherwise, with a test that fails when an entry ships without one, and `init-key` logs a WARN for every entry without a timeout. The queue matters because the root limits no longer bound the waiters: a command takes its own limits first, so a pile-up waits in the queue of that limit. The file and the declarations have to list the same ids: a `::climit/id` without an entry makes `wrap` log `no config found for specified queue` and run the handler with no limiter at all, and a test fails when a command declares an id the file does not list. Only key a limit by profile when the work contends on something shared (`update-file`, images, fonts, thumbnails, the profile row): a global cap on a cheap command adds a rejection path nobody needs, which is why `profile-plugin-ops` and `submit-audit-events` declare only `by-profile`. The production file lives outside the repository: mirror it there, and remember that a parked thread only goes away with a restart or a deploy.
|
||||
- Limit declarations, acquisition order and queue diagnosis: `mem:backend/rpc-concurrency`.
|
||||
|
||||
## DB helpers
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user