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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
mod pool;
mod request;
mod handshake;

use {Client, InnerConfig, Config, Connection, Document, IntoArg, Opts, DEFAULT_PORT,
        Request, Response, Result, Run, Server, Session, SessionManager, r2d2};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use errors::*;
use futures::{Async, Poll, Stream, StreamExt};
use futures::task::Context;
use futures::channel::mpsc;
use futures::executor::block_on;
use indexmap::IndexMap;
use parking_lot::RwLock;
use protobuf::ProtobufEnum;
use ql2::proto::Query_QueryType as QueryType;
use reql_types::{Change, ServerStatus};
use serde::de::DeserializeOwned;
use slog::Logger;
use std::{error, thread};
use std::cmp::Ordering;
use std::io::{self, Read, Write};
use std::net::SocketAddr;
use std::net::TcpStream;
use std::time::{Duration, Instant};
use uuid::Uuid;

lazy_static! {
    static ref CONFIG: RwLock<IndexMap<Connection, InnerConfig>> = RwLock::new(IndexMap::new());
    static ref POOL: RwLock<IndexMap<Connection, r2d2::Pool<SessionManager>>> = RwLock::new(IndexMap::new());
}

const CHANNEL_SIZE: usize = 1024;

pub fn connect<'a>(client: &Client, cfg: Config<'a>) -> Result<Connection> {
    if let Err(ref error) = client.term {
        return Err(error.clone());
    }
    let conn = Connection(Uuid::new_v4());
    let logger = client.logger.new(o!("command" => "connect"));
    //let query = format!("{}.connect({})", client.query, arg.string);
    //debug!(logger, "{}", query);
    info!(logger, "creating connection pool...");
    conn.set_config(cfg, logger.clone())?;
    conn.set_latency()?;
    let session = SessionManager(conn);
    let r2d2 = r2d2::Pool::builder()
        .max_size(144)
        .idle_timeout(Some(Duration::from_secs(30)))
        .max_lifetime(Some(Duration::from_secs(150)))
        .min_idle(Some(5))
        .connection_timeout(Duration::from_secs(3))
        .build(session)?;
    conn.set_pool(r2d2);
    info!(logger, "connection pool created successfully");
    conn.maintain();
    Ok(conn)
}

impl<A: IntoArg> Run<A> for Client {
    fn run<T: DeserializeOwned + Send + 'static>(&self, args: A) -> Result<Response<T>> {
        let cterm = match self.term {
            Ok(ref term) => term.clone(),
            Err(ref error) => {
                return Err(error.clone());
            }
        };
        let arg = args.into_arg();
        let aterm = arg.term?;
        let logger = self.logger.new(o!("command" => "run"));
        let query = format!("{}.run({})", self.query, arg.string);
        debug!(logger, "{}", query);
        let conn = match arg.pool {
            Some(conn) => conn.clone(),
            None => {
                let msg = String::from("`run` requires a connection");
                return Err(DriverError::Other(msg))?;
            }
        };
        let pool = match POOL.read().get(&conn) {
            Some(pool) => pool.clone(),
            None => {
                let msg = String::from("bug: connection not in POOL");
                return Err(DriverError::Other(msg))?;
            }
        };
        let cfg = match CONFIG.read().get(&conn) {
            Some(cfg) => cfg.clone(),
            None => {
                return Err(io_error("a tokio handle is required"))?;
            }
        };
        let (tx, rx) = mpsc::channel(CHANNEL_SIZE);
        // @TODO spawning a thread per query is less than ideal. Ideally we will
        // need first class support for Tokio to get rid of this.
        ::std::thread::spawn(move || {
            let req = Request {
                term: cterm,
                opts: aterm,
                pool: pool,
                cfg: cfg,
                tx: tx,
                write: true,
                retry: false,
                logger: logger,
            };
            req.submit();
        });
        Ok(Response {
               done: false,
               rx: rx,
           })
    }
}

impl<T: DeserializeOwned + Send> Stream for Response<T> {
    type Item = Option<Document<T>>;
    type Error = Error;

    fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Self::Item>, Self::Error> {
        if self.done {
            return Ok(Async::Ready(None));
        }
        match self.rx.poll_next(cx) {
            Ok(Async::Pending) => Ok(Async::Pending),
            Ok(Async::Ready(Some(res))) => {
                match res {
                    Ok(data) => Ok(Async::Ready(Some(data))),
                    Err(error) => Err(error),
                }
            }
            Ok(Async::Ready(None)) => {
                self.done = true;
                Ok(Async::Ready(None))
            }
            Err(_) => {
                self.done = true;
                let msg = String::from("an error occured while processing the stream");
                Err(DriverError::Other(msg))?
            }
        }
    }
}

fn io_error<T>(err: T) -> io::Error
    where T: Into<Box<error::Error + Send + Sync>>
{
    io::Error::new(io::ErrorKind::Other, err)
}

impl<'a> Default for Config<'a> {
    fn default() -> Config<'a> {
        let ip4 = format!("127.0.0.1:{}", DEFAULT_PORT);
        let ip6 = format!("[::1]:{}", DEFAULT_PORT);
        Config {
            servers: vec![
                ip4.parse().unwrap(),
                ip6.parse().unwrap(),
            ],
            db: "test",
            user: "admin",
            password: "",
            // @TODO number of retries doesn't mean much
            // let's use a timeout instead and make it an
            // option in both connect and run. The connect
            // one will be the user default and the run one
            // will have the highest precedence. Also let's
            // call it `retry_timeout` to communicate clearly
            // what it does.
            retries: 5,
            #[cfg(feature = "tls")]
            tls: None,
        }
    }
}

