rate_coordinator
rate_coordinator
¶
Daemon-level mirror of instrument rate limits.
Cross-job dispatch backoff does NOT live here — it is structural in the
baton: BatonCore._instruments is shared by every job, so a
RateLimitHit from any job marks the instrument baton-wide, moves all
jobs' affected sheets to WAITING, and blocks dispatch until the baton's
own RateLimitExpired timer fires. The baton is the single authority
for dispatch decisions.
This coordinator is the daemon-level OBSERVABILITY mirror of that state,
fed by a write-through in the baton adapter's run loop
(BatonAdapter._report_rate_limit_cross_job →
JobManager._on_rate_limit → report_rate_limit(), #206).
Consumers:
BackpressureController— escalates pressure to HIGH while any instrument is rate-limited;- submit-time warnings (
JobManager.submit_job) — "X clears in Ns"; GlobalSheetSchedulerviais_rate_limited()— built and tested but not yet driving execution (#238).
Keys are instrument NAMES — the same key space the baton's
InstrumentState uses and that mzt clear-rate-limits --instrument
passes (the manager clears the baton and this mirror with the same
string). Do not key by provider/backend type: that would split the key
space and break the shared clear contract.
Satisfies the RateLimitChecker protocol defined in scheduler.py
so the GlobalSheetScheduler can query limits before dispatching.
Lock ordering (daemon-wide): 1. GlobalSheetScheduler._lock 2. RateLimitCoordinator._lock ← this module 3. BackpressureController (lock-free — reads are atomic) 4. CentralLearningStore._lock (future — Stage 5)
Attributes¶
Classes¶
RateLimitEvent
dataclass
¶
A single rate limit event reported by a job.
RateLimitCoordinator
¶
In-memory daemon-level mirror of instrument rate limits.
Fed by the baton adapter's write-through (#206): every
RateLimitHit the baton processes is reported here via
JobManager._on_rate_limit. The baton itself already enforces
cross-job dispatch backoff (its instrument state is baton-wide);
this mirror exists so daemon-level consumers — backpressure,
submit-time warnings, the scheduler — see the same limits.
Keys are instrument names; entries self-expire via per-entry
resume times (active_limits filters, prune_stale removes).
The async is_rate_limited method satisfies the
RateLimitChecker protocol so the scheduler can consult
limits before dispatching sheets.
Source code in src/marianne/daemon/rate_coordinator.py
Attributes¶
Methods:¶
report_rate_limit
async
¶
Record a rate limit hit for an instrument.
If a limit is already active for the instrument, the resume time is extended to whichever is later (existing or newly reported).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument
|
str
|
Instrument that was rate-limited (e.g.
|
required |
wait_seconds
|
float
|
Suggested wait duration in seconds. |
required |
job_id
|
str
|
Job that encountered the limit. |
required |
sheet_num
|
int
|
Sheet that triggered the limit. |
required |
Source code in src/marianne/daemon/rate_coordinator.py
is_rate_limited
async
¶
Check if an instrument is currently rate-limited.
Satisfies the RateLimitChecker protocol used by
GlobalSheetScheduler.next_sheet().
The model parameter is accepted for protocol compatibility
but currently unused — limits are tracked per instrument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument
|
str
|
Instrument to check. |
required |
model
|
str | None
|
Unused; accepted for protocol compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
float
|
|
Source code in src/marianne/daemon/rate_coordinator.py
clear_limits
async
¶
Clear active rate limits, optionally for a specific instrument.
Removes the active limit entry so is_rate_limited() will
return False immediately. Event history is preserved for
diagnostics — only the active limit is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument
|
str | None
|
If provided, clear only this instrument's limit.
If |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of limits cleared. |
Source code in src/marianne/daemon/rate_coordinator.py
prune_stale
async
¶
Remove expired events and limits.
Called periodically by ResourceMonitor._loop() to prevent
unbounded memory growth. report_rate_limit() also prunes
on each call via the active write path, but this periodic
prune ensures cleanup even during quiet periods.
Returns:
| Type | Description |
|---|---|
int
|
Number of stale events removed. |