Skip to content

Commit b966c27

Browse files
committed
refactor(layout): use blocked IO for queued and outstanding reads
Signed-off-by: Joe Isaacs <[email protected]>
1 parent a9686a3 commit b966c27

5 files changed

Lines changed: 36 additions & 34 deletions

File tree

‎vortex-layout/src/plan/physical/operator.rs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ pub enum Step {
3434
pub enum SourceReady {
3535
/// The next compute call can produce output or finish without blocking.
3636
Ready,
37-
/// New reads were queued through [`Cx::request_read`]. Run again when bytes are available.
38-
Read,
3937
/// Nothing can happen until the condition clears.
4038
Blocked(SourceBlocked),
4139
}
4240

4341
/// Why a source cannot compute yet.
4442
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4543
pub enum SourceBlocked {
46-
/// A read was requested and has not been delivered.
44+
/// Wait for bytes from newly queued or outstanding reads.
4745
Io,
4846
/// The inlet needs more batches or its sink to close it. It must be open and have room
4947
/// for its sink to push; debug builds check that the sink can still wake this source.
@@ -57,9 +55,10 @@ pub trait Source: Operator {
5755
/// Returning [`SourceReady::Ready`] guarantees that call can proceed without blocking.
5856
/// Any input or choice made here can be retained by the source for that call.
5957
///
60-
/// Queue one or more reads with [`Cx::request_read`] and return [`SourceReady::Read`] to
61-
/// issue them. Readiness is checked again when bytes are available, including bytes already
62-
/// delivered; they are available through [`Cx::take_segment`] in arrival order.
58+
/// Queue reads with [`Cx::request_read`] and return
59+
/// `SourceReady::Blocked(SourceBlocked::Io)`. The driver issues newly queued reads
60+
/// before waiting. Readiness is checked again when bytes are available, including bytes
61+
/// already delivered; they are available through [`Cx::take_segment`] in arrival order.
6362
fn ready(&mut self, _cx: &mut Cx<'_>) -> VortexResult<SourceReady> {
6463
Ok(SourceReady::Ready)
6564
}
@@ -153,7 +152,7 @@ pub struct Cx<'a> {
153152
pub(super) arena: &'a mut Arena,
154153
pub(super) inlets: &'a [PortId],
155154
pub(super) bytes: &'a mut VecDeque<(SegmentId, BufferHandle)>,
156-
pub(super) reads: SmallVec<[SegmentId; 1]>,
155+
pub(super) reads: &'a mut SmallVec<[SegmentId; 1]>,
157156
pub(super) session: &'a VortexSession,
158157
pub(super) exec: &'a mut ExecutionCtx,
159158
pub(super) touched: &'a mut InletSet,
@@ -169,8 +168,9 @@ impl Cx<'_> {
169168
self.changed.pop()
170169
}
171170

172-
/// Queues a segment read during [`Source::ready`]. Return [`SourceReady::Read`] after
173-
/// queuing all the reads needed for this call.
171+
/// Queues a segment read during [`Source::ready`]. Return
172+
/// `SourceReady::Blocked(SourceBlocked::Io)` after queuing the reads needed for
173+
/// this call.
174174
pub fn request_read(&mut self, segment: SegmentId) {
175175
self.reads.push(segment);
176176
}

‎vortex-layout/src/plan/physical/pipeline/scan.rs‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ pub(crate) struct Pipeline {
124124

125125
/// What one run of a pipeline came to.
126126
enum Progress {
127-
/// The source wants these segments read.
128-
Read(SmallVec<[SegmentId; 1]>),
129127
/// The source waits for this.
130128
Blocked(SourceBlocked),
131129
/// An outlet is full.
@@ -134,12 +132,13 @@ enum Progress {
134132
Done,
135133
}
136134

137-
/// Runs `pipe` until it blocks, finishes, or requests a read.
135+
/// Runs `pipe` until it blocks or finishes, collecting any reads its source requests.
138136
fn drive(
139137
pipe: &mut Pipeline,
140138
arena: &mut Arena,
141139
session: &VortexSession,
142140
exec: &mut ExecutionCtx,
141+
reads: &mut SmallVec<[SegmentId; 1]>,
143142
) -> VortexResult<Progress> {
144143
let Pipeline {
145144
source,
@@ -158,7 +157,7 @@ fn drive(
158157
arena,
159158
inlets,
160159
bytes,
161-
reads: SmallVec::new(),
160+
reads,
162161
session,
163162
exec,
164163
touched,
@@ -190,12 +189,11 @@ fn drive(
190189
let ready = source.ready(&mut cx)?;
191190
cx.changed.clear();
192191
vortex_ensure!(
193-
cx.reads.is_empty() == (ready != SourceReady::Read),
194-
"A source must return Read exactly when it queues reads"
192+
cx.reads.is_empty() || ready == SourceReady::Blocked(SourceBlocked::Io),
193+
"A source that queues reads must block on IO"
195194
);
196195
match ready {
197196
SourceReady::Ready => {}
198-
SourceReady::Read => return Ok(Progress::Read(cx.reads)),
199197
SourceReady::Blocked(blocked) => return Ok(Progress::Blocked(blocked)),
200198
}
201199
}
@@ -393,20 +391,20 @@ impl Core {
393391
.as_mut()
394392
.ok_or_else(|| vortex_err!("Pipeline {id} is not live"))?;
395393
pipe.queued = false;
396-
let progress = drive(pipe, &mut self.arena, &self.session, &mut self.exec)?;
394+
let mut reads = SmallVec::new();
395+
let progress = drive(
396+
pipe,
397+
&mut self.arena,
398+
&self.session,
399+
&mut self.exec,
400+
&mut reads,
401+
)?;
402+
for segment_id in reads {
403+
self.ask(id, segment_id);
404+
}
397405
#[cfg(debug_assertions)]
398406
self.validate_progress(id, &progress);
399407
match &progress {
400-
Progress::Read(segments) => {
401-
for &segment_id in segments {
402-
self.ask(id, segment_id);
403-
}
404-
if self.pipeline(id).bytes.is_empty() {
405-
self.pipeline_mut(id).state = State::Blocked(SourceBlocked::Io);
406-
} else {
407-
self.make_runnable(id);
408-
}
409-
}
410408
// A read-ahead source can still have delivered bytes to consume.
411409
Progress::Blocked(SourceBlocked::Io) if !self.pipeline(id).bytes.is_empty() => {
412410
self.make_runnable(id);

‎vortex-layout/src/plan/physical/pipeline/scan/validate.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl Core {
8080
);
8181
}
8282
Progress::Blocked(SourceBlocked::Io) => assert!(
83-
pipe.pending > 0,
84-
"pipeline {id} waits for a read with none in flight"
83+
pipe.pending > 0 || !pipe.bytes.is_empty(),
84+
"pipeline {id} waits for a read with none in flight or delivered"
8585
),
8686
Progress::Done => {
8787
for (index, &port) in pipe.inlets.iter().enumerate() {
@@ -93,7 +93,7 @@ impl Core {
9393
);
9494
}
9595
}
96-
Progress::Read(_) | Progress::Outlet => {}
96+
Progress::Outlet => {}
9797
}
9898
}
9999
}

‎vortex-layout/src/plan/physical/pipeline/scheduling_tests.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl Source for ManyReads {
218218
while let Some(segment) = self.wanted.pop() {
219219
cx.request_read(segment);
220220
}
221-
return Ok(SourceReady::Read);
221+
return Ok(SourceReady::Blocked(SourceBlocked::Io));
222222
}
223223
Ok(if self.left == 0 || cx.has_bytes() {
224224
SourceReady::Ready
@@ -267,6 +267,10 @@ fn a_source_has_several_reads_in_flight(#[values(1, 3)] deliveries: usize) -> Vo
267267
);
268268
first_wait = false;
269269
}
270+
assert!(
271+
matches!(scan.step()?, Turn::Waiting),
272+
"waiting for IO must not reissue reads or compute before bytes arrive"
273+
);
270274
// Deliver the last asked for first.
271275
for _ in 0..deliveries {
272276
let read = inflight
@@ -305,7 +309,7 @@ impl Source for ReadAfterInlet {
305309
};
306310
cx.request_read(SegmentId::from(u32::try_from(batch.len())?));
307311
self.batch = Some(batch);
308-
Ok(SourceReady::Read)
312+
Ok(SourceReady::Blocked(SourceBlocked::Io))
309313
}
310314
}
311315

@@ -370,7 +374,7 @@ impl Source for FirstOf {
370374
while let Some(segment) = self.wanted.pop() {
371375
cx.request_read(segment);
372376
}
373-
return Ok(SourceReady::Read);
377+
return Ok(SourceReady::Blocked(SourceBlocked::Io));
374378
}
375379
Ok(if cx.has_bytes() {
376380
SourceReady::Ready

‎vortex-layout/src/plan/physical/pipeline/synthetic.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Source for RowSource {
179179
if let Some(segment) = self.segment.take() {
180180
self.waiting = true;
181181
cx.request_read(segment);
182-
return Ok(SourceReady::Read);
182+
return Ok(SourceReady::Blocked(SourceBlocked::Io));
183183
}
184184
Ok(if self.waiting && !cx.has_bytes() {
185185
SourceReady::Blocked(SourceBlocked::Io)

0 commit comments

Comments
 (0)