Repository navigation
fix(xds): deterministic resource emission — sort 7 map-order sites and marshal Any payloads deterministically (#772) - #780
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 andagent/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:
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.
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.
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.
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.
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.
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