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
//! Communication channel between servers
//!
//! This module contains the `Channel` struct and some types interact with it.
//!
//! # Examples
//!
//! ```rust
//! # extern crate copra;
//! # extern crate tokio_core;
//! # use std::error::Error;
//! use copra::ChannelBuilder;
//! use tokio_core::reactor::Core;
//!
//! # fn main() {
//! #     try_main().unwrap();
//! # }
//! # fn try_main() -> Result<(), Box<Error>> {
//! let core = Core::new()?;
//! let builder = ChannelBuilder::single_server("127.0.0.1:8000", core.handle());
//! let channel = core.run(builder.build())?;
//! # }
//! ```

use bytes::Bytes;
use tokio_core::net::TcpStream;
use tokio_core::reactor::Handle;
use tokio_io::AsyncRead;
use tokio_io::codec::Framed;
use tokio_proto::multiplex::ClientProto;
use tokio_proto::TcpClient;
use futures::{Async, Future, IntoFuture, Poll};
use futures::sync::mpsc;
use futures::sync::oneshot;
use std::error::Error;
use std::fmt;
use std::io;
use std::net::{AddrParseError, SocketAddr};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use protocol::{BrpcProtocol, ProtoCodecClient, Protocol, RpcProtocol};
use load_balancer::{CallInfo, ServerEndPort, ServerId};
use load_balancer::single_server::SingleServerLoadBalancer;
use message::{RpcRequestMeta, RpcResponseMeta};

use self::backend::ChannelBackend;
use self::connector::Connector;

mod backend;
pub(crate) mod connector;

/// A future returned by `ChannelBuilder::build` which will resolve to a `Channel`
/// when the channel is ready for use.
pub type ChannelBuildFuture = Box<Future<Item = Channel, Error = ChannelBuildError>>;

// TODO: make this fully public
// TODO: move this to a better place
pub(crate) type RequestPackage = (RpcRequestMeta, Bytes);

pub(crate) type ResponsePackage = (RpcResponseMeta, Bytes);

type FeedbackSender = oneshot::Sender<(ServerId, CallInfo)>;

type FeedbackReceiver = oneshot::Receiver<(ServerId, CallInfo)>;

type OneShotSender = oneshot::Sender<io::Result<(ResponsePackage, FeedbackHandle)>>;

type OneShotReceiver = oneshot::Receiver<io::Result<(ResponsePackage, FeedbackHandle)>>;

type ChannelSender = mpsc::UnboundedSender<(OneShotSender, RequestPackage)>;

type ChannelReceiver = mpsc::UnboundedReceiver<(OneShotSender, RequestPackage)>;

/// The error when building a channel
#[derive(Clone, Debug)]
pub enum ChannelBuildError {
    /// An error occured when parsing `SocketAddr` from string
    AddrParseError(AddrParseError),
    /// Failed to connect to a server or a cluster
    ConnectError,
}

impl fmt::Display for ChannelBuildError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ChannelBuildError::AddrParseError(ref e) => write!(f, "address parse error: {}", e),
            ChannelBuildError::ConnectError => write!(f, "connection error"),
        }
    }
}

impl Error for ChannelBuildError {
    fn description(&self) -> &str {
        match *self {
            ChannelBuildError::AddrParseError(_) => "failed to parse socket address from raw string",
            ChannelBuildError::ConnectError => "failed to connect to a remote server",
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            ChannelBuildError::AddrParseError(ref e) => Some(e),
            ChannelBuildError::ConnectError => None,
        }
    }
}

/// [WIP] The error occured when sending or receiving packages
#[derive(Debug)]
pub enum ChannelError {
    /// Can not issue new request because the number of pending requests has
    /// reached the concurrency limit
    ConcurrencyLimitReached,
    /// Io error from TCP socket
    IoError(io::Error),
    /// [WIP] Other errors that need to be explicated
    UnknownError,
}

impl fmt::Display for ChannelError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ChannelError::ConcurrencyLimitReached => write!(f, "Concurrency limit reached"),
            ChannelError::IoError(ref e) => write!(f, "Io error: {}", e),
            ChannelError::UnknownError => write!(f, "other errors might be worth discussion"),
        }
    }
}

impl Error for ChannelError {
    fn description(&self) -> &str {
        match *self {
            ChannelError::ConcurrencyLimitReached => "concurrency limit reached",
            ChannelError::IoError(_) => "io error from TCP socket",
            ChannelError::UnknownError => "[WIP] other errors",
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            ChannelError::IoError(ref e) => Some(e),
            _ => None,
        }
    }
}

