fix: protocol out of sync when mixing sunsubscribe requests and slot migrations #691
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 #690
In the TCP dumps provided by the issuer, we found the following traces during slot migration:
In the above sequence, (2) was the proactive
sunsubscribe
message sent by the server due to slot migration, and the message was wrongly delivered to the (1)SUNSUBSCRIBE
request sent by the user. The true response to the (1) should be (4), theMOVED
.The wrong delivery of (2) caused the pipeline out-of-sync. The (4)
MOVED
was also wrongly delivered to (3), theHGET
request. Then finally the (5), the true response of theHGET
, couldn't be delivered anywhere, thus the pipeline panicked.This situation is the same as valkey-io/valkey#1066.
Workaround
Ideally, we need to differentiate whether the server sent those
sunsubscribe
messages due to slot migration, but it is impossible. Our only choice is to ignore allsunsubscribe
messages and make a dummy response for theSUNSUBSCRIBE
request. BesidesSUNSUBSCRIBE
, we also apply this method to all*UNSUBSCRIBE
so that we don't need to keep track of how many channels and patterns the client has subscribed to.*unsubscribe
messages while delivering messages to requests.*UNSUBSCRIBE
requests by injecting aPING
request after each*UNSUBSCRIBE
.*UNSUBSCRIBE
request succeeds, then thePONG
response will be delivered to it.*UNSUBSCRIBE
request fails, such as MOVED or CLUSTERDOWN, then the error will be delivered to it, and the followingPONG
response will be discarded.If the injected
PING
failed with a NOPERM error, we treat it as a normal PONG response.