Skip to content

Commit 3cc726f

Browse files
committed
Make ShardAwarePortGenerator to iterate less
Implementation is not optimal, it iterates over ports and filters ones that don't match `port % total_shards == shard_id` Let's make it better by calculate right start and iterating only over correct ports.
1 parent facc90a commit 3cc726f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cassandra/connection.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,21 @@ def __init__(self, start_port: int, end_port: int):
673673
self.start_port = start_port
674674
self.end_port = end_port
675675

676+
@staticmethod
677+
def _align(value: int, total_shards: int):
678+
shift = value % total_shards
679+
if shift == 0:
680+
return value
681+
return value + total_shards - shift
682+
676683
def generate(self, shard_id: int, total_shards: int) -> Generator[int]:
677-
start = random.randrange(self.start_port, self.end_port)
678-
available_ports = itertools.chain(range(start, self.end_port), range(self.start_port, start))
684+
start = self._align(random.randrange(self.start_port, self.end_port), total_shards) + shard_id
685+
begging = self._align(self.start_port, total_shards) + shard_id
686+
available_ports = itertools.chain(range(start, self.end_port, total_shards),
687+
range(begging, start, total_shards))
688+
679689
for port in available_ports:
680-
if port % total_shards == shard_id:
681-
yield port
690+
yield port
682691

683692

684693
DefaultShardawarePortGenerator = ShardAwarePortGenerator(DEFAULT_LOCAL_PORT_LOW, DEFAULT_LOCAL_PORT_HIGH)

0 commit comments

Comments
 (0)