Skip to content

Commit 025342c

Browse files
authored
Merge pull request #1094 from Lorak-mmk/fix-unnamable-types
Fix unnamable types
2 parents 4314d7a + 8a3e60d commit 025342c

File tree

11 files changed

+67
-91
lines changed

11 files changed

+67
-91
lines changed

scylla-cql/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ full-serialization = [
5656
]
5757

5858
[lints.rust]
59+
unnameable_types = "warn"
5960
unreachable_pub = "warn"

scylla-macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ quote = "1.0"
1919
proc-macro2 = "1.0"
2020

2121
[lints.rust]
22+
unnameable_types = "warn"
2223
unreachable_pub = "warn"

scylla/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ name = "benchmark"
9696
harness = false
9797

9898
[lints.rust]
99+
unnameable_types = "warn"
99100
unreachable_pub = "warn"
100101
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(scylla_cloud_tests)'] }

scylla/src/cloud/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod config;
22

33
use std::net::SocketAddr;
44

5-
pub(crate) use config::CloudConfig;
5+
pub use config::CloudConfig;
66
pub use config::CloudConfigError;
77
use openssl::{
88
error::ErrorStack,

scylla/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,4 @@ pub use transport::load_balancing;
271271
pub use transport::retry_policy;
272272
pub use transport::speculative_execution;
273273

274-
pub use transport::metrics::Metrics;
274+
pub use transport::metrics::{Metrics, MetricsError};

scylla/src/transport/cluster.rs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tracing::{debug, warn};
2727
use uuid::Uuid;
2828

2929
use super::locator::tablets::{RawTablet, Tablet, TabletsInfo};
30-
use super::node::{KnownNode, NodeAddr};
30+
use super::node::{InternalKnownNode, NodeAddr};
3131
use super::NodeRef;
3232

3333
use super::locator::ReplicaLocator;
@@ -59,12 +59,6 @@ impl<'a> std::fmt::Debug for ClusterNeatDebug<'a> {
5959
}
6060
}
6161

