forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_load_balancing_policy.rs
78 lines (67 loc) · 2.31 KB
/
custom_load_balancing_policy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use anyhow::Result;
use rand::thread_rng;
use rand::Rng;
use scylla::transport::NodeRef;
use scylla::{
load_balancing::{LoadBalancingPolicy, RoutingInfo},
routing::Shard,
transport::{ClusterData, ExecutionProfile},
Session, SessionBuilder,
};
use std::{env, sync::Arc};
/// Example load balancing policy that prefers nodes from favorite datacenter
/// This is, of course, very naive, as it is completely non token-aware.
/// For more realistic implementation, see [`DefaultPolicy`](scylla::load_balancing::DefaultPolicy).
#[derive(Debug)]
struct CustomLoadBalancingPolicy {
fav_datacenter_name: String,
}
fn with_random_shard(node: NodeRef) -> (NodeRef, Option<Shard>) {
let nr_shards = node
.sharder()
.map(|sharder| sharder.nr_shards.get())
.unwrap_or(1);
(node, Some(thread_rng().gen_range(0..nr_shards) as Shard))
}
impl LoadBalancingPolicy for CustomLoadBalancingPolicy {
fn pick<'a>(
&'a self,
_info: &'a RoutingInfo,
cluster: &'a ClusterData,
) -> Option<(NodeRef<'a>, Option<Shard>)> {
self.fallback(_info, cluster).next()
}
fn fallback<'a>(
&'a self,
_info: &'a RoutingInfo,
cluster: &'a ClusterData,
) -> scylla::load_balancing::FallbackPlan<'a> {
let fav_dc_nodes = cluster
.replica_locator()
.unique_nodes_in_datacenter_ring(&self.fav_datacenter_name);
match fav_dc_nodes {
Some(nodes) => Box::new(nodes.iter().map(with_random_shard)),
// If there is no dc with provided name, fallback to other datacenters
None => Box::new(cluster.get_nodes_info().iter().map(with_random_shard)),
}
}
fn name(&self) -> String {
"CustomPolicy".to_string()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
let custom_load_balancing = CustomLoadBalancingPolicy {
fav_datacenter_name: "PL".to_string(),
};
let profile = ExecutionProfile::builder()
.load_balancing_policy(Arc::new(custom_load_balancing))
.build();
let _session: Session = SessionBuilder::new()
.known_node(uri)
.default_execution_profile_handle(profile.into_handle())
.build()
.await?;
Ok(())
}