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
2222use 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
122138impl 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 ( ) )
0 commit comments