Repository navigation
v4: absorb main 3.5.0, part 4 - pub/sub: retire subscriptions the server still holds - #3408
Merged
Merged
Conversation
… restore and the heartbeat (port of main #3368) Unsubscribing a channel's last handler retires its subscription on the client whether or not the server-side unsubscribe succeeds. v4 had main's gap: a declined (BUSY, LOADING, MASTERDOWN) or timed-out UNSUBSCRIBE, or one sent while the SUBSCRIBE was still unacknowledged, left the server subscribed with nothing left to clean it up until the connection was replaced. - Subscription: a retiring state readable without the registration lock, plus a retry-needed flag consumed before re-issuing, so a slow reply is not re-sent on every heartbeat. Restore paths skip retired subscriptions. - The UNSUBSCRIBE is never sent as fire-and-forget, though a caller who asked for that still does not wait, so a declined one is always observed. An incomplete one records the server and is tracked by the multiplexer, which re-issues it from the heartbeat and from EnsureSubscriptions unless a newer subscription owns, or may yet claim, that server. - An unsubscribe while the subscribe is in flight goes out behind it on the same connection, so the caller does not return with the server still subscribed. An ack for a retired subscription, or a stray for a live one, is unsubscribed as a tracked retirement. - Self-correction: an older UNSUBSCRIBE that lands after a newer subscription for the channel was restored onto the same server re-subscribes that one. - The synchronous Unsubscribe surfaces the server's exception rather than an AggregateException. Also from main's SubscriptionRestorationTests, which #3353 created and #3407 missed: - a timed-out subscribe's server is tracked as a possible stray, since its late ack reaches nobody in v4; - an internal restore that deferred to an in-flight attempt is re-driven when that attempt fails; - UnsubscribeAsync's task carries the subscriber's async state (an existing v4 bug). Main's FailedUnsubscribeUnitTests (14 of 18 failed on v4) and SubscriptionRestorationTests come across with old-core sends replaced by v4's; the bridge-identity test keeps its intent (restored on the new connection, and delivers) without the PhysicalBridge assertions.
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.
Fourth slice of absorbing main's 3.5.0 into v4 (after #3403, #3406, #3407): main's #3368, plus the part of #3353's tests that #3407 missed. #3387 follows separately.
The bug (#3367)
Unsubscribing a channel's last handler retires its subscription on the client whether or not the server-side unsubscribe succeeds. v4 had main's gap. Each of the following left the server subscribed, pushing messages nothing receives, with nothing on the client to clean it up until the connection was replaced:
UNSUBSCRIBE(BUSY,LOADING,MASTERDOWN);UNSUBSCRIBEtimed out;UNSUBSCRIBEwas sent while theSUBSCRIBEwas still unacknowledged.How it maps onto v4
Main hooks into
TrackSubscriptionsProcessor. Here the equivalent is the settle path inSubscription.SendAsync.IsRetiredis readable without the registration lock. A retry-needed flag is consumed before re-issuing, so an unsubscribe merely waiting on a slow reply is not re-sent on every heartbeat. Restore paths (EnsureSubscriptions,EnsureSubscribedToServer[Async],ResubscribeToServer) skip retired subscriptions.UNSUBSCRIBEis never sent as fire-and-forget. A caller who asked for that still doesn't wait, but a declined one is always observed. An incomplete one records the server, and the multiplexer tracks the retirement. It is re-issued from the heartbeat and fromEnsureSubscriptionsuntil confirmed or the connection drops, unless a newer subscription for the channel owns that server, or may yet claim it (TryUnsubscribeRetiringServer, underlock (subscriptions)as in main).UNSUBSCRIBEcan land on a server after a newer subscription for the channel was restored there. When that happens, the newer one forgets the server and subscribes again.Unsubscribesurfaces the server'sRedisServerException(Busy), as the async path does, rather than anAggregateExceptionfrom the wait.One difference from main: v4 completes settle continuations on the thread pool, so the reply to an
UNSUBSCRIBEcan be handled after the reply to a laterSUBSCRIBEon the same connection. The self-correction can then re-subscribe a subscription that was fine. That costs one extra, idempotentSUBSCRIBE, and no messages are lost.From main's
SubscriptionRestorationTests(#3353)#3353 created this file, and #3407 ported only
SubscriptionRestoreRaceTests. Bringing it across exposed three more things:LateAcknowledgementCannotReplaceANewerSubscription). A timeout is the one failure that leaves the server's state unknown. That server is now tracked as a possible stray: the record is dropped if the subscription comes back to it, and the server is unsubscribed if the subscription lands elsewhere or retires.DeferredRestoreIsRedrivenWhenTheInFlightAttemptFails). Previously nothing brought it back.UnsubscribeAsync's task carries the subscriber's async state (UnsubscribeTaskPreservesTheCallersAsyncState). This was an existing v4 bug.Tests
FailedUnsubscribeUnitTests(18 cases): 14 failed on v4 before this; all pass now. One send was replaced with v4'sSubscription.SendAsync.SubscriptionRestorationTests(44 cases): the old-core sends are replaced by v4's:PUBLISHpinned viaConnections.ServerContext;Subscription.SendAsyncfor the stray.ConnectionFailedevent instead of comparingPhysicalBridgeinstances, which v4 doesn't have.