Public: anyone can read this; the crew can change it.
A held fetch judges each round's haves together: three git processes a round, not three a have #17539
Merged 12h 51m ago, live in production
held-batch → main 0 commits 2 threads · 2 unresolved
2 files
changed +220
−1
src/git/own_refs.ts +58 −1
| @@ -417,6 +417,60 @@ export async function reachableFromOwnRefs( | ||
| 417 | 417 | return true; |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | /** | |
| 421 | * {@link reachableFromOwnRefs} for each of `oids` at once, the same verdict id by id (#16876): one | |
| 422 | * `for-each-ref`, one type lookup and one `rev-list` for all the commits, where asking id by id | |
| 423 | * spent those three for every id. A runner's fetch sends dozens of haves in a round, and the | |
| 424 | * forge's own repository has seventeen thousand refs to read for each. A commit is beyond the refs | |
| 425 | * exactly when `rev-list <commits> --not --all` prints it: one a ref reaches is excluded. A tree or | |
| 426 | * a file is still asked alone, as before. | |
| 427 | */ | |
| 428 | export async function reachableEach( | |
| 429 | dir: string, | |
| 430 | oids: readonly string[], | |
| 431 | ): Promise<Map<string, boolean>> { | |
| 432 | const now = Date.now(); | |
| 433 | const verdict = new Map<string, boolean>(); | |
| 434 | let left: string[] = []; | |
| 435 | for (const oid of new Set(oids)) { | |
| 436 | if (knownReached(dir, oid, now)) verdict.set(oid, true); | |
| 437 | else if (!FULL.test(oid)) verdict.set(oid, false); | |
| 438 | else left.push(oid); | |
| 439 | } | |
| 440 | if (left.length === 0) return verdict; | |
| 441 | const tips = await ask(dir, ["for-each-ref", "--format=%(objectname) %(*objectname)"]); | |
| 442 | if (tips.code !== 0) { | |
| 443 | for (const oid of left) verdict.set(oid, false); | |
| 444 | return verdict; | |
| 445 | } | |
| 446 | const atTip = new Set(tips.out.split(/\s+/).filter((t) => t !== "")); | |
| 447 | const refsKey = String(Bun.hash(tips.out)); | |
| 448 | const tipped = left.filter((oid) => atTip.has(oid)); | |
| 449 | keepReached(dir, tipped, now); | |
| 450 | for (const oid of tipped) verdict.set(oid, true); | |
| 451 | left = left.filter((oid) => !atTip.has(oid)); | |
| 452 | for (const oid of left.filter((oid) => knownRefused(dir, refsKey, oid))) verdict.set(oid, false); | |
| 453 | left = left.filter((oid) => !verdict.has(oid)); | |
| 454 | if (left.length === 0) return verdict; | |
| 455 | ||
| 456 | const types = await typesIn(dir, left); | |
| 457 | const commits = left.filter((oid) => types.get(oid) === "commit"); | |
| 458 | for (const oid of left.filter((oid) => !types.has(oid))) verdict.set(oid, false); | |
| 459 | if (commits.length > 0) { | |
| 460 | const beyond = await ask(dir, ["rev-list", "--stdin", "--not", "--all"], `${commits.join("\n")}\n`); | |
| 461 | if (beyond.code !== 0) for (const oid of commits) verdict.set(oid, false); | |
| 462 | else { | |
| 463 | const out = new Set(beyond.out.split("\n").filter((line) => line !== "")); | |
| 464 | const reached = commits.filter((oid) => !out.has(oid)); | |
| 465 | keepReached(dir, reached, now); | |
| 466 | for (const oid of commits) verdict.set(oid, !out.has(oid)); | |
| 467 | } | |
| 468 | } | |
| 469 | for (const oid of left.filter((oid) => types.has(oid) && types.get(oid) !== "commit")) | |
| 470 | verdict.set(oid, await reachableFromOwnRefs(dir, [oid])); | |
| 471 | return verdict; | |
| 472 | } | |
| 473 | ||
| 420 | 474 | /** |
| 421 | 475 | * Whether a walk of everything `dir`'s refs reach meets every one of `oids`; |
| 422 | 476 | * it stops at the last one found, and answers no past `WALK_BOUND`. |
| @@ -813,14 +867,17 @@ export function heldNegotiation( | ||
| 813 | 867 | const judge = async (ids: Set<string>): Promise<void> => { |
| 814 | 868 | const fresh = [...ids].filter((id) => !verdicts.has(id) && !allowed.has(id)); |
| 815 | 869 | const held = await typesIn(dir, fresh.filter((id) => FULL.test(id))); |
| 870 | // The held ones are asked together, at most ASKED_MAX in all; the rest are dropped unasked. | |
| 871 | const asking: string[] = []; | |
| 816 | 872 | for (const id of fresh) { |
| 817 | 873 | if (!held.has(id)) verdicts.set(id, FULL.test(id)); |
| 818 | 874 | else if (asked >= ASKED_MAX) verdicts.set(id, false); |
| 819 | 875 | else { |
| 820 | 876 | asked++; |
| 821 | verdicts.set(id, await reachableFromOwnRefs(dir, [id])); | |
| 877 | asking.push(id); | |
| 822 | 878 | } |
| 823 | 879 | } |
| 880 | if (asking.length > 0) for (const [id, ok] of await reachableEach(dir, asking)) verdicts.set(id, ok); | |
| 824 | 881 | }; |
| 825 | 882 | |
| 826 | 883 | const run = async (bytes: Uint8Array): Promise<Uint8Array> => { |
test/held-batch.test.ts added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | /** | |
| 2 | * A held reader's haves are judged together (`reachableEach`, `src/git/own_refs.ts`, #16876): the | |
| 3 | * same verdict id by id as asking `reachableFromOwnRefs` one at a time, for a commit the fork's | |
| 4 | * refs reach, one behind a tip, the lender's own commit and file, an id nothing holds and a short | |
| 5 | * one; and a round of haves costs a fixed few git processes, not three for each. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm, writeFile } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 12 | import { heldNegotiation, reachableEach, reachableFromOwnRefs } from "../src/git/own_refs.ts"; | |
| 13 | ||
| 14 | let root = ""; | |
| 15 | let lender = ""; | |
| 16 | let work = ""; | |
| 17 | const ids: Record<string, string> = {}; | |
| 18 | ||
| 19 | async function git(cwd: string, args: string[]): Promise<string> { | |
| 20 | const proc = Bun.spawn(["git", ...args], { cwd, env: gitClientEnv(), stdin: "ignore", stdout: "pipe", stderr: "pipe" }); | |
| 21 | const [out, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]); | |
| 22 | if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`); | |
| 23 | return out.trim(); | |
| 24 | } | |
| 25 | ||
| 26 | async function commitFile(name: string, content: string): Promise<{ commit: string; blob: string }> { | |
| 27 | await writeFile(join(work, "f.txt"), content); | |
| 28 | await git(work, ["add", "f.txt"]); | |
| 29 | await git(work, ["commit", "-qm", name]); | |
| 30 | return { commit: await git(work, ["rev-parse", "HEAD"]), blob: await git(work, ["rev-parse", "HEAD:f.txt"]) }; | |
| 31 | } | |
| 32 | ||
| 33 | /** A fork borrowing the lender's objects, whose only ref is `tip`. */ | |
| 34 | async function fork(name: string, tip: string): Promise<string> { | |
| 35 | const dir = join(root, name); | |
| 36 | await git(root, ["init", "-q", "--bare", dir]); | |
| 37 | await writeFile(join(dir, "objects/info/alternates"), `${join(lender, "objects")}\n`); | |
| 38 | await git(dir, ["update-ref", "refs/heads/main", tip]); | |
| 39 | return dir; | |
| 40 | } | |
| 41 | ||
| 42 | beforeAll(async () => { | |
| 43 | root = await mkdtemp(join(tmpdir(), "held-batch-")); | |
| 44 | lender = join(root, "lender.git"); | |
| 45 | work = join(root, "work"); | |
| 46 | await git(root, ["init", "-q", "--bare", lender]); | |
| 47 | await git(root, ["init", "-q", "-b", "main", work]); | |
| 48 | const old = await commitFile("old", "old\n"); | |
| 49 | const shared = await commitFile("shared", "shared\n"); | |
| 50 | await git(work, ["push", "-q", lender, "HEAD:refs/heads/main"]); | |
| 51 | const secret = await commitFile("secret", "secret\n"); | |
| 52 | await git(work, ["push", "-q", lender, "HEAD:refs/heads/private"]); | |
| 53 | Object.assign(ids, { | |
| 54 | tip: shared.commit, | |
| 55 | behind: old.commit, | |
| 56 | sharedFile: shared.blob, | |
| 57 | secretCommit: secret.commit, | |
| 58 | secretFile: secret.blob, | |
| 59 | absent: "1".repeat(40), | |
| 60 | short: shared.commit.slice(0, 12), | |
| 61 | }); | |
| 62 | }); | |
| 63 | afterAll(() => rm(root, { recursive: true, force: true })); | |
| 64 | ||
| 65 | test("together, each id gets the verdict it gets asked alone", async () => { | |
| 66 | const together = await reachableEach(await fork("a.git", ids.tip!), Object.values(ids)); | |
| 67 | const alone = await fork("b.git", ids.tip!); | |
| 68 | for (const [name, id] of Object.entries(ids)) expect([name, together.get(id)]).toEqual([name, await reachableFromOwnRefs(alone, [id])]); | |
| 69 | expect(together.get(ids.tip!)).toBe(true); | |
| 70 | expect(together.get(ids.behind!)).toBe(true); | |
| 71 | expect(together.get(ids.secretCommit!)).toBe(false); | |
| 72 | expect(together.get(ids.secretFile!)).toBe(false); | |
| 73 | }); | |
| 74 | ||
| 75 | test("a round of haves is a few git processes, however many haves it holds", async () => { | |
| 76 | const dir = await fork("c.git", ids.tip!); | |
| 77 | // Haves of commits the fork reaches, and the lender's secret, in one round. | |
| 78 | const haves = [ids.behind!, ids.secretCommit!, ids.tip!]; | |
| 79 | const pkt = (line: string) => `${(line.length + 4).toString(16).padStart(4, "0")}${line}`; | |
| 80 | const round = new TextEncoder().encode(`${haves.map((h) => pkt(`have ${h}\n`)).join("")}0000`); | |
| 81 | const spawn = Bun.spawn.bind(Bun); | |
| 82 | let spawned = 0; | |
| 83 | (Bun as { spawn: typeof Bun.spawn }).spawn = ((cmd: unknown, opts?: unknown) => { | |
| 84 | spawned++; | |
| 85 | return (spawn as (a: unknown, b?: unknown) => ReturnType<typeof Bun.spawn>)(cmd, opts); | |
| 86 | }) as typeof Bun.spawn; | |
| 87 | let out: Uint8Array; | |
| 88 | try { | |
| 89 | out = await heldNegotiation(dir).push(round); | |
| 90 | } finally { | |
| 91 | (Bun as { spawn: typeof Bun.spawn }).spawn = spawn; | |
| 92 | } | |
| 93 | const text = new TextDecoder().decode(out); | |
| 94 | // The secret is dropped; what the fork reaches is passed on. | |
| 95 | expect(text).not.toContain(ids.secretCommit!); | |
| 96 | expect(text).toContain(`have ${ids.behind!}`); | |
| 97 | // Types, refs and one rev-list for the round, whatever its size (it was three a have). | |
| 98 | expect(spawned).toBeLessThanOrEqual(4); | |
| 99 | }); | |
| 100 | ||
| 101 | /** | |
| 102 | * A lender of `n` commits on main and one private commit, made by one fast-import; a fork of main. | |
| 103 | */ | |
| 104 | async function many(name: string, n: number): Promise<{ fork: string; commits: string[]; secret: string; tree: string }> { | |
| 105 | const dir = join(root, `${name}-lender.git`); | |
| 106 | await git(root, ["init", "-q", "--bare", dir]); | |
| 107 | let stream = ""; | |
| 108 | for (let i = 1; i <= n; i++) | |
| 109 | stream += `commit refs/heads/main\nmark :${i}\ncommitter t <t@t> ${1_700_000_000 + i} +0000\ndata 2\nc\n${i > 1 ? `from :${i - 1}\n` : ""}M 100644 inline f.txt\ndata ${String(i).length}\n${i}\n\n`; | |
| 110 | stream += `commit refs/heads/private\ncommitter t <t@t> 1700009999 +0000\ndata 2\ns\nfrom :${n}\nM 100644 inline secret.txt\ndata 6\nsecret\n\n`; | |
| 111 | const p = Bun.spawn(["git", "fast-import", "--quiet"], { cwd: dir, env: gitClientEnv(), stdin: new Blob([stream]), stdout: "ignore", stderr: "ignore" }); | |
| 112 | expect(await p.exited).toBe(0); | |
| 113 | const commits = (await git(dir, ["rev-list", "refs/heads/main"])).split("\n"); | |
| 114 | const secret = await git(dir, ["rev-parse", "refs/heads/private"]); | |
| 115 | const tree = await git(dir, ["rev-parse", "refs/heads/main~3^{tree}"]); | |
| 116 | const fork = join(root, `${name}-fork.git`); | |
| 117 | await git(root, ["init", "-q", "--bare", fork]); | |
| 118 | await writeFile(join(fork, "objects/info/alternates"), `${join(dir, "objects")}\n`); | |
| 119 | await git(fork, ["update-ref", "refs/heads/main", commits[0]!]); | |
| 120 | return { fork, commits, secret, tree }; | |
| 121 | } | |
| 122 | ||
| 123 | test("many ids, commits and not, together and alone: the same verdicts, and the lender's commit refused", async () => { | |
| 124 | const a = await many("m1", 70); | |
| 125 | const b = await many("m2", 70); | |
| 126 | const pick = (x: typeof a) => [...x.commits, x.secret, x.tree, "2".repeat(40)]; | |
| 127 | const together = await reachableEach(a.fork, pick(a)); | |
| 128 | const alone = new Map<string, boolean>(); | |
| 129 | for (const id of pick(b)) alone.set(id, await reachableFromOwnRefs(b.fork, [id])); | |
| 130 | // The two lenders are made alike, so their ids match one for one. | |
| 131 | expect(pick(a).map((id) => together.get(id))).toEqual(pick(b).map((id) => alone.get(id))); | |
| 132 | expect(together.get(a.secret)).toBe(false); | |
| 133 | expect(together.get(a.tree)).toBe(true); | |
| 134 | expect([...together.values()].filter((v) => v).length).toBe(71); | |
| 135 | }); | |
| 136 | ||
| 137 | test("what is learned is kept alike: a reached id is not asked again, either way", async () => { | |
| 138 | const a = await many("k1", 5); | |
| 139 | await reachableEach(a.fork, a.commits); | |
| 140 | const spawn = Bun.spawn.bind(Bun); | |
| 141 | let spawned = 0; | |
| 142 | (Bun as { spawn: typeof Bun.spawn }).spawn = ((cmd: unknown, opts?: unknown) => { | |
| 143 | spawned++; | |
| 144 | return (spawn as (x: unknown, y?: unknown) => ReturnType<typeof Bun.spawn>)(cmd, opts); | |
| 145 | }) as typeof Bun.spawn; | |
| 146 | try { | |
| 147 | for (const id of a.commits) expect(await reachableFromOwnRefs(a.fork, [id])).toBe(true); | |
| 148 | expect((await reachableEach(a.fork, a.commits)).size).toBe(a.commits.length); | |
| 149 | } finally { | |
| 150 | (Bun as { spawn: typeof Bun.spawn }).spawn = spawn; | |
| 151 | } | |
| 152 | expect(spawned).toBe(0); | |
| 153 | }); | |
| 154 | ||
| 155 | test("past ASKED_MAX a round's held haves are dropped unasked, as before: 64 asked, the rest not", async () => { | |
| 156 | const a = await many("x1", 70); | |
| 157 | const pkt = (line: string) => `${(line.length + 4).toString(16).padStart(4, "0")}${line}`; | |
| 158 | const round = new TextEncoder().encode(`${a.commits.map((h) => pkt(`have ${h}\n`)).join("")}0000`); | |
| 159 | const text = new TextDecoder().decode(await heldNegotiation(a.fork).push(round)); | |
| 160 | const kept = a.commits.filter((c) => text.includes(`have ${c}`)); | |
| 161 | expect(kept).toEqual(a.commits.slice(0, 64)); | |
| 162 | }); |
new above
offline
Next: commits found beyond the refs are not kept as refused (keepRefused), so a repeating lender-only have is re-walked every round.