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

Runner jobs' git-upload-pack: count the work per job, then cut it so the serving thread is not held #17984

opened this

Merged 16h 23m ago, live in production

Runner jobs that fetch the same commit now share one pack. The first job's fetch is built once, and every other job sending the same request reads that result instead of starting git again. Each fetch also writes what it cost to the run's log.

Notes

opened this

Runner jobs that fetch the same commit now share one pack. The first job's fetch is built once, and every other job sending the same request reads that result instead of starting git again. Each fetch also writes what it cost to the run's log.

Counter (src/git/fetch_work.ts). Every runner-job info/refs and git-upload-pack request writes a git refs: … / git fetch: … line to the run's log. The line gives the git processes spawned and the number of waits before the answer could start. Processes are counted where they are spawned: upload-pack in smart-http.ts, mkfifo in pull-pipe.ts, and the negotiation checks in own_refs.ts. For one job, taken from test/runner-fetch-share.test.ts:

  • Before: git fetch: built, 2 processes, 3 awaits (mkfifo + upload-pack)
  • After, for every other job on the same commit: git fetch: shared, the answer another job's fetch built, 0 processes, 1 await

The cut (src/git/pack_share.ts).

  • One build per request. The first fetch's upload-pack output is written under dataDir/job-packs as git produces it. Any job sending the same negotiation for the same commit ids follows that file as it grows, so it gets bytes before the build finishes.
  • Key. The key is the repository, the job's commit ids and a SHA-256 of the negotiation bytes, never a ref name, so a moved branch can't get a stale pack. The advertisement is shared the same way, keyed by repository and ids.
  • Limits. A finished answer lives 60 s, with at most 8 at once; past that a fetch builds its own, as before.
  • No blocking. All file reads and writes use node:fs/promises.
  • Builds outlive the client. A shared build runs on a request of the forge's own, so the first client hanging up doesn't kill a pack others are reading.
  • Token first. The token check (held(ctx)) and the "only the job's ids" refusal still run before anything is built.
  • Per forge. The share is kept per app in a WeakMap (in runners.ts), not a module-level Map.

Tests.

  • test/runner-fetch-share.test.ts: 6 concurrent jobs on one commit give builds() === 1, 2 processes in total and 5 shared. It also checks that a different commit builds again, that a want outside the job's ids builds nothing, that the advertisement is built once, and that a share expires after 60 s.
  • test/request-path-sync.test.ts now also scans src/git/smart-http.ts, pack_share.ts and fetch_work.ts, with no allowance, so the fetch path can't gain a *Sync call. The existing allowed list is unchanged.

What I left out:

  • No count of bytes read synchronously. The fetch path has no synchronous reads, so that number would always be 0. The extended sync-call test enforces it instead.
  • Fork fetches aren't shared. Fetches limited to a fork's own refs (ownRefsOnly) and transfers that carry their own release callback still build per request.
  • Two files not added to the sync scan. own_refs.ts (push-only readdirSync) and pull-pipe.ts (pullPipeSync, used only over SSH) would each need a new allowed row, which would grow the list.

Checks. I ran the runner, checks-runner, smart-http/upload-pack, pull-pipe, module-state, timers-named, unused-exports and hollow tests, and the request-path sync test; all pass. bun x tsc --noEmit and bun scripts/gate.ts are clean. I didn't run the full suite; the forge's check runs it on this push. The keep_plan and estimate tools weren't available in this run, so I recorded no plan or estimate.

Try: on the preview, run a check whose runner jobs fetch one commit, then open the run's log. You should see one git fetch: built line, and git fetch: shared … 0 processes for the other jobs.

🤖 Generated with Claude Code

Closes #17975

2 earlier comments
commented

Runner jobs that fetch the same commit now share one pack. The first job's fetch is built once, and every other job sending the same request reads that result instead of starting git again. Each fetch also writes what it cost to the run's log.

Counter (src/git/fetch_work.ts). Every runner-job info/refs and git-upload-pack request writes a git refs: … / git fetch: … line to the run's log. The line gives the git processes spawned and the number of waits before the answer could start. Processes are counted where they are spawned: upload-pack in smart-http.ts, mkfifo in pull-pipe.ts, and the negotiation checks in own_refs.ts. For one job, taken from test/runner-fetch-share.test.ts:

  • Before: git fetch: built, 2 processes, 3 awaits (mkfifo + upload-pack)
  • After, for every other job on the same commit: git fetch: shared, the answer another job's fetch built, 0 processes, 1 await

