You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "ding-young (via GitHub)" <gi...@apache.org> on 2025/06/30 01:05:39 UTC
[PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
ding-young opened a new pull request, #16616:
URL: https://github.com/apache/datafusion/pull/16616
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #16482 .
## Rationale for this change
As described in above issue, since we introduced cooperative stream when reading spill file, we need to test that the reader stream properly yields. Since it is not obvious to write a test using SpillManager api, this PR adds a test that manually mocks `SpillManager::read_spill_as_stream`. (Note that writing to a spill file does not use the stream API.)
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
In the test, the producer/sender stream always returns `Poll::Ready`, consumer/receiver stream polls it repeatedly and buffer capacity for `spawn_buffered` is set large enough so that yielding behavior comes from `CooperativeStream`.
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
The visibility of `spawn_buffered` has changed
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2247700676
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,58 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
+ // To make sure that inner stream is polled multiple times, loop until the buffer is full
+ // Ideally, the stream will yield before the loop ends
+ for _ in 0..buffer_capacity {
+ match mock_stream.as_mut().poll_next(cx) {
+ Poll::Ready(Some(Ok(batch))) => {
+ collected.push(batch);
+ }
+ Poll::Ready(Some(Err(e))) => {
+ return Poll::Ready(Some(Err(e)));
+ }
+ Poll::Ready(None) => {
+ break;
+ }
+ Poll::Pending => {
+ // polling inner stream may return Pending only when it reaches budget, since
+ // we intentionally made ProducerStream always return Ready
+ return Poll::Pending;
+ }
+ }
+ }
+
+ // This should be unreachable since the stream is canceled
+ unreachable!("Expected the stream to be canceled, but it continued polling");
+ });
Review Comment:
Updated (Clippy told me to use `unreachable!`), thanks @comphead
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2244192033
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,62 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use arrow::compute::concat_batches;
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
+ // To make sure that inner stream is polled multiple times, loop until the buffer is full
+ // Ideally, the stream will yield before the loop ends
+ for _ in 0..buffer_capacity {
Review Comment:
Updated to loop until the buffer_capacity
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#issuecomment-3145541443
Thanks again @ding-young and @comphead
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "pepijnve (via GitHub)" <gi...@apache.org>.
pepijnve commented on PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#issuecomment-3139965024
> @pepijnve I wonder if you have some time to review this PR as well?
@alamb IIRC, I provided feedback out of band which was integrated by @ding-young
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #16616:
URL: https://github.com/apache/datafusion/pull/16616
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2174042836
##########
datafusion/physical-plan/src/common.rs:
##########
@@ -91,7 +91,7 @@ fn build_file_list_recurse(
/// If running in a tokio context spawns the execution of `stream` to a separate task
/// allowing it to execute in parallel with an intermediate buffer of size `buffer`
-pub(crate) fn spawn_buffered(
+pub fn spawn_buffered(
Review Comment:
I changed the visibility of `spawn_buffered` to pub to use it in the test.
If this isn't ideal, I'll follow any suggestions. (Perhaps duplicating the code might be better?)
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2246843792
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,63 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use arrow::compute::concat_batches;
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
+ // To make sure that inner stream is polled multiple times, loop until the buffer is full
+ // Ideally, the stream will yield before the loop ends
+ for _ in 0..buffer_capacity {
+ match mock_stream.as_mut().poll_next(cx) {
+ Poll::Ready(Some(Ok(batch))) => {
+ collected.push(batch);
+ }
+ Poll::Ready(Some(Err(e))) => {
+ return Poll::Ready(Some(Err(e)));
+ }
+ Poll::Ready(None) => {
+ break;
+ }
+ Poll::Pending => {
+ // polling inner stream may return Pending only when it reaches budget, since
+ // we intentionally made ProducerStream always return Ready
+ return Poll::Pending;
+ }
+ }
+ }
+
+ // This should be unreachable since the stream is canceled
+ assert!(false, "This should be unreachable if cooperative stream properly yields");
Review Comment:
Added ! Thanks @comphead
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#issuecomment-3020899458
@pepijnve I wonder if you have some time to review this PR as well?
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2246843792
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,63 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use arrow::compute::concat_batches;
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
+ // To make sure that inner stream is polled multiple times, loop until the buffer is full
+ // Ideally, the stream will yield before the loop ends
+ for _ in 0..buffer_capacity {
+ match mock_stream.as_mut().poll_next(cx) {
+ Poll::Ready(Some(Ok(batch))) => {
+ collected.push(batch);
+ }
+ Poll::Ready(Some(Err(e))) => {
+ return Poll::Ready(Some(Err(e)));
+ }
+ Poll::Ready(None) => {
+ break;
+ }
+ Poll::Pending => {
+ // polling inner stream may return Pending only when it reaches budget, since
+ // we intentionally made ProducerStream always return Ready
+ return Poll::Pending;
+ }
+ }
+ }
+
+ // This should be unreachable since the stream is canceled
+ assert!(false, "This should be unreachable if cooperative stream properly yields");
Review Comment:
Added ! Thanks @comphead
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2175983510
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,61 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use arrow::compute::concat_batches;
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
Review Comment:
I would think of having some cap of iterations or timelimit instead of infinite loop?
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#issuecomment-3017408194
Thank you @pepijnve and fyi @zhuqi-lucas
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2246355066
##########
datafusion/physical-plan/src/common.rs:
##########
@@ -91,7 +91,7 @@ fn build_file_list_recurse(
/// If running in a tokio context spawns the execution of `stream` to a separate task
/// allowing it to execute in parallel with an intermediate buffer of size `buffer`
-pub(crate) fn spawn_buffered(
+pub fn spawn_buffered(
Review Comment:
this seems fine to me
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Add tests for yielding in `SpillManager::read_spill_as_stream` [datafusion]
Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on code in PR #16616:
URL: https://github.com/apache/datafusion/pull/16616#discussion_r2244192336
##########
datafusion/core/tests/execution/coop.rs:
##########
@@ -250,6 +252,61 @@ async fn agg_grouped_topk_yields(
query_yields(aggr, session_ctx.task_ctx()).await
}
+#[rstest]
+#[tokio::test]
+// A test that mocks the behavior of `SpillManager::read_spill_as_stream` without file access
+// to verify that a cooperative stream would properly yields in a spill file read scenario
+async fn spill_reader_stream_yield() -> Result<(), Box<dyn Error>> {
+ use arrow::compute::concat_batches;
+ use datafusion_physical_plan::common::spawn_buffered;
+
+ // A mock stream that always returns `Poll::Ready(Some(...))` immediately
+ let always_ready =
+ make_lazy_exec("value", false).execute(0, SessionContext::new().task_ctx())?;
+
+ // this function makes a consumer stream that resembles how read_stream from spill file is constructed
+ let stream = make_cooperative(always_ready);
+
+ // Set large buffer so that buffer always has free space for the producer/sender
+ let buffer_capacity = 100_000;
+ let mut mock_stream = spawn_buffered(stream, buffer_capacity);
+ let schema = mock_stream.schema();
+
+ let consumer_stream = futures::stream::poll_fn(move |cx| {
+ let mut collected = vec![];
Review Comment:
Fixed :)
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org