web search done right
Refactor sonic service to use primitive sonic connections
Mikkel Denker0b11b20c6dc5
diff --git a/core/src/api/webgraph.rs b/core/src/api/webgraph.rsindex fec808fd..af780bcb 100644--- a/core/src/api/webgraph.rs+++ b/core/src/api/webgraph.rs@@ -65,16 +65,21 @@ pub async fn similar_sites(.with_limit(Duration::from_millis(200)).take(5);- let conn = sonic::service::ResilientConnection::create(host, retry);+ let mut conn = sonic::service::ResilientConnection::create_with_timeout(+ host,+ Duration::from_secs(30),+ retry,+ )+ .await+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;match conn- .send(+ .send_with_timeout(&crate::entrypoint::webgraph_server::SimilarSites {sites: params.sites,top_n: params.top_n,},- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{@@ -109,13 +114,18 @@ pub async fn knows_site(.with_limit(Duration::from_millis(200)).take(5);- let conn = sonic::service::ResilientConnection::create(host, retry);+ let mut conn = sonic::service::ResilientConnection::create_with_timeout(+ host,+ Duration::from_secs(30),+ retry,+ )+ .await+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;match conn- .send(+ .send_with_timeout(&crate::entrypoint::webgraph_server::Knows { site: params.site },- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{diff --git a/core/src/crawler/mod.rs b/core/src/crawler/mod.rsindex d857d2cf..93b165fe 100644--- a/core/src/crawler/mod.rs+++ b/core/src/crawler/mod.rs@@ -53,7 +53,7 @@ pub enum Error {InvalidRedirect,}-type Result<T> = std::result::Result<T, anyhow::Error>;+type Result<T, E = anyhow::Error> = std::result::Result<T, E>;#[derive(Clone, Debug, PartialEq, Eq, Hash)]struct Site(String);diff --git a/core/src/crawler/worker.rs b/core/src/crawler/worker.rsindex 913743ec..412485a7 100644--- a/core/src/crawler/worker.rs+++ b/core/src/crawler/worker.rs@@ -102,12 +102,17 @@ impl WorkerThread {})}- fn coordinator_conn(+ async fn coordinator_conn(&self,- ) -> sonic::service::ResilientConnection<CoordinatorService, ExponentialBackoff> {+ ) -> Result<sonic::service::ResilientConnection<CoordinatorService>> {let retry = ExponentialBackoff::from_millis(1_000).with_limit(Duration::from_secs(10));- sonic::service::ResilientConnection::create(self.coordinator_host, retry)+ Ok(sonic::service::ResilientConnection::create_with_timeout(+ self.coordinator_host,+ Duration::from_secs(30),+ retry,+ )+ .await?)}pub async fn run(mut self) {@@ -133,17 +138,16 @@ impl WorkerThread {}}} else {- let conn = self.coordinator_conn();+ let mut conn = self.coordinator_conn().await.unwrap();let results = self.results.lock().await.drain(..).collect::<Vec<_>>();let res = conn- .send(+ .send_with_timeout(&NewJobs {responses: results,num_jobs: 2 * self.num_jobs_per_fetch,},- Duration::from_secs(60),- Some(Duration::from_secs(60 * 60)),+ Duration::from_secs(60 * 60),).await;diff --git a/core/src/distributed/sonic/mod.rs b/core/src/distributed/sonic/mod.rsindex 2b55c60a..fc516ebf 100644--- a/core/src/distributed/sonic/mod.rs+++ b/core/src/distributed/sonic/mod.rs@@ -123,7 +123,7 @@ 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();let header = Header {@@ -141,17 +141,17 @@ wherelet mut buf = vec![0; header.body_size];self.stream.read_exact(&mut buf).await?;- self.stream.shutdown().await?;+ self.stream.flush().await?;Ok(bincode::deserialize(&buf).unwrap())}- pub async fn send(self, request: &Req) -> Result<Res> {+ pub async fn send(&mut self, request: &Req) -> Result<Res> {self.send_with_timeout(request, Duration::from_secs(90)).await}- pub async fn send_with_timeout(self, request: &Req, timeout: Duration) -> Result<Res> {+ pub async fn send_with_timeout(&mut 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),@@ -185,6 +185,43 @@ where}}+pub struct ResilientConnection<Req, Res> {+ conn: Connection<Req, Res>,+}++impl<Req, Res> ResilientConnection<Req, Res>+where+ Req: Serialize,+ Res: DeserializeOwned,+{+ pub async fn create_with_timeout(+ server: impl ToSocketAddrs + Clone,+ timeout: Duration,+ retry: impl Iterator<Item = Duration>,+ ) -> Result<Self> {+ let mut conn = Connection::create_with_timeout(server.clone(), timeout).await;+ let mut retry = retry;++ loop {+ match conn {+ Ok(conn) => return Ok(ResilientConnection { conn }),+ Err(_) => {+ if let Some(timeout) = retry.next() {+ tokio::time::sleep(timeout).await;+ conn = Connection::create_with_timeout(server.clone(), timeout).await;+ } else {+ return Err(Error::ConnectionTimeout);+ }+ }+ }+ }+ }++ pub async fn send_with_timeout(&mut self, request: &Req, timeout: Duration) -> Result<Res> {+ self.conn.send_with_timeout(request, timeout).await+ }+}+#[cfg(test)]mod tests {use std::{collections::HashMap, future::Future};@@ -243,7 +280,7 @@ mod tests {req.respond(b1).await?;Ok(())},- |con| async move {+ |mut 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.rsindex 90862096..c1a378fe 100644--- a/core/src/distributed/sonic/service.rs+++ b/core/src/distributed/sonic/service.rs@@ -1,8 +1,8 @@-use std::{marker::PhantomData, net::SocketAddr, time::Duration};+use std::{net::SocketAddr, time::Duration};use tokio::net::ToSocketAddrs;-use super::{Error, Result};+use super::Result;#[async_trait::async_trait]pub trait Service: Sized {@@ -13,10 +13,6 @@ pub trait Service: Sized {async fn handle(req: Self::Request, server: &mut Self) -> Result<Self::Response>;}-pub struct Connection<'a, S: Service> {- inner: super::Connection<S::RequestRef<'a>, S::Response>,-}-#[async_trait::async_trait]pub trait Message<S: Service> {type Response;@@ -32,12 +28,6 @@ pub struct Server<S: Service> {service: S,}-pub struct ResilientConnection<S: Service, Rt: Iterator<Item = Duration>> {- addr: SocketAddr,- retry: Rt,- marker: PhantomData<S>,-}-impl<S: Service> Server<S> {pub async fn bind(service: S, addr: impl ToSocketAddrs) -> Result<Self> {Ok(Server {@@ -52,6 +42,10 @@ impl<S: Service> Server<S> {}}+pub struct Connection<'a, S: Service> {+ inner: super::Connection<S::RequestRef<'a>, S::Response>,+}+impl<'a, S: Service> Connection<'a, S> {#[allow(dead_code)]pub async fn create(server: impl ToSocketAddrs) -> Result<Connection<'a, S>> {@@ -69,7 +63,10 @@ impl<'a, S: Service> Connection<'a, S> {})}#[allow(dead_code)]- pub async fn send_without_timeout<R: Wrapper<S>>(self, request: &'a R) -> Result<R::Response> {+ pub async fn send_without_timeout<R: Wrapper<S>>(+ &mut self,+ request: &'a R,+ ) -> Result<R::Response> {Ok(R::unwrap_response(self.inner.send_without_timeout(&R::wrap_request_ref(request))@@ -78,12 +75,12 @@ impl<'a, S: Service> Connection<'a, S> {.unwrap())}#[allow(dead_code)]- pub async fn send<R: Wrapper<S>>(self, request: &'a R) -> Result<R::Response> {+ pub async fn send<R: Wrapper<S>>(&mut 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>>(- self,+ &mut self,request: &'a R,timeout: Duration,) -> Result<R::Response> {@@ -96,42 +93,29 @@ impl<'a, S: Service> Connection<'a, S> {}}-impl<S: Service, Rt: Iterator<Item = Duration>> ResilientConnection<S, Rt> {- pub fn create(addr: SocketAddr, retry: Rt) -> Self {- Self {- addr,- retry,- marker: PhantomData,- }+pub struct ResilientConnection<'a, S: Service> {+ inner: super::ResilientConnection<S::RequestRef<'a>, S::Response>,+}++impl<'a, S: Service> ResilientConnection<'a, S> {+ pub async fn create_with_timeout<Rt: Iterator<Item = Duration>>(+ addr: SocketAddr,+ timeout: Duration,+ retry: Rt,+ ) -> Result<ResilientConnection<'a, S>> {+ Ok(Self {+ inner: super::ResilientConnection::create_with_timeout(addr, timeout, retry).await?,+ })}/// If `req_timeout` is None, send with default timeout of 90 seconds.- pub async fn send<R: Wrapper<S>>(- mut self,- request: &R,- conn_timeout: Duration,- req_timeout: Option<Duration>,+ pub async fn send_with_timeout<R: Wrapper<S>>(+ &mut self,+ request: &'a R,+ timeout: Duration,) -> Result<R::Response> {- loop {- match Connection::create_with_timeout(&self.addr, conn_timeout).await {- Ok(conn) => {- if let Some(req_timeout) = req_timeout {- return conn.send_with_timeout(request, req_timeout).await;- } else {- return conn.send(request).await;- }- }- Err(Error::ConnectionTimeout) => {- if let Some(timeout) = self.retry.next() {- tokio::time::sleep(timeout).await;- continue;- } else {- return Err(Error::ConnectionTimeout);- }- }- Err(e) => return Err(e),- }- }+ let req_ref = R::wrap_request_ref(request);+ Ok(R::unwrap_response(self.inner.send_with_timeout(&req_ref, timeout).await?).unwrap())}}diff --git a/core/src/mapreduce/manager.rs b/core/src/mapreduce/manager.rsindex 2ebb3326..fd77690f 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 conn = self.connect::<W, I, O>().await?;+ let mut 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 conn = self.connect().await?;+ let mut 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.rsindex 33520426..8d1bd172 100644--- a/core/src/searcher/distributed.rs+++ b/core/src/searcher/distributed.rs@@ -55,15 +55,14 @@ pub enum Error {impl RemoteSearcher {async fn search(&self, query: &SearchQuery) -> Result<InitialWebsiteResult> {- let conn = self.conn();+ let mut conn = self.conn().await;if let Ok(Some(body)) = conn- .send(+ .send_with_timeout(&search_server::Search {query: query.clone(),},- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{@@ -78,16 +77,15 @@ impl RemoteSearcher {pointers: &[inverted_index::WebsitePointer],original_query: &str,) -> Result<Vec<RetrievedWebpage>> {- let conn = self.conn();+ let mut conn = self.conn().await;if let Ok(Some(body)) = conn- .send(+ .send_with_timeout(&search_server::RetrieveWebsites {websites: pointers.to_vec(),query: original_query.to_string(),},- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{@@ -97,15 +95,14 @@ impl RemoteSearcher {}async fn get_webpage(&self, url: &str) -> Result<Option<RetrievedWebpage>> {- let conn = self.conn();+ let mut conn = self.conn().await;if let Ok(body) = conn- .send(+ .send_with_timeout(&search_server::GetWebpage {url: url.to_string(),},- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{@@ -116,15 +113,14 @@ impl RemoteSearcher {}async fn get_homepage_descriptions(&self, urls: &[Url]) -> HashMap<Url, String> {- let conn = self.conn();+ let mut conn = self.conn().await;if let Ok(body) = conn- .send(+ .send_with_timeout(&search_server::GetHomepageDescriptions {urls: urls.to_vec(),},- Duration::from_secs(5),- Some(Duration::from_secs(60)),+ Duration::from_secs(60),).await{@@ -134,14 +130,18 @@ impl RemoteSearcher {HashMap::new()}- fn conn(- &self,- ) -> sonic::service::ResilientConnection<SearchService, impl Iterator<Item = Duration>> {+ async fn conn(&self) -> sonic::service::ResilientConnection<SearchService> {let retry = ExponentialBackoff::from_millis(30).with_limit(Duration::from_millis(200)).take(5);- sonic::service::ResilientConnection::create(self.addr, retry)+ sonic::service::ResilientConnection::create_with_timeout(+ self.addr,+ Duration::from_secs(30),+ retry,+ )+ .await+ .unwrap()}}