Skip to main content

mz_storage_controller/
lib.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//! Implementation of the storage controller trait.
11
12use std::any::Any;
13use std::collections::btree_map;
14use std::collections::{BTreeMap, BTreeSet};
15use std::fmt::Debug;
16use std::str::FromStr;
17use std::sync::{Arc, Mutex};
18use std::time::Duration;
19
20use crate::collection_mgmt::{
21    AppendOnlyIntrospectionConfig, CollectionManagerKind, DifferentialIntrospectionConfig,
22};
23use crate::instance::{Instance, ReplicaConfig};
24use async_trait::async_trait;
25use chrono::{DateTime, DurationRound, TimeDelta, Utc};
26use derivative::Derivative;
27use differential_dataflow::lattice::Lattice;
28use futures::FutureExt;
29use futures::StreamExt;
30use itertools::Itertools;
31use mz_build_info::BuildInfo;
32use mz_cluster_client::client::ClusterReplicaLocation;
33use mz_cluster_client::metrics::{ControllerMetrics, WallclockLagMetrics};
34use mz_cluster_client::{ReplicaId, WallclockLagFn};
35use mz_controller_types::dyncfgs::{
36    ENABLE_0DT_DEPLOYMENT_SOURCES, WALLCLOCK_LAG_RECORDING_INTERVAL,
37};
38use mz_dyncfg::ConfigUpdates;
39use mz_ore::collections::CollectionExt;
40use mz_ore::metrics::MetricsRegistry;
41use mz_ore::now::NowFn;
42use mz_ore::task::AbortOnDropHandle;
43use mz_ore::{assert_none, halt, instrument, soft_panic_or_log};
44use mz_persist_client::batch::ProtoBatch;
45use mz_persist_client::cache::PersistClientCache;
46use mz_persist_client::cfg::USE_CRITICAL_SINCE_SNAPSHOT;
47use mz_persist_client::critical::Opaque;
48use mz_persist_client::read::ReadHandle;
49use mz_persist_client::schema::CaESchema;
50use mz_persist_client::write::WriteHandle;
51use mz_persist_client::{Diagnostics, PersistClient, PersistLocation, ShardId};
52use mz_persist_types::codec_impls::UnitSchema;
53use mz_repr::adt::timestamp::CheckedTimestamp;
54use mz_repr::{Datum, Diff, GlobalId, RelationDesc, RelationVersion, Row, Timestamp};
55use mz_storage_client::client::{
56    AppendOnlyUpdate, RunIngestionCommand, RunOneshotIngestion, RunSinkCommand, Status,
57    StatusUpdate, StorageCommand, StorageResponse, TableData,
58};
59use mz_storage_client::controller::{
60    BoxFuture, CollectionDescription, DataSource, ExportDescription, ExportState,
61    IntrospectionType, MonotonicAppender, PersistEpoch, Response, StorageController,
62    StorageMetadata, StorageTxn, StorageWriteOp, TableRegistration, WallclockLag,
63    WallclockLagHistogramPeriod,
64};
65use mz_storage_client::healthcheck::{
66    MZ_AWS_PRIVATELINK_CONNECTION_STATUS_HISTORY_DESC, MZ_SINK_STATUS_HISTORY_DESC,
67    MZ_SOURCE_STATUS_HISTORY_DESC, REPLICA_STATUS_HISTORY_DESC,
68};
69use mz_storage_client::metrics::StorageControllerMetrics;
70use mz_storage_client::statistics::{
71    ControllerSinkStatistics, ControllerSourceStatistics, WebhookStatistics,
72};
73use mz_storage_client::storage_collections::StorageCollections;
74use mz_storage_types::configuration::StorageConfiguration;
75use mz_storage_types::connections::ConnectionContext;
76use mz_storage_types::connections::inline::InlinedConnection;
77use mz_storage_types::controller::{AlterError, CollectionMetadata, StorageError, TxnsCodecRow};
78use mz_storage_types::errors::CollectionMissing;
79use mz_storage_types::instances::StorageInstanceId;
80use mz_storage_types::oneshot_sources::{OneshotIngestionRequest, OneshotResultCallback};
81use mz_storage_types::parameters::StorageParameters;
82use mz_storage_types::read_holds::ReadHold;
83use mz_storage_types::read_policy::ReadPolicy;
84use mz_storage_types::sinks::{StorageSinkConnection, StorageSinkDesc};
85use mz_storage_types::sources::{
86    GenericSourceConnection, IngestionDescription, SourceConnection, SourceData, SourceDesc,
87    SourceExport, SourceExportDataConfig,
88};
89use mz_storage_types::{AlterCompatible, StorageDiff, dyncfgs};
90use mz_txn_wal::metrics::Metrics as TxnMetrics;
91use mz_txn_wal::txn_read::TxnsRead;
92use mz_txn_wal::txns::TxnsHandle;
93use timely::order::PartialOrder;
94use timely::progress::frontier::MutableAntichain;
95use timely::progress::{Antichain, ChangeBatch};
96use tokio::sync::watch::{Sender, channel};
97use tokio::sync::{mpsc, oneshot};
98use tokio::time::MissedTickBehavior;
99use tokio::time::error::Elapsed;
100use tracing::{debug, info, warn};
101
102mod collection_mgmt;
103mod history;
104mod instance;
105mod persist_handles;
106mod rtr;
107mod statistics;
108
109#[derive(Derivative)]
110#[derivative(Debug)]
111struct PendingOneshotIngestion {
112    /// Callback used to provide results of the ingestion.
113    #[derivative(Debug = "ignore")]
114    result_tx: OneshotResultCallback<ProtoBatch>,
115    /// Cluster currently running this ingestion
116    cluster_id: StorageInstanceId,
117}
118
119impl PendingOneshotIngestion {
120    /// Consume the pending ingestion, responding with a cancelation message.
121    ///
122    /// TODO(cf2): Refine these error messages so they're not stringly typed.
123    pub(crate) fn cancel(self) {
124        (self.result_tx)(vec![Err("canceled".to_string())])
125    }
126}
127
128/// A storage controller for a storage instance.
129#[derive(Derivative)]
130#[derivative(Debug)]
131pub struct Controller {
132    /// The build information for this process.
133    build_info: &'static BuildInfo,
134    /// A function that returns the current time.
135    now: NowFn,
136
137    /// Whether or not this controller is in read-only mode.
138    ///
139    /// When in read-only mode, neither this controller nor the instances
140    /// controlled by it are allowed to affect changes to external systems
141    /// (largely persist).
142    read_only: bool,
143
144    /// Collections maintained by the storage controller.
145    ///
146    /// This collection only grows, although individual collections may be rendered unusable.
147    /// This is to prevent the re-binding of identifiers to other descriptions.
148    pub(crate) collections: BTreeMap<GlobalId, CollectionState>,
149
150    /// Map from IDs of objects that have been dropped to replicas we are still
151    /// expecting DroppedId messages from. This is cleared out once all replicas
152    /// have responded.
153    ///
154    /// We use this only to catch problems in the protocol between controller
155    /// and replicas, for example we can differentiate between late messages for
156    /// objects that have already been dropped and unexpected (read erroneous)
157    /// messages from the replica.
158    dropped_objects: BTreeMap<GlobalId, BTreeSet<ReplicaId>>,
159
160    /// Write handle for table shards.
161    pub(crate) persist_table_worker: persist_handles::PersistTableWriteWorker,
162    /// A shared TxnsCache running in a task and communicated with over a channel.
163    txns_read: TxnsRead<Timestamp>,
164    txns_metrics: Arc<TxnMetrics>,
165    stashed_responses: Vec<(Option<ReplicaId>, StorageResponse)>,
166    /// Channel for sending table handle drops.
167    #[derivative(Debug = "ignore")]
168    pending_table_handle_drops_tx: mpsc::UnboundedSender<GlobalId>,
169    /// Channel for receiving table handle drops.
170    #[derivative(Debug = "ignore")]
171    pending_table_handle_drops_rx: mpsc::UnboundedReceiver<GlobalId>,
172    /// Closures that can be used to send responses from oneshot ingestions.
173    #[derivative(Debug = "ignore")]
174    pending_oneshot_ingestions: BTreeMap<uuid::Uuid, PendingOneshotIngestion>,
175
176    /// Interface for managed collections
177    pub(crate) collection_manager: collection_mgmt::CollectionManager,
178
179    /// Tracks which collection is responsible for which [`IntrospectionType`].
180    pub(crate) introspection_ids: BTreeMap<IntrospectionType, GlobalId>,
181    /// Tokens for tasks that drive updating introspection collections. Dropping
182    /// this will make sure that any tasks (or other resources) will stop when
183    /// needed.
184    // TODO(aljoscha): Should these live somewhere else?
185    introspection_tokens: Arc<Mutex<BTreeMap<GlobalId, Box<dyn Any + Send + Sync>>>>,
186
187    // The following two fields must always be locked in order.
188    /// Consolidated metrics updates to periodically write. We do not eagerly initialize this,
189    /// and its contents are entirely driven by `StorageResponse::StatisticsUpdates`'s, as well
190    /// as webhook statistics.
191    source_statistics: Arc<Mutex<statistics::SourceStatistics>>,
192    /// Consolidated metrics updates to periodically write. We do not eagerly initialize this,
193    /// and its contents are entirely driven by `StorageResponse::StatisticsUpdates`'s.
194    sink_statistics: Arc<Mutex<BTreeMap<(GlobalId, Option<ReplicaId>), ControllerSinkStatistics>>>,
195    /// A way to update the statistics interval in the statistics tasks.
196    statistics_interval_sender: Sender<Duration>,
197
198    /// Clients for all known storage instances.
199    instances: BTreeMap<StorageInstanceId, Instance>,
200    /// Per-replica dyncfg overrides, consulted when a replica connects. The
201    /// overrides for a replica created by DDL are committed in the same
202    /// transaction that creates it, so they are installed before the replica
203    /// connects.
204    replica_dyncfg_overrides: BTreeMap<ReplicaId, ConfigUpdates>,
205    /// Set to `true` once `initialization_complete` has been called.
206    initialized: bool,
207    /// Storage configuration to apply to newly provisioned instances, and use during purification.
208    config: StorageConfiguration,
209    /// The persist location where all storage collections are being written to
210    persist_location: PersistLocation,
211    /// A persist client used to write to storage collections
212    persist: Arc<PersistClientCache>,
213    /// Metrics of the Storage controller
214    metrics: StorageControllerMetrics,
215    /// `(read, write)` frontiers that have been recorded in the `Frontiers` collection, kept to be
216    /// able to retract old rows.
217    recorded_frontiers: BTreeMap<GlobalId, (Antichain<Timestamp>, Antichain<Timestamp>)>,
218    /// Write frontiers that have been recorded in the `ReplicaFrontiers` collection, kept to be
219    /// able to retract old rows.
220    recorded_replica_frontiers: BTreeMap<(GlobalId, ReplicaId), Antichain<Timestamp>>,
221
222    /// A function that computes the lag between the given time and wallclock time.
223    #[derivative(Debug = "ignore")]
224    wallclock_lag: WallclockLagFn<Timestamp>,
225    /// The last time wallclock lag introspection was recorded.
226    wallclock_lag_last_recorded: DateTime<Utc>,
227
228    /// Handle to a [StorageCollections].
229    storage_collections: Arc<dyn StorageCollections + Send + Sync>,
230    /// Migrated storage collections that can be written even in read only mode.
231    migrated_storage_collections: BTreeSet<GlobalId>,
232
233    /// Ticker for scheduling periodic maintenance work.
234    maintenance_ticker: tokio::time::Interval,
235    /// Whether maintenance work was scheduled.
236    maintenance_scheduled: bool,
237
238    /// Shared transmit channel for replicas to send responses.
239    instance_response_tx: mpsc::UnboundedSender<(Option<ReplicaId>, StorageResponse)>,
240    /// Receive end for replica responses.
241    instance_response_rx: mpsc::UnboundedReceiver<(Option<ReplicaId>, StorageResponse)>,
242
243    /// Background task run at startup to warm persist state.
244    persist_warm_task: Option<AbortOnDropHandle<Box<dyn Debug + Send>>>,
245}
246
247/// Warm up persist state for `shard_ids` in a background task.
248///
249/// With better parallelism during startup this would likely be unnecessary, but empirically we see
250/// some nice speedups with this relatively simple function.
251/// What `create_collections_for_bootstrap` opens per collection: a write handle for collections
252/// the controller writes directly, or just a recent, linearized upper for txns-managed tables,
253/// whose writes go through the table-write worker (which opens its own handles per
254/// registration).
255enum WriteHandleOrUpper {
256    Handle(WriteHandle<SourceData, (), Timestamp, StorageDiff>),
257    Upper(Antichain<Timestamp>),
258}
259
260impl WriteHandleOrUpper {
261    fn upper(&self) -> Antichain<Timestamp> {
262        match self {
263            Self::Handle(handle) => handle.upper().clone(),
264            Self::Upper(upper) => upper.clone(),
265        }
266    }
267
268    /// Returns the write handle, panicking for the `Upper` variant. Only call for data sources
269    /// that are never tables.
270    fn expect_handle(self, context: &str) -> WriteHandle<SourceData, (), Timestamp, StorageDiff> {
271        match self {
272            Self::Handle(handle) => handle,
273            Self::Upper(_) => panic!("write handle required: {context}"),
274        }
275    }
276}
277
278fn warm_persist_state_in_background(
279    client: PersistClient,
280    shard_ids: impl Iterator<Item = ShardId> + Send + 'static,
281) -> mz_ore::task::JoinHandle<Box<dyn Debug + Send>> {
282    /// Bound the number of shards that we warm at a single time, to limit our overall resource use.
283    const MAX_CONCURRENT_WARMS: usize = 16;
284    let logic = async move {
285        let fetchers: Vec<_> = tokio_stream::iter(shard_ids)
286            .map(|shard_id| {
287                let client = client.clone();
288                async move {
289                    client
290                        .create_batch_fetcher::<SourceData, (), mz_repr::Timestamp, StorageDiff>(
291                            shard_id,
292                            Arc::new(RelationDesc::empty()),
293                            Arc::new(UnitSchema),
294                            true,
295                            Diagnostics::from_purpose("warm persist load state"),
296                        )
297                        .await
298                }
299            })
300            .buffer_unordered(MAX_CONCURRENT_WARMS)
301            .collect()
302            .await;
303        let fetchers: Box<dyn Debug + Send> = Box::new(fetchers);
304        fetchers
305    };
306    mz_ore::task::spawn(|| "warm_persist_load_state", logic)
307}
308
309#[async_trait(?Send)]
310impl StorageController for Controller {
311    fn initialization_complete(&mut self) {
312        self.reconcile_dangling_statistics();
313        self.initialized = true;
314
315        for instance in self.instances.values_mut() {
316            instance.send(StorageCommand::InitializationComplete);
317        }
318    }
319
320    fn update_parameters(&mut self, config_params: StorageParameters) {
321        self.storage_collections
322            .update_parameters(config_params.clone());
323
324        // We serialize the dyncfg updates in StorageParameters, but configure
325        // persist separately.
326        self.persist.cfg().apply_from(&config_params.dyncfg_updates);
327
328        for instance in self.instances.values_mut() {
329            let params = Box::new(config_params.clone());
330            instance.send(StorageCommand::UpdateConfiguration(params));
331        }
332        self.config.update(config_params);
333        self.statistics_interval_sender
334            .send_replace(self.config.parameters.statistics_interval);
335        self.collection_manager.update_user_batch_duration(
336            self.config
337                .parameters
338                .user_storage_managed_collections_batch_duration,
339        );
340    }
341
342    fn update_replica_dyncfg_overrides(
343        &mut self,
344        mut overrides: BTreeMap<StorageInstanceId, BTreeMap<ReplicaId, ConfigUpdates>>,
345    ) {
346        self.replica_dyncfg_overrides = overrides
347            .values()
348            .flat_map(|replicas| replicas.iter())
349            .map(|(replica_id, updates)| (*replica_id, updates.clone()))
350            .collect();
351        for (id, instance) in self.instances.iter_mut() {
352            let instance_overrides = overrides.remove(id).unwrap_or_default();
353            instance.update_replica_dyncfg_overrides(instance_overrides);
354        }
355    }
356
357    /// Get the current configuration
358    fn config(&self) -> &StorageConfiguration {
359        &self.config
360    }
361
362    fn collection_metadata(&self, id: GlobalId) -> Result<CollectionMetadata, CollectionMissing> {
363        self.storage_collections.collection_metadata(id)
364    }
365
366    fn collection_hydrated(&self, collection_id: GlobalId) -> Result<bool, StorageError> {
367        let collection = self.collection(collection_id)?;
368
369        let instance_id = match &collection.data_source {
370            DataSource::Ingestion(ingestion_description) => ingestion_description.instance_id,
371            DataSource::IngestionExport { ingestion_id, .. } => {
372                let ingestion_state = self.collections.get(ingestion_id).expect("known to exist");
373
374                let instance_id = match &ingestion_state.data_source {
375                    DataSource::Ingestion(ingestion_desc) => ingestion_desc.instance_id,
376                    _ => unreachable!("SourceExport must only refer to primary source"),
377                };
378
379                instance_id
380            }
381            _ => return Ok(true),
382        };
383
384        let instance = self.instances.get(&instance_id).ok_or_else(|| {
385            StorageError::IngestionInstanceMissing {
386                storage_instance_id: instance_id,
387                ingestion_id: collection_id,
388            }
389        })?;
390
391        if instance.replica_ids().next().is_none() {
392            // Ingestions on zero-replica clusters are always considered
393            // hydrated.
394            return Ok(true);
395        }
396
397        match &collection.extra_state {
398            CollectionStateExtra::Ingestion(ingestion_state) => {
399                // An ingestion is hydrated if it is hydrated on at least one replica.
400                Ok(ingestion_state.hydrated_on.len() >= 1)
401            }
402            CollectionStateExtra::Export(_) => {
403                // For now, sinks are always considered hydrated. We rely on
404                // them starting up "instantly" and don't wait for them when
405                // checking hydration status of a replica.  TODO(sinks): base
406                // this off of the sink shard's frontier?
407                Ok(true)
408            }
409            CollectionStateExtra::None => {
410                // For now, objects that are not ingestions are always
411                // considered hydrated. This is tables and webhooks, as of
412                // today.
413                Ok(true)
414            }
415        }
416    }
417
418    #[mz_ore::instrument(level = "debug")]
419    fn collections_hydrated_on_replicas(
420        &self,
421        target_replica_ids: Option<Vec<ReplicaId>>,
422        target_cluster_id: &StorageInstanceId,
423        exclude_collections: &BTreeSet<GlobalId>,
424    ) -> Result<bool, StorageError> {
425        // If an empty set of replicas is provided there can be
426        // no collections on them, we'll count this as hydrated.
427        if target_replica_ids.as_ref().is_some_and(|v| v.is_empty()) {
428            return Ok(true);
429        }
430
431        // If target_replica_ids is provided, use it as the set of target
432        // replicas. Otherwise check for hydration on any replica.
433        let target_replicas: Option<BTreeSet<ReplicaId>> =
434            target_replica_ids.map(|ids| ids.into_iter().collect());
435
436        let instance = self.instances.get(target_cluster_id);
437
438        let mut all_hydrated = true;
439        for (collection_id, collection_state) in self.collections.iter() {
440            if collection_id.is_transient() || exclude_collections.contains(collection_id) {
441                continue;
442            }
443            let hydrated = match &collection_state.extra_state {
444                CollectionStateExtra::Ingestion(state) => {
445                    if &state.instance_id != target_cluster_id {
446                        continue;
447                    }
448                    match &target_replicas {
449                        Some(target_replicas) => {
450                            // Not scheduled on any target replica means it can
451                            // never hydrate there, so it does not count (see
452                            // the trait docs). If the instance is unknown (the
453                            // cluster is being dropped concurrently) the
454                            // scheduled set is empty and every ingestion is
455                            // skipped. Readiness callers gate on replica health
456                            // separately, which covers that window.
457                            let scheduled_on = instance
458                                .map(|i| i.get_active_replicas_for_object(collection_id))
459                                .unwrap_or_default();
460                            if scheduled_on.is_disjoint(target_replicas) {
461                                true
462                            } else {
463                                !state.hydrated_on.is_disjoint(target_replicas)
464                            }
465                        }
466                        None => {
467                            // Not target replicas, so check that it's hydrated
468                            // on at least one replica.
469                            state.hydrated_on.len() >= 1
470                        }
471                    }
472                }
473                CollectionStateExtra::Export(_) => {
474                    // For now, sinks are always considered hydrated. We rely on
475                    // them starting up "instantly" and don't wait for them when
476                    // checking hydration status of a replica.  TODO(sinks):
477                    // base this off of the sink shard's frontier?
478                    true
479                }
480                CollectionStateExtra::None => {
481                    // For now, objects that are not ingestions are always
482                    // considered hydrated. This is tables and webhooks, as of
483                    // today.
484                    true
485                }
486            };
487            if !hydrated {
488                tracing::info!(%collection_id, "collection is not hydrated on any replica");
489                all_hydrated = false;
490                // We continue with our loop instead of breaking out early, so
491                // that we log all non-hydrated replicas.
492            }
493        }
494        Ok(all_hydrated)
495    }
496
497    fn collection_frontiers(
498        &self,
499        id: GlobalId,
500    ) -> Result<(Antichain<Timestamp>, Antichain<Timestamp>), CollectionMissing> {
501        let frontiers = self.storage_collections.collection_frontiers(id)?;
502        Ok((frontiers.implied_capability, frontiers.write_frontier))
503    }
504
505    fn collections_frontiers(
506        &self,
507        mut ids: Vec<GlobalId>,
508    ) -> Result<Vec<(GlobalId, Antichain<Timestamp>, Antichain<Timestamp>)>, CollectionMissing>
509    {
510        let mut result = vec![];
511        // In theory, we could pull all our frontiers from storage collections...
512        // but in practice those frontiers may not be identical. For historical reasons, we use the
513        // locally-tracked frontier for sinks but the storage-collections-maintained frontier for
514        // sources.
515        ids.retain(|&id| match self.export(id) {
516            Ok(export) => {
517                result.push((
518                    id,
519                    export.input_hold().since().clone(),
520                    export.write_frontier.clone(),
521                ));
522                false
523            }
524            Err(_) => true,
525        });
526        result.extend(
527            self.storage_collections
528                .collections_frontiers(ids)?
529                .into_iter()
530                .map(|frontiers| {
531                    (
532                        frontiers.id,
533                        frontiers.implied_capability,
534                        frontiers.write_frontier,
535                    )
536                }),
537        );
538
539        Ok(result)
540    }
541
542    fn active_collection_metadatas(&self) -> Vec<(GlobalId, CollectionMetadata)> {
543        self.storage_collections.active_collection_metadatas()
544    }
545
546    fn active_ingestion_exports(
547        &self,
548        instance_id: StorageInstanceId,
549    ) -> Box<dyn Iterator<Item = &GlobalId> + '_> {
550        let active_storage_collections: BTreeMap<_, _> = self
551            .storage_collections
552            .active_collection_frontiers()
553            .into_iter()
554            .map(|c| (c.id, c))
555            .collect();
556
557        let active_exports = self.instances[&instance_id]
558            .active_ingestion_exports()
559            .filter(move |id| {
560                let frontiers = active_storage_collections.get(id);
561                match frontiers {
562                    Some(frontiers) => !frontiers.write_frontier.is_empty(),
563                    None => {
564                        // Not "active", so we don't care here.
565                        false
566                    }
567                }
568            });
569
570        Box::new(active_exports)
571    }
572
573    fn check_exists(&self, id: GlobalId) -> Result<(), StorageError> {
574        self.storage_collections.check_exists(id)
575    }
576
577    fn create_instance(&mut self, id: StorageInstanceId, workload_class: Option<String>) {
578        let metrics = self.metrics.for_instance(id);
579        let mut instance = Instance::new(
580            workload_class,
581            metrics,
582            self.now.clone(),
583            self.instance_response_tx.clone(),
584        );
585        if self.initialized {
586            instance.send(StorageCommand::InitializationComplete);
587        }
588        if !self.read_only {
589            instance.send(StorageCommand::AllowWrites);
590        }
591
592        let params = Box::new(self.config.parameters.clone());
593        instance.send(StorageCommand::UpdateConfiguration(params));
594
595        let old_instance = self.instances.insert(id, instance);
596        assert_none!(old_instance, "storage instance {id} already exists");
597    }
598
599    fn drop_instance(&mut self, id: StorageInstanceId) {
600        let instance = self.instances.remove(&id);
601        assert!(instance.is_some(), "storage instance {id} does not exist");
602    }
603
604    fn update_instance_workload_class(
605        &mut self,
606        id: StorageInstanceId,
607        workload_class: Option<String>,
608    ) {
609        let instance = self
610            .instances
611            .get_mut(&id)
612            .unwrap_or_else(|| panic!("instance {id} does not exist"));
613
614        instance.workload_class = workload_class;
615    }
616
617    fn connect_replica(
618        &mut self,
619        instance_id: StorageInstanceId,
620        replica_id: ReplicaId,
621        location: ClusterReplicaLocation,
622    ) {
623        let instance = self
624            .instances
625            .get_mut(&instance_id)
626            .unwrap_or_else(|| panic!("instance {instance_id} does not exist"));
627
628        let config = ReplicaConfig {
629            build_info: self.build_info,
630            location,
631            grpc_client: self.config.parameters.grpc_client.clone(),
632        };
633        let dyncfg_override = self
634            .replica_dyncfg_overrides
635            .get(&replica_id)
636            .cloned()
637            .unwrap_or_default();
638        instance.add_replica(replica_id, config, dyncfg_override);
639    }
640
641    fn drop_replica(&mut self, instance_id: StorageInstanceId, replica_id: ReplicaId) {
642        // The coordinator only re-pushes the override map when the scoped
643        // configuration itself changes, so a dropped replica's entry would
644        // otherwise be retained until the next such change.
645        self.replica_dyncfg_overrides.remove(&replica_id);
646
647        let instance = self
648            .instances
649            .get_mut(&instance_id)
650            .unwrap_or_else(|| panic!("instance {instance_id} does not exist"));
651
652        let status_now = mz_ore::now::to_datetime((self.now)());
653        let mut source_status_updates = vec![];
654        let mut sink_status_updates = vec![];
655
656        // NOTE: mz_source_statuses and mz_sink_statuses rely on per-replica `paused` meaning
657        // "replica dropped"
658        let make_update = |id, object_type| StatusUpdate {
659            id,
660            status: Status::Paused,
661            timestamp: status_now,
662            error: None,
663            hints: BTreeSet::from([format!(
664                "The replica running this {object_type} has been dropped"
665            )]),
666            namespaced_errors: Default::default(),
667            replica_id: Some(replica_id),
668        };
669
670        for ingestion_id in instance.active_ingestions() {
671            if let Some(active_replicas) = self.dropped_objects.get_mut(ingestion_id) {
672                active_replicas.remove(&replica_id);
673                if active_replicas.is_empty() {
674                    self.dropped_objects.remove(ingestion_id);
675                }
676            }
677
678            let ingestion = self
679                .collections
680                .get_mut(ingestion_id)
681                .expect("instance contains unknown ingestion");
682
683            let ingestion_description = match &ingestion.data_source {
684                DataSource::Ingestion(ingestion_description) => ingestion_description.clone(),
685                _ => panic!(
686                    "unexpected data source for ingestion: {:?}",
687                    ingestion.data_source
688                ),
689            };
690
691            let old_style_ingestion = *ingestion_id != ingestion_description.remap_collection_id;
692            let subsource_ids = ingestion_description.collection_ids().filter(|id| {
693                // NOTE(aljoscha): We filter out the remap collection for old style
694                // ingestions because it doesn't get any status updates about it from the
695                // replica side. So we don't want to synthesize a 'paused' status here.
696                // New style ingestion do, since the source itself contains the remap data.
697                let should_discard =
698                    old_style_ingestion && id == &ingestion_description.remap_collection_id;
699                !should_discard
700            });
701            for id in subsource_ids {
702                source_status_updates.push(make_update(id, "source"));
703            }
704        }
705
706        for id in instance.active_exports() {
707            if let Some(active_replicas) = self.dropped_objects.get_mut(id) {
708                active_replicas.remove(&replica_id);
709                if active_replicas.is_empty() {
710                    self.dropped_objects.remove(id);
711                }
712            }
713
714            sink_status_updates.push(make_update(*id, "sink"));
715        }
716
717        instance.drop_replica(replica_id);
718
719        if !self.read_only {
720            if !source_status_updates.is_empty() {
721                self.append_status_introspection_updates(
722                    IntrospectionType::SourceStatusHistory,
723                    source_status_updates,
724                );
725            }
726            if !sink_status_updates.is_empty() {
727                self.append_status_introspection_updates(
728                    IntrospectionType::SinkStatusHistory,
729                    sink_status_updates,
730                );
731            }
732        }
733    }
734
735    async fn evolve_nullability_for_bootstrap(
736        &mut self,
737        storage_metadata: &StorageMetadata,
738        collections: Vec<(GlobalId, RelationDesc)>,
739    ) -> Result<(), StorageError> {
740        let persist_client = self
741            .persist
742            .open(self.persist_location.clone())
743            .await
744            .unwrap();
745
746        for (global_id, relation_desc) in collections {
747            let shard_id = storage_metadata.get_collection_shard(global_id)?;
748            let diagnostics = Diagnostics {
749                shard_name: global_id.to_string(),
750                handle_purpose: "evolve nullability for bootstrap".to_string(),
751            };
752            let latest_schema = persist_client
753                .latest_schema::<SourceData, (), Timestamp, StorageDiff>(shard_id, diagnostics)
754                .await
755                .expect("invalid persist usage");
756            let Some((schema_id, current_schema, _)) = latest_schema else {
757                tracing::debug!(?global_id, "no schema registered");
758                continue;
759            };
760            tracing::debug!(?global_id, ?current_schema, new_schema = ?relation_desc, "migrating schema");
761
762            let diagnostics = Diagnostics {
763                shard_name: global_id.to_string(),
764                handle_purpose: "evolve nullability for bootstrap".to_string(),
765            };
766            let evolve_result = persist_client
767                .compare_and_evolve_schema::<SourceData, (), Timestamp, StorageDiff>(
768                    shard_id,
769                    schema_id,
770                    &relation_desc,
771                    &UnitSchema,
772                    diagnostics,
773                )
774                .await
775                .expect("invalid persist usage");
776            match evolve_result {
777                CaESchema::Ok(_) => (),
778                CaESchema::ExpectedMismatch {
779                    schema_id,
780                    key,
781                    val: _,
782                } => {
783                    return Err(StorageError::PersistSchemaEvolveRace {
784                        global_id,
785                        shard_id,
786                        schema_id,
787                        relation_desc: key,
788                    });
789                }
790                CaESchema::Incompatible => {
791                    return Err(StorageError::PersistInvalidSchemaEvolve {
792                        global_id,
793                        shard_id,
794                    });
795                }
796            };
797        }
798
799        Ok(())
800    }
801
802    /// Create and "execute" the described collection.
803    ///
804    /// "Execute" is in scare quotes because what executing a collection means
805    /// varies widely based on the type of collection you're creating.
806    ///
807    /// The general process creating a collection undergoes is:
808    /// 1. Enrich the description we get from the user with the metadata only
809    ///    the storage controller's metadata. This is mostly a matter of
810    ///    separating concerns.
811    /// 2. Generate write and read persist handles for the collection.
812    /// 3. Store the collection's metadata in the appropriate field.
813    /// 4. "Execute" the collection. What that means is contingent on the type of
814    ///    collection. so consult the code for more details.
815    ///
816    // TODO(aljoscha): It would be swell if we could refactor this Leviathan of
817    // a method/move individual parts to their own methods. @guswynn observes
818    // that a number of these operations could be moved into fns on
819    // `DataSource`.
820    #[instrument(name = "storage::create_collections")]
821    async fn create_collections_for_bootstrap(
822        &mut self,
823        storage_metadata: &StorageMetadata,
824        register_ts: Option<Timestamp>,
825        mut collections: Vec<(GlobalId, CollectionDescription)>,
826        migrated_storage_collections: &BTreeSet<GlobalId>,
827    ) -> Result<(), StorageError> {
828        self.migrated_storage_collections
829            .extend(migrated_storage_collections.iter().cloned());
830
831        self.storage_collections
832            .create_collections_for_bootstrap(
833                storage_metadata,
834                register_ts,
835                collections.clone(),
836                migrated_storage_collections,
837            )
838            .await?;
839
840        // At this point we're connected to all the collection shards in persist. Our warming task
841        // is no longer useful, so abort it if it's still running.
842        drop(self.persist_warm_task.take());
843
844        // Validate first, to avoid corrupting state.
845        // 1. create a dropped identifier, or
846        // 2. create an existing identifier with a new description.
847        // Make sure to check for errors within `ingestions` as well.
848        collections.sort_by_key(|(id, _)| *id);
849        collections.dedup();
850        for pos in 1..collections.len() {
851            if collections[pos - 1].0 == collections[pos].0 {
852                return Err(StorageError::CollectionIdReused(collections[pos].0));
853            }
854        }
855
856        // We first enrich each collection description with some additional metadata...
857        let enriched_with_metadata = collections
858            .into_iter()
859            .map(|(id, description)| {
860                let data_shard = storage_metadata.get_collection_shard(id)?;
861
862                // If the shard is being managed by txn-wal (initially, tables), then we need to
863                // pass along the shard id for the txns shard to dataflow rendering.
864                let txns_shard = description
865                    .data_source
866                    .in_txns()
867                    .then(|| *self.txns_read.txns_id());
868
869                let metadata = CollectionMetadata {
870                    persist_location: self.persist_location.clone(),
871                    data_shard,
872                    relation_desc: description.desc.clone(),
873                    txns_shard,
874                };
875
876                Ok((id, description, metadata))
877            })
878            .collect_vec();
879
880        // So that we can open persist handles for each collections concurrently.
881        let persist_client = self
882            .persist
883            .open(self.persist_location.clone())
884            .await
885            .unwrap();
886        let persist_client = &persist_client;
887
888        // Reborrow the `&mut self` as immutable, as all the concurrent work to be processed in
889        // this stream cannot all have exclusive access.
890        use futures::stream::{StreamExt, TryStreamExt};
891        let this = &*self;
892        let mut to_register: Vec<_> = futures::stream::iter(enriched_with_metadata)
893            .map(|data: Result<_, StorageError>| {
894                async move {
895                    let (id, description, metadata) = data?;
896
897                    // should be replaced with real introspection (https://github.com/MaterializeInc/database-issues/issues/4078)
898                    // but for now, it's helpful to have this mapping written down somewhere
899                    debug!("mapping GlobalId={} to shard ({})", id, metadata.data_shard);
900
901                    // Tables are written through the txns table-write worker, which opens its
902                    // own write handles per registration, so opening one here would be pure
903                    // overhead (an extra persist open per table on the startup path). The
904                    // controller only needs a recent upper for them.
905                    let write = if matches!(description.data_source, DataSource::Table) {
906                        let diagnostics = Diagnostics {
907                            shard_name: id.to_string(),
908                            handle_purpose: format!("controller data for {}", id),
909                        };
910                        let upper = persist_client
911                            .recent_upper::<SourceData, (), Timestamp, StorageDiff>(
912                                metadata.data_shard,
913                                diagnostics,
914                            )
915                            .await
916                            .expect("invalid persist usage");
917                        WriteHandleOrUpper::Upper(upper)
918                    } else {
919                        let write = this
920                            .open_data_handles(
921                                &id,
922                                metadata.data_shard,
923                                metadata.relation_desc.clone(),
924                                persist_client,
925                            )
926                            .await;
927                        WriteHandleOrUpper::Handle(write)
928                    };
929
930                    Ok::<_, StorageError>((id, description, write, metadata))
931                }
932            })
933            // Poll each future for each collection concurrently, maximum of 50 at a time.
934            .buffer_unordered(50)
935            // HERE BE DRAGONS:
936            //
937            // There are at least 2 subtleties in using `FuturesUnordered` (which
938            // `buffer_unordered` uses underneath:
939            // - One is captured here <https://github.com/rust-lang/futures-rs/issues/2387>
940            // - And the other is deadlocking if processing an OUTPUT of a `FuturesUnordered`
941            // stream attempts to obtain an async mutex that is also obtained in the futures
942            // being polled.
943            //
944            // Both of these could potentially be issues in all usages of `buffer_unordered` in
945            // this method, so we stick the standard advice: only use `try_collect` or
946            // `collect`!
947            .try_collect()
948            .await?;
949
950        // The set of collections that we should render at the end of this
951        // function.
952        let mut to_execute = BTreeSet::new();
953        // New collections that are being created; this is distinct from the set
954        // of collections we plan to execute because
955        // `DataSource::IngestionExport` is added as a new collection, but is
956        // not executed directly.
957        let mut new_collections = BTreeSet::new();
958
959        // Reorder in dependency order.
960        to_register.sort_by_key(|(id, ..)| *id);
961
962        // Register tables first, but register them in reverse order since earlier tables
963        // can depend on later tables.
964        //
965        // Note: We could do more complex sorting to avoid the allocations, but IMO it's
966        // easier to reason about it this way.
967        let (tables_to_register, collections_to_register): (Vec<_>, Vec<_>) = to_register
968            .into_iter()
969            .partition(|(_id, desc, ..)| desc.data_source == DataSource::Table);
970        let to_register = tables_to_register
971            .into_iter()
972            .rev()
973            .chain(collections_to_register.into_iter());
974
975        // Statistics need a level of indirection so we can mutably borrow
976        // `self` when registering collections and when we are inserting
977        // statistics.
978        let mut new_webhook_statistic_entries = BTreeSet::new();
979
980        for (id, description, write, metadata) in to_register {
981            let is_in_txns = |id, metadata: &CollectionMetadata| {
982                metadata.txns_shard.is_some()
983                    && !(self.read_only && migrated_storage_collections.contains(&id))
984            };
985
986            to_execute.insert(id);
987            new_collections.insert(id);
988
989            let write_frontier = write.upper();
990
991            // Determine if this collection has another dependency.
992            let storage_dependencies = self.determine_collection_dependencies(id, &description)?;
993
994            let dependency_read_holds = self
995                .storage_collections
996                .acquire_read_holds(storage_dependencies)
997                .expect("can acquire read holds");
998
999            let mut dependency_since = Antichain::from_elem(Timestamp::MIN);
1000            for read_hold in dependency_read_holds.iter() {
1001                dependency_since.join_assign(read_hold.since());
1002            }
1003
1004            let data_source = description.data_source;
1005
1006            // Assert some invariants.
1007            //
1008            // TODO(alter_table): Include Tables (is_in_txns) in this check. After
1009            // supporting ALTER TABLE, it's now possible for a table to have a dependency
1010            // and thus run this check. But tables are managed by txn-wal and thus the
1011            // upper of the shard's `write_handle` generally isn't the logical upper of
1012            // the shard. Instead we need to thread through the upper of the `txn` shard
1013            // here so we can check this invariant.
1014            if !dependency_read_holds.is_empty()
1015                && !is_in_txns(id, &metadata)
1016                && !matches!(&data_source, DataSource::Sink { .. })
1017            {
1018                // As the halt message says, this can happen when trying to come
1019                // up in read-only mode and the current read-write
1020                // environmentd/controller deletes a collection. We exit
1021                // gracefully, which means we'll get restarted and get to try
1022                // again.
1023                //
1024                // We only do this when our own upper is not empty. An empty
1025                // upper means the collection is sealed and no-one can write to
1026                // it anymore, so an empty dependency since cannot hurt us. And
1027                // a sealed collection is durable state rather than a race, so
1028                // halting on it would wedge bootstrap forever instead of
1029                // resolving on the next attempt.
1030                if dependency_since.is_empty() && !write_frontier.is_empty() {
1031                    halt!(
1032                        "dependency since frontier is empty while dependent upper \
1033                        is not empty (dependent id={id}, write_frontier={:?}, dependency_read_holds={:?}), \
1034                        this indicates concurrent deletion of a collection",
1035                        write_frontier,
1036                        dependency_read_holds,
1037                    );
1038                }
1039
1040                // If the dependency is between a remap shard and a source (and
1041                // not a "primary" dependency), the dependency since cannot be
1042                // beyond the dependent (our) upper unless the collection is
1043                // new. If the since is allowed to "catch up" to the upper, that
1044                // is `upper <= since`, a restarting ingestion cannot
1045                // differentiate between updates that have already been written
1046                // out to the backing persist shard and updates that have yet to
1047                // be written. We would write duplicate updates.
1048                //
1049                // If this check fails, it means that the read hold installed on
1050                // the dependency was probably not upheld –– if it were, the
1051                // dependency's since could not have advanced as far the
1052                // dependent's upper.
1053                //
1054                // We don't care about the dependency since when the write
1055                // frontier is empty. In that case, no-one can write down any
1056                // more updates.
1057                if description.primary.is_none() {
1058                    mz_ore::soft_assert_or_log!(
1059                        write_frontier.elements() == &[Timestamp::MIN]
1060                            || write_frontier.is_empty()
1061                            || PartialOrder::less_than(&dependency_since, &write_frontier),
1062                        "dependency since has advanced past dependent ({id}) upper \n
1063                            dependent ({id}): upper {:?} \n
1064                            dependency since {:?} \n
1065                            dependency read holds: {:?}",
1066                        write_frontier,
1067                        dependency_since,
1068                        dependency_read_holds,
1069                    );
1070                }
1071            }
1072
1073            // Perform data source-specific setup.
1074            let mut extra_state = CollectionStateExtra::None;
1075            let mut maybe_instance_id = None;
1076            match &data_source {
1077                DataSource::Introspection(typ) => {
1078                    debug!(
1079                        ?data_source, meta = ?metadata,
1080                        "registering {id} with persist monotonic worker",
1081                    );
1082                    // We always register the collection with the collection manager,
1083                    // regardless of read-only mode. The CollectionManager itself is
1084                    // aware of read-only mode and will not attempt to write before told
1085                    // to do so.
1086                    //
1087                    self.register_introspection_collection(
1088                        id,
1089                        *typ,
1090                        write.expect_handle("introspection collections are not tables"),
1091                        persist_client.clone(),
1092                    )?;
1093                }
1094                DataSource::Webhook => {
1095                    debug!(
1096                        ?data_source, meta = ?metadata,
1097                        "registering {id} with persist monotonic worker",
1098                    );
1099                    // This collection of statistics is periodically aggregated into
1100                    // `source_statistics`.
1101                    new_webhook_statistic_entries.insert(id);
1102                    // Register the collection so our manager knows about it.
1103                    //
1104                    // NOTE: Maybe this shouldn't be in the collection manager,
1105                    // and collection manager should only be responsible for
1106                    // built-in introspection collections?
1107                    self.collection_manager.register_append_only_collection(
1108                        id,
1109                        write.expect_handle("webhook collections are not tables"),
1110                        false,
1111                        None,
1112                    );
1113                }
1114                DataSource::IngestionExport {
1115                    ingestion_id,
1116                    details,
1117                    data_config,
1118                } => {
1119                    debug!(
1120                        ?data_source, meta = ?metadata,
1121                        "not registering {id} with a controller persist worker",
1122                    );
1123                    // Adjust the source to contain this export.
1124                    let ingestion_state = self
1125                        .collections
1126                        .get_mut(ingestion_id)
1127                        .expect("known to exist");
1128
1129                    let instance_id = match &mut ingestion_state.data_source {
1130                        DataSource::Ingestion(ingestion_desc) => {
1131                            ingestion_desc.source_exports.insert(
1132                                id,
1133                                SourceExport {
1134                                    storage_metadata: (),
1135                                    details: details.clone(),
1136                                    data_config: data_config.clone(),
1137                                },
1138                            );
1139
1140                            // Record the ingestion's cluster ID for the
1141                            // ingestion export. This way we always have a
1142                            // record of it, even if the ingestion's collection
1143                            // description disappears.
1144                            ingestion_desc.instance_id
1145                        }
1146                        _ => unreachable!(
1147                            "SourceExport must only refer to primary sources that already exist"
1148                        ),
1149                    };
1150
1151                    // Executing the source export doesn't do anything, ensure we execute the source instead.
1152                    to_execute.remove(&id);
1153                    to_execute.insert(*ingestion_id);
1154
1155                    let ingestion_state = IngestionState {
1156                        read_capabilities: MutableAntichain::from(dependency_since.clone()),
1157                        dependency_read_holds,
1158                        derived_since: dependency_since,
1159                        write_frontier: Antichain::from_elem(Timestamp::MIN),
1160                        hold_policy: ReadPolicy::step_back(),
1161                        instance_id,
1162                        hydrated_on: BTreeSet::new(),
1163                    };
1164
1165                    extra_state = CollectionStateExtra::Ingestion(ingestion_state);
1166                    maybe_instance_id = Some(instance_id);
1167                }
1168                DataSource::Table => {
1169                    debug!(
1170                        ?data_source, meta = ?metadata,
1171                        "not registering {id} with the txns shard here; the caller does that \
1172                         through the group committer",
1173                    );
1174                }
1175                DataSource::Progress | DataSource::Other => {
1176                    debug!(
1177                        ?data_source, meta = ?metadata,
1178                        "not registering {id} with a controller persist worker",
1179                    );
1180                }
1181                DataSource::Ingestion(ingestion_desc) => {
1182                    debug!(
1183                        ?data_source, meta = ?metadata,
1184                        "not registering {id} with a controller persist worker",
1185                    );
1186
1187                    let mut dependency_since = Antichain::from_elem(Timestamp::MIN);
1188                    for read_hold in dependency_read_holds.iter() {
1189                        dependency_since.join_assign(read_hold.since());
1190                    }
1191
1192                    let ingestion_state = IngestionState {
1193                        read_capabilities: MutableAntichain::from(dependency_since.clone()),
1194                        dependency_read_holds,
1195                        derived_since: dependency_since,
1196                        write_frontier: Antichain::from_elem(Timestamp::MIN),
1197                        hold_policy: ReadPolicy::step_back(),
1198                        instance_id: ingestion_desc.instance_id,
1199                        hydrated_on: BTreeSet::new(),
1200                    };
1201
1202                    extra_state = CollectionStateExtra::Ingestion(ingestion_state);
1203                    maybe_instance_id = Some(ingestion_desc.instance_id);
1204                }
1205                DataSource::Sink { desc } => {
1206                    let mut dependency_since = Antichain::from_elem(Timestamp::MIN);
1207                    for read_hold in dependency_read_holds.iter() {
1208                        dependency_since.join_assign(read_hold.since());
1209                    }
1210
1211                    let [self_hold, read_hold] =
1212                        dependency_read_holds.try_into().expect("two holds");
1213
1214                    let state = ExportState::new(
1215                        desc.instance_id,
1216                        read_hold,
1217                        self_hold,
1218                        write_frontier.clone(),
1219                        ReadPolicy::step_back(),
1220                    );
1221                    maybe_instance_id = Some(state.cluster_id);
1222                    extra_state = CollectionStateExtra::Export(state);
1223                }
1224            }
1225
1226            let wallclock_lag_metrics = self.metrics.wallclock_lag_metrics(id, maybe_instance_id);
1227            let collection_state =
1228                CollectionState::new(data_source, metadata, extra_state, wallclock_lag_metrics);
1229
1230            self.collections.insert(id, collection_state);
1231        }
1232
1233        {
1234            let mut source_statistics = self.source_statistics.lock().expect("poisoned");
1235
1236            // Webhooks don't run on clusters/replicas, so we initialize their
1237            // statistics collection here.
1238            for id in new_webhook_statistic_entries {
1239                source_statistics.webhook_statistics.entry(id).or_default();
1240            }
1241
1242            // Sources and sinks only have statistics in the collection when
1243            // there is a replica that is reporting them. No need to initialize
1244            // here.
1245        }
1246
1247        self.append_shard_mappings(new_collections.into_iter(), Diff::ONE);
1248
1249        // TODO(guswynn): perform the io in this final section concurrently.
1250        for id in to_execute {
1251            match &self.collection(id)?.data_source {
1252                DataSource::Ingestion(ingestion) => {
1253                    if !self.read_only
1254                        || (ENABLE_0DT_DEPLOYMENT_SOURCES.get(self.config.config_set())
1255                            && ingestion.desc.connection.supports_read_only())
1256                    {
1257                        self.run_ingestion(id)?;
1258                    }
1259                }
1260                DataSource::IngestionExport { .. } => unreachable!(
1261                    "ingestion exports do not execute directly, but instead schedule their source to be re-executed"
1262                ),
1263                DataSource::Introspection(_)
1264                | DataSource::Webhook
1265                | DataSource::Table
1266                | DataSource::Progress
1267                | DataSource::Other => {}
1268                DataSource::Sink { .. } => {
1269                    if !self.read_only {
1270                        self.run_export(id)?;
1271                    }
1272                }
1273            };
1274        }
1275
1276        Ok(())
1277    }
1278
1279    fn check_alter_ingestion_source_desc(
1280        &mut self,
1281        ingestion_id: GlobalId,
1282        source_desc: &SourceDesc,
1283    ) -> Result<(), StorageError> {
1284        let source_collection = self.collection(ingestion_id)?;
1285        let data_source = &source_collection.data_source;
1286        match &data_source {
1287            DataSource::Ingestion(cur_ingestion) => {
1288                cur_ingestion
1289                    .desc
1290                    .alter_compatible(ingestion_id, source_desc)?;
1291            }
1292            o => {
1293                tracing::info!(
1294                    "{ingestion_id} inalterable because its data source is {:?} and not an ingestion",
1295                    o
1296                );
1297                Err(AlterError { id: ingestion_id })?
1298            }
1299        }
1300
1301        Ok(())
1302    }
1303
1304    async fn alter_ingestion_source_desc(
1305        &mut self,
1306        ingestion_ids: BTreeMap<GlobalId, SourceDesc>,
1307    ) -> Result<(), StorageError> {
1308        let mut ingestions_to_run = BTreeSet::new();
1309
1310        for (id, new_desc) in ingestion_ids {
1311            let collection = self
1312                .collections
1313                .get_mut(&id)
1314                .ok_or_else(|| StorageError::IdentifierMissing(id))?;
1315
1316            match &mut collection.data_source {
1317                DataSource::Ingestion(ingestion) => {
1318                    if ingestion.desc != new_desc {
1319                        tracing::info!(
1320                            from = ?ingestion.desc,
1321                            to = ?new_desc,
1322                            "alter_ingestion_source_desc, updating"
1323                        );
1324                        ingestion.desc = new_desc;
1325                        ingestions_to_run.insert(id);
1326                    }
1327                }
1328                o => {
1329                    tracing::warn!("alter_ingestion_source_desc called on {:?}", o);
1330                    Err(StorageError::IdentifierInvalid(id))?;
1331                }
1332            }
1333        }
1334
1335        for id in ingestions_to_run {
1336            self.run_ingestion(id)?;
1337        }
1338        Ok(())
1339    }
1340
1341    async fn alter_ingestion_connections(
1342        &mut self,
1343        source_connections: BTreeMap<GlobalId, GenericSourceConnection<InlinedConnection>>,
1344    ) -> Result<(), StorageError> {
1345        let mut ingestions_to_run = BTreeSet::new();
1346
1347        for (id, conn) in source_connections {
1348            let collection = self
1349                .collections
1350                .get_mut(&id)
1351                .ok_or_else(|| StorageError::IdentifierMissing(id))?;
1352
1353            match &mut collection.data_source {
1354                DataSource::Ingestion(ingestion) => {
1355                    // If the connection hasn't changed, there's no sense in
1356                    // re-rendering the dataflow.
1357                    if ingestion.desc.connection != conn {
1358                        tracing::info!(from = ?ingestion.desc.connection, to = ?conn, "alter_ingestion_connections, updating");
1359                        ingestion.desc.connection = conn;
1360                        ingestions_to_run.insert(id);
1361                    } else {
1362                        tracing::warn!(
1363                            "update_source_connection called on {id} but the \
1364                            connection was the same"
1365                        );
1366                    }
1367                }
1368                o => {
1369                    tracing::warn!("update_source_connection called on {:?}", o);
1370                    Err(StorageError::IdentifierInvalid(id))?;
1371                }
1372            }
1373        }
1374
1375        for id in ingestions_to_run {
1376            self.run_ingestion(id)?;
1377        }
1378        Ok(())
1379    }
1380
1381    async fn alter_ingestion_export_data_configs(
1382        &mut self,
1383        source_exports: BTreeMap<GlobalId, SourceExportDataConfig>,
1384    ) -> Result<(), StorageError> {
1385        let mut ingestions_to_run = BTreeSet::new();
1386
1387        for (source_export_id, new_data_config) in source_exports {
1388            // We need to adjust the data config on the CollectionState for
1389            // the source export collection directly
1390            let source_export_collection = self
1391                .collections
1392                .get_mut(&source_export_id)
1393                .ok_or_else(|| StorageError::IdentifierMissing(source_export_id))?;
1394            let ingestion_id = match &mut source_export_collection.data_source {
1395                DataSource::IngestionExport {
1396                    ingestion_id,
1397                    details: _,
1398                    data_config,
1399                } => {
1400                    *data_config = new_data_config.clone();
1401                    *ingestion_id
1402                }
1403                o => {
1404                    tracing::warn!("alter_ingestion_export_data_configs called on {:?}", o);
1405                    Err(StorageError::IdentifierInvalid(source_export_id))?
1406                }
1407            };
1408            // We also need to adjust the data config on the CollectionState of the
1409            // Ingestion that the export is associated with.
1410            let ingestion_collection = self
1411                .collections
1412                .get_mut(&ingestion_id)
1413                .ok_or_else(|| StorageError::IdentifierMissing(ingestion_id))?;
1414
1415            match &mut ingestion_collection.data_source {
1416                DataSource::Ingestion(ingestion_desc) => {
1417                    let source_export = ingestion_desc
1418                        .source_exports
1419                        .get_mut(&source_export_id)
1420                        .ok_or_else(|| StorageError::IdentifierMissing(source_export_id))?;
1421
1422                    // If the data config hasn't changed, there's no sense in
1423                    // re-rendering the dataflow.
1424                    if source_export.data_config != new_data_config {
1425                        tracing::info!(?source_export_id, from = ?source_export.data_config, to = ?new_data_config, "alter_ingestion_export_data_configs, updating");
1426                        source_export.data_config = new_data_config;
1427
1428                        ingestions_to_run.insert(ingestion_id);
1429                    } else {
1430                        tracing::warn!(
1431                            "alter_ingestion_export_data_configs called on \
1432                                    export {source_export_id} of {ingestion_id} but \
1433                                    the data config was the same"
1434                        );
1435                    }
1436                }
1437                o => {
1438                    tracing::warn!("alter_ingestion_export_data_configs called on {:?}", o);
1439                    Err(StorageError::IdentifierInvalid(ingestion_id))?
1440                }
1441            }
1442        }
1443
1444        for id in ingestions_to_run {
1445            self.run_ingestion(id)?;
1446        }
1447        Ok(())
1448    }
1449
1450    async fn alter_table_desc(
1451        &mut self,
1452        existing_collection: GlobalId,
1453        new_collection: GlobalId,
1454        new_desc: RelationDesc,
1455        expected_version: RelationVersion,
1456    ) -> Result<(), StorageError> {
1457        let data_shard = {
1458            let Controller {
1459                collections,
1460                storage_collections,
1461                ..
1462            } = self;
1463
1464            let existing = collections
1465                .get(&existing_collection)
1466                .ok_or(StorageError::IdentifierMissing(existing_collection))?;
1467            if existing.data_source != DataSource::Table {
1468                return Err(StorageError::IdentifierInvalid(existing_collection));
1469            }
1470
1471            // Let StorageCollections know!
1472            storage_collections
1473                .alter_table_desc(
1474                    existing_collection,
1475                    new_collection,
1476                    new_desc.clone(),
1477                    expected_version,
1478                )
1479                .await?;
1480
1481            existing.collection_metadata.data_shard.clone()
1482        };
1483
1484        let collection_meta = CollectionMetadata {
1485            persist_location: self.persist_location.clone(),
1486            data_shard,
1487            relation_desc: new_desc.clone(),
1488            // TODO(alter_table): Support schema evolution on sources.
1489            txns_shard: Some(self.txns_read.txns_id().clone()),
1490        };
1491        // TODO(alter_table): Support schema evolution on sources.
1492        let wallclock_lag_metrics = self.metrics.wallclock_lag_metrics(new_collection, None);
1493        let collection_state = CollectionState::new(
1494            DataSource::Table,
1495            collection_meta,
1496            CollectionStateExtra::None,
1497            wallclock_lag_metrics,
1498        );
1499
1500        // Great! We have successfully evolved the schema of our Table, now we need to update our
1501        // in-memory data structures.
1502        self.collections.insert(new_collection, collection_state);
1503
1504        self.append_shard_mappings([new_collection].into_iter(), Diff::ONE);
1505
1506        Ok(())
1507    }
1508
1509    async fn register_table_collections(
1510        &mut self,
1511        register_ts: Timestamp,
1512        ids: Vec<GlobalId>,
1513    ) -> Result<(), StorageError> {
1514        let mut tables = self.table_registrations(ids)?;
1515
1516        // A read-only deployment only writes its migrated builtin tables.
1517        if self.read_only {
1518            tables.retain(|table| self.migrated_storage_collections.contains(&table.id));
1519        }
1520        if tables.is_empty() {
1521            return Ok(());
1522        }
1523
1524        match self
1525            .persist_table_worker
1526            .register(register_ts, tables)
1527            .await
1528        {
1529            Ok(res) => res,
1530            Err(_recv) => Err(StorageError::ShuttingDown("persist_table_worker")),
1531        }
1532    }
1533
1534    fn table_registrations(
1535        &self,
1536        ids: Vec<GlobalId>,
1537    ) -> Result<Vec<TableRegistration>, StorageError> {
1538        // The storage data source decides which table catalog items use txn-wal.
1539        let mut tables = Vec::with_capacity(ids.len());
1540        for id in ids {
1541            let collection = self.collection(id)?;
1542            if matches!(collection.data_source, DataSource::Table) {
1543                let metadata = &collection.collection_metadata;
1544                tables.push(TableRegistration {
1545                    id,
1546                    data_shard: metadata.data_shard,
1547                    relation_desc: metadata.relation_desc.clone(),
1548                });
1549            }
1550        }
1551        Ok(tables)
1552    }
1553
1554    fn txns_table_ids(&self, ids: Vec<GlobalId>) -> Result<Vec<GlobalId>, StorageError> {
1555        let mut tables = Vec::with_capacity(ids.len());
1556        for id in ids {
1557            let collection = self.collection(id)?;
1558            if matches!(collection.data_source, DataSource::Table) {
1559                tables.push(id);
1560            }
1561        }
1562        Ok(tables)
1563    }
1564
1565    fn export(&self, id: GlobalId) -> Result<&ExportState, StorageError> {
1566        self.collections
1567            .get(&id)
1568            .and_then(|c| match &c.extra_state {
1569                CollectionStateExtra::Export(state) => Some(state),
1570                _ => None,
1571            })
1572            .ok_or(StorageError::IdentifierMissing(id))
1573    }
1574
1575    fn export_mut(&mut self, id: GlobalId) -> Result<&mut ExportState, StorageError> {
1576        self.collections
1577            .get_mut(&id)
1578            .and_then(|c| match &mut c.extra_state {
1579                CollectionStateExtra::Export(state) => Some(state),
1580                _ => None,
1581            })
1582            .ok_or(StorageError::IdentifierMissing(id))
1583    }
1584
1585    /// Create a oneshot ingestion.
1586    async fn create_oneshot_ingestion(
1587        &mut self,
1588        ingestion_id: uuid::Uuid,
1589        collection_id: GlobalId,
1590        instance_id: StorageInstanceId,
1591        request: OneshotIngestionRequest,
1592        result_tx: OneshotResultCallback<ProtoBatch>,
1593    ) -> Result<(), StorageError> {
1594        let collection_meta = self
1595            .collections
1596            .get(&collection_id)
1597            .ok_or_else(|| StorageError::IdentifierMissing(collection_id))?
1598            .collection_metadata
1599            .clone();
1600        let instance = self.instances.get_mut(&instance_id).ok_or_else(|| {
1601            // TODO(cf2): Refine this error.
1602            StorageError::Generic(anyhow::anyhow!("missing cluster {instance_id}"))
1603        })?;
1604        let oneshot_cmd = RunOneshotIngestion {
1605            ingestion_id,
1606            collection_id,
1607            collection_meta,
1608            request,
1609        };
1610
1611        if !self.read_only {
1612            instance.send(StorageCommand::RunOneshotIngestion(Box::new(oneshot_cmd)));
1613            let pending = PendingOneshotIngestion {
1614                result_tx,
1615                cluster_id: instance_id,
1616            };
1617            let novel = self
1618                .pending_oneshot_ingestions
1619                .insert(ingestion_id, pending);
1620            assert_none!(novel);
1621            Ok(())
1622        } else {
1623            Err(StorageError::ReadOnly)
1624        }
1625    }
1626
1627    fn cancel_oneshot_ingestion(&mut self, ingestion_id: uuid::Uuid) -> Result<(), StorageError> {
1628        if self.read_only {
1629            return Err(StorageError::ReadOnly);
1630        }
1631
1632        let pending = self
1633            .pending_oneshot_ingestions
1634            .remove(&ingestion_id)
1635            .ok_or_else(|| {
1636                // TODO(cf2): Refine this error.
1637                StorageError::Generic(anyhow::anyhow!("missing oneshot ingestion {ingestion_id}"))
1638            })?;
1639
1640        match self.instances.get_mut(&pending.cluster_id) {
1641            Some(instance) => {
1642                instance.send(StorageCommand::CancelOneshotIngestion(ingestion_id));
1643            }
1644            None => {
1645                mz_ore::soft_panic_or_log!(
1646                    "canceling oneshot ingestion on non-existent cluster, ingestion {:?}, instance {}",
1647                    ingestion_id,
1648                    pending.cluster_id,
1649                );
1650            }
1651        }
1652        // Respond to the user that the request has been canceled.
1653        pending.cancel();
1654
1655        Ok(())
1656    }
1657
1658    async fn alter_export(
1659        &mut self,
1660        id: GlobalId,
1661        new_description: ExportDescription,
1662    ) -> Result<(), StorageError> {
1663        let from_id = new_description.sink.from;
1664
1665        // Acquire read holds at StorageCollections to ensure that the
1666        // sinked collection is not dropped while we're sinking it.
1667        let desired_read_holds = vec![from_id.clone(), id.clone()];
1668        let [input_hold, self_hold] = self
1669            .storage_collections
1670            .acquire_read_holds(desired_read_holds)
1671            .expect("missing dependency")
1672            .try_into()
1673            .expect("expected number of holds");
1674        let from_storage_metadata = self.storage_collections.collection_metadata(from_id)?;
1675        let to_storage_metadata = self.storage_collections.collection_metadata(id)?;
1676
1677        // Check whether the sink's write frontier is beyond the read hold we got
1678        let cur_export = self.export_mut(id)?;
1679        let input_readable = cur_export
1680            .write_frontier
1681            .iter()
1682            .all(|t| input_hold.since().less_than(t));
1683        if !input_readable {
1684            return Err(StorageError::ReadBeforeSince(from_id));
1685        }
1686
1687        let new_export = ExportState {
1688            read_capabilities: cur_export.read_capabilities.clone(),
1689            cluster_id: new_description.instance_id,
1690            derived_since: cur_export.derived_since.clone(),
1691            read_holds: [input_hold, self_hold],
1692            read_policy: cur_export.read_policy.clone(),
1693            write_frontier: cur_export.write_frontier.clone(),
1694        };
1695        *cur_export = new_export;
1696
1697        // For `ALTER SINK`, the snapshot should only occur if the sink has not made any progress.
1698        // This prevents unnecessary decoding in the sink.
1699        // If the write frontier of the sink is strictly larger than its read hold, it must have at
1700        // least written out its snapshot, and we can skip reading it; otherwise assume we may have
1701        // to replay from the beginning.
1702        // TODO(SS-513): unify this with run_export, if possible
1703        let with_snapshot = new_description.sink.with_snapshot
1704            && !PartialOrder::less_than(&new_description.sink.as_of, &cur_export.write_frontier);
1705
1706        let cmd = RunSinkCommand {
1707            id,
1708            description: StorageSinkDesc {
1709                from: from_id,
1710                from_desc: new_description.sink.from_desc.clone(),
1711                connection: new_description.sink.connection.clone(),
1712                envelope: new_description.sink.envelope,
1713                as_of: new_description.sink.as_of.clone(),
1714                version: new_description.sink.version,
1715                from_storage_metadata,
1716                with_snapshot,
1717                to_storage_metadata,
1718                commit_interval: new_description.sink.commit_interval,
1719            },
1720        };
1721
1722        // Fetch the client for this export's cluster.
1723        let instance = self
1724            .instances
1725            .get_mut(&new_description.instance_id)
1726            .ok_or_else(|| StorageError::ExportInstanceMissing {
1727                storage_instance_id: new_description.instance_id,
1728                export_id: id,
1729            })?;
1730
1731        instance.send(StorageCommand::RunSink(Box::new(cmd)));
1732
1733        // alter_export_connections rebuilds the sink from this description and
1734        // checks new connections against it with alter_compatible, so it must
1735        // describe the sink we just told the cluster to run.
1736        let state = self
1737            .collections
1738            .get_mut(&id)
1739            .expect("export known to exist");
1740        let DataSource::Sink { desc } = &mut state.data_source else {
1741            panic!("export known to exist")
1742        };
1743        *desc = new_description;
1744
1745        Ok(())
1746    }
1747
1748    /// Create the sinks described by the `ExportDescription`.
1749    async fn alter_export_connections(
1750        &mut self,
1751        exports: BTreeMap<GlobalId, StorageSinkConnection>,
1752    ) -> Result<(), StorageError> {
1753        let mut updates_by_instance =
1754            BTreeMap::<StorageInstanceId, Vec<(RunSinkCommand, ExportDescription)>>::new();
1755
1756        for (id, connection) in exports {
1757            // We stage changes in new_export_description and then apply all
1758            // updates to exports at the end.
1759            //
1760            // We don't just go ahead and clone the `ExportState` itself and
1761            // update that because `ExportState` is not clone, because it holds
1762            // a `ReadHandle` and cloning that would cause additional work for
1763            // whoever guarantees those read holds.
1764            let (mut new_export_description, as_of): (ExportDescription, _) = {
1765                let export = &self.collections[&id];
1766                let DataSource::Sink { desc } = &export.data_source else {
1767                    panic!("export exists")
1768                };
1769                let CollectionStateExtra::Export(state) = &export.extra_state else {
1770                    panic!("export exists")
1771                };
1772                let export_description = desc.clone();
1773                let as_of = state.input_hold().since().clone();
1774
1775                (export_description, as_of)
1776            };
1777            let current_sink = new_export_description.sink.clone();
1778
1779            new_export_description.sink.connection = connection;
1780
1781            // Ensure compatibility
1782            current_sink.alter_compatible(id, &new_export_description.sink)?;
1783
1784            let from_storage_metadata = self
1785                .storage_collections
1786                .collection_metadata(new_export_description.sink.from)?;
1787            let to_storage_metadata = self.storage_collections.collection_metadata(id)?;
1788
1789            let cmd = RunSinkCommand {
1790                id,
1791                description: StorageSinkDesc {
1792                    from: new_export_description.sink.from,
1793                    from_desc: new_export_description.sink.from_desc.clone(),
1794                    connection: new_export_description.sink.connection.clone(),
1795                    envelope: new_export_description.sink.envelope,
1796                    with_snapshot: new_export_description.sink.with_snapshot,
1797                    version: new_export_description.sink.version,
1798                    // Here we are about to send a RunSinkCommand with the current read capaibility
1799                    // held by this sink. However, clusters are already running a version of the
1800                    // sink and nothing guarantees that by the time this command arrives at the
1801                    // clusters they won't have made additional progress such that this read
1802                    // capability is invalidated.
1803                    // The solution to this problem is for the controller to track specific
1804                    // executions of dataflows such that it can track the shutdown of the current
1805                    // instance and the initialization of the new instance separately and ensure
1806                    // read holds are held for the correct amount of time.
1807                    // TODO(petrosagg): change the controller to explicitly track dataflow executions
1808                    as_of: as_of.to_owned(),
1809                    from_storage_metadata,
1810                    to_storage_metadata,
1811                    commit_interval: new_export_description.sink.commit_interval,
1812                },
1813            };
1814
1815            let update = updates_by_instance
1816                .entry(new_export_description.instance_id)
1817                .or_default();
1818            update.push((cmd, new_export_description));
1819        }
1820
1821        for (instance_id, updates) in updates_by_instance {
1822            let mut export_updates = BTreeMap::new();
1823            let mut cmds = Vec::with_capacity(updates.len());
1824
1825            for (cmd, export_state) in updates {
1826                export_updates.insert(cmd.id, export_state);
1827                cmds.push(cmd);
1828            }
1829
1830            // Fetch the client for this exports's cluster.
1831            let instance = self.instances.get_mut(&instance_id).ok_or_else(|| {
1832                StorageError::ExportInstanceMissing {
1833                    storage_instance_id: instance_id,
1834                    export_id: *export_updates
1835                        .keys()
1836                        .next()
1837                        .expect("set of exports not empty"),
1838                }
1839            })?;
1840
1841            for cmd in cmds {
1842                instance.send(StorageCommand::RunSink(Box::new(cmd)));
1843            }
1844
1845            // Update state only after all possible errors have occurred.
1846            for (id, new_export_description) in export_updates {
1847                let Some(state) = self.collections.get_mut(&id) else {
1848                    panic!("export known to exist")
1849                };
1850                let DataSource::Sink { desc } = &mut state.data_source else {
1851                    panic!("export known to exist")
1852                };
1853                *desc = new_export_description;
1854            }
1855        }
1856
1857        Ok(())
1858    }
1859
1860    fn drop_tables(
1861        &mut self,
1862        storage_metadata: &StorageMetadata,
1863        identifiers: Vec<GlobalId>,
1864    ) -> Result<(), StorageError> {
1865        let (table_write_ids, data_source_ids): (Vec<_>, Vec<_>) = identifiers
1866            .into_iter()
1867            .partition(|id| match self.collections[id].data_source {
1868                DataSource::Table => true,
1869                DataSource::IngestionExport { .. } | DataSource::Webhook => false,
1870                _ => panic!("identifier is not a table: {}", id),
1871            });
1872
1873        if table_write_ids.len() > 0 {
1874            let tx = self.pending_table_handle_drops_tx.clone();
1875            for identifier in table_write_ids {
1876                let _ = tx.send(identifier);
1877            }
1878        }
1879
1880        if data_source_ids.len() > 0 {
1881            self.validate_collection_ids(data_source_ids.iter().cloned())?;
1882            self.drop_sources_unvalidated(storage_metadata, data_source_ids)?;
1883        }
1884
1885        Ok(())
1886    }
1887
1888    fn drop_sources(
1889        &mut self,
1890        storage_metadata: &StorageMetadata,
1891        identifiers: Vec<GlobalId>,
1892    ) -> Result<(), StorageError> {
1893        self.validate_collection_ids(identifiers.iter().cloned())?;
1894        self.drop_sources_unvalidated(storage_metadata, identifiers)
1895    }
1896
1897    fn drop_sources_unvalidated(
1898        &mut self,
1899        storage_metadata: &StorageMetadata,
1900        ids: Vec<GlobalId>,
1901    ) -> Result<(), StorageError> {
1902        // Keep track of which ingestions we have to execute still, and which we
1903        // have to change because of dropped subsources.
1904        let mut ingestions_to_execute = BTreeSet::new();
1905        let mut ingestions_to_drop = BTreeSet::new();
1906        let mut source_statistics_to_drop = Vec::new();
1907
1908        // Ingestions (and their exports) are also collections, but we keep
1909        // track of non-ingestion collections separately, because they have
1910        // slightly different cleanup logic below.
1911        let mut collections_to_drop = Vec::new();
1912
1913        for id in ids.iter() {
1914            let collection_state = self.collections.get(id);
1915
1916            if let Some(collection_state) = collection_state {
1917                match collection_state.data_source {
1918                    DataSource::Webhook => {
1919                        // TODO(parkmycar): The Collection Manager and PersistMonotonicWriter
1920                        // could probably use some love and maybe get merged together?
1921                        let fut = self.collection_manager.unregister_collection(*id);
1922                        mz_ore::task::spawn(|| format!("storage-webhook-cleanup-{id}"), fut);
1923
1924                        collections_to_drop.push(*id);
1925                        source_statistics_to_drop.push(*id);
1926                    }
1927                    DataSource::Ingestion(_) => {
1928                        ingestions_to_drop.insert(*id);
1929                        source_statistics_to_drop.push(*id);
1930                    }
1931                    DataSource::IngestionExport { ingestion_id, .. } => {
1932                        // If we are dropping source exports, we need to modify the
1933                        // ingestion that it runs on.
1934                        //
1935                        // If we remove this export, we need to stop producing data to
1936                        // it, so plan to re-execute the ingestion with the amended
1937                        // description.
1938                        ingestions_to_execute.insert(ingestion_id);
1939
1940                        // Adjust the source to remove this export.
1941                        let ingestion_state = match self.collections.get_mut(&ingestion_id) {
1942                            Some(ingestion_collection) => ingestion_collection,
1943                            // Primary ingestion already dropped.
1944                            None => {
1945                                tracing::error!(
1946                                    "primary source {ingestion_id} seemingly dropped before subsource {id}"
1947                                );
1948                                continue;
1949                            }
1950                        };
1951
1952                        match &mut ingestion_state.data_source {
1953                            DataSource::Ingestion(ingestion_desc) => {
1954                                let removed = ingestion_desc.source_exports.remove(id);
1955                                mz_ore::soft_assert_or_log!(
1956                                    removed.is_some(),
1957                                    "dropped subsource {id} already removed from source exports"
1958                                );
1959                            }
1960                            _ => unreachable!(
1961                                "SourceExport must only refer to primary sources that already exist"
1962                            ),
1963                        };
1964
1965                        // Ingestion exports also have ReadHolds that we need to
1966                        // downgrade, and much of their drop machinery is the
1967                        // same as for the "main" ingestion.
1968                        ingestions_to_drop.insert(*id);
1969                        source_statistics_to_drop.push(*id);
1970                    }
1971                    DataSource::Progress | DataSource::Table | DataSource::Other => {
1972                        collections_to_drop.push(*id);
1973                    }
1974                    DataSource::Introspection(_) | DataSource::Sink { .. } => {
1975                        // Collections of these types are either not sources and should be dropped
1976                        // through other means, or are sources but should never be dropped.
1977                        soft_panic_or_log!(
1978                            "drop_sources called on a {:?} (id={id}))",
1979                            collection_state.data_source,
1980                        );
1981                    }
1982                }
1983            }
1984        }
1985
1986        // Do not bother re-executing ingestions we know we plan to drop.
1987        ingestions_to_execute.retain(|id| !ingestions_to_drop.contains(id));
1988        for ingestion_id in ingestions_to_execute {
1989            self.run_ingestion(ingestion_id)?;
1990        }
1991
1992        // For ingestions, we fabricate a new hold that will propagate through
1993        // the cluster and then back to us.
1994
1995        // We don't explicitly remove read capabilities! Downgrading the
1996        // frontier of the source to `[]` (the empty Antichain), will propagate
1997        // to the storage dependencies.
1998        let ingestion_policies = ingestions_to_drop
1999            .iter()
2000            .map(|id| (*id, ReadPolicy::ValidFrom(Antichain::new())))
2001            .collect();
2002
2003        tracing::debug!(
2004            ?ingestion_policies,
2005            "dropping sources by setting read hold policies"
2006        );
2007        self.set_hold_policies(ingestion_policies);
2008
2009        // Delete all collection->shard mappings
2010        let shards_to_update: BTreeSet<_> = ingestions_to_drop
2011            .iter()
2012            .chain(collections_to_drop.iter())
2013            .cloned()
2014            .collect();
2015        self.append_shard_mappings(shards_to_update.into_iter(), Diff::MINUS_ONE);
2016
2017        let status_now = mz_ore::now::to_datetime((self.now)());
2018        let mut status_updates = vec![];
2019        for id in ingestions_to_drop.iter() {
2020            status_updates.push(StatusUpdate::new(*id, status_now, Status::Dropped));
2021        }
2022
2023        if !self.read_only {
2024            self.append_status_introspection_updates(
2025                IntrospectionType::SourceStatusHistory,
2026                status_updates,
2027            );
2028        }
2029
2030        {
2031            let mut source_statistics = self.source_statistics.lock().expect("poisoned");
2032            for id in source_statistics_to_drop {
2033                source_statistics
2034                    .source_statistics
2035                    .retain(|(stats_id, _), _| stats_id != &id);
2036                source_statistics
2037                    .webhook_statistics
2038                    .retain(|stats_id, _| stats_id != &id);
2039            }
2040        }
2041
2042        // Remove collection state
2043        for id in ingestions_to_drop.iter().chain(collections_to_drop.iter()) {
2044            tracing::info!(%id, "dropping collection state");
2045            let collection = self
2046                .collections
2047                .remove(id)
2048                .expect("list populated after checking that self.collections contains it");
2049
2050            let instance = match &collection.extra_state {
2051                CollectionStateExtra::Ingestion(ingestion) => Some(ingestion.instance_id),
2052                CollectionStateExtra::Export(export) => Some(export.cluster_id()),
2053                CollectionStateExtra::None => None,
2054            }
2055            .and_then(|i| self.instances.get(&i));
2056
2057            // Record which replicas were running a collection, so that we can
2058            // match DroppedId messages against them and eventually remove state
2059            // from self.dropped_objects
2060            if let Some(instance) = instance {
2061                let active_replicas = instance.get_active_replicas_for_object(id);
2062                if !active_replicas.is_empty() {
2063                    // The remap collection of an ingestion doesn't have extra
2064                    // state and doesn't have an instance_id, but we still get
2065                    // upper updates for them, so want to make sure to populate
2066                    // dropped_ids.
2067                    // TODO(aljoscha): All this is a bit icky. But here we are
2068                    // for now...
2069                    match &collection.data_source {
2070                        DataSource::Ingestion(ingestion_desc) => {
2071                            if *id != ingestion_desc.remap_collection_id {
2072                                self.dropped_objects.insert(
2073                                    ingestion_desc.remap_collection_id,
2074                                    active_replicas.clone(),
2075                                );
2076                            }
2077                        }
2078                        _ => {}
2079                    }
2080
2081                    self.dropped_objects.insert(*id, active_replicas);
2082                }
2083            }
2084        }
2085
2086        // Also let StorageCollections know!
2087        self.storage_collections
2088            .drop_collections_unvalidated(storage_metadata, ids);
2089
2090        Ok(())
2091    }
2092
2093    /// Drops the read capability for the sinks and allows their resources to be reclaimed.
2094    fn drop_sinks(
2095        &mut self,
2096        storage_metadata: &StorageMetadata,
2097        identifiers: Vec<GlobalId>,
2098    ) -> Result<(), StorageError> {
2099        self.validate_export_ids(identifiers.iter().cloned())?;
2100        self.drop_sinks_unvalidated(storage_metadata, identifiers);
2101        Ok(())
2102    }
2103
2104    fn drop_sinks_unvalidated(
2105        &mut self,
2106        storage_metadata: &StorageMetadata,
2107        mut sinks_to_drop: Vec<GlobalId>,
2108    ) {
2109        // Ignore exports that have already been removed.
2110        sinks_to_drop.retain(|id| self.export(*id).is_ok());
2111
2112        // TODO: ideally we'd advance the write frontier ourselves here, but this function's
2113        // not yet marked async.
2114
2115        // We don't explicitly remove read capabilities! Downgrading the
2116        // frontier of the source to `[]` (the empty Antichain), will propagate
2117        // to the storage dependencies.
2118        let drop_policy = sinks_to_drop
2119            .iter()
2120            .map(|id| (*id, ReadPolicy::ValidFrom(Antichain::new())))
2121            .collect();
2122
2123        tracing::debug!(
2124            ?drop_policy,
2125            "dropping sources by setting read hold policies"
2126        );
2127        self.set_hold_policies(drop_policy);
2128
2129        // Record the drop status for all sink drops.
2130        //
2131        // We also delete the items' statistics objects.
2132        //
2133        // The locks are held for a short time, only while we do some removals from a map.
2134
2135        let status_now = mz_ore::now::to_datetime((self.now)());
2136
2137        // Record the drop status for all pending sink drops.
2138        let mut status_updates = vec![];
2139        {
2140            let mut sink_statistics = self.sink_statistics.lock().expect("poisoned");
2141            for id in sinks_to_drop.iter() {
2142                status_updates.push(StatusUpdate::new(*id, status_now, Status::Dropped));
2143                sink_statistics.retain(|(stats_id, _), _| stats_id != id);
2144            }
2145        }
2146
2147        if !self.read_only {
2148            self.append_status_introspection_updates(
2149                IntrospectionType::SinkStatusHistory,
2150                status_updates,
2151            );
2152        }
2153
2154        // Remove collection/export state
2155        for id in sinks_to_drop.iter() {
2156            tracing::info!(%id, "dropping export state");
2157            let collection = self
2158                .collections
2159                .remove(id)
2160                .expect("list populated after checking that self.collections contains it");
2161
2162            let instance = match &collection.extra_state {
2163                CollectionStateExtra::Ingestion(ingestion) => Some(ingestion.instance_id),
2164                CollectionStateExtra::Export(export) => Some(export.cluster_id()),
2165                CollectionStateExtra::None => None,
2166            }
2167            .and_then(|i| self.instances.get(&i));
2168
2169            // Record how many replicas were running an export, so that we can
2170            // match `DroppedId` messages against it and eventually remove state
2171            // from `self.dropped_objects`.
2172            if let Some(instance) = instance {
2173                let active_replicas = instance.get_active_replicas_for_object(id);
2174                if !active_replicas.is_empty() {
2175                    self.dropped_objects.insert(*id, active_replicas);
2176                }
2177            }
2178        }
2179
2180        // Also let StorageCollections know!
2181        self.storage_collections
2182            .drop_collections_unvalidated(storage_metadata, sinks_to_drop);
2183    }
2184
2185    #[instrument(level = "debug")]
2186    fn append_table(
2187        &mut self,
2188        write_ts: Timestamp,
2189        advance_to: Timestamp,
2190        commands: Vec<(GlobalId, Vec<TableData>)>,
2191    ) -> Result<tokio::sync::oneshot::Receiver<Result<(), StorageError>>, StorageError> {
2192        if self.read_only {
2193            // While in read only mode, ONLY collections that have been migrated
2194            // and need to be re-hydrated in read only mode can be written to.
2195            if !commands
2196                .iter()
2197                .all(|(id, _)| id.is_system() && self.migrated_storage_collections.contains(id))
2198            {
2199                return Err(StorageError::ReadOnly);
2200            }
2201        }
2202
2203        // TODO(petrosagg): validate appends against the expected RelationDesc of the collection
2204        for (id, updates) in commands.iter() {
2205            if !updates.is_empty() {
2206                if !write_ts.less_than(&advance_to) {
2207                    return Err(StorageError::UpdateBeyondUpper(*id));
2208                }
2209            }
2210        }
2211
2212        Ok(self
2213            .persist_table_worker
2214            .append(write_ts, advance_to, commands))
2215    }
2216
2217    fn table_write_handle(&self) -> Arc<dyn mz_storage_client::controller::TableWriteHandle> {
2218        Arc::new(persist_handles::TableWriteWorkerHandle(
2219            self.persist_table_worker.clone(),
2220        ))
2221    }
2222
2223    fn monotonic_appender(&self, id: GlobalId) -> Result<MonotonicAppender, StorageError> {
2224        self.collection_manager.monotonic_appender(id)
2225    }
2226
2227    fn webhook_statistics(&self, id: GlobalId) -> Result<Arc<WebhookStatistics>, StorageError> {
2228        // Call to this method are usually cached so the lock is not in the critical path.
2229        let source_statistics = self.source_statistics.lock().expect("poisoned");
2230        source_statistics
2231            .webhook_statistics
2232            .get(&id)
2233            .cloned()
2234            .ok_or(StorageError::IdentifierMissing(id))
2235    }
2236
2237    async fn ready(&mut self) {
2238        if self.maintenance_scheduled {
2239            return;
2240        }
2241
2242        if !self.pending_table_handle_drops_rx.is_empty() {
2243            return;
2244        }
2245
2246        tokio::select! {
2247            Some(m) = self.instance_response_rx.recv() => {
2248                self.stashed_responses.push(m);
2249                while let Ok(m) = self.instance_response_rx.try_recv() {
2250                    self.stashed_responses.push(m);
2251                }
2252            }
2253            _ = self.maintenance_ticker.tick() => {
2254                self.maintenance_scheduled = true;
2255            },
2256        };
2257    }
2258
2259    #[instrument(level = "debug")]
2260    fn process(
2261        &mut self,
2262        storage_metadata: &StorageMetadata,
2263    ) -> Result<Option<Response>, anyhow::Error> {
2264        // Perform periodic maintenance work.
2265        if self.maintenance_scheduled {
2266            self.maintain();
2267            self.maintenance_scheduled = false;
2268        }
2269
2270        for instance in self.instances.values_mut() {
2271            instance.rehydrate_failed_replicas();
2272        }
2273
2274        let mut status_updates = vec![];
2275        let mut updated_frontiers = BTreeMap::new();
2276
2277        // Take the currently stashed responses so that we can call mut receiver functions in the loop.
2278        let stashed_responses = std::mem::take(&mut self.stashed_responses);
2279        for resp in stashed_responses {
2280            match resp {
2281                (_replica_id, StorageResponse::FrontierUpper(id, upper)) => {
2282                    self.update_write_frontier(id, &upper);
2283                    updated_frontiers.insert(id, upper);
2284                }
2285                (replica_id, StorageResponse::DroppedId(id)) => {
2286                    let replica_id = replica_id.expect("DroppedId from unknown replica");
2287                    if let Some(remaining_replicas) = self.dropped_objects.get_mut(&id) {
2288                        remaining_replicas.remove(&replica_id);
2289                        if remaining_replicas.is_empty() {
2290                            self.dropped_objects.remove(&id);
2291                        }
2292                    } else {
2293                        soft_panic_or_log!("unexpected DroppedId for {id}");
2294                    }
2295                }
2296                (replica_id, StorageResponse::StatisticsUpdates(source_stats, sink_stats)) => {
2297                    // Note we only hold the locks while moving some plain-old-data around here.
2298                    {
2299                        // NOTE(aljoscha): We explicitly unwrap the `Option`,
2300                        // because we expect that stats coming from replicas
2301                        // have a replica id.
2302                        //
2303                        // If this is `None` and we would use that to access
2304                        // state below we might clobber something unexpectedly.
2305                        let replica_id = if let Some(replica_id) = replica_id {
2306                            replica_id
2307                        } else {
2308                            tracing::error!(
2309                                ?source_stats,
2310                                "missing replica_id for source statistics update"
2311                            );
2312                            continue;
2313                        };
2314
2315                        let mut shared_stats = self.source_statistics.lock().expect("poisoned");
2316
2317                        for stat in source_stats {
2318                            let collection_id = stat.id.clone();
2319
2320                            if self.collection(collection_id).is_err() {
2321                                // We can get updates for collections that have
2322                                // already been deleted, ignore those.
2323                                continue;
2324                            }
2325
2326                            let entry = shared_stats
2327                                .source_statistics
2328                                .entry((stat.id, Some(replica_id)));
2329
2330                            match entry {
2331                                btree_map::Entry::Vacant(vacant_entry) => {
2332                                    let mut stats = ControllerSourceStatistics::new(
2333                                        collection_id,
2334                                        Some(replica_id),
2335                                    );
2336                                    stats.incorporate(stat);
2337                                    vacant_entry.insert(stats);
2338                                }
2339                                btree_map::Entry::Occupied(mut occupied_entry) => {
2340                                    occupied_entry.get_mut().incorporate(stat);
2341                                }
2342                            }
2343                        }
2344                    }
2345
2346                    {
2347                        // NOTE(aljoscha); Same as above. We want to be
2348                        // explicit.
2349                        //
2350                        // Also, technically for sinks there is no webhook
2351                        // "sources" that would force us to use an `Option`. But
2352                        // we still have to use an option for other reasons: the
2353                        // scraper expects a trait for working with stats, and
2354                        // that in the end forces the sink stats map to also
2355                        // have `Option` in the key. Do I like that? No, but
2356                        // here we are.
2357                        let replica_id = if let Some(replica_id) = replica_id {
2358                            replica_id
2359                        } else {
2360                            tracing::error!(
2361                                ?sink_stats,
2362                                "missing replica_id for sink statistics update"
2363                            );
2364                            continue;
2365                        };
2366
2367                        let mut shared_stats = self.sink_statistics.lock().expect("poisoned");
2368
2369                        for stat in sink_stats {
2370                            let collection_id = stat.id.clone();
2371
2372                            if self.collection(collection_id).is_err() {
2373                                // We can get updates for collections that have
2374                                // already been deleted, ignore those.
2375                                continue;
2376                            }
2377
2378                            let entry = shared_stats.entry((stat.id, Some(replica_id)));
2379
2380                            match entry {
2381                                btree_map::Entry::Vacant(vacant_entry) => {
2382                                    let mut stats =
2383                                        ControllerSinkStatistics::new(collection_id, replica_id);
2384                                    stats.incorporate(stat);
2385                                    vacant_entry.insert(stats);
2386                                }
2387                                btree_map::Entry::Occupied(mut occupied_entry) => {
2388                                    occupied_entry.get_mut().incorporate(stat);
2389                                }
2390                            }
2391                        }
2392                    }
2393                }
2394                (replica_id, StorageResponse::StatusUpdate(mut status_update)) => {
2395                    // NOTE(aljoscha): We sniff out the hydration status for
2396                    // ingestions from status updates. This is the easiest we
2397                    // can do right now, without going deeper into changing the
2398                    // comms protocol between controller and cluster. We cannot,
2399                    // for example use `StorageResponse::FrontierUpper`,
2400                    // because those will already get sent when the ingestion is
2401                    // just being created.
2402                    //
2403                    // Sources differ in when they will report as Running. Kafka
2404                    // UPSERT sources will only switch to `Running` once their
2405                    // state has been initialized from persist, which is the
2406                    // first case that we care about right now.
2407                    //
2408                    // I wouldn't say it's ideal, but it's workable until we
2409                    // find something better.
2410                    match status_update.status {
2411                        Status::Running => {
2412                            let collection = self.collections.get_mut(&status_update.id);
2413                            match collection {
2414                                Some(collection) => {
2415                                    match collection.extra_state {
2416                                        CollectionStateExtra::Ingestion(
2417                                            ref mut ingestion_state,
2418                                        ) => {
2419                                            if ingestion_state.hydrated_on.is_empty() {
2420                                                tracing::debug!(ingestion_id = %status_update.id, "ingestion is hydrated");
2421                                            }
2422                                            ingestion_state.hydrated_on.insert(replica_id.expect(
2423                                                "replica id should be present for status running",
2424                                            ));
2425                                        }
2426                                        CollectionStateExtra::Export(_) => {
2427                                            // TODO(sinks): track sink hydration?
2428                                        }
2429                                        CollectionStateExtra::None => {
2430                                            // Nothing to do
2431                                        }
2432                                    }
2433                                }
2434                                None => (), // no collection, let's say that's fine
2435                                            // here
2436                            }
2437                        }
2438                        Status::Paused => {
2439                            let collection = self.collections.get_mut(&status_update.id);
2440                            match collection {
2441                                Some(collection) => {
2442                                    match collection.extra_state {
2443                                        CollectionStateExtra::Ingestion(
2444                                            ref mut ingestion_state,
2445                                        ) => {
2446                                            // TODO: Paused gets send when there
2447                                            // are no active replicas. We should
2448                                            // change this to send a targeted
2449                                            // Pause for each replica, and do
2450                                            // more fine-grained hydration
2451                                            // tracking here.
2452                                            tracing::debug!(ingestion_id = %status_update.id, "ingestion is now paused");
2453                                            ingestion_state.hydrated_on.clear();
2454                                        }
2455                                        CollectionStateExtra::Export(_) => {
2456                                            // TODO(sinks): track sink hydration?
2457                                        }
2458                                        CollectionStateExtra::None => {
2459                                            // Nothing to do
2460                                        }
2461                                    }
2462                                }
2463                                None => (), // no collection, let's say that's fine
2464                                            // here
2465                            }
2466                        }
2467                        _ => (),
2468                    }
2469
2470                    // Set replica_id in the status update if available
2471                    if let Some(id) = replica_id {
2472                        status_update.replica_id = Some(id);
2473                    }
2474                    status_updates.push(status_update);
2475                }
2476                (_replica_id, StorageResponse::StagedBatches(batches)) => {
2477                    for (ingestion_id, batches) in batches {
2478                        match self.pending_oneshot_ingestions.remove(&ingestion_id) {
2479                            Some(pending) => {
2480                                // Send a cancel command so our command history is correct. And to
2481                                // avoid duplicate work once we have active replication.
2482                                if let Some(instance) = self.instances.get_mut(&pending.cluster_id)
2483                                {
2484                                    instance
2485                                        .send(StorageCommand::CancelOneshotIngestion(ingestion_id));
2486                                }
2487                                // Send the results down our channel.
2488                                (pending.result_tx)(batches)
2489                            }
2490                            None => {
2491                                // We might not be tracking this oneshot ingestion anymore because
2492                                // it was canceled.
2493                            }
2494                        }
2495                    }
2496                }
2497            }
2498        }
2499
2500        self.record_status_updates(status_updates);
2501
2502        // Process dropped tables in a single batch.
2503        let mut dropped_table_ids = Vec::new();
2504        while let Ok(dropped_id) = self.pending_table_handle_drops_rx.try_recv() {
2505            dropped_table_ids.push(dropped_id);
2506        }
2507        if !dropped_table_ids.is_empty() {
2508            self.drop_sources(storage_metadata, dropped_table_ids)?;
2509        }
2510
2511        if updated_frontiers.is_empty() {
2512            Ok(None)
2513        } else {
2514            Ok(Some(Response::FrontierUpdates(
2515                updated_frontiers.into_iter().collect(),
2516            )))
2517        }
2518    }
2519
2520    async fn inspect_persist_state(
2521        &self,
2522        id: GlobalId,
2523    ) -> Result<serde_json::Value, anyhow::Error> {
2524        let collection = &self.storage_collections.collection_metadata(id)?;
2525        let client = self
2526            .persist
2527            .open(collection.persist_location.clone())
2528            .await?;
2529        let shard_state = client
2530            .inspect_shard::<Timestamp>(&collection.data_shard)
2531            .await?;
2532        let json_state = serde_json::to_value(shard_state)?;
2533        Ok(json_state)
2534    }
2535
2536    fn append_introspection_updates(
2537        &mut self,
2538        type_: IntrospectionType,
2539        updates: Vec<(Row, Diff)>,
2540    ) {
2541        let id = self.introspection_ids[&type_];
2542        let updates = updates.into_iter().map(|update| update.into()).collect();
2543        self.collection_manager.blind_write(id, updates);
2544    }
2545
2546    fn append_status_introspection_updates(
2547        &mut self,
2548        type_: IntrospectionType,
2549        updates: Vec<StatusUpdate>,
2550    ) {
2551        let id = self.introspection_ids[&type_];
2552        let updates: Vec<_> = updates.into_iter().map(|update| update.into()).collect();
2553        if !updates.is_empty() {
2554            self.collection_manager.blind_write(id, updates);
2555        }
2556    }
2557
2558    fn update_introspection_collection(&mut self, type_: IntrospectionType, op: StorageWriteOp) {
2559        let id = self.introspection_ids[&type_];
2560        self.collection_manager.differential_write(id, op);
2561    }
2562
2563    fn append_only_introspection_tx(
2564        &self,
2565        type_: IntrospectionType,
2566    ) -> mpsc::UnboundedSender<(
2567        Vec<AppendOnlyUpdate>,
2568        oneshot::Sender<Result<(), StorageError>>,
2569    )> {
2570        let id = self.introspection_ids[&type_];
2571        self.collection_manager.append_only_write_sender(id)
2572    }
2573
2574    fn differential_introspection_tx(
2575        &self,
2576        type_: IntrospectionType,
2577    ) -> mpsc::UnboundedSender<(StorageWriteOp, oneshot::Sender<Result<(), StorageError>>)> {
2578        let id = self.introspection_ids[&type_];
2579        self.collection_manager.differential_write_sender(id)
2580    }
2581
2582    async fn real_time_recent_timestamp(
2583        &self,
2584        timestamp_objects: BTreeSet<GlobalId>,
2585        timeout: Duration,
2586    ) -> Result<BoxFuture<Result<Timestamp, StorageError>>, StorageError> {
2587        use mz_storage_types::sources::GenericSourceConnection;
2588
2589        let mut rtr_futures = BTreeMap::new();
2590
2591        // Only user sources can be read from w/ RTR.
2592        for id in timestamp_objects.into_iter().filter(GlobalId::is_user) {
2593            let collection = match self.collection(id) {
2594                Ok(c) => c,
2595                // Not a storage item, which we accept.
2596                Err(_) => continue,
2597            };
2598
2599            let (source_conn, remap_id) = match &collection.data_source {
2600                DataSource::Ingestion(IngestionDescription {
2601                    desc: SourceDesc { connection, .. },
2602                    remap_collection_id,
2603                    ..
2604                }) => match connection {
2605                    GenericSourceConnection::Kafka(_)
2606                    | GenericSourceConnection::Postgres(_)
2607                    | GenericSourceConnection::MySql(_)
2608                    | GenericSourceConnection::SqlServer(_) => {
2609                        (connection.clone(), *remap_collection_id)
2610                    }
2611
2612                    // These internal sources do not yet (and might never)
2613                    // support RTR. However, erroring if they're selected from
2614                    // poses an annoying user experience, so instead just skip
2615                    // over them.
2616                    GenericSourceConnection::LoadGenerator(_) => continue,
2617                },
2618                // Skip over all other objects
2619                _ => {
2620                    continue;
2621                }
2622            };
2623
2624            // Prepare for getting the external system's frontier.
2625            let config = self.config().clone();
2626
2627            // Determine the remap collection we plan to read from.
2628            //
2629            // Note that the process of reading from the remap shard is the same
2630            // as other areas in this code that do the same thing, but we inline
2631            // it here because we must prove that we have not taken ownership of
2632            // `self` to move the stream of data from the remap shard into a
2633            // future.
2634            let read_handle = self.read_handle_for_snapshot(remap_id).await?;
2635
2636            // Have to acquire a read hold to prevent the since from advancing
2637            // while we read.
2638            let remap_read_hold = self
2639                .storage_collections
2640                .acquire_read_holds(vec![remap_id])
2641                .map_err(|_e| StorageError::ReadBeforeSince(remap_id))?
2642                .expect_element(|| "known to be exactly one");
2643
2644            let remap_as_of = remap_read_hold
2645                .since()
2646                .to_owned()
2647                .into_option()
2648                .ok_or(StorageError::ReadBeforeSince(remap_id))?;
2649
2650            rtr_futures.insert(
2651                id,
2652                tokio::time::timeout(timeout, async move {
2653                    use mz_storage_types::sources::SourceConnection as _;
2654
2655                    // Fetch the remap shard's contents; we must do this first so
2656                    // that the `as_of` doesn't change.
2657                    let as_of = Antichain::from_elem(remap_as_of);
2658                    let remap_subscribe = read_handle
2659                        .subscribe(as_of.clone())
2660                        .await
2661                        .map_err(|_| StorageError::ReadBeforeSince(remap_id))?;
2662
2663                    tracing::debug!(?id, type_ = source_conn.name(), upstream = ?source_conn.external_reference(), "fetching real time recency");
2664
2665                    let result = rtr::real_time_recency_ts(
2666                        source_conn,
2667                        id,
2668                        config,
2669                        as_of,
2670                        remap_subscribe,
2671                    )
2672                    .await
2673                    .map_err(|e| {
2674                            tracing::debug!(?id, "real time recency error: {:?}", e);
2675                            e
2676                        });
2677
2678                    // Drop once we have read succesfully.
2679                    drop(remap_read_hold);
2680
2681                    result
2682                }),
2683            );
2684        }
2685
2686        Ok(Box::pin(async move {
2687            let (ids, futs): (Vec<_>, Vec<_>) = rtr_futures.into_iter().unzip();
2688            ids.into_iter()
2689                .zip_eq(futures::future::join_all(futs).await)
2690                .try_fold(Timestamp::MIN, |curr, (id, per_source_res)| {
2691                    let new =
2692                        per_source_res.map_err(|_e: Elapsed| StorageError::RtrTimeout(id))??;
2693                    Ok::<_, StorageError>(std::cmp::max(curr, new))
2694                })
2695        }))
2696    }
2697
2698    fn dump(&self) -> Result<serde_json::Value, anyhow::Error> {
2699        // Destructure `self` here so we don't forget to consider dumping newly added fields.
2700        let Self {
2701            build_info: _,
2702            now: _,
2703            read_only,
2704            collections,
2705            dropped_objects,
2706            persist_table_worker: _,
2707            txns_read: _,
2708            txns_metrics: _,
2709            stashed_responses,
2710            pending_table_handle_drops_tx: _,
2711            pending_table_handle_drops_rx: _,
2712            pending_oneshot_ingestions,
2713            collection_manager: _,
2714            introspection_ids,
2715            introspection_tokens: _,
2716            source_statistics: _,
2717            sink_statistics: _,
2718            statistics_interval_sender: _,
2719            instances,
2720            replica_dyncfg_overrides: _,
2721            initialized,
2722            config,
2723            persist_location,
2724            persist: _,
2725            metrics: _,
2726            recorded_frontiers,
2727            recorded_replica_frontiers,
2728            wallclock_lag: _,
2729            wallclock_lag_last_recorded,
2730            storage_collections: _,
2731            migrated_storage_collections,
2732            maintenance_ticker: _,
2733            maintenance_scheduled,
2734            instance_response_tx: _,
2735            instance_response_rx: _,
2736            persist_warm_task: _,
2737        } = self;
2738
2739        let collections: BTreeMap<_, _> = collections
2740            .iter()
2741            .map(|(id, c)| (id.to_string(), format!("{c:?}")))
2742            .collect();
2743        let dropped_objects: BTreeMap<_, _> = dropped_objects
2744            .iter()
2745            .map(|(id, rs)| (id.to_string(), format!("{rs:?}")))
2746            .collect();
2747        let stashed_responses: Vec<_> =
2748            stashed_responses.iter().map(|r| format!("{r:?}")).collect();
2749        let pending_oneshot_ingestions: BTreeMap<_, _> = pending_oneshot_ingestions
2750            .iter()
2751            .map(|(uuid, i)| (uuid.to_string(), format!("{i:?}")))
2752            .collect();
2753        let introspection_ids: BTreeMap<_, _> = introspection_ids
2754            .iter()
2755            .map(|(typ, id)| (format!("{typ:?}"), id.to_string()))
2756            .collect();
2757        let instances: BTreeMap<_, _> = instances
2758            .iter()
2759            .map(|(id, i)| (id.to_string(), format!("{i:?}")))
2760            .collect();
2761        let recorded_frontiers: BTreeMap<_, _> = recorded_frontiers
2762            .iter()
2763            .map(|(id, fs)| (id.to_string(), format!("{fs:?}")))
2764            .collect();
2765        let recorded_replica_frontiers: Vec<_> = recorded_replica_frontiers
2766            .iter()
2767            .map(|((gid, rid), f)| (gid.to_string(), rid.to_string(), format!("{f:?}")))
2768            .collect();
2769        let migrated_storage_collections: Vec<_> = migrated_storage_collections
2770            .iter()
2771            .map(|id| id.to_string())
2772            .collect();
2773
2774        Ok(serde_json::json!({
2775            "read_only": read_only,
2776            "collections": collections,
2777            "dropped_objects": dropped_objects,
2778            "stashed_responses": stashed_responses,
2779            "pending_oneshot_ingestions": pending_oneshot_ingestions,
2780            "introspection_ids": introspection_ids,
2781            "instances": instances,
2782            "initialized": initialized,
2783            "config": format!("{config:?}"),
2784            "persist_location": format!("{persist_location:?}"),
2785            "recorded_frontiers": recorded_frontiers,
2786            "recorded_replica_frontiers": recorded_replica_frontiers,
2787            "wallclock_lag_last_recorded": format!("{wallclock_lag_last_recorded:?}"),
2788            "migrated_storage_collections": migrated_storage_collections,
2789            "maintenance_scheduled": maintenance_scheduled,
2790        }))
2791    }
2792}
2793
2794/// Seed [`StorageTxn`] with any state required to instantiate a
2795/// [`StorageController`].
2796///
2797/// This cannot be a member of [`StorageController`] because it cannot take a
2798/// `self` parameter.
2799///
2800pub fn prepare_initialization(txn: &mut dyn StorageTxn) -> Result<(), StorageError> {
2801    if txn.get_txn_wal_shard().is_none() {
2802        let txns_id = ShardId::new();
2803        txn.write_txn_wal_shard(txns_id)?;
2804    }
2805
2806    Ok(())
2807}
2808
2809impl Controller
2810where
2811    Self: StorageController,
2812{
2813    /// Create a new storage controller from a client it should wrap.
2814    ///
2815    /// Note that when creating a new storage controller, you must also
2816    /// reconcile it with the previous state.
2817    ///
2818    /// # Panics
2819    /// If this function is called before [`prepare_initialization`].
2820    pub async fn new(
2821        build_info: &'static BuildInfo,
2822        persist_location: PersistLocation,
2823        persist_clients: Arc<PersistClientCache>,
2824        now: NowFn,
2825        wallclock_lag: WallclockLagFn<Timestamp>,
2826        txns_metrics: Arc<TxnMetrics>,
2827        read_only: bool,
2828        metrics_registry: &MetricsRegistry,
2829        controller_metrics: ControllerMetrics,
2830        connection_context: ConnectionContext,
2831        txn: &dyn StorageTxn,
2832        storage_collections: Arc<dyn StorageCollections + Send + Sync>,
2833    ) -> Self {
2834        let txns_client = persist_clients
2835            .open(persist_location.clone())
2836            .await
2837            .expect("location should be valid");
2838
2839        let persist_warm_task = warm_persist_state_in_background(
2840            txns_client.clone(),
2841            txn.get_collection_metadata().into_values(),
2842        );
2843        let persist_warm_task = Some(persist_warm_task.abort_on_drop());
2844
2845        // This value must be already installed because we must ensure it's
2846        // durably recorded before it is used, otherwise we risk leaking persist
2847        // state.
2848        let txns_id = txn
2849            .get_txn_wal_shard()
2850            .expect("must call prepare initialization before creating storage controller");
2851
2852        let persist_table_worker = if read_only {
2853            let txns_write = txns_client
2854                .open_writer(
2855                    txns_id,
2856                    Arc::new(TxnsCodecRow::desc()),
2857                    Arc::new(UnitSchema),
2858                    Diagnostics {
2859                        shard_name: "txns".to_owned(),
2860                        handle_purpose: "follow txns upper".to_owned(),
2861                    },
2862                )
2863                .await
2864                .expect("txns schema shouldn't change");
2865            persist_handles::PersistTableWriteWorker::new_read_only_mode(
2866                txns_write,
2867                txns_client.clone(),
2868            )
2869        } else {
2870            let mut txns = TxnsHandle::open(
2871                Timestamp::MIN,
2872                txns_client.clone(),
2873                txns_client.dyncfgs().clone(),
2874                Arc::clone(&txns_metrics),
2875                txns_id,
2876                Opaque::encode(&PersistEpoch::default()),
2877            )
2878            .await;
2879            txns.upgrade_version().await;
2880            persist_handles::PersistTableWriteWorker::new_txns(txns, txns_client.clone())
2881        };
2882        let txns_read = TxnsRead::start::<TxnsCodecRow>(txns_client.clone(), txns_id).await;
2883
2884        let collection_manager = collection_mgmt::CollectionManager::new(read_only, now.clone());
2885
2886        let introspection_ids = BTreeMap::new();
2887        let introspection_tokens = Arc::new(Mutex::new(BTreeMap::new()));
2888
2889        let (statistics_interval_sender, _) =
2890            channel(mz_storage_types::parameters::STATISTICS_INTERVAL_DEFAULT);
2891
2892        let (pending_table_handle_drops_tx, pending_table_handle_drops_rx) =
2893            tokio::sync::mpsc::unbounded_channel();
2894
2895        let mut maintenance_ticker = tokio::time::interval(Duration::from_secs(1));
2896        maintenance_ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
2897
2898        let (instance_response_tx, instance_response_rx) = mpsc::unbounded_channel();
2899
2900        let metrics = StorageControllerMetrics::new(metrics_registry, controller_metrics);
2901
2902        let now_dt = mz_ore::now::to_datetime(now());
2903
2904        Self {
2905            build_info,
2906            collections: BTreeMap::default(),
2907            dropped_objects: Default::default(),
2908            persist_table_worker,
2909            txns_read,
2910            txns_metrics,
2911            stashed_responses: vec![],
2912            pending_table_handle_drops_tx,
2913            pending_table_handle_drops_rx,
2914            pending_oneshot_ingestions: BTreeMap::default(),
2915            collection_manager,
2916            introspection_ids,
2917            introspection_tokens,
2918            now,
2919            read_only,
2920            source_statistics: Arc::new(Mutex::new(statistics::SourceStatistics {
2921                source_statistics: BTreeMap::new(),
2922                webhook_statistics: BTreeMap::new(),
2923            })),
2924            sink_statistics: Arc::new(Mutex::new(BTreeMap::new())),
2925            statistics_interval_sender,
2926            instances: BTreeMap::new(),
2927            replica_dyncfg_overrides: BTreeMap::new(),
2928            initialized: false,
2929            config: StorageConfiguration::new(connection_context, mz_dyncfgs::all_dyncfgs()),
2930            persist_location,
2931            persist: persist_clients,
2932            metrics,
2933            recorded_frontiers: BTreeMap::new(),
2934            recorded_replica_frontiers: BTreeMap::new(),
2935            wallclock_lag,
2936            wallclock_lag_last_recorded: now_dt,
2937            storage_collections,
2938            migrated_storage_collections: BTreeSet::new(),
2939            maintenance_ticker,
2940            maintenance_scheduled: false,
2941            instance_response_rx,
2942            instance_response_tx,
2943            persist_warm_task,
2944        }
2945    }
2946
2947    // This is different from `set_read_policies`, which is for external users.
2948    // This method is for setting the policy that the controller uses when
2949    // maintaining the read holds that it has for collections/exports at the
2950    // StorageCollections.
2951    //
2952    // This is really only used when dropping things, where we set the
2953    // ReadPolicy to the empty Antichain.
2954    #[instrument(level = "debug")]
2955    fn set_hold_policies(&mut self, policies: Vec<(GlobalId, ReadPolicy)>) {
2956        let mut read_capability_changes = BTreeMap::default();
2957
2958        for (id, policy) in policies.into_iter() {
2959            if let Some(collection) = self.collections.get_mut(&id) {
2960                let (write_frontier, derived_since, hold_policy) = match &mut collection.extra_state
2961                {
2962                    CollectionStateExtra::Ingestion(ingestion) => (
2963                        ingestion.write_frontier.borrow(),
2964                        &mut ingestion.derived_since,
2965                        &mut ingestion.hold_policy,
2966                    ),
2967                    CollectionStateExtra::None => {
2968                        unreachable!("set_hold_policies is only called for ingestions");
2969                    }
2970                    CollectionStateExtra::Export(export) => (
2971                        export.write_frontier.borrow(),
2972                        &mut export.derived_since,
2973                        &mut export.read_policy,
2974                    ),
2975                };
2976
2977                let new_derived_since = policy.frontier(write_frontier);
2978                let mut update = swap_updates(derived_since, new_derived_since);
2979                if !update.is_empty() {
2980                    read_capability_changes.insert(id, update);
2981                }
2982
2983                *hold_policy = policy;
2984            }
2985        }
2986
2987        if !read_capability_changes.is_empty() {
2988            self.update_hold_capabilities(&mut read_capability_changes);
2989        }
2990    }
2991
2992    #[instrument(level = "debug", fields(updates))]
2993    fn update_write_frontier(&mut self, id: GlobalId, new_upper: &Antichain<Timestamp>) {
2994        let mut read_capability_changes = BTreeMap::default();
2995
2996        if let Some(collection) = self.collections.get_mut(&id) {
2997            let (write_frontier, derived_since, hold_policy) = match &mut collection.extra_state {
2998                CollectionStateExtra::Ingestion(ingestion) => (
2999                    &mut ingestion.write_frontier,
3000                    &mut ingestion.derived_since,
3001                    &ingestion.hold_policy,
3002                ),
3003                CollectionStateExtra::None => {
3004                    if matches!(collection.data_source, DataSource::Progress) {
3005                        // We do get these, but can't do anything with it!
3006                    } else {
3007                        tracing::error!(
3008                            ?collection,
3009                            ?new_upper,
3010                            "updated write frontier for collection which is not an ingestion"
3011                        );
3012                    }
3013                    return;
3014                }
3015                CollectionStateExtra::Export(export) => (
3016                    &mut export.write_frontier,
3017                    &mut export.derived_since,
3018                    &export.read_policy,
3019                ),
3020            };
3021
3022            if PartialOrder::less_than(write_frontier, new_upper) {
3023                write_frontier.clone_from(new_upper);
3024            }
3025
3026            let new_derived_since = hold_policy.frontier(write_frontier.borrow());
3027            let mut update = swap_updates(derived_since, new_derived_since);
3028            if !update.is_empty() {
3029                read_capability_changes.insert(id, update);
3030            }
3031        } else if self.dropped_objects.contains_key(&id) {
3032            // We dropped an object but might still get updates from cluster
3033            // side, before it notices the drop. This is expected and fine.
3034        } else {
3035            soft_panic_or_log!("spurious upper update for {id}: {new_upper:?}");
3036        }
3037
3038        if !read_capability_changes.is_empty() {
3039            self.update_hold_capabilities(&mut read_capability_changes);
3040        }
3041    }
3042
3043    // This is different from `update_read_capabilities`, which is for external users.
3044    // This method is for maintaining the read holds that the controller has at
3045    // the StorageCollections, for storage dependencies.
3046    #[instrument(level = "debug", fields(updates))]
3047    fn update_hold_capabilities(
3048        &mut self,
3049        updates: &mut BTreeMap<GlobalId, ChangeBatch<Timestamp>>,
3050    ) {
3051        // Location to record consequences that we need to act on.
3052        let mut collections_net = BTreeMap::new();
3053
3054        // We must not rely on any specific relative ordering of `GlobalId`s.
3055        // That said, it is reasonable to assume that collections generally have
3056        // greater IDs than their dependencies, so starting with the largest is
3057        // a useful optimization.
3058        while let Some(key) = updates.keys().rev().next().cloned() {
3059            let mut update = updates.remove(&key).unwrap();
3060
3061            if key.is_user() {
3062                debug!(id = %key, ?update, "update_hold_capability");
3063            }
3064
3065            if let Some(collection) = self.collections.get_mut(&key) {
3066                match &mut collection.extra_state {
3067                    CollectionStateExtra::Ingestion(ingestion) => {
3068                        let changes = ingestion.read_capabilities.update_iter(update.drain());
3069                        update.extend(changes);
3070
3071                        let (changes, frontier, _cluster_id) =
3072                            collections_net.entry(key).or_insert_with(|| {
3073                                (
3074                                    <ChangeBatch<_>>::new(),
3075                                    Antichain::new(),
3076                                    ingestion.instance_id,
3077                                )
3078                            });
3079
3080                        changes.extend(update.drain());
3081                        *frontier = ingestion.read_capabilities.frontier().to_owned();
3082                    }
3083                    CollectionStateExtra::None => {
3084                        // WIP: See if this ever panics in ci.
3085                        soft_panic_or_log!(
3086                            "trying to update holds for collection {collection:?} which is not \
3087                             an ingestion: {update:?}"
3088                        );
3089                        continue;
3090                    }
3091                    CollectionStateExtra::Export(export) => {
3092                        let changes = export.read_capabilities.update_iter(update.drain());
3093                        update.extend(changes);
3094
3095                        let (changes, frontier, _cluster_id) =
3096                            collections_net.entry(key).or_insert_with(|| {
3097                                (<ChangeBatch<_>>::new(), Antichain::new(), export.cluster_id)
3098                            });
3099
3100                        changes.extend(update.drain());
3101                        *frontier = export.read_capabilities.frontier().to_owned();
3102                    }
3103                }
3104            } else {
3105                // This is confusing and we should probably error.
3106                tracing::warn!(id = ?key, ?update, "update_hold_capabilities for unknown object");
3107            }
3108        }
3109
3110        // Translate our net compute actions into `AllowCompaction` commands and
3111        // downgrade persist sinces.
3112        for (key, (mut changes, frontier, cluster_id)) in collections_net {
3113            if !changes.is_empty() {
3114                if key.is_user() {
3115                    debug!(id = %key, ?frontier, "downgrading ingestion read holds!");
3116                }
3117
3118                let collection = self
3119                    .collections
3120                    .get_mut(&key)
3121                    .expect("missing collection state");
3122
3123                let read_holds = match &mut collection.extra_state {
3124                    CollectionStateExtra::Ingestion(ingestion) => {
3125                        ingestion.dependency_read_holds.as_mut_slice()
3126                    }
3127                    CollectionStateExtra::Export(export) => export.read_holds.as_mut_slice(),
3128                    CollectionStateExtra::None => {
3129                        soft_panic_or_log!(
3130                            "trying to downgrade read holds for collection which is not an \
3131                             ingestion: {collection:?}"
3132                        );
3133                        continue;
3134                    }
3135                };
3136
3137                for read_hold in read_holds.iter_mut() {
3138                    read_hold
3139                        .try_downgrade(frontier.clone())
3140                        .expect("we only advance the frontier");
3141                }
3142
3143                // Send AllowCompaction command directly to the instance
3144                if let Some(instance) = self.instances.get_mut(&cluster_id) {
3145                    instance.send(StorageCommand::AllowCompaction(key, frontier.clone()));
3146                } else {
3147                    soft_panic_or_log!(
3148                        "missing instance client for cluster {cluster_id} while we still have outstanding AllowCompaction command {frontier:?} for {key}"
3149                    );
3150                }
3151            }
3152        }
3153    }
3154
3155    /// Validate that a collection exists for all identifiers, and error if any do not.
3156    fn validate_collection_ids(
3157        &self,
3158        ids: impl Iterator<Item = GlobalId>,
3159    ) -> Result<(), StorageError> {
3160        for id in ids {
3161            self.storage_collections.check_exists(id)?;
3162        }
3163        Ok(())
3164    }
3165
3166    /// Validate that a collection exists for all identifiers, and error if any do not.
3167    fn validate_export_ids(&self, ids: impl Iterator<Item = GlobalId>) -> Result<(), StorageError> {
3168        for id in ids {
3169            self.export(id)?;
3170        }
3171        Ok(())
3172    }
3173
3174    /// Opens a write handle and synchronizes its cached upper.
3175    async fn open_data_handles(
3176        &self,
3177        id: &GlobalId,
3178        shard: ShardId,
3179        relation_desc: RelationDesc,
3180        persist_client: &PersistClient,
3181    ) -> WriteHandle<SourceData, (), Timestamp, StorageDiff> {
3182        let diagnostics = Diagnostics {
3183            shard_name: id.to_string(),
3184            handle_purpose: format!("controller data for {}", id),
3185        };
3186
3187        let mut write = persist_client
3188            .open_writer(
3189                shard,
3190                Arc::new(relation_desc),
3191                Arc::new(UnitSchema),
3192                diagnostics.clone(),
3193            )
3194            .await
3195            .expect("invalid persist usage");
3196
3197        // Synchronize the cached upper before it initializes collection state.
3198        write.fetch_recent_upper().await;
3199
3200        write
3201    }
3202
3203    /// Registers the given introspection collection and does any preparatory
3204    /// work that we have to do before we start writing to it. This
3205    /// preparatory work will include partial truncation or other cleanup
3206    /// schemes, depending on introspection type.
3207    fn register_introspection_collection(
3208        &mut self,
3209        id: GlobalId,
3210        introspection_type: IntrospectionType,
3211        write_handle: WriteHandle<SourceData, (), Timestamp, StorageDiff>,
3212        persist_client: PersistClient,
3213    ) -> Result<(), StorageError> {
3214        tracing::info!(%id, ?introspection_type, "registering introspection collection");
3215
3216        // In read-only mode we create a new shard for all migrated storage collections. So we
3217        // "trick" the write task into thinking that it's not in read-only mode so something is
3218        // advancing this new shard.
3219        //
3220        // This is the storage-side of forcing writes to a self-owned replacement shard while
3221        // read-only. Migrated builtin MVs need the same treatment on their own write path; see
3222        // `ComputeController::allow_writes_in_read_only`.
3223        let force_writable = self.read_only && self.migrated_storage_collections.contains(&id);
3224        if force_writable {
3225            assert!(id.is_system(), "unexpected non-system global id: {id:?}");
3226            info!("writing to migrated storage collection {id} in read-only mode");
3227        }
3228
3229        let prev = self.introspection_ids.insert(introspection_type, id);
3230        assert!(
3231            prev.is_none(),
3232            "cannot have multiple IDs for introspection type"
3233        );
3234
3235        let metadata = self.storage_collections.collection_metadata(id)?.clone();
3236
3237        let read_handle_fn = move || {
3238            let persist_client = persist_client.clone();
3239            let metadata = metadata.clone();
3240
3241            let fut = async move {
3242                let read_handle = persist_client
3243                    .open_leased_reader::<SourceData, (), Timestamp, StorageDiff>(
3244                        metadata.data_shard,
3245                        Arc::new(metadata.relation_desc.clone()),
3246                        Arc::new(UnitSchema),
3247                        Diagnostics {
3248                            shard_name: id.to_string(),
3249                            handle_purpose: format!("snapshot {}", id),
3250                        },
3251                        USE_CRITICAL_SINCE_SNAPSHOT.get(persist_client.dyncfgs()),
3252                    )
3253                    .await
3254                    .expect("invalid persist usage");
3255                read_handle
3256            };
3257
3258            fut.boxed()
3259        };
3260
3261        let recent_upper = write_handle.shared_upper();
3262
3263        match CollectionManagerKind::from(&introspection_type) {
3264            // For these, we first register the collection and then prepare it,
3265            // because the code that prepares differential collection expects to
3266            // be able to update desired state via the collection manager
3267            // already.
3268            CollectionManagerKind::Differential => {
3269                let statistics_retention_duration =
3270                    dyncfgs::STATISTICS_RETENTION_DURATION.get(self.config().config_set());
3271
3272                // These do a shallow copy.
3273                let introspection_config = DifferentialIntrospectionConfig {
3274                    recent_upper,
3275                    introspection_type,
3276                    storage_collections: Arc::clone(&self.storage_collections),
3277                    collection_manager: self.collection_manager.clone(),
3278                    source_statistics: Arc::clone(&self.source_statistics),
3279                    sink_statistics: Arc::clone(&self.sink_statistics),
3280                    statistics_interval: self.config.parameters.statistics_interval.clone(),
3281                    statistics_interval_receiver: self.statistics_interval_sender.subscribe(),
3282                    statistics_retention_duration,
3283                    metrics: self.metrics.clone(),
3284                    introspection_tokens: Arc::clone(&self.introspection_tokens),
3285                };
3286                self.collection_manager.register_differential_collection(
3287                    id,
3288                    write_handle,
3289                    read_handle_fn,
3290                    force_writable,
3291                    introspection_config,
3292                );
3293            }
3294            // For these, we first have to prepare and then register with
3295            // collection manager, because the preparation logic wants to read
3296            // the shard's contents and then do uncontested writes.
3297            //
3298            // TODO(aljoscha): We should make the truncation/cleanup work that
3299            // happens when we take over instead be a periodic thing, and make
3300            // it resilient to the upper moving concurrently.
3301            CollectionManagerKind::AppendOnly => {
3302                let introspection_config = AppendOnlyIntrospectionConfig {
3303                    introspection_type,
3304                    config_set: Arc::clone(self.config.config_set()),
3305                    parameters: self.config.parameters.clone(),
3306                    storage_collections: Arc::clone(&self.storage_collections),
3307                };
3308                self.collection_manager.register_append_only_collection(
3309                    id,
3310                    write_handle,
3311                    force_writable,
3312                    Some(introspection_config),
3313                );
3314            }
3315        }
3316
3317        Ok(())
3318    }
3319
3320    /// Remove statistics for sources/sinks that were dropped but still have statistics rows
3321    /// hanging around.
3322    fn reconcile_dangling_statistics(&self) {
3323        self.source_statistics
3324            .lock()
3325            .expect("poisoned")
3326            .source_statistics
3327            // collections should also contain subsources.
3328            .retain(|(k, _replica_id), _| self.storage_collections.check_exists(*k).is_ok());
3329        self.sink_statistics
3330            .lock()
3331            .expect("poisoned")
3332            .retain(|(k, _replica_id), _| self.export(*k).is_ok());
3333    }
3334
3335    /// Appends a new global ID, shard ID pair to the appropriate collection.
3336    /// Use a `diff` of 1 to append a new entry; -1 to retract an existing
3337    /// entry.
3338    ///
3339    /// # Panics
3340    /// - If `self.collections` does not have an entry for `global_id`.
3341    /// - If `IntrospectionType::ShardMapping`'s `GlobalId` is not registered as
3342    ///   a managed collection.
3343    /// - If diff is any value other than `1` or `-1`.
3344    #[instrument(level = "debug")]
3345    fn append_shard_mappings<I>(&self, global_ids: I, diff: Diff)
3346    where
3347        I: Iterator<Item = GlobalId>,
3348    {
3349        mz_ore::soft_assert_or_log!(
3350            diff == Diff::MINUS_ONE || diff == Diff::ONE,
3351            "use 1 for insert or -1 for delete"
3352        );
3353
3354        let id = *self
3355            .introspection_ids
3356            .get(&IntrospectionType::ShardMapping)
3357            .expect("should be registered before this call");
3358
3359        let mut updates = vec![];
3360        // Pack updates into rows
3361        let mut row_buf = Row::default();
3362
3363        for global_id in global_ids {
3364            let shard_id = if let Some(collection) = self.collections.get(&global_id) {
3365                collection.collection_metadata.data_shard.clone()
3366            } else {
3367                panic!("unknown global id: {}", global_id);
3368            };
3369
3370            let mut packer = row_buf.packer();
3371            packer.push(Datum::from(global_id.to_string().as_str()));
3372            packer.push(Datum::from(shard_id.to_string().as_str()));
3373            updates.push((row_buf.clone(), diff));
3374        }
3375
3376        self.collection_manager.differential_append(id, updates);
3377    }
3378
3379    /// Determines and returns this collection's dependencies, if any.
3380    fn determine_collection_dependencies(
3381        &self,
3382        self_id: GlobalId,
3383        collection_desc: &CollectionDescription,
3384    ) -> Result<Vec<GlobalId>, StorageError> {
3385        let mut dependencies = Vec::new();
3386
3387        if let Some(id) = collection_desc.primary {
3388            dependencies.push(id);
3389        }
3390
3391        match &collection_desc.data_source {
3392            DataSource::Introspection(_)
3393            | DataSource::Webhook
3394            | DataSource::Table
3395            | DataSource::Progress
3396            | DataSource::Other => (),
3397            DataSource::IngestionExport { ingestion_id, .. } => {
3398                // Ingestion exports depend on their primary source's remap
3399                // collection.
3400                let source_collection = self.collection(*ingestion_id)?;
3401                let ingestion_remap_collection_id = match &source_collection.data_source {
3402                    DataSource::Ingestion(ingestion) => ingestion.remap_collection_id,
3403                    _ => unreachable!(
3404                        "SourceExport must only refer to primary sources that already exist"
3405                    ),
3406                };
3407
3408                // Ingestion exports (aka. subsources) must make sure that 1)
3409                // their own collection's since stays one step behind the upper,
3410                // and, 2) that the remap shard's since stays one step behind
3411                // their upper. Hence they track themselves and the remap shard
3412                // as dependencies.
3413                dependencies.extend([self_id, ingestion_remap_collection_id]);
3414            }
3415            // Ingestions depend on their remap collection.
3416            DataSource::Ingestion(ingestion) => {
3417                // Ingestions must make sure that 1) their own collection's
3418                // since stays one step behind the upper, and, 2) that the remap
3419                // shard's since stays one step behind their upper. Hence they
3420                // track themselves and the remap shard as dependencies.
3421                dependencies.push(self_id);
3422                if self_id != ingestion.remap_collection_id {
3423                    dependencies.push(ingestion.remap_collection_id);
3424                }
3425            }
3426            DataSource::Sink { desc } => {
3427                // Sinks hold back their own frontier and the frontier of their input.
3428                dependencies.extend([self_id, desc.sink.from]);
3429            }
3430        };
3431
3432        Ok(dependencies)
3433    }
3434
3435    async fn read_handle_for_snapshot(
3436        &self,
3437        id: GlobalId,
3438    ) -> Result<ReadHandle<SourceData, (), Timestamp, StorageDiff>, StorageError> {
3439        let metadata = self.storage_collections.collection_metadata(id)?;
3440        read_handle_for_snapshot(&self.persist, id, &metadata).await
3441    }
3442
3443    /// Handles writing of status updates for sources/sinks to the appropriate
3444    /// status relation
3445    fn record_status_updates(&mut self, updates: Vec<StatusUpdate>) {
3446        if self.read_only {
3447            return;
3448        }
3449
3450        let mut sink_status_updates = vec![];
3451        let mut source_status_updates = vec![];
3452
3453        for update in updates {
3454            let id = update.id;
3455            if self.export(id).is_ok() {
3456                sink_status_updates.push(update);
3457            } else if self.storage_collections.check_exists(id).is_ok() {
3458                source_status_updates.push(update);
3459            }
3460        }
3461
3462        self.append_status_introspection_updates(
3463            IntrospectionType::SourceStatusHistory,
3464            source_status_updates,
3465        );
3466        self.append_status_introspection_updates(
3467            IntrospectionType::SinkStatusHistory,
3468            sink_status_updates,
3469        );
3470    }
3471
3472    fn collection(&self, id: GlobalId) -> Result<&CollectionState, StorageError> {
3473        self.collections
3474            .get(&id)
3475            .ok_or(StorageError::IdentifierMissing(id))
3476    }
3477
3478    /// Runs the identified ingestion using the current definition of the
3479    /// ingestion in-memory.
3480    fn run_ingestion(&mut self, id: GlobalId) -> Result<(), StorageError> {
3481        tracing::info!(%id, "starting ingestion");
3482
3483        let collection = self.collection(id)?;
3484        let ingestion_description = match &collection.data_source {
3485            DataSource::Ingestion(i) => i.clone(),
3486            _ => {
3487                tracing::warn!("run_ingestion called on non-ingestion ID {}", id);
3488                Err(StorageError::IdentifierInvalid(id))?
3489            }
3490        };
3491
3492        // Enrich all of the exports with their metadata
3493        let mut source_exports = BTreeMap::new();
3494        for (export_id, export) in ingestion_description.source_exports.clone() {
3495            let export_storage_metadata = self.collection(export_id)?.collection_metadata.clone();
3496            source_exports.insert(
3497                export_id,
3498                SourceExport {
3499                    storage_metadata: export_storage_metadata,
3500                    details: export.details,
3501                    data_config: export.data_config,
3502                },
3503            );
3504        }
3505
3506        let remap_collection = self.collection(ingestion_description.remap_collection_id)?;
3507
3508        let description = IngestionDescription::<CollectionMetadata> {
3509            source_exports,
3510            remap_metadata: remap_collection.collection_metadata.clone(),
3511            // The rest of the fields are identical
3512            desc: ingestion_description.desc.clone(),
3513            instance_id: ingestion_description.instance_id,
3514            remap_collection_id: ingestion_description.remap_collection_id,
3515        };
3516
3517        let storage_instance_id = description.instance_id;
3518        // Fetch the client for this ingestion's instance.
3519        let instance = self
3520            .instances
3521            .get_mut(&storage_instance_id)
3522            .ok_or_else(|| StorageError::IngestionInstanceMissing {
3523                storage_instance_id,
3524                ingestion_id: id,
3525            })?;
3526
3527        let augmented_ingestion = Box::new(RunIngestionCommand { id, description });
3528        instance.send(StorageCommand::RunIngestion(augmented_ingestion));
3529
3530        Ok(())
3531    }
3532
3533    /// Runs the identified export using the current definition of the export
3534    /// that we have in memory.
3535    fn run_export(&mut self, id: GlobalId) -> Result<(), StorageError> {
3536        let DataSource::Sink { desc: description } = &self.collections[&id].data_source else {
3537            return Err(StorageError::IdentifierMissing(id));
3538        };
3539
3540        let from_storage_metadata = self
3541            .storage_collections
3542            .collection_metadata(description.sink.from)?;
3543        let to_storage_metadata = self.storage_collections.collection_metadata(id)?;
3544
3545        // Choose an as-of frontier for this execution of the sink. If the write frontier of the sink
3546        // is strictly larger than its read hold, it must have at least written out its snapshot, and we can skip
3547        // reading it; otherwise assume we may have to replay from the beginning.
3548        let export_state = self.storage_collections.collection_frontiers(id)?;
3549        let mut as_of = description.sink.as_of.clone();
3550        as_of.join_assign(&export_state.implied_capability);
3551        let with_snapshot = description.sink.with_snapshot
3552            && !PartialOrder::less_than(&as_of, &export_state.write_frontier);
3553
3554        info!(
3555            sink_id = %id,
3556            from_id = %description.sink.from,
3557            write_frontier = ?export_state.write_frontier,
3558            ?as_of,
3559            ?with_snapshot,
3560            "run_export"
3561        );
3562
3563        let cmd = RunSinkCommand {
3564            id,
3565            description: StorageSinkDesc {
3566                from: description.sink.from,
3567                from_desc: description.sink.from_desc.clone(),
3568                connection: description.sink.connection.clone(),
3569                envelope: description.sink.envelope,
3570                as_of,
3571                version: description.sink.version,
3572                from_storage_metadata,
3573                with_snapshot,
3574                to_storage_metadata,
3575                commit_interval: description.sink.commit_interval,
3576            },
3577        };
3578
3579        let storage_instance_id = description.instance_id.clone();
3580
3581        let instance = self
3582            .instances
3583            .get_mut(&storage_instance_id)
3584            .ok_or_else(|| StorageError::ExportInstanceMissing {
3585                storage_instance_id,
3586                export_id: id,
3587            })?;
3588
3589        instance.send(StorageCommand::RunSink(Box::new(cmd)));
3590
3591        Ok(())
3592    }
3593
3594    /// Update introspection with the current frontiers of storage objects.
3595    ///
3596    /// This method is invoked by `Controller::maintain`, which we expect to be called once per
3597    /// second during normal operation.
3598    fn update_frontier_introspection(&mut self) {
3599        let mut global_frontiers = BTreeMap::new();
3600        let mut replica_frontiers = BTreeMap::new();
3601
3602        for collection_frontiers in self.storage_collections.active_collection_frontiers() {
3603            let id = collection_frontiers.id;
3604            let since = collection_frontiers.read_capabilities;
3605            let upper = collection_frontiers.write_frontier;
3606
3607            let instance = self
3608                .collections
3609                .get(&id)
3610                .and_then(|collection_state| match &collection_state.extra_state {
3611                    CollectionStateExtra::Ingestion(ingestion) => Some(ingestion.instance_id),
3612                    CollectionStateExtra::Export(export) => Some(export.cluster_id()),
3613                    CollectionStateExtra::None => None,
3614                })
3615                .and_then(|i| self.instances.get(&i));
3616
3617            if let Some(instance) = instance {
3618                for replica_id in instance.replica_ids() {
3619                    replica_frontiers.insert((id, replica_id), upper.clone());
3620                }
3621            }
3622
3623            global_frontiers.insert(id, (since, upper));
3624        }
3625
3626        let mut global_updates = Vec::new();
3627        let mut replica_updates = Vec::new();
3628
3629        let mut push_global_update =
3630            |id: GlobalId,
3631             (since, upper): (Antichain<Timestamp>, Antichain<Timestamp>),
3632             diff: Diff| {
3633                let read_frontier = since.into_option().map_or(Datum::Null, |t| t.into());
3634                let write_frontier = upper.into_option().map_or(Datum::Null, |t| t.into());
3635                let row = Row::pack_slice(&[
3636                    Datum::String(&id.to_string()),
3637                    read_frontier,
3638                    write_frontier,
3639                ]);
3640                global_updates.push((row, diff));
3641            };
3642
3643        let mut push_replica_update =
3644            |(id, replica_id): (GlobalId, ReplicaId), upper: Antichain<Timestamp>, diff: Diff| {
3645                let write_frontier = upper.into_option().map_or(Datum::Null, |t| t.into());
3646                let row = Row::pack_slice(&[
3647                    Datum::String(&id.to_string()),
3648                    Datum::String(&replica_id.to_string()),
3649                    write_frontier,
3650                ]);
3651                replica_updates.push((row, diff));
3652            };
3653
3654        let mut old_global_frontiers =
3655            std::mem::replace(&mut self.recorded_frontiers, global_frontiers);
3656        for (&id, new) in &self.recorded_frontiers {
3657            match old_global_frontiers.remove(&id) {
3658                Some(old) if &old != new => {
3659                    push_global_update(id, new.clone(), Diff::ONE);
3660                    push_global_update(id, old, Diff::MINUS_ONE);
3661                }
3662                Some(_) => (),
3663                None => push_global_update(id, new.clone(), Diff::ONE),
3664            }
3665        }
3666        for (id, old) in old_global_frontiers {
3667            push_global_update(id, old, Diff::MINUS_ONE);
3668        }
3669
3670        let mut old_replica_frontiers =
3671            std::mem::replace(&mut self.recorded_replica_frontiers, replica_frontiers);
3672        for (&key, new) in &self.recorded_replica_frontiers {
3673            match old_replica_frontiers.remove(&key) {
3674                Some(old) if &old != new => {
3675                    push_replica_update(key, new.clone(), Diff::ONE);
3676                    push_replica_update(key, old, Diff::MINUS_ONE);
3677                }
3678                Some(_) => (),
3679                None => push_replica_update(key, new.clone(), Diff::ONE),
3680            }
3681        }
3682        for (key, old) in old_replica_frontiers {
3683            push_replica_update(key, old, Diff::MINUS_ONE);
3684        }
3685
3686        let id = self.introspection_ids[&IntrospectionType::Frontiers];
3687        self.collection_manager
3688            .differential_append(id, global_updates);
3689
3690        let id = self.introspection_ids[&IntrospectionType::ReplicaFrontiers];
3691        self.collection_manager
3692            .differential_append(id, replica_updates);
3693    }
3694
3695    /// Refresh the wallclock lag introspection and metrics with the current lag values.
3696    ///
3697    /// This method produces wallclock lag metrics of two different shapes:
3698    ///
3699    /// * Histories: For each replica and each collection, we measure the lag of the write frontier
3700    ///   behind the wallclock time every second. Every minute we emit the maximum lag observed
3701    ///   over the last minute, together with the current time.
3702    /// * Histograms: For each collection, we measure the lag of the write frontier behind
3703    ///   wallclock time every second. Every minute we emit all lags observed over the last minute,
3704    ///   together with the current histogram period.
3705    ///
3706    /// Histories are emitted to both Mz introspection and Prometheus, histograms only to
3707    /// introspection. We treat lags of unreadable collections (i.e. collections that contain no
3708    /// readable times) as undefined and set them to NULL in introspection and `u64::MAX` in
3709    /// Prometheus.
3710    ///
3711    /// This method is invoked by `Controller::maintain`, which we expect to be called once per
3712    /// second during normal operation.
3713    fn refresh_wallclock_lag(&mut self) {
3714        let now_ms = (self.now)();
3715        let histogram_period =
3716            WallclockLagHistogramPeriod::from_epoch_millis(now_ms, self.config.config_set());
3717
3718        let frontier_lag = |frontier: &Antichain<Timestamp>| match frontier.as_option() {
3719            Some(ts) => (self.wallclock_lag)(*ts),
3720            None => Duration::ZERO,
3721        };
3722
3723        for frontiers in self.storage_collections.active_collection_frontiers() {
3724            let id = frontiers.id;
3725            let Some(collection) = self.collections.get_mut(&id) else {
3726                continue;
3727            };
3728
3729            let collection_unreadable =
3730                PartialOrder::less_equal(&frontiers.write_frontier, &frontiers.read_capabilities);
3731            let lag = if collection_unreadable {
3732                WallclockLag::Undefined
3733            } else {
3734                let lag = frontier_lag(&frontiers.write_frontier);
3735                WallclockLag::Seconds(lag.as_secs())
3736            };
3737
3738            collection.wallclock_lag_max = collection.wallclock_lag_max.max(lag);
3739
3740            // No way to specify values as undefined in Prometheus metrics, so we use the
3741            // maximum value instead.
3742            let secs = lag.unwrap_seconds_or(u64::MAX);
3743            collection.wallclock_lag_metrics.observe(secs);
3744
3745            if let Some(stash) = &mut collection.wallclock_lag_histogram_stash {
3746                let bucket = lag.map_seconds(|secs| secs.next_power_of_two());
3747
3748                let instance_id = match &collection.extra_state {
3749                    CollectionStateExtra::Ingestion(i) => Some(i.instance_id),
3750                    CollectionStateExtra::Export(e) => Some(e.cluster_id()),
3751                    CollectionStateExtra::None => None,
3752                };
3753                let workload_class = instance_id
3754                    .and_then(|id| self.instances.get(&id))
3755                    .and_then(|i| i.workload_class.clone());
3756                let labels = match workload_class {
3757                    Some(wc) => [("workload_class", wc.clone())].into(),
3758                    None => BTreeMap::new(),
3759                };
3760
3761                let key = (histogram_period, bucket, labels);
3762                *stash.entry(key).or_default() += Diff::ONE;
3763            }
3764        }
3765
3766        // Record lags to persist, if it's time.
3767        self.maybe_record_wallclock_lag();
3768    }
3769
3770    /// Produce new wallclock lag introspection updates, provided enough time has passed since the
3771    /// last recording.
3772    ///
3773    /// We emit new introspection updates if the system time has passed into a new multiple of the
3774    /// recording interval (typically 1 minute) since the last refresh. The compute controller uses
3775    /// the same approach, ensuring that both controllers commit their lags at roughly the same
3776    /// time, avoiding confusion caused by inconsistencies.
3777    fn maybe_record_wallclock_lag(&mut self) {
3778        if self.read_only {
3779            return;
3780        }
3781
3782        let duration_trunc = |datetime: DateTime<_>, interval| {
3783            let td = TimeDelta::from_std(interval).ok()?;
3784            datetime.duration_trunc(td).ok()
3785        };
3786
3787        let interval = WALLCLOCK_LAG_RECORDING_INTERVAL.get(self.config.config_set());
3788        let now_dt = mz_ore::now::to_datetime((self.now)());
3789        let now_trunc = duration_trunc(now_dt, interval).unwrap_or_else(|| {
3790            soft_panic_or_log!("excessive wallclock lag recording interval: {interval:?}");
3791            let default = WALLCLOCK_LAG_RECORDING_INTERVAL.default();
3792            duration_trunc(now_dt, *default).unwrap()
3793        });
3794        if now_trunc <= self.wallclock_lag_last_recorded {
3795            return;
3796        }
3797
3798        let now_ts: CheckedTimestamp<_> = now_trunc.try_into().expect("must fit");
3799
3800        let mut history_updates = Vec::new();
3801        let mut histogram_updates = Vec::new();
3802        let mut row_buf = Row::default();
3803        for frontiers in self.storage_collections.active_collection_frontiers() {
3804            let id = frontiers.id;
3805            let Some(collection) = self.collections.get_mut(&id) else {
3806                continue;
3807            };
3808
3809            let max_lag = std::mem::replace(&mut collection.wallclock_lag_max, WallclockLag::MIN);
3810            let row = Row::pack_slice(&[
3811                Datum::String(&id.to_string()),
3812                Datum::Null,
3813                max_lag.into_interval_datum(),
3814                Datum::TimestampTz(now_ts),
3815            ]);
3816            history_updates.push((row, Diff::ONE));
3817
3818            let Some(stash) = &mut collection.wallclock_lag_histogram_stash else {
3819                continue;
3820            };
3821
3822            for ((period, lag, labels), count) in std::mem::take(stash) {
3823                let mut packer = row_buf.packer();
3824                packer.extend([
3825                    Datum::TimestampTz(period.start),
3826                    Datum::TimestampTz(period.end),
3827                    Datum::String(&id.to_string()),
3828                    lag.into_uint64_datum(),
3829                ]);
3830                let labels = labels.iter().map(|(k, v)| (*k, Datum::String(v)));
3831                packer.push_dict(labels);
3832
3833                histogram_updates.push((row_buf.clone(), count));
3834            }
3835        }
3836
3837        if !history_updates.is_empty() {
3838            self.append_introspection_updates(
3839                IntrospectionType::WallclockLagHistory,
3840                history_updates,
3841            );
3842        }
3843        if !histogram_updates.is_empty() {
3844            self.append_introspection_updates(
3845                IntrospectionType::WallclockLagHistogram,
3846                histogram_updates,
3847            );
3848        }
3849
3850        self.wallclock_lag_last_recorded = now_trunc;
3851    }
3852
3853    /// Run periodic tasks.
3854    ///
3855    /// This method is invoked roughly once per second during normal operation. It is a good place
3856    /// for tasks that need to run periodically, such as state cleanup or updating of metrics.
3857    fn maintain(&mut self) {
3858        self.update_frontier_introspection();
3859        self.refresh_wallclock_lag();
3860
3861        // Perform instance maintenance work.
3862        for instance in self.instances.values_mut() {
3863            instance.refresh_state_metrics();
3864        }
3865    }
3866}
3867
3868impl From<&IntrospectionType> for CollectionManagerKind {
3869    fn from(value: &IntrospectionType) -> Self {
3870        match value {
3871            IntrospectionType::ShardMapping
3872            | IntrospectionType::Frontiers
3873            | IntrospectionType::ReplicaFrontiers
3874            | IntrospectionType::StorageSourceStatistics
3875            | IntrospectionType::StorageSinkStatistics
3876            | IntrospectionType::ComputeDependencies
3877            | IntrospectionType::ComputeOperatorHydrationStatus
3878            | IntrospectionType::ComputeMaterializedViewRefreshes
3879            | IntrospectionType::ComputeErrorCounts
3880            | IntrospectionType::ComputeHydrationTimes
3881            | IntrospectionType::ComputeObjectArrangementSizes => {
3882                CollectionManagerKind::Differential
3883            }
3884
3885            IntrospectionType::SourceStatusHistory
3886            | IntrospectionType::SinkStatusHistory
3887            | IntrospectionType::PrivatelinkConnectionStatusHistory
3888            | IntrospectionType::ReplicaStatusHistory
3889            | IntrospectionType::ReplicaMetricsHistory
3890            | IntrospectionType::WallclockLagHistory
3891            | IntrospectionType::WallclockLagHistogram
3892            | IntrospectionType::PreparedStatementHistory
3893            | IntrospectionType::StatementExecutionHistory
3894            | IntrospectionType::SessionHistory
3895            | IntrospectionType::StatementLifecycleHistory
3896            | IntrospectionType::SqlText => CollectionManagerKind::AppendOnly,
3897        }
3898    }
3899}
3900
3901/// Get the current rows in the given statistics table. This is used to bootstrap
3902/// the statistics tasks.
3903///
3904// TODO(guswynn): we need to be more careful about the update time we get here:
3905// <https://github.com/MaterializeInc/database-issues/issues/7564>
3906async fn snapshot_statistics(
3907    id: GlobalId,
3908    upper: Antichain<Timestamp>,
3909    storage_collections: &Arc<dyn StorageCollections + Send + Sync>,
3910) -> Vec<Row> {
3911    match upper.as_option() {
3912        Some(f) if f > &Timestamp::MIN => {
3913            let as_of = f.step_back().unwrap();
3914
3915            let snapshot = storage_collections.snapshot(id, as_of).await.unwrap();
3916            snapshot
3917                .into_iter()
3918                .map(|(row, diff)| {
3919                    assert_eq!(diff, 1);
3920                    row
3921                })
3922                .collect()
3923        }
3924        // If collection is closed or the frontier is the minimum, we cannot
3925        // or don't need to truncate (respectively).
3926        _ => Vec::new(),
3927    }
3928}
3929
3930async fn read_handle_for_snapshot(
3931    persist: &PersistClientCache,
3932    id: GlobalId,
3933    metadata: &CollectionMetadata,
3934) -> Result<ReadHandle<SourceData, (), Timestamp, StorageDiff>, StorageError> {
3935    let persist_client = persist
3936        .open(metadata.persist_location.clone())
3937        .await
3938        .unwrap();
3939
3940    // We create a new read handle every time someone requests a snapshot and then immediately
3941    // expire it instead of keeping a read handle permanently in our state to avoid having it
3942    // heartbeat continously. The assumption is that calls to snapshot are rare and therefore
3943    // worth it to always create a new handle.
3944    let read_handle = persist_client
3945        .open_leased_reader::<SourceData, (), _, _>(
3946            metadata.data_shard,
3947            Arc::new(metadata.relation_desc.clone()),
3948            Arc::new(UnitSchema),
3949            Diagnostics {
3950                shard_name: id.to_string(),
3951                handle_purpose: format!("snapshot {}", id),
3952            },
3953            USE_CRITICAL_SINCE_SNAPSHOT.get(persist_client.dyncfgs()),
3954        )
3955        .await
3956        .expect("invalid persist usage");
3957    Ok(read_handle)
3958}
3959
3960/// State maintained about individual collections.
3961#[derive(Debug)]
3962struct CollectionState {
3963    /// The source of this collection's data.
3964    pub data_source: DataSource,
3965
3966    pub collection_metadata: CollectionMetadata,
3967
3968    pub extra_state: CollectionStateExtra,
3969
3970    /// Maximum frontier wallclock lag since the last `WallclockLagHistory` introspection update.
3971    wallclock_lag_max: WallclockLag,
3972    /// Frontier wallclock lag measurements stashed until the next `WallclockLagHistogram`
3973    /// introspection update.
3974    ///
3975    /// Keys are `(period, lag, labels)` triples, values are counts.
3976    ///
3977    /// If this is `None`, wallclock lag is not tracked for this collection.
3978    wallclock_lag_histogram_stash: Option<
3979        BTreeMap<
3980            (
3981                WallclockLagHistogramPeriod,
3982                WallclockLag,
3983                BTreeMap<&'static str, String>,
3984            ),
3985            Diff,
3986        >,
3987    >,
3988    /// Frontier wallclock lag metrics tracked for this collection.
3989    wallclock_lag_metrics: WallclockLagMetrics,
3990}
3991
3992impl CollectionState {
3993    fn new(
3994        data_source: DataSource,
3995        collection_metadata: CollectionMetadata,
3996        extra_state: CollectionStateExtra,
3997        wallclock_lag_metrics: WallclockLagMetrics,
3998    ) -> Self {
3999        // Only collect wallclock lag histogram data for collections written by storage, to avoid
4000        // duplicate measurements. Collections written by other components (e.g. compute) have
4001        // their wallclock lags recorded by these components.
4002        let wallclock_lag_histogram_stash = match &data_source {
4003            DataSource::Other => None,
4004            _ => Some(Default::default()),
4005        };
4006
4007        Self {
4008            data_source,
4009            collection_metadata,
4010            extra_state,
4011            wallclock_lag_max: WallclockLag::MIN,
4012            wallclock_lag_histogram_stash,
4013            wallclock_lag_metrics,
4014        }
4015    }
4016}
4017
4018/// Additional state that the controller maintains for select collection types.
4019#[derive(Debug)]
4020enum CollectionStateExtra {
4021    Ingestion(IngestionState),
4022    Export(ExportState),
4023    None,
4024}
4025
4026/// State maintained about ingestions and ingestion exports
4027#[derive(Debug)]
4028struct IngestionState {
4029    /// Really only for keeping track of changes to the `derived_since`.
4030    pub read_capabilities: MutableAntichain<Timestamp>,
4031
4032    /// The current since frontier, derived from `write_frontier` using
4033    /// `hold_policy`.
4034    pub derived_since: Antichain<Timestamp>,
4035
4036    /// Holds that this ingestion (or ingestion export) has on its dependencies.
4037    pub dependency_read_holds: Vec<ReadHold>,
4038
4039    /// Reported write frontier.
4040    pub write_frontier: Antichain<Timestamp>,
4041
4042    /// The policy that drives how we downgrade our read hold. That is how we
4043    /// derive our since from our upper.
4044    ///
4045    /// This is a _storage-controller-internal_ policy used to derive its
4046    /// personal read hold on the collection. It should not be confused with any
4047    /// read policies that the adapter might install at [StorageCollections].
4048    pub hold_policy: ReadPolicy,
4049
4050    /// The ID of the instance in which the ingestion is running.
4051    pub instance_id: StorageInstanceId,
4052
4053    /// Set of replica IDs on which this ingestion is hydrated.
4054    pub hydrated_on: BTreeSet<ReplicaId>,
4055}
4056
4057/// A description of a status history collection.
4058///
4059/// Used to inform partial truncation, see
4060/// [`collection_mgmt::partially_truncate_status_history`].
4061struct StatusHistoryDesc<K> {
4062    retention_policy: StatusHistoryRetentionPolicy,
4063    extract_key: Box<dyn Fn(&[Datum]) -> K + Send>,
4064    extract_time: Box<dyn Fn(&[Datum]) -> CheckedTimestamp<DateTime<Utc>> + Send>,
4065}
4066enum StatusHistoryRetentionPolicy {
4067    // Truncates everything but the last N updates for each key.
4068    LastN(usize),
4069    // Truncates everything past the time window for each key.
4070    TimeWindow(Duration),
4071}
4072
4073fn source_status_history_desc(
4074    params: &StorageParameters,
4075) -> StatusHistoryDesc<(GlobalId, Option<ReplicaId>)> {
4076    let desc = &MZ_SOURCE_STATUS_HISTORY_DESC;
4077    let (source_id_idx, _) = desc.get_by_name(&"source_id".into()).expect("exists");
4078    let (replica_id_idx, _) = desc.get_by_name(&"replica_id".into()).expect("exists");
4079    let (time_idx, _) = desc.get_by_name(&"occurred_at".into()).expect("exists");
4080
4081    StatusHistoryDesc {
4082        retention_policy: StatusHistoryRetentionPolicy::LastN(
4083            params.keep_n_source_status_history_entries,
4084        ),
4085        extract_key: Box::new(move |datums| {
4086            (
4087                GlobalId::from_str(datums[source_id_idx].unwrap_str()).expect("GlobalId column"),
4088                if datums[replica_id_idx].is_null() {
4089                    None
4090                } else {
4091                    Some(
4092                        ReplicaId::from_str(datums[replica_id_idx].unwrap_str())
4093                            .expect("ReplicaId column"),
4094                    )
4095                },
4096            )
4097        }),
4098        extract_time: Box::new(move |datums| datums[time_idx].unwrap_timestamptz()),
4099    }
4100}
4101
4102fn sink_status_history_desc(
4103    params: &StorageParameters,
4104) -> StatusHistoryDesc<(GlobalId, Option<ReplicaId>)> {
4105    let desc = &MZ_SINK_STATUS_HISTORY_DESC;
4106    let (sink_id_idx, _) = desc.get_by_name(&"sink_id".into()).expect("exists");
4107    let (replica_id_idx, _) = desc.get_by_name(&"replica_id".into()).expect("exists");
4108    let (time_idx, _) = desc.get_by_name(&"occurred_at".into()).expect("exists");
4109
4110    StatusHistoryDesc {
4111        retention_policy: StatusHistoryRetentionPolicy::LastN(
4112            params.keep_n_sink_status_history_entries,
4113        ),
4114        extract_key: Box::new(move |datums| {
4115            (
4116                GlobalId::from_str(datums[sink_id_idx].unwrap_str()).expect("GlobalId column"),
4117                if datums[replica_id_idx].is_null() {
4118                    None
4119                } else {
4120                    Some(
4121                        ReplicaId::from_str(datums[replica_id_idx].unwrap_str())
4122                            .expect("ReplicaId column"),
4123                    )
4124                },
4125            )
4126        }),
4127        extract_time: Box::new(move |datums| datums[time_idx].unwrap_timestamptz()),
4128    }
4129}
4130
4131fn privatelink_status_history_desc(params: &StorageParameters) -> StatusHistoryDesc<GlobalId> {
4132    let desc = &MZ_AWS_PRIVATELINK_CONNECTION_STATUS_HISTORY_DESC;
4133    let (key_idx, _) = desc.get_by_name(&"connection_id".into()).expect("exists");
4134    let (time_idx, _) = desc.get_by_name(&"occurred_at".into()).expect("exists");
4135
4136    StatusHistoryDesc {
4137        retention_policy: StatusHistoryRetentionPolicy::LastN(
4138            params.keep_n_privatelink_status_history_entries,
4139        ),
4140        extract_key: Box::new(move |datums| {
4141            GlobalId::from_str(datums[key_idx].unwrap_str()).expect("GlobalId column")
4142        }),
4143        extract_time: Box::new(move |datums| datums[time_idx].unwrap_timestamptz()),
4144    }
4145}
4146
4147fn replica_status_history_desc(params: &StorageParameters) -> StatusHistoryDesc<(GlobalId, u64)> {
4148    let desc = &REPLICA_STATUS_HISTORY_DESC;
4149    let (replica_idx, _) = desc.get_by_name(&"replica_id".into()).expect("exists");
4150    let (process_idx, _) = desc.get_by_name(&"process_id".into()).expect("exists");
4151    let (time_idx, _) = desc.get_by_name(&"occurred_at".into()).expect("exists");
4152
4153    StatusHistoryDesc {
4154        retention_policy: StatusHistoryRetentionPolicy::TimeWindow(
4155            params.replica_status_history_retention_window,
4156        ),
4157        extract_key: Box::new(move |datums| {
4158            (
4159                GlobalId::from_str(datums[replica_idx].unwrap_str()).expect("GlobalId column"),
4160                datums[process_idx].unwrap_uint64(),
4161            )
4162        }),
4163        extract_time: Box::new(move |datums| datums[time_idx].unwrap_timestamptz()),
4164    }
4165}
4166
4167/// Replace one antichain with another, tracking the overall changes in the returned `ChangeBatch`.
4168fn swap_updates(
4169    from: &mut Antichain<Timestamp>,
4170    mut replace_with: Antichain<Timestamp>,
4171) -> ChangeBatch<Timestamp> {
4172    let mut update = ChangeBatch::new();
4173    if PartialOrder::less_equal(from, &replace_with) {
4174        update.extend(replace_with.iter().map(|time| (*time, 1)));
4175        std::mem::swap(from, &mut replace_with);
4176        update.extend(replace_with.iter().map(|time| (*time, -1)));
4177    }
4178    update
4179}