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
sunsubscribemessage sent by the server due to slot migration, and the message was wrongly delivered to the (1)SUNSUBSCRIBErequest 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)
MOVEDwas also wrongly delivered to (3), theHGETrequest. 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
sunsubscribemessages due to slot migration, but it is impossible. Our only choice is to ignore allsunsubscribemessages and make a dummy response for theSUNSUBSCRIBErequest. BesidesSUNSUBSCRIBE, we also apply this method to all*UNSUBSCRIBEso that we don't need to keep track of how many channels and patterns the client has subscribed to.*unsubscribemessages while delivering messages to requests.*UNSUBSCRIBErequests by injecting aPINGrequest after each*UNSUBSCRIBE.*UNSUBSCRIBErequest succeeds, then thePONGresponse will be delivered to it.*UNSUBSCRIBErequest fails, such as MOVED or CLUSTERDOWN, then the error will be delivered to it, and the followingPONGresponse will be discarded.If the injected
PINGfailed with a NOPERM error, we treat it as a normal PONG response.