Skip to content

Commit 4f01775

Browse files
committed
feat: add make_query_plan_with_exclusion to RackAwareRoundRobinPolicy
Optimized exclusion-aware query plan that avoids re-computing non-local-rack and remote host lists.
1 parent 9f34e9e commit 4f01775

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

cassandra/policies.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,56 @@ def make_query_plan(self, working_keyspace=None, query=None):
503503
for host in remote_hosts:
504504
yield host
505505

506+
def make_query_plan_with_exclusion(self, working_keyspace=None, query=None, excluded=()):
507+
pos = self._position
508+
self._position += 1
509+
510+
local_rack_live = self._live_hosts.get((self.local_dc, self.local_rack), ())
511+
length = len(local_rack_live)
512+
remote_hosts = self._remote_hosts
513+
if not excluded:
514+
if length:
515+
p = pos % length
516+
for i in range(length):
517+
yield local_rack_live[(p + i) % length]
518+
519+
local_non_rack = self._non_local_rack_hosts
520+
length = len(local_non_rack)
521+
if length:
522+
p = pos % length
523+
for i in range(length):
524+
yield local_non_rack[(p + i) % length]
525+
526+
for host in remote_hosts:
527+
yield host
528+
return
529+
530+
if not isinstance(excluded, set):
531+
excluded = set(excluded)
532+
533+
if length:
534+
p = pos % length
535+
for i in range(length):
536+
host = local_rack_live[(p + i) % length]
537+
if host in excluded:
538+
continue
539+
yield host
540+
541+
local_non_rack = self._non_local_rack_hosts
542+
length = len(local_non_rack)
543+
if length:
544+
p = pos % length
545+
for i in range(length):
546+
host = local_non_rack[(p + i) % length]
547+
if host in excluded:
548+
continue
549+
yield host
550+
551+
for host in remote_hosts:
552+
if host in excluded:
553+
continue
554+
yield host
555+
506556
def on_up(self, host):
507557
dc = self._dc(host)
508558
rack = self._rack(host)

0 commit comments

Comments
 (0)