Skip to content

fix(registrar): resume the agent watch across dependency-set changes (#1239) - #1266

Merged
bpalermo merged 3 commits into
mainfrom
fix/1239-watch-filter-resume
Oct 5, 2026
Merged

bpalermo merged 3 commits into
mainfrom
fix/1239-watch-filter-resume

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Root cause

registry/internal/registrar/registrar.go watchLoop (main at 65e473a, lines 590-594) cleared the resume token whenever the filter generation moved:

services, filterGen := r.assertFilter(streamCancel)
if filterGen != lastFilterGen {
	lastVersion = ""
	lastFilterGen = filterGen
}

SetServiceFilter bumps filterGen and cancels the live stream on every dependency-set change (a server-streaming RPC cannot change its filter in place). So every change re-opened WatchEndpoints with last_version = "", which Snapshot.resumeLocked can only classify resent, shrink or growth alike. The registrar was not rejecting a good token: it never got one. Every TCP mesh service is always in every node's dependency set (agent/internal/xds/cache/cache.go), so a TCP service appearing or disappearing changes every node's set. That matches the soak: resent steps lined up with fleet-wide service left dependency set, and current never fired.

Clearing the token on a growth is necessary, because a growth needs content the agent lacks. On a shrink it was not.

Fix

The agent now tracks held: the services whose cached endpoints are exactly what its resume token names. It is narrowed by every filter change (their entries are purged), set to the stream's filter at SNAPSHOT_COMPLETE, and emptied when a resend clears the cache. At each (re)open, resumeFor decides:

  • filter ⊆ held (a shrink, or no change): send last_version as on any reconnect. The version names the registrar's whole snapshot, so a token that is current is current for any subset of the services it was earned on. The response is current or renamed.
  • filter ⊄ held (a growth): last_version would be wrongly answered current while the added services are missing. It stays empty, and the token goes in the new partial_resume {version, services: held ∩ filter}. A registrar that knows the field sends only the missing services' endpoints, as unversioned ENDPOINT_ADDED events (not FULL_SNAPSHOT, which clears the agent cache). It does this only when the token names its current contents, and sets extended: true on the marker. A stale token is resent in full.

Hardening in the same code:

  • The cache now admits endpoint events only for the current filter (scope, updated in the same critical section as the purge). Before this, a stream still carrying the old filter could re-insert a service the purge had just dropped.
  • A switch from the full watch to a filter now purges everything outside the new filter. Before, it purged nothing.
  • A stream opened without a token always gets a full resend unless the marker says extended. If such a resend has nothing in the filter it sends no FULL_SNAPSHOT, so the cache is now cleared at the marker.
  • New label aether_registrar_watch_starts_total{resume="extended"}, plus a debug log WatchEndpoints resolved with the outcome.

Wire compatibility (one-release skew)

agent registrar shrink growth
new new current/renamed extended (or resent if stale)
new old current/renamed (same claim a token always made) old registrar ignores partial_resume, sees no token, resent; agent sees no extended and treats it as a resend
old new resent (unchanged) resent (unchanged)

The proto adds fields only (WatchEndpointsRequest.partial_resume = 6, PartialResume, WatchEndpointsResponse.extended = 6), on edition 2023.

Tests

  • registry/internal/registrar/watch_resume_test.go: TestWatchLoop_DependencySetChangeKeepsTheToken drives the real watch loop through narrow, shrink and grow. It fails on main's logic (the shrink request carries last_version "") and passes with the fix. There are unit tests for resumeFor (including the negative: a service that left and re-entered within one stream is not held), the scope guard, extended, cut-extended and resend starts, and the empty-resend clear.
  • registrar/internal/server/partial_resume_test.go: shrunk filter resumes; partial resume current and renamed; stale token is resent, never extended; empty partial version; last_version beats partial_resume; full-watch partial; metric labels.
  • bazel test and bazel test --config=race on //registrar/internal/server:server_test and //registry/internal/registrar:registrar_test, plus the dependent cmd/services tests. Lint with fail_on_violation and make format-check are clean.
  • kind (e2e/eastwest-quic.sh up, kubernetes backend, this stack's images): scaling quic-b 1→0 (service left dependency set) logged resume=current events=0. Scaling 0→1 logged resume=extended events=1, and client-a → quic-b answered 200 ×5 afterwards.

Runbook: the "Reconnect outcomes" list in docs/runbook.md now covers extended and dependency-set changes.

Review findings fixed

An independent adversarial review found three lost updates, one bug of #1266 and two older holes it widened. All three are fixed in this PR. Each fix is proven by a regression test that fails on the pre-fix code; I reverted each fix temporarily to confirm. The tests are adapted from the reviewer's.

  • F1 (introduced by this PR): flap during the initial exchange. completeStart set held = filter ∩ scope. That undid the narrowing SetServiceFilter applied while the exchange was still arriving: a purged service that came back before the marker was held partial or empty, and the next reconnect was answered current. The fix tracks streamComplete: the stream's filter at open, narrowed by every later filter change. completeStart holds only streamComplete ∩ scope. Tests in watch_flap_test.go, one per way a stream can start (resend, plain current resume, extension):
    • TestConsumeStream_FlapDuringInitialResendIsNotHeld
    • TestConsumeStream_FlapDuringCurrentResumeIsNotHeld
    • TestConsumeStream_FlapDuringExtensionIsNotHeld
  • F2 (older bug, made permanent by fix(registrar): hand watchers the version on no-op revisions (#1241) #1267): publications interleaved. Broadcaster.Publish held publishMu only for reading, so two publications could interleave their mutate and broadcast steps. On one endpoint a watcher received REMOVED x (v2) then ADDED x (v1) and kept x. One batch's unversioned events could also land after another's versioned last event, breaking the registrar/agent: the watch version is stamped on every event, so a stream truncated mid-snapshot or mid-batch leaves the agent with last_version = V and partial state — a reconnect that matches V skips the resend #1203 rule. Publications are now serialized: publishMu is a sync.Mutex. Lock order is publishMu, then Snapshot.mu or the broadcaster's mu, never both at once, so SubscribeWith and MarkVersion cannot deadlock. Tests in publish_order_test.go:
    • TestPublish_SameKeyPublicationsAreOrdered
    • TestPublish_ConcurrentPublicationsKeepEveryVersionHonest: 8 goroutines × 200 registers and multi-IP unregisters on shared keys. At every versioned event the watcher's cache must hash to that version. Without the fix it fails 3/3 runs with hundreds of violations.
    • TestWatchEndpoints_PreSnapshotBatchCannotRegressTheToken relied on the old concurrency: batch C published while batch B was open. C now goes through a goroutine because it waits for B. Its assertions are unchanged.
  • P2 (older hole, wider exposure from this PR): empty resend after a presented token. A stale token resent for a filter with no endpoints carries no FULL_SNAPSHOT, so the cache was never cleared. Now the marker decides. If its content hash differs from the presented token's, it was a resend, and the cache is cleared at the marker; a renamed answer always has an equal hash. Tests:
  • Review gap: the original watch-loop test never checked the cache. The existing test checked only request fields, against a fake that always answers "1". The new TestWatchLoop_DependencySetChangeCacheContents runs the real watch loop against a fake that answers like the registrar (current, extended or resent). Through narrow, shrink, grow and grow it asserts both the outcomes (resent once, then current, current, extended, extended) and the exact cache contents after each step.

Closes #1239

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 3 commits October 5, 2026 13:42
…1239)

Every dependency-set change re-opened the agent's WatchEndpoints stream with
an empty resume token: watchLoop cleared lastVersion whenever the filter
generation moved, shrink or growth alike, so the registrar could only answer
with a full (filtered) resend. A TCP service appearing or disappearing changes
every node's set, so each such change resent every agent's snapshot.

The agent now tracks which services its cache holds at the resume token
(held) and presents the token accordingly:

- filter shrank or is unchanged: last_version as on any reconnect. The version
  names the registrar's whole snapshot, so a current token is current for any
  subset of the services it was earned on.
- filter grew: the token goes in the new partial_resume {version, services}
  with last_version empty. A registrar that knows the field sends only the
  added services' endpoints (ENDPOINT_ADDED) when the version names its current
  contents and marks the SNAPSHOT_COMPLETE extended; a stale token is resent in
  full. An older registrar sees no token and resends: skew-safe both ways.

The cache only admits endpoint events for the current filter (a stream still
carrying the old filter can no longer refill a purged service), the purge on a
switch from the full watch now drops everything outside the new filter, and a
tokenless resend with nothing in the filter now clears the cache at the marker.

New resume label value: aether_registrar_watch_starts_total{resume="extended"}.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
F1. completeStart held the stream's whole filter. A service the filter
dropped while the initial exchange was still arriving was purged, and the
stream kept delivering after the cancellation; if the service came back
before the marker it was held partial (or empty) under a token the
registrar answers "current". The agent now tracks streamComplete (the
stream's filter at the open, narrowed by every later filter change) and
holds only that.

F2. Broadcaster.Publish held publishMu only for reading, so two
publications interleaved mutate and broadcast: on one endpoint a watcher
could receive REMOVED x (v2) then ADDED x (v1) and keep x, or get one
batch's unversioned events after another's versioned last event (#1203).
Publications are now serialized; lock order is publishMu, then
Snapshot.mu or the broadcaster's mu, never both.

P2. A stream that presented last_version could not tell an empty resend
from a renamed resume, so a stale token resent for a filter with no
endpoints left stale entries held under the new token. The marker now
decides: a content hash different from the presented token's is a
resend, and the cache is cleared (renamed always has an equal hash).

Regression tests are adapted from the reviewer's failing ones.
TestWatchEndpoints_PreSnapshotBatchCannotRegressTheToken now publishes its
batch C from a goroutine, because C waits for B. Its assertions are
unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
#1239)

TestPublish_ConcurrentPublicationsKeepEveryVersionHonest failed on a slow CI
runner: the watcher's 256-event buffer overflowed (the designed force-resync)
before the recorder drained it, and the "kept up" precondition failed.

Each publisher now waits for headroom of publishers x 3 events (3 is the
most one publication emits) before it publishes. All eight at once still
cannot fill the buffer, whatever the scheduler does, and the publications
still run concurrently. The test also asserts that it verified at least
400 versioned events, so it cannot pass vacuously (it verifies about
1200). With Publish back on a read lock it still fails 20/20 runs, and
10/10 under race.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo force-pushed the fix/1239-watch-filter-resume branch from 2ecfd24 to 3162a2e Compare October 5, 2026 16:42
@bpalermo
bpalermo merged commit f2fc59a into main Oct 5, 2026
24 checks passed
@bpalermo
bpalermo deleted the fix/1239-watch-filter-resume branch October 5, 2026 16:57
bpalermo added a commit that referenced this pull request Oct 5, 2026
…ht-publication watch test teeth (#1274)

* refactor(registrar): one snapshot-version parser for agent and server (#1272)

The agent's versionContentHash (registry/internal/registrar) duplicated the
registrar server's parser (registrar/internal/server/snapshot.go), and
#1266's emptyResend cache-clear depends on the two agreeing on "renamed".

Move the version format -- its documentation, Format, ContentHash and a
Compare (Same / Renamed / Different) -- into common/snapshotversion, a
stdlib-only leaf package, and use it on both sides: the server's
resumeLocked and the agent's emptyResend now classify with the same
function. Table tests cover every form (<rev>.<hash>, <rev>+<hash>,
hash:<hash>, legacy counter, empty, garbage) and the mixed-form
comparisons both sides make.

Pure refactor: s.version is always rendered from s.contentHash, so
comparing a token to the version's hash is comparing it to the content
hash. Existing tests pass unchanged (publish_order_test only follows the
helper's new name).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

* test(registrar): give the in-flight-publication watch test teeth again (#1271)

Since publications are serialized (#1266), batch C can no longer overtake
an in-flight batch B, so TestWatchEndpoints_PreSnapshotBatchCannotRegressTheToken
passed even with SubscribeWith no longer excluding publications: B's
duplicate broadcast now carries the snapshot's own version, so the token
and cache assertions cannot see it.

Assert the rule SubscribeWith actually enforces: each batch is in the
watch's snapshot or on its channel, never both -- B's and C's endpoints
reach the stream exactly once. With publishMu dropped from SubscribeWith
(local mutation, not committed) the test fails 10/10 on B delivered
twice; the original assertions pass under the same mutation.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
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.

registrar: every dependency-set change restarts the agent watch with a full resend (resume=current never fires)

1 participant