netapp/src/client.rs

245 lines
6.6 KiB
Rust
Raw Normal View History

2020-12-02 12:30:47 +00:00
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{self, AtomicU32};
use std::sync::{Arc, Mutex};
2020-12-02 12:30:47 +00:00
use arc_swap::ArcSwapOption;
2022-07-21 15:34:53 +00:00
use async_trait::async_trait;
use log::{debug, error, trace};
2020-12-02 12:30:47 +00:00
2022-07-18 13:21:13 +00:00
use futures::channel::mpsc::{unbounded, UnboundedReceiver};
2022-07-21 15:34:53 +00:00
use futures::io::AsyncReadExt;
use kuska_handshake::async_std::{handshake_client, BoxStream};
2020-12-02 12:30:47 +00:00
use tokio::net::TcpStream;
use tokio::select;
2020-12-02 12:30:47 +00:00
use tokio::sync::{mpsc, oneshot, watch};
use tokio_util::compat::*;
#[cfg(feature = "telemetry")]
use opentelemetry::{
trace::{FutureExt, Span, SpanKind, TraceContextExt, Tracer},
2022-02-18 19:23:10 +00:00
Context, KeyValue,
};
#[cfg(feature = "telemetry")]
use opentelemetry_contrib::trace::propagator::binary::*;
2020-12-02 12:30:47 +00:00
use crate::error::*;
2022-07-21 15:34:53 +00:00
use crate::message::*;
2020-12-02 12:30:47 +00:00
use crate::netapp::*;
2022-07-21 15:34:53 +00:00
use crate::recv::*;
use crate::send::*;
2020-12-02 12:30:47 +00:00
use crate::util::*;
2020-12-02 19:12:24 +00:00
pub(crate) struct ClientConn {
pub(crate) remote_addr: SocketAddr,
pub(crate) peer_id: NodeID,
2020-12-02 19:12:24 +00:00
2022-07-21 15:34:53 +00:00
query_send: ArcSwapOption<mpsc::UnboundedSender<(RequestID, RequestPriority, ByteStream)>>,
2021-10-12 15:59:46 +00:00
next_query_number: AtomicU32,
2022-07-18 13:21:13 +00:00
inflight: Mutex<HashMap<RequestID, oneshot::Sender<UnboundedReceiver<Packet>>>>,
2020-12-02 12:30:47 +00:00
}
impl ClientConn {
pub(crate) async fn init(
netapp: Arc<NetApp>,
socket: TcpStream,
peer_id: NodeID,
2020-12-02 12:30:47 +00:00
) -> Result<(), Error> {
let remote_addr = socket.peer_addr()?;
let mut socket = socket.compat();
2020-12-02 12:30:47 +00:00
// Do handshake to authenticate and prove our identity to server
2020-12-02 12:30:47 +00:00
let handshake = handshake_client(
&mut socket,
2020-12-02 12:30:47 +00:00
netapp.netid.clone(),
2021-10-12 11:18:24 +00:00
netapp.id,
2020-12-02 12:30:47 +00:00
netapp.privkey.clone(),
2021-10-12 11:18:24 +00:00
peer_id,
2020-12-02 12:30:47 +00:00
)
.await?;
debug!(
"Handshake complete (client) with {}@{}",
hex::encode(&peer_id),
2020-12-02 12:30:47 +00:00
remote_addr
);
// Create BoxStream layer that encodes content
let (read, write) = socket.split();
let (mut read, write) =
2020-12-02 12:30:47 +00:00
BoxStream::from_handshake(read, write, handshake, 0x8000).split_read_write();
// Before doing anything, receive version tag and
// check they are running the same version as us
let mut their_version_tag = VersionTag::default();
read.read_exact(&mut their_version_tag[..]).await?;
if their_version_tag != netapp.version_tag {
let msg = format!(
2022-02-21 15:57:07 +00:00
"different version tags: {} (theirs) vs. {} (ours)",
hex::encode(their_version_tag),
hex::encode(netapp.version_tag)
);
2022-02-21 15:57:07 +00:00
error!("Cannot connect to {}: {}", hex::encode(&peer_id[..8]), msg);
return Err(Error::VersionMismatch(msg));
}
// Build and launch stuff that manages sending requests client-side
2020-12-02 12:30:47 +00:00
let (query_send, query_recv) = mpsc::unbounded_channel();
let (stop_recv_loop, stop_recv_loop_recv) = watch::channel(false);
2020-12-02 12:30:47 +00:00
let conn = Arc::new(ClientConn {
remote_addr,
2021-10-12 11:18:24 +00:00
peer_id,
2021-10-12 15:59:46 +00:00
next_query_number: AtomicU32::from(RequestID::default()),
query_send: ArcSwapOption::new(Some(Arc::new(query_send))),
inflight: Mutex::new(HashMap::new()),
2020-12-02 12:30:47 +00:00
});
2021-10-12 11:18:24 +00:00
netapp.connected_as_client(peer_id, conn.clone());
2020-12-02 12:30:47 +00:00
tokio::spawn(async move {
let send_future = tokio::spawn(conn.clone().send_loop(query_recv, write));
2020-12-02 12:30:47 +00:00
let conn2 = conn.clone();
let recv_future = tokio::spawn(async move {
select! {
r = conn2.recv_loop(read) => r,
_ = await_exit(stop_recv_loop_recv) => Ok(())
}
});
send_future.await.log_err("ClientConn send_loop");
2021-10-13 15:14:26 +00:00
// FIXME: should do here: wait for inflight requests to all have their response
stop_recv_loop
.send(true)
.log_err("ClientConn send true to stop_recv_loop");
recv_future.await.log_err("ClientConn recv_loop");
// Make sure we don't wait on any more requests that won't
// have a response
conn.inflight.lock().unwrap().clear();
2020-12-02 12:30:47 +00:00
netapp.disconnected_as_client(&peer_id, conn);
2020-12-02 12:30:47 +00:00
});
Ok(())
}
pub fn close(&self) {
self.query_send.store(None);
2020-12-02 12:30:47 +00:00
}
pub(crate) async fn call<T>(
2020-12-02 12:30:47 +00:00
self: Arc<Self>,
2022-07-21 18:22:56 +00:00
req: Req<T>,
2022-02-21 11:04:09 +00:00
path: &str,
2020-12-02 12:30:47 +00:00
prio: RequestPriority,
2022-07-21 18:22:56 +00:00
) -> Result<Resp<T>, Error>
2020-12-02 12:30:47 +00:00
where
T: Message,
{
let query_send = self.query_send.load_full().ok_or(Error::ConnectionClosed)?;
2020-12-02 12:30:47 +00:00
let id = self
.next_query_number
.fetch_add(1, atomic::Ordering::Relaxed);
2022-02-18 18:01:23 +00:00
cfg_if::cfg_if! {
if #[cfg(feature = "telemetry")] {
let tracer = opentelemetry::global::tracer("netapp");
2022-02-18 19:23:10 +00:00
let mut span = tracer.span_builder(format!("RPC >> {}", path))
2022-04-07 08:31:37 +00:00
.with_kind(SpanKind::Client)
.start(&tracer);
let propagator = BinaryPropagator::new();
let telemetry_id = Some(propagator.to_bytes(span.span_context()).to_vec());
2022-02-18 18:01:23 +00:00
} else {
let telemetry_id: Option<Vec<u8>> = None;
2022-02-18 18:01:23 +00:00
}
};
// Encode request
2022-07-21 18:22:56 +00:00
let body = req.msg_ser.unwrap().clone();
let stream = req.body.into_stream();
2022-02-21 11:01:04 +00:00
let request = QueryMessage {
prio,
path: path.as_bytes(),
telemetry_id,
body: &body[..],
};
let bytes = request.encode();
drop(body);
2020-12-02 12:30:47 +00:00
2022-02-18 18:01:23 +00:00
// Send request through
2020-12-02 12:30:47 +00:00
let (resp_send, resp_recv) = oneshot::channel();
let old = self.inflight.lock().unwrap().insert(id, resp_send);
if let Some(old_ch) = old {
error!(
"Too many inflight requests! RequestID collision. Interrupting previous request."
);
2022-07-18 13:21:13 +00:00
if old_ch.send(unbounded().1).is_err() {
debug!("Could not send empty response to collisionned request, probably because request was interrupted. Dropping response.");
}
}
2020-12-02 12:30:47 +00:00
trace!("request: query_send {}, {} bytes", id, bytes.len());
2022-02-18 19:23:10 +00:00
#[cfg(feature = "telemetry")]
span.set_attribute(KeyValue::new("len_query", bytes.len() as i64));
query_send.send((id, prio, Framing::new(bytes, stream).into_stream()))?;
2020-12-02 12:30:47 +00:00
cfg_if::cfg_if! {
if #[cfg(feature = "telemetry")] {
let stream = resp_recv
.with_context(Context::current_with_span(span))
.await?;
} else {
let stream = resp_recv.await?;
}
}
let (resp, stream) = Framing::from_stream(stream).await?.into_parts();
if resp.is_empty() {
return Err(Error::Message(
"Response is 0 bytes, either a collision or a protocol error".into(),
));
}
2020-12-02 12:30:47 +00:00
trace!("request response {}: ", id);
2021-10-12 15:59:46 +00:00
let code = resp[0];
if code == 0 {
let ser_resp = rmp_serde::decode::from_read_ref(&resp[1..])?;
2022-07-21 18:22:56 +00:00
Ok(Resp {
_phantom: Default::default(),
msg: ser_resp,
body: BodyData::Stream(stream),
})
2021-10-12 15:59:46 +00:00
} else {
let msg = String::from_utf8(resp[1..].to_vec()).unwrap_or_default();
Err(Error::Remote(code, msg))
2021-10-12 15:59:46 +00:00
}
2020-12-02 12:30:47 +00:00
}
}
impl SendLoop for ClientConn {}
#[async_trait]
impl RecvLoop for ClientConn {
2022-07-18 13:21:13 +00:00
fn recv_handler(self: &Arc<Self>, id: RequestID, stream: UnboundedReceiver<Packet>) {
trace!("ClientConn recv_handler {}", id);
2020-12-07 15:00:12 +00:00
let mut inflight = self.inflight.lock().unwrap();
if let Some(ch) = inflight.remove(&id) {
if ch.send(stream).is_err() {
debug!("Could not send request response, probably because request was interrupted. Dropping response.");
}
}
2020-12-02 12:30:47 +00:00
}
}