Skip to content

fix(xds): deterministic resource emission — sort 7 map-order sites and marshal Any payloads deterministically (#772) - #780

Merged
bpalermo merged 2 commits into
mainfrom
fix/772-a4-xds-determinism
Sep 16, 2026
Merged

bpalermo merged 2 commits into
mainfrom
fix/772-a4-xds-determinism

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Phase A4 of #772 (#135's mechanism on RDS/HCM filters/UDP + non-deterministic Any marshal). Data path: validated on talos-main with the Phase A batch.

fix(xds): make every map-derived xDS resource byte-stable (#772, A4)

Incident #135 (map-iteration order -> every push re-hashed -> full CDS
replace -> RSS cascade) was fixed for clusters and transport-socket
matches. The 2026-09-15 review found the same mechanism alive on RDS and
three other resource shapes (races report S6/S7).

Determinism here is protocol-visible. go-control-plane versions each
resource for delta-xDS by hashing its marshalled bytes, and Envoy skips
an update only on an unchanged MessageUtil::hash.
proto.MarshalOptions{Deterministic:true} canonicalises map fields but
preserves repeated-field order exactly as built, so a repeated field
assembled by ranging a Go map is a different resource on every push.

Audited every for ... := range <map> in agent/internal/xds/cache and
agent/internal/xds/proxy (86 sites, enumerated with a go/types pass, not
grep) for one whose iteration order reaches an emitted resource. Seven
needed fixing; the rest either write into another map (set membership,
order-free) or feed a helper that already sorts.

Fixed, worst first:

  • cache/capture.go extensionHTTPFilters: the HCM's http_filters union was
    built by ranging serviceRoutes, serviceChainFilters and
    serviceInboundFilters. HTTP filter order is the chain's EXECUTION
    order, so two escape-hatch filters ran in a random order relative to
    each other, and every per-pod listener re-hashed on each rebuild.
  • cache/capture.go captureKnownTargets: the redirect-all catch-all's
    known-target routes came out in map order. Envoy evaluates a virtual
    host's routes first-match-wins, so map order decided which authority
    regex won when two services' short-name spellings overlap.
  • proxy/l4route.go GenerateUDPCaptureListener (S7): the udp_proxy primary
    cluster was picked by for _, backends := range udpRoutes { break }.
    With two UDPRoute-backed services the pod's UDP listener forwarded to a
    different backend on each rebuild of identical input.
  • cache/capture.go captureVhosts and cache/cluster.go
    clustersEndpointsAndVhosts (S6): cap_http and aether_outbound
    RouteConfiguration.virtual_hosts, built by ranging c.captureAuthorities
    / gammaRoutes / c.clusters. cap_http is the primary client route table
    on a capture-enabled node.
  • config/common.go TypedConfig: an Any freezes its payload bytes at
    construction, so the enclosing resource's deterministic marshal cannot
    canonicalise what is inside it. The aether_stats TypedStruct carries a
    structpb.Struct (a proto map) on EVERY per-pod HCM, so every inbound,
    outbound and capture listener hashed differently on each regeneration.
    Marshal the Any deterministically. Found by the new test, not by the
    review.
  • cache/cluster.go VirtualHosts, cache/listener.go meshListeners and
    appClusters, cache/snapshot.go secrets: the per-type resource slices.
    These are name-indexed by go-control-plane so their order is not
    protocol-visible, but sorting them makes the emitted resource set
    canonical and is what lets the regression test compare bytes at all.

Left unsorted on purpose: every dependency-set/route-domain/effective-
route builder (map -> map, so order cannot escape), the identity-binding
and inbound-identity-binding collectors (log-only, and they already sort
their own output), observedUpstreamsLocked (already sorts), the
localWorkloads SPIFFE-ID lists (UpstreamTransportSocketMatches sorts
them), AppPortsFromPod and SortSubsetKeys (already sort), the waypoint
builders (already sort), the endpoint subset metadata and the cluster's
transport_socket_matcher exact_match_map (proto maps NOT inside an Any,
so MarshalResource canonicalises them).

New: cache/ordering.go with the two sort helpers and the rationale, and
TestSnapshotDeterministic_ShuffledInputOrder, which builds a fully
populated mesh-mode cache 12 times from shuffled inputs and requires
byte-identical proto output for every resource type plus every builder
slice. Verified it FAILS on the pre-fix tree: reverting the production
changes fails 5/5 runs (on cap_http/aether_outbound RouteConfiguration,
appClusters, endpoints and captureVhosts), and reverting only the UDP
primary-cluster pick fails on the Listener type. With the fixes it passes
20/20.

No resource contents change, only ordering.

Verification: bazel build //... //test/e2e:e2e_test; bazel test //...
(98/98 pass, including //test/envoy_validate:envoy_validate_test, which
runs the pinned proxy Envoy over the generated bootstraps);
bazel run //:format.check; bazel build --config=lint
--@aspect_rules_lint//lint:fail_on_violation //...; bazel run //:gazelle
(no churn beyond the two new files); bazel mod tidy idempotent;
scripts/go-deps-audit.sh exit 0.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 2 commits September 15, 2026 19:46
Incident #135 (map-iteration order -> every push re-hashed -> full CDS
replace -> RSS cascade) was fixed for clusters and transport-socket
matches. The 2026-09-15 review found the same mechanism alive on RDS and
three other resource shapes (races report S6/S7).