The cut (src/git/pack_share.ts).

  • One build per request. The first fetch's upload-pack output is written under dataDir/job-packs as git produces it. Any job sending the same negotiation for the same commit ids follows that file as it grows, so it gets bytes before the build finishes.
  • Key. The key is the repository, the job's commit ids and a SHA-256 of the negotiation bytes, never a ref name, so a moved branch can't get a stale pack. The advertisement is shared the same way, keyed by repository and ids.
  • Limits. A finished answer lives 60 s, with at most 8 at once; past that a fetch builds its own, as before.
  • No blocking. All file reads and writes use node:fs/promises.
  • Builds outlive the client. A shared build runs on a request of the forge's own, so the first client hanging up doesn't kill a pack others are reading.
  • Token first. The token check (held(ctx)) and the "only the job's ids" refusal still run before anything is built.
  • Per forge. The share is kept per app in a WeakMap (in runners.ts), not a module-level Map.

Tests.

  • test/runner-fetch-share.test.ts: 6 concurrent jobs on one commit give builds() === 1, 2 processes in total and 5 shared. It also checks that a different commit builds again, that a want outside the job's ids builds nothing, that the advertisement is built once, and that a share expires after 60 s.
  • test/request-path-sync.test.ts now also scans src/git/smart-http.ts, pack_share.ts and fetch_work.ts, with no allowance, so the fetch path can't gain a *Sync call. The existing allowed list is unchanged.

What I left out:

  • No count of bytes read synchronously. The fetch path has no synchronous reads, so that number would always be 0. The extended sync-call test enforces it instead.
  • Fork fetches aren't shared. Fetches limited to a fork's own refs (ownRefsOnly) and transfers that carry their own release callback still build per request.
  • Two files not added to the sync scan. own_refs.ts (push-only readdirSync) and pull-pipe.ts (pullPipeSync, used only over SSH) would each need a new allowed row, which would grow the list.

Checks. I ran the runner, checks-runner, smart-http/upload-pack, pull-pipe, module-state, timers-named, unused-exports and hollow tests, and the request-path sync test; all pass. bun x tsc --noEmit and bun scripts/gate.ts are clean. I didn't run the full suite; the forge's check runs it on this push. The keep_plan and estimate tools weren't available in this run, so I recorded no plan or estimate.

Try: on the preview, run a check whose runner jobs fetch one commit, then open the run's log. You should see one git fetch: built line, and git fetch: shared … 0 processes for the other jobs.

🤖 Generated with Claude Code

commented

Runner jobs that fetch the same commit now share one pack. The first job's fetch is built once, and every other job sending the same request reads that result instead of starting git again. Each fetch also writes what it cost to the run's log.

Counter (src/git/fetch_work.ts). Every runner-job info/refs and git-upload-pack request writes a git refs: … / git fetch: … line to the run's log. The line gives the git processes spawned and the number of waits before the answer could start. Processes are counted where they are spawned: upload-pack in smart-http.ts, mkfifo in pull-pipe.ts, and the negotiation checks in own_refs.ts. For one job, taken from test/runner-fetch-share.test.ts:

  • Before: git fetch: built, 2 processes, 3 awaits (mkfifo + upload-pack)
  • After, for every other job on the same commit: git fetch: shared, the answer another job's fetch built, 0 processes, 1 await

The cut (src/git/pack_share.ts).

  • One build per request. The first fetch's upload-pack output is written under dataDir/job-packs as git produces it. Any job sending the same negotiation for the same commit ids follows that file as it grows, so it gets bytes before the build finishes.
  • Key. The key is the repository, the job's commit ids and a SHA-256 of the negotiation bytes, never a ref name, so a moved branch can't get a stale pack. The advertisement is shared the same way, keyed by repository and ids.
  • Limits. A finished answer lives 60 s, with at most 8 at once; past that a fetch builds its own, as before. Leftover files are cleared by a one-shot job-pack-sweep timer run under background.
  • No blocking. All file reads and writes use node:fs/promises.
  • Builds outlive the client. A shared build runs on a request of the forge's own, so the first client hanging up doesn't kill a pack others are reading.
  • Token first. The token check (held(ctx)) and the "only the job's ids" refusal still run before anything is built.
  • Per forge. The share is kept per app in a WeakMap (in runners.ts), not a module-level Map.

