Skip to content

feat(metrics): emit a per-pool deadline-driven required-replicas gauge - #538

Open
sohom-cs wants to merge 7 commits into
llm-d:mainfrom
sohom-cs:feat/pool-deadline-required-replicas
Open

sohom-cs wants to merge 7 commits into
llm-d:mainfrom
sohom-cs:feat/pool-deadline-required-replicas

Conversation

@sohom-cs

@sohom-cs sohom-cs commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Stacked on #526: until it merges, this branch carries its 4 commits, and the change here is the last three. Adds the per-pool gauge agreed in #526's review: llm_d_async_async_pool_deadline_required_replicas, labelled by pool_name.

  • Worker pools get an optional deadline_scaling block next to gate_type/gate_params, with the queue block's four fields. A queue's own throughput_rps overrides the pool's.
  • Each poll sums the pool's work per bucket across its queues (a queue's unexpired items divided by its throughput), then divides once with the pool's settings. RequiredReplicas now takes that work and AddWork builds it; the per-queue gauge is the one-queue case.
  • A failed deadline read on any of the pool's queues drops the pool's series for that poll; a pool with no queues reads 0.

Not done: pool_name is the async worker pool, so two worker pools that send to one deployment still undercount; that needs an explicit scaling_group.

Why is this change needed?

Queues that share a deployment need their work summed before the division: sum() of the per-queue gauges over-counts and max() under-counts. The two examples from the review are test cases (2, not 3; 4, not 2).

How was this tested?

  • Unit tests added/updated
  • Integration/e2e tests added/updated
  • Manual testing performed

Table tests for the pool sums and the throughput override; poll-loop tests for the pool series (set, absent without settings, 0 without queues, dropped on a failed read); a table case for work in the bucket around the cold start; loadWorkerPools rejects an invalid pool block.

go vet and go test -race on the changed packages, make test and make lint pass (Go 1.26.0, Darwin arm64). Not run: a KEDA ScaledObject against the pool gauge.

Checklist

  • Commits are signed off (git commit -s) per DCO
  • Code follows project contributing guidelines
  • Tests pass locally (make test)
  • Linters pass (make lint)
  • Documentation updated (if applicable)

Related Issues

Fixes #465

Release note

Adds `llm_d_async_async_pool_deadline_required_replicas`, the replicas a worker pool's deployment needs for the deadlines of all its queues, from a new optional `deadline_scaling` block on worker pools. A queue's own `throughput_rps` overrides the pool's.

Add a per-queue deadline_scaling block (throughput_rps, cold_start_seconds,
max_replicas, optional safety_factor) and the gauge
llm_d_async_async_deadline_required_replicas. Each backlog poll computes it
next to the deadline-proximity snapshot, from the same cumulative counts:
the maximum over bucket boundaries t with queued items of
B(t) / (R * (t - C) * safety_factor), rounded up and capped at
max_replicas; items due at or before the cold start pin it to
max_replicas. Both the redis-sortedset and sql transports report those
counts, so both get the gauge; flows expose a queue's settings through a
DeadlineScaling method, and the pipeline module does not change.

