Skip to content

Commit dea239a

Browse files
committed
lbp_config: make LBP optional
In this commit, we make `load_balancing_kind` optional in `LoadBalancingConfig`. Now, there are two cases: - for cluster's LBP, there is a default value (round-robin) which driver's going to use if user did not provide LBP. Thus, the `unwrap_or` call in `LoadBalancingConfig::build` - for exec profile's LBP, if user did not provide an LBP kind (load_balancing_kind is None), then, according to cpp-driver, cluster's LBP should be used (instead of some predefined default). After this commit, we are finally consistent with cpp-driver. Now, if user did not specify an LBP for given execution profile, the cluster's default LBP (session-wide LBP) will be assigned to the profile.
1 parent 922b1a3 commit dea239a

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

scylla-rust-wrapper/src/cluster.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,26 @@ const DRIVER_VERSION: &str = env!("CARGO_PKG_VERSION");
5151
pub(crate) struct LoadBalancingConfig {
5252
pub(crate) token_awareness_enabled: bool,
5353
pub(crate) token_aware_shuffling_replicas_enabled: bool,
54-
pub(crate) load_balancing_kind: LoadBalancingKind,
54+
pub(crate) load_balancing_kind: Option<LoadBalancingKind>,
5555
pub(crate) latency_awareness_enabled: bool,
5656
pub(crate) latency_awareness_builder: LatencyAwarenessBuilder,
5757
}
5858
impl LoadBalancingConfig {
5959
// This is `async` to prevent running this function from beyond tokio context,
6060
// as it results in panic due to DefaultPolicyBuilder::build() spawning a tokio task.
6161
pub(crate) async fn build(self) -> Arc<dyn LoadBalancingPolicy> {
62+
let load_balancing_kind = self
63+
.load_balancing_kind
64+
// Round robin is chosen by default for cluster wide LBP.
65+
.unwrap_or(LoadBalancingKind::RoundRobin);
66+
6267
let mut builder = DefaultPolicyBuilder::new().token_aware(self.token_awareness_enabled);
6368
if self.token_awareness_enabled {
6469
// Cpp-driver enables shuffling replicas only if token aware routing is enabled.
6570
builder =
6671
builder.enable_shuffling_replicas(self.token_aware_shuffling_replicas_enabled);
6772
}
68-
if let LoadBalancingKind::DcAware { local_dc } = self.load_balancing_kind {
73+
if let LoadBalancingKind::DcAware { local_dc } = load_balancing_kind {
6974
builder = builder.prefer_datacenter(local_dc).permit_dc_failover(true)
7075
}
7176
if self.latency_awareness_enabled {
@@ -79,7 +84,7 @@ impl Default for LoadBalancingConfig {
7984
Self {
8085
token_awareness_enabled: true,
8186
token_aware_shuffling_replicas_enabled: true,
82-
load_balancing_kind: LoadBalancingKind::RoundRobin,
87+
load_balancing_kind: None,
8388
latency_awareness_enabled: false,
8489
latency_awareness_builder: Default::default(),
8590
}
@@ -456,7 +461,7 @@ pub unsafe extern "C" fn cass_cluster_set_credentials_n(
456461
#[no_mangle]
457462
pub unsafe extern "C" fn cass_cluster_set_load_balance_round_robin(cluster_raw: *mut CassCluster) {
458463
let cluster = ptr_to_ref_mut(cluster_raw);
459-
cluster.load_balancing_config.load_balancing_kind = LoadBalancingKind::RoundRobin;
464+
cluster.load_balancing_config.load_balancing_kind = Some(LoadBalancingKind::RoundRobin);
460465
}
461466

462467
#[no_mangle]
@@ -495,7 +500,7 @@ pub(crate) unsafe fn set_load_balance_dc_aware_n(
495500
.unwrap()
496501
.to_string();
497502

498-
load_balancing_config.load_balancing_kind = LoadBalancingKind::DcAware { local_dc };
503+
load_balancing_config.load_balancing_kind = Some(LoadBalancingKind::DcAware { local_dc });
499504

500505
CassError::CASS_OK
501506
}
@@ -850,10 +855,7 @@ mod tests {
850855
/* Test valid configurations */
851856
let cluster = ptr_to_ref(cluster_raw);
852857
{
853-
assert_matches!(
854-
cluster.load_balancing_config.load_balancing_kind,
855-
LoadBalancingKind::RoundRobin
856-
);
858+
assert_matches!(cluster.load_balancing_config.load_balancing_kind, None);
857859
assert!(cluster.load_balancing_config.token_awareness_enabled);
858860
assert!(!cluster.load_balancing_config.latency_awareness_enabled);
859861
}
@@ -882,7 +884,7 @@ mod tests {
882884

883885
let load_balancing_kind = &cluster.load_balancing_config.load_balancing_kind;
884886
match load_balancing_kind {
885-
LoadBalancingKind::DcAware { local_dc } => {
887+
Some(LoadBalancingKind::DcAware { local_dc }) => {
886888
assert_eq!(local_dc, "eu")
887889
}
888890
_ => panic!("Expected preferred dc"),

scylla-rust-wrapper/src/exec_profile.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ impl CassExecProfile {
4242
}
4343
}
4444

45-
pub(crate) async fn build(self) -> ExecutionProfile {
46-
self.inner
47-
.load_balancing_policy(self.load_balancing_config.build().await)
48-
.build()
45+
pub(crate) async fn build(
46+
self,
47+
cluster_default_profile: &ExecutionProfile,
48+
) -> ExecutionProfile {
49+
let load_balacing = if self.load_balancing_config.load_balancing_kind.is_some() {
50+
self.load_balancing_config.build().await
51+
} else {
52+
// If load balancing config does not have LB kind defined,
53+
// we make use of cluster's LBP.
54+
cluster_default_profile.get_load_balancing_policy().clone()
55+
};
56+
57+
self.inner.load_balancing_policy(load_balacing).build()
4958
}
5059
}
5160

@@ -353,7 +362,7 @@ pub unsafe extern "C" fn cass_execution_profile_set_load_balance_round_robin(
353362
profile: *mut CassExecProfile,
354363
) -> CassError {
355364
let profile_builder = ptr_to_ref_mut(profile);
356-
profile_builder.load_balancing_config.load_balancing_kind = LoadBalancingKind::RoundRobin;
365+
profile_builder.load_balancing_config.load_balancing_kind = Some(LoadBalancingKind::RoundRobin);
357366

358367
CassError::CASS_OK
359368
}
@@ -473,10 +482,7 @@ mod tests {
473482
/* Test valid configurations */
474483
let profile = ptr_to_ref(profile_raw);
475484
{
476-
assert_matches!(
477-
profile.load_balancing_config.load_balancing_kind,
478-
LoadBalancingKind::RoundRobin
479-
);
485+
assert_matches!(profile.load_balancing_config.load_balancing_kind, None);
480486
assert!(profile.load_balancing_config.token_awareness_enabled);
481487
assert!(!profile.load_balancing_config.latency_awareness_enabled);
482488
}
@@ -505,7 +511,7 @@ mod tests {
505511

506512
let load_balancing_kind = &profile.load_balancing_config.load_balancing_kind;
507513
match load_balancing_kind {
508-
LoadBalancingKind::DcAware { local_dc } => {
514+
Some(LoadBalancingKind::DcAware { local_dc }) => {
509515
assert_eq!(local_dc, "eu")
510516
}
511517
_ => panic!("Expected preferred dc"),

scylla-rust-wrapper/src/session.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ impl CassSessionInner {
109109
"Already connecting, closing, or connected".msg(),
110110
));
111111
}
112+
113+
let mut session_builder = session_builder_fut.await;
114+
let default_profile = session_builder
115+
.config
116+
.default_execution_profile_handle
117+
.to_profile();
118+
112119
let mut exec_profile_map = HashMap::with_capacity(exec_profile_builder_map.len());
113120
for (name, builder) in exec_profile_builder_map {
114-
exec_profile_map.insert(name, builder.build().await.into_handle());
121+
exec_profile_map.insert(name, builder.build(&default_profile).await.into_handle());
115122
}
116123

117-
let mut session_builder = session_builder_fut.await;
118124
if let Some(keyspace) = keyspace {
119125
session_builder = session_builder.use_keyspace(keyspace, false);
120126
}

0 commit comments

Comments
 (0)