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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use arc_swap::{ArcSwap, AsRaw};
use cassandra_protocol::frame::{Frame, Version};
use cassandra_protocol::query::utils::quote;
use futures::future::{join_all, try_join_all};
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::watch::Receiver;
use tracing::*;

use crate::cluster::topology::NodeDistance;
use crate::cluster::ConnectionManager;
use crate::error::{Error, Result as CdrsResult};
use crate::transport::CdrsTransport;

async fn new_connection<T: CdrsTransport, CM: ConnectionManager<T>>(
    connection_manager: &CM,
    broadcast_rpc_address: SocketAddr,
    timeout: Option<Duration>,
) -> CdrsResult<T> {
    if let Some(timeout) = timeout {
        tokio::time::timeout(
            timeout,
            connection_manager.connection(None, None, broadcast_rpc_address),
        )
        .await
        .map_err(|_| {
            Error::Timeout(format!(
                "Timeout waiting for connection to: {}",
                broadcast_rpc_address
            ))
        })
        .and_then(|result| result)
    } else {
        connection_manager
            .connection(None, None, broadcast_rpc_address)
            .await
    }
}

/// Configuration for node connection pools. By default, the pool size depends on the number of
/// cpu for local nodes and a fixed value for remote, and there is no timeout. If the distance to a
/// given node is unknown, it is treated as remote.
#[derive(Clone, Copy)]
pub struct ConnectionPoolConfig {
    local_size: usize,
    remote_size: usize,
    connect_timeout: Option<Duration>,
}

impl Default for ConnectionPoolConfig {
    fn default() -> Self {
        ConnectionPoolConfig {
            local_size: 1,
            remote_size: 1,
            connect_timeout: None,
        }
    }
}

impl ConnectionPoolConfig {
    /// Creates a new configuration for a pool of given size, with optional connect timeout.
    pub fn new(local_size: usize, remote_size: usize, connect_timeout: Option<Duration>) -> Self {
        assert!(local_size > 0 && remote_size > 0);
        ConnectionPoolConfig {
            local_size,
            remote_size,
            connect_timeout,
        }
    }
}

/// Factory for node connection pools.
pub struct ConnectionPoolFactory<T: CdrsTransport + 'static, CM: ConnectionManager<T> + 'static> {
    config: ConnectionPoolConfig,
    version: Version,
    connection_manager: Arc<CM>,
    keyspace_receiver: Receiver<Option<String>>,
    _transport: PhantomData<T>,
}

impl<T: CdrsTransport, CM: ConnectionManager<T>> ConnectionPoolFactory<T, CM> {
    pub fn new(
        config: ConnectionPoolConfig,
        version: Version,
        connection_manager: CM,
        keyspace_receiver: Receiver<Option<String>>,
    ) -> Self {
        ConnectionPoolFactory {
            config,
            version,
            connection_manager: Arc::new(connection_manager),
            keyspace_receiver,
            _transport: Default::default(),
        }
    }

    #[inline]
    pub fn connection_manager(&self) -> &CM {
        self.connection_manager.as_ref()
    }

    pub async fn create(
        &self,
        node_distance: NodeDistance,
        broadcast_rpc_address: SocketAddr,
    ) -> CdrsResult<Arc<ConnectionPool<T, CM>>> {
        let pool = Arc::new(
            ConnectionPool::new(
                self.connection_manager.clone(),
                broadcast_rpc_address,
                node_distance,
                self.config,
            )
            .await?,
        );

        // watch for keyspace changes
        let mut keyspace_receiver = self.keyspace_receiver.clone();
        let pool_clone = pool.clone();
        let version = self.version;

        tokio::spawn(async move {
            while let Ok(()) = keyspace_receiver.changed().await {
                let keyspace = keyspace_receiver.borrow().clone();
                if let Some(keyspace) = keyspace {
                    let use_frame = Arc::new(Frame::new_req_query(
                        format!("USE {}", quote(&keyspace)),
                        Default::default(),
                        None,
                        false,
                        None,
                        None,
                        None,
                        None,
                        Default::default(),
                        version,
                    ));

                    join_all(pool_clone.pool.iter()
                        .map(|connection| connection.load().clone())
                        .filter(|connection| !connection.is_broken())
                        .map(|connection| {
                            let use_frame = use_frame.clone();
                            async move {
                                if let Err(error) = connection.write_frame(use_frame.as_ref()).await {
                                    error!(%error, %broadcast_rpc_address, "Error settings keyspace for connection!");
                                }
                            }
                        })).await;
                }
            }
        });

        Ok(pool)
    }
}

/// Node connection pool.
pub struct ConnectionPool<T: CdrsTransport, CM: ConnectionManager<T>> {
    connection_manager: Arc<CM>,
    broadcast_rpc_address: SocketAddr,
    config: ConnectionPoolConfig,
    pool: Vec<ArcSwap<T>>,
    current_index: AtomicUsize,
}

impl<T: CdrsTransport, CM: ConnectionManager<T>> ConnectionPool<T, CM> {
    async fn new(
        connection_manager: Arc<CM>,
        broadcast_rpc_address: SocketAddr,
        node_distance: NodeDistance,
        config: ConnectionPoolConfig,
    ) -> CdrsResult<Self> {
        let size = if node_distance == NodeDistance::Local {
            config.local_size
        } else {
            config.remote_size
        };

        // initialize the pool
        let pool = try_join_all((0..size).into_iter().map(|_| {
            new_connection(
                connection_manager.as_ref(),
                broadcast_rpc_address,
                config.connect_timeout,
            )
        }))
        .await?
        .into_iter()
        .map(ArcSwap::from_pointee)
        .collect();

        Ok(ConnectionPool {
            connection_manager,
            broadcast_rpc_address,
            config,
            pool,
            current_index: AtomicUsize::new(0),
        })
    }

    #[inline]
    pub async fn connection(&self) -> CdrsResult<Arc<T>> {
        let index = self.current_index.fetch_add(1, Ordering::Relaxed) % self.pool.len();
        let slot = &self.pool[index];

        let connection = slot.load().clone();
        if !connection.is_broken() {
            return Ok(connection);
        }

        debug!("Establishing new connection...");

        let new_connection = Arc::new(
            new_connection(
                self.connection_manager.as_ref(),
                self.broadcast_rpc_address,
                self.config.connect_timeout,
            )
            .await?,
        );

        // another thread might have already updated this slot, so try once and use current one
        let previous = slot.compare_and_swap(&connection, new_connection.clone());

        Ok(if previous.as_raw() == (&connection).as_raw() {
            new_connection
        } else {
            previous.clone()
        })
    }
}