Fixes llm-d#465

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Compare the demand with max_replicas while it is still a float: a tiny
throughput_rps or safety_factor can push it past the int range or to
+Inf, and the conversion then gave -9223372036854775808 on linux/amd64.
Also return at least 1 for any positive demand, so the 1e-9 rounding
tolerance can no longer turn queued work into 0 replicas.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Review on llm-d#526: late work shouldn't drive scaling, and a failed backlog
read should drop the required-replicas series so KEDA's fallback applies
instead of reading 0. QueueBacklogStat gets ExpiringCountsFailed, since
zeroed counts look like an empty queue. The docs say the per-queue value
is right only when each queue has its own deployment, and to aggregate
processor replicas with max().

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Queues that share a deployment need their deadline work summed before
the division: summing their per-queue gauges over-counts and max()
under-counts (llm-d#526 review).

Worker pools get an optional deadline_scaling block, and the backlog
poller publishes llm_d_async_async_pool_deadline_required_replicas,
labelled by pool_name. Within each horizon, a queue's unexpired items
are divided by its throughput_rps (the queue's own, else the pool's)
and summed across the pool before the division by (t - C) * s.
RequiredReplicas now takes this work per bucket and AddWork builds it,
so a single queue is a pool of one. A failed deadline read on any of a
pool's queues drops the pool's series.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T12:20:09.249533Z c407518 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c407518ee1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/observability.md Outdated
| `llm_d_async_async_inference_latency_time_millis` | Histogram | Time in milliseconds spent calling `llm-d-router` (or other inference gateway), measured around each request attempt. Isolates "model time" from "queue time". Always registered. |
| `llm_d_async_async_queue_residence_time_millis` | Histogram | Time in milliseconds a message spent buffered in-process, from broker ingestion until a worker pulled it for processing. Measures the async delay introduced by the system (queue time). Always registered. |
| `llm_d_async_async_deadline_proximity_millis` | Histogram | Snapshot histogram of time in milliseconds remaining until each queued item's deadline, rebuilt once per backlog poll from exact cumulative `ZCOUNT` counts per bucket (`le="0"` holds items past their deadline but still queued; higher buckets are cumulative, so every item counts in each bucket it expires in). Redis sorted-set only; Cloud Pub/Sub cannot expose per-item deadlines. Because each poll replaces the snapshot, the series is not monotonic — `rate()` is meaningless; use `histogram_quantile` per scrape. The `_sum` is estimated from bucket midpoints. |
| `llm_d_async_async_deadline_required_replicas` | Gauge | Replicas a queue needs to finish its queued items before their deadlines, recomputed each backlog poll from the deadline-proximity counts and the queue's `deadline_scaling` settings (`throughput_rps` R, `cold_start_seconds` C, `max_replicas`, optional `safety_factor` s): the maximum over bucket boundaries t > 0 of B(t) / (R · (t − C) · s), where B(t) counts the items due within t that haven't expired, rounded up and capped at `max_replicas`. Expired items don't count; items due within C pin it to `max_replicas`. A failed deadline read drops the series for that poll, so KEDA's `fallback` applies. Only queues with `deadline_scaling`, on `redis-sortedset` and `sql`. The value is per queue, so it is right only when each queue has its own deployment; for queues that share one, use the pool gauge below. Every processor replica publishes the same value, so aggregate across them with `max()`. Meant as a KEDA trigger with threshold 1. |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require KEDA to reject empty results before relying on fallback

With the documented max(...) aggregation, deleting the last matching series produces an empty query result. KEDA's Prometheus scaler defaults ignoreNullValues to true, so it interprets that result as zero rather than a scaler error; configuring fallback alone does not change this. A deadline-read outage can therefore request a scale-down instead of activating the fallback promised here. Document that the trigger must set ignoreNullValues: "false" alongside its fallback configuration.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, with the default ignoreNullValues: true KEDA reads the empty result as 0. Fixed in 11da710: the docs now say the trigger needs ignoreNullValues: "false" for the fallback to apply.

Comment thread pkg/deadlinescaling/deadlinescaling.go Outdated
Comment on lines +73 to +76
if horizon <= coldStart {
return c.MaxReplicas
}
need = math.Max(need, work[i]/((horizon-coldStart).Seconds()*safety))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle cold starts between histogram boundaries

When cold_start_seconds falls between bucket boundaries, this guard misses items due before cold start. For example, with throughput 2, cold start 90 seconds, and a cap of 10, an item due in 75 seconds contributes zero through the 60-second bucket and one starting at 120 seconds. The calculator returns 1 rather than the documented cap of 10 because no nonempty horizon satisfies this condition. Include a count boundary at cold start or conservatively handle the bucket that straddles it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 11da710: new work in the bucket that starts before C now pins the result to max_replicas, so your example returns 10. Test: due in the bucket around the cold start.

Comment thread pkg/server/runner.go Outdated
Comment on lines +550 to +553
if work, ok := poolWork[name]; ok && !poolPartial[name] {
metrics.SetPoolDeadlineRequiredReplicas(name, cfg.RequiredReplicas(buckets, work))
} else {
metrics.RemovePoolDeadlineRequiredReplicas(name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Publish zero demand for configured pools with no queues

After a Redis hot reload removes the final queue from a configured pool, QueueBacklog succeeds but poolWork[name] has no entry, so this branch deletes the pool gauge. Empty queue configurations are explicitly supported by LoadSortedSetConfigAllowEmptyQueues; this is zero demand, not an unavailable source. Consumers configured to invoke fallback on missing metrics will retain fallback replicas instead of scaling the drained pool down. Publish zero for a successful empty-pool snapshot and reserve deletion for failed or unsupported reads.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 11da710: after a successful read, a pool with no queues reads 0; a failed read still drops the series. Test: TestPollBacklogPublishesZeroForPoolWithoutQueues.

A cold start between two deadline-proximity boundaries let items that
may be due before it count as ordinary work: new work in the bucket that
starts before the cold start now pins the result to max_replicas, as
work due within it does.

A pool with no queues after a successful read, e.g. after a reload
removed the last one, now reads 0 instead of losing its series, so an
autoscaler scales it down instead of holding its fallback replicas.

The docs now note that KEDA uses its fallback for a missing series only
when the trigger sets ignoreNullValues to "false"; by default it reads
the empty result as 0.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
@jtechapps jtechapps mentioned this pull request Nov 16, 2026
19 of 30 tasks
@jtechapps jtechapps added this to the v1.1.0 milestone Nov 16, 2026
@jtechapps

Copy link
Copy Markdown
Collaborator

Note to other maintainers do not merge before #526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: autoscaling metric producer — emit deadline-driven required-replicas gauge for KEDA

2 participants