Skip to content

Commit 61b0f76

Browse files
authored
[branch-54] Resolve lost wakeup in SpillPoolReader with multiple conc… (#23654)
## Which issue does this PR close? - part of #22547 54.x branch backport of fix for #23447 ## Rationale for this change See main PR #23522 ## What changes are included in this PR? See main PR #23522 ## Are these changes tested? Additional test cases added to verify existing behaviour Fix manually tested with reproduction code from #23447 ## Are there any user-facing changes? No
1 parent 2142d5b commit 61b0f76

2 files changed

Lines changed: 199 additions & 61 deletions

File tree

‎datafusion/physical-plan/src/repartition/mod.rs‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,7 +2913,7 @@ mod tests {
29132913

29142914
#[cfg(test)]
29152915
mod test {
2916-
use arrow::array::record_batch;
2916+
use arrow::array::{UInt32Array, record_batch};
29172917
use arrow::compute::SortOptions;
29182918
use arrow::datatypes::{DataType, Field, Schema};
29192919
use datafusion_common::assert_batches_eq;
@@ -3214,6 +3214,95 @@ mod test {
32143214
Ok(())
32153215
}
32163216

3217+
/// Regression test for order preservation across spill *file rotation*.
3218+
///
3219+
/// A `preserve_order` repartition relies on each per-(input, output) spill pool delivering
3220+
/// batches in strict FIFO order (see [`spill_pool::channel`] / [`SpillPoolWriter`]). This uses
3221+
/// the same memory profile as [`Self::test_preserve_order_with_spilling`] — which is tuned to
3222+
/// force spilling while still completing — but additionally sets `max_spill_file_size_bytes`
3223+
/// to 1 so every spilled batch lands in its own file. That exercises the FIFO-across-rotation
3224+
/// path: if ordering were lost across rotated files (e.g. by feeding an ordered pool with a
3225+
/// shared multi-producer writer), the downstream `StreamingMerge` would emit out-of-order rows
3226+
/// and the sortedness assertion below would fail.
3227+
#[tokio::test]
3228+
async fn test_preserve_order_with_spill_file_rotation() -> Result<()> {
3229+
use datafusion_execution::config::SessionConfig;
3230+
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
3231+
3232+
// Same sorted input as `test_preserve_order_with_spilling`:
3233+
// Partition1: [1,3], [5,7], [9,11]; Partition2: [2,4], [6,8], [10,12]
3234+
let batch1 = record_batch!(("c0", UInt32, [1, 3])).unwrap();
3235+
let batch2 = record_batch!(("c0", UInt32, [2, 4])).unwrap();
3236+
let batch3 = record_batch!(("c0", UInt32, [5, 7])).unwrap();
3237+
let batch4 = record_batch!(("c0", UInt32, [6, 8])).unwrap();
3238+
let batch5 = record_batch!(("c0", UInt32, [9, 11])).unwrap();
3239+
let batch6 = record_batch!(("c0", UInt32, [10, 12])).unwrap();
3240+
let schema = batch1.schema();
3241+
let sort_exprs = LexOrdering::new([PhysicalSortExpr {
3242+
expr: col("c0", &schema).unwrap(),
3243+
options: SortOptions::default().asc(),
3244+
}])
3245+
.unwrap();
3246+
let partition1 = vec![batch1, batch3, batch5];
3247+
let partition2 = vec![batch2, batch4, batch6];
3248+
let input_partitions = vec![partition1, partition2];
3249+
3250+
// Force a new spill file per spilled batch to exercise FIFO across rotation.
3251+
let mut session_config = SessionConfig::new();
3252+
session_config
3253+
.options_mut()
3254+
.execution
3255+
.max_spill_file_size_bytes = 1;
3256+
// Same tight limit as `test_preserve_order_with_spilling`: forces spilling while leaving
3257+
// the merge enough non-spillable headroom to complete.
3258+
let runtime = RuntimeEnvBuilder::default()
3259+
.with_memory_limit(608, 1.0)
3260+
.build_arc()?;
3261+
let task_ctx = Arc::new(
3262+
TaskContext::default()
3263+
.with_session_config(session_config)
3264+
.with_runtime(runtime),
3265+
);
3266+
3267+
let exec = TestMemoryExec::try_new(&input_partitions, Arc::clone(&schema), None)?
3268+
.try_with_sort_information(vec![sort_exprs.clone(), sort_exprs])?;
3269+
let exec = Arc::new(TestMemoryExec::update_cache(&Arc::new(exec)));
3270+
let exec = RepartitionExec::try_new(exec, Partitioning::RoundRobinBatch(3))?
3271+
.with_preserve_order();
3272+
3273+
// Each output partition merges sorted substreams, so its rows must be non-decreasing.
3274+
for i in 0..exec.partitioning().partition_count() {
3275+
let mut stream = exec.execute(i, Arc::clone(&task_ctx))?;
3276+
let mut last: Option<u32> = None;
3277+
while let Some(result) = stream.next().await {
3278+
let batch = result?;
3279+
let col = batch
3280+
.column(0)
3281+
.as_any()
3282+
.downcast_ref::<UInt32Array>()
3283+
.unwrap();
3284+
for r in 0..col.len() {
3285+
let v = col.value(r);
3286+
if let Some(prev) = last {
3287+
assert!(
3288+
prev <= v,
3289+
"output partition {i} not sorted: {prev} came before {v}"
3290+
);
3291+
}
3292+
last = Some(v);
3293+
}
3294+
}
3295+
}
3296+
3297+
let metrics = exec.metrics().unwrap();
3298+
assert!(
3299+
metrics.spill_count().unwrap() > 0,
3300+
"Expected spilling to occur for order-preserving repartition at this \
3301+
memory limit. If this fails, the memory limit may need adjustment."
3302+
);
3303+
Ok(())
3304+
}
3305+
32173306
fn test_schema() -> Arc<Schema> {
32183307
Arc::new(Schema::new(vec![Field::new("c0", DataType::UInt32, false)]))
32193308
}

‎datafusion/physical-plan/src/spill/spill_pool.rs‎

Lines changed: 109 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use futures::{Stream, StreamExt};
1919
use std::collections::VecDeque;
20+
use std::mem;
2021
use std::sync::Arc;
2122
use std::task::Waker;
2223

@@ -56,11 +57,9 @@ struct SpillPoolShared {
5657
spill_manager: Arc<SpillManager>,
5758
/// Pool-level waker to notify when new files are available (single reader)
5859
waker: Option<Waker>,
59-
/// Whether the writer has been dropped (no more files will be added)
60-
writer_dropped: bool,
6160
/// Writer's reference to the current file (shared by all cloned writers).
6261
/// Has its own lock to allow I/O without blocking queue access.
63-
current_write_file: Option<Arc<Mutex<ActiveSpillFileShared>>>,
62+
open_write_files: VecDeque<Arc<Mutex<ActiveSpillFileShared>>>,
6463
/// Number of active writer clones. Only when this reaches zero should
6564
/// `writer_dropped` be set to true. This prevents premature EOF signaling
6665
/// when one writer clone is dropped while others are still active.
@@ -74,8 +73,7 @@ impl SpillPoolShared {
7473
files: VecDeque::new(),
7574
spill_manager,
7675
waker: None,
77-
writer_dropped: false,
78-
current_write_file: None,
76+
open_write_files: VecDeque::new(),
7977
active_writer_count: 1,
8078
}
8179
}
@@ -171,8 +169,10 @@ impl SpillPoolWriter {
171169
// Fine-grained locking: Lock shared state briefly for queue access
172170
let mut shared = self.shared.lock();
173171

174-
// Create new file if we don't have one yet
175-
if shared.current_write_file.is_none() {
172+
// Create new file if there is none available to append to
173+
let write_file = if !shared.open_write_files.is_empty() {
174+
shared.open_write_files.pop_front().unwrap()
175+
} else {
176176
let spill_manager = Arc::clone(&shared.spill_manager);
177177
// Release shared lock before disk I/O (fine-grained locking)
178178
drop(shared);
@@ -193,52 +193,49 @@ impl SpillPoolWriter {
193193
// Re-acquire lock and push to shared queue
194194
shared = self.shared.lock();
195195
shared.files.push_back(Arc::clone(&file_shared));
196-
shared.current_write_file = Some(file_shared);
197196
shared.wake(); // Wake readers waiting for new files
198-
}
197+
file_shared
198+
};
199199

200-
let current_write_file = shared.current_write_file.take();
201200
// Release shared lock before file I/O (fine-grained locking)
202201
// This allows readers to access the queue while we do disk I/O
203202
drop(shared);
204203

205204
// Write batch to current file - lock only the specific file
206-
if let Some(current_file) = current_write_file {
207-
// Now lock just this file for I/O (separate from shared lock)
208-
let mut file_shared = current_file.lock();
209-
210-
// Append the batch
211-
if let Some(ref mut writer) = file_shared.writer {
212-
writer.append_batch(batch)?;
213-
// make sure we flush the writer for readers
214-
writer.flush()?;
215-
file_shared.batches_written += 1;
216-
file_shared.estimated_size += batch_size;
217-
}
205+
let mut file_shared = write_file.lock();
206+
207+
// Append the batch
208+
if let Some(ref mut writer) = file_shared.writer {
209+
writer.append_batch(batch)?;
210+
// make sure we flush the writer for readers
211+
writer.flush()?;
212+
file_shared.batches_written += 1;
213+
file_shared.estimated_size += batch_size;
214+
}
218215

219-
// Wake reader waiting on this specific file
220-
file_shared.wake();
216+
// Wake reader waiting on this specific file
217+
file_shared.wake();
221218

222-
// Check if we need to rotate
223-
let needs_rotation = file_shared.estimated_size > self.max_file_size_bytes;
219+
let max_file_size_reached = file_shared.estimated_size > self.max_file_size_bytes;
224220

225-
if needs_rotation {
226-
// Finish the IPC writer
227-
if let Some(mut writer) = file_shared.writer.take() {
228-
writer.finish()?;
229-
}
230-
// Mark as finished so readers know not to wait for more data
231-
file_shared.writer_finished = true;
232-
// Wake reader waiting on this file (it's now finished)
233-
file_shared.wake();
234-
// Don't put back current_write_file - let it rotate
235-
} else {
236-
// Release file lock
237-
drop(file_shared);
238-
// Put back the current file for further writing
239-
let mut shared = self.shared.lock();
240-
shared.current_write_file = Some(current_file);
221+
if max_file_size_reached {
222+
// Finish the IPC writer
223+
if let Some(mut writer) = file_shared.writer.take() {
224+
writer.finish()?;
241225
}
226+
// Mark as finished so readers know not to wait for more data
227+
file_shared.writer_finished = true;
228+
// Wake reader waiting on this file (it's now finished)
229+
file_shared.wake();
230+
231+
// Don't place `write_file` back in the `open_write_files` queue so we don't
232+
// try writing to it again
233+
} else {
234+
// Release file lock
235+
drop(file_shared);
236+
// Put back the current file for further writing
237+
let mut shared = self.shared.lock();
238+
shared.open_write_files.push_back(write_file);
242239
}
243240

244241
Ok(())
@@ -258,31 +255,32 @@ impl Drop for SpillPoolWriter {
258255
return;
259256
}
260257

261-
// Finalize the current file when the last writer is dropped
262-
if let Some(current_file) = shared.current_write_file.take() {
263-
// Release shared lock before locking file
258+
// Finalize any spill files that were not finished yet
259+
if !shared.open_write_files.is_empty() {
260+
let files = mem::take(&mut shared.open_write_files);
264261
drop(shared);
265262

266-
let mut file_shared = current_file.lock();
263+
for file in files {
264+
let mut file_shared = file.lock();
267265

268-
// Finish the current writer if it exists
269-
if let Some(mut writer) = file_shared.writer.take() {
270-
// Ignore errors on drop - we're in destructor
271-
let _ = writer.finish();
272-
}
266+
// Finish the current writer if it exists
267+
if let Some(mut writer) = file_shared.writer.take() {
268+
// Ignore errors on drop - we're in destructor
269+
let _ = writer.finish();
270+
}
273271

274-
// Mark as finished so readers know not to wait for more data
275-
file_shared.writer_finished = true;
272+
// Mark as finished so readers know not to wait for more data
273+
file_shared.writer_finished = true;
276274

277-
// Wake reader waiting on this file (it's now finished)
278-
file_shared.wake();
275+
// Wake reader waiting on this file (it's now finished)
276+
file_shared.wake();
277+
drop(file_shared);
278+
}
279279

280-
drop(file_shared);
281280
shared = self.shared.lock();
282281
}
283282

284-
// Mark writer as dropped and wake pool-level readers
285-
shared.writer_dropped = true;
283+
// Wake pool-level readers
286284
shared.wake();
287285
}
288286
}
@@ -722,7 +720,7 @@ impl Stream for SpillPoolReader {
722720
}
723721

724722
// No files in queue - check if writer is done
725-
if shared.writer_dropped {
723+
if shared.active_writer_count == 0 {
726724
// Writer is done and no more files will be added - EOF
727725
return Poll::Ready(None);
728726
}
@@ -746,7 +744,7 @@ mod tests {
746744
use crate::metrics::{ExecutionPlanMetricsSet, SpillMetrics};
747745
use arrow::array::{ArrayRef, Int32Array};
748746
use arrow::datatypes::{DataType, Field, Schema};
749-
use datafusion_common_runtime::SpawnedTask;
747+
use datafusion_common_runtime::{JoinSet, SpawnedTask};
750748
use datafusion_execution::runtime_env::RuntimeEnv;
751749

752750
fn create_test_schema() -> SchemaRef {
@@ -1204,6 +1202,57 @@ mod tests {
12041202
Ok(())
12051203
}
12061204

1205+
#[tokio::test(flavor = "multi_thread", worker_threads = 10)]
1206+
async fn test_concurrent_writers() -> Result<()> {
1207+
let (writer, mut reader) = create_spill_channel(1024 * 1024);
1208+
1209+
// Spawn writer tasks
1210+
let mut writer_join_set = JoinSet::new();
1211+
for w in 0..10 {
1212+
let writer = writer.clone();
1213+
writer_join_set.spawn(async move {
1214+
for b in 0..10 {
1215+
let batch = create_test_batch((w * 100) + (b * 10), 10);
1216+
writer.push_batch(&batch).unwrap();
1217+
}
1218+
});
1219+
}
1220+
drop(writer);
1221+
1222+
// Reader task (runs concurrently)
1223+
let reader_handle = SpawnedTask::spawn(async move {
1224+
let mut batch_order = vec![];
1225+
loop {
1226+
match reader.next().await {
1227+
None => break,
1228+
Some(batch) => {
1229+
let batch = batch.unwrap();
1230+
1231+
assert_eq!(batch.num_rows(), 10);
1232+
1233+
let col = batch
1234+
.column(0)
1235+
.as_any()
1236+
.downcast_ref::<Int32Array>()
1237+
.unwrap();
1238+
batch_order.push(col.value(0) / 10);
1239+
}
1240+
}
1241+
}
1242+
batch_order
1243+
});
1244+
1245+
// Wait for both to complete
1246+
writer_join_set.join_all().await;
1247+
let mut batch_order = reader_handle.await.unwrap();
1248+
1249+
// When used with multiple writers, order is not guaranteed
1250+
batch_order.sort();
1251+
assert_eq!(batch_order, (0i32..100i32).collect::<Vec<_>>());
1252+
1253+
Ok(())
1254+
}
1255+
12071256
#[tokio::test]
12081257
async fn test_reader_catches_up_to_writer() -> Result<()> {
12091258
let (writer, mut reader) = create_spill_channel(1024 * 1024);

0 commit comments

Comments
 (0)