impl Ord for Server {
    fn cmp(&self, other: &Server) -> Ordering {
        self.latency.cmp(&other.latency)
    }
}

impl PartialOrd for Server {
    fn partial_cmp(&self, other: &Server) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Server {
    fn eq(&self, other: &Server) -> bool {
        self.latency == other.latency
    }
}

impl Connection {
    fn set_config<'a>(&self, cfg: Config<'a>, logger: Logger) -> Result<()> {
        let opts = Opts {
            db: cfg.db.into(),
            user: cfg.user.into(),
            password: cfg.password.into(),
            retries: cfg.retries,
            reproducible: false,
            #[cfg(feature = "tls")]
            tls: cfg.tls,
        };

        let mut cluster = IndexMap::new();
        let host = String::from("initial-cluster");
        let server = Server::new(&host, cfg.servers);
        cluster.insert(host, server);

        CONFIG
            .write()
            .insert(*self,
                    InnerConfig {
                        cluster: cluster,
                        opts: opts,
                        logger: logger,
                    });

        Ok(())
    }

    fn maintain(&self) {
        self.reset_cluster();
        let conn = *self;
        let (tx, rx) = ::std::sync::mpsc::sync_channel::<()>(CHANNEL_SIZE);
        thread::spawn(move || {
            let r = Client::new();
            let query = r.db("rethinkdb")
                .table("server_status")
                .changes()
                .with_args(args!({include_initial: true}));
            let mut send = true;
            loop {
                let changes = query
                    .run::<Change<ServerStatus, ServerStatus>>(conn)
                    .unwrap();
                let future = changes.for_each(|change| {
                    if let Some(Document::Expected(change)) = change {
                        if let Some(ref mut config) = CONFIG.write().get_mut(&conn) {
                            let cluster = &mut config.cluster;
                            if let Some(status) = change.new_val {
                                let mut addresses = Vec::new();
                                for addr in status.network.canonical_addresses {
                                    let socket = SocketAddr::new(addr.host,
                                                                 status.network.reql_port);
                                    addresses.push(socket);
                                }
                                let mut server = Server::new(&status.name, addresses);
                                server.set_latency();
                                cluster.insert(server.name.to_owned(), server);
                                if send {
                                    if let Ok(_) = tx.send(()) {
                                        send = false;
                                    }
                                }
                            } else if let Some(status) = change.old_val {
                                cluster.remove(&status.name);
                            }
                        }
                    }
                    Ok(())
                });
                let _ = block_on(future);
                thread::sleep(Duration::from_millis(500));
            }
        });
        // wait for at least one database result before continuing
        let _ = rx.recv();
    }

    fn reset_cluster(&self) {
        if let Some(ref mut config) = CONFIG.write().get_mut(self) {
            config.cluster = IndexMap::new();
        }
    }

    fn set_latency(&self) -> Result<()> {
        match CONFIG.write().get_mut(self) {
            Some(ref mut config) => {
                for server in config.cluster.values_mut() {
                    server.set_latency();
                }
                Ok(())
            }
            None => {
                let msg = String::from("conn.set_latency() called before setting configuration");
                Err(DriverError::Other(msg))?
            }
        }
    }

    fn config(&self) -> InnerConfig {
        CONFIG.read().get(self).unwrap().clone()
    }

    fn set_pool(&self, pool: r2d2::Pool<SessionManager>) {
        POOL.write().insert(*self, pool);
    }
}

impl Server {
    fn new(host: &str, addresses: Vec<SocketAddr>) -> Server {
        Server {
            name: host.to_string(),
            addresses: addresses,
            latency: Duration::from_millis(u64::max_value()),
        }
    }

    fn set_latency(&mut self) {
        for address in self.addresses.iter() {
            let start = Instant::now();
            if let Ok(_) = TcpStream::connect(address) {
                self.latency = start.elapsed();
                break;
            }
        }
    }
}

fn write_query(conn: &mut Session, query: &str) -> Result<()> {
    let query = query.as_bytes();
    let token = conn.id;
    if let Err(error) = conn.stream.write_u64::<LittleEndian>(token) {
        conn.broken = true;
        return Err(io_error(error))?;
    }
    if let Err(error) = conn.stream.write_u32::<LittleEndian>(query.len() as u32) {
        conn.broken = true;
        return Err(io_error(error))?;
    }
    if let Err(error) = conn.stream.write_all(query) {
        conn.broken = true;
        return Err(io_error(error))?;
    }
    if let Err(error) = conn.stream.flush() {
        conn.broken = true;
        return Err(io_error(error))?;
    }
    Ok(())
}

fn read_query(conn: &mut Session) -> Result<Vec<u8>> {
    let _ = match conn.stream.read_u64::<LittleEndian>() {
        Ok(token) => token,
        Err(error) => {
            conn.broken = true;
            return Err(io_error(error))?;
        }
    };
    let len = match conn.stream.read_u32::<LittleEndian>() {
        Ok(len) => len,
        Err(error) => {
            conn.broken = true;
            return Err(io_error(error))?;
        }
    };
    let mut resp = vec![0u8; len as usize];
    if let Err(error) = conn.stream.read_exact(&mut resp) {
        conn.broken = true;
        return Err(io_error(error))?;
    }
    Ok(resp)
}

fn wrap_query(query_type: QueryType, query: Option<String>, options: Option<String>) -> String {
    let mut qry = format!("[{}", query_type.value());
    if let Some(query) = query {
        qry.push_str(&format!(",{}", query));
    }
    if let Some(options) = options {
        qry.push_str(&format!(",{}", options));
    }
    qry.push_str("]");
    qry
}