Commit 01bd657
authored
Parameterize queued_jobs_aggregate ClickHouse query (#8007)
**Impact:** AWS autoscaler Lambda and any future consumers of the
`queued_jobs_aggregate` HUD API endpoint
**Risk:** low
## What
Replaces hardcoded filter values in the `queued_jobs_aggregate`
ClickHouse query with named parameters that have sensible defaults, and
adds support for a `defaults` mechanism in `queryClickhouseSaved` so
callers can omit parameters and get the previous behavior.
## Why
The `queued_jobs_aggregate` query powers the AWS autoscaler
(`scale-up-chron` Lambda) that decides how many CI runners to spin up
based on queued job counts. The query previously hardcoded the
queued-time threshold (30 min), max age window (3 days), and org filter
(`pytorch`, `pytorch-labs`, `meta-pytorch`) — making it impossible for
different callers (e.g., a per-repo autoscaler or a new fleet) to
customize behavior without duplicating the query.
Part of the work tracked in
[pytorch/ci-infra#499](pytorch/ci-infra#499)
and companion changes in
[pytorch/ci-infra#500](pytorch/ci-infra#500) and
[jeanschmidt/actions-runner-controller#1](jeanschmidt/actions-runner-controller#1).
## How
- Introduced a `"defaults"` key in `params.json` — a new convention for
ClickHouse saved queries. When a caller omits a parameter, the default
value is used instead, preserving backward compatibility for existing
callers that pass `{}`.
- Used ClickHouse's `toIntervalMinute()` / `toIntervalDay()` functions
instead of `INTERVAL` literals to accept parameterized values.
- Added an optional `repo` filter (`{repo: String}`) that, when empty
string (the default), matches all repos — allowing callers to scope
results to a single repository without changing the query structure.
## Changes
- **`torchci/clickhouse_queries/queued_jobs_aggregate/query.sql`**
- `INTERVAL 30 MINUTE` → `toIntervalMinute({queuedThresholdMinutes:
Int64})`
- `INTERVAL 3 DAY` → `toIntervalDay({maxAgeDays: Int64})`
- Hardcoded org list → `{orgs: Array(String)}` parameter
- Added optional `{repo: String}` filter (no-op when empty string)
- **`torchci/clickhouse_queries/queued_jobs_aggregate/params.json`**
- Declared four typed parameters: `queuedThresholdMinutes`,
`maxAgeDays`, `orgs`, `repo`
- Added `defaults` block preserving the original hardcoded values (30
min, 3 days, pytorch orgs, all repos)
- Added a test case with zeroed-out / empty values
- **`torchci/lib/clickhouse.ts`**
- `queryClickhouseSaved` now reads `defaults` from `params.json` and
falls back to them when `inputParams` does not provide a value (via
`inputParams[key] !== undefined ? inputParams[key] : defaults[key]`)
- Backward compatible: existing queries without a `defaults` key default
to `{}` via `?? {}`
## Notes
- This is the first query to use the `defaults` convention. Other
queries can adopt the same pattern if needed.
- The autoscaler Lambda currently calls this endpoint with no parameters
— it will automatically get the default values and see no behavior
change.
- The companion ci-infra PR
([#500](pytorch/ci-infra#500)) updates the
autoscaler to pass custom parameters when needed.
## Testing
- Verify existing behavior is preserved: call
`/api/clickhouse/queued_jobs_aggregate?parameters={}` and confirm the
response matches the previous hardcoded query results.
- Test parameter overrides: call with explicit `queuedThresholdMinutes`,
`maxAgeDays`, `orgs`, and `repo` values and verify the results reflect
the custom filters.
- Test partial overrides: call with only some parameters (e.g., just
`repo`) and confirm defaults are applied for the rest.
- Run `yarn build` in `torchci/` to verify TypeScript compilation.
Signed-off-by: Jean Schmidt <contato@jschmidt.me>1 parent 865c77c commit 01bd657
3 files changed
Lines changed: 32 additions & 14 deletions
Lines changed: 21 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
Lines changed: 4 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
54 | | - | |
| 52 | + | |
| 53 | + | |
55 | 54 | | |
56 | 55 | | |
57 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
92 | 90 | | |
93 | 91 | | |
94 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
| |||
0 commit comments