Skip to content

Commit e484adc

Browse files
authored
Merge pull request #511 from wprzytula/debuggable-session
Made Session debuggable
2 parents ba0463d + 9a4fbee commit e484adc

File tree

10 files changed

+78
-3
lines changed

10 files changed

+78
-3
lines changed

examples/custom_load_balancing_policy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use scylla::{
77
use std::{env, sync::Arc};
88

99
/// Example load balancing policy that prefers nodes from favorite datacenter
10+
#[derive(Debug)]
1011
struct CustomLoadBalancingPolicy {
1112
fav_datacenter_name: String,
1213
}

scylla/src/transport/caching_session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bytes::Bytes;
88
use dashmap::DashMap;
99

1010
/// Provides auto caching while executing queries
11+
#[derive(Debug)]
1112
pub struct CachingSession {
1213
pub session: Session,
1314
/// The prepared statement cache size

scylla/src/transport/cluster.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ pub struct Cluster {
3030
_worker_handle: RemoteHandle<()>,
3131
}
3232

33-
#[derive(Clone)]
33+
/// Enables printing [Cluster] struct in a neat way, by skipping the rather useless
34+
/// print of channels state and printing [ClusterData] neatly.
35+
pub struct ClusterNeatDebug<'a>(pub &'a Cluster);
36+
impl<'a> std::fmt::Debug for ClusterNeatDebug<'a> {
37+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38+
let cluster = self.0;
39+
f.debug_struct("Cluster")
40+
.field("data", &ClusterDataNeatDebug(&cluster.data.load()))
41+
.finish_non_exhaustive()
42+
}
43+
}
44+
45+
#[derive(Clone, Debug)]
3446
pub struct Datacenter {
3547
pub nodes: Vec<Arc<Node>>,
3648
pub rack_count: usize,
@@ -45,6 +57,31 @@ pub struct ClusterData {
4557
pub(crate) datacenters: HashMap<String, Datacenter>,
4658
}
4759

60+
/// Enables printing [ClusterData] struct in a neat way, skipping the clutter involved by
61+
/// [ClusterData::ring] being large and [Self::keyspaces] debug print being very verbose by default.
62+
pub struct ClusterDataNeatDebug<'a>(pub &'a Arc<ClusterData>);
63+
impl<'a> std::fmt::Debug for ClusterDataNeatDebug<'a> {
64+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65+
let cluster_data = &self.0;
66+
67+
f.debug_struct("ClusterData")
68+
.field("known_peers", &cluster_data.known_peers)
69+
.field("ring", {
70+
struct RingSizePrinter(usize);
71+
impl std::fmt::Debug for RingSizePrinter {
72+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73+
write!(f, "<size={}>", self.0)
74+
}
75+
}
76+
&RingSizePrinter(cluster_data.ring.len())
77+
})
78+
.field("keyspaces", &cluster_data.keyspaces.keys())
79+
.field("all_nodes", &cluster_data.all_nodes)
80+
.field("datacenters", &cluster_data.datacenters)
81+
.finish_non_exhaustive()
82+
}
83+
}
84+
4885
// Works in the background to keep the cluster updated
4986
struct ClusterWorker {
5087
// Cluster data to keep updated:

scylla/src/transport/connection_pool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ pub struct NodeConnectionPool {
144144
pool_updated_notify: Arc<Notify>,
145145
}
146146

147+
impl std::fmt::Debug for NodeConnectionPool {
148+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149+
f.debug_struct("NodeConnectionPool")
150+
.field("conns", &self.conns)
151+
.finish_non_exhaustive()
152+
}
153+
}
154+
147155
impl NodeConnectionPool {
148156
pub fn new(
149157
address: IpAddr,

scylla/src/transport/load_balancing/dc_aware_round_robin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::{
77
use tracing::trace;
88

99
/// A data-center aware Round-robin load balancing policy.
10+
#[derive(Debug)]
1011
pub struct DcAwareRoundRobinPolicy {
1112
index: AtomicUsize,
1213
local_dc: String,

scylla/src/transport/load_balancing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> Statement<'a> {
3535
pub type Plan<'a> = Box<dyn Iterator<Item = Arc<Node>> + Send + Sync + 'a>;
3636

3737
/// Policy that decides which nodes to contact for each query
38-
pub trait LoadBalancingPolicy: Send + Sync {
38+
pub trait LoadBalancingPolicy: Send + Sync + std::fmt::Debug {
3939
/// It is used for each query to find which nodes to query first
4040
fn plan<'a>(&self, statement: &Statement, cluster: &'a ClusterData) -> Plan<'a>;
4141

scylla/src/transport/load_balancing/round_robin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::{
77
use tracing::trace;
88

99
/// A Round-robin load balancing policy.
10+
#[derive(Debug)]
1011
pub struct RoundRobinPolicy {
1112
index: AtomicUsize,
1213
}

scylla/src/transport/load_balancing/token_aware.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{collections::HashMap, sync::Arc};
99
use tracing::trace;
1010

1111
/// A wrapper load balancing policy that adds token awareness to a child policy.
12+
#[derive(Debug)]
1213
pub struct TokenAwarePolicy {
1314
child_policy: Box<dyn ChildLoadBalancingPolicy>,
1415
}
@@ -433,6 +434,7 @@ mod tests {
433434

434435
// Used as child policy for TokenAwarePolicy tests
435436
// Forwards plan passed to it in apply_child_policy() method
437+
#[derive(Debug)]
436438
struct DumbPolicy {}
437439

438440
impl LoadBalancingPolicy for DumbPolicy {

scylla/src/transport/node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
};
1616

1717
/// Node represents a cluster node along with it's data and connections
18+
#[derive(Debug)]
1819
pub struct Node {
1920
pub address: SocketAddr,
2021
pub datacenter: Option<String>,

scylla/src/transport/session.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::query::Query;
2525
use crate::routing::Token;
2626
use crate::statement::{Consistency, SerialConsistency};
2727
use crate::tracing::{GetTracingConfig, TracingEvent, TracingInfo};
28-
use crate::transport::cluster::{Cluster, ClusterData};
28+
use crate::transport::cluster::{Cluster, ClusterData, ClusterNeatDebug};
2929
use crate::transport::connection::{
3030
BatchResult, Connection, ConnectionConfig, VerifiedKeyspaceName,
3131
};
@@ -65,6 +65,29 @@ pub struct Session {
6565
auto_await_schema_agreement_timeout: Option<Duration>,
6666
}
6767

68+
/// This implementation deliberately omits some details from Cluster in order
69+
/// to avoid cluttering the print with much information of little usability.
70+
impl std::fmt::Debug for Session {
71+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72+
f.debug_struct("Session")
73+
.field("cluster", &ClusterNeatDebug(&self.cluster))
74+
.field("load_balancer", &self.load_balancer)
75+
.field("schema_agreement_interval", &self.schema_agreement_interval)
76+
.field("retry_policy", &self.retry_policy)
77+
.field(
78+
"speculative_execution_policy",
79+
&self.speculative_execution_policy,
80+
)
81+
.field("metrics", &self.metrics)
82+
.field("default_consistency", &self.default_consistency)
83+
.field(
84+
"auto_await_schema_agreement_timeout",
85+
&self.auto_await_schema_agreement_timeout,
86+
)
87+
.finish()
88+
}
89+
}
90+
6891
/// Configuration options for [`Session`].
6992
/// Can be created manually, but usually it's easier to use
7093
/// [SessionBuilder](super::session_builder::SessionBuilder)

0 commit comments

Comments
 (0)