Skip to content

Commit defd9eb

Browse files
authored
Merge pull request datastax#944 from damien-instaclustr/token-fix
Token range ownership
2 parents 94df983 + 7d5a624 commit defd9eb

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
3.15.0
2+
======
3+
Bug Fixes
4+
---------
5+
* Tokenmap.get_replicas returns the wrong value if token coincides with the end of the range (PYTHON-978)
6+
17
3.14.0
28
======
9+
April 17, 2018
310

411
Features
512
--------

cassandra/metadata.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from binascii import unhexlify
16-
from bisect import bisect_right
16+
from bisect import bisect_left
1717
from collections import defaultdict, Mapping
1818
from functools import total_ordering
1919
from hashlib import md5
@@ -1486,10 +1486,9 @@ def get_replicas(self, keyspace, token):
14861486
tokens_to_hosts = self.tokens_to_hosts_by_ks.get(keyspace, None)
14871487

14881488
if tokens_to_hosts:
1489-
# token range ownership is exclusive on the LHS (the start token), so
1490-
# we use bisect_right, which, in the case of a tie/exact match,
1491-
# picks an insertion point to the right of the existing match
1492-
point = bisect_right(self.ring, token)
1489+
# The values in self.ring correspond to the end of the
1490+
# token range up to and including the value listed.
1491+
point = bisect_left(self.ring, token)
14931492
if point == len(self.ring):
14941493
return tokens_to_hosts[self.ring[0]]
14951494
else:

tests/integration/standard/test_metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,8 @@ def test_token_map(self):
11721172

11731173
for i, token in enumerate(ring):
11741174
self.assertEqual(set(get_replicas('test3rf', token)), set(owners))
1175-
self.assertEqual(set(get_replicas('test2rf', token)), set([owners[(i + 1) % 3], owners[(i + 2) % 3]]))
1176-
self.assertEqual(set(get_replicas('test1rf', token)), set([owners[(i + 1) % 3]]))
1175+
self.assertEqual(set(get_replicas('test2rf', token)), set([owners[i], owners[(i + 1) % 3]]))
1176+
self.assertEqual(set(get_replicas('test1rf', token)), set([owners[i]]))
11771177
cluster.shutdown()
11781178

11791179

tests/unit/test_metadata.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ def _get_replicas(self, token_klass):
312312
token_map = TokenMap(token_klass, token_to_primary_replica, tokens, metadata)
313313

314314
# tokens match node tokens exactly
315-
for i, token in enumerate(tokens):
316-
expected_host = hosts[(i + 1) % len(hosts)]
315+
for token, expected_host in zip(tokens, hosts):
317316
replicas = token_map.get_replicas("ks", token)
318317
self.assertEqual(set(replicas), {expected_host})
319318

0 commit comments

Comments
 (0)