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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::routing::Token;
use crate::transport::connection::Connection;
use crate::transport::connection::VerifiedKeyspaceName;
use crate::transport::connection_pool::{NodeConnectionPool, PoolConfig};
use crate::transport::errors::QueryError;
use std::{
hash::{Hash, Hasher},
net::SocketAddr,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
pub struct Node {
pub address: SocketAddr,
pub datacenter: Option<String>,
pub rack: Option<String>,
pool: NodeConnectionPool,
down_marker: AtomicBool,
}
impl Node {
pub(crate) fn new(
address: SocketAddr,
pool_config: PoolConfig,
datacenter: Option<String>,
rack: Option<String>,
keyspace_name: Option<VerifiedKeyspaceName>,
) -> Self {
let pool =
NodeConnectionPool::new(address.ip(), address.port(), pool_config, keyspace_name);
Node {
address,
datacenter,
rack,
pool,
down_marker: false.into(),
}
}
pub(crate) async fn connection_for_token(
&self,
token: Token,
) -> Result<Arc<Connection>, QueryError> {
self.pool.connection_for_token(token)
}
pub(crate) async fn random_connection(&self) -> Result<Arc<Connection>, QueryError> {
self.pool.random_connection()
}
pub fn is_down(&self) -> bool {
self.down_marker.load(Ordering::Relaxed)
}
pub(crate) fn change_down_marker(&self, is_down: bool) {
self.down_marker.store(is_down, Ordering::Relaxed);
}
pub(crate) async fn use_keyspace(
&self,
keyspace_name: VerifiedKeyspaceName,
) -> Result<(), QueryError> {
self.pool.use_keyspace(keyspace_name).await
}
pub(crate) fn get_working_connections(&self) -> Result<Vec<Arc<Connection>>, QueryError> {
self.pool.get_working_connections()
}
pub(crate) async fn wait_until_pool_initialized(&self) {
self.pool.wait_until_initialized().await
}
}
impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool {
self.address == other.address
}
}
impl Eq for Node {}
impl Hash for Node {
fn hash<H: Hasher>(&self, state: &mut H) {
self.address.hash(state);
}
}