Skip to content

Commit 05dbebc

Browse files
committed
add shuffle_replicas config option
PYTHON-643
1 parent 9b8f7ef commit 05dbebc

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

cassandra/policies.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from itertools import islice, cycle, groupby, repeat
1616
import logging
17-
from random import randint
17+
from random import randint, shuffle
1818
from threading import Lock
1919
import socket
2020

@@ -320,13 +320,18 @@ class TokenAwarePolicy(LoadBalancingPolicy):
320320
321321
If no :attr:`~.Statement.routing_key` is set on the query, the child
322322
policy's query plan will be used as is.
323+
324+
If :attr:`.shuffle_replicas` is truthy, :attr:`~.HostDistance.LOCAL`
325+
replicas will be yielded in a random order, followed by the remaining
326+
hosts in the order provided child policy's query plan.
323327
"""
324328

325329
_child_policy = None
326330
_cluster_metadata = None
327331

328-
def __init__(self, child_policy):
332+
def __init__(self, child_policy, shuffle_replicas=False):
329333
self._child_policy = child_policy
334+
self.shuffle_replicas = shuffle_replicas
330335

331336
def populate(self, cluster, hosts):
332337
self._cluster_metadata = cluster.metadata
@@ -361,6 +366,8 @@ def make_query_plan(self, working_keyspace=None, query=None):
361366
yield host
362367
else:
363368
replicas = self._cluster_metadata.get_replicas(keyspace, routing_key)
369+
if self.shuffle_replicas:
370+
shuffle(replicas)
364371
for replica in replicas:
365372
if replica.is_up and \
366373
child.distance(replica) == HostDistance.LOCAL:

0 commit comments

Comments
 (0)