Skip to content

Commit 6700d02

Browse files
authored
test: bound merge fan-in in the final hash aggregate spill test (apache#25424)
## Which issue does this PR close? - Closes apache#25423. ## Rationale for this change Case G of `aggregate_memory_spill.slt` runs four final partitions in a 1 MiB greedy pool. A partition that replays its spills cannot spill again. Its replay table fails to grow when its own merge buffers and the state of the other partitions fill the pool. CI hit this on two merge-queue runs (see the issue). ## What changes are included in this PR? - `aggregate_memory_spill.slt`: set `datafusion.runtime.max_spill_merge_fan_in = 2` for Case G and reset it at the end, as apache#25252 did for `ordered_aggregate_spill.slt`. The memory limit stays at 1 MiB. Cases A to F are unchanged. - `FinalHashAggregateStream::new_with_input` builds the stream over a given input, like `OrderedFinalAggregateStream::new_with_input`. `new` calls it. - Tests in `hash_stream.rs` spill and replay partition 0 while partitions 1 to 3 keep their state in a shared 1 MiB greedy pool. Channel inputs return `Pending` after each batch, so the interleaving does not depend on task scheduling. Input sizes follow the observed reservations: the held partitions read batches until they reserve 512 KiB, and partition 0 reads batches until it has spilled the requested number of times. So the cases do not depend on the memory accounting of a platform or feature set, such as `force_hash_collisions`. The tests check the grouped results against a reference, the spill metrics, and that the pool returns to the held size after completion, after a drop during replay, and after an input error, and to zero after all streams drop. These tests cover the spill and replay lifecycle; the SLT case covers the fan-in regression. ## What is the testing strategy for this PR? Local runs on this branch: - The SLT file 480 times as 16 concurrent copies, each in its own working directory. Before the change: 12 failures, all in Case G with the `ResourcesExhausted` error from the issue. After the change: 0 failures. - The new tests 50 times with default features and 20 times with `force_hash_collisions` enabled in `datafusion-common`, once with `--profile ci` and that feature as the extended CI job runs them, and all `datafusion-physical-plan` unit tests. - `cargo test -p datafusion-sqllogictest --test sqllogictests` for all files. - `cargo fmt --all -- --check` and `cargo clippy --all-targets --all-features -- -D warnings`. ## Are there any user-facing changes? No.
1 parent 1cfc355 commit 6700d02

2 files changed

Lines changed: 306 additions & 3 deletions

File tree

‎datafusion/physical-plan/src/aggregates/hash_stream.rs‎

Lines changed: 296 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,18 @@ impl FinalHashAggregateStream {
736736
));
737737
debug_assert_eq!(agg.input_order_mode, InputOrderMode::Linear);
738738

