Skip to content

Commit 7728c76

Browse files
antiguruclaude
andcommitted
compute: route all MV sink batch writes through SharedBatchWrite
With shared batches disabled, `SharedBatchWriter::start` now returns a write that joins no shared batch and sends every update to the worker's own batch, instead of returning `None` and leaving each sink with a separate per-worker path. Both sinks have a single write path as a result. Add unit tests for a shared batch whose handles drop without finishing, and a testdrive phase that runs both sinks with sharing disabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 55efba7 commit 7728c76

5 files changed

Lines changed: 179 additions & 103 deletions

File tree

‎src/compute/src/sink/materialized_view.rs‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,6 @@ mod write {
857857
let mut state = State::new(
858858
sink_id,
859859
worker_id,
860-
writer,
861860
shared_batch_writer,
862861
sink_metrics,
863862
channel_logging,
@@ -964,7 +963,6 @@ mod write {
964963
struct State {
965964
sink_id: GlobalId,
966965
worker_id: usize,
967-
persist_writer: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
968966
/// Coalesces batches across the workers in this process while enabled.
969967
shared_batch_writer: SharedBatchWriter,
970968
/// Contains `desired - persist`, reflecting the updates we would like to commit to
@@ -1002,7 +1000,6 @@ mod write {
10021000
fn new(
10031001
sink_id: GlobalId,
10041002
worker_id: usize,
1005-
persist_writer: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
10061003
shared_batch_writer: SharedBatchWriter,
10071004
metrics: SinkMetrics,
10081005
logging: Option<ChannelLogging>,
@@ -1019,7 +1016,6 @@ mod write {
10191016
let mut state = Self {
10201017
sink_id,
10211018
worker_id,
1022-
persist_writer,
10231019
shared_batch_writer,
10241020
corrections: OkErr::new(
10251021
Correction::new(
@@ -1197,26 +1193,15 @@ mod write {
11971193
return None;
11981194
}
11991195

1200-
let batches = match self.shared_batch_writer.start(&desc) {
1201-
Some(mut write) => {
1202-
for (kv, t, d) in updates {
1203-
write.push(&kv, t, d).await;
1204-
}
1205-
let batches = write.finish().await;
1206-
if batches.is_empty() {
1207-
self.trace("contributed to a shared batch");
1208-
return None;
1209-
}
1210-
batches
1211-
}
1212-
None => vec![
1213-
self.persist_writer
1214-
.batch(updates, desc.lower.clone(), desc.upper.clone())
1215-
.await
1216-
.expect("valid usage")
1217-
.into_transmittable_batch(),
1218-
],
1219-
};
1196+
let mut write = self.shared_batch_writer.start(&desc);
1197+
for (kv, t, d) in updates {
1198+
write.push(&kv, t, d).await;
1199+
}
1200+
let batches = write.finish().await;
1201+
if batches.is_empty() {
1202+
self.trace("contributed to a shared batch");
1203+
return None;
1204+
}
12201205

12211206
self.trace(format!("wrote {} batches", batches.len()));
12221207
Some((desc, batches, cap))

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -956,13 +956,8 @@ mod write {
956956
},
957957
);
958958

959-
let batches = match batch_writer.shared.start(&desc) {
960-
Some(write) => write_shared_batch(write, &mut updates_rx).await,
961-
None => write_worker_batch(&batch_writer.writer, &desc, &mut updates_rx)
962-
.await
963-
.into_iter()
964-
.collect(),
965-
};
959+
let batches =
960+
write_batches(batch_writer.shared.start(&desc), &mut updates_rx).await;
966961
let corrections = read_back.await;
967962

968963
if let Err(err) = resp_tx.send(WriteResponse { batches }) {
@@ -981,7 +976,7 @@ mod write {
981976

982977
/// Write the updates for one interval through `write`, returning the batches this worker
983978
/// delivers.
984-
async fn write_shared_batch(
979+
async fn write_batches(
985980
mut write: SharedBatchWrite<'_>,
986981
updates_rx: &mut mpsc::Receiver<Vec<((SourceData, ()), Timestamp, StorageDiff)>>,
987982
) -> Vec<ProtoBatch> {
@@ -993,27 +988,6 @@ mod write {
993988
write.finish().await
994989
}
995990

996-
/// Write this worker's own batch for `desc`, or nothing if it has no updates.
997-
async fn write_worker_batch(
998-
writer: &WriteHandle<SourceData, (), Timestamp, StorageDiff>,
999-
desc: &BatchDescription,
1000-
updates_rx: &mut mpsc::Receiver<Vec<((SourceData, ()), Timestamp, StorageDiff)>>,
1001-
) -> Option<ProtoBatch> {
1002-
// Create the builder lazily: an idle sink's descriptions find no corrections.
1003-
let mut builder = None;
1004-
while let Some(chunk) = updates_rx.recv().await {
1005-
let builder = builder.get_or_insert_with(|| writer.builder(desc.lower.clone()));
1006-
for ((k, v), t, d) in &chunk {
1007-
builder.add(k, v, t, d).await.expect("valid usage");
1008-
}
1009-
}
1010-
let batch = builder?
1011-
.finish(desc.upper.clone())
1012-
.await
1013-
.expect("valid usage");
1014-
Some(batch.into_transmittable_batch())
1015-
}
1016-
1017991
/// Apply a coalesced batch of updates to the correction buffers, on a blocking thread.
1018992
///
1019993
/// See [`apply_command`] for why this work must not run on a Tokio worker thread.

‎src/compute/src/sink/shared_batch.rs‎

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
//! share `desc.shared_id`, because the `mint` operator broadcasts one description.
1515
//!
1616
//! One task per process encodes and uploads a shared batch, so routing large intervals through it
17-
//! serializes work the workers otherwise do in parallel. Hydrating a 9.5 GB MV on 8 workers against a
18-
//! local blob store took 33 s through the shared batch and 26 s with per-worker batches. Each worker therefore stages
19-
//! its updates and hands them to the shared batch only if they stay below a size threshold. Past
20-
//! the threshold, it writes its own batch, whose parts are large enough on their own.
17+
//! serializes work the workers otherwise do in parallel, which measurably slows hydration. Each
18+
//! worker therefore stages its updates and hands them to the shared batch only if they stay below a
19+
//! size threshold. Past the threshold, it writes its own batch, whose parts are large enough on
20+
//! their own.
2121
2222
use std::sync::Arc;
2323

@@ -74,37 +74,50 @@ impl SharedBatchWriter {
7474
}
7575
}
7676

77-
/// Starts this worker's write for `desc`, or returns `None` when sharing is disabled and the
78-
/// worker writes its own batch.
79-
pub(super) fn start(&self, desc: &BatchDescription) -> Option<SharedBatchWrite<'_>> {
77+
/// Starts this worker's write for `desc`.
78+
pub(super) fn start(&self, desc: &BatchDescription) -> SharedBatchWrite<'_> {
8079
// Read per interval, so a flag flip applies without re-rendering the sink. Workers may
8180
// disagree on an interval: each either contributes to the shared batch or writes its own,
82-
// and the append operator appends all of them together.
83-
if !self.enabled.get() {
84-
return None;
85-
}
86-
let shared = self.shared_batches.builder(
87-
desc.shared_id,
88-
self.persist_client.clone(),
89-
self.shard_id,
90-
self.shard_name.clone(),
91-
self.schemas.clone(),
92-
desc.lower.clone(),
93-
desc.upper.clone(),
94-
);
95-
Some(SharedBatchWrite {
81+
// and the append operator appends all of them together. A disabled writer joins no shared
82+
// batch at all, so turning the flag off also stops the shared builder tasks.
83+
let shared = self.enabled.get().then(|| {
84+
self.shared_batches.builder(
85+
desc.shared_id,
86+
self.persist_client.clone(),
87+
self.shard_id,
88+
self.shard_name.clone(),
89+
self.schemas.clone(),
90+
desc.lower.clone(),
91+
desc.upper.clone(),
92+
)
93+
});
94+
SharedBatchWrite {
9695
writer: self,
9796
desc: desc.clone(),
9897
max_bytes: self.max_bytes.get(),
9998
staged: PartBuilder::new(&*self.schemas.key, &*self.schemas.val),
10099
own: None,
101100
shared,
102-
})
101+
}
102+
}
103+
104+
fn own_builder(
105+
&self,
106+
desc: &BatchDescription,
107+
) -> BatchBuilder<SourceData, (), Timestamp, StorageDiff> {
108+
self.persist_client.batch_builder(
109+
self.shard_id,
110+
&self.shard_name,
111+
self.schemas.clone(),
112+
desc.lower.clone(),
113+
None,
114+
)
103115
}
104116
}
105117

106118
/// One worker's write for one interval, which contributes to the shared batch until the worker's
107-
/// updates reach the size threshold and writes the worker's own batch after.
119+
/// updates reach the size threshold and writes the worker's own batch after. With sharing disabled,
120+
/// all updates go to the worker's own batch.
108121
///
109122
/// The caller must [`finish`](Self::finish) it, also when it pushed nothing: only the last
110123
/// worker to finish receives the shared batch, and any worker holding a write may be that last
@@ -113,53 +126,57 @@ pub(super) struct SharedBatchWrite<'a> {
113126
writer: &'a SharedBatchWriter,
114127
desc: BatchDescription,
115128
max_bytes: usize,
116-
/// Updates not yet handed to either batch. Empty once `own` exists.
129+
/// Updates not yet handed to either batch. Empty once `own` exists, and always empty without
130+
/// `shared`.
117131
staged: PartBuilder<SourceData, RelationDesc, (), UnitSchema>,
132+
/// Created on the first update that does not go to the shared batch, so a worker without
133+
/// updates writes no batch.
118134
own: Option<BatchBuilder<SourceData, (), Timestamp, StorageDiff>>,
119-
shared: SharedBatchBuilder,
135+
shared: Option<SharedBatchBuilder>,
120136
}
121137

122138
impl SharedBatchWrite<'_> {
123139
/// Adds an update.
124140
pub(super) async fn push(&mut self, (k, v): &(SourceData, ()), t: Timestamp, d: StorageDiff) {
125-
if let Some(own) = &mut self.own {
126-
own.add(k, v, &t, &d).await.expect("valid usage");
127-
return;
128-
}
129-
self.staged.push(k, v, t, d);
130-
if self.staged.goodbytes() >= self.max_bytes {
131-
let writer = self.writer;
132-
let mut own = writer.persist_client.batch_builder(
133-
writer.shard_id,
134-
&writer.shard_name,
135-
writer.schemas.clone(),
136-
self.desc.lower.clone(),
137-
None,
138-
);
139-
let staged = self
140-
.staged
141-
.finish_and_replace(&*writer.schemas.key, &*writer.schemas.val);
142-
own.add_part(staged).await.expect("valid usage");
143-
self.own = Some(own);
141+
if self.own.is_none() {
142+
if self.shared.is_some() {
143+
self.staged.push(k, v, t, d);
144+
if self.staged.goodbytes() < self.max_bytes {
145+
return;
146+
}
147+
let writer = self.writer;
148+
let staged = self
149+
.staged
150+
.finish_and_replace(&*writer.schemas.key, &*writer.schemas.val);
151+
let mut own = writer.own_builder(&self.desc);
152+
own.add_part(staged).await.expect("valid usage");
153+
self.own = Some(own);
154+
return;
155+
}
156+
self.own = Some(self.writer.own_builder(&self.desc));
144157
}
158+
let own = self.own.as_mut().expect("created above");
159+
own.add(k, v, &t, &d).await.expect("valid usage");
145160
}
146161

147162
/// Hands over the staged updates and returns the batches this worker delivers: its own batch
148-
/// if its updates reached the threshold, and the shared batch if it is the last worker to
149-
/// finish.
163+
/// if it has one, and the shared batch if it is the last worker to finish.
150164
pub(super) async fn finish(self) -> Vec<ProtoBatch> {
151165
let own = match self.own {
152166
Some(own) => Some(
153167
own.finish(self.desc.upper.clone())
154168
.await
155169
.expect("valid usage"),
156170
),
157-
None => {
158-
self.shared.push(self.staged.finish()).await;
159-
None
171+
None => None,
172+
};
173+
let shared = match self.shared {
174+
Some(shared) => {
175+
shared.push(self.staged.finish()).await;
176+
shared.finish().await
160177
}
178+
None => None,
161179
};
162-
let shared = self.shared.finish().await;
163180
own.into_iter()
164181
.chain(shared)
165182
.map(|batch| batch.into_transmittable_batch())

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,76 @@ mod tests {
315315
)
316316
}
317317

318+
fn test_builder(
319+
shared: &SharedBatches,
320+
client: &PersistClient,
321+
batch_id: SharedBatchId,
322+
shard_id: ShardId,
323+
) -> SharedBatchBuilder {
324+
shared.builder(
325+
batch_id,
326+
client.clone(),
327+
shard_id,
328+
"test".to_string(),
329+
bool_schemas(),
330+
Antichain::from_elem(0.into()),
331+
Antichain::from_elem(1.into()),
332+
)
333+
}
334+
335+
// A worker that goes away after pushing, without finishing, leaves its data to whichever
336+
// handle finishes last.
337+
#[mz_ore::test(tokio::test)]
338+
async fn test_shared_batch_dropped_handle() {
339+
let client = PersistClient::new_for_tests().await;
340+
let shared = SharedBatches::new();
341+
let batch_id = SharedBatchId::new();
342+
let shard_id = ShardId::new();
343+
let schemas = bool_schemas();
344+
let first = test_builder(&shared, &client, batch_id, shard_id);
345+
let second = test_builder(&shared, &client, batch_id, shard_id);
346+
first.push(bool_part(&schemas)).await;
347+
second.push(bool_part(&schemas)).await;
348+
drop(second);
349+
let batch = first
350+
.finish()
351+
.await
352+
.expect("last finisher receives the batch");
353+
assert_eq!(
354+
batch.into_hollow_batch().len,
355+
2,
356+
"batch should include the dropped handle's updates"
357+
);
358+
}
359+
360+
// When every handle goes away without finishing, the batch is abandoned: it is delivered to
361+
// nobody, and a later builder for the same id starts a fresh batch without its data.
362+
#[mz_ore::test(tokio::test)]
363+
async fn test_shared_batch_abandoned() {
364+
let client = PersistClient::new_for_tests().await;
365+
let shared = SharedBatches::new();
366+
let batch_id = SharedBatchId::new();
367+
let shard_id = ShardId::new();
368+
let schemas = bool_schemas();
369+
let first = test_builder(&shared, &client, batch_id, shard_id);
370+
let second = test_builder(&shared, &client, batch_id, shard_id);
371+
first.push(bool_part(&schemas)).await;
372+
second.push(bool_part(&schemas)).await;
373+
drop(first);
374+
drop(second);
375+
let third = test_builder(&shared, &client, batch_id, shard_id);
376+
third.push(bool_part(&schemas)).await;
377+
let batch = third
378+
.finish()
379+
.await
380+
.expect("sole handle receives the batch");
381+
assert_eq!(
382+
batch.into_hollow_batch().len,
383+
1,
384+
"batch should exclude the abandoned batch's updates"
385+
);
386+
}
387+
318388
// Sweeping dead entries while handles for a live batch finish concurrently must not lose the
319389
// batch: exactly one of the finishing handles receives it.
320390
#[mz_ore::test(tokio::test(flavor = "multi_thread", worker_threads = 4))]

0 commit comments

Comments
 (0)