Skip to content

fix(registrar): subscribe before the snapshot read; resolve RPC removals per stored protocol - #1208

Merged
bpalermo merged 1 commit into
mainfrom
fix/1205-1206-watch-subscribe-and-remove
Oct 4, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/1205-1206-watch-subscribe-and-remove

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Fixes #1205
Fixes #1206

#1205: a broadcast during the initial exchange was lost on that stream

Mechanism. WatchEndpoints ran WatchStart, sent the snapshot, the catalog and SNAPSHOT_COMPLETE, and only then called Subscribe. A batch applied and broadcast in that window found no subscription for the stream. The agent's live cache missed the change until an unrelated event touched the service, and with #1193's stable versions that can take a long time.

Fix. Subscribe first, then read the snapshot, then send it and the marker. Batches published during the send buffer on the watcher's channel and follow the marker.

Ordering/token rule. Subscribing first alone is not enough. Snapshot.Apply / DiffAndReplaceAt and Broadcast were separate steps. A batch B could be applied before the snapshot read but broadcast after the subscribe. B would then reach the stream after the marker, carrying B's version. If another batch C was applied in between, that version names contents without C, while the agent's cache holds C. The agent's token would regress to a name its cache does not match.

The rule implemented is mutual exclusion between publications and watch starts, so no pre-snapshot batch can ever be buffered:

  • Broadcaster.Publish(mutate) holds publishMu for reading around the snapshot mutation and its broadcast. Publications stay concurrent with each other, as before. RegisterEndpoint, UnregisterEndpoint and Syncer.sync all mutate through it.
  • Broadcaster.SubscribeWith(id, services, start) holds publishMu for writing around Subscribe plus Snapshot.WatchStart. Both are in-memory and short. The sends happen after the lock is released.

So every batch is either:

It is never both. Every version the agent adopts after the marker therefore names the snapshot plus exactly the batches it has received, so the token cannot move backwards or onto contents the cache doesn't hold. Lock order is publishMu, then snapshot.mu / broadcaster.mu, everywhere.

Overflow. If the buffer (256) overflows while a large snapshot is still being sent, the broadcaster closes the channel as before. streamEvents returns DataLoss and the agent clears its token.

#1206: the snapshot-first unregister removed nothing until the next sync (confirmed real)

Mechanism. UnregisterEndpointRequest carries no protocol, and the RPC built ENDPOINT_REMOVED events without one. Snapshot.Apply keys a removal by (service, protocol, ip), so the removal keyed PROTOCOL_UNSPECIFIED and matched nothing. The broadcast event also carried no protocol, so the agent's removeLocked (keyed by protocol too) matched nothing either. The endpoint stayed in the snapshot, the catalog and every agent cache until the next sync's diff.

Fix. A new Snapshot.RemoveIPs(service, ips) resolves the removal against the stored entries under the write lock. It emits one ENDPOINT_REMOVED per protocol each IP is actually held under, carrying the stored endpoint. An IP the snapshot does not hold yields no event, and then nothing is broadcast. It returns the catalog transitions and refreshes the content hash and version: the version is dirty (<rev>+<hash>), as with Apply.

The write-behind side is untouched: per-protocol EnqueueUnregister (#1152), the DerivedEndpoints release, and the etcd "shield until observed" overlay. The legacy write-through path is unchanged too.

Tests (registrar/internal/server/watch_ordering_test.go)

The model of the agent's cache replays the stream the way registry/internal/registrar does: the first FULL_SNAPSHOT clears, entries are keyed by (protocol, service, ip), and the token is the last non-empty version.

test red before green after
TestWatchEndpoints_ChangeDuringInitialExchangeIsDelivered: a RegisterEndpoint fired from inside the stream's Send of the first FULL_SNAPSHOT, and of SNAPSHOT_COMPLETE red on main: both subtests, the change never reached the stream delivered; the token ends on snapshot.Version()
TestWatchEndpoints_PreSnapshotBatchCannotRegressTheToken: B is applied but its broadcast is held, C is published, then a watch starts, then B is released red with a naive subscribe-first without the publish lock (verified by removing the lock): the token ended on B's 40+10a3… instead of the snapshot's 40+ec94… the token equals the snapshot's version; B and C are both in the cache
TestUnregisterEndpoint_RemovesUnderTheRegisteredProtocol: HTTP, TCP and UDP; register two IPs through the RPC, unregister both through the RPC, no sync red on main: all 3 protocols; still in GetAll, still in the catalog, content hash not restored, still in the agent cache gone from the snapshot, the catalog and the agent cache at once; the content hash returns to its pre-register value; the token is the snapshot's version

Gates

  • bazel test --config=race --runs_per_test=10 on all 11 go_test targets under //registrar/... and //registry/... (from bazel query 'kind(go_test, …)'): 11/11 PASSED, 10 runs each.
  • bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //registrar/... //registry/...: clean.
  • make format-check: clean. make gazelle: run; it only adds the new test file to BUILD.bazel.
  • No proto change, no chart change, no agent change. The agent already removes by the protocol the event carries.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…als per stored protocol

#1205: WatchEndpoints subscribed to the broadcaster only after sending the
snapshot and SNAPSHOT_COMPLETE, so a batch broadcast in between was never
delivered on that stream. It now subscribes first, with publications
excluded: Broadcaster.Publish makes a snapshot mutation and its broadcast one
publication, and Broadcaster.SubscribeWith runs Subscribe plus the snapshot
read with no publication in flight. Every batch is therefore either in the
snapshot or on the watcher's channel, never both, so no batch older than the
snapshot can follow the marker and move the agent's token backwards.

#1206: UnregisterEndpoint built ENDPOINT_REMOVED events without a protocol,
so Snapshot.Apply and the agent's cache (both keyed by service, protocol, ip)
removed nothing until the next sync. Snapshot.RemoveIPs resolves the removal
against the stored entries under the lock: one REMOVED event per protocol an
IP is held under, carrying the stored endpoint; none for an IP not held.

Fixes #1205
Fixes #1206

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit 28c9278 into main Oct 4, 2026
24 checks passed
@bpalermo
bpalermo deleted the fix/1205-1206-watch-subscribe-and-remove branch October 4, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment