Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3c63c0

Browse files
committed
policies: fix dc aware and rack aware policies initialization
In cases when nodes are listed not in a proper groups of dc (for DCAwareRoundRobinPolicy) and dc+rack (for RackAwareRoundRobinPolicy). Policy register only nodes from the last group or dc+rack. So, policy knows only last group of nodes, rest of the nodes considered to be non-existent, until node is restarted, which can be days.
1 parent c431bb5 commit b3c63c0

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

cassandra/policies.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import random
1515

1616
from collections import namedtuple
17-
from functools import lru_cache
1817
from itertools import islice, cycle, groupby, repeat
1918
import logging
2019
from random import randint, shuffle
@@ -254,7 +253,7 @@ def _dc(self, host):
254253

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

259258
if not self.local_dc:
260259
self._endpoints = [
@@ -374,9 +373,9 @@ def _dc(self, host):
374373

375374
def populate(self, cluster, hosts):
376375
for (dc, rack), rack_hosts in groupby(hosts, lambda host: (self._dc(host), self._rack(host))):
377-
self._live_hosts[(dc, rack)] = tuple(set(rack_hosts))
376+
self._live_hosts[(dc, rack)] = tuple({*rack_hosts, *self._live_hosts.get((dc, rack), [])})
378377
for dc, dc_hosts in groupby(hosts, lambda host: self._dc(host)):
379-
self._dc_live_hosts[dc] = tuple(set(dc_hosts))
378+
self._dc_live_hosts[dc] = tuple({*dc_hosts, *self._dc_live_hosts.get(dc, [])})
380379

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

tests/unit/test_policies.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import random
1515
import unittest
1616

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

202+
random.shuffle(hosts)
203+
202204
policy = policy_specialization(*constructor_args)
203205
policy.populate(None, hosts)
204206
qplan = list(policy.make_query_plan())
@@ -213,6 +215,8 @@ def test_with_remotes(self, policy_specialization, constructor_args):
213215
for h in hosts[4:]:
214216
h.set_location_info("dc2", "rack1")
215217

218+
random.shuffle(hosts)
219+
216220
local_rack_hosts = set(h for h in hosts if h.datacenter == "dc1" and h.rack == "rack1")
217221
local_hosts = set(h for h in hosts if h.datacenter == "dc1" and h.rack != "rack1")
218222
remote_hosts = set(h for h in hosts if h.datacenter != "dc1")

0 commit comments

Comments
 (0)