-
Notifications
You must be signed in to change notification settings - Fork 288
Add support for native async stream sources #357
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
//! Conversion to the `Stream` type from iterators. | ||
|
||
use crate::progress::Timestamp; | ||
use std::pin::Pin; | ||
use std::sync::Arc; | ||
use std::task::{Context, Poll}; | ||
|
||
use crate::Data; | ||
use crate::dataflow::channels::Message; | ||
use crate::dataflow::operators::generic::operator::source; | ||
use crate::dataflow::{Stream, Scope}; | ||
use crate::dataflow::operators::CapabilitySet; | ||
use crate::dataflow::{Scope, Stream}; | ||
use crate::progress::Timestamp; | ||
use crate::Data; | ||
|
||
/// Converts to a timely `Stream`. | ||
pub trait ToStream<T: Timestamp, D: Data> { | ||
|
@@ -56,3 +60,82 @@ impl<T: Timestamp, I: IntoIterator+'static> ToStream<T, I::Item> for I where I:: | |
}) | ||
} | ||
} | ||
|
||
/// Data and progress events of the native stream. | ||
pub enum Event<F: IntoIterator, D> { | ||
/// Indicates that timestamps have advanced to frontier F | ||
Progress(F), | ||
/// Indicates that event D happened at time T | ||
Message(F::Item, D), | ||
} | ||
|
||
/// Converts to a timely `Stream`. | ||
pub trait ToStreamAsync<T: Timestamp, D: Data> { | ||
/// Converts a [native `Stream`](futures_util::stream::Stream) of [`Event`s](Event) into a [timely | ||
frankmcsherry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// `Stream`](crate::dataflow::Stream). | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use futures_util::stream; | ||
/// | ||
/// use timely::dataflow::operators::{Capture, Event, ToStream, ToStreamAsync}; | ||
/// use timely::dataflow::operators::capture::Extract; | ||
/// | ||
/// let native_stream = stream::iter(vec![ | ||
/// Event::Message(0, 0), | ||
/// Event::Message(0, 1), | ||
/// Event::Message(0, 2), | ||
/// Event::Progress(Some(0)), | ||
/// ]); | ||
/// | ||
/// let native_stream = Box::pin(native_stream); | ||
/// | ||
/// let (data1, data2) = timely::example(|scope| { | ||
/// let data1 = native_stream.to_stream(scope).capture(); | ||
/// let data2 = vec![0,1,2].to_stream(scope).capture(); | ||
/// | ||
/// (data1, data2) | ||
/// }); | ||
/// | ||
/// assert_eq!(data1.extract(), data2.extract()); | ||
/// ``` | ||
fn to_stream<S: Scope<Timestamp = T>>(self: Pin<Box<Self>>, scope: &S) -> Stream<S, D>; | ||
} | ||
|
||
impl<T, D, F, I> ToStreamAsync<T, D> for I | ||
where | ||
D: Data, | ||
T: Timestamp, | ||
F: IntoIterator<Item = T>, | ||
I: futures_util::stream::Stream<Item = Event<F, D>> + ?Sized + 'static, | ||
{ | ||
fn to_stream<S: Scope<Timestamp = T>>(mut self: Pin<Box<Self>>, scope: &S) -> Stream<S, D> { | ||
source(scope, "ToStreamAsync", move |capability, info| { | ||
let activator = Arc::new(scope.sync_activator_for(&info.address[..])); | ||
|
||
let mut cap_set = CapabilitySet::from_elem(capability); | ||
|
||
move |output| { | ||
let waker = futures_util::task::waker_ref(&activator); | ||
let mut context = Context::from_waker(&waker); | ||
|
||
// Consume all the ready items of the source_stream and issue them to the operator | ||
while let Poll::Ready(item) = self.as_mut().poll_next(&mut context) { | ||
match item { | ||
Some(Event::Progress(time)) => { | ||
cap_set.downgrade(time); | ||
} | ||
Some(Event::Message(time, data)) => { | ||
output.session(&cap_set.delayed(&time)).give(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but this will eventually have throughput issues. At least, timely can send data around much faster than it can mint capabilities. It may eventually be smart to have the |
||
} | ||
None => { | ||
cap_set.downgrade(&[]); | ||
break; | ||
} | ||
frankmcsherry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but can this be
or is the generic argument
B
necessary / helpful?