Skip to content

web search done right

Slight change of sonic protocol so close happens from client side. This should allow us to disable lingering for the socket since the client will only close the connection once it has received adequate data to satisfy the user request. No stray tcp packets should therefore hit the socket.

Mikkel Denkere9301e3e84e8
diff --git a/core/src/api/webgraph.rs b/core/src/api/webgraph.rs
index af780bcb..4d785d3e 100644
--- a/core/src/api/webgraph.rs
+++ b/core/src/api/webgraph.rs
@@ -65,7 +65,7 @@ pub async fn similar_sites(
.with_limit(Duration::from_millis(200))
.take(5);
- let mut conn = sonic::service::ResilientConnection::create_with_timeout(
+ let conn = sonic::service::ResilientConnection::create_with_timeout(
host,
Duration::from_secs(30),
retry,
@@ -114,7 +114,7 @@ pub async fn knows_site(
.with_limit(Duration::from_millis(200))
.take(5);
- let mut conn = sonic::service::ResilientConnection::create_with_timeout(
+ let conn = sonic::service::ResilientConnection::create_with_timeout(
host,
Duration::from_secs(30),
retry,
diff --git a/core/src/crawler/worker.rs b/core/src/crawler/worker.rs
index 412485a7..eaa41111 100644
--- a/core/src/crawler/worker.rs
+++ b/core/src/crawler/worker.rs
@@ -138,7 +138,7 @@ impl WorkerThread {
}
}
} else {
- let mut conn = self.coordinator_conn().await.unwrap();
+ let conn = self.coordinator_conn().await.unwrap();
let results = self.results.lock().await.drain(..).collect::<Vec<_>>();
let res = conn
diff --git a/core/src/distributed/sonic/mod.rs b/core/src/distributed/sonic/mod.rs
index fc516ebf..c07167da 100644
--- a/core/src/distributed/sonic/mod.rs
+++ b/core/src/distributed/sonic/mod.rs
@@ -40,63 +40,17 @@ pub enum Error {
#[error("Failed to get response for request: connection timeout")]
RequestTimeout,
+ #[error("Could not build connection pool")]
+ PoolCreation,
+
#[error("Other")]
Other(#[from] anyhow::Error),
}
-pub struct Server<Req, Res> {
- pub(super) listener: TcpListener,
- marker: PhantomData<(Req, Res)>,
-}
pub struct Connection<Req, Res> {
stream: TcpStream,
marker: PhantomData<(Req, Res)>,
}
-pub struct Request<Req, Res> {
- stream: TcpStream,
- body: Option<Req>,
- marker: PhantomData<(Req, Res)>,
-}
-
-#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
-#[repr(C)]
-struct Header {
- body_size: usize,
-}
-
-impl<Req, Res> Server<Req, Res>
-where
- Req: DeserializeOwned,
-{
- pub async fn bind(addr: impl ToSocketAddrs) -> Result<Self> {
- let listener = TcpListener::bind(addr).await?;
- Ok(Server {
- listener,
- marker: PhantomData,
- })
- }
- pub async fn accept(&self) -> Result<Request<Req, Res>> {
- let (mut stream, client) = self.listener.accept().await?;
- tracing::debug!("accepted connection from: {}", &client);
-
- let mut header_buf = vec![0; std::mem::size_of::<Header>()];
- stream.read_exact(&mut header_buf).await?;
- let header: Header = *bytemuck::from_bytes(&header_buf);
-
- let mut buf = vec![0; header.body_size];
-
- stream.read_exact(&mut buf).await?;
- tracing::debug!("received bytes: {:?}", &buf);
-
- let body = Some(bincode::deserialize(&buf).unwrap());
-
- Ok(Request {
- stream,
- body,
- marker: PhantomData,
- })
- }
-}
impl<Req, Res> Connection<Req, Res>
where
@@ -123,9 +77,14 @@ where
}
}
- pub async fn send_without_timeout(&mut self, request: &Req) -> Result<Res> {
+ pub async fn send_without_timeout(mut self, request: &Req) -> Result<Res> {
let bytes = bincode::serialize(&request).unwrap();
+ // disable linger to avoid TIME_WAIT.
+ // should be safe since the connection is closed from the client side.
+ // No stray packets should therefore find its way to the socket.
+ self.stream.set_linger(Some(Duration::from_secs(0)))?;
+
let header = Header {
body_size: bytes.len(),
};
@@ -140,18 +99,18 @@ where
let mut buf = vec![0; header.body_size];
self.stream.read_exact(&mut buf).await?;
-
self.stream.flush().await?;
+ self.stream.shutdown().await?;
Ok(bincode::deserialize(&buf).unwrap())
}
- pub async fn send(&mut self, request: &Req) -> Result<Res> {
+ pub async fn send(self, request: &Req) -> Result<Res> {
self.send_with_timeout(request, Duration::from_secs(90))
.await
}
- pub async fn send_with_timeout(&mut self, request: &Req, timeout: Duration) -> Result<Res> {
+ pub async fn send_with_timeout(self, request: &Req, timeout: Duration) -> Result<Res> {
match tokio::time::timeout(timeout, self.send_without_timeout(request)).await {
Ok(res) => res,
Err(_) => Err(Error::RequestTimeout),
@@ -159,6 +118,58 @@ where
}
}
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+#[repr(C)]
+struct Header {
+ body_size: usize,
+}
+
+pub struct Server<Req, Res> {
+ pub(super) listener: TcpListener,
+ marker: PhantomData<(Req, Res)>,
+}
+
+impl<Req, Res> Server<Req, Res>
+where
+ Req: DeserializeOwned,
+{
+ pub async fn bind(addr: impl ToSocketAddrs) -> Result<Self> {
+ let listener = TcpListener::bind(addr).await?;
+ Ok(Server {
+ listener,
+ marker: PhantomData,
+ })
+ }
+
+ pub async fn accept(&self) -> Result<Request<Req, Res>> {
+ let (mut stream, client) = self.listener.accept().await?;
+ tracing::debug!("accepted connection from: {}", &client);
+
+ let mut header_buf = vec![0; std::mem::size_of::<Header>()];
+ stream.read_exact(&mut header_buf).await?;
+ let header: Header = *bytemuck::from_bytes(&header_buf);
+
+ let mut buf = vec![0; header.body_size];
+
+ stream.read_exact(&mut buf).await?;
+ tracing::debug!("received bytes: {:?}", &buf);
+
+ let body = Some(bincode::deserialize(&buf).unwrap());
+
+ Ok(Request {
+ stream,
+ body,
+ marker: PhantomData,
+ })
+ }
+}
+
+pub struct Request<Req, Res> {
+ stream: TcpStream,
+ body: Option<Req>,
+ marker: PhantomData<(Req, Res)>,
+}
+
impl<Req, Res> Request<Req, Res>
where
Res: Serialize,
@@ -173,13 +184,19 @@ where
self.stream.write_all(&bytes).await?;
self.stream.flush().await?;
- self.stream.shutdown().await?;
+ // wait for client to close connection
+ let mut buf: [u8; 1] = [0];
+ tokio::time::timeout(Duration::from_secs(5), self.stream.read_exact(&mut buf))
+ .await
+ .ok();
Ok(())
}
+
pub fn body(&self) -> &Req {
self.body.as_ref().unwrap()
}
+
fn take_body(&mut self) -> Req {
self.body.take().expect("body was taken twice")
}
@@ -217,7 +234,7 @@ where
}
}
- pub async fn send_with_timeout(&mut self, request: &Req, timeout: Duration) -> Result<Res> {
+ pub async fn send_with_timeout(self, request: &Req, timeout: Duration) -> Result<Res> {
self.conn.send_with_timeout(request, timeout).await
}
}
@@ -280,7 +297,7 @@ mod tests {
req.respond(b1).await?;
Ok(())
},
- |mut con| async move {
+ |con| async move {
let res = con.send(&a2).await?;
prop_assert_eq!(res, b2);
Ok(())
diff --git a/core/src/distributed/sonic/service.rs b/core/src/distributed/sonic/service.rs
index c1a378fe..8b1a292d 100644
--- a/core/src/distributed/sonic/service.rs
+++ b/core/src/distributed/sonic/service.rs
@@ -5,10 +5,10 @@ use tokio::net::ToSocketAddrs;
use super::Result;
#[async_trait::async_trait]
-pub trait Service: Sized {
- type Request: serde::de::DeserializeOwned;
- type RequestRef<'a>: serde::Serialize;
- type Response: serde::Serialize + serde::de::DeserializeOwned;
+pub trait Service: Sized + Send + Sync {
+ type Request: serde::de::DeserializeOwned + Send + Sync;
+ type RequestRef<'a>: serde::Serialize + Send + Sync;
+ type Response: serde::Serialize + serde::de::DeserializeOwned + Send + Sync;
async fn handle(req: Self::Request, server: &mut Self) -> Result<Self::Response>;
}
@@ -63,10 +63,7 @@ impl<'a, S: Service> Connection<'a, S> {
})
}
#[allow(dead_code)]
- pub async fn send_without_timeout<R: Wrapper<S>>(
- &mut self,
- request: &'a R,
- ) -> Result<R::Response> {
+ pub async fn send_without_timeout<R: Wrapper<S>>(self, request: &'a R) -> Result<R::Response> {
Ok(R::unwrap_response(
self.inner
.send_without_timeout(&R::wrap_request_ref(request))
@@ -75,12 +72,12 @@ impl<'a, S: Service> Connection<'a, S> {
.unwrap())
}
#[allow(dead_code)]
- pub async fn send<R: Wrapper<S>>(&mut self, request: &'a R) -> Result<R::Response> {
+ pub async fn send<R: Wrapper<S>>(self, request: &'a R) -> Result<R::Response> {
Ok(R::unwrap_response(self.inner.send(&R::wrap_request_ref(request)).await?).unwrap())
}
#[allow(dead_code)]
pub async fn send_with_timeout<R: Wrapper<S>>(
- &mut self,
+ self,
request: &'a R,
timeout: Duration,
) -> Result<R::Response> {
@@ -108,9 +105,8 @@ impl<'a, S: Service> ResilientConnection<'a, S> {
})
}
- /// If `req_timeout` is None, send with default timeout of 90 seconds.
pub async fn send_with_timeout<R: Wrapper<S>>(
- &mut self,
+ self,
request: &'a R,
timeout: Duration,
) -> Result<R::Response> {
diff --git a/core/src/mapreduce/manager.rs b/core/src/mapreduce/manager.rs
index fd77690f..2ebb3326 100644
--- a/core/src/mapreduce/manager.rs
+++ b/core/src/mapreduce/manager.rs
@@ -51,7 +51,7 @@ impl RemoteWorker {
I: Map<W, O> + Send,
O: Serialize + DeserializeOwned + Send,
{
- let mut conn = self.connect::<W, I, O>().await?;
+ let conn = self.connect::<W, I, O>().await?;
match conn.send(&Task::Job(job)).await {
Ok(Some(res)) => Ok(res),
_ => Err(Error::NoResponse),
@@ -65,7 +65,7 @@ impl RemoteWorker {
O: Serialize + DeserializeOwned + Send,
{
debug!("closing worker {:}", self.addr);
- let mut conn = self.connect().await?;
+ let conn = self.connect().await?;
let res = conn.send(&Task::<I>::AllFinished).await?;
debug_assert!(res.is_none());
diff --git a/core/src/searcher/distributed.rs b/core/src/searcher/distributed.rs
index 8d1bd172..3f02dcf6 100644
--- a/core/src/searcher/distributed.rs
+++ b/core/src/searcher/distributed.rs
@@ -55,7 +55,7 @@ pub enum Error {
impl RemoteSearcher {
async fn search(&self, query: &SearchQuery) -> Result<InitialWebsiteResult> {
- let mut conn = self.conn().await;
+ let conn = self.conn().await;
if let Ok(Some(body)) = conn
.send_with_timeout(
@@ -77,7 +77,7 @@ impl RemoteSearcher {
pointers: &[inverted_index::WebsitePointer],
original_query: &str,
) -> Result<Vec<RetrievedWebpage>> {
- let mut conn = self.conn().await;
+ let conn = self.conn().await;
if let Ok(Some(body)) = conn
.send_with_timeout(
@@ -95,7 +95,7 @@ impl RemoteSearcher {
}
async fn get_webpage(&self, url: &str) -> Result<Option<RetrievedWebpage>> {
- let mut conn = self.conn().await;
+ let conn = self.conn().await;
if let Ok(body) = conn
.send_with_timeout(
@@ -113,7 +113,7 @@ impl RemoteSearcher {
}
async fn get_homepage_descriptions(&self, urls: &[Url]) -> HashMap<Url, String> {
- let mut conn = self.conn().await;
+ let conn = self.conn().await;
if let Ok(body) = conn
.send_with_timeout(