Repository navigation
feat: implement hybrid cache architecture - #1246
fabiovincenzi wants to merge 84 commits into
Conversation
✅ Deploy Preview for endearing-brigadeiros-63f9d0 ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1246 +/- ##
==========================================
+ Coverage 88.98% 89.03% +0.04%
==========================================
Files 132 138 +6
Lines 7281 7586 +305
Branches 1365 1423 +58
==========================================
+ Hits 6479 6754 +275
- Misses 494 510 +16
- Partials 308 322 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
12b56bd to
734621c
Compare
Signed-off-by: Fabio Vincenzi <93596376+fabiovincenzi@users.noreply.github.com>
…zi/git-proxy into feature/hybrid-cache
06kellyjac
left a comment
There was a problem hiding this comment.
I'm looking forward to faster pulls!
Caches are a common place for logic or security problems so we may want added scrutiny and testing for this PR to be extra safe :)
Added some thoughts, not ran this locally just yet.
jescalada
left a comment
There was a problem hiding this comment.
Just a code review for now - I'll be testing the code later!
I was wondering if you could do some profiling to verify/document the speed improvements with this PR. Would be nice to try it out with massive repos (for example https://github.com/backstage/backstage which is >10GB) to see the difference.
I'll try to do some profiling soon as well 🙂
|
why on earth is backstage's repo 10GB? 😭 |
|
@06kellyjac It might be because of the tons of plugins that come with the repo (many of which have redundant components). Pulling with |
jescalada
left a comment
There was a problem hiding this comment.
Just a few more comments from me (and Claude). 👍🏼
Also, I think it'd be great to document this a bit better (for example in website/docs/deployment), so users (and agents) are aware of the caching feature even if it's disabled by default,
There was a problem hiding this comment.
Thanks for fixing all those issues! I had Claude do some benchmarking here and things are looking great. Check out this gist for the full details: https://gist.github.com/jescalada/9c0e93aec698cc5a73f3f53c2de9b3ff
I also asked it to find any other bugs or security flaws, and these are the most notable ones (the rest are at the bottom of that gist although I think they're trivial/not applicable):
AI-review:
- User config ignored
The user config is ignored for every cache setting except enabled.
cache-manager.tsbuilds thecacheManagersingleton when the module is imported.index.tsimports./src/proxy statically, beforesetConfigFile(argv.c)andinitUserConfig()run. As a resultcacheDir,maxSizeGB,maxRepositoriesandgitTimeoutSecondsalways come from the bundledproxy.config.json.Fix: read the config lazily in
CacheManager, either through getters that callgetCacheConfig()or by creating the instance on first use.
- Incompatibility with current Docker setup
The documented Docker volume stops the proxy from starting, and without it the cache is wiped on every restart.
proxyPreparations()runsfs.rmSync('./.remote', { recursive: true, force: true }). With the suggested-v $(pwd)/cache:/app/.remote/cache mount, that call throwsEBUSY: resource busy or locked,rmdir './.remote/cache'(reproduced with a bind mount), andstart()rejects. Without a mount, the defaultcacheDirlives inside .remote, so every restart deletes the cache.Fix: move the default
cacheDirout of.remote(or skip it in the startup sweep), and remove leftover.staging-*folders at startup. As things stand they leak (8.5 GiB per crash on Backstage) whenevercacheDiris outside .remote.
- Git 2.31 requirement not enforced (check if this is necessary first)
git 2.31 or newer is required, but nothing checks for it. Older git ignores
GIT_CONFIG_COUNT. On those versions the Authorization header is never sent, andcredential.helperis never reset, so git falls back to whatever credential helpers the host has configured. Debian 11 ships git 2.30 and Ubuntu 20.04 ships 2.25.Fix: check
git --versionat startup when the cache is enabled and refuse to start if it is too old. (Warning: Claude didn't actually test against an older git version)
- Timeout doesn't stop git immediately
The timeout does not reliably stop git.
runGitsendsSIGTERMto the git parent and rejects immediately, without waiting for close or killing the process group.buildBarethen deletes the staging dir while children such asindex-packmay still be running. After the 600 s timeout fired in the benchmark, the process took another ~110 s to exit, which fits a child still holding thestderrpipe. In a long-running proxy, the repo lock is released while the oldindex-packkeeps using CPU and the next push starts another full clone.Fix: spawn with
detached: true,kill -pid, escalate toSIGKILLafter a grace period, and settle the promise on close.
… clones at startup
…default to an hour
jescalada
left a comment
There was a problem hiding this comment.
Just one more thing left to fix the CI 🙂
| // Dot-prefixed so getCacheStats() and eviction ignore it while it is built | ||
| const staging = path.join( | ||
| path.dirname(bareRepo), | ||
| `.staging-${path.basename(bareRepo)}-${crypto.randomUUID()}`, |
There was a problem hiding this comment.
I think this is making Windows tests fail due to very long filenames
| `.staging-${path.basename(bareRepo)}-${crypto.randomUUID()}`, | |
| `.staging-${crypto.randomUUID()}`, |
There was a problem hiding this comment.
(May still need npm run format after applying this)
|
|
||
| Git commands on the mirror use `--git-dir`, so a broken mirror inside another repository (such as the GitProxy checkout around `./.repo-cache`) is never mistaken for that repository. | ||
|
|
||
| Every clone into the cache is done in a dot-prefixed staging folder (`.staging-<key>-<uuid>`) and only moved into place once it succeeded, so a failed or interrupted clone never leaves a half-written mirror behind. Staging folders are ignored by the cache statistics and by eviction, and leftovers from a crash are removed at startup. |
There was a problem hiding this comment.
For accuracy if the previous change is applied
| Every clone into the cache is done in a dot-prefixed staging folder (`.staging-<key>-<uuid>`) and only moved into place once it succeeded, so a failed or interrupted clone never leaves a half-written mirror behind. Staging folders are ignored by the cache statistics and by eviction, and leftovers from a crash are removed at startup. | |
| Every clone into the cache is done in a dot-prefixed staging folder (`.staging-<uuid>`) and only moved into place once it succeeded, so a failed or interrupted clone never leaves a half-written mirror behind. Staging folders are ignored by the cache statistics and by eviction, and leftovers from a crash are removed at startup. |
Issue: #1207
Summary
An opt-in repository cache for the
pullRemotestep. GitProxy keeps one bare mirror per upstream repository and only fetches the new commits on each push, then clones the per-push working copy locally from the mirror. With the cache disabled (the default)pullRemoteis unchanged.On backstage/backstage (8.7 GB pack), the
pullRemotestep of a one-commit push goes from 145 s to 6.4 s on a cache hit, with peak memory from 3.3 GB to about 100 MB and no event-loop stalls (benchmark). The first push of a repository is slower (full history instead of a shallow clone).How it works
createPullRemote()returnsPullRemoteHTTPSCached/PullRemoteSSHCachedwhencache.enabledis true. Both hand a protocol-specificRemoteAccesstoperformCachedClone().<cacheDir>/<host>__<project>__<repo>-<sha256>:git clone --bareon a miss,git fetch --pruneon a hit. A fetch failure on a readable mirror fails the push and keeps the mirror; only a corrupted mirror is rebuilt. Clones go through a.staging-*folder and are moved into place only on success.git clonefrom the mirror into the usual.remote/<hash>/folder, removed byclearBareCloneas before.maxSizeGBormaxRepositories; the mirror just used is always kept.spawnwith a configurable timeout that kills the whole process tree.Security
GIT_CONFIG_*environment variables as anhttp.<origin>.extraHeader, never on the command line, in a URL or in the mirror's config.credential.helperis reset andGIT_TERMINAL_PROMPT=0.GIT_CONFIG_NOSYSTEM=1, emptyHOME), so/etc/gitconfig,~/.gitconfigand~/.netrcnever apply to a client's clone.Configuration
{ "cache": { "enabled": true, "maxSizeGB": 10, "maxRepositories": 50, "cacheDir": "./.repo-cache", "gitTimeoutSeconds": 3600 } }All settings are optional; only
enabledis needed. The cache lives outside./.remote, which is emptied on startup. Requires git 2.31 or later, checked at startup when the cache is enabled. Several GitProxy instances must not share acacheDir.Docs:
website/docs/deployment.mdx(Repository Cache) andsrc/proxy/processors/push-action/README.md.Tests
test/processors/cachedClone.test.ts,cacheManager.test.ts,gitOperations.test.ts: offline, against a local bare remote and local HTTP server (keys, miss/hit/rebuild, concurrency, eviction, credentials, host-config isolation, timeouts).test/processors/hybridCache.integration.test.ts: end-to-end against github.com, only withRUN_NETWORK_TESTS=true.