Skip to content

Commit 1143477

Browse files
Don't check if host is in initial contact points when setting default local_dc
1 parent dbb1cdf commit 1143477

File tree

2 files changed

+6
-23
lines changed

2 files changed

+6
-23
lines changed

cassandra/policies.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ def __init__(self, local_dc='', used_hosts_per_remote_dc=0):
245245
self.used_hosts_per_remote_dc = used_hosts_per_remote_dc
246246
self._dc_live_hosts = {}
247247
self._position = 0
248-
self._endpoints = []
249248
LoadBalancingPolicy.__init__(self)
250249

251250
def _dc(self, host):
@@ -255,11 +254,6 @@ def populate(self, cluster, hosts):
255254
for dc, dc_hosts in groupby(hosts, lambda h: self._dc(h)):
256255
self._dc_live_hosts[dc] = tuple({*dc_hosts, *self._dc_live_hosts.get(dc, [])})
257256

258-
if not self.local_dc:
259-
self._endpoints = [
260-
endpoint
261-
for endpoint in cluster.endpoints_resolved]
262-
263257
self._position = randint(0, len(hosts) - 1) if hosts else 0
264258

265259
def distance(self, host):
@@ -301,13 +295,11 @@ def on_up(self, host):
301295
# not worrying about threads because this will happen during
302296
# control connection startup/refresh
303297
if not self.local_dc and host.datacenter:
304-
if host.endpoint in self._endpoints:
305-
self.local_dc = host.datacenter
306-
log.info("Using datacenter '%s' for DCAwareRoundRobinPolicy (via host '%s'); "
307-
"if incorrect, please specify a local_dc to the constructor, "
308-
"or limit contact points to local cluster nodes" %
309-
(self.local_dc, host.endpoint))
310-
del self._endpoints
298+
self.local_dc = host.datacenter
299+
log.info("Using datacenter '%s' for DCAwareRoundRobinPolicy (via host '%s'); "
300+
"if incorrect, please specify a local_dc to the constructor, "
301+
"or limit contact points to local cluster nodes" %
302+
(self.local_dc, host.endpoint))
311303

312304
dc = self._dc(host)
313305
with self._hosts_lock:

tests/unit/test_policies.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,6 @@ def test_default_dc(self):
556556
assert policy.local_dc != host_remote.datacenter
557557
assert policy.local_dc == host_local.datacenter
558558

559-
# contact DC second
560-
policy = DCAwareRoundRobinPolicy()
561-
policy.populate(cluster, [host_none])
562-
assert not policy.local_dc
563-
policy.on_add(host_remote)
564-
policy.on_add(host_local)
565-
assert policy.local_dc != host_remote.datacenter
566-
assert policy.local_dc == host_local.datacenter
567-
568559
# no DC
569560
policy = DCAwareRoundRobinPolicy()
570561
policy.populate(cluster, [host_none])
@@ -577,7 +568,7 @@ def test_default_dc(self):
577568
policy.populate(cluster, [host_none])
578569
assert not policy.local_dc
579570
policy.on_add(host_remote)
580-
assert not policy.local_dc
571+
assert policy.local_dc
581572

582573
class TokenAwarePolicyTest(unittest.TestCase):
583574

0 commit comments

Comments
 (0)