Skip to main content

mz_storage/render/
sources.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Logic related to the creation of dataflow sources.
11//!
12//! See [`render_source`] for more details.
13
14use std::collections::BTreeMap;
15use std::iter;
16use std::sync::Arc;
17
18use differential_dataflow::{AsCollection, VecCollection};
19use mz_expr::MfpPlan;
20use mz_ore::cast::CastLossy;
21use mz_persist_client::operators::shard_source::SnapshotMode;
22use mz_repr::{Datum, Diff, GlobalId, Row, RowPacker};
23use mz_storage_operators::persist_source;
24use mz_storage_operators::persist_source::Subtime;
25use mz_storage_types::controller::CollectionMetadata;
26use mz_storage_types::dyncfgs;
27use mz_storage_types::errors::{
28    DataflowError, DecodeError, EnvelopeError, UpsertError, UpsertNullKeyError, UpsertValueError,
29};
30use mz_storage_types::parameters::StorageMaxInflightBytesConfig;
31use mz_storage_types::sources::envelope::{KeyEnvelope, NoneEnvelope, UpsertEnvelope, UpsertStyle};
32use mz_storage_types::sources::*;
33use mz_timely_util::builder_async::{PressOnDropButton, button};
34use mz_timely_util::operator::CollectionExt;
35use mz_timely_util::order::refine_antichain;
36use serde::{Deserialize, Serialize};
37use timely::container::CapacityContainerBuilder;
38use timely::dataflow::StreamVec;
39use timely::dataflow::operators::vec::Map;
40use timely::dataflow::operators::{ConnectLoop, Feedback, Leave, OkErr};
41use timely::dataflow::scope::Scope;
42use timely::progress::{Antichain, Timestamp};
43
44use crate::decode::{render_decode_cdcv2, render_decode_delimited};
45use crate::healthcheck::{HealthStatusMessage, StatusNamespace};
46use crate::source::types::{DecodeResult, SourceOutput, SourceRender};
47use crate::source::{self, RawSourceCreationConfig, SourceExportCreationConfig};
48use crate::upsert::{UpsertKey, UpsertSourceTime, UpsertValue};
49
50/// _Renders_ complete _differential_ collections
51/// that represent the final source and its errors
52/// as requested by the original `CREATE SOURCE` statement,
53/// encapsulated in the passed `SourceInstanceDesc`.
54///
55/// The first element in the returned tuple is the pair of Collections,
56/// the second is a type-erased token that will keep the source
57/// alive as long as it is not dropped.
58///
59/// This function is intended to implement the recipe described here:
60/// <https://github.com/MaterializeInc/materialize/blob/main/doc/developer/platform/architecture-storage.md#source-ingestion>
61pub fn render_source<'scope, 'root, C>(
62    scope: Scope<'scope, mz_repr::Timestamp>,
63    root_scope: Scope<'root, ()>,
64    dataflow_debug_name: &String,
65    connection: C,
66    description: IngestionDescription<CollectionMetadata>,
67    committed_uppers: BTreeMap<GlobalId, StreamVec<'scope, mz_repr::Timestamp, ()>>,
68    storage_state: &crate::storage_state::StorageState,
69    base_source_config: RawSourceCreationConfig,
70) -> (
71    BTreeMap<
72        GlobalId,
73        (
74            VecCollection<'scope, mz_repr::Timestamp, Row, Diff>,
75            VecCollection<'scope, mz_repr::Timestamp, DataflowError, Diff>,
76        ),
77    >,
78    Vec<StreamVec<'root, (), HealthStatusMessage>>,
79    StreamVec<'scope, mz_repr::Timestamp, ()>,
80    Vec<PressOnDropButton>,
81)
82where
83    C: SourceConnection + SourceRender + 'static,
84    C::Time: UpsertSourceTime,
85{
86    // Tokens that we should return from the method.
87    let mut needed_tokens = Vec::new();
88
89    // Note that this `render_source` attaches a single _instance_ of a source
90    // to the passed `Scope`, and this instance may be disabled if the
91    // source type does not support multiple instances. `render_source`
92    // is called on each timely worker as part of
93    // [`super::build_storage_dataflow`].
94
95    // A set of channels (1 per worker) used to signal rehydration being finished
96    // to raw sources. These are channels and not timely streams because they
97    // have to cross a scope boundary.
98    //
99    // Note that these will be entirely subsumed by full `hydration` backpressure,
100    // once that is implemented.
101    let (starter, mut start_signal) = tokio::sync::mpsc::channel::<()>(1);
102    let start_signal = async move {
103        let _ = start_signal.recv().await;
104    };
105
106    // Build the _raw_ ok and error sources using `create_raw_source` and the
107    // correct `SourceReader` implementations
108    let (exports, health, remap_upper, source_tokens) = source::create_raw_source(
109        scope,
110        root_scope,
111        storage_state,
112        committed_uppers,
113        &base_source_config,
114        connection,
115        start_signal,
116    );
117
118    needed_tokens.extend(source_tokens);
119
120    let mut health_streams = Vec::with_capacity(exports.len() + 1);
121    health_streams.push(health);
122
123    let mut outputs = BTreeMap::new();
124    for (export_id, export) in exports {
125        type CB<C> = CapacityContainerBuilder<C>;
126        let (ok_stream, err_stream) =
127            export.map_fallible::<CB<_>, CB<_>, _, _, _>("export-demux-ok-err", |r| r);
128
129        // All sources should push their various error streams into this vector,
130        // whose contents will be concatenated and inserted along the collection.
131        // All subsources include the non-definite errors of the ingestion
132        let mut error_collections = Vec::new();
133
134        let data_config = base_source_config.source_exports[&export_id]
135            .data_config
136            .clone();
137        let (ok, extra_tokens, health_stream) = render_source_stream(
138            scope,
139            dataflow_debug_name,
140            export_id,
141            ok_stream,
142            data_config,
143            &description,
144            &mut error_collections,
145            storage_state,
146            &base_source_config,
147            starter.clone(),
148        );
149        needed_tokens.extend(extra_tokens);
150
151        // Flatten the error collections.
152        let err_collection = match error_collections.len() {
153            0 => err_stream,
154            _ => err_stream.concatenate(error_collections),
155        };
156
157        outputs.insert(export_id, (ok, err_collection));
158
159        health_streams.extend(health_stream.into_iter().map(|s| s.leave(root_scope)));
160    }
161    (outputs, health_streams, remap_upper, needed_tokens)
162}
163
164/// Completes the rendering of a particular source stream by applying decoding and envelope
165/// processing as necessary
166fn render_source_stream<'scope, FromTime>(
167    scope: Scope<'scope, mz_repr::Timestamp>,
168    dataflow_debug_name: &String,
169    export_id: GlobalId,
170    ok_source: VecCollection<'scope, mz_repr::Timestamp, SourceOutput<FromTime>, Diff>,
171    data_config: SourceExportDataConfig,
172    description: &IngestionDescription<CollectionMetadata>,
173    error_collections: &mut Vec<VecCollection<'scope, mz_repr::Timestamp, DataflowError, Diff>>,
174    storage_state: &crate::storage_state::StorageState,
175    base_source_config: &RawSourceCreationConfig,
176    rehydrated_token: impl std::any::Any + 'static,
177) -> (
178    VecCollection<'scope, mz_repr::Timestamp, Row, Diff>,
179    Vec<PressOnDropButton>,
180    Vec<StreamVec<'scope, mz_repr::Timestamp, HealthStatusMessage>>,
181)
182where
183    FromTime: Timestamp + Sync,
184    FromTime: UpsertSourceTime,
185{
186    let mut needed_tokens = vec![];
187
188    // Use the envelope and encoding configs for this particular source export
189    let SourceExportDataConfig { encoding, envelope } = data_config;
190
191    let SourceDesc {
192        connection: _,
193        timestamp_interval: _,
194    } = description.desc;
195
196    let (decoded_stream, decode_health) = match encoding {
197        None => (
198            ok_source.map(|r| DecodeResult {
199                // This is safe because the current set of sources produce
200                // either:
201                // 1. Non-nullable keys
202                // 2. No keys at all.
203                //
204                // Please see the comment on `key_envelope_no_encoding` in
205                // `mz_sql::plan::statement::ddl` for more details.
206                key: Some(Ok(r.key)),
207                value: Some(Ok(r.value)),
208                metadata: r.metadata,
209                from_time: r.from_time,
210            }),
211            None,
212        ),
213        Some(encoding) => {
214            let (decoded_stream, decode_health) = render_decode_delimited(
215                ok_source,
216                encoding.key,
217                encoding.value,
218                dataflow_debug_name.clone(),
219                storage_state.metrics.decode_defs.clone(),
220                storage_state.storage_configuration.clone(),
221            );
222            (decoded_stream, Some(decode_health))
223        }
224    };
225
226    // render envelopes
227    let (envelope_ok, envelope_health) = match &envelope {
228        SourceEnvelope::Upsert(upsert_envelope) => {
229            let upsert_input = upsert_commands(decoded_stream, upsert_envelope.clone());
230
231            let persist_clients = Arc::clone(&storage_state.persist_clients);
232            // TODO: Get this to work with the as_of.
233            let resume_upper = base_source_config.resume_uppers[&export_id].clone();
234
235            let upper_ts = resume_upper
236                .as_option()
237                .expect("resuming an already finished ingestion")
238                .clone();
239            let outer_mz_scope = scope.clone();
240            let (upsert, health_update) = scope.scoped(
241                &format!("upsert_rehydration_backpressure({})", export_id),
242                |scope| {
243                    let (
244                        previous_ok,
245                        previous_err,
246                        previous_token,
247                        feedback_handle,
248                        backpressure_metrics,
249                    ) = {
250                        let as_of = Antichain::from_elem(upper_ts.saturating_sub(1));
251
252                        let backpressure_max_inflight_bytes = get_backpressure_max_inflight_bytes(
253                            &storage_state
254                                .storage_configuration
255                                .parameters
256                                .storage_dataflow_max_inflight_bytes_config,
257                            &storage_state.instance_context.cluster_memory_limit,
258                        );
259
260                        let (feedback_handle, flow_control, backpressure_metrics) =
261                            if let Some(storage_dataflow_max_inflight_bytes) =
262                                backpressure_max_inflight_bytes
263                            {
264                                tracing::info!(
265                                    ?backpressure_max_inflight_bytes,
266                                    "timely-{} using backpressure in upsert for source {}",
267                                    base_source_config.worker_id,
268                                    export_id
269                                );
270                                if !storage_state
271                                    .storage_configuration
272                                    .parameters
273                                    .storage_dataflow_max_inflight_bytes_config
274                                    .disk_only
275                                    || storage_state.instance_context.scratch_directory.is_some()
276                                {
277                                    let (feedback_handle, feedback_data) =
278                                        scope.feedback(Default::default());
279
280                                    // TODO(guswynn): cleanup
281                                    let backpressure_metrics = Some(
282                                        base_source_config
283                                            .metrics
284                                            .get_backpressure_metrics(export_id, scope.index()),
285                                    );
286
287                                    (
288                                        Some(feedback_handle),
289                                        Some(persist_source::FlowControl {
290                                            progress_stream: feedback_data,
291                                            max_inflight_bytes: storage_dataflow_max_inflight_bytes,
292                                            summary: (Default::default(), Subtime::least_summary()),
293                                            metrics: backpressure_metrics
294                                                .as_ref()
295                                                .map(|m| m.operator_metrics()),
296                                        }),
297                                        backpressure_metrics,
298                                    )
299                                } else {
300                                    (None, None, None)
301                                }
302                            } else {
303                                (None, None, None)
304                            };
305
306                        let storage_metadata = description.source_exports[&export_id]
307                            .storage_metadata
308                            .clone();
309
310                        let error_handler =
311                            storage_state.error_handler("upsert_rehydration", export_id);
312
313                        let (ok_stream, err_stream, tok) = persist_source::persist_source_core(
314                            outer_mz_scope,
315                            scope,
316                            export_id,
317                            persist_clients,
318                            storage_metadata,
319                            None,
320                            Some(as_of),
321                            SnapshotMode::Include,
322                            Antichain::new(),
323                            None::<&mut MfpPlan>,
324                            flow_control,
325                            false.then_some(|| unreachable!()),
326                            async {},
327                            error_handler,
328                        );
329                        (
330                            ok_stream.as_collection(),
331                            err_stream.as_collection(),
332                            Some(tok),
333                            feedback_handle,
334                            backpressure_metrics,
335                        )
336                    };
337
338                    let export_statistics = storage_state
339                        .aggregated_statistics
340                        .get_source(&export_id)
341                        .expect("statistics initialized")
342                        .clone();
343                    let export_config = SourceExportCreationConfig {
344                        id: export_id,
345                        worker_id: base_source_config.worker_id,
346                        metrics: base_source_config.metrics.clone(),
347                        source_statistics: export_statistics,
348                    };
349                    // The v2 stash spills only to swap, through the
350                    // buffer pool. On a replica that cannot swap, v1 keeps its
351                    // state in RocksDB on the scratch disk where one exists,
352                    // and v2's footprint without swap has not been measured.
353                    let use_upsert_v2 = dyncfgs::ENABLE_UPSERT_V2
354                        .get(storage_state.storage_configuration.config_set())
355                        && storage_state.instance_context.swap_available;
356                    let (upsert, health_update, snapshot_progress, upsert_token) = if use_upsert_v2
357                    {
358                        let (stats_shutdown, stats_button) = button(scope, scope.addr());
359                        needed_tokens.push(stats_button.press_on_drop());
360                        crate::upsert::upsert_v2(
361                            upsert_input.enter(scope),
362                            upsert_envelope.clone(),
363                            refine_antichain(&resume_upper),
364                            previous_ok,
365                            previous_err,
366                            previous_token,
367                            export_config,
368                            backpressure_metrics,
369                            stats_shutdown,
370                        )
371                    } else {
372                        crate::upsert::upsert(
373                            upsert_input.enter(scope),
374                            upsert_envelope.clone(),
375                            refine_antichain(&resume_upper),
376                            previous_ok,
377                            previous_err,
378                            previous_token,
379                            export_config,
380                            &storage_state.instance_context,
381                            &storage_state.storage_configuration,
382                            &storage_state.dataflow_parameters,
383                            backpressure_metrics,
384                        )
385                    };
386
387                    // Even though we register the `persist_sink` token at a top-level,
388                    // which will stop any data from being committed, we also register
389                    // a token for the `upsert` operator which may be in the middle of
390                    // rehydration processing the `persist_source` input above.
391                    needed_tokens.push(upsert_token);
392
393                    // If configured, delay raw sources until we rehydrate the upsert
394                    // source. Otherwise, drop the token, unblocking the sources at the
395                    // end rendering.
396                    if dyncfgs::DELAY_SOURCES_PAST_REHYDRATION
397                        .get(storage_state.storage_configuration.config_set())
398                    {
399                        crate::upsert::rehydration_finished(
400                            scope.clone(),
401                            base_source_config,
402                            rehydrated_token,
403                            refine_antichain(&resume_upper),
404                            snapshot_progress.clone(),
405                        );
406                    } else {
407                        drop(rehydrated_token)
408                    };
409
410                    // If backpressure from persist is enabled, we connect the upsert operator's
411                    // snapshot progress to the persist source feedback handle.
412                    if let Some(feedback_handle) = feedback_handle {
413                        snapshot_progress.connect_loop(feedback_handle);
414                    }
415
416                    (
417                        upsert.leave(outer_mz_scope),
418                        health_update
419                            .map(|(id, update)| HealthStatusMessage {
420                                id,
421                                namespace: StatusNamespace::Upsert,
422                                update,
423                            })
424                            .leave(outer_mz_scope),
425                    )
426                },
427            );
428
429            let (upsert_ok, upsert_err) = upsert.inner.ok_err(split_ok_err);
430            error_collections.push(upsert_err.as_collection());
431
432            (upsert_ok.as_collection(), Some(health_update))
433        }
434        SourceEnvelope::None(none_envelope) => {
435            let results = append_metadata_to_value(decoded_stream);
436
437            let flattened_stream = flatten_results_prepend_keys(none_envelope, results);
438
439            let (stream, errors) = flattened_stream.inner.ok_err(split_ok_err);
440
441            error_collections.push(errors.as_collection());
442            (stream.as_collection(), None)
443        }
444        SourceEnvelope::CdcV2 => {
445            let (oks, token) = render_decode_cdcv2(&decoded_stream);
446            needed_tokens.push(token);
447            (oks, None)
448        }
449    };
450
451    // Return the collections and any needed tokens.
452    let health = decode_health.into_iter().chain(envelope_health).collect();
453    (envelope_ok, needed_tokens, health)
454}
455
456// Returns the maximum limit of inflight bytes for backpressure based on given config
457// and the current cluster size
458fn get_backpressure_max_inflight_bytes(
459    inflight_bytes_config: &StorageMaxInflightBytesConfig,
460    cluster_memory_limit: &Option<usize>,
461) -> Option<usize> {
462    let StorageMaxInflightBytesConfig {
463        max_inflight_bytes_default,
464        max_inflight_bytes_cluster_size_fraction,
465        disk_only: _,
466    } = inflight_bytes_config;
467
468    // Will use backpressure only if the default inflight value is provided
469    if max_inflight_bytes_default.is_some() {
470        let current_cluster_max_bytes_limit =
471            cluster_memory_limit.as_ref().and_then(|cluster_memory| {
472                max_inflight_bytes_cluster_size_fraction.map(|fraction| {
473                    // We just need close the correct % of bytes here, so we just use lossy casts.
474                    usize::cast_lossy(f64::cast_lossy(*cluster_memory) * fraction)
475                })
476            });
477        current_cluster_max_bytes_limit.or(*max_inflight_bytes_default)
478    } else {
479        None
480    }
481}
482
483// TODO: Maybe we should finally move this to some central place and re-use. There seem to be
484// enough instances of this by now.
485fn split_ok_err<O, E, T, D>(x: (Result<O, E>, T, D)) -> Result<(O, T, D), (E, T, D)> {
486    match x {
487        (Ok(ok), ts, diff) => Ok((ok, ts, diff)),
488        (Err(err), ts, diff) => Err((err, ts, diff)),
489    }
490}
491
492/// After handling metadata insertion, we split streams into key/value parts for convenience
493#[derive(
494    Debug,
495    Clone,
496    Hash,
497    PartialEq,
498    Eq,
499    Ord,
500    PartialOrd,
501    Serialize,
502    Deserialize
503)]
504struct KV {
505    key: Option<Result<Row, DecodeError>>,
506    val: Option<Result<Row, DecodeError>>,
507}
508
509fn append_metadata_to_value<'scope, T: Timestamp, FromTime: Timestamp>(
510    results: VecCollection<'scope, T, DecodeResult<FromTime>, Diff>,
511) -> VecCollection<'scope, T, KV, Diff> {
512    results.map(move |res| {
513        let val = res.value.map(|val_result| {
514            val_result.map(|mut val| {
515                if !res.metadata.is_empty() {
516                    RowPacker::for_existing_row(&mut val).extend_by_row(&res.metadata);
517                }
518                val
519            })
520        });
521
522        KV { val, key: res.key }
523    })
524}
525
526/// Convert from streams of [`DecodeResult`] to UpsertCommands, inserting the Key according to [`KeyEnvelope`]
527fn upsert_commands<'scope, T: Timestamp, FromTime: Timestamp>(
528    input: VecCollection<'scope, T, DecodeResult<FromTime>, Diff>,
529    upsert_envelope: UpsertEnvelope,
530) -> VecCollection<'scope, T, (UpsertKey, Option<UpsertValue>, FromTime), Diff> {
531    let mut row_buf = Row::default();
532    input.map(move |result| {
533        let from_time = result.from_time;
534
535        let key = match result.key {
536            Some(Ok(key)) => Ok(key),
537            None => Err(UpsertError::NullKey(UpsertNullKeyError)),
538            Some(Err(err)) => Err(UpsertError::KeyDecode(err)),
539        };
540
541        // If we have a well-formed key we can continue, otherwise we're upserting an error
542        let key = match key {
543            Ok(key) => key,
544            Err(err) => match result.value {
545                Some(_) => {
546                    return (
547                        UpsertKey::from_key(Err(&err)),
548                        Some(Err(Box::new(err))),
549                        from_time,
550                    );
551                }
552                None => return (UpsertKey::from_key(Err(&err)), None, from_time),
553            },
554        };
555
556        // We can now apply the key envelope
557        let key_row = match upsert_envelope.style {
558            // flattened or debezium
559            UpsertStyle::Debezium { .. }
560            | UpsertStyle::Default(KeyEnvelope::Flattened)
561            | UpsertStyle::ValueErrInline {
562                key_envelope: KeyEnvelope::Flattened,
563                error_column: _,
564            } => key,
565            // named
566            UpsertStyle::Default(KeyEnvelope::Named(_))
567            | UpsertStyle::ValueErrInline {
568                key_envelope: KeyEnvelope::Named(_),
569                error_column: _,
570            } => {
571                if key.iter().nth(1).is_none() {
572                    key
573                } else {
574                    row_buf.packer().push_list(key.iter());
575                    row_buf.clone()
576                }
577            }
578            UpsertStyle::Default(KeyEnvelope::None)
579            | UpsertStyle::ValueErrInline {
580                key_envelope: KeyEnvelope::None,
581                error_column: _,
582            } => unreachable!(),
583        };
584
585        let key = UpsertKey::from_key(Ok(&key_row));
586
587        let metadata = result.metadata;
588
589        let value = match result.value {
590            Some(Ok(ref row)) => match upsert_envelope.style {
591                UpsertStyle::Debezium { after_idx } => match row.iter().nth(after_idx).unwrap() {
592                    Datum::List(after) => {
593                        let mut packer = row_buf.packer();
594                        packer.extend(after.iter());
595                        packer.extend_by_row(&metadata);
596                        Some(Ok(row_buf.clone()))
597                    }
598                    Datum::Null => None,
599                    d => panic!("type error: expected record, found {:?}", d),
600                },
601                UpsertStyle::Default(_) => {
602                    let mut packer = row_buf.packer();
603                    packer.extend_by_row(&key_row);
604                    packer.extend_by_row(row);
605                    packer.extend_by_row(&metadata);
606                    Some(Ok(row_buf.clone()))
607                }
608                UpsertStyle::ValueErrInline { .. } => {
609                    let mut packer = row_buf.packer();
610                    packer.extend_by_row(&key_row);
611                    // The 'error' column is null
612                    packer.push(Datum::Null);
613                    packer.extend_by_row(row);
614                    packer.extend_by_row(&metadata);
615                    Some(Ok(row_buf.clone()))
616                }
617            },
618            Some(Err(inner)) => {
619                match upsert_envelope.style {
620                    UpsertStyle::ValueErrInline { .. } => {
621                        let mut count = 0;
622                        // inline the error in the data output
623                        let err_string = inner.to_string();
624                        let mut packer = row_buf.packer();
625                        for datum in key_row.iter() {
626                            packer.push(datum);
627                            count += 1;
628                        }
629                        // The 'error' column is a record with a 'description' column
630                        packer.push_list(iter::once(Datum::String(&err_string)));
631                        count += 1;
632                        let metadata_len = metadata.as_row_ref().iter().count();
633                        // push nulls for all value columns
634                        packer.extend(
635                            iter::repeat(Datum::Null)
636                                .take(upsert_envelope.source_arity - count - metadata_len),
637                        );
638                        packer.extend_by_row(&metadata);
639                        Some(Ok(row_buf.clone()))
640                    }
641                    _ => Some(Err(Box::new(UpsertError::Value(UpsertValueError {
642                        for_key: key_row,
643                        inner,
644                    })))),
645                }
646            }
647            None => None,
648        };
649
650        (key, value, from_time)
651    })
652}
653
654/// Convert from streams of [`DecodeResult`] to Rows, inserting the Key according to [`KeyEnvelope`]
655fn flatten_results_prepend_keys<'scope, T: Timestamp>(
656    none_envelope: &NoneEnvelope,
657    results: VecCollection<'scope, T, KV, Diff>,
658) -> VecCollection<'scope, T, Result<Row, DataflowError>, Diff> {
659    let NoneEnvelope {
660        key_envelope,
661        key_arity,
662    } = none_envelope;
663
664    let null_key_columns = Row::pack_slice(&vec![Datum::Null; *key_arity]);
665
666    match key_envelope {
667        KeyEnvelope::None => {
668            results.flat_map(|KV { val, .. }| val.map(|result| result.map_err(Into::into)))
669        }
670        KeyEnvelope::Flattened => results
671            .flat_map(raise_key_value_errors)
672            .map(move |maybe_kv| {
673                maybe_kv.map(|(key, value)| {
674                    let mut key = key.unwrap_or_else(|| null_key_columns.clone());
675                    RowPacker::for_existing_row(&mut key).extend_by_row(&value);
676                    key
677                })
678            }),
679        KeyEnvelope::Named(_) => {
680            results
681                .flat_map(raise_key_value_errors)
682                .map(move |maybe_kv| {
683                    maybe_kv.map(|(key, value)| {
684                        let mut key = key.unwrap_or_else(|| null_key_columns.clone());
685                        // Named semantics rename a key that is a single column, and encode a
686                        // multi-column field as a struct with that name
687                        let row = if key.iter().nth(1).is_none() {
688                            RowPacker::for_existing_row(&mut key).extend_by_row(&value);
689                            key
690                        } else {
691                            let mut new_row = Row::default();
692                            let mut packer = new_row.packer();
693                            packer.push_list(key.iter());
694                            packer.extend_by_row(&value);
695                            new_row
696                        };
697                        row
698                    })
699                })
700        }
701    }
702}
703
704/// Handle possibly missing key or value portions of messages
705fn raise_key_value_errors(
706    KV { key, val }: KV,
707) -> Option<Result<(Option<Row>, Row), DataflowError>> {
708    match (key, val) {
709        (Some(Ok(key)), Some(Ok(value))) => Some(Ok((Some(key), value))),
710        (None, Some(Ok(value))) => Some(Ok((None, value))),
711        // always prioritize the value error if either or both have an error
712        (_, Some(Err(e))) => Some(Err(e.into())),
713        (Some(Err(e)), _) => Some(Err(e.into())),
714        (None, None) => None,
715        // TODO(petrosagg): these errors would be better grouped under an EnvelopeError enum
716        _ => Some(Err(DataflowError::from(EnvelopeError::Flat(
717            "Value not present for message".into(),
718        )))),
719    }
720}
721
722#[cfg(test)]
723mod test {
724    use super::*;
725
726    #[mz_ore::test]
727    fn test_no_default() {
728        let config = StorageMaxInflightBytesConfig {
729            max_inflight_bytes_default: None,
730            max_inflight_bytes_cluster_size_fraction: Some(0.5),
731            disk_only: false,
732        };
733        let memory_limit = Some(1000);
734
735        let backpressure_inflight_bytes_limit =
736            get_backpressure_max_inflight_bytes(&config, &memory_limit);
737
738        assert_eq!(backpressure_inflight_bytes_limit, None)
739    }
740
741    #[mz_ore::test]
742    fn test_no_matching_size() {
743        let config = StorageMaxInflightBytesConfig {
744            max_inflight_bytes_default: Some(10000),
745            max_inflight_bytes_cluster_size_fraction: Some(0.5),
746            disk_only: false,
747        };
748
749        let backpressure_inflight_bytes_limit = get_backpressure_max_inflight_bytes(&config, &None);
750
751        assert_eq!(
752            backpressure_inflight_bytes_limit,
753            config.max_inflight_bytes_default
754        )
755    }
756
757    #[mz_ore::test]
758    fn test_calculated_cluster_limit() {
759        let config = StorageMaxInflightBytesConfig {
760            max_inflight_bytes_default: Some(10000),
761            max_inflight_bytes_cluster_size_fraction: Some(0.5),
762            disk_only: false,
763        };
764        let memory_limit = Some(2000);
765
766        let backpressure_inflight_bytes_limit =
767            get_backpressure_max_inflight_bytes(&config, &memory_limit);
768
769        // the limit should be 50% of 2000 i.e. 1000
770        assert_eq!(backpressure_inflight_bytes_limit, Some(1000));
771    }
772}