Repository navigation
fix(registrar): subscribe before the snapshot read; resolve RPC removals per stored protocol - #1208
Merged
Merged
Conversation
…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
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.
Fixes #1205
Fixes #1206
#1205: a broadcast during the initial exchange was lost on that stream
Mechanism.
WatchEndpointsranWatchStart, sent the snapshot, the catalog andSNAPSHOT_COMPLETE, and only then calledSubscribe. 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/DiffAndReplaceAtandBroadcastwere 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)holdspublishMufor reading around the snapshot mutation and its broadcast. Publications stay concurrent with each other, as before.RegisterEndpoint,UnregisterEndpointandSyncer.syncall mutate through it.Broadcaster.SubscribeWith(id, services, start)holdspublishMufor writing aroundSubscribeplusSnapshot.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, thensnapshot.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.
streamEventsreturnsDataLossand the agent clears its token.#1206: the snapshot-first unregister removed nothing until the next sync (confirmed real)
Mechanism.
UnregisterEndpointRequestcarries no protocol, and the RPC builtENDPOINT_REMOVEDevents without one.Snapshot.Applykeys a removal by (service, protocol, ip), so the removal keyedPROTOCOL_UNSPECIFIEDand matched nothing. The broadcast event also carried no protocol, so the agent'sremoveLocked(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 oneENDPOINT_REMOVEDper 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 withApply.The write-behind side is untouched: per-protocol
EnqueueUnregister(#1152), theDerivedEndpointsrelease, 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/registrardoes: the first FULL_SNAPSHOT clears, entries are keyed by (protocol, service, ip), and the token is the last non-empty version.TestWatchEndpoints_ChangeDuringInitialExchangeIsDelivered: aRegisterEndpointfired from inside the stream'sSendof the first FULL_SNAPSHOT, and ofSNAPSHOT_COMPLETEsnapshot.Version()TestWatchEndpoints_PreSnapshotBatchCannotRegressTheToken: B is applied but its broadcast is held, C is published, then a watch starts, then B is released40+10a3…instead of the snapshot's40+ec94…TestUnregisterEndpoint_RemovesUnderTheRegisteredProtocol: HTTP, TCP and UDP; register two IPs through the RPC, unregister both through the RPC, no syncGetAll, still in the catalog, content hash not restored, still in the agent cacheGates
bazel test --config=race --runs_per_test=10on all 11go_testtargets under//registrar/...and//registry/...(frombazel 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 toBUILD.bazel.🤖 Generated with Claude Code
https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr