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.    
 
Pull Request check-list
Please make sure to review and check all of these items:
NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.
Description of change
NodesManagercan mutate state (ie: nodes cache + slots cache + startup nodes + default node) from multiple client threads, through both re-initializing fromCLUSTER SLOTS, and from following aMOVED/ASKredirect. Right now, there isn't proper synchronization of state across multiple threads, which can result in the state getting corrupted, or the NodesManager otherwise behaving weirdly:update_moved_exceptionjust sets an exception on theNodesManager, which we expect to trigger an update to the state the next time we fetch a node from theNodesManager. But_moved_exceptionisn't synchronized. Suppose two threads A & B sequence like: A calls update_moved_exception, B calls update_moved_exception, A calls get_node_from_slot, B calls get_node_from_slot. A's update to_moved_exceptiongets lost, and when A callsget_node_from_slot, it doesn't actually follow the redirect. To avoid this problem, I've changed the slot move logic to immediately apply the update to the slot state, rather than queueing it up for later._get_or_create_cluster_nodecan mutate theroleof aClusterNode, but the node is referenced from theslots_cache. Because we expectslots_cache[node][0]to always be the primary, this can cause strange behavior for readers ofslots_cachebetween the time_get_or_create_cluster_nodeis called, and wheninitializeresetsslots_cacheat the end of the update.initialize&_update_moved_slotsboth mutateslots_cache, and aren't synchronized with each other. This can cause the slots cache to get into a weird state, where eg: nodes are deleted from the slots cache, or duplicated.initializeallows multiple callers to initialize concurrently, which is both extra load on the cluster, and can cause strange behavior in corner cases.To fix all of these:
self._lockaround all places where any mutation happens inNodesManager.update_moved_exception&_update_moved_slotswith justmove_slot, to avoid racing multiple slot updates._initialize_lockto serialize / deduplicate calls toinitialize.I've added some tests to try to exercise the situations above, and verified that they fail before / pass after this change.
This PR mainly focuses on the sync Redis client, but I've tried to update the asyncio one as well. It doesn't (as far as I can tell) suffer from most of these issues, other than issue 4, so the changes there are a bit lighter. This PR is pretty hefty already - I'm happy to split the asyncio changes out into a separate PR if that's easier for review.
Also, in general, this PR is a lot easier to review with "ignore whitespace" turned on in the diff options, because a lot of these changes mean indenting big blocks of code inside a
with self._lockblock.