impl From<AddrParseError> for ChannelBuildError {
    fn from(e: AddrParseError) -> Self {
        ChannelBuildError::AddrParseError(e)
    }
}

//TODO: make this private
#[doc(hidden)]
pub struct MetaClientProtocol {
    proto: Box<RpcProtocol>,
    handle: Handle,
    addr: SocketAddr,
}

impl fmt::Debug for MetaClientProtocol {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MetaClientProtocol {{ addr: {:?} }}", self.addr)
    }
}

impl MetaClientProtocol {
    /// Create a new instance.
    pub fn new(proto_type: &Protocol, handle: Handle, addr: SocketAddr) -> Self {
        let proto = match proto_type {
            // TODO: unify construction interface of protocols
            &Protocol::Brpc => Box::new(BrpcProtocol::new()),
            _ => unimplemented!(),
        };
        MetaClientProtocol {
            proto,
            handle,
            addr,
        }
    }
}

impl ClientProto<TcpStream> for MetaClientProtocol {
    type Request = RequestPackage;
    type Response = ResponsePackage;
    type Transport = Framed<Connector, ProtoCodecClient>;
    type BindTransport = Result<Self::Transport, io::Error>;

    fn bind_transport(&self, io: TcpStream) -> Self::BindTransport {
        let conn = Connector::from_stream(self.addr.clone(), io, self.handle.clone());
        let codec = ProtoCodecClient::new(self.proto.new_boxed());
        let framed = conn.framed(codec);
        Ok(framed)
    }
}

#[derive(Debug)]
enum ConnectMode<'a> {
    Single(&'a str),
}

/// Channel factory, which can be used to setup a new channel
///
/// This builder ease the configuration of the channel. You can chain up the configuration methods,
/// and call `build` to consume it, which will return a future that resolves to a `Channel`.
#[derive(Debug)]
pub struct ChannelBuilder<'a> {
    mode: ConnectMode<'a>,
    handle: Handle,
    protocol: Option<Protocol>,
    deadline: Option<Option<Duration>>,
    max_retry: Option<u32>,
    max_concurrency: Option<u32>,
}

impl<'a> ChannelBuilder<'a> {
    /// Connect to a server by IP address.
    ///
    /// This method will create a new channel builder.
    pub fn single_server(addr: &'a str, handle: Handle) -> Self {
        ChannelBuilder {
            mode: ConnectMode::Single(addr),
            handle: handle,
            protocol: None,
            deadline: None,
            max_retry: None,
            max_concurrency: None,
        }
    }

    /// [WIP] Choose a communication protocol.
    ///
    /// This RPC framework is intended to support multiple communication protocols
    /// (e.g. grpc and plain http). Currently, it is recommended to leave this to
    /// default value.
    ///
    /// Default to `brpc` protocol (`brpc` is a pure protobuf message protocol
    /// used in [brpc] framework).
    ///
    /// [brpc]: https://github.com/brpc/brpc
    ///
    pub fn protocol(mut self, protocol: Protocol) -> Self {
        self.protocol = Some(protocol);
        self
    }

    /// [WIP] Set request deadline.
    ///
    /// A request will be set to failed if it reaches its deadline.
    ///
    /// Default to `None`. which means we will wait until the reponse is returned or
    /// some error is raised.
    pub fn deadline(mut self, deadline: Option<Duration>) -> Self {
        self.deadline = Some(deadline);
        self
    }

    /// Set concurrency limit.
    ///
    /// The number of unresolved requests will be confined below `max_concurrency`.
    /// When this limit is reached, new request will fail immidiately. Thus, it can
    /// be used to model backpressure.
    ///
    /// Default to `None`, no limit imposed.
    pub fn max_concurrency(mut self, max_concurrency: u32) -> Self {
        self.max_concurrency = Some(max_concurrency);
        self
    }

