Skip to content

refactor(xds): split the snapshot cache into per-concern packages (#544) - #553

Merged
bpalermo merged 1 commit into
mainfrom
refactor/544-cache-package-split
Jul 19, 2026
Merged

bpalermo merged 1 commit into
mainfrom
refactor/544-cache-package-split

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Summary

Fixes #544.

Mechanical, no-behavior-change package split of agent/internal/xds/cache. After mapping the whole package I extracted the two Envoy-proto-free, self-contained infrastructure concerns into dedicated sub-packages and left the stateful SnapshotCache core in the root, because a per-resource split does not compile without a rewrite (see rationale below).

What moved

New sub-package From Dependencies (narrowed)
cache/snapversion version.go (generateSnapshotVersion → snapversion.Generate) only go.uber.org/atomic
cache/cachemetrics metrics.go (cacheMetrics → cachemetrics.Metrics, exported ctor + methods) only OTel metric API

Tests moved with their code (version_test.go → snapversion, metrics_test.go → cachemetrics); coverage is identical — no test case dropped. The root go_library no longer depends on otel/metric, and the root go_test no longer pulls otel/sdk/metric + atomic — those are now isolated in the two sub-packages.

Why not a full per-resource split

The package is one SnapshotCache struct whose methods share cross-cutting mutexes and maps:

  • depMu → 6 files, listenerMu → 6, clusterMu → 5 (incl. mtls.go), localMu → 5, secretMu/subsetMu/edgeMu → 3 each.
  • subsetMu/secretMu are written in one file but read inside the single generateSnapshot orchestrator (snapshot.go), so their state cannot leave the root without cross-package lock coordination in the hottest path.
  • The edge value types (VirtualHost, Route, RouteBackend, EdgeGatewayEntry, EdgeTLSCert) are SnapshotCache fields and the public API consumed by the external agent/internal/edge/gatewayapi reconciler + its tests (the sink interface). Moving them (and their bound equal* comparison helpers) would create a wide external blast radius and risk an import cycle (root imports the type; the helpers can't import root) — exactly the risk refactor(xds): split agent/internal/xds/cache into per-resource sub-packages #544 warns against.
  • The heavy per-resource Envoy builders already live in the sibling agent/internal/xds/proxy package (28 files, each importing only the protos it needs). The cache package is the stateful orchestrator over proxy.

So the largest safe split is the two infra concerns above.

Preserved exactly

snapshotMu serialization semantics, the unified full-snapshot model, the clusterMu(outer)→localMu(inner) lock order (#548), and the dependency/route memo semantics (#549/#551). No lock acquisition was reordered; no builder logic changed.

Testing

  • bazel build //... — pass.
  • bazel test --@rules_go//go/config:race=true --build_tests_only //agent/... //agent/internal/xds/... — all cache targets pass (cache_test, cachemetrics_test, snapversion_test, proxy_test, ...). One unrelated failure, //agent/internal/cni/server:server_test (TestHandlePodTerminatingPoolClosePhase), reproduces identically on the clean base commit under -race and is in a package this PR does not touch.
  • make gazelle + make format clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

Extract the two Envoy-proto-free, self-contained infrastructure concerns
out of the agent/internal/xds/cache package into dedicated sub-packages:

- snapversion: the xDS snapshot version-string generator (was version.go).
  Depends only on go.uber.org/atomic.
- cachemetrics: the snapshot-generation OTel instruments (was metrics.go).
  Depends only on the OTel metric API; isolates that dependency (and the
  sdk/metric test dep) from the root package.

The stateful SnapshotCache core stays in the root package unchanged: a
naive per-resource split does not compile because nearly every builder is a
*SnapshotCache method sharing cross-cutting mutexes (depMu spans 6 files,
listenerMu 6, clusterMu 5, localMu 5) and maps, all read under the single
generateSnapshot orchestrator. The heavy per-resource Envoy builders already
live in the sibling agent/internal/xds/proxy package (28 files). snapshotMu
semantics, the unified full-snapshot model, the clusterMu->localMu lock order
(#548), and the dep/route memo semantics (#549/#551) are untouched.

Tests move with their code (version_test.go -> snapversion, metrics_test.go ->
cachemetrics); coverage is identical. No behavior change.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR
@bpalermo
bpalermo merged commit 8e9eb16 into main Jul 19, 2026
10 checks passed
@bpalermo
bpalermo deleted the refactor/544-cache-package-split branch July 19, 2026 18:21
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.

refactor(xds): split agent/internal/xds/cache into per-resource sub-packages

1 participant