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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! proxy is the mod which contains genneral proxy

pub mod fnv;
mod handler;
pub mod ketama;
mod node;
mod ping;

use self::fnv::Fnv1a64;
use self::handler::Handle;
use self::ketama::HashRing;
use self::node::spawn_node;
use self::ping::Ping;

use crate::com::*;
use crate::ClusterConfig;

use futures::lazy;
use futures::task::Task;
use futures::unsync::mpsc::{channel, Sender};
use futures::{AsyncSink, Future, Sink, Stream};
use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::current_thread;
use tokio_codec::{Decoder, Encoder};

use std::cell::RefCell;
use std::collections::VecDeque;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::Hasher;
use std::net::SocketAddr;
use std::rc::Rc;

fn spawn_ping<T: Request + 'static>(proxy: Rc<Proxy<T>>) -> Result<(), ()> {
    if let Some(limit) = proxy.cc.ping_fail_limit {
        info!("setup ping for cluster {}", proxy.cc.name);
        // default ping interval 500
        let interval = proxy.cc.ping_interval.map(|x| x as u64).unwrap_or(500u64);

        let addrs = proxy.spots.keys().cloned();
        for addr in addrs {
            let ping = Ping::new(proxy.clone(), addr, limit, interval);
            current_thread::spawn(ping);
        }
    } else {
        debug!("skip proxy ping feature for cluster {}", proxy.cc.name);
    }
    Ok(())
}

fn start_listen<T: Request + 'static>(p: (Rc<Proxy<T>>, TcpListener)) -> Result<Rc<Proxy<T>>, ()> {
    let (proxy, listen) = p;
    let rc_proxy = proxy.clone();
    let amt = listen
        .incoming()
        .for_each(move |sock| {
            accept_conn(rc_proxy.clone(), sock);
            Ok(())
        })
        .map_err(|err| {
            error!("fail to start_cluster due {:?}", err);
        });
    current_thread::spawn(amt);
    Ok(proxy.clone())
}

fn accept_conn<T: Request + 'static>(proxy: Rc<Proxy<T>>, sock: TcpStream) {
    let sock = set_read_write_timeout(sock, proxy.cc.read_timeout, proxy.cc.write_timeout)
        .expect("set read/write timeout in proxy frontend must be ok");

    sock.set_nodelay(true).expect("set nodelay must ok");
    let codec = T::handle_codec();
    let (req_tx, req_rx) = codec.framed(sock).split();

    let proxy = proxy.clone();
    let handle = Handle::new(
        proxy,
        req_rx.map_err(|_err| {
            error!("fail to recv from upstream handle rx");
            Error::Critical
        }),
        req_tx,
    )
    .map_err(|err| {
        error!("get handle error due {:?}", err);
    });
    current_thread::spawn(handle);
}

pub fn start_proxy<T: Request + 'static>(proxy: Proxy<T>) {
    let addr = proxy
        .cc
        .listen_addr
        .clone()
        .parse::<SocketAddr>()
        .expect("parse socket never fail");

    let fut = lazy(move || -> Result<(SocketAddr, Proxy<T>), ()> { Ok((addr, proxy)) })
        .and_then(|(addr, proxy)| {
            let listen = create_reuse_port_listener(&addr).expect("bind never fail");
            Ok((Rc::new(proxy), listen))
        })
        .and_then(|(proxy, listen)| {
            proxy.init_conns().map_err(|err| {
                error!("fail to create init proxy connections due to {:?}", err);
            })?;
            Ok((proxy, listen))
        })
        .and_then(start_listen)
        .and_then(spawn_ping);

    current_thread::block_on_all(fut).unwrap();
}

#[allow(unused)]
struct ServerLine {
    addr: String,
    weight: usize,
    alias: Option<String>,
}

impl ServerLine {
    fn parse_servers(servers: &[String]) -> AsResult<Vec<ServerLine>> {
        // e.g.: 192.168.1.2:1074:10 redis-20
        let mut sl = Vec::with_capacity(servers.len());
        for server in servers {
            let mut iter = server.split(' ');
            let first_part = iter.next().expect("first partation must exists");
            if first_part.chars().filter(|x| *x == ':').count() == 1 {
                let alias = iter.next().map(|x| x.to_string());
                sl.push(ServerLine {
                    addr: first_part.to_string(),
                    weight: 1,
                    alias,
                });
            }

            let mut fp_sp = first_part.rsplitn(2, ':').filter(|x| !x.is_empty());
            let weight = {
                let weight_str = fp_sp.next().unwrap_or("1");
                weight_str.parse::<usize>()?
            };
            let addr = fp_sp.next().expect("addr never be absent").to_owned();
            drop(fp_sp);
            let alias = iter.next().map(|x| x.to_string());
            sl.push(ServerLine {
                addr,
                weight,
                alias,
            });
        }
        Ok(sl)
    }

