Skip to content

Commit 63cf7ec

Browse files
barshauldvora-h
andauthored
Added dynamic_startup_nodes configuration to RedisCluster. (#2244)
* Added dynamic_startup_nodes configuration to RedisCluster. By default, uses only the initial passed startup nodes to refresh the cluster topology. If set to true, sets the startup nodes to all of the discovered nodes. * Added RedisCluster specific options to the README file and updated CHANGES * Fixed timeout_error_topology_refresh test Co-authored-by: dvora-h <[email protected]>
1 parent d3a7a75 commit 63cf7ec

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

CHANGES

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* Fix broken connection writer lock-up for asyncio (#2065)
1212
* Fix auth bug when provided with no username (#2086)
1313
* Fix missing ClusterPipeline._lock (#2189)
14+
* Added dynaminc_startup_nodes configuration to RedisCluster
1415
* Fix reusing the old nodes' connections when cluster topology refresh is being done
1516
* Fix RedisCluster to immediately raise AuthenticationError without a retry
16-
1717
* 4.1.3 (Feb 8, 2022)
1818
* Fix flushdb and flushall (#1926)
1919
* Add redis5 and redis4 dockers (#1871)

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ a slots cache which maps each of the 16384 slots to the node/s handling them,
10061006
a nodes cache that contains ClusterNode objects (name, host, port, redis connection)
10071007
for all of the cluster's nodes, and a commands cache contains all the server
10081008
supported commands that were retrieved using the Redis 'COMMAND' output.
1009+
See *RedisCluster specific options* below for more.
10091010

10101011
RedisCluster instance can be directly used to execute Redis commands. When a
10111012
command is being executed through the cluster instance, the target node(s) will
@@ -1245,6 +1246,55 @@ The following commands are not supported:
12451246

12461247
Using scripting within pipelines in cluster mode is **not supported**.
12471248

1249+
1250+
**RedisCluster specific options**
1251+
1252+
require_full_coverage: (default=False)
1253+
1254+
When set to False (default value): the client will not require a
1255+
full coverage of the slots. However, if not all slots are covered,
1256+
and at least one node has 'cluster-require-full-coverage' set to
1257+
'yes,' the server will throw a ClusterDownError for some key-based
1258+
commands. See -
1259+
https://redis.io/topics/cluster-tutorial#redis-cluster-configuration-parameters
1260+
When set to True: all slots must be covered to construct the
1261+
cluster client. If not all slots are covered, RedisClusterException
1262+
will be thrown.
1263+
1264+
read_from_replicas: (default=False)
1265+
1266+
Enable read from replicas in READONLY mode. You can read possibly
1267+
stale data.
1268+
When set to true, read commands will be assigned between the
1269+
primary and its replications in a Round-Robin manner.
1270+
1271+
dynamic_startup_nodes: (default=False)
1272+
1273+
Set the RedisCluster's startup nodes to all of the discovered nodes.
1274+
If true, the cluster's discovered nodes will be used to determine the
1275+
cluster nodes-slots mapping in the next topology refresh.
1276+
It will remove the initial passed startup nodes if their endpoints aren't
1277+
listed in the CLUSTER SLOTS output.
1278+
If you use dynamic DNS endpoints for startup nodes but CLUSTER SLOTS lists
1279+
specific IP addresses, keep it at false.
1280+
1281+
cluster_error_retry_attempts: (default=3)
1282+
1283+
Retry command execution attempts when encountering ClusterDownError
1284+
or ConnectionError
1285+
1286+
reinitialize_steps: (default=10)
1287+
1288+
Specifies the number of MOVED errors that need to occur before
1289+
reinitializing the whole cluster topology. If a MOVED error occurs
1290+
and the cluster does not need to be reinitialized on this current
1291+
error handling, only the MOVED slot will be patched with the
1292+
redirected node.
1293+
To reinitialize the cluster on every MOVED error, set
1294+
reinitialize_steps to 1.
1295+
To avoid reinitializing the cluster on moved errors, set
1296+
reinitialize_steps to 0.
1297+
12481298
### Author
12491299

12501300
redis-py is developed and maintained by [Redis Inc](https://redis.com). It can be found [here](

redis/cluster.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ def __init__(
482482
require_full_coverage=False,
483483
reinitialize_steps=10,
484484
read_from_replicas=False,
485+
dynamic_startup_nodes=False,
485486
url=None,
486487
**kwargs,
487488
):
@@ -509,6 +510,14 @@ def __init__(
509510
stale data.
510511
When set to true, read commands will be assigned between the
511512
primary and its replications in a Round-Robin manner.
513+
:dynamic_startup_nodes: 'bool'
514+
Set the RedisCluster's startup nodes to all of the discovered nodes.
515+
If true, the cluster's discovered nodes will be used to determine the
516+
cluster nodes-slots mapping in the next topology refresh.
517+
It will remove the initial passed startup nodes if their endpoints aren't
518+
listed in the CLUSTER SLOTS output.
519+
If you use dynamic DNS endpoints for startup nodes but CLUSTER SLOTS lists
520+
specific IP addresses, keep it at false.
512521
:cluster_error_retry_attempts: 'int'
513522
Retry command execution attempts when encountering ClusterDownError
514523
or ConnectionError
@@ -598,6 +607,7 @@ def __init__(
598607
startup_nodes=startup_nodes,
599608
from_url=from_url,
600609
require_full_coverage=require_full_coverage,
610+
dynamic_startup_nodes=dynamic_startup_nodes,
601611
**kwargs,
602612
)
603613

@@ -1283,6 +1293,7 @@ def __init__(
12831293
from_url=False,
12841294
require_full_coverage=False,
12851295
lock=None,
1296+
dynamic_startup_nodes=False,
12861297
**kwargs,
12871298
):
12881299
self.nodes_cache = {}
@@ -1292,6 +1303,7 @@ def __init__(
12921303
self.populate_startup_nodes(startup_nodes)
12931304
self.from_url = from_url
12941305
self._require_full_coverage = require_full_coverage
1306+
self._dynamic_startup_nodes = dynamic_startup_nodes
12951307
self._moved_exception = None
12961308
self.connection_kwargs = kwargs
12971309
self.read_load_balancer = LoadBalancer()
@@ -1612,8 +1624,9 @@ def initialize(self):
16121624
self.slots_cache = tmp_slots
16131625
# Set the default node
16141626
self.default_node = self.get_nodes_by_server_type(PRIMARY)[0]
1615-
# Populate the startup nodes with all discovered nodes
1616-
self.startup_nodes = tmp_nodes_cache
1627+
if self._dynamic_startup_nodes:
1628+
# Populate the startup nodes with all discovered nodes
1629+
self.startup_nodes = tmp_nodes_cache
16171630
# If initialize was called after a MovedError, clear it
16181631
self._moved_exception = None
16191632

tests/test_cluster.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def __init__(self, val=0):
673673

674674
def moved_redirect_effect(connection, *args, **options):
675675
# raise a timeout for 5 times so we'll need to reinitilize the topology
676-
if count.val >= 5:
676+
if count.val == 4:
677677
parse_response.side_effect = real_func
678678
count.val += 1
679679
raise TimeoutError()
@@ -2285,6 +2285,27 @@ def cmd_init_mock(self, r):
22852285
assert rc.get_node(host=default_host, port=7001) is not None
22862286
assert rc.get_node(host=default_host, port=7002) is not None
22872287

2288+
@pytest.mark.parametrize("dynamic_startup_nodes", [True, False])
2289+
def test_init_slots_dynamic_startup_nodes(self, dynamic_startup_nodes):
2290+
rc = get_mocked_redis_client(
2291+
2292+
port=7000,
2293+
cluster_slots=default_cluster_slots,
2294+
dynamic_startup_nodes=dynamic_startup_nodes,
2295+
)
2296+
# Nodes are taken from default_cluster_slots
2297+
discovered_nodes = [
2298+
"127.0.0.1:7000",
2299+
"127.0.0.1:7001",
2300+
"127.0.0.1:7002",
2301+
"127.0.0.1:7003",
2302+
]
2303+
startup_nodes = list(rc.nodes_manager.startup_nodes.keys())
2304+
if dynamic_startup_nodes is True:
2305+
assert startup_nodes.sort() == discovered_nodes.sort()
2306+
else:
2307+
assert startup_nodes == ["[email protected]:7000"]
2308+
22882309

22892310
@pytest.mark.onlycluster
22902311
class TestClusterPubSubObject:

0 commit comments

Comments
 (0)