forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.rs
317 lines (282 loc) · 12.3 KB
/
state.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::errors::{BadQuery, ConnectionPoolError};
use crate::network::{Connection, PoolConfig, VerifiedKeyspaceName};
use crate::policies::host_filter::HostFilter;
use crate::prepared_statement::TokenCalculationError;
use crate::routing::locator::tablets::{RawTablet, Tablet, TabletsInfo};
use crate::routing::locator::ReplicaLocator;
use crate::routing::partitioner::{calculate_token_for_partition_key, PartitionerName};
use crate::routing::{Shard, Token};
use itertools::Itertools;
use scylla_cql::frame::response::result::TableSpec;
use scylla_cql::serialize::row::SerializedValues;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tracing::{debug, warn};
use uuid::Uuid;
use super::metadata::{Keyspace, Metadata, Strategy};
use super::node::{Node, NodeRef};
#[derive(Clone)]
pub struct ClusterState {
pub(crate) known_peers: HashMap<Uuid, Arc<Node>>, // Invariant: nonempty after Cluster::new()
pub(crate) keyspaces: HashMap<String, Keyspace>,
pub(crate) locator: ReplicaLocator,
}
/// Enables printing [ClusterState] struct in a neat way, skipping the clutter involved by
/// [ClusterState::ring] being large and [Self::keyspaces] debug print being very verbose by default.
pub(crate) struct ClusterStateNeatDebug<'a>(pub(crate) &'a Arc<ClusterState>);
impl std::fmt::Debug for ClusterStateNeatDebug<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let cluster_data = &self.0;
f.debug_struct("ClusterState")
.field("known_peers", &cluster_data.known_peers)
.field("ring", {
struct RingSizePrinter(usize);
impl std::fmt::Debug for RingSizePrinter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<size={}>", self.0)
}
}
&RingSizePrinter(cluster_data.locator.ring().len())
})
.field("keyspaces", &cluster_data.keyspaces.keys())
.finish_non_exhaustive()
}
}
impl ClusterState {
pub(crate) async fn wait_until_all_pools_are_initialized(&self) {
for node in self.locator.unique_nodes_in_global_ring().iter() {
node.wait_until_pool_initialized().await;
}
}
/// Creates new ClusterState using information about topology held in `metadata`.
/// Uses provided `known_peers` hashmap to recycle nodes if possible.
pub(crate) async fn new(
metadata: Metadata,
pool_config: &PoolConfig,
known_peers: &HashMap<Uuid, Arc<Node>>,
used_keyspace: &Option<VerifiedKeyspaceName>,
host_filter: Option<&dyn HostFilter>,
mut tablets: TabletsInfo,
old_keyspaces: &HashMap<String, Keyspace>,
) -> Self {
// Create new updated known_peers and ring
let mut new_known_peers: HashMap<Uuid, Arc<Node>> =
HashMap::with_capacity(metadata.peers.len());
let mut ring: Vec<(Token, Arc<Node>)> = Vec::new();
for peer in metadata.peers {
// Take existing Arc<Node> if possible, otherwise create new one
// Changing rack/datacenter but not ip address seems improbable
// so we can just create new node and connections then
let peer_host_id = peer.host_id;
let peer_address = peer.address;
let peer_tokens;
let node: Arc<Node> = match known_peers.get(&peer_host_id) {
Some(node) if node.datacenter == peer.datacenter && node.rack == peer.rack => {
let (peer_endpoint, tokens) = peer.into_peer_endpoint_and_tokens();
peer_tokens = tokens;
if node.address == peer_address {
node.clone()
} else {
// If IP changes, the Node struct is recreated, but the underlying pool is preserved and notified about the IP change.
Arc::new(Node::inherit_with_ip_changed(node, peer_endpoint))
}
}
_ => {
let is_enabled = host_filter.map_or(true, |f| f.accept(&peer));
let (peer_endpoint, tokens) = peer.into_peer_endpoint_and_tokens();
peer_tokens = tokens;
Arc::new(Node::new(
peer_endpoint,
pool_config.clone(),
used_keyspace.clone(),
is_enabled,
))
}
};
new_known_peers.insert(peer_host_id, node.clone());
for token in peer_tokens {
ring.push((token, node.clone()));
}
}
let keyspaces: HashMap<String, Keyspace> = metadata
.keyspaces
.into_iter()
.filter_map(|(ks_name, ks)| match ks {
Ok(ks) => Some((ks_name, ks)),
Err(e) => {
if let Some(old_ks) = old_keyspaces.get(&ks_name) {
warn!("Encountered an error while processing metadata of keyspace \"{ks_name}\": {e}.\
Re-using older version of this keyspace metadata");
Some((ks_name, old_ks.clone()))
} else {
warn!("Encountered an error while processing metadata of keyspace \"{ks_name}\": {e}.\
No previous version of this keyspace metadata found, so it will not be\
present in ClusterData until next refresh.");
None
}
}
})
.collect();
{
let removed_nodes = {
let mut removed_nodes = HashSet::new();
for old_peer in known_peers {
if !new_known_peers.contains_key(old_peer.0) {
removed_nodes.insert(*old_peer.0);
}
}
removed_nodes
};
let table_predicate = |spec: &TableSpec| {
if let Some(ks) = keyspaces.get(spec.ks_name()) {
ks.tables.contains_key(spec.table_name())
} else {
false
}
};
let recreated_nodes = {
let mut recreated_nodes = HashMap::new();
for (old_peer_id, old_peer_node) in known_peers {
if let Some(new_peer_node) = new_known_peers.get(old_peer_id) {
if !Arc::ptr_eq(old_peer_node, new_peer_node) {
recreated_nodes.insert(*old_peer_id, Arc::clone(new_peer_node));
}
}
}
recreated_nodes
};
tablets.perform_maintenance(
&table_predicate,
&removed_nodes,
&new_known_peers,
&recreated_nodes,
)
}
let (locator, keyspaces) = tokio::task::spawn_blocking(move || {
let keyspace_strategies = keyspaces.values().map(|ks| &ks.strategy);
let locator = ReplicaLocator::new(ring.into_iter(), keyspace_strategies, tablets);
(locator, keyspaces)
})
.await
.unwrap();
ClusterState {
known_peers: new_known_peers,
keyspaces,
locator,
}
}
/// Access keyspaces details collected by the driver
/// Driver collects various schema details like tables, partitioners, columns, types.
/// They can be read using this method
pub fn get_keyspace_info(&self) -> &HashMap<String, Keyspace> {
&self.keyspaces
}
/// Access details about nodes known to the driver
pub fn get_nodes_info(&self) -> &[Arc<Node>] {
self.locator.unique_nodes_in_global_ring()
}
/// Compute token of a table partition key
pub fn compute_token(
&self,
keyspace: &str,
table: &str,
partition_key: &SerializedValues,
) -> Result<Token, BadQuery> {
let partitioner = self
.keyspaces
.get(keyspace)
.and_then(|k| k.tables.get(table))
.and_then(|t| t.partitioner.as_deref())
.and_then(PartitionerName::from_str)
.unwrap_or_default();
calculate_token_for_partition_key(partition_key, &partitioner).map_err(|err| match err {
TokenCalculationError::ValueTooLong(values_len) => {
BadQuery::ValuesTooLongForKey(values_len, u16::MAX.into())
}
})
}
/// Access to replicas owning a given token
pub fn get_token_endpoints(
&self,
keyspace: &str,
table: &str,
token: Token,
) -> Vec<(Arc<Node>, Shard)> {
let table_spec = TableSpec::borrowed(keyspace, table);
self.get_token_endpoints_iter(&table_spec, token)
.map(|(node, shard)| (node.clone(), shard))
.collect()
}
pub(crate) fn get_token_endpoints_iter(
&self,
table_spec: &TableSpec,
token: Token,
) -> impl Iterator<Item = (NodeRef<'_>, Shard)> + Clone {
let keyspace = self.keyspaces.get(table_spec.ks_name());
let strategy = keyspace
.map(|k| &k.strategy)
.unwrap_or(&Strategy::LocalStrategy);
let replica_set = self
.replica_locator()
.replicas_for_token(token, strategy, None, table_spec);
replica_set.into_iter()
}
/// Access to replicas owning a given partition key (similar to `nodetool getendpoints`)
pub fn get_endpoints(
&self,
keyspace: &str,
table: &str,
partition_key: &SerializedValues,
) -> Result<Vec<(Arc<Node>, Shard)>, BadQuery> {
let token = self.compute_token(keyspace, table, partition_key)?;
Ok(self.get_token_endpoints(keyspace, table, token))
}
/// Access replica location info
pub fn replica_locator(&self) -> &ReplicaLocator {
&self.locator
}
/// Returns nonempty iterator of working connections to all shards.
pub(crate) fn iter_working_connections(
&self,
) -> Result<impl Iterator<Item = Arc<Connection>> + '_, ConnectionPoolError> {
// The returned iterator is nonempty by nonemptiness invariant of `self.known_peers`.
assert!(!self.known_peers.is_empty());
let mut peers_iter = self.known_peers.values();
// First we try to find the first working pool of connections.
// If none is found, return error.
let first_working_pool = peers_iter
.by_ref()
.map(|node| node.get_working_connections())
.find_or_first(Result::is_ok)
.expect("impossible: known_peers was asserted to be nonempty")?;
let remaining_pools_iter = peers_iter
.map(|node| node.get_working_connections())
.flatten_ok()
.flatten();
Ok(first_working_pool.into_iter().chain(remaining_pools_iter))
// By an invariant `self.known_peers` is nonempty, so the returned iterator
// is nonempty, too.
}
pub(super) fn update_tablets(&mut self, raw_tablets: Vec<(TableSpec<'static>, RawTablet)>) {
let replica_translator = |uuid: Uuid| self.known_peers.get(&uuid).cloned();
for (table, raw_tablet) in raw_tablets.into_iter() {
// Should we skip tablets that belong to a keyspace not present in
// self.keyspaces? The keyspace could have been, without driver's knowledge:
// 1. Dropped - in which case we'll remove its info soon (when refreshing
// topology) anyway.
// 2. Created - no harm in storing the info now.
//
// So I think we can safely skip checking keyspace presence.
let tablet = match Tablet::from_raw_tablet(raw_tablet, replica_translator) {
Ok(t) => t,
Err((t, f)) => {
debug!("Nodes ({}) that are replicas for a tablet {{ks: {}, table: {}, range: [{}. {}]}} not present in current ClusterState.known_peers. \
Skipping these replicas until topology refresh",
f.iter().format(", "), table.ks_name(), table.table_name(), t.range().0.value(), t.range().1.value());
t
}
};
self.locator.tablets.add_tablet(table, tablet);
}
}
}