Repository navigation
fix(ci): bound source cache snapshots so PR bursts cannot fill main's claim #5542
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
Open
shepherdjerred
wants to merge
3
commits into
main
Choose a base branch
from
ci-source-cache-admission
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6ac223d
fix(ci): bound source cache snapshots so PR bursts cannot fill main's…
shepherdjerred c226356
fix(ci): reap killed source-cache writers and harden admission
shepherdjerred 2c65ffe
test(ci): pass the default-branch flag to the checkout-commit emit test
shepherdjerred 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
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
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
145 changes: 145 additions & 0 deletions
145
packages/woodpecker-config-extension/src/checkout/admission.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,145 @@ | ||
| import { mkdir, readdir, rename, rm, statfs } from "node:fs/promises"; | ||
| import nodePath from "node:path"; | ||
| import { z } from "zod"; | ||
| import { SnapshotSchema } from "./git.ts"; | ||
| const { join } = nodePath; | ||
|
|
||
| /** | ||
| * Whose checkout publishes a snapshot. Default-branch work may fill more of a | ||
| * claim than a proposal, so a burst of pull requests never takes the headroom | ||
| * main's own checkouts publish into. | ||
| */ | ||
| export const SourceCacheTierSchema = z.enum(["main", "proposal"]); | ||
| export type SourceCacheTier = z.infer<typeof SourceCacheTierSchema>; | ||
|
|
||
| /** | ||
| * Share of a claim's capacity that declared snapshot bytes may reach. | ||
| * | ||
| * Every snapshot is a complete object store, about 1.16 GiB for the monorepo, | ||
| * and ZFS charges each block clone to the claim's quota in full. Collection | ||
| * runs every five minutes and keeps 8 GiB, so without a ceiling a burst of | ||
| * distinct commits between two runs fills the claim, and from then on every | ||
| * checkout on it fails with EDQUOT, hits included. The quota above the main | ||
| * ceiling stays free for metadata, ownership setup and snapshot times. | ||
| */ | ||
| export const SOURCE_CACHE_CEILING: Readonly<Record<SourceCacheTier, number>> = { | ||
| main: 0.85, | ||
| proposal: 0.7, | ||
| }; | ||
|
|
||
| const RESERVATION = "reservation.json"; | ||
| const ReservationSchema = z.object({ | ||
| bytes: z.number().int().nonnegative(), | ||
| }); | ||
| const ENTRY = /^[a-f\d]{40}(?:\.tmp-[a-f\d-]{36})?$/u; | ||
|
|
||
| /** The bytes the cache root's claim can hold. */ | ||
| export async function claimCapacity(root: string): Promise<number> { | ||
| const space = await statfs(root); | ||
| return space.blocks * space.bsize; | ||
| } | ||
|
|
||
| export function ceilingBytes(tier: SourceCacheTier, capacity: number): number { | ||
| return Math.floor(SOURCE_CACHE_CEILING[tier] * capacity); | ||
| } | ||
|
|
||
| /** Published by rename, so a concurrent survey never reads a partial file. */ | ||
| async function writeReservation(temporary: string, bytes: number) { | ||
| const partial = join(temporary, `${RESERVATION}.${crypto.randomUUID()}`); | ||
| await Bun.write(partial, JSON.stringify({ bytes })); | ||
| await rename(partial, join(temporary, RESERVATION)); | ||
| } | ||
|
|
||
| async function declared(path: string, temporary: boolean) { | ||
| try { | ||
| const json: unknown = await Bun.file( | ||
| join(path, temporary ? RESERVATION : "manifest.json"), | ||
| ).json(); | ||
| return temporary | ||
| ? ReservationSchema.parse(json).bytes | ||
| : SnapshotSchema.parse(json).objectBytes; | ||
| } catch (error) { | ||
| if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) | ||
| throw error; | ||
| // A copy that has not declared yet or was withdrawn since the listing. | ||
| // Published snapshots gain their manifest before the rename, so one | ||
| // without it was cut short by an interrupted collection; the next | ||
| // collection removes it, and a hit on it still fails its manifest read. | ||
| if (!temporary) | ||
| console.warn(`Source cache snapshot without a manifest: ${path}`); | ||
| // Either way the caller counts it at full size, which can only refuse more. | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sum every snapshot's declared object bytes: published manifests and the | ||
| * reservations of copies in flight. A copy that has not declared yet (a | ||
| * writer between `mkdir` and its reservation) or a snapshot that lost its | ||
| * manifest counts as `undeclared`. A killed writer's reservation stays | ||
| * counted until the next collection removes its directory. | ||
| */ | ||
| export async function surveySnapshots(parent: string, undeclared: number) { | ||
| let total = 0; | ||
| let largest = 0; | ||
| for (const entry of await readdir(parent, { withFileTypes: true })) { | ||
| if (!entry.isDirectory() || !ENTRY.test(entry.name)) | ||
| throw new Error("Unexpected source cache snapshot entry"); | ||
| const bytes = | ||
| (await declared( | ||
| join(parent, entry.name), | ||
| entry.name.includes(".tmp-"), | ||
| )) ?? undeclared; | ||
| total += bytes; | ||
| largest = Math.max(largest, bytes); | ||
| } | ||
| return { total, largest }; | ||
| } | ||
|
|
||
| /** | ||
| * Reserve room for a snapshot before writing it. The reservation is visible | ||
| * to every other writer before this one counts, so of two concurrent writers | ||
| * at least one sees the other: both may be refused, never both admitted past | ||
| * the ceiling. Returns the temporary directory to publish from, or undefined | ||
| * when the snapshot would cross the ceiling. | ||
| */ | ||
| export async function reserveSnapshot(input: { | ||
| readonly parent: string; | ||
| readonly commit: string; | ||
| readonly bytes: number; | ||
| readonly ceiling: number; | ||
| }): Promise<string | undefined> { | ||
| await mkdir(input.parent, { recursive: true }); | ||
| const temporary = join( | ||
| input.parent, | ||
| `${input.commit}.tmp-${crypto.randomUUID()}`, | ||
| ); | ||
| await mkdir(temporary); | ||
| return (await resizeReservation({ ...input, temporary })) | ||
| ? temporary | ||
| : undefined; | ||
| } | ||
|
|
||
| /** Restate a reservation's size; on refusal its directory is removed. */ | ||
| export async function resizeReservation(input: { | ||
| readonly parent: string; | ||
| readonly temporary: string; | ||
| readonly bytes: number; | ||
| readonly ceiling: number; | ||
| }): Promise<boolean> { | ||
| try { | ||
| await writeReservation(input.temporary, input.bytes); | ||
| const { total } = await surveySnapshots(input.parent, input.bytes); | ||
| if (total <= input.ceiling) return true; | ||
| } catch (error) { | ||
| await rm(input.temporary, { recursive: true, force: true }); | ||
| throw error; | ||
| } | ||
| await rm(input.temporary, { recursive: true, force: true }); | ||
| return false; | ||
| } | ||
|
|
||
| /** A published snapshot carries its manifest, not its reservation. */ | ||
| export async function releaseReservation(temporary: string) { | ||
| await rm(join(temporary, RESERVATION)); | ||
| } | ||
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.