Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pd: use tokio_util::sync::PollSender in consensus svc #436

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ csv = "1.1"
directories = "4.0"
tokio = { version = "1.16", features = ["full"]}
tokio-stream = "0.1"
tokio-util = "0.6"
tokio-util = "0.7"
tower = { version = "0.4", features = ["full"]}
tracing = "0.1"
regex = "1.5"
Expand Down
66 changes: 14 additions & 52 deletions pd/src/consensus/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,18 @@ use std::{
task::{Context, Poll},
};

use futures::{ready, FutureExt};
use futures::FutureExt;
use tendermint::abci::{ConsensusRequest, ConsensusResponse};
use tokio::sync::{
mpsc::{self, error::SendError, OwnedPermit},
oneshot,
};
use tokio_util::sync::ReusableBoxFuture;
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::PollSender;
use tower_abci::BoxError;

use super::{Message, Worker};
use crate::{state, RequestExt};

enum State {
NoPermit,
Waiting,
Permit(OwnedPermit<Message>),
}

#[derive(Clone)]
pub struct Consensus {
queue: mpsc::Sender<Message>,
future: ReusableBoxFuture<Result<OwnedPermit<Message>, SendError<()>>>,
state: State,
queue: PollSender<Message>,
}

impl Consensus {
Expand All @@ -35,60 +25,32 @@ impl Consensus {
tokio::spawn(Worker::new(state, queue_rx).await?.run());

Ok(Self {
queue: queue_tx,
state: State::NoPermit,
future: ReusableBoxFuture::new(async { unreachable!() }),
queue: PollSender::new(queue_tx),
})
}
}

impl Clone for Consensus {
fn clone(&self) -> Self {
Self {
queue: self.queue.clone(),
state: State::NoPermit,
future: ReusableBoxFuture::new(async { unreachable!() }),
}
}
}

impl tower::Service<ConsensusRequest> for Consensus {
type Response = ConsensusResponse;
type Error = BoxError;
type Future =
Pin<Box<dyn Future<Output = Result<ConsensusResponse, BoxError>> + Send + 'static>>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.state {
State::Permit(_) => Poll::Ready(Ok(())),
State::NoPermit => {
self.future.set(self.queue.clone().reserve_owned());
self.state = State::Waiting;
self.poll_ready(cx)
}
State::Waiting => {
let permit = ready!(self.future.poll(cx))?;
self.state = State::Permit(permit);
Poll::Ready(Ok(()))
}
}
self.queue.poll_reserve(cx).map_err(Into::into)
}

fn call(&mut self, req: ConsensusRequest) -> Self::Future {
let permit = if let State::Permit(p) = std::mem::replace(&mut self.state, State::NoPermit) {
p
} else {
panic!("called without poll_ready");
};

let span = req.create_span();
let (tx, rx) = oneshot::channel();

permit.send(Message {
req,
rsp_sender: tx,
span,
});
self.queue
Comment on lines 44 to +47
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may want to check if the channel is closed here and bail early (in case it closed between a successful poll_ready call and the call to call). i don't know how that matters in pd, specifically, though, since there's only ever going to be a single consensus background worker and it's expected to live for the entire lifetime of the node...if it's gone, panicking in the send_item call is probably also fine.

I didn't change this because the previous code didn't have such a check.

.send_item(Message {
req,
rsp_sender: tx,
span,
})
.expect("called without `poll_ready`");

async move { Ok(rx.await.expect("worker error??")) }.boxed()
}
Expand Down