2020-04-17 13:36:16 +00:00
|
|
|
use std::borrow::Borrow;
|
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-10 20:01:48 +00:00
|
|
|
use hyper::{Body, Method, Request, StatusCode};
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
use crate::data::*;
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::membership::System;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::proto::Message;
|
2020-04-12 13:51:19 +00:00
|
|
|
use crate::server::*;
|
|
|
|
use crate::tls_util;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn rpc_call_many(
|
|
|
|
sys: Arc<System>,
|
|
|
|
to: &[UUID],
|
2020-04-16 21:13:15 +00:00
|
|
|
msg: Message,
|
2020-04-10 20:01:48 +00:00
|
|
|
timeout: Duration,
|
|
|
|
) -> Vec<Result<Message, Error>> {
|
2020-04-16 21:13:15 +00:00
|
|
|
let msg = Arc::new(msg);
|
2020-04-10 20:01:48 +00:00
|
|
|
let mut resp_stream = to
|
|
|
|
.iter()
|
2020-04-16 21:13:15 +00:00
|
|
|
.map(|to| rpc_call(sys.clone(), to, msg.clone(), timeout))
|
2020-04-07 14:26:22 +00:00
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
results.push(resp);
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
results
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn rpc_try_call_many(
|
|
|
|
sys: Arc<System>,
|
|
|
|
to: &[UUID],
|
2020-04-16 21:13:15 +00:00
|
|
|
msg: Message,
|
2020-04-10 20:01:48 +00:00
|
|
|
stop_after: usize,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Vec<Message>, Error> {
|
2020-04-16 21:13:15 +00:00
|
|
|
let sys2 = sys.clone();
|
|
|
|
let msg = Arc::new(msg);
|
2020-04-17 13:36:16 +00:00
|
|
|
let mut resp_stream = to
|
|
|
|
.to_vec()
|
2020-04-16 21:13:15 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(move |to| rpc_call(sys2.clone(), to.clone(), msg.clone(), timeout))
|
2020-04-07 22:39:07 +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 {
|
2020-04-10 20:01:48 +00:00
|
|
|
break;
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
errors.push(e);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
|
|
|
|
if results.len() >= stop_after {
|
2020-04-16 21:13:15 +00:00
|
|
|
// Continue requests in background
|
|
|
|
// TODO: make this optionnal (only usefull for write requests)
|
|
|
|
sys.background.spawn(async move {
|
|
|
|
resp_stream.collect::<Vec<_>>().await;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2020-04-07 22:39:07 +00:00
|
|
|
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 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:13:15 +00:00
|
|
|
pub async fn rpc_call<M: Borrow<Message>, N: Borrow<UUID>>(
|
2020-04-10 20:01:48 +00:00
|
|
|
sys: Arc<System>,
|
2020-04-16 21:13:15 +00:00
|
|
|
to: N,
|
|
|
|
msg: M,
|
2020-04-10 20:01:48 +00:00
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-07 14:26:22 +00:00
|
|
|
let addr = {
|
2020-04-11 21:53:32 +00:00
|
|
|
let status = sys.status.borrow().clone();
|
2020-04-16 21:13:15 +00:00
|
|
|
match status.nodes.get(to.borrow()) {
|
2020-04-07 14:26:22 +00:00
|
|
|
Some(status) => status.addr.clone(),
|
2020-04-17 13:36:16 +00:00
|
|
|
None => {
|
|
|
|
return Err(Error::Message(format!(
|
|
|
|
"Peer ID not found: {:?}",
|
|
|
|
to.borrow()
|
|
|
|
)))
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
sys.rpc_client.call(&addr, msg, timeout).await
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:19 +00:00
|
|
|
pub enum RpcClient {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcClient {
|
2020-04-12 13:51:19 +00:00
|
|
|
pub fn new(tls_config: &Option<TlsConfig>) -> Result<Self, Error> {
|
|
|
|
if let Some(cf) = tls_config {
|
|
|
|
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
|
|
|
|
|
|
|
Ok(RpcClient::HTTPS(Client::builder().build(connector)))
|
|
|
|
} else {
|
|
|
|
Ok(RpcClient::HTTP(Client::new()))
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:13:15 +00:00
|
|
|
pub async fn call<M: Borrow<Message>>(
|
2020-04-10 20:01:48 +00:00
|
|
|
&self,
|
|
|
|
to_addr: &SocketAddr,
|
2020-04-16 21:13:15 +00:00
|
|
|
msg: M,
|
2020-04-10 20:01:48 +00:00
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-12 13:51:19 +00:00
|
|
|
let uri = match self {
|
|
|
|
RpcClient::HTTP(_) => format!("http://{}/rpc", to_addr),
|
|
|
|
RpcClient::HTTPS(_) => format!("https://{}/rpc", to_addr),
|
|
|
|
};
|
|
|
|
|
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-12 13:51:19 +00:00
|
|
|
let resp_fut = match self {
|
|
|
|
RpcClient::HTTP(client) => client.request(req).fuse(),
|
|
|
|
RpcClient::HTTPS(client) => client.request(req).fuse(),
|
|
|
|
};
|
|
|
|
let resp = tokio::time::timeout(timeout, resp_fut)
|
|
|
|
.await?
|
|
|
|
.map_err(|e| {
|
2020-04-16 12:50:49 +00:00
|
|
|
eprintln!(
|
|
|
|
"RPC HTTP client error when connecting to {}: {}",
|
|
|
|
to_addr, e
|
|
|
|
);
|
2020-04-12 13:51:19 +00:00
|
|
|
e
|
|
|
|
})?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
if resp.status() == StatusCode::OK {
|
|
|
|
let body = hyper::body::to_bytes(resp.into_body()).await?;
|
|
|
|
let msg = rmp_serde::decode::from_read::<_, Message>(body.into_buf())?;
|
|
|
|
match msg {
|
|
|
|
Message::Error(e) => Err(Error::RPCError(e)),
|
2020-04-10 20:01:48 +00:00
|
|
|
x => Ok(x),
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::RPCError(format!("Status code {}", resp.status())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|