62-
#[derive(Clone, Debug)]
63-
pub struct Datacenter {
64-
pub nodes: Vec<Arc<Node>>,
65-
pub rack_count: usize,
66-
}
67-
6862
#[derive(Clone)]
6963
pub struct ClusterData {
7064
pub(crate) known_peers: HashMap<Uuid, Arc<Node>>, // Invariant: nonempty after Cluster::new()
@@ -145,7 +139,7 @@ struct UseKeyspaceRequest {
145139

146140
impl Cluster {
147141
pub(crate) async fn new(
148-
known_nodes: Vec<KnownNode>,
142+
known_nodes: Vec<InternalKnownNode>,
149143
pool_config: PoolConfig,
150144
keyspaces_to_fetch: Vec<String>,
151145
fetch_schema_metadata: bool,
@@ -257,18 +251,6 @@ impl Cluster {
257251
}
258252

259253
impl ClusterData {
260-
// Updates information about rack count in each datacenter
261-
fn update_rack_count(datacenters: &mut HashMap<String, Datacenter>) {
262-
for datacenter in datacenters.values_mut() {
263-
datacenter.rack_count = datacenter
264-
.nodes
265-
.iter()
266-
.filter_map(|node| node.rack.as_ref())
267-
.unique()
268-
.count();
269-
}
270-
}
271-
272254
pub(crate) async fn wait_until_all_pools_are_initialized(&self) {
273255
for node in self.locator.unique_nodes_in_global_ring().iter() {
274256
node.wait_until_pool_initialized().await;
@@ -289,7 +271,6 @@ impl ClusterData {
289271
let mut new_known_peers: HashMap<Uuid, Arc<Node>> =
290272
HashMap::with_capacity(metadata.peers.len());
291273
let mut ring: Vec<(Token, Arc<Node>)> = Vec::new();
292-
let mut datacenters: HashMap<String, Datacenter> = HashMap::new();
293274

294275
for peer in metadata.peers {
295276
// Take existing Arc<Node> if possible, otherwise create new one
@@ -325,19 +306,6 @@ impl ClusterData {
325306

326307
new_known_peers.insert(peer_host_id, node.clone());
327308

328-
if let Some(dc) = &node.datacenter {
329-
match datacenters.get_mut(dc) {
330-
Some(v) => v.nodes.push(node.clone()),
331-
None => {
332-
let v = Datacenter {
333-
nodes: vec![node.clone()],
334-
rack_count: 0,
335-
};
336-
datacenters.insert(dc.clone(), v);
337-
}
338-
}
339-
}
340-
341309
for token in peer_tokens {
342310
ring.push((token, node.clone()));
343311
}
@@ -384,8 +352,6 @@ impl ClusterData {
384352
)
385353
}
386354

387-
Self::update_rack_count(&mut datacenters);
388-
389355
let keyspaces = metadata.keyspaces;
390356
let (locator, keyspaces) = tokio::task::spawn_blocking(move || {
391357
let keyspace_strategies = keyspaces.values().map(|ks| &ks.strategy);
@@ -409,25 +375,6 @@ impl ClusterData {
409375
&self.keyspaces
410376
}
411377

412-
/// Access datacenter details collected by the driver
413-
/// Returned `HashMap` is indexed by names of datacenters
414-
pub fn get_datacenters_info(&self) -> HashMap<String, Datacenter> {
415-
self.locator
416-
.datacenter_names()
417-
.iter()
418-
.map(|dc_name| {
419-
let nodes = self
420-
.locator
421-
.unique_nodes_in_datacenter_ring(dc_name)
422-
.unwrap()
423-
.to_vec();
424-
let rack_count = nodes.iter().map(|node| node.rack.as_ref()).unique().count();
425-
426-
(dc_name.clone(), Datacenter { nodes, rack_count })
427-
})
428-
.collect()
429-
}
430-
431378
/// Access details about nodes known to the driver
432379
pub fn get_nodes_info(&self) -> &[Arc<Node>] {
433380
self.locator.unique_nodes_in_global_ring()

scylla/src/transport/node.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,43 @@ impl Hash for Node {
227227
pub enum KnownNode {
228228
Hostname(String),
229229
Address(SocketAddr),
230+
}
231+
232+
/// Describes a database server known on `Session` startup.
233+
/// It is similar to [KnownNode] but includes also `CloudEndpoint` variant,
234+
/// which is created by the driver if session is configured to connect to
235+
/// serverless cluster.
236+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
237+
pub(crate) enum InternalKnownNode {
238+
Hostname(String),
239+
Address(SocketAddr),
230240
#[cfg(feature = "cloud")]
231241
CloudEndpoint(CloudEndpoint),
232242
}
233243

244+
impl From<KnownNode> for InternalKnownNode {
245+
fn from(value: KnownNode) -> Self {
246+
match value {
247+
KnownNode::Hostname(s) => InternalKnownNode::Hostname(s),
248+
KnownNode::Address(s) => InternalKnownNode::Address(s),
249+
}
250+
}
251+
}
252+
234253
/// Describes a database server in the serverless Scylla Cloud.
235254
#[cfg(feature = "cloud")]
236-
#[non_exhaustive]
237255
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
238-
pub struct CloudEndpoint {
239-
pub hostname: String,
240-
pub datacenter: String,
256+
pub(crate) struct CloudEndpoint {
257+
pub(crate) hostname: String,
258+
pub(crate) datacenter: String,
241259
}
242260

243261
/// Describes a database server known on Session startup, with already resolved address.
244262
#[derive(Debug, Clone)]
245-
#[non_exhaustive]
246-
pub struct ResolvedContactPoint {
247-
pub address: SocketAddr,
248-
pub datacenter: Option<String>,
263+
pub(crate) struct ResolvedContactPoint {
264+
pub(crate) address: SocketAddr,
265+
#[cfg_attr(not(feature = "cloud"), allow(unused))]
266+
pub(crate) datacenter: Option<String>,
249267
}
250268

251269
// Resolve the given hostname using a DNS lookup if necessary.
@@ -275,12 +293,12 @@ pub(crate) async fn resolve_hostname(hostname: &str) -> Result<SocketAddr, io::E
275293
})
276294
}
277295

278-
/// Transforms the given [`KnownNode`]s into [`ContactPoint`]s.
296+
/// Transforms the given [`InternalKnownNode`]s into [`ContactPoint`]s.
279297
///
280298
/// In case of a hostname, resolves it using a DNS lookup.
281299
/// In case of a plain IP address, parses it and uses straight.
282300
pub(crate) async fn resolve_contact_points(
283-
known_nodes: &[KnownNode],
301+
known_nodes: &[InternalKnownNode],
284302
) -> (Vec<ResolvedContactPoint>, Vec<String>) {
285303
// Find IP addresses of all known nodes passed in the config
286304
let mut initial_peers: Vec<ResolvedContactPoint> = Vec::with_capacity(known_nodes.len());
@@ -290,16 +308,16 @@ pub(crate) async fn resolve_contact_points(
290308

291309
for node in known_nodes.iter() {
292310
match node {
293-
KnownNode::Hostname(hostname) => {
311+
InternalKnownNode::Hostname(hostname) => {
294312
to_resolve.push((hostname, None));
295313
hostnames.push(hostname.clone());
296314
}
297-
KnownNode::Address(address) => initial_peers.push(ResolvedContactPoint {
315+
InternalKnownNode::Address(address) => initial_peers.push(ResolvedContactPoint {
298316
address: *address,
299317
datacenter: None,
300318
}),
301319
#[cfg(feature = "cloud")]
302-
KnownNode::CloudEndpoint(CloudEndpoint {
320+
InternalKnownNode::CloudEndpoint(CloudEndpoint {
303321
hostname,
304322
datacenter,
305323
}) => to_resolve.push((hostname, Some(datacenter.clone()))),

scylla/src/transport/session.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use super::errors::TracingProtocolError;
4444
use super::execution_profile::{ExecutionProfile, ExecutionProfileHandle, ExecutionProfileInner};
4545
#[cfg(feature = "cloud")]
4646
use super::node::CloudEndpoint;
47-
use super::node::KnownNode;
47+
use super::node::{InternalKnownNode, KnownNode};
4848
use super::partitioner::PartitionerName;
4949
use super::query_result::MaybeFirstRowTypedError;
5050
use super::topology::UntranslatedPeer;
@@ -489,23 +489,28 @@ impl Session {
489489
let known_nodes = config.known_nodes;
490490

491491
#[cfg(feature = "cloud")]
492-
let known_nodes = if let Some(cloud_servers) =
493-
config.cloud_config.as_ref().map(|cloud_config| {
494-
cloud_config
492+
let cloud_known_nodes: Option<Vec<InternalKnownNode>> =
493+
if let Some(ref cloud_config) = config.cloud_config {
494+
let cloud_servers = cloud_config
495495
.get_datacenters()
496496
.iter()
497497
.map(|(dc_name, dc_data)| {
498-
KnownNode::CloudEndpoint(CloudEndpoint {
498+
InternalKnownNode::CloudEndpoint(CloudEndpoint {
499499
hostname: dc_data.get_server().to_owned(),
500500
datacenter: dc_name.clone(),
501501
})
502502
})
503-
.collect()
504-
}) {
505-
cloud_servers
506-
} else {
507-
known_nodes
508-
};
503+
.collect();
504+
Some(cloud_servers)
505+
} else {
506+
None
507+
};
508+
509+
#[cfg(not(feature = "cloud"))]
510+
let cloud_known_nodes: Option<Vec<InternalKnownNode>> = None;
511+
512+
let known_nodes = cloud_known_nodes
513+
.unwrap_or_else(|| known_nodes.into_iter().map(|node| node.into()).collect());
509514

510515
// Ensure there is at least one known node
511516
if known_nodes.is_empty() {

scylla/src/transport/session_builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ use openssl::ssl::SslContext;
2929
use tracing::warn;
3030

3131
mod sealed {
32+
// This is a sealed trait - its whole purpose is to be unnameable.
33+
// This means we need to disable the check.
34+
#[allow(unknown_lints)] // Rust 1.66 doesn't know this lint
35+
#[allow(unnameable_types)]
3236
pub trait Sealed {}
3337
}
3438
pub trait SessionBuilderKind: sealed::Sealed + Clone {}

scylla/src/transport/session_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::routing::Token;
88
use crate::statement::Consistency;
99
use crate::test_utils::{scylla_supports_tablets, setup_tracing};
1010
use crate::tracing::TracingInfo;
11-
use crate::transport::cluster::Datacenter;
1211
use crate::transport::errors::{BadKeyspaceName, BadQuery, DbError, QueryError};
1312
use crate::transport::partitioner::{
1413
calculate_token_for_partition_key, Murmur3Partitioner, Partitioner, PartitionerName,
@@ -1874,10 +1873,11 @@ async fn test_turning_off_schema_fetching() {
18741873
let cluster_data = &session.get_cluster_data();
18751874
let keyspace = &cluster_data.get_keyspace_info()[&ks];
18761875

1877-
let datacenters: HashMap<String, Datacenter> = cluster_data.get_datacenters_info();
1878-
let datacenter_repfactors: HashMap<String, usize> = datacenters
1879-
.into_keys()
1880-
.map(|dc_name| (dc_name, 1))
1876+
let datacenter_repfactors: HashMap<String, usize> = cluster_data
1877+
.replica_locator()
1878+
.datacenter_names()
1879+
.iter()
1880+
.map(|dc_name| (dc_name.to_owned(), 1))
18811881
.collect();
18821882

18831883
assert_eq!(

0 commit comments

Comments
 (0)