Skip to content

fix(git): rebuild a cached delta bundle once the mirror tip moves - #466

Draft
stuartwdouglas wants to merge 2 commits into
mainfrom
sleeper/bundle-stale-on-tip-move
Draft

stuartwdouglas wants to merge 2 commits into
mainfrom
sleeper/bundle-stale-on-tip-move

Conversation

@stuartwdouglas

@stuartwdouglas stuartwdouglas commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Follows #467 (bitmaps off for incremental upload-pack and bundle builds), now on main.

Problem

Delta bundles are cached under the snapshot base alone (bundleCacheKey = upstreamURL + ".bundle." + base) for bundle-cache-ttl (default 2h). The first bundle built after a snapshot is served to every later clone, so each clone fetches the rest of the gap in its freshen step. On cash-server, three cold clones all got the same 718 KB bundle, and freshen then took 18–22s per clone. That fetch is served by cachew's mirror (Roxy redirects git reads to cachew), and before #467 it paid the bitmap fill-in. #467 makes that fetch cheap; this PR makes the gap it has to cover small.

Change

  • Cached bundles record the mirror HEAD they were built to (X-Cachew-Bundle-Tip).
  • On a plain GET, if this replica's mirror has moved past that tip (merge-base --is-ancestor), the bundle is rebuilt through the existing bundleBuilds coalescing. pregenerateBundle uses the same check.
  • Ranged resumes keep serving the entry they started on.
  • A tip this replica doesn't have came from a newer replica and is served as-is, so replicas don't rebuild each other's bundles.

The tip is checked on lookup rather than added to the key. The key is computed before GetOrCreate: with the tip in the key, a pod with no local mirror would have to clone before it could serve from the shared cache, and every tip would leave a 2h object behind.

Build cost

Builds run with bitmaps off since #467, so a rebuild scales with the delta (on the bb-block staging mirror a 47-commit bundle took about 1s, against 14s with bitmaps).

Build rate

At most one build is in flight per (repo, base) per replica, because bundleBuilds coalesces them. A rebuild needs a bundle request and a new mirror HEAD since the last build. Mirror fetches are gated by ref-check-interval (10s), ensure-refs and the 15m periodic fetch, so the worst case is one build per mirror fetch, not one per clone.

Client tip hint (?tip=<sha>, second commit)

The client waits for ensure-refs before requesting the bundle, but the GET can land on a different replica (at least 3 sit behind cachew.mesh), whose mirror may be behind. The bundle request now accepts an optional tip=<sha>, sent by bloxlet in squareup/blox#5600:

  • tip is validated with commitSHARe. Anything else is ignored rather than rejected, so older clients are unaffected.
  • If the cached bundle's tip already contains tip, it's served as-is.
  • Otherwise a replica whose mirror lacks tip calls freshenMirror (the same one the unknown-base path uses), then takes the stale-tip path. The miss/build path does the same before createBundle.
  • A bogus or unreachable tip costs at most one upstream fetch per ref-check-interval (10s) per replica.

Production 0.8.5 predates #452, so its ensure-refs returns an empty HEAD and clients send no tip until a release that includes #452 is deployed.

Testing

  • New TestBundleRebuiltWhenMirrorTipMoves fails with the check stubbed out and passes with it.
  • New TestBundleFromNewerReplicaIsKept passes.
  • New TestBundleTipFreshensMirrorBehindClient and TestBundleBogusTipFetchesAtMostOncePerInterval fail with the freshen stubbed out and pass with it.
  • go test -race ./internal/strategy/git/ ./internal/gitclone/ passes and tsk is clean.
  • Not yet exercised against a deployed cachew.

🤖 Generated with Claude Code

@stuartwdouglas
stuartwdouglas requested a review from a team as a code owner November 24, 2026 21:45
@stuartwdouglas
stuartwdouglas requested review from joshfriend and removed request for a team November 24, 2026 21:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T06:23:06.642166Z 45df1f6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@stuartwdouglas
stuartwdouglas marked this pull request as draft November 25, 2026 06:28
stuartwdouglas added a commit that referenced this pull request Nov 25, 2026
…#467)

## Problem

When an upload-pack request's haves aren't covered by a bitmap, git
first fills one in by walking the whole tree on the haves side
(`haves/boundary` → `boundary-fill-in`). On a large monorepo mirror,
that costs about the same for a 47-commit gap as for weeks of history.
It affects two paths:

- **Incremental fetches served from the mirror.** On Roxy workstations,
git reads to github.com are redirected to cachew. A clone's catch-up
fetch (want new HEAD, have the snapshot commit) lands in
`serveReadyRepo` → `git http-backend` → upload-pack.
- **Delta bundle builds** (`git bundle create HEAD ^base`), which a
clone waits on when the bundle isn't cached yet.

## Change

