-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Adding RedisCluster client to support Redis Cluster Mode #1660
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1660 +/- ##
==========================================
+ Coverage 89.04% 90.13% +1.08%
==========================================
Files 53 59 +6
Lines 11052 14085 +3033
==========================================
+ Hits 9841 12695 +2854
- Misses 1211 1390 +179
Continue to review full report at Codecov.
|
@barshaul Thanks for the submission - this is so exciting! We'll start to review it over the next short while - with the goal of getting this into the next beta, as discussed. Just one ask as we go through this - mind having a look, to see why codecov coverage dropped from ~89% down to ~77% with this commit? High test coverage is a big goal. |
redis/commands/core.py
Outdated
|
||
# SERVER INFORMATION | ||
|
||
class AclCommands: |
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.
kk, so for this PR scope- I'll refactor the classes' names and add docstring, but I can leave all in the same file, yup?
def check_slots_coverage(self, slots_cache): | ||
# Validate if all slots are covered or if we should try next | ||
# startup node | ||
for i in range(0, REDIS_CLUSTER_HASH_SLOTS): |
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.
It will not give us the required result. We want to check if all slots from 0 to REDIS_CLUSTER_HASH_SLOTS are found in slots_cache, what you suggests mean that if we'll have slots not in the above range we'll get false, but we can have only one slot in slots_cache and it will return true...
|
||
|
||
class NodesManager: | ||
def __init__(self, startup_nodes, from_url=False, |
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.
If the user creates an instance with redisCluster.from_url(), then the url is being parsed and processed to a startup node in the RedisCluster init method, which then passes the startup node to the nodeManager.
So, no matter what - the nodes manager excepts to get startup_nodes. from_url bool is only there to indicate if we need to create all the other cluster's Redis nodes with a costumed connection pool. See NodesManager::create_redis_node
…on key-based commands to be executed against the default node, if target_nodes were not passed. All tests were adjusted.
…etting the port to the default value (6379). e.g. "redis://localhost" will be parsed to host=localhost, port=6379
… reports so it won't get override.
…he codecov ignore section. removed the 'non redismod' marker from the pytest run in install_and_test.sh
* redis/redis-py#1660 Signed-off-by: Andrew-Chen-Wang <acwangpython@gmail.com>
Pull Request check-list
Please make sure to review and check all of these items:
$ tox
pass with this change (including linting)?NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.
Description of change
closes #931
Added support for cluster mode and provides a client for
Redis Cluster.
The cluster client is based on redis-py-cluster
by Grokzen, with a lot of added and
changed functionality.
Create RedisCluster:
Connecting redis-py to the Redis Cluster instance(s) is easy.
RedisCluster requires at least one node to discover the whole cluster nodes,
and there is multiple ways of creating a RedisCluster instance:
When a RedisCluster instance is being created it first attempts to establish a
connection to one of the provided startup nodes. If none of the startup nodes
are reachable, a 'RedisClusterException' will be thrown.
After a connection to the one of the cluster's nodes is established, the
RedisCluster instance will be initialized with 3 caches:
a slots cache which maps each of the 16384 slots to the node/s handling them,
a nodes cache that contains ClusterNode objects (name, host, port, redis connection)
for all of the cluster's nodes, and a commands cache contains all the server
supported commands that were retrieved using the Redis 'COMMAND' output.
RedisCluster instance can be directly used to execute Redis commands. When a
command is being executed through the cluster instance, the target node(s) will
be internally determined. When using a key-based command, the target node will
be the node that holds the key's slot.
Cluster management commands or other cluster commands have predefined node
group targets (all-primaries, all-nodes, random-node, all-replicas), which are
outlined in the command’s function documentation.
For example, ‘KEYS’ command will be sent to all primaries and return all keys
in the cluster, and ‘CLUSTER NODES’ command will be sent to a random node.
Other management commands will require you to pass the target node/s to execute
the command on.
Specifying Target Nodes:
As mentioned above, some RedisCluster commands will require you to provide the
target node/s that you want to execute the command on, and in other cases, the
target node will be determined by the client itself. That being said, ALL
RedisCluster commands can be executed against a specific node or a group of
nodes by passing the command kwarg
target_nodes
.The best practice is to specify target nodes using RedisCluster class's node
flags: PRIMARIES, REPLICAS, ALL_NODES, RANDOM. When a nodes flag is passed
along with a command, it will be internally resolved to the relevant node/s.
If the nodes topology of the cluster changes during the execution of a command,
the client will be able to resolve the nodes flag again with the new topology
and attempt to retry executing the command.
You could also pass ClusterNodes directly if you want to execute a command on a
specific node / node group that isn't addressed by the nodes flag. However, if
the command execution fails due to cluster topology changes, a retry attempt
will not be made, since the passed target node/s may no longer be valid, and
the relevant cluster or connection error will be returned.
In addition, you can use the RedisCluster instance to obtain the Redis instance
of a specific node and execute commands on that node directly. The Redis client,
however, cannot handle cluster failures and retries.
Multi-key commands:
Redis supports multi-key commands in Cluster Mode, such as Set type unions or
intersections, mset and mget, as long as the keys all hash to the same slot.
By using RedisCluster client, you can use the known functions (e.g. mget, mset)
to perform an atomic multi-key operation. However, you must ensure all keys are
mapped to the same slot, otherwise a RedisClusterException will be thrown.
Redis Cluster implements a concept called hash tags that can be used in order
to force certain keys to be stored in the same hash slot, see
Keys hash tag.
You can also use nonatomic for some of the multikey operations, and pass keys
that aren't mapped to the same slot. The client will then map the keys to the
relevant slots, sending the commands to the slots' node owners. Non-atomic
operations batch the keys according to their hash value, and then each batch is
sent separately to the slot's owner.
Cluster PubSub:
When a ClusterPubSub instance is created without specifying a node, a single
node will be transparently chosen for the pubsub connection on the
first command execution. The node will be determined by:
set to true, a replica can be selected.
Known limitations with pubsub:
Pattern subscribe and publish do not work properly because if we hash a pattern
like fo* we will get a keyslot for that string but there is a endless
possibilities of channel names based on that pattern that we can’t know in
advance. This feature is not limited but the commands is not recommended to use
right now.
See redis-py-cluster documentaion
for more.
Read Only Mode
By default, Redis Cluster always returns MOVE redirection response on accessing
a replica node. You can overcome this limitation and scale read commands with
READONLY mode.
To enable READONLY mode pass read_from_replicas=True to RedisCluster
constructor. When set to true, read commands will be assigned between the
primary and its replications in a Round-Robin manner.
You could also enable READONLY mode in runtime by running readonly() method,
or disable it with readwrite().
See Redis Cluster tutorial and
Redis Cluster specifications
to learn more about Redis Cluster.