Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cassandra/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import random

from collections import namedtuple
from functools import lru_cache
from itertools import islice, cycle, groupby, repeat
import logging
from random import randint, shuffle
Expand Down Expand Up @@ -254,7 +253,7 @@ def _dc(self, host):

def populate(self, cluster, hosts):
for dc, dc_hosts in groupby(hosts, lambda h: self._dc(h)):
self._dc_live_hosts[dc] = tuple(set(dc_hosts))
self._dc_live_hosts[dc] = tuple({*dc_hosts, *self._dc_live_hosts.get(dc, [])})

Comment on lines 255 to 257

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think such comprehensions / unpacking (or w/e is this called) is much less readable than something simpler like

for dc, dc_hosts in groupby(hosts, lambda h: self._dc(h)):
    self._dc_live_hosts.setdefault(dc, set())
    self._dc_live_hosts[dc].update(dc_hosts)

I'd prefer it to be changed, but I won't hold this PR over that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw if we are staying with the current version, let's change .get(dc, []) to .get(dc,set()).
It won't change how the code works but makes it imo easier to reason about - _dc_live_hosts is a dict str -> set of hosts, so let's keep it that way.

if not self.local_dc:
self._endpoints = [
Expand Down Expand Up @@ -374,9 +373,9 @@ def _dc(self, host):

def populate(self, cluster, hosts):
for (dc, rack), rack_hosts in groupby(hosts, lambda host: (self._dc(host), self._rack(host))):
self._live_hosts[(dc, rack)] = tuple(set(rack_hosts))
self._live_hosts[(dc, rack)] = tuple({*rack_hosts, *self._live_hosts.get((dc, rack), [])})
for dc, dc_hosts in groupby(hosts, lambda host: self._dc(host)):
self._dc_live_hosts[dc] = tuple(set(dc_hosts))
self._dc_live_hosts[dc] = tuple({*dc_hosts, *self._dc_live_hosts.get(dc, [])})

self._position = randint(0, len(hosts) - 1) if hosts else 0

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random
import unittest

from itertools import islice, cycle
Expand Down Expand Up @@ -199,6 +199,8 @@ def test_no_remote(self, policy_specialization, constructor_args):
h.set_location_info("dc1", "rack1")
hosts.append(h)

random.shuffle(hosts)

policy = policy_specialization(*constructor_args)
policy.populate(None, hosts)
qplan = list(policy.make_query_plan())
Expand All @@ -213,6 +215,8 @@ def test_with_remotes(self, policy_specialization, constructor_args):
for h in hosts[4:]:
h.set_location_info("dc2", "rack1")

random.shuffle(hosts)

local_rack_hosts = set(h for h in hosts if h.datacenter == "dc1" and h.rack == "rack1")
local_hosts = set(h for h in hosts if h.datacenter == "dc1" and h.rack != "rack1")
remote_hosts = set(h for h in hosts if h.datacenter != "dc1")
Expand Down
Loading