Determinism here is protocol-visible. go-control-plane versions each
resource for delta-xDS by hashing its marshalled bytes, and Envoy skips
an update only on an unchanged MessageUtil::hash.
proto.MarshalOptions{Deterministic:true} canonicalises map fields but
preserves repeated-field order exactly as built, so a repeated field
assembled by ranging a Go map is a different resource on every push.

Audited every `for ... := range <map>` in agent/internal/xds/cache and
agent/internal/xds/proxy (86 sites, enumerated with a go/types pass, not
grep) for one whose iteration order reaches an emitted resource. Seven
needed fixing; the rest either write into another map (set membership,
order-free) or feed a helper that already sorts.

Fixed, worst first:

- cache/capture.go extensionHTTPFilters: the HCM's http_filters union was
  built by ranging serviceRoutes, serviceChainFilters and
  serviceInboundFilters. HTTP filter order is the chain's EXECUTION
  order, so two escape-hatch filters ran in a random order relative to
  each other, and every per-pod listener re-hashed on each rebuild.
- cache/capture.go captureKnownTargets: the redirect-all catch-all's
  known-target routes came out in map order. Envoy evaluates a virtual
  host's routes first-match-wins, so map order decided which authority
  regex won when two services' short-name spellings overlap.
- proxy/l4route.go GenerateUDPCaptureListener (S7): the udp_proxy primary
  cluster was picked by `for _, backends := range udpRoutes { break }`.
  With two UDPRoute-backed services the pod's UDP listener forwarded to a
  different backend on each rebuild of identical input.
- cache/capture.go captureVhosts and cache/cluster.go
  clustersEndpointsAndVhosts (S6): cap_http and aether_outbound
  RouteConfiguration.virtual_hosts, built by ranging c.captureAuthorities
  / gammaRoutes / c.clusters. cap_http is the primary client route table
  on a capture-enabled node.
- config/common.go TypedConfig: an Any freezes its payload bytes at
  construction, so the enclosing resource's deterministic marshal cannot
  canonicalise what is inside it. The aether_stats TypedStruct carries a
  structpb.Struct (a proto map) on EVERY per-pod HCM, so every inbound,
  outbound and capture listener hashed differently on each regeneration.
  Marshal the Any deterministically. Found by the new test, not by the
  review.
- cache/cluster.go VirtualHosts, cache/listener.go meshListeners and
  appClusters, cache/snapshot.go secrets: the per-type resource slices.
  These are name-indexed by go-control-plane so their order is not
  protocol-visible, but sorting them makes the emitted resource set
  canonical and is what lets the regression test compare bytes at all.

Left unsorted on purpose: every dependency-set/route-domain/effective-
route builder (map -> map, so order cannot escape), the identity-binding
and inbound-identity-binding collectors (log-only, and they already sort
their own output), observedUpstreamsLocked (already sorts), the
localWorkloads SPIFFE-ID lists (UpstreamTransportSocketMatches sorts
them), AppPortsFromPod and SortSubsetKeys (already sort), the waypoint
builders (already sort), the endpoint subset metadata and the cluster's
transport_socket_matcher exact_match_map (proto maps NOT inside an Any,
so MarshalResource canonicalises them).

New: cache/ordering.go with the two sort helpers and the rationale, and
TestSnapshotDeterministic_ShuffledInputOrder, which builds a fully
populated mesh-mode cache 12 times from shuffled inputs and requires
byte-identical proto output for every resource type plus every builder
slice. Verified it FAILS on the pre-fix tree: reverting the production
changes fails 5/5 runs (on cap_http/aether_outbound RouteConfiguration,
appClusters, endpoints and captureVhosts), and reverting only the UDP
primary-cluster pick fails on the Listener type. With the fixes it passes
20/20.

No resource contents change, only ordering.

Verification: bazel build //... //test/e2e:e2e_test; bazel test //...
(98/98 pass, including //test/envoy_validate:envoy_validate_test, which
runs the pinned proxy Envoy over the generated bootstraps);
bazel run //:format.check; bazel build --config=lint
--@aspect_rules_lint//lint:fail_on_violation //...; bazel run //:gazelle
(no churn beyond the two new files); bazel mod tidy idempotent;
scripts/go-deps-audit.sh exit 0.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit b5290de into main Sep 16, 2026
20 checks passed
@bpalermo
bpalermo deleted the fix/772-a4-xds-determinism branch September 16, 2026 00:08
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