Feeder admission and scheduling under query load: design (roadmap 0.9.3)
Status: APPROVED by the maintainer on 2026-11-21 (change A and step B; B1 and B2 out of scope), with the control lane of section 3.1 added before implementation as requested.
v1.0.0 note. Since v1.0.0 an exhausted feeder lane answers
503 busy, not503 import_in_progress(an approved exception of the v1.0 contract freeze, decision 4; see compatibility). The text below records the 0.9.x design, where the code was kept.
The v0.8.0 baseline of the feeder-under-load check failed Q2 and Q3. By its decision rule, 0.9.3 continues with the smallest server change that addresses the failing mechanisms. This document states the mechanisms, the proposed change, its invariants and how it is verified. It does not change the approved criteria.
1. What the baseline showed
| Clients | Observation | Criterion |
|---|---|---|
| 64 | The feeder’s event requests were refused with 503 import_in_progress 188–243 times per 60 s window; in one observation continuously for several seconds, with marker lags of 7–11 s |
Q2 (that observation), Q3 at 64 clients (2.1 % of the no-query rate) |
| 16 | No refusal, but catch-up of a backlog at 16.3 % of the no-query rate | Q3 at 16 clients |
2. How the server behaves today (v0.8.0, read from the code)
- One admission semaphore of
KNOTREL_MAX_PENDING_JOBSpermits (default 32) is shared by every graph job (Service::admit, andService::execute_feeder_workerfor feeder jobs). A request that finds no free permit is refused immediately. This covers query requests and also every feeder request: events, barrier, gap, session registration and heartbeat. Imports have their own semaphore of one permit. - Consequence: with enough concurrent query requests, the feeder can be
refused for as long as the queries keep every permit busy. Even the
heartbeat can be refused, which can let the lease expire and turn
Knotrel
stalealthough the feeder is alive. - The graph lock is a
parking_lot::RwLock. Query-only jobs take it shared; feeder jobs take it exclusively.parking_lot’s lock is task-fair: once a writer waits, newly arriving readers wait behind it. So a waiting feeder job already does not starve behind a stream of new queries; it waits for the readers that hold the lock when it arrives. - Therefore the 16-client slowdown is not explained by the lock policy
alone. At 100k nodes a query batch of 8
connectedholds the lock for a few milliseconds; waiting for the in-flight readers costs about that much per feeder batch, while the measured cost was about 34 ms per 1,024-event batch against 5.5 ms without queries. The remaining time is probably CPU contention: 16 clients’ breadth-first searches keep most of the 8 cores busy, and PostgreSQL, the feeder and the load tool share the same machine. This is a hypothesis, not a measurement.
3. Change A: reserved admission for feeder requests
What: a second semaphore, used only by feeder requests in source-managed mode, so that queries can never take the capacity the feeder needs.
- New setting
KNOTREL_FEEDER_RESERVED_JOBS, integer from 1 to 64, default- Ignored in standalone mode (no feeder routes).
- Feeder events, barrier, gap, session registration and heartbeat acquire a permit from the reserved semaphore instead of the shared one. Query requests, stats and inspection keep using the shared semaphore, unchanged. Imports keep their own single permit, unchanged.
- When the reserved semaphore is exhausted (only if the feeder sends more than
4 concurrent requests, which the reference feeder never does), the feeder
request is refused exactly as today:
503 import_in_progress, counted asbusy.
Why 4: the reference feeder has at most one events, barrier or gap request in flight at a time; four leaves room for a retry overlapping a slow request and keeps the extra bound small.
3.1 Control lane: session and heartbeat keep access
Lease renewal must not depend on how busy the data path is. The reserved capacity is therefore split into two independent semaphores:
| Lane | Requests | Permits |
|---|---|---|
| Feeder data | events, barrier, gap | KNOTREL_FEEDER_RESERVED_JOBS (default 4) |
| Feeder control | session registration, heartbeat | 2, fixed (one heartbeat and one registration in flight) |
- A session or heartbeat request never takes a permit of the shared query semaphore or of the data lane, so neither query load nor a burst of events and barriers can refuse it at admission.
- After admission both lanes run through the same worker and take the graph lock exclusively, as today. A heartbeat therefore waits at most for the job holding the lock (one events batch or the readers present when it queues; the lock is task-fair), milliseconds against a lease of at least 1 s.
- Refusal of a control request is still possible only if more than two are in flight at once; the reference feeder never does that.
- Test that closes the lease question: with every shared permit held by
query jobs and every data-lane permit held by feeder jobs, a feeder with a
1 s lease heartbeats for several lease periods, and the lease never expires
and the status never becomes
stale; a session registration succeeds in the same state. Until this test passes, the lease risk is not considered resolved.
Invariants kept (each with a test):
- Bounded work. Outstanding graph jobs stay bounded:
KNOTREL_MAX_PENDING_JOBS + KNOTREL_FEEDER_RESERVED_JOBS + 2plus one import, instead ofKNOTREL_MAX_PENDING_JOBSplus one import. The documented bound indocs/http-api.md(resource bounds) changes accordingly, and a test fills every lane and checks the bound. - Permits are always released. A permit of any lane returns to its
semaphore when the job ends, whatever the outcome: success, protocol error
(for example
stale_generation), worker failure, a request cancelled by its client (the permit stays with the running worker until it finishes, as today), and a request that hits the server’s request timeout. Each case has a test that checks the lane is back at full capacity. - Same job path. Reserved permits only change admission. The job still runs through the same worker, lock, failure marker, panic handling, cancellation (the permit stays with the worker), drain accounting and telemetry.
- No new outcome. No HTTP route, field, status or error code changes. A
refused feeder request still gets
503 import_in_progress. - Queries unaffected when no feeder request runs. The shared semaphore and the query path are unchanged.
Observability: requests_rejected_total{reason="busy"} keeps counting
feeder refusals. The capacity gauge gained no new series in v0.9.0; since
v1.0.0 the admission gauges and the rejection counter carry a lane label
(observability contract, section 21). GET /v1/info
reports the new setting as feeder_reserved_jobs in source-managed mode
(additive field; documented in docs/openapi.yaml and docs/http-api.md).
Expected effect: the 64-client refusals and the heartbeat risk disappear. This targets Q2’s failing observation and Q3 at 64 clients. It does not by itself address Q3 at 16 clients, where there were no refusals.
4. Step B: diagnose the 16-client slowdown before changing anything else
Because the lock already gives waiting writers precedence (section 2), no lock change is proposed blindly. CPU contention is a hypothesis to verify, not a finding. First a diagnostic run, declared here before it runs, descriptive only (no criterion), that separates the three places a feeder request can spend time:
| Phase | Source |
|---|---|
| Admission wait | refusals (503) of feeder requests and the time the feeder spends backing off and retrying, from the feeder log; admission itself does not queue |
| Lock wait | graph_job_queue_seconds (worker scheduling) and graph_job_lock_wait_seconds for kind="feeder_events" |
| Execution | graph_job_execution_seconds for kind="feeder_events" |
The same three are recorded for the query kinds. Details:
- Cell L2, G100k, 16 clients, 3 observations, the baseline build (
v0.8.0) and the candidate with change A, same protocol. - Recorded in addition: Knotrel’s
/metricsbefore and after each catch-up (graph_job_queue_seconds,graph_job_lock_wait_secondsandgraph_job_execution_secondsforkind="feeder_events"and for the query kinds), the feeder’s time per request (from its log), and host CPU usage sampled every second.
Decision, from the diagnostic:
| Finding | Action |
|---|---|
| Feeder events wait mostly for the lock (lock wait ≫ execution) | Out of scope for 0.9.3 (approval, 2026-11-21); reported with the numbers. Would be B1: let a queued feeder job pass ahead of the readers it would otherwise wait for: readers that have not yet started their traversal re-check a “feeder waiting” flag and yield before taking the shared lock. Designed and reviewed as an addendum to this document before coding. |
| Feeder events run slowly while the lock is held (execution ≫ lock wait) and the CPUs are saturated | Out of scope for 0.9.3; reported with the numbers. Would be B2: bound the number of query jobs that run concurrently (new setting, default the number of CPUs minus one), so a feeder job finds a core. Same addendum and review. |
| Neither dominates (the time is in HTTP, PostgreSQL or the feeder) | B0: no further server change. |
In every case 0.9.3 implements only change A; a criterion that still fails is reported as failing, with the diagnostic’s breakdown, for a maintainer decision.
B1 and B2 trade query throughput for feeder progress; Q5 (query throughput ≥ 90 %, p99 ≤ 125 % of the baseline) bounds that trade.
5. Verification
- Tests for change A: queries holding every shared permit do not block
feeder events, barrier, heartbeat or session (unit tests in
service.rswith the existing permit-holding fixtures); a flood of feeder requests beyond the reserved permits gets503 import_in_progress; standalone mode is unchanged; settings parsing andGET /v1/info; the existing admission, cancellation, panic and drain tests keep passing. - The postgres-feeder integration tests on PostgreSQL 15 to 19.
- The approved measurement on the candidate: all cells of the protocol,
3 observations, against Q1 to Q4 and Q5. Same machine and conditions as the
baseline, recorded in
feeder-under-load.mdwith raw output. - Review of the change and its measurement before merge. The final v0.9.0 campaign (0.9.6) qualifies it again.
6. Compatibility and documentation
- No HTTP contract change; one additive
/v1/infofield; one new setting with a default. Deployments that do not set it get 4 reserved feeder permits. - Update:
docs/http-api.md(settings table, resource bounds, admission),docs/openapi.yaml(/v1/info),docs/observability-contract.md(the admission capacity row), the server README,docs/deployment.mdif it sizesKNOTREL_MAX_PENDING_JOBS, the console’s runtime decoder only if it reads the new field (it does not need to), and the release notes.
7. Risks
- A misbehaving feeder can now use up to 4 more concurrent jobs than before. The feeder is already a trusted, fenced writer (one active session); the bound stays fixed and small.
- Change A may not be enough for Q3 at 64 clients if the remaining time is CPU contention too; the measurement will show it, and step B applies.
- A single-machine artifact: if the diagnostic finds that the 16-client slowdown comes from the load generator, PostgreSQL and Knotrel sharing 8 cores, no server change can remove it. That is reported as such (B0), not hidden.
8. To approve
- Change A as specified (reserved permits for feeder requests, default 4,
setting
KNOTREL_FEEDER_RESERVED_JOBS, additive/v1/infofield). - Step B: the diagnostic run and its decision table, with B1 or B2 designed as an addendum only if the diagnostic points to them.
- The verification plan, including the full re-measurement against Q1–Q5.