Skip to content

Migrated targeted nodes to kwargs in Cluster Mode #1762

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

Merged
merged 10 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ and attempt to retry executing the command.
>>> rc.cluster_meet('127.0.0.1', 6379, target_nodes=Redis.ALL_NODES)
>>> # ping all replicas
>>> rc.ping(target_nodes=Redis.REPLICAS)
>>> # ping a specific node
>>> # ping a random node
>>> rc.ping(target_nodes=Redis.RANDOM)
>>> # get the keys from all cluster nodes
>>> rc.keys(target_nodes=Redis.ALL_NODES)
Expand Down Expand Up @@ -1158,7 +1158,7 @@ readwrite() method.
>>> from cluster import RedisCluster as Redis
# Use 'debug' log level to print the node that the command is executed on
>>> rc_readonly = Redis(startup_nodes=startup_nodes,
read_from_replicas=True, debug=True)
read_from_replicas=True)
>>> rc_readonly.set('{foo}1', 'bar1')
>>> for i in range(0, 4):
# Assigns read command to the slot's hosts in a Round-Robin manner
Expand Down
12 changes: 12 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,18 @@ def __init__(
def __repr__(self):
return f"{type(self).__name__}<{repr(self.connection_pool)}>"

def get_encoder(self):
"""
Get the connection pool's encoder
"""
return self.connection_pool.get_encoder()

def get_connection_kwargs(self):
"""
Get the connection's key-word arguments
"""
return self.connection_pool.connection_kwargs

def set_response_callback(self, command, callback):
"Set a custom Response Callback"
self.response_callbacks[command] = callback
Expand Down
39 changes: 33 additions & 6 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import OrderedDict

from redis.client import CaseInsensitiveDict, PubSub, Redis
from redis.commands import ClusterCommands, CommandsParser
from redis.commands import CommandsParser, RedisClusterCommands
from redis.connection import ConnectionPool, DefaultParser, Encoder, parse_url
from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot
from redis.exceptions import (
Expand Down Expand Up @@ -94,6 +94,7 @@ def fix_server(*args):
"charset",
"connection_class",
"connection_pool",
"client_name",
"db",
"decode_responses",
"encoding",
Expand Down Expand Up @@ -198,7 +199,7 @@ class ClusterParser(DefaultParser):
)


class RedisCluster(ClusterCommands):
class RedisCluster(RedisClusterCommands):
RedisClusterRequestTTL = 16

PRIMARIES = "primaries"
Expand All @@ -212,6 +213,18 @@ class RedisCluster(ClusterCommands):
COMMAND_FLAGS = dict_merge(
list_keys_to_dict(
[
"ACL CAT",
"ACL DELUSER",
"ACL GENPASS",
"ACL GETUSER",
"ACL HELP",
"ACL LIST",
"ACL LOG",
"ACL LOAD",
"ACL SAVE",
"ACL SETUSER",
"ACL USERS",
"ACL WHOAMI",
"CLIENT LIST",
"CLIENT SETNAME",
"CLIENT GETNAME",
Expand Down Expand Up @@ -770,6 +783,18 @@ def determine_slot(self, *args):
def reinitialize_caches(self):
self.nodes_manager.initialize()

def get_encoder(self):
"""
Get the connections' encoder
"""
return self.encoder

def get_connection_kwargs(self):
"""
Get the connections' key-word arguments
"""
return self.nodes_manager.connection_kwargs

def _is_nodes_flag(self, target_nodes):
return isinstance(target_nodes, str) and target_nodes in self.node_flags

Expand Down Expand Up @@ -1383,7 +1408,8 @@ def initialize(self):
# isn't a full coverage
raise RedisClusterException(
f"All slots are not covered after query all startup_nodes. "
f"{len(self.slots_cache)} of {REDIS_CLUSTER_HASH_SLOTS} covered..."
f"{len(self.slots_cache)} of {REDIS_CLUSTER_HASH_SLOTS} "
f"covered..."
)
elif not fully_covered and not self._require_full_coverage:
# The user set require_full_coverage to False.
Expand All @@ -1402,7 +1428,8 @@ def initialize(self):
"cluster-require-full-coverage configuration to no on "
"all of the cluster nodes if you wish the cluster to "
"be able to serve without being fully covered."
f"{len(self.slots_cache)} of {REDIS_CLUSTER_HASH_SLOTS} covered..."
f"{len(self.slots_cache)} of {REDIS_CLUSTER_HASH_SLOTS} "
f"covered..."
)

# Set the tmp variables to the real variables
Expand Down Expand Up @@ -1950,8 +1977,8 @@ def block_pipeline_command(func):

def inner(*args, **kwargs):
raise RedisClusterException(
f"ERROR: Calling pipelined function {func.__name__} is blocked when "
f"running redis in cluster mode..."
f"ERROR: Calling pipelined function {func.__name__} is blocked "
f"when running redis in cluster mode..."
)

return inner
Expand Down
4 changes: 2 additions & 2 deletions redis/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from .cluster import ClusterCommands
from .cluster import RedisClusterCommands
from .core import CoreCommands
from .helpers import list_or_args
from .parser import CommandsParser
from .redismodules import RedisModuleCommands
from .sentinel import SentinelCommands

__all__ = [
"ClusterCommands",
"RedisClusterCommands",
"CommandsParser",
"CoreCommands",
"list_or_args",
Expand Down
Loading