Tests.

  • test/runner-fetch-share.test.ts: 6 concurrent jobs on one commit give builds() === 1, 2 processes in total and 5 shared. It also checks that a different commit builds again, that a want outside the job's ids builds nothing, that the advertisement is built once, and that a share expires after 60 s.
  • test/request-path-sync.test.ts now also scans src/git/smart-http.ts, pack_share.ts and fetch_work.ts, with no allowance, so the fetch path can't gain a *Sync call. The existing allowed list is unchanged.
  • test/job-slices.test.ts: the check failed on this test because the new job-pack-sweep timer was neither timed nor listed in UNTIMED. It is now listed with its reason. The test was right; this was a missing entry, not a test change. That test, timers-named and the new share tests pass (18 tests).

What I left out:

  • No count of bytes read synchronously. The fetch path has no synchronous reads, so that number would always be 0. The extended sync-call test enforces it instead.
  • Fork fetches aren't shared. Fetches limited to a fork's own refs (ownRefsOnly) and transfers that carry their own release callback still build per request.
  • Two files not added to the sync scan. own_refs.ts (push-only readdirSync) and pull-pipe.ts (pullPipeSync, used only over SSH) would each need a new allowed row, which would grow the list.

Try: on the preview, run a check whose runner jobs fetch one commit, then open the run's log. You should see one git fetch: built line, and git fetch: shared … 0 processes for the other jobs.

🤖 Generated with Claude Code

commented

I've addressed sentinel's points 1, 3, 4 and 5 and pushed. Point 2 isn't done: I don't have a real job's log lines to show (explained below). The type check is clean, the gate passes, and 171 tests across 21 files pass, picked by hand because scripts/fast.ts named almost the whole suite. They include the dead-code, duplicate-import, module-state, request-path-sync and job-slices tests.

Runner jobs' fetches through /api/v1/runner/jobs/:id/git/* now record what each fetch cost and write it in the run's log. Jobs that fetch the same commit with the same negotiation share one upload-pack build; the rest spawn no git process. This round changes:

  • The counter now measures instead of hand-counting (src/git/fetch_work.ts). There are no more hand-written work.awaits++ lines. waited() wraps each wait the handler really makes: the negotiation body read, each git question in own_refs.ts, the transfer permit, the fifo and the shared directory being made ready. It counts each wait and how long it took. A new syncBytes field is counted where own_refs.ts calls readSync. All of these do nothing outside a counted fetch. The log line now reads like git fetch: built, 2 processes, 4 awaits (3 ms), 0 bytes read synchronously.
  • The disk used by shared packs is capped by size (src/git/pack_share.ts):
    • When the files on disk come to more than 1 GiB, finished packs are dropped oldest first.
    • A pack that grows past 256 MiB leaves the share, and its file is deleted when its last reader finishes. If every reader has gone, git is stopped.
    • A new pack is shared only while the files are under 1 GiB and fewer than two shared builds are running. Anything else is built for its own fetch alone, as before.
    • The worst case is just under 1 GiB plus two packs being built.
    • I didn't take the suggestion to cut the key down to the wants. The runner fetches into a mirror it keeps, so its negotiation sends have lines, and ignoring them would serve much larger packs.
  • The ref advertisement is no longer shared. It lists refs, and it is one small process, so each fetch gets its own and a branch that moved is shown as it is now. The header comment now says only packs are shared.
  • test/request-path-sync.test.ts now says why own_refs.ts and pull-pipe.ts are left out of the scan. own_refs.ts calls readdirSync and readSync only in refDeltaBases, which only a push's quarantine reaches. pullPipeSync is called only from smart-ssh.ts.

New tests in test/runner-fetch-share.test.ts cover:

  • dropping oldest-first once the byte limit is passed;
  • a pack over the size limit leaving the share and its file being deleted;
  • no new sharing while the files are at the limit;
  • a moved branch showing up in the next advertisement;
  • the counter counting real waits and synchronous reads, and nothing outside a fetch.

What I couldn't do: point 2 asks for git fetch: built … and git fetch: shared … lines from a real job on the preview. The preview has no runner, and my token isn't an admin there (/api/v1/admin/runners answers 404), so I can't register one. The only before/after I have comes from the test, which runs the real handler on a real repository: before git fetch: built, 2 processes, 4 awaits (3 ms), 0 bytes read synchronously; after git fetch: shared, the answer another job's fetch built, 0 processes, 1 await (0 ms), 0 bytes read synchronously. The real lines will appear in every runner job's log once this is deployed.

How to try it: bun test test/runner-fetch-share.test.ts test/request-path-sync.test.ts. After it is deployed, a run's log shows one git refs: … and one git fetch: … line per job, and the jobs after the first on the same commit say shared.

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs