Try to wrap stream into SCC to factor the errored and completed checks

This commit is contained in:
Dominik Werder
2021-04-30 20:20:30 +02:00
parent 168e532974
commit a23400aaf8
5 changed files with 94 additions and 28 deletions

View File

@@ -185,7 +185,7 @@ pub fn pre_binned_bytes_for_http(
query: &PreBinnedQuery, query: &PreBinnedQuery,
) -> Result<PreBinnedValueByteStream, Error> { ) -> Result<PreBinnedValueByteStream, Error> {
info!("pre_binned_bytes_for_http {:?} {:?}", query, node); info!("pre_binned_bytes_for_http {:?} {:?}", query, node);
let ret = PreBinnedValueByteStream::new( let ret = super::cache::pbv::pre_binned_value_byte_stream_new(
query.patch.clone(), query.patch.clone(),
query.channel.clone(), query.channel.clone(),
query.agg_kind.clone(), query.agg_kind.clone(),

38
disk/src/cache/pbv.rs vendored
View File

@@ -9,6 +9,7 @@ use err::Error;
use futures_core::Stream; use futures_core::Stream;
use futures_util::{FutureExt, StreamExt}; use futures_util::{FutureExt, StreamExt};
use netpod::log::*; use netpod::log::*;
use netpod::streamext::SCC;
use netpod::{ use netpod::{
AggKind, BinnedRange, Channel, NanoRange, NodeConfig, PreBinnedPatchCoord, PreBinnedPatchIterator, AggKind, BinnedRange, Channel, NanoRange, NodeConfig, PreBinnedPatchCoord, PreBinnedPatchIterator,
PreBinnedPatchRange, PreBinnedPatchRange,
@@ -17,41 +18,32 @@ use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
pub struct PreBinnedValueByteStream { pub type PreBinnedValueByteStream = SCC<PreBinnedValueByteStreamInner>;
pub struct PreBinnedValueByteStreamInner {
inp: PreBinnedValueStream, inp: PreBinnedValueStream,
errored: bool,
completed: bool,
} }
impl PreBinnedValueByteStream { pub fn pre_binned_value_byte_stream_new(
pub fn new(patch: PreBinnedPatchCoord, channel: Channel, agg_kind: AggKind, node_config: &NodeConfig) -> Self { patch: PreBinnedPatchCoord,
Self { channel: Channel,
inp: PreBinnedValueStream::new(patch, channel, agg_kind, node_config), agg_kind: AggKind,
errored: false, node_config: &NodeConfig,
completed: false, ) -> PreBinnedValueByteStream {
} let s1 = PreBinnedValueStream::new(patch, channel, agg_kind, node_config);
} let s2 = PreBinnedValueByteStreamInner { inp: s1 };
SCC::new(s2)
} }
impl Stream for PreBinnedValueByteStream { impl Stream for PreBinnedValueByteStreamInner {
type Item = Result<Bytes, Error>; type Item = Result<Bytes, Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
use Poll::*; use Poll::*;
if self.completed {
panic!("poll_next on completed");
}
if self.errored {
self.completed = true;
return Ready(None);
}
match self.inp.poll_next_unpin(cx) { match self.inp.poll_next_unpin(cx) {
Ready(Some(item)) => match make_frame::<PreBinnedHttpFrame>(&item) { Ready(Some(item)) => match make_frame::<PreBinnedHttpFrame>(&item) {
Ok(buf) => Ready(Some(Ok(buf.freeze()))), Ok(buf) => Ready(Some(Ok(buf.freeze()))),
Err(e) => { Err(e) => Ready(Some(Err(e.into()))),
self.errored = true;
Ready(Some(Err(e.into())))
}
}, },
Ready(None) => Ready(None), Ready(None) => Ready(None),
Pending => Pending, Pending => Pending,

View File

@@ -10,6 +10,8 @@ use timeunits::*;
#[allow(unused_imports)] #[allow(unused_imports)]
use tracing::{debug, error, info, trace, warn}; use tracing::{debug, error, info, trace, warn};
pub mod streamext;
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AggQuerySingleChannel { pub struct AggQuerySingleChannel {
pub channel_config: ChannelConfig, pub channel_config: ChannelConfig,

72
netpod/src/streamext.rs Normal file
View File

@@ -0,0 +1,72 @@
use err::Error;
use futures_core::Stream;
use futures_util::StreamExt;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct SCC<S>
where
S: Stream,
{
inp: S,
errored: bool,
completed: bool,
}
impl<S> SCC<S>
where
S: Stream,
{
pub fn new(inp: S) -> Self {
Self {
inp,
errored: false,
completed: false,
}
}
}
impl<S, I> Stream for SCC<S>
where
S: Stream<Item = Result<I, Error>> + Unpin,
{
type Item = <S as Stream>::Item;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
use Poll::*;
if self.completed {
panic!("poll_next on completed");
} else if self.errored {
self.completed = true;
Ready(None)
} else {
match self.inp.poll_next_unpin(cx) {
Ready(Some(Ok(k))) => Ready(Some(Ok(k))),
Ready(Some(Err(e))) => {
self.errored = true;
Ready(Some(Err(e)))
}
Ready(None) => {
self.completed = true;
Ready(None)
}
Pending => Pending,
}
}
}
}
pub trait IntoSCC<S>
where
S: Stream,
{
fn into_scc(self) -> SCC<S>;
}
impl<S> IntoSCC<S> for S
where
S: Stream,
{
fn into_scc(self) -> SCC<S> {
SCC::new(self)
}
}

View File

@@ -52,21 +52,21 @@ pub async fn get_binned(
let g = match item { let g = match item {
Ok(frame) => { Ok(frame) => {
type ExpectedType = disk::cache::BinnedBytesForHttpStreamFrame; type ExpectedType = disk::cache::BinnedBytesForHttpStreamFrame;
info!("frame len {}", frame.buf().len()); let n1 = frame.buf().len();
match bincode::deserialize::<ExpectedType>(frame.buf()) { match bincode::deserialize::<ExpectedType>(frame.buf()) {
Ok(item) => match item { Ok(item) => match item {
Ok(item) => { Ok(item) => {
info!("item: {:?}", item); info!("len {} item {:?}", n1, item);
bin_count += 1; bin_count += 1;
Some(Ok(item)) Some(Ok(item))
} }
Err(e) => { Err(e) => {
error!("error frame: {:?}", e); error!("len {} error frame {:?}", n1, e);
Some(Err(e)) Some(Err(e))
} }
}, },
Err(e) => { Err(e) => {
error!("bincode error: {:?}", e); error!("len {} bincode error {:?}", n1, e);
Some(Err(e.into())) Some(Err(e.into()))
} }
} }