- `serveReadyRepo` already buffers the request body for the "not our
ref" replay. A `/git-upload-pack` request whose body has `have` lines is
now marked incremental. Detection uses the same gzip-aware 64 KiB
inspect window as clone counting; `decodeUploadPackPrefix` is factored
out of `inspectUploadPackBody`.
- Incremental requests run http-backend with `GIT_CONFIG_COUNT=1`,
`pack.useBitmaps=false`. This is command-scope config, and the mirror's
own config is unchanged. The "not our ref" replay gets the same
override.
- Clones (wants only), v2 `ls-refs` and `info/refs` keep bitmaps,
because a full object walk benefits from them. v0 and v2 requests both
take this path.
- A fetch whose first `have` falls past the 64 KiB window is treated as
a clone and keeps bitmaps. That is today's behaviour.
- Bundles are built with `-c pack.useBitmaps=false`.

## Measurements

These were taken on the bb-block staging cash-server mirror (git 2.52.1,
MIDX bitmap fresh and covering HEAD, boundary traversal on). Each fetch
is a protocol v2 `fetch` through `git http-backend` with the CGI env
cachew uses, two runs each:

| have | bitmaps on | bitmaps off |
|---|---|---|
| HEAD~47 | 13–15s, 4.3 MB | <1s, 1.1 MB |
| HEAD~2000 (~6 days) | 24s, 72 MB | 8s, 42 MB |

`pack-objects` runs on the same mirror:
- HEAD~5: 13s on vs <1s off.
- HEAD~150: 14s vs 1s.
- ~1 month (10,859 commits): 51s and 316 MB vs 32s and 204 MB.
- A 47-commit delta bundle build: 14s vs about 1s.

Production baseline for a cold bundle build:
`snapshot.bundle?base=<main~47>` against bb-block prod took 37.2s to
first byte (4.49 MB). Part of that may be time spent waiting behind a
mirror fetch under `WithFetchExclusion`.

Not measured: a client advertising many unrelated branch tips as haves.
If that proves slow, a cap on `have` lines is the limit to add.

## Testing

- `TestUploadPackSkipsBitmapsOnlyForIncrementalFetches` drives the real
POST handler and a real `git http-backend` on the fixture mirror. A PATH
wrapper records the `pack.usebitmaps` value each backend run sees. There
are six cases:
  - v2 fetch with haves
  - v0 fetch with haves
  - gzipped v0 fetch with haves
  - fetch replayed after "not our ref" (both runs `false`)
  - v0 clone
  - v2 ls-refs

The clone and ls-refs cases expect the value unset. With detection
forced off, the three with-haves cases fail.
- The fake-git bundle test now matches `bundle create` anywhere in argv,
because `-c` shifts the arguments.
- `go test -race ./internal/strategy/git/ ./internal/gitclone/` passes
and `tsk` is clean.

Follow-up: #466, now a draft, will carry the bundle staleness fix and
the `tip=` hint after this merges.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Delta bundles are cached under the snapshot base alone for
bundle-cache-ttl (2h). The first bundle built after a snapshot was
served to every later clone, so each clone fetched the rest of the gap
from upstream. On cash-server that GitHub freshen took 18-22s per clone,
because GitHub took 18s to build a 47-commit pack.

Cached bundles now record the mirror HEAD they were built to. A plain
GET whose replica mirror has moved past that tip rebuilds through the
existing bundleBuilds coalescing. Ranged resumes keep the entry they
started on. A tip this replica lacks came from a newer replica and is
served as-is, so replicas don't rebuild each other's bundles.

Rebuilds are synchronous. They stay cheap because bundles are built
without bitmaps: about 1s for 47 commits on the staging mirror.

Co-authored-by: Sleeper Service <a6659a6442a259afd31da6e85f71b9e9f3fe127dd3cc88d2ae9fb544922bcb3c>
A client's ensure-refs and its delta bundle GET can reach different
replicas, so the bundle replica's mirror may still be behind the HEAD
ensure-refs just fetched, and the stale-tip check then misses.

The bundle URL now accepts an optional tip=<sha>, the HEAD the client's
ensure-refs resolved. A cached bundle that already contains tip is
served as-is. Otherwise, a replica whose mirror lacks tip runs
freshenMirror before deciding, the same fetch the unknown-base path
uses. Its ref-check gate (10s) and the fetch timeout bound that work, so
a bogus or unreachable tip costs at most one fetch per interval. A tip
that isn't a full commit SHA is ignored. Older clients send no tip and
are unaffected.

Co-authored-by: Sleeper Service <a6659a6442a259afd31da6e85f71b9e9f3fe127dd3cc88d2ae9fb544922bcb3c>
@stuartwdouglas
stuartwdouglas force-pushed the sleeper/bundle-stale-on-tip-move branch from 45df1f6 to 7b64399 Compare November 25, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant