Knotrel Docs

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, not 503 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)

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.

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)

Invariants kept (each with a test):

  1. Bounded work. Outstanding graph jobs stay bounded: KNOTREL_MAX_PENDING_JOBS + KNOTREL_FEEDER_RESERVED_JOBS + 2 plus one import, instead of KNOTREL_MAX_PENDING_JOBS plus one import. The documented bound in docs/http-api.md (resource bounds) changes accordingly, and a test fills every lane and checks the bound.
  2. 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.
  3. 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.
  4. No new outcome. No HTTP route, field, status or error code changes. A refused feeder request still gets 503 import_in_progress.
  5. 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:

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

  1. Tests for change A: queries holding every shared permit do not block feeder events, barrier, heartbeat or session (unit tests in service.rs with the existing permit-holding fixtures); a flood of feeder requests beyond the reserved permits gets 503 import_in_progress; standalone mode is unchanged; settings parsing and GET /v1/info; the existing admission, cancellation, panic and drain tests keep passing.
  2. The postgres-feeder integration tests on PostgreSQL 15 to 19.
  3. 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.md with raw output.
  4. 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

7. Risks

8. To approve

  1. Change A as specified (reserved permits for feeder requests, default 4, setting KNOTREL_FEEDER_RESERVED_JOBS, additive /v1/info field).
  2. Step B: the diagnostic run and its decision table, with B1 or B2 designed as an addendum only if the diagnostic points to them.
  3. The verification plan, including the full re-measurement against Q1–Q5.