Skip to content

feat(compose): host-wide Maven proxy, so each host downloads an artifact once - #46

Closed
azlekov wants to merge 1 commit into
mainfrom
feat/maven-proxy
Closed

azlekov wants to merge 1 commit into
mainfrom
feat/maven-proxy

Conversation

@azlekov

@azlekov azlekov commented Nov 25, 2026

Copy link
Copy Markdown

Refs #45

Why

Each runner slot keeps its own Gradle cache under slots/<id>/cache/gradle, and the pruner evicts from those caches. A host therefore downloaded the same Robolectric jars (nativeruntime-dist-compat at ~150 MB, android-all-instrumented at ~190 MB) once per slot and again after each eviction. Maven Central's per-IP, per-file limit then blocked the beelink host (5 slots behind one IP) for ~23 h. This is the structural fix: one upstream download per host. The pruner fix for Gradle 9's cache dirs is in a separate branch.

What

  • maven-proxy service (maven-proxy/default.conf.template, nginx 1.31.9-alpine): a caching proxy at /maven2/, /google/ and /plugins/.
    • proxy_cache_lock makes concurrent misses for one file wait for a single upstream download.
    • Plugin Portal redirects are followed inside the proxy and cached under the requested path.
    • 200 is cached until LRU eviction or 90 days unused; 404 and maven-metadata.xml for 10 min. 429 and 5xx are never cached.
    • The cache lives in RUNNER_DATA_DIR/maven-proxy, bounded by MAVEN_PROXY_MAX_SIZE (default 20g).
    • Upstreams are resolved at request time through Docker DNS, so the proxy starts without upstream DNS. No port is published.
  • maven-proxy/maven-proxy.gradle: a Groovy init script that ships in the image. The preflight copies it into each slot's ~/.gradle/init.d, and only when it has changed.
    • It probes /healthz once per build. If the proxy is down, the build keeps its declared repositories and Gradle logs a warning.
    • It redirects only repositories whose URL is Maven Central, Google Maven or the Plugin Portal. When no plugin repositories are declared, it also makes the implicit portal explicit so project plugins go through the proxy. Other repositories are untouched.
    • It points Test tasks' robolectric.dependency.repo.url at the proxy, because Robolectric downloads android-all jars itself at test time.
    • It raises Gradle's 30 s read timeout (unless the build set one), since a request waiting on the cache lock gets no bytes until the download finishes.
  • Turning it off: set MAVEN_PROXY=off in .env to disable it on a host, or in a job's env to disable it for that job. --scale maven-proxy=0 also works, because the runners fall back to upstream.

Why nginx, not GRADLE_RO_DEP_CACHE or Reposilite

A read-only dependency cache seeded by the pruner:

  • helps only after a seed pass, so slots still download new artifacts themselves and concurrent first fetches are not coalesced;
  • must match the cache layout of each Gradle version in use;
  • needs a copy-and-swap of a multi-GB tree to stay unchanged while builds read it;
  • never sees Robolectric's own downloads, and android-all-instrumented is one of the two jars that triggered the block.

The proxy is independent of the Gradle version, serves any Maven client, and fails open. Reposilite would mean a JVM (heavy on the Pi), stored state and an admin token, with no clear coalescing of concurrent misses.

Tests

tests/maven-proxy.sh:

  • Preflight: installs the script, does not rewrite an unchanged one, replaces a stale one, and removes it with MAVEN_PROXY=off or without a URL.
  • Proxy: starts the real compose maven-proxy service against a fake upstream nginx, and checks:
    • each prefix routes to its upstream, and a second request is a HIT with one upstream fetch;
    • 5 concurrent misses on a slow 8 MB file cause 1 upstream fetch;
    • a redirect is followed and then cached;
    • 429 is passed through and not cached, metadata is cached, and unknown paths return 404 without reaching upstream.
  • Gradle (when gradle is on PATH): repositories are redirected and Robolectric is pointed at the proxy; a dependency resolves through the proxy; the declared repositories are kept, with a warning, when the proxy is down; MAVEN_PROXY=off opts a job out.

Local results:

  • Proxy and Gradle parts pass against Gradle 9.9.2, run from the official gradle image. With --warning-mode=all there are no deprecations, and the configuration cache works.
  • A mutation check (lock off, 429 cached, no redirect follow) fails the test as expected.
  • All tests/*.sh pass in ubuntu:24.04; shellcheck and docker compose config are clean.

image-smoke.sh now checks that the init script is in the image.

Follow-up (not in this PR)

Robolectric keeps its downloaded jars in ~/.m2/repository. That path is not linked into the slot, so it lives in the container's writable layer, outside the pruner's budget.

🤖 Generated with Claude Code

…artifact once

Every runner slot keeps its own Gradle cache, so a host fetched the same jars once per slot and again after
each pruner eviction, until Maven Central's per-IP, per-file limit blocked it for ~23 h (#45).

- maven-proxy service: nginx proxy_cache for Maven Central, Google Maven and the Gradle Plugin Portal.
  proxy_cache_lock coalesces concurrent misses into one upstream download, Plugin Portal redirects are
  followed and cached, 429/5xx are never cached, and the cache is LRU-bounded by MAVEN_PROXY_MAX_SIZE.
- maven-proxy.gradle: init script that the preflight installs into ~/.gradle/init.d. It redirects only the
  three upstream repositories and Robolectric's runtime downloads, and leaves the build as declared when
  the proxy does not answer.
- MAVEN_PROXY=off disables it per host (in .env) or per job (in env).
- tests/maven-proxy.sh covers the preflight install, the real compose service against a fake upstream,
  and, when gradle is on PATH, the init script.

Refs #45

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@azlekov

azlekov commented Nov 25, 2026

Copy link
Copy Markdown
Author

Closing: the issue #45 work lands in one consolidated PR together with the pruner fix. The branch feat/maven-proxy stays for that PR.

@azlekov azlekov closed this Nov 25, 2026
@azlekov
azlekov deleted the feat/maven-proxy branch November 26, 2026 08:20
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