Skip to content

Commit f29f06d

Browse files
committed
Docs for tokens and token maps
1 parent 5d95a6b commit f29f06d

2 files changed

Lines changed: 57 additions & 14 deletions

File tree

cassandra/metadata.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ def rebuild_token_map(self, partitioner, token_map):
243243
For internal use only.
244244
"""
245245
if partitioner.endswith('RandomPartitioner'):
246-
token_cls = MD5Token
246+
token_class = MD5Token
247247
elif partitioner.endswith('Murmur3Partitioner'):
248-
token_cls = Murmur3Token
248+
token_class = Murmur3Token
249249
elif partitioner.endswith('ByteOrderedPartitioner'):
250-
token_cls = BytesToken
250+
token_class = BytesToken
251251
else:
252252
self.token_map = None
253253
return
@@ -256,20 +256,20 @@ def rebuild_token_map(self, partitioner, token_map):
256256
ring = []
257257
for host, token_strings in token_map.iteritems():
258258
for token_string in token_strings:
259-
token = token_cls(token_string)
259+
token = token_class(token_string)
260260
ring.append(token)
261261
tokens_to_hosts[token].add(host)
262262

263263
ring = sorted(ring)
264-
self.token_map = TokenMap(token_cls, tokens_to_hosts, ring)
264+
self.token_map = TokenMap(token_class, tokens_to_hosts, ring)
265265

266266
def get_replicas(self, key):
267267
"""
268268
Returns a list of :class:`.Host` instances that are replicas for a given
269269
partition key.
270270
"""
271271
t = self.token_map
272-
return t.get_replicas(t.token_cls.from_key(key))
272+
return t.get_replicas(t.token_class.from_key(key))
273273

274274
def add_host(self, address):
275275
cluster = self.cluster_ref()
@@ -607,13 +607,35 @@ def as_cql_query(self):
607607

608608

609609
class TokenMap(object):
610+
"""
611+
Information about the layout of the ring.
612+
"""
613+
614+
token_class = None
615+
"""
616+
A subclass of :class:`.Token`, depending on what partitioner the cluster uses.
617+
"""
618+
619+
tokens_to_hosts = None
620+
"""
621+
A map of :class:`.Token` objects to :class:`.Host` objects.
622+
"""
623+
624+
ring = None
625+
"""
626+
An ordered list of :class:`.Token` instances in the ring.
627+
"""
610628

611-
def __init__(self, token_cls, tokens_to_hosts, ring):
612-
self.token_cls = token_cls
629+
def __init__(self, token_class, tokens_to_hosts, ring):
630+
self.token_class = token_class
613631
self.tokens_to_hosts = tokens_to_hosts
614632
self.ring = ring
615633

616634
def get_replicas(self, token):
635+
"""
636+
Get :class:`.Host` instances representing all of the replica nodes
637+
for a given :class:`.Token`.
638+
"""
617639
point = bisect_left(self.ring, token)
618640
if point == 0 and token != self.ring[0]:
619641
return self.tokens_to_hosts[self.ring[-1]]
@@ -624,6 +646,9 @@ def get_replicas(self, token):
624646

625647

626648
class Token(object):
649+
"""
650+
Abstract class representing a token.
651+
"""
627652

628653
@classmethod
629654
def hash_fn(cls, key):
@@ -645,27 +670,39 @@ def __cmp__(self, other):
645670
MAX_LONG = (2 ** 63) - 1
646671

647672
class Murmur3Token(Token):
673+
"""
674+
A token for ``Murmur3Partitioner``.
675+
"""
648676

649677
@classmethod
650678
def hash_fn(cls, key):
651679
h = murmur3(key)
652680
return h if h != MIN_LONG else MAX_LONG
653681

654-
def __init__(self, token_string):
655-
self.value = int(token_string)
682+
def __init__(self, token):
683+
""" `token` should be an int or string representing the token """
684+
self.value = int(token)
656685

657686

658687
class MD5Token(Token):
688+
"""
689+
A token for ``RandomPartitioner``.
690+
"""
659691

660692
@classmethod
661693
def hash_fn(cls, key):
662694
return abs(varint_unpack(md5('foo').digest()))
663695

664-
def __init__(self, token_string):
665-
self.value = int(token_string)
696+
def __init__(self, token):
697+
""" `token` should be an int or string representing the token """
698+
self.value = int(token)
666699

667700

668701
class BytesToken(Token):
702+
"""
703+
A token for ``ByteOrderedPartitioner``.
704+
"""
669705

670706
def __init__(self, token_string):
707+
""" `token_string` should be string representing the token """
671708
self.value = token_string

docs/api/cassandra/metadata.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
:members:
88
:exclude-members: rebuild_schema, rebuild_token_map, add_host, remove_host, get_host
99

10+
Schemas
11+
-------
12+
1013
.. autoclass:: KeyspaceMetadata ()
1114
:members:
1215

@@ -19,10 +22,13 @@
1922
.. autoclass:: IndexMetadata ()
2023
:members:
2124

22-
.. autoclass:: TokenMap
25+
Tokens and Ring Topology
26+
------------------------
27+
28+
.. autoclass:: TokenMap ()
2329
:members:
2430

25-
.. autoclass:: Token
31+
.. autoclass:: Token ()
2632
:members:
2733

2834
.. autoclass:: Murmur3Token

0 commit comments

Comments
 (0)