Skip to content

Commit cd57c4d

Browse files
committed
(minor improvement): small refactoring in 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 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. All those are probably very very minor improvements, perhaps in a large cluster it'll be noticable. Signed-off-by: Yaniv Kaul <[email protected]>
1 parent 472b679 commit cd57c4d

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

cassandra/metadata.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -657,34 +657,34 @@ def make_token_replica_map(self, token_to_host_owner, ring):
657657
dc_to_current_index[dc] = index
658658

659659
replicas_remaining = dc_rf_map[dc]
660-
replicas_this_dc = 0
660+
num_replicas_this_dc = 0
661661
skipped_hosts = []
662662
racks_placed = set()
663-
racks_this_dc = dc_racks[dc]
664-
hosts_this_dc = len(hosts_per_dc[dc])
663+
num_racks_this_dc = len(dc_racks[dc])
664+
num_hosts_this_dc = len(hosts_per_dc[dc])
665665

666666
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)
667+
if token_offset_index >= num_tokens:
668+
token_offset_index = token_offset_index - num_tokens
669669

670-
token_offset = token_offsets[token_offset_index]
671-
host = token_to_host_owner[ring[token_offset]]
672-
if replicas_remaining == 0 or replicas_this_dc == hosts_this_dc:
670+
if replicas_remaining == 0 or num_replicas_this_dc == num_hosts_this_dc:
673671
break
674672

673+
token_offset = token_offsets[token_offset_index]
674+
host = token_to_host_owner[ring[token_offset]]
675675
if host in replicas:
676676
continue
677677

678-
if host.rack in racks_placed and len(racks_placed) < len(racks_this_dc):
678+
if host.rack in racks_placed and len(racks_placed) < num_racks_this_dc:
679679
skipped_hosts.append(host)
680680
continue
681681

682682
replicas.append(host)
683-
replicas_this_dc += 1
683+
num_replicas_this_dc += 1
684684
replicas_remaining -= 1
685685
racks_placed.add(host.rack)
686686

687-
if len(racks_placed) == len(racks_this_dc):
687+
if len(racks_placed) == num_racks_this_dc:
688688
for host in skipped_hosts:
689689
if replicas_remaining == 0:
690690
break

0 commit comments

Comments
 (0)