* perf(dispatcher): read tasks, inputs and runtimes in one statement before sending
ListTasksForDispatch returns each assigned task with its current runtime row and
the payload row holding its input, located through read_payloads, so the
dispatcher builds an action from one round trip instead of three. A task whose
runtime row is gone is not returned and not sent. A failed read still requeues
the requested tasks through a bounded recovery lookup. The scheduler no longer
publishes an empty task-assigned-bulk message for locally delivered items.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* perf(scheduler): cache dispatcher ids per worker for one second
schedule-step-runs looked up the dispatcher of every assigned worker on
each bulk of assignments (0.74 ms of the 0.86 ms step on the dispatcher-side
trace). The scheduler now keeps a 10000-entry, one-second TTL cache from
worker id to dispatcher id in front of GetDispatcherIdsForWorkers, consults
it first and queries only the worker ids it holds no entry for, adding what
comes back. There is no invalidation, matching the other repository caches:
a worker that moves dispatchers is seen within one second, the same window
as its liveness check. Workers without a dispatcher are not cached and are
queried again. The cancellation and dead-letter paths keep querying.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* perf(dispatcher): shape the dispatch read so the generic plan is chosen
ListTasksForDispatch stayed on a custom plan for every call under the pgx
default of prepared statements and plan_cache_mode=auto: with the id array
unknown the generic plan is costed at ten elements (354 against the custom
plan's 251 on the bench database), so Postgres never adopted it and every
execution paid 0.9 to 2.2 ms of planning for 0.1 to 0.3 ms of work.
The statement now reads ONE task by scalar parameters (tenant_id, task id):
the task row, its runtime row joined on the primary key, and the payload rows
read_payloads returns for one-element arrays built from that row. Nothing in
its estimate depends on a parameter value, so the generic plan costs the same
as the custom plan and is adopted on the sixth execution. The repository
queues one statement per task id in a single pgx batch
(ListTasksForDispatchBatch, hand-written over the generated constant and row
type), so a bulk of assignments is still one round trip. read_payloads
remains the only place with payload lookup rules; the Go side
(tasksForDispatchFromRows, Resolve, the recovery requeue) is unchanged.
Measured on a scratch Postgres 15.6 (three daily partitions of v1_task and
v1_payload, 30k tasks, 60k payload rows, 30k runtime rows, ANALYZEd), one
task id per execution, EXPLAIN (ANALYZE) EXECUTE under plan_cache_mode=auto;
"after 6" is the mode of the sixth and seventh execution, planning and
execution are the seventh's:
shape generic custom after 6 plan ms exec ms
current (tasks CTE x3, array param) 471.12 317.37 custom 0.836 0.130
(a) NOT MATERIALIZED tasks CTE, pkey runtime 642.55 126.54 custom 0.826 0.111
(a') MATERIALIZED variant of the same 699.40 333.66 custom 0.687 0.095
(b) per-task LATERAL read_payloads, array 464.87 100.81 custom 0.670 0.085
(c) per-task LATERAL, scalar params 84.23 84.23 generic 0.006 0.070
(c') shipped: task CTE, scalar params 202.91 202.91 generic 0.011 0.103
(c) is the cheapest plan but sqlc types the columns of a LEFT JOIN LATERAL
function as NOT NULL, which would fail to scan a task without a payload row;
(c') keeps the payload lookup in a CTE joined with ON true, which sqlc types
as nullable, and reaches the same mode. Cold connection, fresh session, per
round over seven rounds (three repetitions): main's three statements planned
2.7 to 4.2 ms in round 1 and 0.6 to 0.9 ms in rounds 2 to 6; the shipped
statement planned 3.0 to 3.4 ms in round 1, 0.6 to 0.7 ms in rounds 2 to 5,
and 0.007 ms from round 7, with execution 0.07 to 0.10 ms.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(dispatcher): keep one copy of a payload row shared by several tasks
The dispatch read runs one statement per task, so tasks sharing a
payload_reference_id each return their own copy of the owner's payload
row. tasksForDispatchFromRows appended every copy, and Resolve then
mapped every referrer onto every copy, which is quadratic in the number
of tasks sharing a row. Keep each payload row once, keyed by
(tenant_id, id, inserted_at, type).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* feat(scheduler): update the dispatcher id cache when a worker reconnects elsewhere
The scheduler's worker to dispatcher cache expired entries on a one
second TTL alone, so every cached worker was re-queried once a second
and a worker that reconnected to another dispatcher was routed to the
old one until its entry expired.
The dispatcher now publishes a worker-dispatcher-changed message to the
tenant's scheduler partition right after it records itself on a
reconnecting worker's row, carrying the worker id and the new
dispatcher id. Both Listen paths share that step through claimWorker,
which leaves a worker already on this dispatcher alone. The scheduler
routes the message into the cache with set, so the entry is replaced
on the event without a query.
With updates event-driven, the TTL moves from 1 s to 5 s, the worker
liveness window; it remains the safety net for a lost publish. An
older scheduler logs the unknown message id and drops it, and an older
dispatcher simply never sends it, so mixed-version fleets fall back to
the TTL.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* perf(dispatcher): read all assigned tasks in one statement that plans generically
The dispatcher's pre-send read queued one scalar-parameter statement per
task in a pgx batch so that each execution planned generically; Postgres
runs the queued statements serially, so a bulk of N assignments cost N
executions. ListTasksForDispatch is one statement again, taking the id
array, and still settles on the generic plan under the pgx default of
prepared statements with plan_cache_mode=auto.
Why `id = ANY($ids)` and a plain `unnest($ids)` cannot: under auto Postgres
keeps the custom plan while it costs lower than the generic one. The custom
plan sees the array as a constant and knows its length (estimate_array_length
for ANY, array_unnest_support for unnest), while the generic plan assumes
ten elements, so for short arrays the custom plan always costs less and
every execution is planned again. The statement now reads the ids through
`unnest((SELECT $ids::bigint[]))`: the scalar subquery becomes an initplan
whose output both plans estimate at ten rows, so the two costs coincide and
the generic plan is adopted on the sixth execution. The per-task payload
lookup is a LEFT JOIN LATERAL of read_payloads with one-element arrays, the
runtime match is an inner join on the primary key, and the plan probes
v1_task_runtime and v1_task by index per id with runtime partition pruning.
sqlc types the columns of a LEFT JOIN LATERAL function as NOT NULL (also
when wrapped in a subselect), which would fail to scan a task without a
payload row, so the statement, its nullable row type and the reader are
hand-written in tasks-overwrite.go like CreateTasks and the query leaves
tasks.sql. tasksForDispatchFromRows (with the payload-row dedupe), Resolve,
the dispatcher and the dispatcher id cache are unchanged.
Measured on a scratch Postgres 15.6 (three daily partitions of v1_task and
v1_payload, 30k tasks, 60k payload rows, 30k runtime rows, ANALYZEd),
PREPARE (uuid, bigint[]) and EXPLAIN (ANALYZE) EXECUTE under
plan_cache_mode=auto; "after 6" is the plan of the sixth and seventh
execution, planning and execution are the seventh's, one execution for all
ids:
shape ids generic custom after 6 plan ms exec ms
0 main's CTEs, id = ANY($ids) 1 471.29 317.53 custom 1.175 0.166
20 471.29 631.79 generic 0.010 1.635
100 471.29 1388.31 generic 0.017 30.610
A unnest($ids), LATERAL read_payloads 1 145.45 60.44 custom 0.707 0.071
20 145.45 239.91 generic 0.013 0.305
100 145.45 871.57 generic 0.018 0.992
B unnest($ids), aggregated payloads CTE 1 605.28 317.13 custom 0.891 0.140
20 605.28 914.78 generic 0.009 1.578
100 605.28 2496.53 generic 0.028 30.912
C unnest WITH ORDINALITY, LATERAL 1 145.38 60.42 custom 0.743 0.074
20 145.38 239.79 generic 0.007 0.261
100 145.38 871.05 generic 0.011 0.898
D unnest((SELECT $ids)) CTE, LATERAL 1 145.46 145.46 generic 0.006 0.075
20 145.46 145.46 generic 0.007 0.260
100 145.46 145.46 generic 0.013 0.914
D2 shipped: D without the CTEs 1 145.39 145.39 generic 0.010 0.085
20 145.39 145.39 generic 0.012 0.270
100 145.39 145.39 generic 0.017 0.950
D3 D2 with LATERAL (SELECT * FROM ...) 1 145.39 145.39 generic 0.014 0.085
20 145.39 145.39 generic 0.017 0.292
100 145.39 145.39 generic 0.018 1.014
E unnest((SELECT $ids)), payloads CTE 1 605.29 605.29 generic 0.006 0.109
20 605.29 605.29 generic 0.010 1.574
100 605.29 605.29 generic 0.014 30.427
F id = ANY((SELECT $ids)), LATERAL 1 464.91 464.91 generic 0.022 0.199
20 464.91 464.91 generic 0.011 0.263
100 464.91 464.91 generic 0.014 0.922
G id = ANY((SELECT $ids)), payloads CTE 1 699.58 699.58 generic 0.034 0.239
20 699.58 699.58 generic 0.011 2.017
100 699.58 699.58 generic 0.055 35.494
The aggregated-CTE shapes (0, B, E, G) go generic for 20 or more ids but
join the tasks to the payload CTE with an OR condition that runs in N x M,
30 ms at 100 ids. The LATERAL shapes stay at one index probe per task. D2
is D without the CTEs so sqlc.embed resolves against the table; its plan is
identical. Cold connection, fresh session, planning ms per round over seven
rounds with one id (three repetitions): 6.6/3.9/3.2 in round 1, 0.75 to
1.23 in rounds 2 to 6, 0.035/0.018/0.011 from round 7, with execution 0.10
to 0.61 ms. Through pgx on one connection, pg_prepared_statements shows
five custom plans and then generic plans from the sixth call on.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* chore(dispatcher): apply review feedback
Drops the dispatcher unit tests and the repository dispatch read test, which the end
to end suites cover, removes a narrating comment, and documents the worker dispatcher
change notification.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(dispatcher): requeue batch items whose worker is on another dispatcher
A batch start that reaches a dispatcher the worker has left now takes the same
retry path as single assignments, so the scheduler reassigns the items instead
of leaving them to their step timeout. The dispatch read marks a present payload
row by its id and reads the external id directly.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(dispatcher): requeue only batch items still assigned to the batch's worker
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(dispatcher): requeue only tasks still assigned to the message's worker after a failed read
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* chore(scheduler): drop a narrating comment
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* chore(dispatcher): use tenant pool handles for the dispatch read
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix: internal retries piling up
* fix: add query to release operator on shutdown and wire it up
* fix: mark dispatcher draining so we don't have a shutting-down dispatcher claiming an operator
* fix(dispatcher): carry the attempt's retry count so failed-read recovery never fails a newer attempt
The task-assigned-bulk message now carries the retry count of every task it
assigns, keyed by worker and task id. The dispatcher requeues a task after a
failed dispatch read only when its runtime row names the same worker and the
same retry count as the message. A task whose retry count the message does not
carry is left to the timeout path.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* chore(dispatcher): drop the recovery and message unit tests
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix: simplify a bunch after alexander's changes
* fix: embed structs to clean up the code a bit
* refactor: simplify, fix a couple edge cases, rm unused query
* fix: simplify, clean up
* fix: conflicts
* fix: restore some stuff
* fix: simplify more
---------
Co-authored-by: Alexander Belanger <alexander@hatchet.run>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>