Skip to content

Commit 42c6ca5

Browse files
antiguruclaude
andcommitted
compute: address review of MV sink shared batches
* `PersistClient::batch_builder` honors `persist_batch_max_runs` when `max_runs` is `None`, so the shared and per-worker MV sink builders follow the config like `WriteHandle::builder` does. * The sync sink starts a write only once an interval has updates, so idle intervals no longer take the `SharedBatches` lock or spawn a builder task. * Test `BatchBuilder::add_part`'s lower check and flushing, and make the sweep race test detect an evicted live entry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7728c76 commit 42c6ca5

4 files changed

Lines changed: 137 additions & 10 deletions

File tree

‎src/compute/src/sink/materialized_view_v2.rs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use crate::sink::materialized_view::{
8282
BatchDescription, BatchesStream, DescsStream, DesiredStreams, OkErr, PersistApi,
8383
PersistStreams, SharedSinkFrontier, advance, operator_name, persist_source,
8484
};
85-
use crate::sink::shared_batch::{SharedBatchWrite, SharedBatchWriter};
85+
use crate::sink::shared_batch::SharedBatchWriter;
8686

8787
/// Renders an MV sink writing the given desired collection into the `target` persist collection.
8888
///
@@ -956,8 +956,7 @@ mod write {
956956
},
957957
);
958958

959-
let batches =
960-
write_batches(batch_writer.shared.start(&desc), &mut updates_rx).await;
959+
let batches = write_batches(&batch_writer.shared, &desc, &mut updates_rx).await;
961960
let corrections = read_back.await;
962961

963962
if let Err(err) = resp_tx.send(WriteResponse { batches }) {
@@ -974,18 +973,26 @@ mod write {
974973
}
975974
}
976975

977-
/// Write the updates for one interval through `write`, returning the batches this worker
978-
/// delivers.
976+
/// Write the updates for `desc` through `writer`, returning the batches this worker delivers.
979977
async fn write_batches(
980-
mut write: SharedBatchWrite<'_>,
978+
writer: &SharedBatchWriter,
979+
desc: &BatchDescription,
981980
updates_rx: &mut mpsc::Receiver<Vec<((SourceData, ()), Timestamp, StorageDiff)>>,
982981
) -> Vec<ProtoBatch> {
982+
// Start the write only once there are updates, since `maybe_start_batch` requests a write
983+
// for every ready interval, including idle ones. A worker that never joins a shared batch
984+
// cannot be the last holder responsible for delivering it.
985+
let mut write = None;
983986
while let Some(chunk) = updates_rx.recv().await {
987+
let write = write.get_or_insert_with(|| writer.start(desc));
984988
for (kv, t, d) in &chunk {
985989
write.push(kv, *t, *d).await;
986990
}
987991
}
988-
write.finish().await
992+
match write {
993+
Some(write) => write.finish().await,
994+
None => Vec::new(),
995+
}
989996
}
990997

991998
/// Apply a coalesced batch of updates to the correction buffers, on a blocking thread.

‎src/persist-client/src/batch.rs‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ mod tests {
16281628
use crate::cfg::BATCH_BUILDER_MAX_OUTSTANDING_PARTS;
16291629
use crate::internal::paths::{BlobKey, PartialBlobKey};
16301630
use crate::tests::{all_ok, new_test_client};
1631+
use mz_persist_types::codec_impls::StringSchema;
16311632

16321633
#[mz_ore::test(tokio::test)]
16331634
#[cfg_attr(miri, ignore)] // unsupported operation: returning ready events from epoll_wait is not yet implemented
@@ -1759,6 +1760,108 @@ mod tests {
17591760
}
17601761
}
17611762

