From 11473e5a12ca57802c7d9790000f4b12ef65b0c5 Mon Sep 17 00:00:00 2001 From: Fran Garcia Date: Mon, 19 Jul 2021 12:32:29 +0100 Subject: [PATCH] Fix reading from replicas in pipelined commands --- rediscluster/client.py | 8 -------- rediscluster/connection.py | 6 ++++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/rediscluster/client.py b/rediscluster/client.py index 844cb4c9..c9b4684a 100644 --- a/rediscluster/client.py +++ b/rediscluster/client.py @@ -582,7 +582,6 @@ def _execute_command(self, *args, **kwargs): redirect_addr = None asking = False - is_read_replica = False try_random_node = False slot = self._determine_slot(*args) @@ -610,7 +609,6 @@ def _execute_command(self, *args, **kwargs): slot, self.read_from_replicas and (command in READ_COMMANDS) ) - is_read_replica = node['server_type'] == 'slave' connection = self.connection_pool.get_connection_by_node(node) @@ -620,12 +618,6 @@ def _execute_command(self, *args, **kwargs): connection.send_command('ASKING') self.parse_response(connection, "ASKING", **kwargs) asking = False - if is_read_replica: - # Ask read replica to accept reads (see https://redis.io/commands/readonly) - # TODO: do we need to handle errors from this response? - connection.send_command('READONLY') - self.parse_response(connection, 'READONLY', **kwargs) - is_read_replica = False connection.send_command(*args) return self.parse_response(connection, command, **kwargs) diff --git a/rediscluster/connection.py b/rediscluster/connection.py index 70dadd0b..43511d05 100644 --- a/rediscluster/connection.py +++ b/rediscluster/connection.py @@ -270,7 +270,8 @@ def make_connection(self, node): self._created_connections_per_node.setdefault(node['name'], 0) self._created_connections_per_node[node['name']] += 1 - connection = self.connection_class(host=node["host"], port=node["port"], **self.connection_kwargs) + readonly = node["server_type"] == "slave" + connection = self.connection_class(host=node["host"], port=node["port"], readonly=readonly, **self.connection_kwargs) # Must store node in the connection to make it easier to track connection.node = node @@ -472,7 +473,8 @@ def reset(self): def make_connection(self, node): """ Create a new connection """ - connection = self.connection_class(host=node["host"], port=node["port"], **self.connection_kwargs) + readonly = node["server_type"] == "slave" + connection = self.connection_class(host=node["host"], port=node["port"], readonly=readonly, **self.connection_kwargs) self._connections.append(connection) connection.node = node return connection