Skip to content

Commit

Permalink
Cluster commands linkdocs (#2069)
Browse files Browse the repository at this point in the history
* Link documentation for all cluster commands

Added links to the documentation in the docstrings in redis/commands/cluster.py
Part of #1712

* copy stralgo comment from commands/core.py to commands/cluster.py

* fix linters

Co-authored-by: enjoy-binbin <[email protected]>
Co-authored-by: Chayim I. Kirshen <[email protected]>
Co-authored-by: dvora-h <[email protected]>
  • Loading branch information
4 people authored Apr 4, 2022
1 parent d4fcd99 commit 143107a
Show file tree
Hide file tree
Showing 2 changed files with 348 additions and 263 deletions.
95 changes: 90 additions & 5 deletions redis/commands/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def mget_nonatomic(self, keys, *args):
if keys belong to more than one slot.
Returns a list of values ordered identically to ``keys``
For more information see https://redis.io/commands/mget
"""

from redis.client import EMPTY_RESPONSE
Expand Down Expand Up @@ -77,6 +79,8 @@ def mset_nonatomic(self, mapping):
Splits the keys into different slots and then calls MSET
for the keys of every slot. This operation will not be atomic
if keys belong to more than one slot.
For more information see https://redis.io/commands/mset
"""

# Partition the keys by slot
Expand Down Expand Up @@ -115,6 +119,8 @@ def exists(self, *keys):
Returns the number of ``names`` that exist in the
whole cluster. The keys are first split up into slots
and then an EXISTS command is sent for every slot
For more information see https://redis.io/commands/exists
"""
return self._split_command_across_slots("EXISTS", *keys)

Expand All @@ -126,6 +132,8 @@ def delete(self, *keys):
Non-existant keys are ignored.
Returns the number of keys that were deleted.
For more information see https://redis.io/commands/del
"""
return self._split_command_across_slots("DEL", *keys)

Expand All @@ -139,6 +147,8 @@ def touch(self, *keys):
Non-existant keys are ignored.
Returns the number of keys that were touched.
For more information see https://redis.io/commands/touch
"""
return self._split_command_across_slots("TOUCH", *keys)

Expand All @@ -151,6 +161,8 @@ def unlink(self, *keys):
Non-existant keys are ignored.
Returns the number of keys that were unlinked.
For more information see https://redis.io/commands/unlink
"""
return self._split_command_across_slots("UNLINK", *keys)

Expand All @@ -164,12 +176,27 @@ class ClusterManagementCommands(ManagementCommands):
"""

def slaveof(self, *args, **kwargs):
"""
Make the server a replica of another instance, or promote it as master.
For more information see https://redis.io/commands/slaveof
"""
raise RedisClusterException("SLAVEOF is not supported in cluster mode")

def replicaof(self, *args, **kwargs):
"""
Make the server a replica of another instance, or promote it as master.
For more information see https://redis.io/commands/replicaof
"""
raise RedisClusterException("REPLICAOF is not supported in cluster" " mode")

def swapdb(self, *args, **kwargs):
"""
Swaps two Redis databases.
For more information see https://redis.io/commands/swapdb
"""
raise RedisClusterException("SWAPDB is not supported in cluster" " mode")


Expand All @@ -193,6 +220,25 @@ def stralgo(
withmatchlen=False,
**kwargs,
):
"""
Implements complex algorithms that operate on strings.
Right now the only algorithm implemented is the LCS algorithm
(longest common substring). However new algorithms could be
implemented in the future.
``algo`` Right now must be LCS
``value1`` and ``value2`` Can be two strings or two keys
``specific_argument`` Specifying if the arguments to the algorithm
will be keys or strings. strings is the default.
``len`` Returns just the len of the match.
``idx`` Returns the match positions in each string.
``minmatchlen`` Restrict the list of matches to the ones of a given
minimal length. Can be provided only when ``idx`` set to True.
``withmatchlen`` Returns the matches with the len of the match.
Can be provided only when ``idx`` set to True.
For more information see https://redis.io/commands/stralgo
"""
target_nodes = kwargs.pop("target_nodes", None)
if specific_argument == "strings" and target_nodes is None:
target_nodes = "default-node"
Expand Down Expand Up @@ -292,6 +338,8 @@ def cluster_addslots(self, target_node, *slots):
:target_node: 'ClusterNode'
The node to execute the command on
For more information see https://redis.io/commands/cluster-addslots
"""
return self.execute_command(
"CLUSTER ADDSLOTS", *slots, target_nodes=target_node
Expand All @@ -307,7 +355,7 @@ def cluster_addslotsrange(self, target_node, *slots):
:target_node: 'ClusterNode'
The node to execute the command on
For more information check https://redis.io/commands/cluster-addslotsrange
For more information see https://redis.io/commands/cluster-addslotsrange
"""
return self.execute_command(
"CLUSTER ADDSLOTSRANGE", *slots, target_nodes=target_node
Expand All @@ -317,13 +365,17 @@ def cluster_countkeysinslot(self, slot_id):
"""
Return the number of local keys in the specified hash slot
Send to node based on specified slot_id
For more information see https://redis.io/commands/cluster-countkeysinslot
"""
return self.execute_command("CLUSTER COUNTKEYSINSLOT", slot_id)

def cluster_count_failure_report(self, node_id):
"""
Return the number of failure reports active for a given node
Sends to a random node
For more information see https://redis.io/commands/cluster-count-failure-reports
"""
return self.execute_command("CLUSTER COUNT-FAILURE-REPORTS", node_id)

Expand All @@ -333,6 +385,8 @@ def cluster_delslots(self, *slots):
It determines by it self what node the slot is in and sends it there
Returns a list of the results for each processed slot.
For more information see https://redis.io/commands/cluster-delslots
"""
return [self.execute_command("CLUSTER DELSLOTS", slot) for slot in slots]

Expand All @@ -343,7 +397,7 @@ def cluster_delslotsrange(self, *slots):
from the node, while CLUSTER DELSLOTSRANGE takes a list of slot ranges to remove
from the node.
For more information check https://redis.io/commands/cluster-delslotsrange
For more information see https://redis.io/commands/cluster-delslotsrange
"""
return self.execute_command("CLUSTER DELSLOTSRANGE", *slots)

Expand All @@ -354,6 +408,8 @@ def cluster_failover(self, target_node, option=None):
:target_node: 'ClusterNode'
The node to execute the command on
For more information see https://redis.io/commands/cluster-failover
"""
if option:
if option.upper() not in ["FORCE", "TAKEOVER"]:
Expand All @@ -372,36 +428,45 @@ def cluster_info(self, target_nodes=None):
Provides info about Redis Cluster node state.
The command will be sent to a random node in the cluster if no target
node is specified.
For more information see https://redis.io/commands/cluster-info
"""
return self.execute_command("CLUSTER INFO", target_nodes=target_nodes)

def cluster_keyslot(self, key):
"""
Returns the hash slot of the specified key
Sends to random node in the cluster
For more information see https://redis.io/commands/cluster-keyslot
"""
return self.execute_command("CLUSTER KEYSLOT", key)

def cluster_meet(self, host, port, target_nodes=None):
"""
Force a node cluster to handshake with another node.
Sends to specified node.
For more information see https://redis.io/commands/cluster-meet
"""
return self.execute_command(
"CLUSTER MEET", host, port, target_nodes=target_nodes
)

def cluster_nodes(self):
"""
Force a node cluster to handshake with another node
Get Cluster config for the node.
Sends to random node in the cluster
For more information see https://redis.io/commands/cluster-nodes
"""
return self.execute_command("CLUSTER NODES")

def cluster_replicate(self, target_nodes, node_id):
"""
Reconfigure a node as a slave of the specified master node
For more information see https://redis.io/commands/cluster-replicate
"""
return self.execute_command(
"CLUSTER REPLICATE", node_id, target_nodes=target_nodes
Expand All @@ -413,6 +478,8 @@ def cluster_reset(self, soft=True, target_nodes=None):
If 'soft' is True then it will send 'SOFT' argument
If 'soft' is False then it will send 'HARD' argument
For more information see https://redis.io/commands/cluster-reset
"""
return self.execute_command(
"CLUSTER RESET", b"SOFT" if soft else b"HARD", target_nodes=target_nodes
Expand All @@ -421,18 +488,24 @@ def cluster_reset(self, soft=True, target_nodes=None):
def cluster_save_config(self, target_nodes=None):
"""
Forces the node to save cluster state on disk
For more information see https://redis.io/commands/cluster-saveconfig
"""
return self.execute_command("CLUSTER SAVECONFIG", target_nodes=target_nodes)

def cluster_get_keys_in_slot(self, slot, num_keys):
"""
Returns the number of keys in the specified cluster slot
For more information see https://redis.io/commands/cluster-getkeysinslot
"""
return self.execute_command("CLUSTER GETKEYSINSLOT", slot, num_keys)

def cluster_set_config_epoch(self, epoch, target_nodes=None):
"""
Set the configuration epoch in a new node
For more information see https://redis.io/commands/cluster-set-config-epoch
"""
return self.execute_command(
"CLUSTER SET-CONFIG-EPOCH", epoch, target_nodes=target_nodes
Expand All @@ -444,6 +517,8 @@ def cluster_setslot(self, target_node, node_id, slot_id, state):
:target_node: 'ClusterNode'
The node to execute the command on
For more information see https://redis.io/commands/cluster-setslot
"""
if state.upper() in ("IMPORTING", "NODE", "MIGRATING"):
return self.execute_command(
Expand All @@ -458,13 +533,17 @@ def cluster_setslot_stable(self, slot_id):
"""
Clears migrating / importing state from the slot.
It determines by it self what node the slot is in and sends it there.
For more information see https://redis.io/commands/cluster-setslot
"""
return self.execute_command("CLUSTER SETSLOT", slot_id, "STABLE")

def cluster_replicas(self, node_id, target_nodes=None):
"""
Provides a list of replica nodes replicating from the specified primary
target node.
For more information see https://redis.io/commands/cluster-replicas
"""
return self.execute_command(
"CLUSTER REPLICAS", node_id, target_nodes=target_nodes
Expand All @@ -473,6 +552,8 @@ def cluster_replicas(self, node_id, target_nodes=None):
def cluster_slots(self, target_nodes=None):
"""
Get array of Cluster slot to node mappings
For more information see https://redis.io/commands/cluster-slots
"""
return self.execute_command("CLUSTER SLOTS", target_nodes=target_nodes)

Expand All @@ -484,7 +565,7 @@ def cluster_links(self, target_node):
This command outputs information of all such peer links as an array.
For more information check https://redis.io/commands/cluster-links
For more information see https://redis.io/commands/cluster-links
"""
return self.execute_command("CLUSTER LINKS", target_nodes=target_node)

Expand All @@ -493,6 +574,8 @@ def readonly(self, target_nodes=None):
Enables read queries.
The command will be sent to the default cluster node if target_nodes is
not specified.
For more information see https://redis.io/commands/readonly
"""
if target_nodes == "replicas" or target_nodes == "all":
# read_from_replicas will only be enabled if the READONLY command
Expand All @@ -505,6 +588,8 @@ def readwrite(self, target_nodes=None):
Disables read queries.
The command will be sent to the default cluster node if target_nodes is
not specified.
For more information see https://redis.io/commands/readwrite
"""
# Reset read from replicas flag
self.read_from_replicas = False
Expand Down
Loading

0 comments on commit 143107a

Please sign in to comment.