-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(supervisor): add cluster pod-count dequeue backpressure source #4027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+352
−26
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9633a9e
feat(supervisor): parse apiserver pod-object count from metrics scrape
nicktrn d76fed4
feat(supervisor): pod-count backpressure source with hysteresis
nicktrn d96449a
feat(supervisor): apiserver metrics fetcher for cluster pod count
nicktrn 8bb439f
feat(supervisor): config for pod-count backpressure source
nicktrn 0e12327
feat(supervisor): select pod-count backpressure source, expose cluste…
nicktrn f7b3e7f
docs(supervisor): server-changes note for pod-count backpressure
nicktrn e2eefd8
fix(supervisor): source-aware backpressure config, 5s scrape interval…
nicktrn 3179918
feat(supervisor): evaluate backpressure sources independently and OR …
nicktrn a3e578b
test(supervisor): cover both backpressure sources enabled together
nicktrn 0160156
fix(supervisor): scrape apiserver /metrics over https so TLS verifies…
nicktrn 33929da
fix(supervisor): scrape timeout + schema-level pod-count hysteresis g…
nicktrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: supervisor | ||
| type: feature | ||
| --- | ||
|
|
||
| The supervisor can pause dequeuing when the Kubernetes cluster is saturated, based on the cluster's total pod count. Opt-in and off by default. |
95 changes: 95 additions & 0 deletions
95
apps/supervisor/src/backpressure/k8sPodCountSignalSource.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { parsePodCount, K8sPodCountSignalSource } from "./k8sPodCountSignalSource.js"; | ||
|
|
||
| describe("parsePodCount", () => { | ||
| it("reads the pods object count", () => { | ||
| const text = [ | ||
| "# HELP apiserver_storage_objects Number of stored objects", | ||
| "# TYPE apiserver_storage_objects gauge", | ||
| 'apiserver_storage_objects{resource="pods"} 8421', | ||
| 'apiserver_storage_objects{resource="configmaps"} 17', | ||
| ].join("\n"); | ||
| expect(parsePodCount(text)).toBe(8421); | ||
| }); | ||
|
|
||
| it("is tolerant of extra labels in any order", () => { | ||
| const text = 'apiserver_storage_objects{group="",resource="pods",extra="x"} 12'; | ||
| expect(parsePodCount(text)).toBe(12); | ||
| }); | ||
|
|
||
| it("parses scientific notation", () => { | ||
| const text = 'apiserver_storage_objects{resource="pods"} 1.2e+04'; | ||
| expect(parsePodCount(text)).toBe(12000); | ||
| }); | ||
|
|
||
| it("throws when the pods metric is absent", () => { | ||
| const text = 'apiserver_storage_objects{resource="configmaps"} 17'; | ||
| expect(() => parsePodCount(text)).toThrow(/not found/); | ||
| }); | ||
|
|
||
| it("throws on a non-finite value (e.g. 1e999)", () => { | ||
| const text = 'apiserver_storage_objects{resource="pods"} 1e999'; | ||
| expect(() => parsePodCount(text)).toThrow(); | ||
| }); | ||
|
|
||
| it("throws on a negative value", () => { | ||
| const text = 'apiserver_storage_objects{resource="pods"} -5'; | ||
| expect(() => parsePodCount(text)).toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| function metrics(count: number): string { | ||
| return `apiserver_storage_objects{resource="pods"} ${count}`; | ||
| } | ||
|
|
||
| describe("K8sPodCountSignalSource", () => { | ||
| it("engages at the engage threshold and reports the count", async () => { | ||
| const counts: number[] = []; | ||
| const source = new K8sPodCountSignalSource({ | ||
| fetchMetrics: async () => metrics(10000), | ||
| engageThreshold: 10000, | ||
| releaseThreshold: 5000, | ||
| reportPodCount: (c) => counts.push(c), | ||
| }); | ||
| const verdict = await source.read(); | ||
| expect(verdict.engaged).toBe(true); | ||
| expect(typeof verdict.ts).toBe("number"); | ||
| expect(counts).toEqual([10000]); | ||
| }); | ||
|
|
||
| it("does not engage below the engage threshold", async () => { | ||
| const source = new K8sPodCountSignalSource({ | ||
| fetchMetrics: async () => metrics(9999), | ||
| engageThreshold: 10000, | ||
| releaseThreshold: 5000, | ||
| }); | ||
| expect((await source.read()).engaged).toBe(false); | ||
| }); | ||
|
|
||
| it("stays engaged in the hysteresis band, releases only below release threshold", async () => { | ||
| let count = 10000; | ||
| const source = new K8sPodCountSignalSource({ | ||
| fetchMetrics: async () => metrics(count), | ||
| engageThreshold: 10000, | ||
| releaseThreshold: 5000, | ||
| }); | ||
| expect((await source.read()).engaged).toBe(true); // engage | ||
| count = 7000; | ||
| expect((await source.read()).engaged).toBe(true); // band -> still engaged | ||
| count = 4999; | ||
| expect((await source.read()).engaged).toBe(false); // below release -> off | ||
| count = 7000; | ||
| expect((await source.read()).engaged).toBe(false); // band again -> stays off | ||
| }); | ||
|
|
||
| it("propagates scrape failures (monitor fails open on throw)", async () => { | ||
| const source = new K8sPodCountSignalSource({ | ||
| fetchMetrics: async () => { | ||
| throw new Error("connection refused"); | ||
| }, | ||
| engageThreshold: 10000, | ||
| releaseThreshold: 5000, | ||
| }); | ||
| await expect(source.read()).rejects.toThrow("connection refused"); | ||
| }); | ||
| }); |
46 changes: 46 additions & 0 deletions
46
apps/supervisor/src/backpressure/k8sPodCountSignalSource.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { BackpressureSignalSource, BackpressureVerdict } from "./backpressureMonitor.js"; | ||
|
|
||
| // Reads the apiserver's stored-pod-object count from a Prometheus /metrics scrape. | ||
| const POD_COUNT_RE = /^apiserver_storage_objects\{[^}]*resource="pods"[^}]*\}\s+([0-9.eE+]+)/m; | ||
|
|
||
| export function parsePodCount(metricsText: string): number { | ||
| const match = metricsText.match(POD_COUNT_RE); | ||
| if (!match) { | ||
| throw new Error('apiserver_storage_objects{resource="pods"} not found in metrics'); | ||
| } | ||
| const value = Number(match[1]); | ||
| if (!Number.isFinite(value)) { | ||
| throw new Error(`unparseable pod count: ${match[1]}`); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| export type K8sPodCountSignalSourceOptions = { | ||
| fetchMetrics: () => Promise<string>; | ||
| engageThreshold: number; | ||
| releaseThreshold: number; | ||
| reportPodCount?: (count: number) => void; | ||
| }; | ||
|
|
||
| // Engage/release with hysteresis so a count hovering near the line doesn't flap. | ||
| export class K8sPodCountSignalSource implements BackpressureSignalSource { | ||
| private engaged = false; | ||
|
|
||
| constructor(private readonly opts: K8sPodCountSignalSourceOptions) {} | ||
|
|
||
| async read(): Promise<BackpressureVerdict> { | ||
| const text = await this.opts.fetchMetrics(); | ||
| const count = parsePodCount(text); | ||
| this.opts.reportPodCount?.(count); | ||
|
|
||
| if (this.engaged) { | ||
| if (count < this.opts.releaseThreshold) { | ||
| this.engaged = false; | ||
| } | ||
| } else if (count >= this.opts.engageThreshold) { | ||
| this.engaged = true; | ||
| } | ||
|
|
||
| return { engaged: this.engaged, ts: Date.now() }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
|
|
||
| // Mock std-env before importing env.ts so the module-level `Env.parse(stdEnv)` | ||
| // doesn't fail in a test environment that lacks required vars. | ||
| vi.mock("std-env", () => ({ | ||
| env: { | ||
| TRIGGER_API_URL: "http://localhost:3030", | ||
| TRIGGER_WORKER_TOKEN: "test-token", | ||
| MANAGED_WORKER_SECRET: "test-secret", | ||
| OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318", | ||
| }, | ||
| })); | ||
|
|
||
| const { Env } = await import("./env.js"); | ||
|
|
||
| // Minimal env that satisfies all required fields; everything else has defaults. | ||
| const base = { | ||
| TRIGGER_API_URL: "http://localhost:3030", | ||
| TRIGGER_WORKER_TOKEN: "test-token", | ||
| MANAGED_WORKER_SECRET: "test-secret", | ||
| OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318", | ||
| }; | ||
|
|
||
| describe("Env superRefine - backpressure source awareness", () => { | ||
| it("pod-count source can be enabled without a Redis host", () => { | ||
| expect(() => | ||
| Env.parse({ | ||
| ...base, | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_POD_COUNT_ENABLED: "true", | ||
| }) | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it("redis source requires a Redis host", () => { | ||
| expect(() => | ||
| Env.parse({ | ||
| ...base, | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_ENABLED: "true", | ||
| }) | ||
| ).toThrow(); | ||
| }); | ||
|
|
||
| it("both sources can be enabled together (with a Redis host)", () => { | ||
| expect(() => | ||
| Env.parse({ | ||
| ...base, | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_ENABLED: "true", | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_REDIS_HOST: "localhost", | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_POD_COUNT_ENABLED: "true", | ||
| }) | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it("rejects pod-count release >= engage when the source is enabled", () => { | ||
| expect(() => | ||
| Env.parse({ | ||
| ...base, | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_POD_COUNT_ENABLED: "true", | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_POD_COUNT_ENGAGE: "100", | ||
| TRIGGER_DEQUEUE_BACKPRESSURE_POD_COUNT_RELEASE: "100", | ||
| }) | ||
| ).toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.