    fn unwrap_spot(sls: &[ServerLine]) -> (Vec<String>, Vec<String>, Vec<usize>) {
        let mut nodes = Vec::new();
        let mut alias = Vec::new();
        let mut weights = Vec::new();
        for sl in sls {
            if sl.alias.is_some() {
                alias.push(
                    sl.alias
                        .as_ref()
                        .cloned()
                        .expect("node addr can't be empty"),
                );
            }
            nodes.push(sl.addr.clone());
            weights.push(sl.weight);
        }
        (nodes, alias, weights)
    }
}

pub struct Proxy<T: Request> {
    pub cc: ClusterConfig,

    spots: HashMap<String, usize>,
    alias: RefCell<HashMap<String, String>>,

    ring: RefCell<HashRing<Fnv1a64>>,
    conns: RefCell<HashMap<String, Sender<T>>>,
    is_alias: bool,
    hash_tag: Vec<u8>,
}

impl<T: Request + 'static> Proxy<T> {
    pub fn new(cc: ClusterConfig) -> AsResult<Proxy<T>> {
        let sls = ServerLine::parse_servers(&cc.servers)?;
        let (addrs, alias, weights) = ServerLine::unwrap_spot(&sls);
        let ring = if alias.is_empty() {
            HashRing::<Fnv1a64>::new(addrs.clone(), weights.clone())?
        } else {
            HashRing::<Fnv1a64>::new(alias.clone(), weights.clone())?
        };
        let alias_map: HashMap<_, _> = alias
            .iter()
            .enumerate()
            .map(|(index, alias_name)| (alias_name.clone(), addrs[index].clone()))
            .collect();

        let spots: HashMap<_, _> = if alias.is_empty() {
            addrs.iter()
        } else {
            alias.iter()
        }
        .zip(weights.iter())
        .map(|(x, y)| (x.clone(), *y))
        .collect();

        let hash_tag = cc
            .hash_tag
            .as_ref()
            .cloned()
            .unwrap_or_else(|| "".to_string())
            .as_bytes()
            .to_vec();

        Ok(Proxy {
            cc,
            spots,
            hash_tag,
            ring: RefCell::new(ring),
            conns: RefCell::new(HashMap::new()),
            is_alias: !alias_map.is_empty(),
            alias: RefCell::new(alias_map),
        })
    }

    fn reconnect(&self, node: &str) -> AsResult<()> {
        let conn = Self::create_conn(node, self.cc.read_timeout, self.cc.write_timeout)?;
        if let Some(mut old) = self.conns.borrow_mut().insert(node.to_string(), conn) {
            old.close().map_err(|err| {
                error!("force done for replaced data");
                let req = err.into_inner();
                req.done_with_error(Error::ClusterDown);
                Error::Critical
            })?;
        }
        Ok(())
    }

    fn init_conns(&self) -> AsResult<()> {
        for server in self.alias.borrow().values().map(|x| x.to_string()) {
            let conn = Self::create_conn(&server, self.cc.read_timeout, self.cc.write_timeout)?;
            self.conns.borrow_mut().insert(server.clone(), conn);
        }
        Ok(())
    }

    fn create_conn(node: &str, rt: Option<u64>, wt: Option<u64>) -> AsResult<Sender<T>> {
        let node_addr = node.to_string();
        let (tx, rx) = channel(1024 * 8);
        let ret_tx = tx.clone();
        let amt = lazy(|| -> Result<(), ()> { Ok(()) })
            .and_then(move |_| {
                node_addr
                    .as_str()
                    .parse()
                    .map_err(|err| error!("fail to parse addr {:?}", err))
            })
            .and_then(|addr| {
                TcpStream::connect(&addr).map_err(|err| error!("fail to connect {:?}", err))
            })
            .and_then(move |sock| {
                let sock = set_read_write_timeout(sock, rt, wt).expect("set timeout must be ok");

                sock.set_nodelay(true).expect("set nodelay must ok");
                let codec = <T as Request>::node_codec();
                let (sink, stream) = codec.framed(sock).split();
                let arx = rx.map_err(|err| {
                    info!("fail to send due to {:?}", err);
                    Error::Critical
                });
                let (nd, nr) = spawn_node(arx, sink, stream);
                current_thread::spawn(nd);
                current_thread::spawn(nr);
                Ok(())
            });
        current_thread::spawn(amt);
        Ok(ret_tx)
    }

    fn add_node(&self, node: &str) {
        if let Some(spot) = self.spots.get(node) {
            self.ring.borrow_mut().add_node(node.to_string(), *spot);
        }
    }

    fn del_node(&self, node: &str) {
        self.ring.borrow_mut().del_node(node);
    }

    fn trans_alias(&self, who: &str) -> String {
        if self.is_alias {
            if let Some(addr) = self.alias.borrow().get(who) {
                return addr.to_string();
            }
        }
        who.to_string()
    }

    fn execute(&self, who: &str, req: T) -> Result<AsyncSink<T>, Error> {
        let node = self.trans_alias(who);

        let rslt = {
            let mut conns = self.conns.borrow_mut();
            let conn = conns
                .get_mut(&node)
                .expect("start_send conn must be exists");
            conn.start_send(req)
        };
        match rslt {
            Ok(AsyncSink::Ready) => match self
                .conns
                .borrow_mut()
                .get_mut(&node)
                .expect("complte conn must be exists")
                .poll_complete()
            {
                Ok(_) => Ok(AsyncSink::Ready),
                Err(err) => {
                    self.reconnect(&node)?;
                    error!("execute fail to poll_complete due to {:?}", err);
                    let req = err.into_inner();
                    req.done_with_error(Error::ClusterDown);
                    Err(Error::Critical)
                }
            },
            Ok(AsyncSink::NotReady(r)) => Ok(AsyncSink::NotReady(r)),
            Err(err) => {
                self.reconnect(&node)?;
                error!("fail to execute to backend due to {:?}", err);
                let req = err.into_inner();
                req.done_with_error(Error::ClusterDown);
                Err(Error::ClusterDown)
            }
        }
    }

    fn dispatch_all(&self, cmds: &mut VecDeque<T>) -> Result<AsyncSink<()>, Error> {
        let mut nodes = HashSet::new();
        loop {
            if let Some(req) = cmds.front().cloned() {
                let hash_code = req.key_hash(&self.hash_tag, fnv1a64);

                let node = {
                    let ring = self.ring.borrow();
                    let pos = ring.get_pos_by_hash(hash_code);
                    let name = ring.get_node_ref_by_pos(pos);
                    self.trans_alias(name)
                };
                let result = {
                    let mut conns = self.conns.borrow_mut();
                    let conn = conns.get_mut(&node).expect("must get the conn");
                    conn.start_send(req.clone()).map_err(|err| {
                        error!("fail to dispatch to backend due to {:?}", err);
                        Error::Critical
                    })
                };

                match result {
                    Ok(AsyncSink::Ready) => {
                        let _ = cmds.pop_front().expect("pop_front never be empty");
                        nodes.insert(node.clone());
                    }
                    Ok(AsyncSink::NotReady(_)) => {
                        break;
                    }
                    Err(err) => {
                        self.reconnect(&node)?;
                        return Err(err);
                    }
                }
            } else {
                return Ok(AsyncSink::Ready);
            }
        }

        if !nodes.is_empty() {
            for node in nodes.into_iter() {
                let rslt = self
                    .conns
                    .borrow_mut()
                    .get_mut(&node)
                    .expect("node connection is never be absent")
                    .poll_complete();
                match rslt {
                    Ok(_) => {}
                    Err(err) => {
                        self.reconnect(&node)?;
                        error!("fail to complete to proxy to backend due to {:?}", err);
                        return Err(Error::ClusterDown);
                    }
                }
            }
        }

        Ok(AsyncSink::Ready)
    }
}

