JingsongLi commented on code in PR #1475:
URL: https://github.com/apache/paimon-rust/pull/1475#discussion_r4674880641
##########
crates/paimon/src/table/read_execution.rs:
##########
@@ -169,34 +205,79 @@ fn merge_streams(
let handle = match tokio::runtime::Handle::try_current() {
Ok(handle) => handle,
Err(error) => {
- drop(streams);
+ drop(items);
Err(crate::Error::UnexpectedError {
message: "Parallel table reads require a Tokio
runtime".into(),
source: Some(Box::new(error)),
})?;
return;
}
};
- let count = streams.len();
+ let count = items.len();
+ let mut unscheduled = VecDeque::from(items);
+ let mut next_index = 0;
let mut pending = JoinSet::new();
- for (index, stream) in streams.into_iter().enumerate() {
- pending.spawn_on(next_batch(index, stream), &handle);
+ // A worker panic has no returned stream index. Keep its task ID so a
+ // finite ordered read can defer the failure until that split is
needed.
+ let mut task_indices = HashMap::new();
+ for _ in 0..parallelism.min(count) {
+ if let Some(item) = unscheduled.pop_front() {
+ let index = next_index;
+ next_index += 1;
+ let task = pending.spawn_on(next_batch(index,
make_stream(item)), &handle);
Review Comment:
**[P2] Preserve the current split's share of the memory budget**
I reproduced this with an ordered `LIMIT 2`, parallelism 2, and a shared 1
MiB `ResourceContext`. With split 0's Parquet footer delayed, split 1 reserves
600,183 bytes for its row group and holds the reservation while its first batch
waits behind split 0. Split 0 then returns `ResourceExhausted` when it tries to
reserve the same amount, even though it can supply both requested rows. The
same test succeeds on `ab2eaef4`, where the second worker starts on the smaller
split 2.
Give the current split priority for memory admission, or use demand-driven
split scheduling for finite ordered reads with a shared memory budget.
##########
crates/paimon/src/table/read_execution.rs:
##########
@@ -169,34 +205,79 @@ fn merge_streams(
let handle = match tokio::runtime::Handle::try_current() {
Ok(handle) => handle,
Err(error) => {
- drop(streams);
+ drop(items);
Err(crate::Error::UnexpectedError {
message: "Parallel table reads require a Tokio
runtime".into(),
source: Some(Box::new(error)),
})?;
return;
}
};
- let count = streams.len();
+ let count = items.len();
+ let mut unscheduled = VecDeque::from(items);
+ let mut next_index = 0;
let mut pending = JoinSet::new();
- for (index, stream) in streams.into_iter().enumerate() {
- pending.spawn_on(next_batch(index, stream), &handle);
+ // A worker panic has no returned stream index. Keep its task ID so a
+ // finite ordered read can defer the failure until that split is
needed.
+ let mut task_indices = HashMap::new();
+ for _ in 0..parallelism.min(count) {
+ if let Some(item) = unscheduled.pop_front() {
+ let index = next_index;
+ next_index += 1;
+ let task = pending.spawn_on(next_batch(index,
make_stream(item)), &handle);
+ if ordered && limit.is_some() {
+ task_indices.insert(task.id(), index);
+ }
+ }
}
let mut ready = BTreeMap::new();
+ let mut ready_errors = BTreeMap::new();
+ let mut completed = BTreeSet::new();
let mut current = 0;
let mut remaining = limit;
while remaining != Some(0) && current < count {
let next = if ordered {
loop {
+ if completed.remove(¤t) {
+ current += 1;
+ if current == count { break Ok(None); }
+ continue;
+ }
+ if let Some(error) = ready_errors.remove(¤t) { break
Err(error); }
if let Some(next) = ready.remove(¤t) { break
Ok(Some(next)); }
- let next = match pending.join_next().await {
- Some(Ok(next)) => next,
+ let next = match pending.join_next_with_id().await {
+ Some(Ok((id, next))) => {
+ task_indices.remove(&id);
+ next
+ }
+ Some(Err(error)) if limit.is_some() => {
+ let index = task_indices.remove(&error.id())
+ .expect("ordered worker must have a split
index");
+ ready_errors.insert(index, worker_error(error));
+ continue;
+ }
Some(Err(error)) => break Err(worker_error(error)),
None => break Ok(None),
};
- // Surface a failed worker even while an earlier group is
- // blocked; cleanup happens before exposing the error.
- if next.0 == current ||
next.1.as_ref().is_some_and(Result::is_err) {
+ if next.1.is_none() {
+ // An empty later split frees its worker slot
immediately.
+ // Keep only its completion marker until ordered output
+ // reaches it, and start the next split now.
+ completed.insert(next.0);
+ drop(next.2);
+ if let Some(item) = unscheduled.pop_front() {
Review Comment:
**[P2] Cap lookahead across consecutive empty splits for a finite LIMIT**
Each speculative EOF opens another split while the current split is still
waiting for I/O. I reproduced this with a filtered `LIMIT 1`, parallelism 2,
and 2,048 equal-size Parquet splits. Split 0 can supply the requested row, but
while it waits, splits 1 through 1,023 return no rows and the window advances
to split 1,024. The current head reads 1,025 files to return one row. The same
test reads two files on `ab2eaef4`.
Cap how far prefetch can advance beyond the ordered output position for
finite limits, and resume refilling the window as that position advances.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]