Repository navigation
Pub/Sub: track retiring subscriptions and drive cleanup via ack, restore, and heartbeat - #3368
aradalvand wants to merge 3 commits into
Conversation
903d730 to
ebe2190
Compare
|
Thanks - the diagnosis in #3367 is accepted, and the I don't want to merge the retry loop as it stands: it gives up after 30 attempts, so the leak is bounded but not closed; each failure starts its own background task with no tie to multiplexer shutdown; and there is the timed-out-then-declined hole you already called out. I'd prefer the client to remember "retire pending" as state and let the existing heartbeat and restore path drive it, which also covers a related gap (unsubscribe while a subscribe is unacknowledged). That work is tracked in #3375, and I'd like to use your I'll leave this open until that lands, in case it is useful as a reference. #3363, which this builds on, has a separate review. Unrelated to your change: the earlier Ubuntu failure here was a flake in the in-process test server. |
…ore, and heartbeat Replaces the per-failure background retry loop with an explicit retiring state on Subscription driven by the existing ack, restore, and heartbeat paths (addressing seredis#3367 and items 1-2 of seredis#3375): - Make Subscription.IsRetired safely readable without RegistrationLock (Interlocked.Exchange / Volatile.Read) and skip retired subscriptions in EnsureSubscriptions, EnsureSubscribedToServer[Async], Release, ResubscribeToServer, and ResubscribeStrandedShardedChannels. - Track retiring subscriptions that still have server endpoints in ConnectionMultiplexer._retiringSubscriptions and drive them from OnHeartbeat() and EnsureSubscriptions(), cleaning up disconnected endpoints in UpdateSubscriptions(). - Honour IsRetired in TryAdoptEndpoint and TrackSubscriptionsProcessor.SetResultCore so late SUBSCRIBE acks on retired subscriptions (e.g. unsubscribed while a subscribe was in flight) are cleaned up without disturbing a newer active subscription on the same or another server. - Queue async unsubscribes behind an in-flight SingleNodeSubscription subscribe on the pending server so async callers wait for the unsubscribe. - Keep FailedUnsubscribeUnitTests (plus timed-out-then-declined and restore-skips-retired cases) and add the four in-flight subscribe/unsubscribe acceptance tests from seredis#3337/seredis#3375 in SubscriptionRestorationTests.
ebe2190 to
e22fafa
Compare
|
Thanks for the feedback — I've rebased onto
|
|
Thanks for this rework. It's the design I had in mind in #3375: a retired subscription keeps its server until the server confirms the unsubscribe, and the ack, restore and heartbeat paths drive it. There are no background tasks and no attempt cap. The cases where an ack races the retire hold up: at least one side always sees the other, so the worst case is a duplicate unsubscribe. The restore paths all skip a retired subscription. The fire-and-forget case and the timed-out-then-declined case are both closed now. The tests pass for me locally, repeatedly. A few things need sorting before this merges, in rough order of severity:
Smaller points:
Given how much of this is now in place, I'm treating this PR as the route for items 1 and 2 of #3375, and I'll close my draft #3377 in its favour. You've left maintainer edits enabled. If you'd rather not take on the remaining items, or would like a hand with any of them, just say so, and I'm happy to push them to this branch myself. Your call entirely. |
…e only, sync pending-subscribe cleanup
|
Just addressed all of these in d8526e8 and updated the PR title and description:
|
Unsubscribing a channel's last handler retires the subscription on the client whether or not the server ran the unsubscribe. If the server declined it (BUSY, LOADING, TRYAGAIN, CLUSTERDOWN) it stayed subscribed, with nothing left on the client that would ever unsubscribe it (#3367). The same applied to the removal of a stray registration (#3375). The reply processing now notices the refusal, including for fire-and-forget, and records it; the heartbeat issues the unsubscribe again, once per beat, until the server runs it or the connection it was subscribed on is replaced. The caller still sees the original failure. The retry goes through the same guard as other stray removals, so it never unsubscribes a channel that has been subscribed again. Fixes #3367. Tests from #3368. Co-authored-by: aradalvand <26527405+aradalvand@users.noreply.github.com>
mgravell
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround on d8526e8. As an independent check I ran the tests from my closed draft (#3377) against this branch. They were written against a different implementation, so they needed adjusting for two design differences here, neither of which is a problem:
UnsubscribeAsyncdoes not complete until the in-flight subscribe is acknowledged, because the unsubscribe is queued behind it.- A retired subscription keeps its server until the unsubscribe reply arrives, so state settles on the reply and not on the send.
With those adjustments the outcomes hold: restores skip a retired subscription, an unsubscribe during an in-flight subscribe leaves the server unsubscribed (literal, pattern and multi-node; sync and async), declined unsubscribes are retried, and a replacement subscription is not disturbed. I looked at the same-server replacement case in particular: the old subscription's ack defers, and the next heartbeat drops the server from the retired record without sending anything. One unsubscribe reaches the server, ahead of the replacement's subscribe, and the replacement is never re-subscribed.
Two things came out of it. Neither blocks merging, hence the approval.
-
An unsubscribe during an in-flight subscribe is sent twice. One is queued behind the pending subscribe when the unsubscribe is called, and the ack path (
TryAdoptEndpointreturning false, thenTryUnsubscribeRetiringServer) sends another. I see twoUNSUBSCRIBEs on the wire when there is no replacement, or when the replacement is confirmed on another server. It is harmless, since the second is a no-op on the server. If the ack path can tell that one is already queued, it could skip the send and rely on the retry-needed flag should the queued one fail. -
A declined cleanup of a stray is not retried when the subscription is live. This is the #3336 case: a late ack arrives for a subscription that is already confirmed on another server, and the stray registration is removed with a fire-and-forget unsubscribe. If the server declines that one (
BUSYetc.), nothing issues it again, because the error handling inTrackSubscriptionsProcessor.SetResultis gated onIsRetired. The stray then delivers duplicates until the connection is replaced. This is item 3 of #3375, which this PR does not claim to address. There is a test for it on the #3377 branch (DeclinedStrayCleanupIsRetriedinSubscriptionRestorationTests).
Do you particularly want to take those two yourself in this PR? If not, I'm happy to merge this as it stands and pick them up myself afterwards. Your call; let me know which you prefer.
|
Thanks for the approval and the verification. Both of those turned out to fit very cleanly on top of the existing retiring machinery, so I went ahead and added them in 97cf584 (and updated the PR description to include item 3 of #3375):
|
Fixes #3367. Addresses items 1, 2, and 3 of #3375.
Unsubscribing a channel's last handler retires its subscription on the client whether or not the server-side unsubscribe succeeds (or even sends, if a
SUBSCRIBEis still in flight). Previously, if the server declined the unsubscribe (e.g.BUSY,LOADING,MASTERDOWN), if the unsubscribe timed out, if aSUBSCRIBEwas unacknowledged when unsubscribed, or if a stray cleanup unsubscribe for a live subscription was declined, the server remained subscribed with nothing left on the client to clean it up until the connection was replaced.Changes
Subscription:Subscription.IsRetiredis safely readable withoutRegistrationLock(Interlocked.Exchange/Volatile.Read).EnsureSubscriptions,EnsureSubscribedToServer[Async], deferredPendingClaimrelease,ResubscribeToServer, andResubscribeStrandedShardedChannels) skip retired subscriptions.TryAdoptEndpointrecords the acked server and returnsfalsewhen retired (re-checkingIsRetiredafter publishing_currentServerso an ack racingUnsubscribeFromServercannot be missed).ConnectionMultiplexer._retiringSubscriptions, then driven byOnHeartbeat()andEnsureSubscriptions()(with disconnected endpoints cleaned up inUpdateSubscriptions()).TryUnsubscribeRetiringServerissue the fire-and-forget unsubscribe underlock (subscriptions)to order against newGetOrAddSubscriptioncalls; if a newer active subscription already owns the server, the server is dropped from the retired record without unsubscribing.TrackSubscriptionsProcessor.SetResultCore, when anUNSUBSCRIBE/PUNSUBSCRIBE/SUNSUBSCRIBEreply arrives on a server and the current active subscription for that channel is a different, non-retired object that believes it is on that server, the endpoint is cleared and the active subscription is immediately re-subscribed — preventing silent message loss if a retired unsubscribe overtakes a lock-free restore.SUBSCRIBEack arrives on another server for a live subscription (Duplicate Pub/Sub messages after Redis Sentinel failover #3336 / Pub/sub: client and server subscription state can disagree #3375 item 3), the stray registration is tracked as a retiredSingleNodeSubscriptionso declined stray cleanups are also retried via the heartbeat/restore path (and benefit from the same self-correction).SingleNodeSubscription.UnsubscribeFromServerAsyncandUnsubscribeFromServerqueue an unsubscribe behind an in-flight_pendingsubscribe on that server (fire-and-forget on the sync path so sync callers likeUnsubscribeAlldo not block or time out, while still closing the gap if the in-flight subscribe times out and its late ack is dropped byMessage.ComputeResult).TryConsumePendingUnsubscribelets the ack path skip sending a secondUNSUBSCRIBEwhen one was already queued behind the pending subscribe.Testing
FailedUnsubscribeUnitTests(RESP2 & RESP3, 18 tests): declined async/sync/fire-and-forget/multi-node unsubscribes, timed-out-then-declined, slow unsubscribe not re-sent by heartbeat, newer subscription not undone, retired unsubscribe overtaking an active subscription self-correcting, and restore skipping a retired subscription.SubscriptionRestorationTests(RESP2 & RESP3, 44 tests): the four in-flight subscribe/unsubscribe scenarios from Fix duplicate Pub/Sub subscription restoration after Sentinel failover #3337/Pub/sub: client and server subscription state can disagree #3375 (asserting singleUNSUBSCRIBEon the wire and server-sidePUBLISH == 0), sync unsubscribe during a subscribe that times out, andDeclinedStrayCleanupIsRetried.*UnitTestspass (1,242 onnet10.0); Release multi-TFM build passes with 0 warnings across all 5 TFMs.Checklist