pub trait Request: Sized + Clone + Debug {
    type Reply: Clone + Debug;
    type HandleCodec: Decoder<Item = Self, Error = Error> + Encoder<Item = Self, Error = Error>;
    type NodeCodec: Decoder<Item = Self::Reply, Error = Error> + Encoder<Item = Self, Error = Error>;

    fn ping_request() -> Self;
    fn handle_codec() -> Self::HandleCodec;
    fn node_codec() -> Self::NodeCodec;
    fn reregister(&self, task: Task);

    fn key_hash(&self, hash_tag: &[u8], hasher: fn(&[u8]) -> u64) -> u64;

    fn subs(&self) -> Option<Vec<Self>>;
    fn is_done(&self) -> bool;

    fn valid(&self) -> bool;
    fn done(&self, data: Self::Reply);
    fn done_with_error(&self, err: Error);
}

#[inline]
pub fn trim_hash_tag<'a, 'b>(key: &'a [u8], hash_tag: &'b [u8]) -> &'a [u8] {
    if hash_tag.len() != 2 {
        return key;
    }
    if let Some(begin) = key.iter().position(|x| *x == hash_tag[0]) {
        if let Some(end_offset) = key[begin..].iter().position(|x| *x == hash_tag[1]) {
            return &key[begin + 1..begin + end_offset];
        }
    }
    key
}

fn fnv1a64(data: &[u8]) -> u64 {
    let mut hasher = Fnv1a64::default();
    hasher.write(data);
    hasher.finish()
}

#[cfg(test)]
#[test]
fn test_trim_hash_tag() {
    assert_eq!(trim_hash_tag(b"abc{a}b", b"{}"), b"a");
    assert_eq!(trim_hash_tag(b"abc{ab", b"{}"), b"abc{ab");
    assert_eq!(trim_hash_tag(b"abc{ab}", b"{}"), b"ab");
    assert_eq!(trim_hash_tag(b"abc{ab}asd{abc}d", b"{}"), b"ab");
    assert_eq!(
        trim_hash_tag(b"abc{ab}asd{abc}d", b"{"),
        b"abc{ab}asd{abc}d"
    );
    assert_eq!(trim_hash_tag(b"abc{ab}asd{abc}d", b""), b"abc{ab}asd{abc}d");
    assert_eq!(
        trim_hash_tag(b"abc{ab}asd{abc}d", b"abc"),
        b"abc{ab}asd{abc}d"
    );
    assert_eq!(trim_hash_tag(b"abc{ab}asd{abc}d", b"ab"), b"");
}