Public: anyone can read this; the crew can change it.

Runners keep their git mirror whole: a job fetches without --depth, so the pack is only what the mirror lacks #17163

opened this

Merged 40h 57m ago, live in production

fix/runner-warm-mirror → main 0 commits

  • Merged. fix/runner-warm-mirror was merged into main as 6314cf4.

Review

Sign in to review this change.
2 files changed +133 −20
src/runner/main.ts +49 −20
@@ -106,6 +106,52 @@ function checkVars(
106106 return out;
107107 }
108108
109 /** One commit a job fetches into the mirror, how deep a shallow fetch would go, and its ref. */
110 interface MirrorTip {
111 readonly oid: string;
112 readonly depth: number;
113 readonly kept: "base" | "commit";
114 }
115
116 /** A job's tips, in order: its base (one deep, shallow), then its commit (`depth` deep). */
117 function mirrorTips(job: { readonly base?: string | undefined; readonly commit: string }, depth: number): MirrorTip[] {
118 return [...(job.base === undefined ? [] : [{ oid: job.base, depth: 1, kept: "base" as const }]), { oid: job.commit, depth, kept: "commit" as const }];
119 }
120
121 /**
122 * The fetch for one tip: `whole` takes the history a kept ref does not already reach (and makes a
123 * shallow mirror whole with --unshallow); `shallow` is the old fetch, as deep as the tip says.
124 */
125 function fetchArgs(url: string, tip: MirrorTip, mode: "whole" | "shallow", shallow: boolean): string[] {
126 const how = mode === "shallow" ? [`--depth=${tip.depth}`] : shallow ? ["--unshallow"] : [];
127 return ["fetch", "--quiet", "--no-tags", ...how, url, tip.oid];
128 }
129
130 /**
131 * Fetch a job's tips into the runner's mirror, kept whole: a fetch without --depth brings only what
132 * the mirror does not already hold, the pull's own commits once main's history is here, and none
133 * for a base older than the last job's. A shallow mirror (an older runner's) is made whole by its
134 * first fetch here. Should a whole fetch fail, the tip is fetched shallow as before, so the mirror
135 * never holds a job up. The last base and commit are kept under refs, what the next fetch offers as
136 * already had. Answers why it failed, or `undefined`.
137 */
138 export async function fillMirror(
139 mirror: string,
140 url: string,
141 job: { readonly base?: string | undefined; readonly commit: string },
142 depth: number,
143 gitFetch: (args: readonly string[]) => Promise<{ ok: true } | { ok: false; error: { stderr: string } }>,
144 ): Promise<string | undefined> {
145 for (const tip of mirrorTips(job, depth)) {
146 const shallow = existsSync(join(mirror, "shallow"));
147 let got = await gitFetch(fetchArgs(url, tip, "whole", shallow));
148 if (!got.ok) got = await gitFetch(fetchArgs(url, tip, "shallow", shallow));
149 if (!got.ok) return `git fetch ${tip.oid.slice(0, 7)}: ${got.error.stderr.trim().slice(0, 300) || "failed"}`;
150 await runGit(["update-ref", `refs/runner/${tip.kept}`, tip.oid], { cwd: mirror });
151 }
152 return undefined;
153 }
154
109155 /** A name of the forge's as a directory name of ours. */
110156 const dirName = (raw: string): string =>
111157 raw.replace(/[^A-Za-z0-9_-]/g, "").slice(0, 80) || "none";
@@ -262,26 +308,9 @@ export async function runRunner(options: RunnerOptions): Promise<RunnerEnd> {
262308 const made = await runGit(["init", "--quiet", "--bare", mirror]);
263309 if (!made.ok) return `git init: ${made.error.stderr || "failed"}`;
264310 }
265 for (const [oid, deep, kept] of [
266 ...(job.base === undefined ? [] : [[job.base, 1, "base"] as const]),
267 [job.commit, depth, "commit"] as const,
268 ]) {
269 const got = await runGit(
270 ["fetch", "--quiet", "--no-tags", `--depth=${deep}`, url, oid],
271 {
272 cwd: mirror,
273 env: auth,
274 timeoutMs: FETCH_MS,
275 signal: stop.signal,
276 },
277 );
278 if (!got.ok)
279 return `git fetch ${oid.slice(0, 7)}: ${got.error.stderr.trim().slice(0, 300) || "failed"}`;
280 // A fetch offers what its refs reach as already had: the last base and the last
281 // commit are kept under a ref, so the next fetch brings only what is new.
282 await runGit(["update-ref", `refs/runner/${kept}`, oid], { cwd: mirror });
283 }
284 return undefined;
311 return fillMirror(mirror, url, job, depth, (args) =>
312 runGit([...args], { cwd: mirror, env: auth, timeoutMs: FETCH_MS, signal: stop.signal }),
313 );
285314 },
286315 );
287316 mirrors.set(
test/runner-warm-mirror.test.ts added +84
@@ -0,0 +1,84 @@
1 /**
2 * A runner's mirror is kept whole (`fillMirror`, `runner/main.ts`): a job fetches its base and commit
3 * without --depth, so the pack holds only what the mirror lacks. On 2026-12-01 runner fetches took a
4 * median of 41 s and up to 194 s, each a shallow fetch whose pack carried the whole tree again.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { existsSync } from "node:fs";
8 import { mkdtemp, rm, writeFile } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { runGit } from "../src/git/env.ts";
12 import { fillMirror } from "../src/runner/main.ts";
13
14 let dir = "";
15 let url = "";
16 const commits: Record<string, string> = {};
17
18 async function git(cwd: string, ...args: string[]): Promise<string> {
19 const done = await runGit(args, { cwd });
20 if (!done.ok) throw new Error(`git ${args.join(" ")}: ${done.error.stderr}`);
21 return done.value.trim();
22 }
23
24 async function commit(src: string, name: string, file: string): Promise<void> {
25 await writeFile(join(src, file), `${name}\n`);
26 await git(src, "add", "-A");
27 await git(src, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "-m", name);
28 commits[name] = await git(src, "rev-parse", "HEAD");
29 }
30
31 /** Objects in a repository, loose and packed. */
32 async function objects(repo: string): Promise<number> {
33 const counts = Object.fromEntries((await git(repo, "count-objects", "-v")).split("\n").map((l) => l.split(": ") as [string, string]));
34 return Number(counts["count"]) + Number(counts["in-pack"]);
35 }
36
37 const fetchIn = (mirror: string) => (args: readonly string[]) => runGit([...args], { cwd: mirror });
38
39 beforeAll(async () => {
40 dir = await mkdtemp(join(tmpdir(), "runner-warm-mirror-"));
41 const src = join(dir, "src");
42 await git(dir, "init", "--quiet", "-b", "main", src);
43 for (const n of ["c1", "c2", "c3", "c4"]) await commit(src, n, `${n}.txt`);
44 await git(src, "switch", "--quiet", "-c", "pull", commits["c2"]!);
45 await commit(src, "p1", "p1.txt");
46 url = `file://${src}`;
47 });
48 afterAll(async () => {
49 await rm(dir, { recursive: true, force: true });
50 });
51
52 test("an older runner's shallow mirror is made whole, and an older base then fetches only the pull's commit", async () => {
53 const mirror = join(dir, "old.git");
54 await git(dir, "init", "--quiet", "--bare", mirror);
55 await git(mirror, "fetch", "--quiet", "--no-tags", "--depth=1", url, commits["c3"]!);
56 expect(existsSync(join(mirror, "shallow"))).toBe(true);
57
58 expect(await fillMirror(mirror, url, { base: commits["c3"]!, commit: commits["c4"]! }, 1, fetchIn(mirror))).toBeUndefined();
59 expect(existsSync(join(mirror, "shallow"))).toBe(false);
60 expect(await git(mirror, "rev-list", "--count", commits["c4"]!)).toBe("4");
61
62 // A base older than the last job's is already here: the pack is the pull's commit, its tree and
63 // its one new file.
64 const before = await objects(mirror);
65 expect(await fillMirror(mirror, url, { base: commits["c2"]!, commit: commits["p1"]! }, 1, fetchIn(mirror))).toBeUndefined();
66 expect((await objects(mirror)) - before).toBe(3);
67 expect(await git(mirror, "rev-parse", "refs/runner/base", "refs/runner/commit")).toBe(`${commits["c2"]}\n${commits["p1"]}`);
68 });
69
70 test("a whole fetch that fails falls back to the shallow one, and a shallow one that fails is the job's error", async () => {
71 const mirror = join(dir, "fallback.git");
72 await git(dir, "init", "--quiet", "--bare", mirror);
73 const asked: string[][] = [];
74 const failWhole = async (args: readonly string[]) => {
75 asked.push([...args]);
76 if (!args.some((a) => a.startsWith("--depth="))) return { ok: false as const, error: { stderr: "no" } };
77 return runGit([...args], { cwd: mirror });
78 };
79 expect(await fillMirror(mirror, url, { base: commits["c2"]!, commit: commits["p1"]! }, 2, failWhole)).toBeUndefined();
80 expect(asked.map((a) => a.find((x) => x.startsWith("--depth=")) ?? "whole")).toEqual(["whole", "--depth=1", "whole", "--depth=2"]);
81
82 const never = async () => ({ ok: false as const, error: { stderr: "refused\n" } });
83 expect(await fillMirror(mirror, url, { commit: commits["c4"]! }, 1, never)).toBe(`git fetch ${commits["c4"]!.slice(0, 7)}: refused`);
84 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs