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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::fmt;

use crate::{
    block::Block,
    error::{Error, Result},
    pool::{Pool, PoolBinding, PoolConfig},
    query::{block_stream::BlockStream, *},
    stream::ConnectingStream,
    transport::ClickhouseTransport,
    types::{Cmd, Packet, ServerInfo},
};
use futures_core::{future::BoxFuture, stream::BoxStream};
use futures_util::{FutureExt, StreamExt};
use log::{info, warn};

/// Retry guard max attempts
const MAX_RETRY_ATTEMTS: usize = 3;

pub(crate) const PING_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);
/// Retry guard timeout between attempts
pub(crate) const RETRY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);

pub struct Client {
    _private: std::marker::PhantomData<()>,
}

#[derive(Clone)]
pub(crate) struct Context {
    pub(crate) server_info: ServerInfo,
    pub(crate) hostname: String,
    pub(crate) config: PoolConfig,
}

impl Default for Context {
    fn default() -> Self {
        Self {
            server_info: ServerInfo::default(),
            hostname: hostname::get().unwrap().into_string().unwrap(),
            config: PoolConfig::default(),
        }
    }
}

/// Clickhouse client handle.
pub struct ClientHandle {
    pub(crate) inner: Option<ClickhouseTransport>,
    pub(crate) context: Context,
    pub(crate) pool: PoolBinding,
}

impl fmt::Debug for ClientHandle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ClientHandle")
            .field("server_info", &self.context.server_info)
            .finish()
    }
}

impl Client {
    pub(crate) async fn open(config: PoolConfig, pool: Option<Pool>) -> Result<ClientHandle> {
        let timeout = match config.connection_timeout {
            Some(timeout) => timeout,
            None => {
                return Err(Error::Other(
                    "Connection timeout was not set on `PoolConfig`".into(),
                ))
            }
        };

        let context = Context {
            config: config.clone(),
            ..Default::default()
        };

        with_timeout(
            async move {
                let addr = match &pool {
                    None => &config.addr,
                    Some(p) => p.get_addr(),
                };

                info!("try to connect to {}", addr);
                if addr.port() == Some(8123) {
                    warn!("You should use port 9000 instead of 8123 because clickhouse-rs work through the binary interface.");
                }
                let mut stream = ConnectingStream::new(addr, &config).await?;
                stream.set_nodelay(true)?;

                let transport = ClickhouseTransport::new(stream, pool.clone());

                let mut handle = ClientHandle {
                    inner: Some(transport),
                    pool: match pool {
                        None => PoolBinding::None,
                        Some(p) => PoolBinding::Detached(p),
                    },
                    context
                };

                handle.hello().await?;
                Ok(handle)
            },
            timeout,
        )
        .await
    }
}

impl ClientHandle {
    async fn hello(&mut self) -> Result<()> {
        let context = self.context.clone();
        info!(
            "[hello] -> \n{:?}\n{:?}\n{:?}\n{:?}\n{:?}",
            &context.hostname,
            &context.server_info,
            &context.config.addr.host(),
            &context.config.database,
            &context.config.username,
        );

        let mut h = None;
        let mut info = None;
        let mut stream = self.inner.take().unwrap().call(Cmd::Hello(context.clone()));

        while let Some(packet) = stream.next().await {
            match packet {
                Ok(Packet::Hello(inner, server_info)) => {
                    info!("[hello] <- {:?}", &server_info);
                    h = Some(inner);
                    info = Some(server_info);
                }
                Ok(Packet::Exception(e)) => return Err(Error::Server(e)),
                Err(e) => return Err(Error::IO(e)),
                _ => {}
            }
        }

        self.inner = h;
        self.context.server_info = info.unwrap();
        Ok(())
    }

    async fn ping(&mut self) -> Result<()> {
        with_timeout(
            async move {
                info!("[ping]");

                let mut h = None;

                let transport = self.inner.take().unwrap().clear().await?;
                let mut stream = transport.call(Cmd::Ping);

                while let Some(packet) = stream.next().await {
                    match packet {
                        Ok(Packet::Pong(inner)) => {
                            info!("[pong]");
                            h = Some(inner);
                        }
                        Ok(Packet::Exception(e)) => return Err(Error::Server(e)),
                        Err(e) => return Err(Error::IO(e)),
                        _ => {}
                    }
                }

                self.inner = h;
                Ok(())
            },
            PING_TIMEOUT,
        )
        .await
    }

    /// Executes Clickhouse `query` on Conn.
    pub fn query<Q>(&mut self, sql: Q) -> QueryResult
    where
        Query: From<Q>,
    {
        let query = Query::from(sql);
        QueryResult {
            client: self,
            query,
        }
    }

    pub(crate) fn wrap_stream<'a, F>(&'a mut self, f: F) -> BoxStream<'a, Result<Block>>
    where
        F: (FnOnce(&'a mut Self) -> BlockStream<'a>) + Send + 'static,
    {
        let fut: BoxFuture<'a, BoxStream<'a, Result<Block>>> = Box::pin(async move {
            let inner: BoxStream<'a, Result<Block>> = match self.check_connection().await {
                Ok(_) => Box::pin(f(self)),
                Err(err) => Box::pin(futures_util::stream::once(futures_util::future::err(err))),
            };
            inner
        });

        Box::pin(fut.flatten_stream())
    }

    /// Check connection and try to reconnect if necessary.
    async fn check_connection(&mut self) -> Result<()> {
        self.pool.detach();

        let source = self.context.config.clone();
        let pool = self.pool.clone();

        retry(self, &source, pool.into()).await?;

        if !self.pool.is_attached() && self.pool.is_some() {
            self.pool.attach();
        }

        Ok(())
    }

    /// Switch Transport AtomicUsize status on takeing/returning connection
    pub(crate) fn set_inside(&self, value: bool) {
        if let Some(ref inner) = self.inner {
            inner.set_inside(value);
        } else {
            unreachable!()
        }
    }
}

pub(crate) async fn retry(
    handle: &mut ClientHandle,
    source: &PoolConfig,
    pool: Option<Pool>,
) -> Result<()> {
    let mut attempt = 0;
    let mut skip_check = false;

    loop {
        if skip_check {
            skip_check = false;
        } else {
            match handle.ping().await {
                Ok(()) => return Ok(()),
                Err(err) => {
                    if attempt >= MAX_RETRY_ATTEMTS {
                        return Err(err);
                    }
                }
            }
        }

        match reconnect(handle, source, pool.clone()).await {
            Ok(()) => continue,
            Err(err) => {
                skip_check = true;
                if attempt >= MAX_RETRY_ATTEMTS {
                    return Err(err);
                }

                tokio::time::sleep(RETRY_TIMEOUT).await;
            }
        }

        attempt += 1;
    }
}

async fn reconnect(conn: &mut ClientHandle, source: &PoolConfig, pool: Option<Pool>) -> Result<()> {
    warn!("[reconnect]");
    let mut new_conn = match pool {
        None => Client::open(source.clone(), pool).await?,
        Some(p) => p.get_handle().await?,
    };
    std::mem::swap(conn, &mut new_conn);
    Ok(())
}

pub(crate) async fn with_timeout<F, T>(future: F, timeout: std::time::Duration) -> F::Output
where
    F: std::future::Future<Output = crate::error::Result<T>>,
{
    tokio::time::timeout(timeout, future).await?
}