1763+
fn string_part(updates: &[((&str, &str), u64, i64)]) -> Part {
1764+
let mut builder = PartBuilder::new(&StringSchema, &StringSchema);
1765+
for ((k, v), t, d) in updates {
1766+
builder.push(&k.to_string(), &v.to_string(), *t, *d);
1767+
}
1768+
builder.finish()
1769+
}
1770+
1771+
#[mz_ore::test(tokio::test)]
1772+
#[cfg_attr(miri, ignore)] // unsupported operation: returning ready events from epoll_wait is not yet implemented
1773+
async fn batch_builder_add_part_before_lower() {
1774+
let client = new_test_client(&ConfigUpdates::default()).await;
1775+
let (write, _) = client
1776+
.expect_open::<String, String, u64, i64>(ShardId::new())
1777+
.await;
1778+
let mut builder = write.builder(Antichain::from_elem(2));
1779+
let part = string_part(&[(("1", "one"), 2, 1), (("2", "two"), 1, 1)]);
1780+
match builder.add_part(part).await {
1781+
Err(InvalidUsage::UpdateNotBeyondLower { ts, .. }) => assert_eq!(ts, 1),
1782+
other => panic!("expected UpdateNotBeyondLower, got {other:?}"),
1783+
}
1784+
}
1785+
1786+
#[mz_ore::test(tokio::test)]
1787+
#[cfg_attr(miri, ignore)] // unsupported operation: returning ready events from epoll_wait is not yet implemented
1788+
async fn batch_builder_add_part_flushing() {
1789+
let small = || string_part(&[(("1", "one"), 1, 1)]);
1790+
let big = string_part(&[
1791+
(("2", "two"), 1, 1),
1792+
(("3", "three"), 1, 1),
1793+
(("4", "four"), 1, 1),
1794+
(("5", "five"), 1, 1),
1795+
]);
1796+
let small_bytes = small().goodbytes();
1797+
assert!(
1798+
big.goodbytes() > 2 * small_bytes,
1799+
"big part must exceed the target"
1800+
);
1801+
1802+
let cache = PersistClientCache::new_no_metrics();
1803+
// Two small parts fit the target exactly. Disable inline writes so every flushed part is
1804+
// a separate blob that `part_count` sees.
1805+
cache.cfg.set_config(&BLOB_TARGET_SIZE, 2 * small_bytes);
1806+
cache.cfg.set_config(&INLINE_WRITES_SINGLE_MAX_BYTES, 0);
1807+
cache.cfg.set_config(&INLINE_WRITES_TOTAL_MAX_BYTES, 0);
1808+
let client = cache
1809+
.open(PersistLocation::new_in_mem())
1810+
.await
1811+
.expect("client construction failed");
1812+
let (mut write, mut read) = client
1813+
.expect_open::<String, String, u64, i64>(ShardId::new())
1814+
.await;
1815+
1816+
let mut builder = write.builder(Antichain::from_elem(0));
1817+
// Parts accumulate while they fit the target.
1818+
let added = builder.add_part(small()).await.expect("valid usage");
1819+
assert!(matches!(added, Added::Record), "first part accumulates");
1820+
let added = builder.add_part(small()).await.expect("valid usage");
1821+
assert!(
1822+
matches!(added, Added::Record),
1823+
"second part fits the target"
1824+
);
1825+
// The third would exceed the target, so the first two flush together.
1826+
let added = builder.add_part(small()).await.expect("valid usage");
1827+
assert!(matches!(added, Added::RecordAndParts), "third part flushes");
1828+
// An oversized part flushes the accumulated part first, and then on its own.
1829+
let added = builder.add_part(big).await.expect("valid usage");
1830+
assert!(
1831+
matches!(added, Added::RecordAndParts),
1832+
"oversized part flushes"
1833+
);
1834+
let added = builder.add_part(small()).await.expect("valid usage");
1835+
assert!(
1836+
matches!(added, Added::Record),
1837+
"accounting resets after a flush"
1838+
);
1839+
1840+
let batch = builder
1841+
.finish(Antichain::from_elem(2))
1842+
.await
1843+
.expect("valid usage");
1844+
// [small, small], [small], [big], [small]
1845+
assert_eq!(batch.batch.part_count(), 4);
1846+
write
1847+
.append_batch(batch, Antichain::from_elem(0), Antichain::from_elem(2))
1848+
.await
1849+
.expect("valid usage")
1850+
.expect("unexpected upper");
1851+
let mut expected = vec![
1852+
(("1".to_owned(), "one".to_owned()), 1, 4),
1853+
(("2".to_owned(), "two".to_owned()), 1, 1),
1854+
(("3".to_owned(), "three".to_owned()), 1, 1),
1855+
(("4".to_owned(), "four".to_owned()), 1, 1),
1856+
(("5".to_owned(), "five".to_owned()), 1, 1),
1857+
];
1858+
expected.sort();
1859+
assert_eq!(
1860+
read.expect_snapshot_and_fetch(1).await,
1861+
all_ok(&expected, 1)
1862+
);
1863+
}
1864+
17621865
#[mz_ore::test(tokio::test)]
17631866
#[cfg_attr(miri, ignore)] // unsupported operation: returning ready events from epoll_wait is not yet implemented
17641867
async fn batch_delete() {

‎src/persist-client/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ impl PersistClient {
550550
/// updates is very large. Individual records, however, should be small
551551
/// enough that we can reasonably chunk them up: O(KB) is definitely fine,
552552
/// O(MB) come talk to us.
553+
///
554+
/// `max_runs` overrides `persist_batch_max_runs` when `Some`. With `None`, the builder honors
555+
/// the configured value, as [WriteHandle::builder] does.
553556
#[instrument(level = "debug", fields(shard = %shard_id))]
554557
pub fn batch_builder<K, V, T, D>(
555558
&self,
@@ -566,7 +569,9 @@ impl PersistClient {
566569
D: Monoid + Ord + Codec64 + Send + Sync,
567570
{
568571
let mut compact_cfg = CompactConfig::new(&self.cfg, shard_id);
569-
compact_cfg.batch.max_runs = max_runs;
572+
if let Some(max_runs) = max_runs {
573+
compact_cfg.batch.max_runs = Some(max_runs);
574+
}
570575
WriteHandle::builder_inner(
571576
&self.cfg,
572577
compact_cfg,

‎src/storage-operators/src/persist.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,19 @@ mod tests {
426426
})
427427
.collect();
428428

429-
for _ in 0..5000 {
429+
for i in 0..5000 {
430430
let batch_id = SharedBatchId::new();
431431
let first = builder(batch_id);
432432
let second = builder(batch_id);
433433
first.push(bool_part(&schemas)).await;
434+
// When both handles push, a sweep that evicted the live entry between the two
435+
// `builder` calls would split the updates across two batches.
436+
let pushes = if i % 2 == 0 {
437+
second.push(bool_part(&schemas)).await;
438+
2
439+
} else {
440+
1
441+
};
434442
let first = mz_ore::task::spawn(|| "finish-first", first.finish());
435443
let second = mz_ore::task::spawn(|| "finish-second", second.finish());
436444
let batches: Vec<_> = [first.await, second.await].into_iter().flatten().collect();
@@ -440,7 +448,11 @@ mod tests {
440448
"exactly one finishing handle receives the batch"
441449
);
442450
for batch in batches {
443-
batch.into_hollow_batch();
451+
assert_eq!(
452+
batch.into_hollow_batch().len,
453+
pushes,
454+
"the batch holds every handle's updates"
455+
);
444456
}
445457
}
446458
drop(churn);

0 commit comments

Comments
 (0)