2020-04-17 13:36:16 +00:00
|
|
|
use std::borrow::Borrow;
|
2020-04-18 17:21:34 +00:00
|
|
|
use std::marker::PhantomData;
|
2020-04-07 14:26:22 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use bytes::IntoBuf;
|
|
|
|
use futures::stream::futures_unordered::FuturesUnordered;
|
|
|
|
use futures::stream::StreamExt;
|
2020-04-10 20:01:48 +00:00
|
|
|
use futures_util::future::FutureExt;
|
2020-04-12 13:51:19 +00:00
|
|
|
use hyper::client::{Client, HttpConnector};
|
2020-04-19 15:15:48 +00:00
|
|
|
use hyper::{Body, Method, Request};
|
2020-04-22 16:51:52 +00:00
|
|
|
use tokio::sync::{watch, Semaphore};
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-18 17:39:08 +00:00
|
|
|
use crate::background::BackgroundRunner;
|
2020-04-07 14:26:22 +00:00
|
|
|
use crate::data::*;
|
|
|
|
use crate::error::Error;
|
2020-04-18 17:21:34 +00:00
|
|
|
use crate::membership::Status;
|
|
|
|
use crate::rpc_server::RpcMessage;
|
2020-04-18 17:39:08 +00:00
|
|
|
use crate::server::TlsConfig;
|
2020-04-12 13:51:19 +00:00
|
|
|
use crate::tls_util;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
pub struct RpcClient<M: RpcMessage> {
|
|
|
|
status: watch::Receiver<Arc<Status>>,
|
|
|
|
background: Arc<BackgroundRunner>,
|
|
|
|
|
|
|
|
pub rpc_addr_client: RpcAddrClient<M>,
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
impl<M: RpcMessage + 'static> RpcClient<M> {
|
|
|
|
pub fn new(
|
|
|
|
rac: RpcAddrClient<M>,
|
|
|
|
background: Arc<BackgroundRunner>,
|
|
|
|
status: watch::Receiver<Arc<Status>>,
|
|
|
|
) -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
rpc_addr_client: rac,
|
|
|
|
background,
|
|
|
|
status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn by_addr(&self) -> &RpcAddrClient<M> {
|
|
|
|
&self.rpc_addr_client
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn call<MB: Borrow<M>, N: Borrow<UUID>>(
|
|
|
|
&self,
|
|
|
|
to: N,
|
|
|
|
msg: MB,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<M, Error> {
|
|
|
|
let addr = {
|
|
|
|
let status = self.status.borrow().clone();
|
|
|
|
match status.nodes.get(to.borrow()) {
|
2020-04-21 17:08:42 +00:00
|
|
|
Some(status) => status.addr,
|
2020-04-18 17:21:34 +00:00
|
|
|
None => {
|
|
|
|
return Err(Error::Message(format!(
|
|
|
|
"Peer ID not found: {:?}",
|
|
|
|
to.borrow()
|
|
|
|
)))
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 17:21:34 +00:00
|
|
|
};
|
|
|
|
self.rpc_addr_client.call(&addr, msg, timeout).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn call_many(&self, to: &[UUID], msg: M, timeout: Duration) -> Vec<Result<M, Error>> {
|
|
|
|
let msg = Arc::new(msg);
|
|
|
|
let mut resp_stream = to
|
|
|
|
.iter()
|
|
|
|
.map(|to| self.call(to, msg.clone(), timeout))
|
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
results.push(resp);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
2020-04-18 17:21:34 +00:00
|
|
|
results
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
pub async fn try_call_many(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
to: &[UUID],
|
|
|
|
msg: M,
|
|
|
|
stop_after: usize,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Vec<M>, Error> {
|
|
|
|
let msg = Arc::new(msg);
|
|
|
|
let mut resp_stream = to
|
|
|
|
.to_vec()
|
|
|
|
.into_iter()
|
|
|
|
.map(|to| {
|
|
|
|
let self2 = self.clone();
|
|
|
|
let msg = msg.clone();
|
2020-04-21 17:08:42 +00:00
|
|
|
async move { self2.call(to, msg, timeout).await }
|
2020-04-18 17:21:34 +00:00
|
|
|
})
|
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
let mut errors = vec![];
|
|
|
|
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
match resp {
|
|
|
|
Ok(msg) => {
|
|
|
|
results.push(msg);
|
|
|
|
if results.len() >= stop_after {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if results.len() >= stop_after {
|
|
|
|
// Continue requests in background
|
|
|
|
// TODO: make this optionnal (only usefull for write requests)
|
2020-04-22 16:51:52 +00:00
|
|
|
self.clone().background.spawn_cancellable(async move {
|
2020-04-18 17:21:34 +00:00
|
|
|
resp_stream.collect::<Vec<_>>().await;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(results)
|
|
|
|
} else {
|
|
|
|
let mut msg = "Too many failures:".to_string();
|
|
|
|
for e in errors {
|
|
|
|
msg += &format!("\n{}", e);
|
|
|
|
}
|
|
|
|
Err(Error::Message(msg))
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
pub struct RpcAddrClient<M: RpcMessage> {
|
|
|
|
phantom: PhantomData<M>,
|
|
|
|
|
|
|
|
pub http_client: Arc<RpcHttpClient>,
|
|
|
|
pub path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M: RpcMessage> RpcAddrClient<M> {
|
|
|
|
pub fn new(http_client: Arc<RpcHttpClient>, path: String) -> Self {
|
|
|
|
Self {
|
|
|
|
phantom: PhantomData::default(),
|
|
|
|
http_client: http_client,
|
|
|
|
path,
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
2020-04-18 17:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn call<MB>(
|
|
|
|
&self,
|
|
|
|
to_addr: &SocketAddr,
|
|
|
|
msg: MB,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<M, Error>
|
|
|
|
where
|
|
|
|
MB: Borrow<M>,
|
|
|
|
{
|
|
|
|
self.http_client
|
|
|
|
.call(&self.path, to_addr, msg, timeout)
|
|
|
|
.await
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 16:51:52 +00:00
|
|
|
pub struct RpcHttpClient {
|
|
|
|
request_limiter: Semaphore,
|
|
|
|
method: ClientMethod,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ClientMethod {
|
2020-04-12 13:51:19 +00:00
|
|
|
HTTP(Client<HttpConnector, hyper::Body>),
|
2020-04-12 17:00:30 +00:00
|
|
|
HTTPS(Client<tls_util::HttpsConnectorFixedDnsname<HttpConnector>, hyper::Body>),
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
impl RpcHttpClient {
|
2020-04-22 16:51:52 +00:00
|
|
|
pub fn new(
|
|
|
|
max_concurrent_requests: usize,
|
|
|
|
tls_config: &Option<TlsConfig>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
let method = if let Some(cf) = tls_config {
|
2020-04-12 13:51:19 +00:00
|
|
|
let ca_certs = tls_util::load_certs(&cf.ca_cert)?;
|
|
|
|
let node_certs = tls_util::load_certs(&cf.node_cert)?;
|
|
|
|
let node_key = tls_util::load_private_key(&cf.node_key)?;
|
|
|
|
|
|
|
|
let mut config = rustls::ClientConfig::new();
|
|
|
|
|
|
|
|
for crt in ca_certs.iter() {
|
|
|
|
config.root_store.add(crt)?;
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:00:30 +00:00
|
|
|
config.set_single_client_cert([&node_certs[..], &ca_certs[..]].concat(), node_key)?;
|
2020-04-12 13:51:19 +00:00
|
|
|
|
|
|
|
let connector =
|
2020-04-12 17:00:30 +00:00
|
|
|
tls_util::HttpsConnectorFixedDnsname::<HttpConnector>::new(config, "garage");
|
2020-04-12 13:51:19 +00:00
|
|
|
|
2020-04-22 16:51:52 +00:00
|
|
|
ClientMethod::HTTPS(Client::builder().build(connector))
|
2020-04-12 13:51:19 +00:00
|
|
|
} else {
|
2020-04-22 16:51:52 +00:00
|
|
|
ClientMethod::HTTP(Client::new())
|
|
|
|
};
|
|
|
|
Ok(RpcHttpClient {
|
|
|
|
method,
|
|
|
|
request_limiter: Semaphore::new(max_concurrent_requests),
|
|
|
|
})
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
async fn call<M, MB>(
|
2020-04-10 20:01:48 +00:00
|
|
|
&self,
|
2020-04-18 17:21:34 +00:00
|
|
|
path: &str,
|
2020-04-10 20:01:48 +00:00
|
|
|
to_addr: &SocketAddr,
|
2020-04-18 17:21:34 +00:00
|
|
|
msg: MB,
|
2020-04-10 20:01:48 +00:00
|
|
|
timeout: Duration,
|
2020-04-18 17:21:34 +00:00
|
|
|
) -> Result<M, Error>
|
|
|
|
where
|
|
|
|
MB: Borrow<M>,
|
|
|
|
M: RpcMessage,
|
|
|
|
{
|
2020-04-22 16:51:52 +00:00
|
|
|
let uri = match self.method {
|
|
|
|
ClientMethod::HTTP(_) => format!("http://{}/{}", to_addr, path),
|
|
|
|
ClientMethod::HTTPS(_) => format!("https://{}/{}", to_addr, path),
|
2020-04-12 13:51:19 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.method(Method::POST)
|
|
|
|
.uri(uri)
|
2020-04-16 21:13:15 +00:00
|
|
|
.body(Body::from(rmp_to_vec_all_named(msg.borrow())?))?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-22 16:51:52 +00:00
|
|
|
let resp_fut = match &self.method {
|
|
|
|
ClientMethod::HTTP(client) => client.request(req).fuse(),
|
|
|
|
ClientMethod::HTTPS(client) => client.request(req).fuse(),
|
2020-04-12 13:51:19 +00:00
|
|
|
};
|
2020-04-22 16:51:52 +00:00
|
|
|
|
|
|
|
let slot = self.request_limiter.acquire().await;
|
2020-04-12 13:51:19 +00:00
|
|
|
let resp = tokio::time::timeout(timeout, resp_fut)
|
2020-04-22 16:51:52 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
debug!(
|
|
|
|
"RPC timeout to {}: {}",
|
|
|
|
to_addr,
|
|
|
|
debug_serialize(msg.borrow())
|
|
|
|
);
|
|
|
|
e
|
|
|
|
})?
|
2020-04-12 13:51:19 +00:00
|
|
|
.map_err(|e| {
|
2020-04-21 12:54:55 +00:00
|
|
|
warn!(
|
2020-04-16 12:50:49 +00:00
|
|
|
"RPC HTTP client error when connecting to {}: {}",
|
|
|
|
to_addr, e
|
|
|
|
);
|
2020-04-12 13:51:19 +00:00
|
|
|
e
|
|
|
|
})?;
|
2020-04-22 16:51:52 +00:00
|
|
|
drop(slot);
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
let status = resp.status();
|
|
|
|
let body = hyper::body::to_bytes(resp.into_body()).await?;
|
|
|
|
match rmp_serde::decode::from_read::<_, Result<M, String>>(body.into_buf()) {
|
2020-04-19 17:08:48 +00:00
|
|
|
Err(e) => Err(Error::RPCError(
|
|
|
|
format!("Invalid reply (deserialize error: {})", e),
|
|
|
|
status,
|
|
|
|
)),
|
|
|
|
Ok(Err(e)) => Err(Error::RPCError(e, status)),
|
2020-04-19 15:15:48 +00:00
|
|
|
Ok(Ok(x)) => Ok(x),
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|