    /// Consume the builder and begin to prepare connection.
    ///
    /// This method returns a future that will resolve to a `Channel`.
    ///
    /// # Errors
    /// The future will yield a `ChannelBuildError` if any error occurs when seting
    /// up the connection.
    pub fn build(self) -> ChannelBuildFuture {
        // TODO: use Default trait
        let protocol = self.protocol.unwrap_or(Protocol::Brpc);
        // TODO: add timeout and retry
        let _deadline = self.deadline.unwrap_or(None);
        let max_concurrency = self.max_concurrency.unwrap_or(1_000_000);
        let handle = self.handle;

        let (tx, rx) = mpsc::unbounded();
        let channel = Channel::new(tx, max_concurrency);

        match self.mode {
            ConnectMode::Single(addr) => {
                let parse = addr.parse::<SocketAddr>()
                    .map_err(|e| ChannelBuildError::AddrParseError(e))
                    .into_future();
                let fut = parse.and_then(move |addr| {
                    let proto = MetaClientProtocol::new(&protocol, handle.clone(), addr.clone());
                    TcpClient::new(proto)
                        .connect(&addr, &handle)
                        .map_err(|_| ChannelBuildError::ConnectError)
                        .map(move |service| {
                            let end_port = ServerEndPort::new(service);
                            let lb = SingleServerLoadBalancer::new(end_port);
                            let backend = ChannelBackend::new(rx, handle.clone(), lb);
                            handle.spawn(backend);
                            channel
                        })
                });
                Box::new(fut)
            }
        }
    }
}

/// A future used internally by the framework. It will resolve to a serialized response.
#[derive(Debug)]
pub struct ChannelFuture {
    rx: Option<OneShotReceiver>,
    counter: Arc<AtomicUsize>,
}

impl ChannelFuture {
    /// Create a new future, used internally
    pub fn new(rx: Option<OneShotReceiver>, counter: Arc<AtomicUsize>) -> Self {
        ChannelFuture { rx, counter }
    }
}

impl Future for ChannelFuture {
    type Item = (ResponsePackage, FeedbackHandle);

    type Error = ChannelError;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        if let Some(ref mut rx) = self.rx {
            let result = try_ready!(
                rx.poll()
                    .map_err(|_| panic!("The sending end of the oneshot is dropped"))
            );
            self.counter.fetch_sub(1, Ordering::Relaxed);

            result
                .map_err(|e| ChannelError::IoError(e))
                .map(|resp| Async::Ready(resp))
        } else {
            Err(ChannelError::ConcurrencyLimitReached)
        }
    }
}

/// Communication channel between servers
///
/// The `Channel` implements `Clone`, `Send`, and `Sync`. Once a channel is create from
/// a `ChannelBuilder`, it can be cloned and used in multiple threads.
#[derive(Clone, Debug)]
pub struct Channel {
    sender: ChannelSender,
    counter: Arc<AtomicUsize>,
    max_concurrency: usize,
}

impl Channel {
    /// Create a new channel.
    ///
    /// This method is used by `ChannelBuilder`.
    pub fn new(sender: ChannelSender, max_concurrency: u32) -> Self {
        Channel {
            sender,
            counter: Arc::new(AtomicUsize::new(0)),
            max_concurrency: max_concurrency as usize,
        }
    }

    /// Issue a request.
    ///
    /// This method deals with serialized, untyped message. It is meaned to be used
    /// internally by the framework. More ergonomic interfaces are provided by the 
    /// auto-generated stubs.
    pub fn call(&self, req: RequestPackage) -> ChannelFuture {
        let (tx, rx) = oneshot::channel();
        let rx = if self.counter.load(Ordering::SeqCst) < self.max_concurrency {
            self.counter.fetch_add(1, Ordering::SeqCst);
            self.sender
                .unbounded_send((tx, req))
                .expect("The receiving end is dropped");
            Some(rx)
        } else {
            None
        };

        ChannelFuture::new(rx, self.counter.clone())
    }

    // TODO: deprecate this
    /// Check if the channel is currently congested (i.e. concurrency limit is reached). 
    pub fn congested(&self) -> bool {
        let current = self.counter.load(Ordering::Relaxed);
        current >= self.max_concurrency
    }
}


/// [WIP] Feedback handle to load balancers
#[derive(Debug)]
pub struct FeedbackHandle {
    id: ServerId,
    sender: FeedbackSender,
}

impl FeedbackHandle {
    /// Create a new handle.
    pub fn new(id: ServerId, sender: FeedbackSender) -> Self {
        FeedbackHandle { id, sender }
    }

    /// Get server ID.
    pub fn server_id(&self) -> ServerId {
        self.id
    }

    /// Send feedback massage.
    pub fn call(self, info: CallInfo) {
        self.sender
            .send((self.id, info))
            .expect("The receiving end of the feedback channel is dropped");
    }
}