Skip to content

feat(tasks): report queue demand per class for autoscalers - #2184

Closed
c-thiel wants to merge 1 commit into
ct/task-drain-idle-exitfrom
ct/task-queue-demand
Closed

c-thiel wants to merge 1 commit into
ct/task-drain-idle-exitfrom
ct/task-queue-demand

Conversation

@c-thiel

@c-thiel c-thiel commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Stacked on #2183.

An autoscaler such as KEDA can now ask how many workers a task queue needs, per demand class. The metrics port (default 9000) answers GET /task-queue/{queue}/demand?class=<name> with counts only:

{"demand": 7, "due": 4, "heartbeat-expired": 1, "running": 2}
  • due: scheduled and due, what a pick can take now. running: running or asked to stop with a live heartbeat. heartbeat-expired: running or asked to stop with an expired heartbeat (per-project and per-warehouse timeouts as in the pick). demand is the sum, the value KEDA scales on.
  • A queue registers named resource demand ranges with TaskQueueRegistry::register_demand_classes; without class the whole queue is counted. Legacy queue names are counted and accepted in the path.
  • Unknown queue or class: 404 without a query. Database error: 503. Read-only maintenance mode: all counts 0 for any queue and class (such a process registers no built-in queues).
  • One statement at default isolation, on the read pool. Each process keeps a count for 5 s per queue and class (single flight), so the database sees at most one count per class per replica per 5 s.
  • No authentication, like /metrics; the answer has no project, warehouse or table names.

The metrics port is now an axum router: /health answers OK, /metrics and every other path still return the Prometheus text, so scrape configs do not change. Lakekeeper runs the histogram upkeep itself, which the exporter's own listener did before.

Index: task_queue_name_status_idx becomes (queue_name, status, scheduled_for, task_id, (COALESCE(resource_demand, 0))), and the pick passes both range bounds as non-null values. A worker whose class has nothing due no longer reads the due tasks of all other classes from the table on every poll (measured 190-620 ms → 10 ms with 200 k due tasks), also in a generic plan. With task_id in the key the index order is the pick's order, so a batch of tasks with one scheduled_for needs no sort (10-12 ms → 0.06 ms per pick).

Migration: the index is rebuilt in 20261015120000, which is not in a release. A development database that already applied it fails the checksum check at startup and must be dropped and created again.

Tests: sqlx test with exact counts at the range edges, NULL demand, due vs. not yet due, running / should-stop / expired heartbeats, a second project with its own heartbeat timeout, legacy names and an empty range; EXPLAIN of the pick and the due count as generic plans (range in Index Cond, no Filter, no Sort in the pick); endpoint tests (JSON shape, cache per class, 404 without a query, read-only, 503 not cached); registry lookup and class validation; metrics router paths.

Release notes

  • Autoscalers can read how many workers a task queue needs from GET /task-queue/{queue}/demand on the metrics port, optionally per demand class (?class=<name>). The answer is JSON with demand, due, heartbeat-expired and running, cached for 5 seconds per replica. See Monitoring → Task Queue Demand.
  • Workers that pick only part of a queue by resource demand find their tasks faster when other parts have a large backlog.

Downstream follow-up

  • New store method CatalogStore::task_queue_demand_impl (required) and CatalogTaskOps::task_queue_demand. Plus uses PostgresBackend only, so nothing to implement there.
  • New TaskQueueRegistry::register_demand_classes and the types TaskDemandClass, TaskQueueDemand, TaskQueueDemandRequest. Plus registers its size classes with it after register_queue for compaction (the catalog registers the compaction queue even with 0 workers, so the catalog pods answer for every class). QueueRegistration is unchanged.
  • lakekeeper::metrics::get_axum_layer_and_install_recorder(port) loses its cancellation-token argument and returns a MetricsServer (serve it with MetricsServer::serve(routes, token)) instead of an ExporterFuture. Plus does not call it.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 88f10118-e7cf-40f9-bd58-e992e0964de9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

The metrics port answers `GET /task-queue/{queue}/demand?class=<name>` with the due, heartbeat-expired and running tasks of a queue and their sum, `demand`, for autoscalers such as KEDA. Queues name their resource demand ranges with `TaskQueueRegistry::register_demand_classes`; without a class the whole queue is counted. An unknown queue or class is a 404 without a query, read-only maintenance mode reports 0 for any queue and class, and each process keeps a count for 5 seconds per queue and class. The count is one statement on the read pool and resolves heartbeat timeouts like the pick.

The metrics port is an axum router now: `/health` answers `OK`, `/metrics` and every other path return the Prometheus text as before, and Lakekeeper runs the histogram upkeep itself.

`task_queue_name_status_idx` becomes `(queue_name, status, scheduled_for, task_id, COALESCE(resource_demand, 0))`, and the pick passes both range bounds as non-null values, so a worker whose range has nothing due skips the due tasks of other ranges in the index, also in a generic plan, and the index order is the pick's order. The index is rebuilt in `20261015120000`, which is not in a release; a development database that already applied it must be dropped and created again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@c-thiel
c-thiel force-pushed the ct/task-queue-demand branch from ddc542c to 286ce37 Compare October 26, 2026 07:43
@github-actions

Copy link
Copy Markdown
Contributor

✅ PR Title Formatted Correctly

The title of this PR match the correct format. Thank you!

@c-thiel

c-thiel commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Replaced by #2187, which carries the same change on main and is merged.

@c-thiel c-thiel closed this Oct 26, 2026
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.

1 participant