2021-10-14 09:50:12 +00:00
|
|
|
//! Contain structs related to making RPCs
|
|
|
|
use std::sync::Arc;
|
2022-02-22 13:52:41 +00:00
|
|
|
use std::time::Duration;
|
2021-10-14 09:50:12 +00:00
|
|
|
|
|
|
|
use futures::future::join_all;
|
|
|
|
use futures::stream::futures_unordered::FuturesUnordered;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use tokio::select;
|
2022-09-19 18:12:19 +00:00
|
|
|
use tokio::sync::watch;
|
2021-10-14 09:50:12 +00:00
|
|
|
|
2022-02-17 22:28:23 +00:00
|
|
|
use opentelemetry::KeyValue;
|
|
|
|
use opentelemetry::{
|
|
|
|
trace::{FutureExt as OtelFutureExt, Span, TraceContextExt, Tracer},
|
|
|
|
Context,
|
|
|
|
};
|
|
|
|
|
2024-02-13 11:55:41 +00:00
|
|
|
pub use garage_net::endpoint::{Endpoint, EndpointHandler, StreamingEndpointHandler};
|
|
|
|
pub use garage_net::message::{
|
2023-01-11 15:12:07 +00:00
|
|
|
IntoReq, Message as Rpc, OrderTag, Req, RequestPriority, Resp, PRIO_BACKGROUND, PRIO_HIGH,
|
|
|
|
PRIO_NORMAL, PRIO_SECONDARY,
|
2022-07-22 16:20:27 +00:00
|
|
|
};
|
2024-02-14 09:04:46 +00:00
|
|
|
use garage_net::peering::PeeringManager;
|
2024-02-13 11:55:41 +00:00
|
|
|
pub use garage_net::{self, NetApp, NodeID};
|
2021-10-14 09:50:12 +00:00
|
|
|
|
2021-11-03 16:00:40 +00:00
|
|
|
use garage_util::data::*;
|
2021-10-19 14:16:10 +00:00
|
|
|
use garage_util::error::Error;
|
2022-02-22 12:53:59 +00:00
|
|
|
use garage_util::metrics::RecordDuration;
|
2021-10-14 09:50:12 +00:00
|
|
|
|
2022-02-16 13:23:04 +00:00
|
|
|
use crate::metrics::RpcMetrics;
|
2021-11-04 15:04:26 +00:00
|
|
|
use crate::ring::Ring;
|
|
|
|
|
2022-09-19 18:12:19 +00:00
|
|
|
// Default RPC timeout = 5 minutes
|
|
|
|
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(300);
|
2021-11-03 16:00:40 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
/// Strategy to apply when making RPC
|
2024-03-27 14:26:08 +00:00
|
|
|
pub struct RequestStrategy<T> {
|
2021-10-14 09:50:12 +00:00
|
|
|
/// Min number of response to consider the request successful
|
|
|
|
pub rs_quorum: Option<usize>,
|
|
|
|
/// Should requests be dropped after enough response are received
|
|
|
|
pub rs_interrupt_after_quorum: bool,
|
|
|
|
/// Request priority
|
|
|
|
pub rs_priority: RequestPriority,
|
2022-09-20 14:01:41 +00:00
|
|
|
/// Custom timeout for this request
|
|
|
|
rs_timeout: Timeout,
|
2024-03-27 14:26:08 +00:00
|
|
|
/// Data to drop when everything completes
|
|
|
|
rs_drop_on_complete: T,
|
2022-09-20 14:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Timeout {
|
|
|
|
None,
|
|
|
|
Default,
|
|
|
|
Custom(Duration),
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 14:26:08 +00:00
|
|
|
impl Clone for RequestStrategy<()> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
RequestStrategy {
|
|
|
|
rs_quorum: self.rs_quorum,
|
|
|
|
rs_interrupt_after_quorum: self.rs_interrupt_after_quorum,
|
|
|
|
rs_priority: self.rs_priority,
|
|
|
|
rs_timeout: self.rs_timeout,
|
|
|
|
rs_drop_on_complete: (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestStrategy<()> {
|
2021-10-14 09:50:12 +00:00
|
|
|
/// Create a RequestStrategy with default timeout and not interrupting when quorum reached
|
|
|
|
pub fn with_priority(prio: RequestPriority) -> Self {
|
|
|
|
RequestStrategy {
|
|
|
|
rs_quorum: None,
|
|
|
|
rs_interrupt_after_quorum: false,
|
|
|
|
rs_priority: prio,
|
2022-09-20 14:01:41 +00:00
|
|
|
rs_timeout: Timeout::Default,
|
2024-03-27 14:26:08 +00:00
|
|
|
rs_drop_on_complete: (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Add an item to be dropped on completion
|
|
|
|
pub fn with_drop_on_completion<T>(self, drop_on_complete: T) -> RequestStrategy<T> {
|
|
|
|
RequestStrategy {
|
|
|
|
rs_quorum: self.rs_quorum,
|
|
|
|
rs_interrupt_after_quorum: self.rs_interrupt_after_quorum,
|
|
|
|
rs_priority: self.rs_priority,
|
|
|
|
rs_timeout: self.rs_timeout,
|
|
|
|
rs_drop_on_complete: drop_on_complete,
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-27 14:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RequestStrategy<T> {
|
2021-10-14 09:50:12 +00:00
|
|
|
/// Set quorum to be reached for request
|
|
|
|
pub fn with_quorum(mut self, quorum: usize) -> Self {
|
|
|
|
self.rs_quorum = Some(quorum);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Set if requests can be dropped after quorum has been reached
|
|
|
|
/// In general true for read requests, and false for write
|
|
|
|
pub fn interrupt_after_quorum(mut self, interrupt: bool) -> Self {
|
|
|
|
self.rs_interrupt_after_quorum = interrupt;
|
|
|
|
self
|
|
|
|
}
|
2022-09-19 18:12:19 +00:00
|
|
|
/// Deactivate timeout for this request
|
|
|
|
pub fn without_timeout(mut self) -> Self {
|
2022-09-20 14:01:41 +00:00
|
|
|
self.rs_timeout = Timeout::None;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Set custom timeout for this request
|
|
|
|
pub fn with_custom_timeout(mut self, timeout: Duration) -> Self {
|
|
|
|
self.rs_timeout = Timeout::Custom(timeout);
|
2022-09-19 18:12:19 +00:00
|
|
|
self
|
|
|
|
}
|
2024-03-27 14:26:08 +00:00
|
|
|
/// Extract drop_on_complete item
|
|
|
|
fn extract_drop_on_complete(self) -> (RequestStrategy<()>, T) {
|
|
|
|
(
|
|
|
|
RequestStrategy {
|
|
|
|
rs_quorum: self.rs_quorum,
|
|
|
|
rs_interrupt_after_quorum: self.rs_interrupt_after_quorum,
|
|
|
|
rs_priority: self.rs_priority,
|
|
|
|
rs_timeout: self.rs_timeout,
|
|
|
|
rs_drop_on_complete: (),
|
|
|
|
},
|
|
|
|
self.rs_drop_on_complete,
|
|
|
|
)
|
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2021-11-04 15:04:26 +00:00
|
|
|
pub struct RpcHelper(Arc<RpcHelperInner>);
|
|
|
|
|
|
|
|
struct RpcHelperInner {
|
|
|
|
our_node_id: Uuid,
|
2024-02-14 09:04:46 +00:00
|
|
|
peering: Arc<PeeringManager>,
|
2021-11-04 15:04:26 +00:00
|
|
|
ring: watch::Receiver<Arc<Ring>>,
|
2022-02-16 13:23:04 +00:00
|
|
|
metrics: RpcMetrics,
|
2022-09-19 18:12:19 +00:00
|
|
|
rpc_timeout: Duration,
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcHelper {
|
2021-11-03 16:00:40 +00:00
|
|
|
pub(crate) fn new(
|
2021-11-04 15:04:26 +00:00
|
|
|
our_node_id: Uuid,
|
2024-02-14 09:04:46 +00:00
|
|
|
peering: Arc<PeeringManager>,
|
2021-11-04 15:04:26 +00:00
|
|
|
ring: watch::Receiver<Arc<Ring>>,
|
2022-09-19 18:12:19 +00:00
|
|
|
rpc_timeout: Option<Duration>,
|
2021-11-03 16:00:40 +00:00
|
|
|
) -> Self {
|
2022-09-19 18:12:19 +00:00
|
|
|
let metrics = RpcMetrics::new();
|
2022-02-16 13:23:04 +00:00
|
|
|
|
2021-11-04 15:04:26 +00:00
|
|
|
Self(Arc::new(RpcHelperInner {
|
|
|
|
our_node_id,
|
2024-02-14 09:04:46 +00:00
|
|
|
peering,
|
2021-11-04 15:04:26 +00:00
|
|
|
ring,
|
2022-02-16 13:23:04 +00:00
|
|
|
metrics,
|
2022-09-19 18:12:19 +00:00
|
|
|
rpc_timeout: rpc_timeout.unwrap_or(DEFAULT_TIMEOUT),
|
2021-11-04 15:04:26 +00:00
|
|
|
}))
|
2021-11-03 16:00:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 18:12:19 +00:00
|
|
|
pub fn rpc_timeout(&self) -> Duration {
|
|
|
|
self.0.rpc_timeout
|
|
|
|
}
|
|
|
|
|
2022-07-22 13:20:00 +00:00
|
|
|
pub async fn call<M, N, H, S>(
|
2021-10-14 09:50:12 +00:00
|
|
|
&self,
|
|
|
|
endpoint: &Endpoint<M, H>,
|
2021-10-15 09:05:09 +00:00
|
|
|
to: Uuid,
|
2022-07-22 13:20:00 +00:00
|
|
|
msg: N,
|
2024-03-27 14:26:08 +00:00
|
|
|
strat: RequestStrategy<()>,
|
2021-10-15 09:05:09 +00:00
|
|
|
) -> Result<S, Error>
|
2021-10-14 09:50:12 +00:00
|
|
|
where
|
2021-10-15 09:05:09 +00:00
|
|
|
M: Rpc<Response = Result<S, Error>>,
|
2022-07-22 13:20:00 +00:00
|
|
|
N: IntoReq<M> + Send,
|
2022-07-22 16:20:27 +00:00
|
|
|
H: StreamingEndpointHandler<M>,
|
2021-10-14 09:50:12 +00:00
|
|
|
{
|
2022-02-24 13:59:49 +00:00
|
|
|
let metric_tags = [
|
|
|
|
KeyValue::new("rpc_endpoint", endpoint.path().to_string()),
|
|
|
|
KeyValue::new("from", format!("{:?}", self.0.our_node_id)),
|
|
|
|
KeyValue::new("to", format!("{:?}", to)),
|
|
|
|
];
|
2022-02-16 13:23:04 +00:00
|
|
|
|
|
|
|
self.0.metrics.rpc_counter.add(1, &metric_tags);
|
|
|
|
|
2021-10-15 09:05:09 +00:00
|
|
|
let node_id = to.into();
|
2022-02-22 13:52:41 +00:00
|
|
|
let rpc_call = endpoint
|
2022-07-22 13:20:00 +00:00
|
|
|
.call_streaming(&node_id, msg, strat.rs_priority)
|
2022-02-22 12:53:59 +00:00
|
|
|
.record_duration(&self.0.metrics.rpc_duration, &metric_tags);
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-09-19 18:12:19 +00:00
|
|
|
let timeout = async {
|
2022-09-20 14:01:41 +00:00
|
|
|
match strat.rs_timeout {
|
|
|
|
Timeout::None => futures::future::pending().await,
|
|
|
|
Timeout::Default => tokio::time::sleep(self.0.rpc_timeout).await,
|
|
|
|
Timeout::Custom(t) => tokio::time::sleep(t).await,
|
2022-09-19 18:12:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
select! {
|
2022-02-17 22:28:23 +00:00
|
|
|
res = rpc_call => {
|
2022-02-16 13:23:04 +00:00
|
|
|
if res.is_err() {
|
|
|
|
self.0.metrics.rpc_netapp_error_counter.add(1, &metric_tags);
|
|
|
|
}
|
2022-07-22 13:20:00 +00:00
|
|
|
let res = res?.into_msg();
|
2022-02-16 13:23:04 +00:00
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
self.0.metrics.rpc_garage_error_counter.add(1, &metric_tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res?)
|
2021-11-03 16:00:40 +00:00
|
|
|
}
|
2022-09-19 18:12:19 +00:00
|
|
|
() = timeout => {
|
2022-02-16 13:23:04 +00:00
|
|
|
self.0.metrics.rpc_timeout_counter.add(1, &metric_tags);
|
2021-11-03 16:00:40 +00:00
|
|
|
Err(Error::Timeout)
|
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 13:20:00 +00:00
|
|
|
pub async fn call_many<M, N, H, S>(
|
2021-10-14 09:50:12 +00:00
|
|
|
&self,
|
|
|
|
endpoint: &Endpoint<M, H>,
|
2021-10-15 09:05:09 +00:00
|
|
|
to: &[Uuid],
|
2022-07-22 13:20:00 +00:00
|
|
|
msg: N,
|
2024-03-27 14:26:08 +00:00
|
|
|
strat: RequestStrategy<()>,
|
2022-07-22 13:20:00 +00:00
|
|
|
) -> Result<Vec<(Uuid, Result<S, Error>)>, Error>
|
2021-10-14 09:50:12 +00:00
|
|
|
where
|
2021-10-15 09:05:09 +00:00
|
|
|
M: Rpc<Response = Result<S, Error>>,
|
2022-07-22 13:20:00 +00:00
|
|
|
N: IntoReq<M>,
|
2022-07-22 16:20:27 +00:00
|
|
|
H: StreamingEndpointHandler<M>,
|
2021-10-14 09:50:12 +00:00
|
|
|
{
|
2024-02-13 11:55:41 +00:00
|
|
|
let msg = msg.into_req().map_err(garage_net::error::Error::from)?;
|
2022-07-22 13:20:00 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
let resps = join_all(
|
|
|
|
to.iter()
|
2024-03-27 14:26:08 +00:00
|
|
|
.map(|to| self.call(endpoint, *to, msg.clone(), strat.clone())),
|
2021-10-14 09:50:12 +00:00
|
|
|
)
|
|
|
|
.await;
|
2022-07-22 13:20:00 +00:00
|
|
|
Ok(to
|
|
|
|
.iter()
|
2021-10-14 09:50:12 +00:00
|
|
|
.cloned()
|
|
|
|
.zip(resps.into_iter())
|
2022-07-22 13:20:00 +00:00
|
|
|
.collect::<Vec<_>>())
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 13:20:00 +00:00
|
|
|
pub async fn broadcast<M, N, H, S>(
|
2021-10-14 09:50:12 +00:00
|
|
|
&self,
|
|
|
|
endpoint: &Endpoint<M, H>,
|
2022-07-22 13:20:00 +00:00
|
|
|
msg: N,
|
2024-03-27 14:26:08 +00:00
|
|
|
strat: RequestStrategy<()>,
|
2022-07-22 13:20:00 +00:00
|
|
|
) -> Result<Vec<(Uuid, Result<S, Error>)>, Error>
|
2021-10-14 09:50:12 +00:00
|
|
|
where
|
2021-10-15 09:05:09 +00:00
|
|
|
M: Rpc<Response = Result<S, Error>>,
|
2022-07-22 13:20:00 +00:00
|
|
|
N: IntoReq<M>,
|
2022-07-22 16:20:27 +00:00
|
|
|
H: StreamingEndpointHandler<M>,
|
2021-10-14 09:50:12 +00:00
|
|
|
{
|
|
|
|
let to = self
|
2021-11-04 15:04:26 +00:00
|
|
|
.0
|
2024-02-14 09:04:46 +00:00
|
|
|
.peering
|
2021-10-14 09:50:12 +00:00
|
|
|
.get_peer_list()
|
|
|
|
.iter()
|
2021-10-15 09:05:09 +00:00
|
|
|
.map(|p| p.id.into())
|
2021-10-14 09:50:12 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.call_many(endpoint, &to[..], msg, strat).await
|
|
|
|
}
|
|
|
|
|
2021-11-04 15:04:26 +00:00
|
|
|
/// Make a RPC call to multiple servers, returning either a Vec of responses,
|
|
|
|
/// or an error if quorum could not be reached due to too many errors
|
2024-03-27 14:26:08 +00:00
|
|
|
pub async fn try_call_many<M, N, H, S, T>(
|
2021-10-14 09:50:12 +00:00
|
|
|
&self,
|
|
|
|
endpoint: &Arc<Endpoint<M, H>>,
|
2021-10-15 09:05:09 +00:00
|
|
|
to: &[Uuid],
|
2022-07-22 16:20:27 +00:00
|
|
|
msg: N,
|
2024-03-27 14:26:08 +00:00
|
|
|
strategy: RequestStrategy<T>,
|
2021-10-15 09:05:09 +00:00
|
|
|
) -> Result<Vec<S>, Error>
|
2021-10-14 09:50:12 +00:00
|
|
|
where
|
2021-10-15 09:05:09 +00:00
|
|
|
M: Rpc<Response = Result<S, Error>> + 'static,
|
2022-07-22 16:20:27 +00:00
|
|
|
N: IntoReq<M>,
|
|
|
|
H: StreamingEndpointHandler<M> + 'static,
|
2022-02-17 22:28:23 +00:00
|
|
|
S: Send + 'static,
|
2024-03-27 14:26:08 +00:00
|
|
|
T: Send + 'static,
|
2021-10-14 09:50:12 +00:00
|
|
|
{
|
2021-11-04 15:04:26 +00:00
|
|
|
let quorum = strategy.rs_quorum.unwrap_or(to.len());
|
2021-10-14 09:50:12 +00:00
|
|
|
|
2022-02-17 22:28:23 +00:00
|
|
|
let tracer = opentelemetry::global::tracer("garage");
|
2022-02-24 12:18:51 +00:00
|
|
|
let span_name = if strategy.rs_interrupt_after_quorum {
|
|
|
|
format!("RPC {} to {} of {}", endpoint.path(), quorum, to.len())
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"RPC {} to {} (quorum {})",
|
|
|
|
endpoint.path(),
|
|
|
|
to.len(),
|
|
|
|
quorum
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let mut span = tracer.start(span_name);
|
2022-02-24 13:59:49 +00:00
|
|
|
span.set_attribute(KeyValue::new("from", format!("{:?}", self.0.our_node_id)));
|
2022-02-17 22:28:23 +00:00
|
|
|
span.set_attribute(KeyValue::new("to", format!("{:?}", to)));
|
|
|
|
span.set_attribute(KeyValue::new("quorum", quorum as i64));
|
2022-02-24 12:18:51 +00:00
|
|
|
span.set_attribute(KeyValue::new(
|
|
|
|
"interrupt_after_quorum",
|
|
|
|
strategy.rs_interrupt_after_quorum.to_string(),
|
|
|
|
));
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-02-22 14:25:13 +00:00
|
|
|
self.try_call_many_internal(endpoint, to, msg, strategy, quorum)
|
|
|
|
.with_context(Context::current_with_span(span))
|
|
|
|
.await
|
|
|
|
}
|
2021-11-04 15:04:26 +00:00
|
|
|
|
2024-03-27 14:26:08 +00:00
|
|
|
async fn try_call_many_internal<M, N, H, S, T>(
|
2022-02-22 14:25:13 +00:00
|
|
|
&self,
|
|
|
|
endpoint: &Arc<Endpoint<M, H>>,
|
|
|
|
to: &[Uuid],
|
2022-07-22 13:20:00 +00:00
|
|
|
msg: N,
|
2024-03-27 14:26:08 +00:00
|
|
|
strategy: RequestStrategy<T>,
|
2022-02-22 14:25:13 +00:00
|
|
|
quorum: usize,
|
|
|
|
) -> Result<Vec<S>, Error>
|
|
|
|
where
|
|
|
|
M: Rpc<Response = Result<S, Error>> + 'static,
|
2022-07-22 13:20:00 +00:00
|
|
|
N: IntoReq<M>,
|
2022-07-22 16:20:27 +00:00
|
|
|
H: StreamingEndpointHandler<M> + 'static,
|
2022-02-22 14:25:13 +00:00
|
|
|
S: Send + 'static,
|
2024-03-27 14:26:08 +00:00
|
|
|
T: Send + 'static,
|
2022-02-22 14:25:13 +00:00
|
|
|
{
|
2024-02-13 11:55:41 +00:00
|
|
|
let msg = msg.into_req().map_err(garage_net::error::Error::from)?;
|
2022-02-22 14:25:13 +00:00
|
|
|
|
2024-03-27 14:26:08 +00:00
|
|
|
let (strategy, drop_on_complete) = strategy.extract_drop_on_complete();
|
|
|
|
|
2022-02-22 14:25:13 +00:00
|
|
|
// Build future for each request
|
|
|
|
// They are not started now: they are added below in a FuturesUnordered
|
|
|
|
// object that will take care of polling them (see below)
|
|
|
|
let requests = to.iter().cloned().map(|to| {
|
|
|
|
let self2 = self.clone();
|
|
|
|
let msg = msg.clone();
|
|
|
|
let endpoint2 = endpoint.clone();
|
2024-03-27 14:26:08 +00:00
|
|
|
let strategy = strategy.clone();
|
2022-02-22 14:25:13 +00:00
|
|
|
(to, async move {
|
2022-07-22 13:20:00 +00:00
|
|
|
self2.call(&endpoint2, to, msg, strategy).await
|
2022-02-22 14:25:13 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// Vectors in which success results and errors will be collected
|
|
|
|
let mut successes = vec![];
|
|
|
|
let mut errors = vec![];
|
|
|
|
|
|
|
|
if strategy.rs_interrupt_after_quorum {
|
|
|
|
// Case 1: once quorum is reached, other requests don't matter.
|
|
|
|
// What we do here is only send the required number of requests
|
|
|
|
// to reach a quorum, priorizing nodes with the lowest latency.
|
|
|
|
// When there are errors, we start new requests to compensate.
|
|
|
|
|
2022-07-25 16:19:35 +00:00
|
|
|
// Reorder requests to priorize closeness / low latency
|
|
|
|
let request_order = self.request_order(to);
|
|
|
|
let mut ord_requests = vec![(); request_order.len()]
|
|
|
|
.into_iter()
|
|
|
|
.map(|_| None)
|
2022-02-22 14:25:13 +00:00
|
|
|
.collect::<Vec<_>>();
|
2022-07-25 16:19:35 +00:00
|
|
|
for (to, fut) in requests {
|
|
|
|
let i = request_order.iter().position(|x| *x == to).unwrap();
|
|
|
|
ord_requests[i] = Some((to, fut));
|
|
|
|
}
|
2022-02-22 14:25:13 +00:00
|
|
|
|
|
|
|
// Make an iterator to take requests in their sorted order
|
2022-07-25 16:19:35 +00:00
|
|
|
let mut requests = ord_requests.into_iter().map(Option::unwrap);
|
2022-02-22 14:25:13 +00:00
|
|
|
|
|
|
|
// resp_stream will contain all of the requests that are currently in flight.
|
|
|
|
// (for the moment none, they will be added in the loop below)
|
|
|
|
let mut resp_stream = FuturesUnordered::new();
|
|
|
|
|
|
|
|
// Do some requests and collect results
|
|
|
|
'request_loop: while successes.len() < quorum {
|
|
|
|
// If the current set of requests that are running is not enough to possibly
|
|
|
|
// reach quorum, start some new requests.
|
|
|
|
while successes.len() + resp_stream.len() < quorum {
|
2022-07-25 16:19:35 +00:00
|
|
|
if let Some((req_to, fut)) = requests.next() {
|
2022-02-22 14:25:13 +00:00
|
|
|
let tracer = opentelemetry::global::tracer("garage");
|
|
|
|
let span = tracer.start(format!("RPC to {:?}", req_to));
|
|
|
|
resp_stream.push(tokio::spawn(
|
|
|
|
fut.with_context(Context::current_with_span(span)),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// If we have no request to add, we know that we won't ever
|
|
|
|
// reach quorum: bail out now.
|
|
|
|
break 'request_loop;
|
2021-11-04 15:04:26 +00:00
|
|
|
}
|
2022-02-22 14:25:13 +00:00
|
|
|
}
|
|
|
|
assert!(!resp_stream.is_empty()); // because of loop invariants
|
2022-02-17 22:28:23 +00:00
|
|
|
|
2022-02-22 14:25:13 +00:00
|
|
|
// Wait for one request to terminate
|
|
|
|
match resp_stream.next().await.unwrap().unwrap() {
|
|
|
|
Ok(msg) => {
|
|
|
|
successes.push(msg);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
errors.push(e);
|
2021-11-04 15:04:26 +00:00
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
2022-02-22 14:25:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Case 2: all of the requests need to be sent in all cases,
|
|
|
|
// and need to terminate. (this is the case for writes that
|
|
|
|
// must be spread to n nodes)
|
|
|
|
// Just start all the requests in parallel and return as soon
|
|
|
|
// as the quorum is reached.
|
|
|
|
let mut resp_stream = requests
|
|
|
|
.map(|(_, fut)| fut)
|
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
match resp {
|
|
|
|
Ok(msg) => {
|
|
|
|
successes.push(msg);
|
|
|
|
if successes.len() >= quorum {
|
|
|
|
break;
|
2021-11-04 15:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 14:25:13 +00:00
|
|
|
Err(e) => {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
2022-02-17 22:28:23 +00:00
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 14:25:13 +00:00
|
|
|
if !resp_stream.is_empty() {
|
|
|
|
// Continue remaining requests in background.
|
2022-12-14 11:51:16 +00:00
|
|
|
// Note: these requests can get interrupted on process shutdown,
|
|
|
|
// we must not count on them being executed for certain.
|
|
|
|
// For all background things that have to happen with certainty,
|
|
|
|
// they have to be put in a proper queue that is persisted to disk.
|
|
|
|
tokio::spawn(async move {
|
2022-02-22 14:25:13 +00:00
|
|
|
resp_stream.collect::<Vec<Result<_, _>>>().await;
|
2024-03-27 14:26:08 +00:00
|
|
|
drop(drop_on_complete);
|
2022-02-22 14:25:13 +00:00
|
|
|
});
|
2022-02-17 22:28:23 +00:00
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
2022-02-22 14:25:13 +00:00
|
|
|
|
|
|
|
if successes.len() >= quorum {
|
|
|
|
Ok(successes)
|
|
|
|
} else {
|
|
|
|
let errors = errors.iter().map(|e| format!("{}", e)).collect::<Vec<_>>();
|
|
|
|
Err(Error::Quorum(quorum, successes.len(), to.len(), errors))
|
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|
2022-07-25 16:19:35 +00:00
|
|
|
|
|
|
|
pub fn request_order(&self, nodes: &[Uuid]) -> Vec<Uuid> {
|
|
|
|
// Retrieve some status variables that we will use to sort requests
|
2024-02-14 09:04:46 +00:00
|
|
|
let peer_list = self.0.peering.get_peer_list();
|
2022-07-25 16:19:35 +00:00
|
|
|
let ring: Arc<Ring> = self.0.ring.borrow().clone();
|
|
|
|
let our_zone = match ring.layout.node_role(&self.0.our_node_id) {
|
|
|
|
Some(pc) => &pc.zone,
|
|
|
|
None => "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Augment requests with some information used to sort them.
|
|
|
|
// The tuples are as follows:
|
|
|
|
// (is another node?, is another zone?, latency, node ID, request future)
|
|
|
|
// We store all of these tuples in a vec that we can sort.
|
|
|
|
// By sorting this vec, we priorize ourself, then nodes in the same zone,
|
|
|
|
// and within a same zone we priorize nodes with the lowest latency.
|
|
|
|
let mut nodes = nodes
|
|
|
|
.iter()
|
|
|
|
.map(|to| {
|
2022-08-29 15:32:45 +00:00
|
|
|
let peer_zone = match ring.layout.node_role(to) {
|
2022-07-25 16:19:35 +00:00
|
|
|
Some(pc) => &pc.zone,
|
|
|
|
None => "",
|
|
|
|
};
|
|
|
|
let peer_avg_ping = peer_list
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.id.as_ref() == to.as_slice())
|
|
|
|
.and_then(|pi| pi.avg_ping)
|
2022-09-20 14:01:41 +00:00
|
|
|
.unwrap_or_else(|| Duration::from_secs(10));
|
2022-07-25 16:19:35 +00:00
|
|
|
(
|
|
|
|
*to != self.0.our_node_id,
|
|
|
|
peer_zone != our_zone,
|
|
|
|
peer_avg_ping,
|
|
|
|
*to,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Sort requests by (priorize ourself, priorize same zone, priorize low latency)
|
|
|
|
nodes.sort_by_key(|(diffnode, diffzone, ping, _to)| (*diffnode, *diffzone, *ping));
|
|
|
|
|
|
|
|
nodes
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, _, _, to)| to)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
2021-10-14 09:50:12 +00:00
|
|
|
}
|