Skip to content
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

async_cluster: improve docs #2208

Merged
merged 1 commit into from
Jun 2, 2022
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
7 changes: 5 additions & 2 deletions docs/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ RedisCluster (Async)
====================
.. autoclass:: redis.asyncio.cluster.RedisCluster
:members:
:member-order: bysource

ClusterNode (Async)
===================
.. autoclass:: redis.asyncio.cluster.ClusterNode
:members:
:member-order: bysource

ClusterPipeline (Async)
===================
=======================
.. autoclass:: redis.asyncio.cluster.ClusterPipeline
:members:
:members: execute_command, execute
:member-order: bysource


Connection
Expand Down
73 changes: 37 additions & 36 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,9 @@ async def on_connect(self, connection: Connection) -> None:
if str_if_bytes(await connection.read_response_without_lock()) != "OK":
raise ConnectionError("READONLY command failed")

def get_node(
self,
host: Optional[str] = None,
port: Optional[int] = None,
node_name: Optional[str] = None,
) -> Optional["ClusterNode"]:
"""Get node by (host, port) or node_name."""
return self.nodes_manager.get_node(host, port, node_name)
def get_nodes(self) -> List["ClusterNode"]:
"""Get all nodes of the cluster."""
return list(self.nodes_manager.nodes_cache.values())

def get_primaries(self) -> List["ClusterNode"]:
"""Get the primary nodes of the cluster."""
Expand All @@ -400,9 +395,29 @@ def get_random_node(self) -> "ClusterNode":
"""Get a random node of the cluster."""
return random.choice(list(self.nodes_manager.nodes_cache.values()))

def get_nodes(self) -> List["ClusterNode"]:
"""Get all nodes of the cluster."""
return list(self.nodes_manager.nodes_cache.values())
def get_default_node(self) -> "ClusterNode":
"""Get the default node of the client."""
return self.nodes_manager.default_node

def set_default_node(self, node: "ClusterNode") -> None:
"""
Set the default node of the client.

:raises DataError: if None is passed or node does not exist in cluster.
"""
if not node or not self.get_node(node_name=node.name):
raise DataError("The requested node does not exist in the cluster.")

self.nodes_manager.default_node = node

def get_node(
self,
host: Optional[str] = None,
port: Optional[int] = None,
node_name: Optional[str] = None,
) -> Optional["ClusterNode"]:
"""Get node by (host, port) or node_name."""
return self.nodes_manager.get_node(host, port, node_name)

def get_node_from_key(
self, key: str, replica: bool = False
Expand All @@ -413,6 +428,7 @@ def get_node_from_key(
:param key:
:param replica:
| Indicates if a replica should be returned
|
None will returned if no replica holds this key

:raises SlotNotCoveredError: if the key is not covered by any slot.
Expand All @@ -431,24 +447,13 @@ def get_node_from_key(

return slot_cache[node_idx]

def get_default_node(self) -> "ClusterNode":
"""Get the default node of the client."""
return self.nodes_manager.default_node

def set_default_node(self, node: "ClusterNode") -> None:
def keyslot(self, key: EncodableT) -> int:
"""
Set the default node of the client.
Find the keyslot for a given key.

:raises DataError: if None is passed or node does not exist in cluster.
See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding
"""
if not node or not self.get_node(node_name=node.name):
raise DataError("The requested node does not exist in the cluster.")

self.nodes_manager.default_node = node

def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None:
"""Set a custom response callback."""
self.response_callbacks[command] = callback
return key_slot(self.encoder.encode(key))

def get_encoder(self) -> Encoder:
"""Get the encoder object of the client."""
Expand All @@ -458,14 +463,9 @@ def get_connection_kwargs(self) -> Dict[str, Optional[Any]]:
"""Get the kwargs passed to :class:`~redis.asyncio.connection.Connection`."""
return self.connection_kwargs

def keyslot(self, key: EncodableT) -> int:
"""
Find the keyslot for a given key.

See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding
"""
k = self.encoder.encode(key)
return key_slot(k)
def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None:
"""Set a custom response callback."""
self.response_callbacks[command] = callback

async def _determine_nodes(
self, command: str, *args: Any, node_flag: Optional[str] = None
Expand Down Expand Up @@ -776,7 +776,6 @@ def __init__(
server_type: Optional[str] = None,
max_connections: int = 2**31,
connection_class: Type[Connection] = Connection,
response_callbacks: Dict[str, Any] = RedisCluster.RESPONSE_CALLBACKS,
**connection_kwargs: Any,
) -> None:
if host == "localhost":
Expand All @@ -792,7 +791,9 @@ def __init__(
self.max_connections = max_connections
self.connection_class = connection_class
self.connection_kwargs = connection_kwargs
self.response_callbacks = response_callbacks
self.response_callbacks = connection_kwargs.pop(
"response_callbacks", RedisCluster.RESPONSE_CALLBACKS
)

self._connections: List[Connection] = []
self._free: Deque[Connection] = collections.deque(maxlen=self.max_connections)
Expand Down