-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix issues with ClusterPipeline connection management #3804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
praboud
wants to merge
7
commits into
redis:master
Choose a base branch
from
praboud:pipeline-conns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−37
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ce49d8
Fix connection leak & dirty connection reuse
praboud 9c07339
Add tests for connection leak and dirty connection reuse bugs
praboud 63db5d7
Add comment
praboud dac7ef5
Merge branch 'master' into pipeline-conns
petyaslavova a8f02b6
Update redis/cluster.py
petyaslavova 826fe06
Apply suggestions from code review
petyaslavova ec3f905
Merge branch 'master' into pipeline-conns
petyaslavova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2627,7 +2627,9 @@ def __init__(self, args, options=None, position=None): | |
| class NodeCommands: | ||
| """ """ | ||
|
|
||
| def __init__(self, parse_response, connection_pool, connection): | ||
| def __init__( | ||
| self, parse_response, connection_pool: ConnectionPool, connection: Connection | ||
| ): | ||
| """ """ | ||
| self.parse_response = parse_response | ||
| self.connection_pool = connection_pool | ||
|
|
@@ -2974,15 +2976,17 @@ def _send_cluster_commands( | |
| attempt = sorted(stack, key=lambda x: x.position) | ||
| is_default_node = False | ||
| # build a list of node objects based on node names we need to | ||
| nodes = {} | ||
| nodes: dict[str, NodeCommands] = {} | ||
| nodes_written = 0 | ||
| nodes_read = 0 | ||
|
|
||
| # as we move through each command that still needs to be processed, | ||
| # we figure out the slot number that command maps to, then from | ||
| # the slot determine the node. | ||
| for c in attempt: | ||
| command_policies = self._pipe._policy_resolver.resolve(c.args[0].lower()) | ||
| try: | ||
| # as we move through each command that still needs to be processed, | ||
| # we figure out the slot number that command maps to, then from | ||
| # the slot determine the node. | ||
| for c in attempt: | ||
| command_policies = self._pipe._policy_resolver.resolve(c.args[0].lower()) | ||
|
|
||
| while True: | ||
| # refer to our internal node -> slot table that | ||
| # tells us where a given command should route to. | ||
| # (it might be possible we have a cached node that no longer | ||
|
|
@@ -3064,50 +3068,48 @@ def _send_cluster_commands( | |
| self._nodes_manager.initialize() | ||
| if is_default_node: | ||
| self._pipe.replace_default_node() | ||
| nodes = {} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be done in the finally block? |
||
| raise | ||
| nodes[node_name] = NodeCommands( | ||
| redis_node.parse_response, | ||
| redis_node.connection_pool, | ||
| connection, | ||
| ) | ||
| nodes[node_name].append(c) | ||
| break | ||
|
|
||
| # send the commands in sequence. | ||
| # we write to all the open sockets for each node first, | ||
| # before reading anything | ||
| # this allows us to flush all the requests out across the | ||
| # network | ||
| # so that we can read them from different sockets as they come back. | ||
| # we dont' multiplex on the sockets as they come available, | ||
| # but that shouldn't make too much difference. | ||
| try: | ||
| # send the commands in sequence. | ||
| # we write to all the open sockets for each node first, | ||
| # before reading anything | ||
| # this allows us to flush all the requests out across the | ||
| # network | ||
| # so that we can read them from different sockets as they come back. | ||
| # we dont' multiplex on the sockets as they come available, | ||
| # but that shouldn't make too much difference. | ||
| node_commands = nodes.values() | ||
| for n in node_commands: | ||
| nodes_written += 1 | ||
| n.write() | ||
|
|
||
| for n in node_commands: | ||
| n.read() | ||
| nodes_read += 1 | ||
| finally: | ||
| # release all of the redis connections we allocated earlier | ||
| # release all the redis connections we allocated earlier | ||
| # back into the connection pool. | ||
| # we used to do this step as part of a try/finally block, | ||
| # but it is really dangerous to | ||
| # release connections back into the pool if for some | ||
| # reason the socket has data still left in it | ||
| # from a previous operation. The write and | ||
| # read operations already have try/catch around them for | ||
| # all known types of errors including connection | ||
| # and socket level errors. | ||
| # So if we hit an exception, something really bad | ||
| # happened and putting any oF | ||
| # these connections back into the pool is a very bad idea. | ||
| # the socket might have unread buffer still sitting in it, | ||
| # and then the next time we read from it we pass the | ||
| # buffered result back from a previous command and | ||
| # every single request after to that connection will always get | ||
| # a mismatched result. | ||
| for n in nodes.values(): | ||
| # if the connection is dirty (that is: we've written | ||
| # commands to it, but haven't read the responses), we need | ||
| # to close the connection before returning it to the pool. | ||
| # otherwise, the next caller to use this connection will | ||
| # read the response from _this_ request, not its own request. | ||
| # disconnecting discards the dirty state & forces the next | ||
| # caller to reconnect. | ||
| # NOTE: dicts have a consistent ordering; we're iterating | ||
| # through nodes.values() in the same order as we are when | ||
| # reading / writing to the connections above, which is critical | ||
| # for how we're using the nodes_written/nodes_read offsets. | ||
| for i, n in enumerate(nodes.values()): | ||
| if i < nodes_written and i >= nodes_read: | ||
| n.connection.disconnect() | ||
| n.connection_pool.release(n.connection) | ||
|
|
||
| # if the response isn't an exception it is a | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I've completely lost the plot, this used to be a
while Trueloop which alwaysbreaks on the first iteration, so this does ~nothing.