Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1262d43

Browse files
mykauldkropachev
authored andcommitted
(minor improvement): small refactoring in NetworkTopologyStrategy's make_token_replica_map()
While trying to look at some random (flaky?) test (#510 (comment) ) I saw some (minor) improvements that can be made to NetworkTopologyStrategy's make_token_replica_map(): 1. Remove some redundant len() calls to outside the loop(s) 2. Align some variable names, start with num_ ... for them. 3. Move token_offset and host assignment within the loop to closer to where it's used. 4. Only add DCs and hosts that are in the map All those are probably very very minor improvements, perhaps in a large cluster it'll be noticable. Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent 8acf8f9 commit 1262d43

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

‎cassandra/metadata.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,14 @@ def make_token_replica_map(self, token_to_host_owner, ring):
629629
hosts_per_dc = defaultdict(set)
630630
for i, token in enumerate(ring):
631631
host = token_to_host_owner[token]
632-
dc_to_token_offset[host.datacenter].append(i)
633-
if host.datacenter and host.rack:
634-
dc_racks[host.datacenter].add(host.rack)
635-
hosts_per_dc[host.datacenter].add(host)
632+
host_dc = host.datacenter
633+
if host_dc in dc_rf_map:
634+
# if the host is in a DC that has a replication factor, add it
635+
# to the list of token offsets for that DC
636+
dc_to_token_offset[host_dc].append(i)
637+
if host.rack:
638+
dc_racks[host_dc].add(host.rack)
639+
hosts_per_dc[host_dc].add(host)
636640

637641
# A map of DCs to an index into the dc_to_token_offset value for that dc.
638642
# This is how we keep track of advancing around the ring for each DC.
@@ -644,8 +648,6 @@ def make_token_replica_map(self, token_to_host_owner, ring):
644648

645649
# go through each DC and find the replicas in that DC
646650
for dc in dc_to_token_offset.keys():
647-
if dc not in dc_rf_map:
648-
continue
649651

650652
# advance our per-DC index until we're up to at least the
651653
# current token in the ring
@@ -657,34 +659,34 @@ def make_token_replica_map(self, token_to_host_owner, ring):
657659
dc_to_current_index[dc] = index
658660

659661
replicas_remaining = dc_rf_map[dc]
660-
replicas_this_dc = 0
662+
num_replicas_this_dc = 0
661663
skipped_hosts = []
662664
racks_placed = set()
663-
racks_this_dc = dc_racks[dc]
664-
hosts_this_dc = len(hosts_per_dc[dc])
665+
num_racks_this_dc = len(dc_racks[dc])
666+
num_hosts_this_dc = len(hosts_per_dc[dc])
665667

666668
for token_offset_index in range(index, index+num_tokens):
667-
if token_offset_index >= len(token_offsets):
668-
token_offset_index = token_offset_index - len(token_offsets)
669+
if replicas_remaining == 0 or num_replicas_this_dc == num_hosts_this_dc:
670+
break
671+
672+
if token_offset_index >= num_tokens:
673+
token_offset_index = token_offset_index - num_tokens
669674

670675
token_offset = token_offsets[token_offset_index]
671676
host = token_to_host_owner[ring[token_offset]]
672-
if replicas_remaining == 0 or replicas_this_dc == hosts_this_dc:
673-
break
674-
675677
if host in replicas:
676678
continue
677679

678-
if host.rack in racks_placed and len(racks_placed) < len(racks_this_dc):
680+
if host.rack in racks_placed and len(racks_placed) < num_racks_this_dc:
679681
skipped_hosts.append(host)
680682
continue
681683

682684
replicas.append(host)
683-
replicas_this_dc += 1
685+
num_replicas_this_dc += 1
684686
replicas_remaining -= 1
685687
racks_placed.add(host.rack)
686688

687-
if len(racks_placed) == len(racks_this_dc):
689+
if len(racks_placed) == num_racks_this_dc:
688690
for host in skipped_hosts:
689691
if replicas_remaining == 0:
690692
break

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /