scylla/routing/
mod.rs

1//! This module holds entities whose goal is to enable routing requests optimally,
2//! that is, choosing a target node and a shard such that it is a replica for
3//! given token.
4//!
5//! This includes:
6//! - token representation,
7//! - shard representation and shard computing logic,
8//! - partitioners, which compute token based on a partition key,
9//! - replica locator, which finds replicas (node + shard) for a given token.
10//!
11
12pub mod locator;
13pub mod partitioner;
14mod sharding;
15
16pub use sharding::{InvalidShardAwarePortRange, Shard, ShardAwarePortRange, ShardCount, Sharder};
17pub(crate) use sharding::{ShardInfo, ShardingError};
18
19#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
20
21/// Token is a result of computing a hash of a primary key
22///
23/// It is basically an i64 with one caveat: i64::MIN is not
24/// a valid token. It is used to represent infinity.
25/// For this reason tokens are normalized - i64::MIN
26/// is replaced with i64::MAX. See this fragment of
27/// Scylla code for more information:
28/// <https://github.com/scylladb/scylladb/blob/4be70bfc2bc7f133cab492b4aac7bab9c790a48c/dht/token.hh#L32>
29///
30/// This struct is a wrapper over i64 that performs this normalization
31/// when initialized using `new()` method.
32pub struct Token {
33    value: i64,
34}
35
36impl Token {
37    /// Creates a new token with given value, normalizing the value if necessary
38    #[inline]
39    pub fn new(value: i64) -> Self {
40        Self {
41            value: if value == i64::MIN { i64::MAX } else { value },
42        }
43    }
44
45    /// Invalid Token - contains i64::MIN as value.
46    ///
47    /// This is (currently) only required by CDCPartitioner.
48    /// See the following comment:
49    /// https://github.com/scylladb/scylla-rust-driver/blob/049dc3546d24e45106fed0fdb985ec2511ab5192/scylla/src/transport/partitioner.rs#L312-L322
50    pub(crate) const INVALID: Self = Token { value: i64::MIN };
51
52    #[inline]
53    pub fn value(&self) -> i64 {
54        self.value
55    }
56}