Skip to content

Commit

Permalink
Allow to prioritize nodes based on their datacenter
Browse files Browse the repository at this point in the history
  • Loading branch information
vponomaryov authored and pkolaczk committed Jul 4, 2024
1 parent 9d24d97 commit dbb2696
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub struct ConnectionConf {
#[clap(long("ssl-key"), value_name = "PATH")]
pub ssl_key_file: Option<PathBuf>,

/// Datacenter name
#[clap(long("datacenter"), required = false, default_value = "")]
pub datacenter: String,

/// Default CQL query consistency level
#[clap(long("consistency"), required = false, default_value = "LOCAL_QUORUM")]
pub consistency: Consistency,
Expand Down
7 changes: 7 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rune::{Any, Value};
use rust_embed::RustEmbed;
use scylla::_macro_internal::ColumnType;
use scylla::frame::response::result::CqlValue;
use scylla::load_balancing::DefaultPolicy;
use scylla::prepared_statement::PreparedStatement;
use scylla::transport::errors::{DbError, NewSessionError, QueryError};
use scylla::transport::session::PoolSize;
Expand Down Expand Up @@ -57,8 +58,14 @@ fn ssl_context(conf: &&ConnectionConf) -> Result<Option<SslContext>, CassError>

/// Configures connection to Cassandra.
pub async fn connect(conf: &ConnectionConf) -> Result<Context, CassError> {
let mut policy_builder = DefaultPolicy::builder().token_aware(true);
let dc = &conf.datacenter;
if !dc.is_empty() {
policy_builder = policy_builder.prefer_datacenter(dc.to_owned()).permit_dc_failover(true);
}
let profile = ExecutionProfile::builder()
.consistency(conf.consistency.scylla_consistency())
.load_balancing_policy(policy_builder.build())
.request_timeout(Some(Duration::from_secs(conf.request_timeout.get() as u64)))
.build();

Expand Down
4 changes: 4 additions & 0 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ impl<'a> Display for RunConfigCmp<'a> {
}

let lines: Vec<Box<dyn Display>> = vec![
self.line("Datacenter", "", |conf| {conf.connection.datacenter.clone()}),
self.line("Consistency", "", |conf| {
conf.connection.consistency.scylla_consistency().to_string()
}),
self.line("Threads", "", |conf| Quantity::from(conf.threads)),
self.line("Connections", "", |conf| {
Quantity::from(conf.connection.count)
Expand Down

0 comments on commit dbb2696

Please sign in to comment.