739-
let schema = Arc::clone(&agg.schema);
740739
let input = agg.input.execute(partition, Arc::clone(context))?;
740+
Self::new_with_input(agg, context, partition, input)
741+
}
742+
743+
/// Builds the stream over `input` instead of executing the plan's input.
744+
pub(in crate::aggregates) fn new_with_input(
745+
agg: &AggregateExec,
746+
context: &Arc<TaskContext>,
747+
partition: usize,
748+
input: SendableRecordBatchStream,
749+
) -> Result<Self> {
750+
let schema = Arc::clone(&agg.schema);
741751
let input_schema = input.schema();
742752
let batch_size = context.session_config().batch_size();
743753
let baseline_metrics = BaselineMetrics::new(&agg.metrics, partition);
@@ -1022,18 +1032,24 @@ mod tests {
10221032

10231033
use super::*;
10241034
use crate::aggregates::{AggregateMode, PhysicalGroupBy};
1035+
use crate::common::collect;
10251036
use crate::execution_plan::ExecutionPlan;
10261037
use crate::test::TestMemoryExec;
10271038
use crate::test::exec::BarrierExec;
10281039

1029-
use arrow::array::{AsArray, Int32Array, Int64Array};
1040+
use arrow::array::{AsArray, Int32Array, Int64Array, StringViewArray};
10301041
use arrow::datatypes::{DataType, Field, Int32Type, Schema};
10311042
use datafusion_common::Result;
1043+
use datafusion_execution::config::SessionConfig;
1044+
use datafusion_execution::memory_pool::{GreedyMemoryPool, MemoryPool};
10321045
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
10331046
use datafusion_functions_aggregate::count::count_udaf;
1047+
use datafusion_functions_aggregate::{min_max::min_udaf, sum::sum_udaf};
10341048
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
10351049
use datafusion_physical_expr::expressions::col;
1036-
use futures::StreamExt;
1050+
use futures::channel::mpsc;
1051+
use futures::{FutureExt, StreamExt};
1052+
use std::collections::BTreeMap;
10371053

10381054
#[tokio::test]
10391055
async fn test_partial_hash_stream_double_emission_race_condition_bug() -> Result<()> {
@@ -1607,4 +1623,281 @@ mod tests {
16071623

16081624
Ok(())
16091625
}
1626+
1627+
#[derive(Clone, Copy)]
1628+
enum Finish {
1629+
Collect,
1630+
DropDuringReplay,
1631+
InputError,
1632+
}
1633+
1634+
#[tokio::test]
1635+
async fn final_hash_spill_replay_with_other_partitions_holding_state() -> Result<()> {
1636+
for spills in [1, 2, 3] {
1637+
run_shared_pool_case(spills, 1024 * 1024, Finish::Collect).await?;
1638+
}
1639+
// An unlimited pool produces the reference results without spilling.
1640+
run_shared_pool_case(0, 10 * 1024 * 1024, Finish::Collect).await
1641+
}
1642+
1643+
#[tokio::test]
1644+
async fn final_hash_spill_replay_releases_memory_on_drop() -> Result<()> {
1645+
run_shared_pool_case(1, 1024 * 1024, Finish::DropDuringReplay).await
1646+
}
1647+
1648+
#[tokio::test]
1649+
async fn final_hash_spill_releases_memory_on_input_error() -> Result<()> {
1650+
run_shared_pool_case(1, 1024 * 1024, Finish::InputError).await
1651+
}
1652+
1653+
/// Partition 0 spills `spills` times and replays while partitions 1..3
1654+
/// keep their aggregate state in the same greedy pool. With `spills` at 0,
1655+
/// partition 0 reads a fixed input that fits in memory.
1656+
///
1657+
/// Input sizes follow the observed reservations, so the cases do not
1658+
/// depend on the memory accounting of a platform or feature set.
1659+
///
1660+
/// These cases cover the spill and replay lifecycle in a shared pool:
1661+
/// results, spill metrics, and memory release. Case G in
1662+
/// `aggregate_memory_spill.slt` covers the merge fan-in regression for
1663+
/// issue #25423.
1664+
async fn run_shared_pool_case(
1665+
spills: usize,
1666+
limit: usize,
1667+
finish: Finish,
1668+
) -> Result<()> {
1669+
const PARTITIONS: usize = 4;
1670+
/// Partitions 1..3 hold at least this much state.
1671+
const HELD_BYTES: usize = 512 * 1024;
1672+
/// A case that never reaches its target fails instead of looping.
1673+
const MAX_BATCHES: i64 = 1000;
1674+
1675+
let schema = Arc::new(Schema::new(vec![
1676+
Field::new("a", DataType::Int64, false),
1677+
Field::new("b", DataType::Int64, false),
1678+
Field::new("v", DataType::Int64, false),
1679+
Field::new("s", DataType::Utf8View, false),
1680+
]));
1681+
let groups = PhysicalGroupBy::new_single(vec![
1682+
(col("a", &schema)?, "a".into()),
1683+
(col("b", &schema)?, "b".into()),
1684+
]);
1685+
let expressions = vec![
1686+
Arc::new(
1687+
AggregateExprBuilder::new(sum_udaf(), vec![col("v", &schema)?])
1688+
.schema(Arc::clone(&schema))
1689+
.alias("sum")
1690+
.build()?,
1691+
),
1692+
Arc::new(
1693+
AggregateExprBuilder::new(min_udaf(), vec![col("s", &schema)?])
1694+
.schema(Arc::clone(&schema))
1695+
.alias("min")
1696+
.build()?,
1697+
),
1698+
];
1699+
let empty = TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(&schema), None)?;
1700+
let partial = AggregateExec::try_new(
1701+
AggregateMode::Partial,
1702+
groups.clone(),
1703+
expressions.clone(),
1704+
vec![None; 2],
1705+
empty,
1706+
Arc::clone(&schema),
1707+
)?;
1708+
let partial_schema = partial.schema();
1709+
let input = TestMemoryExec::try_new_exec(
1710+
&vec![vec![]; PARTITIONS],
1711+
Arc::clone(&partial_schema),
1712+
None,
1713+
)?;
1714+
let aggregate = AggregateExec::try_new(
1715+
AggregateMode::FinalPartitioned,
1716+
groups.as_final(),
1717+
expressions,
1718+
vec![None; 2],
1719+
input,
1720+
schema,
1721+
)?;
1722+
assert_eq!(aggregate.input_order_mode(), &InputOrderMode::Linear);
1723+
1724+
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(limit));
1725+
let context = Arc::new(
1726+
TaskContext::default()
1727+
.with_session_config(SessionConfig::new().with_batch_size(128))
1728+
.with_runtime(
1729+
RuntimeEnvBuilder::new()
1730+
.with_memory_pool(Arc::clone(&pool))
1731+
.build_arc()?,
1732+
),
1733+
);
1734+
let mut streams = vec![];
1735+
let mut senders = vec![];
1736+
for partition in 0..PARTITIONS {
1737+
let (sender, receiver) = mpsc::unbounded();
1738+
let input = Box::pin(RecordBatchStreamAdapter::new(
1739+
Arc::clone(&partial_schema),
1740+
receiver,
1741+
));
1742+
let stream = FinalHashAggregateStream::new_with_input(
1743+
&aggregate, &context, partition, input,
1744+
)?
1745+
.into_stream();
1746+
senders.push(sender);
1747+
streams.push(stream);
1748+
}
1749+
let mut expected = BTreeMap::new();
1750+
let mut make_batch = |partition: i64, start: i64| {
1751+
for value in start..start + 128 {
1752+
let entry = expected
1753+
.entry((
1754+
partition,
1755+
if partition == 0 && value % 128 == 0 {
1756+
0
1757+
} else {
1758+
value
1759+
},
1760+
))
1761+
.or_insert_with(|| (0, (value % 2).to_string()));
1762+
entry.0 += value * 2;
1763+
}
1764+
RecordBatch::try_new(
1765+
Arc::clone(&partial_schema),
1766+
vec![
1767+
Arc::new(Int64Array::from(vec![partition; 128])),
1768+
Arc::new(Int64Array::from_iter_values((start..start + 128).map(
1769+
|value| {
1770+
if partition == 0 && value % 128 == 0 {
1771+
0
1772+
} else {
1773+
value
1774+
}
1775+
},
1776+
))),
1777+
Arc::new(Int64Array::from_iter_values(
1778+
(start..start + 128).map(|v| v * 2),
1779+
)),
1780+
Arc::new(StringViewArray::from_iter_values(
1781+
(start..start + 128).map(|v| if v % 2 == 0 { "0" } else { "1" }),
1782+
)),
1783+
],
1784+
)
1785+
.unwrap()
1786+
};
1787+
// Channel inputs return Pending after each supplied batch, so the
1788+
// interleaving below does not depend on task scheduling.
1789+
let mut feed = |partition: usize, batch: i64| {
1790+
senders[partition]
1791+
.unbounded_send(Ok(make_batch(partition as i64, batch * 128)))
1792+
.unwrap();
1793+
assert!(streams[partition].next().now_or_never().is_none());
1794+
};
1795+
// Keep the state of partitions 1..3 live while partition 0 spills and
1796+
// replays.
1797+
let mut held_batches = 0;
1798+
while pool.reserved() < HELD_BYTES {
1799+
assert!(held_batches < MAX_BATCHES, "held state stays small");
1800+
for partition in 1..PARTITIONS {
1801+
feed(partition, held_batches);
1802+
}
1803+
held_batches += 1;
1804+
}
1805+
let held = pool.reserved();
1806+
let spill_count = || aggregate.metrics().unwrap().spill_count().unwrap();
1807+
assert_eq!(spill_count(), 0);
1808+
// Feed partition 0 until it has spilled `spills` times. Key (0, 0)
1809+
// repeats in every batch, so replay must merge its sum across runs.
1810+
let mut batches = 0;
1811+
loop {
1812+
let done = if spills == 0 {
1813+
batches == 70
1814+
} else {
1815+
spill_count() >= spills
1816+
};
1817+
if done {
1818+
break;
1819+
}
1820+
assert!(batches < MAX_BATCHES, "partition 0 did not spill");
1821+
feed(0, batches);
1822+
batches += 1;
1823+
}
1824+
// Add groups after the last spill so replay also merges the final
1825+
// in-memory run.
1826+
for _ in 0..8 {
1827+
feed(0, batches);
1828+
batches += 1;
1829+
}
1830+
assert!(spill_count() >= spills);
1831+
assert_eq!(spill_count() == 0, spills == 0);
1832+
let mut first = streams.remove(0);
1833+
match finish {
1834+
Finish::Collect => {
1835+
senders[0].close_channel();
1836+
let mut output = collect(first).await?;
1837+
assert_eq!(pool.reserved(), held);
1838+
for sender in &senders[1..] {
1839+
sender.close_channel();
1840+
}
1841+
for stream in streams.drain(..) {
1842+
output.extend(collect(stream).await?);
1843+
}
1844+
let mut actual = BTreeMap::new();
1845+
for batch in output {
1846+
let a = batch
1847+
.column(0)
1848+
.as_any()
1849+
.downcast_ref::<Int64Array>()
1850+
.unwrap();
1851+
let b = batch
1852+
.column(1)
1853+
.as_any()
1854+
.downcast_ref::<Int64Array>()
1855+
.unwrap();
1856+
let sum = batch
1857+
.column(2)
1858+
.as_any()
1859+
.downcast_ref::<Int64Array>()
1860+
.unwrap();
1861+
let min = batch
1862+
.column(3)
1863+
.as_any()
1864+
.downcast_ref::<StringViewArray>()
1865+
.unwrap();
1866+
for row in 0..batch.num_rows() {
1867+
assert!(
1868+
actual
1869+
.insert(
1870+
(a.value(row), b.value(row)),
1871+
(sum.value(row), min.value(row).to_string())
1872+
)
1873+
.is_none()
1874+
);
1875+
}
1876+
}
1877+
assert_eq!(actual, expected);
1878+
assert_eq!(spill_count() == 0, spills == 0);
1879+
}
1880+
Finish::DropDuringReplay => {
1881+
senders[0].close_channel();
1882+
first.next().await.unwrap()?;
1883+
assert!(pool.reserved() > held);
1884+
drop(first);
1885+
assert_eq!(pool.reserved(), held);
1886+
}
1887+
Finish::InputError => {
1888+
senders[0]
1889+
.unbounded_send(datafusion_common::exec_err!(
1890+
"injected input failure"
1891+
))
1892+
.unwrap();
1893+
let error = first.next().await.unwrap().unwrap_err();
1894+
assert!(error.to_string().contains("injected input failure"));
1895+
assert_eq!(pool.reserved(), held);
1896+
drop(first);
1897+
}
1898+
}
1899+
drop(streams);
1900+
assert_eq!(pool.reserved(), 0);
1901+
Ok(())
1902+
}
16101903
}

‎datafusion/sqllogictest/test_files/aggregate_memory_spill.slt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ FROM (
198198
statement ok
199199
SET datafusion.execution.target_partitions = 4
200200

201+
# Four final partitions share the greedy pool. Limit merge buffers so a
202+
# partition that replays its spills can allocate while the other partitions
203+
# retain their aggregate state.
204+
# See https://github.com/apache/datafusion/issues/25423
205+
statement ok
206+
SET datafusion.runtime.max_spill_merge_fan_in = 2
207+
201208
query II
202209
SELECT count(*), sum(total)
203210
FROM (
@@ -230,6 +237,9 @@ FROM (
230237
statement ok
231238
RESET datafusion.runtime.memory_limit
232239

240+
statement ok
241+
RESET datafusion.runtime.max_spill_merge_fan_in
242+
233243
statement ok
234244
RESET datafusion.execution.batch_size
235245

0 commit comments

Comments
 (0)