Skip to content

Commit ebfc00d

Browse files
committed
session: remove get_tracing_info_custom
Remove Session::get_tracing_info_custom - a variant of Session::get_tracing_info that allows for additional customization of the fetch process (number of attempts, interval between attempts, consistency level of fetching tracing info). This customization is now moved to SessionConfig (as new parameters) and new helper functions were introduced to SessionBuilder. Since now this customization is on SessionConfig, create_new_session_builder() in test_utils.rs returns a SessionBuilder with pre-configured large number of TracingInfo fetch attempts, longer delays (to account for a slow CI). Previously each tracing test had to configure it on its own by using get_tracing_info_custom. This change is a part of our effort to stabilize the API and reduce the number of pub functions. Refs #660
1 parent 5ff6ee2 commit ebfc00d

File tree

6 files changed

+169
-126
lines changed

6 files changed

+169
-126
lines changed

examples/tracing.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use scylla::batch::Batch;
77
use scylla::statement::{
88
prepared_statement::PreparedStatement, query::Query, Consistency, SerialConsistency,
99
};
10-
use scylla::tracing::{GetTracingConfig, TracingInfo};
10+
use scylla::tracing::TracingInfo;
1111
use scylla::transport::iterator::RowIterator;
1212
use scylla::QueryResult;
1313
use scylla::{Session, SessionBuilder};
@@ -21,7 +21,10 @@ async fn main() -> Result<()> {
2121
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
2222

2323
println!("Connecting to {} ...", uri);
24-
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
24+
let session: Session = SessionBuilder::new()
25+
.known_node(uri.as_str())
26+
.build()
27+
.await?;
2528

2629
session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
2730

@@ -106,19 +109,18 @@ async fn main() -> Result<()> {
106109
println!("Batch tracing id: {:?}\n", batch_result.tracing_id);
107110

108111
// CUSTOM
109-
// GetTracingConfig allows to specify a custom settings for querying tracing info
112+
// Session configuration allows specifying custom settings for querying tracing info.
110113
// Tracing info might not immediately be available on queried node
111114
// so the driver performs a few attempts with sleeps in between.
112-
113-
let custom_config: GetTracingConfig = GetTracingConfig {
114-
attempts: NonZeroU32::new(8).unwrap(),
115-
interval: Duration::from_millis(100),
116-
consistency: Consistency::One,
117-
};
118-
119-
let _custom_info: TracingInfo = session
120-
.get_tracing_info_custom(&query_tracing_id, &custom_config)
115+
let session: Session = SessionBuilder::new()
116+
.known_node(uri)
117+
.tracing_info_fetch_attempts(NonZeroU32::new(8).unwrap())
118+
.tracing_info_fetch_interval(Duration::from_millis(100))
119+
.tracing_info_fetch_consistency(Consistency::One)
120+
.build()
121121
.await?;
122122

123+
let _custom_info: TracingInfo = session.get_tracing_info(&query_tracing_id).await?;
124+
123125
Ok(())
124126
}

scylla/src/tracing.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use crate::statement::Consistency;
21
use itertools::Itertools;
32
use std::collections::HashMap;
43
use std::net::IpAddr;
5-
use std::num::NonZeroU32;
6-
use std::time::Duration;
74
use uuid::Uuid;
85

96
use crate::cql_to_rust::{FromRow, FromRowError};
@@ -35,20 +32,6 @@ pub struct TracingEvent {
3532
pub thread: Option<String>,
3633
}
3734

38-
/// Used to configure a custom retry strategy when querying tracing info
39-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40-
pub struct GetTracingConfig {
41-
/// Number of attempts to be made before giving up.
42-
/// Default value: 5
43-
pub attempts: NonZeroU32,
44-
/// Interval to wait between each attempt.
45-
/// Default value: 3 milliseconds
46-
pub interval: Duration,
47-
/// Consistency to use in queries that read TracingInfo.
48-
/// Default value: One
49-
pub consistency: Consistency,
50-
}
51-
5235
impl TracingInfo {
5336
/// Returns a list of unique nodes involved in the query
5437
pub fn nodes(&self) -> Vec<IpAddr> {
@@ -60,16 +43,6 @@ impl TracingInfo {
6043
}
6144
}
6245

63-
impl Default for GetTracingConfig {
64-
fn default() -> GetTracingConfig {
65-
GetTracingConfig {
66-
attempts: NonZeroU32::new(5).unwrap(),
67-
interval: Duration::from_millis(3),
68-
consistency: Consistency::One,
69-
}
70-
}
71-
}
72-
7346
// A query used to query TracingInfo from system_traces.sessions
7447
pub(crate) const TRACES_SESSION_QUERY_STR: &str =
7548
"SELECT client, command, coordinator, duration, parameters, request, started_at \

scylla/src/transport/session.rs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::fmt::Display;
2424
use std::future::Future;
2525
use std::io;
2626
use std::net::SocketAddr;
27+
use std::num::NonZeroU32;
2728
use std::str::FromStr;
2829
use std::sync::atomic::AtomicUsize;
2930
use std::sync::atomic::Ordering;
@@ -56,7 +57,7 @@ use crate::prepared_statement::{PartitionKeyError, PreparedStatement};
5657
use crate::query::Query;
5758
use crate::routing::Token;
5859
use crate::statement::{Consistency, SerialConsistency};
59-
use crate::tracing::{GetTracingConfig, TracingEvent, TracingInfo};
60+
use crate::tracing::{TracingEvent, TracingInfo};
6061
use crate::transport::cluster::{Cluster, ClusterData, ClusterNeatDebug};
6162
use crate::transport::connection::{Connection, ConnectionConfig, VerifiedKeyspaceName};
6263
use crate::transport::connection_pool::PoolConfig;
@@ -130,6 +131,9 @@ pub struct Session {
130131
auto_await_schema_agreement_timeout: Option<Duration>,
131132
refresh_metadata_on_auto_schema_agreement: bool,
132133
keyspace_name: ArcSwapOption<String>,
134+
tracing_info_fetch_attempts: NonZeroU32,
135+
tracing_info_fetch_interval: Duration,
136+
tracing_info_fetch_consistency: Consistency,
133137
}
134138

135139
/// This implementation deliberately omits some details from Cluster in order
@@ -235,6 +239,22 @@ pub struct SessionConfig {
235239
/// Please do performance measurements before committing to disabling
236240
/// this option.
237241
pub enable_write_coalescing: bool,
242+
243+
/// Number of attempts to fetch [`TracingInfo`]
244+
/// in [`Session::get_tracing_info`]. Tracing info
245+
/// might not be available immediately on queried node - that's why
246+
/// the driver performs a few attempts with sleeps in between.
247+
pub tracing_info_fetch_attempts: NonZeroU32,
248+
249+
/// Delay between attempts to fetch [`TracingInfo`]
250+
/// in [`Session::get_tracing_info`]. Tracing info
251+
/// might not be available immediately on queried node - that's why
252+
/// the driver performs a few attempts with sleeps in between.
253+
pub tracing_info_fetch_interval: Duration,
254+
255+
/// Consistency level of fetching [`TracingInfo`]
256+
/// in [`Session::get_tracing_info`].
257+
pub tracing_info_fetch_consistency: Consistency,
238258
}
239259

240260
/// Describes database server known on Session startup.
@@ -294,6 +314,9 @@ impl SessionConfig {
294314
#[cfg(feature = "cloud")]
295315
cloud_config: None,
296316
enable_write_coalescing: true,
317+
tracing_info_fetch_attempts: NonZeroU32::new(5).unwrap(),
318+
tracing_info_fetch_interval: Duration::from_millis(3),
319+
tracing_info_fetch_consistency: Consistency::One,
297320
}
298321
}
299322

@@ -544,6 +567,9 @@ impl Session {
544567
refresh_metadata_on_auto_schema_agreement: config
545568
.refresh_metadata_on_auto_schema_agreement,
546569
keyspace_name: ArcSwapOption::default(), // will be set by use_keyspace
570+
tracing_info_fetch_attempts: config.tracing_info_fetch_attempts,
571+
tracing_info_fetch_interval: config.tracing_info_fetch_interval,
572+
tracing_info_fetch_consistency: config.tracing_info_fetch_consistency,
547573
};
548574

549575
if let Some(keyspace_name) = config.used_keyspace {
@@ -1337,8 +1363,24 @@ impl Session {
13371363
/// See [the book](https://rust-driver.docs.scylladb.com/stable/tracing/tracing.html)
13381364
/// for more information about query tracing
13391365
pub async fn get_tracing_info(&self, tracing_id: &Uuid) -> Result<TracingInfo, QueryError> {
1340-
self.get_tracing_info_custom(tracing_id, &GetTracingConfig::default())
1341-
.await
1366+
// tracing_info_fetch_attempts is NonZeroU32 so at least one attempt will be made
1367+
for _ in 0..self.tracing_info_fetch_attempts.get() {
1368+
let current_try: Option<TracingInfo> = self
1369+
.try_getting_tracing_info(tracing_id, Some(self.tracing_info_fetch_consistency))
1370+
.await?;
1371+
1372+
match current_try {
1373+
Some(tracing_info) => return Ok(tracing_info),
1374+
None => tokio::time::sleep(self.tracing_info_fetch_interval).await,
1375+
};
1376+
}
1377+
1378+
Err(QueryError::ProtocolError(
1379+
"All tracing queries returned an empty result, \
1380+
maybe the trace information didn't propagate yet. \
1381+
Consider configuring Session with \
1382+
a longer fetch interval (tracing_info_fetch_interval)",
1383+
))
13421384
}
13431385

13441386
/// Gets the name of the keyspace that is currently set, or `None` if no
@@ -1356,35 +1398,6 @@ impl Session {
13561398
self.keyspace_name.load_full()
13571399
}
13581400

1359-
/// Queries tracing info with custom retry settings.\
1360-
/// Tracing info might not be available immediately on queried node -
1361-
/// that's why the driver performs a few attempts with sleeps in between.
1362-
/// [`GetTracingConfig`] allows to specify a custom querying strategy.
1363-
pub async fn get_tracing_info_custom(
1364-
&self,
1365-
tracing_id: &Uuid,
1366-
config: &GetTracingConfig,
1367-
) -> Result<TracingInfo, QueryError> {
1368-
// config.attempts is NonZeroU32 so at least one attempt will be made
1369-
for _ in 0..config.attempts.get() {
1370-
let current_try: Option<TracingInfo> = self
1371-
.try_getting_tracing_info(tracing_id, Some(config.consistency))
1372-
.await?;
1373-
1374-
match current_try {
1375-
Some(tracing_info) => return Ok(tracing_info),
1376-
None => tokio::time::sleep(config.interval).await,
1377-
};
1378-
}
1379-
1380-
Err(QueryError::ProtocolError(
1381-
"All tracing queries returned an empty result, \
1382-
maybe information didn't reach this node yet. \
1383-
Consider using get_tracing_info_custom with \
1384-
bigger interval in GetTracingConfig",
1385-
))
1386-
}
1387-
13881401
// Tries getting the tracing info
13891402
// If the queries return 0 rows then returns None - the information didn't reach this node yet
13901403
// If there is some other error returns this error

scylla/src/transport/session_builder.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ use crate::cloud::{CloudConfig, CloudConfigError};
1010
#[cfg(feature = "cloud")]
1111
use crate::ExecutionProfile;
1212

13+
use crate::statement::Consistency;
1314
use crate::transport::connection_pool::PoolSize;
1415
use crate::transport::host_filter::HostFilter;
1516
use std::borrow::Borrow;
1617
use std::marker::PhantomData;
1718
use std::net::SocketAddr;
19+
use std::num::NonZeroU32;
1820
#[cfg(feature = "cloud")]
1921
use std::path::Path;
2022
use std::sync::Arc;
@@ -828,6 +830,77 @@ impl<K: SessionBuilderKind> GenericSessionBuilder<K> {
828830
self.config.refresh_metadata_on_auto_schema_agreement = refresh_metadata;
829831
self
830832
}
833+
834+
/// Set the number of attempts to fetch [TracingInfo](crate::tracing::TracingInfo)
835+
/// in [`Session::get_tracing_info`].
836+
/// The default is 5 attempts.
837+
///
838+
/// Tracing info might not be available immediately on queried node - that's why
839+
/// the driver performs a few attempts with sleeps in between.
840+
///
841+
/// # Example
842+
/// ```
843+
/// # use scylla::{Session, SessionBuilder};
844+
/// # use std::num::NonZeroU32;
845+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
846+
/// let session: Session = SessionBuilder::new()
847+
/// .known_node("127.0.0.1:9042")
848+
/// .tracing_info_fetch_attempts(NonZeroU32::new(10).unwrap())
849+
/// .build()
850+
/// .await?;
851+
/// # Ok(())
852+
/// # }
853+
/// ```
854+
pub fn tracing_info_fetch_attempts(mut self, attempts: NonZeroU32) -> Self {
855+
self.config.tracing_info_fetch_attempts = attempts;
856+
self
857+
}
858+
859+
/// Set the delay between attempts to fetch [TracingInfo](crate::tracing::TracingInfo)
860+
/// in [`Session::get_tracing_info`].
861+
/// The default is 3 milliseconds.
862+
///
863+
/// Tracing info might not be available immediately on queried node - that's why
864+
/// the driver performs a few attempts with sleeps in between.
865+
///
866+
/// # Example
867+
/// ```
868+
/// # use scylla::{Session, SessionBuilder};
869+
/// # use std::time::Duration;
870+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
871+
/// let session: Session = SessionBuilder::new()
872+
/// .known_node("127.0.0.1:9042")
873+
/// .tracing_info_fetch_interval(Duration::from_millis(50))
874+
/// .build()
875+
/// .await?;
876+
/// # Ok(())
877+
/// # }
878+
/// ```
879+
pub fn tracing_info_fetch_interval(mut self, interval: Duration) -> Self {
880+
self.config.tracing_info_fetch_interval = interval;
881+
self
882+
}
883+
884+
/// Set the consistency level of fetching [TracingInfo](crate::tracing::TracingInfo)
885+
/// in [`Session::get_tracing_info`].
886+
/// The default is [`Consistency::One`].
887+
///
888+
/// # Example
889+
/// ```
890+
/// # use scylla::{Session, SessionBuilder, statement::Consistency};
891+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
892+
/// let session: Session = SessionBuilder::new()
893+
/// .known_node("127.0.0.1:9042")
894+
/// .tracing_info_fetch_consistency(Consistency::One)
895+
/// .build()
896+
/// .await?;
897+
/// # Ok(())
898+
/// # }
899+
/// ```
900+
pub fn tracing_info_fetch_consistency(mut self, consistency: Consistency) -> Self {
901+
self.config.tracing_info_fetch_consistency = consistency;
902+
self
903+
}
831904
}
832905

833906
/// Creates a [`SessionBuilder`] with default configuration, same as [`SessionBuilder::new`]

0 commit comments

Comments
 (0)