Skip to content

Commit cd58526

Browse files
antiguruclaude
andcommitted
compute: read the shared-batch flag per batch interval
Both MV sinks read `enable_compute_sync_mv_sink_shared_batches` through a config handle when they start a batch, so a flip applies without re-rendering the sink. The sync sink's shared and per-worker write paths are separate functions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d7e04c2 commit cd58526

3 files changed

Lines changed: 76 additions & 60 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,12 @@ mod write {
844844
drop(capabilities);
845845

846846
let writer = persist_api.open_writer().await;
847-
let shared_batch_writer = if ENABLE_SYNC_MV_SINK_SHARED_BATCHES.get(&worker_config) {
848-
Some(SharedBatchWriter::new(&persist_api, &writer).await)
849-
} else {
850-
None
851-
};
847+
let shared_batch_writer = SharedBatchWriter::new(
848+
&persist_api,
849+
&writer,
850+
ENABLE_SYNC_MV_SINK_SHARED_BATCHES.handle(&worker_config),
851+
)
852+
.await;
852853
let sink_metrics = persist_api.open_metrics().await;
853854
let read_only = *read_only_rx.borrow_and_update();
854855
let mut state = State::new(
@@ -960,8 +961,8 @@ mod write {
960961
sink_id: GlobalId,
961962
worker_id: usize,
962963
persist_writer: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
963-
/// Set when batches are coalesced across the workers in this process.
964-
shared_batch_writer: Option<SharedBatchWriter>,
964+
/// Coalesces batches across the workers in this process while enabled.
965+
shared_batch_writer: SharedBatchWriter,
965966
/// Contains `desired - persist`, reflecting the updates we would like to commit to
966967
/// `persist` in order to "correct" it to track `desired`. This collection is only modified
967968
/// by updates received from either the `desired` or `persist` inputs.
@@ -998,7 +999,7 @@ mod write {
998999
sink_id: GlobalId,
9991000
worker_id: usize,
10001001
persist_writer: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
1001-
shared_batch_writer: Option<SharedBatchWriter>,
1002+
shared_batch_writer: SharedBatchWriter,
10021003
metrics: SinkMetrics,
10031004
logging: Option<ChannelLogging>,
10041005
as_of: Antichain<Timestamp>,
@@ -1192,9 +1193,8 @@ mod write {
11921193
return None;
11931194
}
11941195

1195-
let batch = match &self.shared_batch_writer {
1196-
Some(shared) => {
1197-
let mut write = shared.start(&desc);
1196+
let batch = match self.shared_batch_writer.start(&desc) {
1197+
Some(mut write) => {
11981198
for (kv, t, d) in updates {
11991199
write.push(&kv, t, d).await;
12001200
}

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

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use differential_dataflow::{Hashable, VecCollection};
5555
use mz_compute_types::dyncfgs::{
5656
ENABLE_SYNC_MV_SINK_SHARED_BATCHES, MV_SINK_ADVANCE_PERSIST_FRONTIERS,
5757
};
58-
use mz_dyncfg::ConfigSet;
58+
use mz_dyncfg::{ConfigSet, ConfigValHandle};
5959
use mz_ore::cast::CastFrom;
6060
use mz_persist_client::batch::{Batch, ProtoBatch};
6161
use mz_persist_client::write::WriteHandle;
@@ -81,7 +81,7 @@ use crate::sink::materialized_view::{
8181
BatchDescription, BatchesStream, DescsStream, DesiredStreams, OkErr, PersistApi,
8282
PersistStreams, SharedSinkFrontier, advance, operator_name, persist_source,
8383
};
84-
use crate::sink::shared_batch::SharedBatchWriter;
84+
use crate::sink::shared_batch::{SharedBatchWrite, SharedBatchWriter};
8585

8686
/// Renders an MV sink writing the given desired collection into the `target` persist collection.
8787
///
@@ -659,9 +659,7 @@ mod write {
659659
let advance_persist_frontiers_at_startup =
660660
MV_SINK_ADVANCE_PERSIST_FRONTIERS.get(&worker_config);
661661

662-
// Read the shared-batch flag on the Timely thread. `worker_config` is an `Rc<ConfigSet>`
663-
// and thus not `Send`, so the value must be captured before it moves into the Tokio task.
664-
let shared_batches_enabled = ENABLE_SYNC_MV_SINK_SHARED_BATCHES.get(&worker_config);
662+
let shared_batches_enabled = ENABLE_SYNC_MV_SINK_SHARED_BATCHES.handle(&worker_config);
665663

666664
// Mirror the persist-frontier initialization performed by `State::new` below. With the
667665
// flag enabled, `State` advances its Timely-side `persist_frontiers` to `as_of`, opening
@@ -851,22 +849,20 @@ mod write {
851849
const READ_BACK_CHUNKS_IN_FLIGHT: usize = 4;
852850
/// The batch-writing context owned by the Tokio write task.
853851
///
854-
/// With `shared` set, batches are built through the process-global shared batches. The
855-
/// `writer` then serves only to clean up a finished batch when the response channel has
856-
/// already gone away.
852+
/// The `writer` writes per-worker batches and cleans up a finished batch when the response
853+
/// channel has already gone away.
857854
struct BatchWriter {
858855
writer: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
859-
shared: Option<SharedBatchWriter>,
856+
shared: SharedBatchWriter,
860857
}
861858

862859
impl BatchWriter {
863-
async fn new(persist_api: &PersistApi, shared_batches_enabled: bool) -> Self {
860+
async fn new(
861+
persist_api: &PersistApi,
862+
shared_batches_enabled: ConfigValHandle<bool>,
863+
) -> Self {
864864
let writer = persist_api.open_writer().await;
865-
let shared = if shared_batches_enabled {
866-
Some(SharedBatchWriter::new(persist_api, &writer).await)
867-
} else {
868-
None
869-
};
865+
let shared = SharedBatchWriter::new(persist_api, &writer, shared_batches_enabled).await;
870866
Self { writer, shared }
871867
}
872868
}
@@ -943,35 +939,9 @@ mod write {
943939
},
944940
);
945941

946-
let proto_batch = if let Some(shared) = &batch_writer.shared {
947-
let mut write = shared.start(&desc);
948-
while let Some(chunk) = updates_rx.recv().await {
949-
for (kv, t, d) in &chunk {
950-
write.push(kv, *t, *d).await;
951-
}
952-
}
953-
write.finish().await
954-
} else {
955-
// Per-worker path: each worker writes its own batch for the interval. Create
956-
// the builder lazily: an idle sink's descriptions find no corrections.
957-
let mut builder = None;
958-
while let Some(chunk) = updates_rx.recv().await {
959-
let builder = builder
960-
.get_or_insert_with(|| batch_writer.writer.builder(desc.lower.clone()));
961-
for ((k, v), t, d) in &chunk {
962-
builder.add(k, v, t, d).await.expect("valid usage");
963-
}
964-
}
965-
match builder {
966-
Some(builder) => {
967-
let batch = builder
968-
.finish(desc.upper.clone())
969-
.await
970-
.expect("valid usage");
971-
Some(batch.into_transmittable_batch())
972-
}
973-
None => None,
974-
}
942+
let proto_batch = match batch_writer.shared.start(&desc) {
943+
Some(write) => write_shared_batch(write, &mut updates_rx).await,
944+
None => write_worker_batch(&batch_writer.writer, &desc, &mut updates_rx).await,
975945
};
976946
let corrections = read_back.await;
977947

@@ -989,6 +959,41 @@ mod write {
989959
}
990960
}
991961

962+
/// Hand the updates for one interval to the shared batch, returning the batch if this worker
963+
/// delivers it.
964+
async fn write_shared_batch(
965+
mut write: SharedBatchWrite,
966+
updates_rx: &mut mpsc::Receiver<Vec<((SourceData, ()), Timestamp, StorageDiff)>>,
967+
) -> Option<ProtoBatch> {
968+
while let Some(chunk) = updates_rx.recv().await {
969+
for (kv, t, d) in &chunk {
970+
write.push(kv, *t, *d).await;
971+
}
972+
}
973+
write.finish().await
974+
}
975+
976+
/// Write this worker's own batch for `desc`, or nothing if it has no updates.
977+
async fn write_worker_batch(
978+
writer: &WriteHandle<SourceData, (), Timestamp, StorageDiff>,
979+
desc: &BatchDescription,
980+
updates_rx: &mut mpsc::Receiver<Vec<((SourceData, ()), Timestamp, StorageDiff)>>,
981+
) -> Option<ProtoBatch> {
982+
// Create the builder lazily: an idle sink's descriptions find no corrections.
983+
let mut builder = None;
984+
while let Some(chunk) = updates_rx.recv().await {
985+
let builder = builder.get_or_insert_with(|| writer.builder(desc.lower.clone()));
986+
for ((k, v), t, d) in &chunk {
987+
builder.add(k, v, t, d).await.expect("valid usage");
988+
}
989+
}
990+
let batch = builder?
991+
.finish(desc.upper.clone())
992+
.await
993+
.expect("valid usage");
994+
Some(batch.into_transmittable_batch())
995+
}
996+
992997
/// Apply a coalesced batch of updates to the correction buffers, on a blocking thread.
993998
///
994999
/// See [`apply_command`] for why this work must not run on a Tokio worker thread.

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
use std::sync::Arc;
1717

18+
use mz_dyncfg::ConfigValHandle;
1819
use mz_persist_client::batch::ProtoBatch;
1920
use mz_persist_client::write::WriteHandle;
2021
use mz_persist_client::{PersistClient, Schemas};
@@ -42,13 +43,15 @@ pub(super) struct SharedBatchWriter {
4243
shard_name: String,
4344
schemas: Schemas<SourceData, ()>,
4445
shared_batches: SharedBatches,
46+
enabled: ConfigValHandle<bool>,
4547
}
4648

4749
impl SharedBatchWriter {
48-
/// Creates a writer for the shard `writer` writes to.
50+
/// Creates a writer for the shard `writer` writes to, which shares batches while `enabled`.
4951
pub(super) async fn new(
5052
persist_api: &PersistApi,
5153
writer: &WriteHandle<SourceData, (), Timestamp, StorageDiff>,
54+
enabled: ConfigValHandle<bool>,
5255
) -> Self {
5356
Self {
5457
persist_client: persist_api.open_client().await,
@@ -64,11 +67,19 @@ impl SharedBatchWriter {
6467
val: Arc::new(UnitSchema),
6568
},
6669
shared_batches: persist_api.persist_batches.clone(),
70+
enabled,
6771
}
6872
}
6973

70-
/// Starts this worker's contribution to the shared batch for `desc`.
71-
pub(super) fn start(&self, desc: &BatchDescription) -> SharedBatchWrite {
74+
/// Starts this worker's contribution to the shared batch for `desc`, or returns `None` when
75+
/// sharing is disabled and the worker writes its own batch.
76+
pub(super) fn start(&self, desc: &BatchDescription) -> Option<SharedBatchWrite> {
77+
// Read per interval, so a flag flip applies without re-rendering the sink. Workers may
78+
// disagree on an interval: each either contributes to the shared batch or writes its own,
79+
// and the append operator appends all of them together.
80+
if !self.enabled.get() {
81+
return None;
82+
}
7283
let shared = self.shared_batches.builder(
7384
desc.shared_id,
7485
self.persist_client.clone(),
@@ -78,11 +89,11 @@ impl SharedBatchWriter {
7889
desc.lower.clone(),
7990
desc.upper.clone(),
8091
);
81-
SharedBatchWrite {
92+
Some(SharedBatchWrite {
8293
builder: PartBuilder::new(&*self.schemas.key, &*self.schemas.val),
8394
schemas: self.schemas.clone(),
8495
shared,
85-
}
96+
})
8697
}
8798
}
8899

0 commit comments

Comments
 (0)