From d33ec3f828147e34e62e76f8f0cfd25004e68b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Uzarski?= Date: Wed, 16 Oct 2024 14:21:31 +0200 Subject: [PATCH 1/3] cluster: reduce mutability scope for default SB In following commits, we will adjust the defaults according to `cassandra.h`. It will be much clearer if we define all defaults in nested scope. --- scylla-rust-wrapper/src/cluster.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scylla-rust-wrapper/src/cluster.rs b/scylla-rust-wrapper/src/cluster.rs index 1daa0a20..6ccb82a7 100644 --- a/scylla-rust-wrapper/src/cluster.rs +++ b/scylla-rust-wrapper/src/cluster.rs @@ -159,13 +159,17 @@ pub unsafe extern "C" fn cass_cluster_new() -> *mut CassCluster { .consistency(DEFAULT_CONSISTENCY) .request_timeout(Some(Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MILLIS))); - // Set DRIVER_NAME and DRIVER_VERSION of cpp-rust driver. - let custom_identity = SelfIdentity::new() - .with_custom_driver_name(DRIVER_NAME) - .with_custom_driver_version(DRIVER_VERSION); + let default_session_builder = { + // Set DRIVER_NAME and DRIVER_VERSION of cpp-rust driver. + let custom_identity = SelfIdentity::new() + .with_custom_driver_name(DRIVER_NAME) + .with_custom_driver_version(DRIVER_VERSION); + + SessionBuilder::new().custom_identity(custom_identity) + }; Box::into_raw(Box::new(CassCluster { - session_builder: SessionBuilder::new().custom_identity(custom_identity), + session_builder: default_session_builder, port: 9042, contact_points: Vec::new(), // Per DataStax documentation: Without additional configuration the C/C++ driver From 8cb3f499d536a4fc602adf01905774b648f7181b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Uzarski?= Date: Wed, 16 Oct 2024 14:38:34 +0200 Subject: [PATCH 2/3] cluster: adjust config defaults Some of them are already set like this in rust-driver by default, but it does not hurt us to set them here as well, in case something changes in rust-driver. --- scylla-rust-wrapper/src/cluster.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/scylla-rust-wrapper/src/cluster.rs b/scylla-rust-wrapper/src/cluster.rs index 6ccb82a7..3d9dd051 100644 --- a/scylla-rust-wrapper/src/cluster.rs +++ b/scylla-rust-wrapper/src/cluster.rs @@ -28,11 +28,17 @@ use std::time::Duration; use crate::cass_compression_types::CassCompressionType; -// According to `cassandra.h` the default CPP driver's +// According to `cassandra.h` the defaults for // - consistency for statements is LOCAL_ONE, -// - request client timeout is 12000 millis. const DEFAULT_CONSISTENCY: Consistency = Consistency::LocalOne; -const DEFAULT_REQUEST_TIMEOUT_MILLIS: u64 = 12000; +// - request client timeout is 12000 millis, +const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_millis(12000); +// - fetching schema metadata is true +const DEFAULT_DO_FETCH_SCHEMA_METADATA: bool = true; +// - setting TCP_NODELAY is true +const DEFAULT_SET_TCP_NO_DELAY: bool = true; +// - connect timeout is 5000 millis +const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_millis(5000); const DRIVER_NAME: &str = "ScyllaDB Cpp-Rust Driver"; const DRIVER_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -157,15 +163,20 @@ pub fn build_session_builder( pub unsafe extern "C" fn cass_cluster_new() -> *mut CassCluster { let default_execution_profile_builder = ExecutionProfileBuilder::default() .consistency(DEFAULT_CONSISTENCY) - .request_timeout(Some(Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MILLIS))); + .request_timeout(Some(DEFAULT_REQUEST_TIMEOUT)); + // Default config options - according to cassandra.h let default_session_builder = { // Set DRIVER_NAME and DRIVER_VERSION of cpp-rust driver. let custom_identity = SelfIdentity::new() .with_custom_driver_name(DRIVER_NAME) .with_custom_driver_version(DRIVER_VERSION); - SessionBuilder::new().custom_identity(custom_identity) + SessionBuilder::new() + .custom_identity(custom_identity) + .fetch_schema_metadata(DEFAULT_DO_FETCH_SCHEMA_METADATA) + .tcp_nodelay(DEFAULT_SET_TCP_NO_DELAY) + .connection_timeout(DEFAULT_CONNECT_TIMEOUT) }; Box::into_raw(Box::new(CassCluster { From 99f6e449d3f80eefab48b39549b125e80d1fcabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Uzarski?= Date: Wed, 16 Oct 2024 14:53:28 +0200 Subject: [PATCH 3/3] cluster: set keepalive config options Sets the defaults, and implements: - cass_cluster_set_connection_heartbeat_interval - cass_cluster_set_connection_idle_timeout --- scylla-rust-wrapper/src/cluster.rs | 28 ++++++++++++++++++++++++++++ src/testing_unimplemented.cpp | 8 -------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/scylla-rust-wrapper/src/cluster.rs b/scylla-rust-wrapper/src/cluster.rs index 3d9dd051..d11a3d44 100644 --- a/scylla-rust-wrapper/src/cluster.rs +++ b/scylla-rust-wrapper/src/cluster.rs @@ -39,6 +39,10 @@ const DEFAULT_DO_FETCH_SCHEMA_METADATA: bool = true; const DEFAULT_SET_TCP_NO_DELAY: bool = true; // - connect timeout is 5000 millis const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_millis(5000); +// - keepalive interval is 30 secs +const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30); +// - keepalive timeout is 60 secs +const DEFAULT_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(60); const DRIVER_NAME: &str = "ScyllaDB Cpp-Rust Driver"; const DRIVER_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -177,6 +181,8 @@ pub unsafe extern "C" fn cass_cluster_new() -> *mut CassCluster { .fetch_schema_metadata(DEFAULT_DO_FETCH_SCHEMA_METADATA) .tcp_nodelay(DEFAULT_SET_TCP_NO_DELAY) .connection_timeout(DEFAULT_CONNECT_TIMEOUT) + .keepalive_interval(DEFAULT_KEEPALIVE_INTERVAL) + .keepalive_timeout(DEFAULT_KEEPALIVE_TIMEOUT) }; Box::into_raw(Box::new(CassCluster { @@ -358,6 +364,28 @@ pub unsafe extern "C" fn cass_cluster_set_tcp_keepalive( cluster.session_builder.config.tcp_keepalive_interval = tcp_keepalive_interval; } +#[no_mangle] +pub unsafe extern "C" fn cass_cluster_set_connection_heartbeat_interval( + cluster_raw: *mut CassCluster, + interval_secs: c_uint, +) { + let cluster = ptr_to_ref_mut(cluster_raw); + let keepalive_interval = (interval_secs > 0).then(|| Duration::from_secs(interval_secs as u64)); + + cluster.session_builder.config.keepalive_interval = keepalive_interval; +} + +#[no_mangle] +pub unsafe extern "C" fn cass_cluster_set_connection_idle_timeout( + cluster_raw: *mut CassCluster, + timeout_secs: c_uint, +) { + let cluster = ptr_to_ref_mut(cluster_raw); + let keepalive_timeout = (timeout_secs > 0).then(|| Duration::from_secs(timeout_secs as u64)); + + cluster.session_builder.config.keepalive_timeout = keepalive_timeout; +} + #[no_mangle] pub unsafe extern "C" fn cass_cluster_set_connect_timeout( cluster_raw: *mut CassCluster, diff --git a/src/testing_unimplemented.cpp b/src/testing_unimplemented.cpp index 53d82333..44e8e7e2 100644 --- a/src/testing_unimplemented.cpp +++ b/src/testing_unimplemented.cpp @@ -62,14 +62,6 @@ CASS_EXPORT CassError cass_cluster_set_cloud_secure_connection_bundle_no_ssl_lib throw std::runtime_error( "UNIMPLEMENTED cass_cluster_set_cloud_secure_connection_bundle_no_ssl_lib_init\n"); } -CASS_EXPORT void cass_cluster_set_connection_heartbeat_interval(CassCluster* cluster, - unsigned interval_secs) { - throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_connection_heartbeat_interval\n"); -} -CASS_EXPORT void cass_cluster_set_connection_idle_timeout(CassCluster* cluster, - unsigned timeout_secs) { - throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_connection_idle_timeout\n"); -} CASS_EXPORT void cass_cluster_set_constant_reconnect(CassCluster* cluster, cass_uint64_t delay_ms) { throw std::runtime_error("UNIMPLEMENTED cass_cluster_set_constant_reconnect\n"); }