mz_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//! A representative of STORAGE and COMPUTE that maintains summaries of the involved objects.
11//!
12//! The `Controller` provides the ability to create and manipulate storage and compute instances.
13//! Each of Storage and Compute provide their own controllers, accessed through the `storage()`
14//! and `compute(instance_id)` methods. It is an error to access a compute instance before it has
15//! been created.
16//!
17//! The controller also provides a `recv()` method that returns responses from the storage and
18//! compute layers, which may remain of value to the interested user. With time, these responses
19//! may be thinned down in an effort to make the controller more self contained.
20//!
21//! Consult the `StorageController` and `ComputeController` documentation for more information
22//! about each of these interfaces.
23
24use std::collections::btree_map::Entry;
25use std::collections::{BTreeMap, BTreeSet};
26use std::mem;
27use std::num::NonZeroI64;
28use std::sync::Arc;
29use std::time::Duration;
30
31use futures::future::BoxFuture;
32use mz_build_info::BuildInfo;
33use mz_cluster_client::metrics::ControllerMetrics;
34use mz_cluster_client::{ReplicaId, WallclockLagFn};
35use mz_compute_client::controller::{
36    ComputeController, ComputeControllerResponse, ComputeControllerTimestamp, PeekNotification,
37};
38use mz_compute_client::protocol::response::SubscribeBatch;
39use mz_controller_types::WatchSetId;
40use mz_dyncfg::{ConfigSet, ConfigUpdates};
41use mz_orchestrator::{NamespacedOrchestrator, Orchestrator, ServiceProcessMetrics};
42use mz_ore::cast::CastFrom;
43use mz_ore::id_gen::Gen;
44use mz_ore::instrument;
45use mz_ore::metrics::MetricsRegistry;
46use mz_ore::now::{EpochMillis, NowFn};
47use mz_ore::task::AbortOnDropHandle;
48use mz_ore::tracing::OpenTelemetryContext;
49use mz_persist_client::PersistLocation;
50use mz_persist_client::cache::PersistClientCache;
51use mz_persist_types::Codec64;
52use mz_repr::{Datum, GlobalId, Row, TimestampManipulation};
53use mz_service::secrets::SecretsReaderCliArgs;
54use mz_storage_client::controller::{
55    IntrospectionType, StorageController, StorageMetadata, StorageTxn,
56};
57use mz_storage_client::storage_collections::{self, StorageCollections};
58use mz_storage_types::configuration::StorageConfiguration;
59use mz_storage_types::connections::ConnectionContext;
60use mz_storage_types::controller::StorageError;
61use mz_txn_wal::metrics::Metrics as TxnMetrics;
62use serde::Serialize;
63use timely::progress::{Antichain, Timestamp};
64use tokio::sync::mpsc;
65use uuid::Uuid;
66
67pub mod clusters;
68
69// Export this on behalf of the storage controller to provide a unified
70// interface, allowing other crates to depend on this crate alone.
71pub use mz_storage_controller::prepare_initialization;
72
73/// Configures a controller.
74#[derive(Debug, Clone)]
75pub struct ControllerConfig {
76    /// The build information for this process.
77    pub build_info: &'static BuildInfo,
78    /// The orchestrator implementation to use.
79    pub orchestrator: Arc<dyn Orchestrator>,
80    /// The persist location where all storage collections will be written to.
81    pub persist_location: PersistLocation,
82    /// A process-global cache of (blob_uri, consensus_uri) ->
83    /// PersistClient.
84    /// This is intentionally shared between workers.
85    pub persist_clients: Arc<PersistClientCache>,
86    /// The clusterd image to use when starting new cluster processes.
87    pub clusterd_image: String,
88    /// The init container image to use for clusterd.
89    pub init_container_image: Option<String>,
90    /// A number representing the environment's generation.
91    ///
92    /// This is incremented to request that the new process perform a graceful
93    /// transition of power from the prior generation.
94    pub deploy_generation: u64,
95    /// The now function to advance the controller's introspection collections.
96    pub now: NowFn,
97    /// The metrics registry.
98    pub metrics_registry: MetricsRegistry,
99    /// The URL for Persist PubSub.
100    pub persist_pubsub_url: String,
101    /// Arguments for secrets readers.
102    pub secrets_args: SecretsReaderCliArgs,
103    /// The connection context, to thread through to clusterd, with cli flags.
104    pub connection_context: ConnectionContext,
105}
106
107/// Responses that [`Controller`] can produce.
108#[derive(Debug)]
109pub enum ControllerResponse<T = mz_repr::Timestamp> {
110    /// Notification of a worker's response to a specified (by connection id) peek.
111    ///
112    /// Additionally, an `OpenTelemetryContext` to forward trace information
113    /// back into coord. This allows coord traces to be children of work
114    /// done in compute!
115    PeekNotification(Uuid, PeekNotification, OpenTelemetryContext),
116    /// The worker's next response to a specified subscribe.
117    SubscribeResponse(GlobalId, SubscribeBatch<T>),
118    /// The worker's next response to a specified copy to.
119    CopyToResponse(GlobalId, Result<u64, anyhow::Error>),
120    /// Notification that a watch set has finished. See
121    /// [`Controller::install_compute_watch_set`] and
122    /// [`Controller::install_storage_watch_set`] for details.
123    WatchSetFinished(Vec<WatchSetId>),
124}
125
126/// Whether one of the underlying controllers is ready for their `process`
127/// method to be called.
128#[derive(Debug, Default)]
129pub enum Readiness<T> {
130    /// No underlying controllers are ready.
131    #[default]
132    NotReady,
133    /// The storage controller is ready.
134    Storage,
135    /// The compute controller is ready.
136    Compute,
137    /// A batch of metric data is ready.
138    Metrics((ReplicaId, Vec<ServiceProcessMetrics>)),
139    /// An internally-generated message is ready to be returned.
140    Internal(ControllerResponse<T>),
141}
142
143/// A client that maintains soft state and validates commands, in addition to forwarding them.
144pub struct Controller<T: ComputeControllerTimestamp = mz_repr::Timestamp> {
145    pub storage: Box<dyn StorageController<Timestamp = T>>,
146    pub storage_collections: Arc<dyn StorageCollections<Timestamp = T> + Send + Sync>,
147    pub compute: ComputeController<T>,
148    /// The clusterd image to use when starting new cluster processes.
149    clusterd_image: String,
150    /// The init container image to use for clusterd.
151    init_container_image: Option<String>,
152    /// A number representing the environment's generation.
153    deploy_generation: u64,
154    /// Whether or not this controller is in read-only mode.
155    ///
156    /// When in read-only mode, neither this controller nor the instances
157    /// controlled by it are allowed to affect changes to external systems
158    /// (largely persist).
159    read_only: bool,
160    /// The cluster orchestrator.
161    orchestrator: Arc<dyn NamespacedOrchestrator>,
162    /// Tracks the readiness of the underlying controllers.
163    readiness: Readiness<T>,
164    /// Tasks for collecting replica metrics.
165    metrics_tasks: BTreeMap<ReplicaId, AbortOnDropHandle<()>>,
166    /// Sender for the channel over which replica metrics are sent.
167    metrics_tx: mpsc::UnboundedSender<(ReplicaId, Vec<ServiceProcessMetrics>)>,
168    /// Receiver for the channel over which replica metrics are sent.
169    metrics_rx: mpsc::UnboundedReceiver<(ReplicaId, Vec<ServiceProcessMetrics>)>,
170    /// A function providing the current wallclock time.
171    now: NowFn,
172
173    /// The URL for Persist PubSub.
174    persist_pubsub_url: String,
175
176    /// Arguments for secrets readers.
177    secrets_args: SecretsReaderCliArgs,
178
179    /// A map associating a global ID to the set of all the unfulfilled watch
180    /// set ids that include it.
181    ///
182    /// See [`Controller::install_compute_watch_set`]/[`Controller::install_storage_watch_set`] for a description of watch sets.
183    // When a watch set is fulfilled for a given object (that is, when
184    // the object's frontier advances to at least the watch set's
185    // timestamp), the corresponding entry will be removed from the set.
186    unfulfilled_watch_sets_by_object: BTreeMap<GlobalId, BTreeSet<WatchSetId>>,
187    /// A map of installed watch sets indexed by id.
188    unfulfilled_watch_sets: BTreeMap<WatchSetId, (BTreeSet<GlobalId>, T)>,
189    /// A sequence of numbers used to mint unique WatchSetIds.
190    watch_set_id_gen: Gen<WatchSetId>,
191
192    /// A list of watch sets that were already fulfilled as soon as
193    /// they were installed, and thus that must be returned to the
194    /// client on the next call to [`Controller::process_compute_response`]/[`Controller::process_storage_response`].
195    ///
196    /// See [`Controller::install_compute_watch_set`]/[`Controller::install_storage_watch_set`] for a description of watch sets.
197    immediate_watch_sets: Vec<WatchSetId>,
198
199    /// Dynamic system configuration.
200    dyncfg: ConfigSet,
201}
202
203impl<T: ComputeControllerTimestamp> Controller<T> {
204    /// Update the controller configuration.
205    pub fn update_configuration(&mut self, updates: ConfigUpdates) {
206        updates.apply(&self.dyncfg);
207    }
208
209    /// Start sinking the compute controller's introspection data into storage.
210    ///
211    /// This method should be called once the introspection collections have been registered with
212    /// the storage controller. It will panic if invoked earlier than that.
213    pub fn start_compute_introspection_sink(&mut self) {
214        self.compute.start_introspection_sink(&*self.storage);
215    }
216
217    /// Returns the connection context installed in the controller.
218    ///
219    /// This is purely a helper, and can be obtained from `self.storage`.
220    pub fn connection_context(&self) -> &ConnectionContext {
221        &self.storage.config().connection_context
222    }
223
224    /// Returns the storage configuration installed in the storage controller.
225    ///
226    /// This is purely a helper, and can be obtained from `self.storage`.
227    pub fn storage_configuration(&self) -> &StorageConfiguration {
228        self.storage.config()
229    }
230
231    /// Returns the state of the [`Controller`] formatted as JSON.
232    ///
233    /// The returned value is not guaranteed to be stable and may change at any point in time.
234    pub async fn dump(&self) -> Result<serde_json::Value, anyhow::Error> {
235        // Note: We purposefully use the `Debug` formatting for the value of all fields in the
236        // returned object as a tradeoff between usability and stability. `serde_json` will fail
237        // to serialize an object if the keys aren't strings, so `Debug` formatting the values
238        // prevents a future unrelated change from silently breaking this method.
239
240        // Destructure `self` here so we don't forget to consider dumping newly added fields.
241        let Self {
242            storage_collections: _,
243            storage: _,
244            compute,
245            clusterd_image: _,
246            init_container_image: _,
247            deploy_generation,
248            read_only,
249            orchestrator: _,
250            readiness,
251            metrics_tasks: _,
252            metrics_tx: _,
253            metrics_rx: _,
254            now: _,
255            persist_pubsub_url: _,
256            secrets_args: _,
257            unfulfilled_watch_sets_by_object: _,
258            unfulfilled_watch_sets,
259            watch_set_id_gen: _,
260            immediate_watch_sets,
261            dyncfg: _,
262        } = self;
263
264        let compute = compute.dump().await?;
265
266        let unfulfilled_watch_sets: BTreeMap<_, _> = unfulfilled_watch_sets
267            .iter()
268            .map(|(ws_id, watches)| (format!("{ws_id:?}"), format!("{watches:?}")))
269            .collect();
270        let immediate_watch_sets: Vec<_> = immediate_watch_sets
271            .iter()
272            .map(|watch| format!("{watch:?}"))
273            .collect();
274
275        fn field(
276            key: &str,
277            value: impl Serialize,
278        ) -> Result<(String, serde_json::Value), anyhow::Error> {
279            let value = serde_json::to_value(value)?;
280            Ok((key.to_string(), value))
281        }
282
283        let map = serde_json::Map::from_iter([
284            field("compute", compute)?,
285            field("deploy_generation", deploy_generation)?,
286            field("read_only", read_only)?,
287            field("readiness", format!("{readiness:?}"))?,
288            field("unfulfilled_watch_sets", unfulfilled_watch_sets)?,
289            field("immediate_watch_sets", immediate_watch_sets)?,
290        ]);
291        Ok(serde_json::Value::Object(map))
292    }
293}
294
295impl<T> Controller<T>
296where
297    T: ComputeControllerTimestamp,
298{
299    pub fn update_orchestrator_scheduling_config(
300        &self,
301        config: mz_orchestrator::scheduling_config::ServiceSchedulingConfig,
302    ) {
303        self.orchestrator.update_scheduling_config(config);
304    }
305    /// Marks the end of any initialization commands.
306    ///
307    /// The implementor may wait for this method to be called before implementing prior commands,
308    /// and so it is important for a user to invoke this method as soon as it is comfortable.
309    /// This method can be invoked immediately, at the potential expense of performance.
310    pub fn initialization_complete(&mut self) {
311        self.storage.initialization_complete();
312        self.compute.initialization_complete();
313    }
314
315    /// Reports whether the controller is in read only mode.
316    pub fn read_only(&self) -> bool {
317        self.read_only
318    }
319
320    /// Returns `Some` if there is an immediately available
321    /// internally-generated response that we need to return to the
322    /// client (as opposed to waiting for a response from compute or storage).
323    fn take_internal_response(&mut self) -> Option<ControllerResponse<T>> {
324        let ws = std::mem::take(&mut self.immediate_watch_sets);
325        (!ws.is_empty()).then_some(ControllerResponse::WatchSetFinished(ws))
326    }
327
328    /// Waits until the controller is ready to process a response.
329    ///
330    /// This method may block for an arbitrarily long time.
331    ///
332    /// When the method returns, the owner should call [`Controller::ready`] to
333    /// process the ready message.
334    ///
335    /// This method is cancellation safe.
336    pub async fn ready(&mut self) {
337        if let Readiness::NotReady = self.readiness {
338            // the coordinator wants to be able to make a simple
339            // sequence of ready, process, ready, process, .... calls,
340            // but the controller sometimes has responses immediately
341            // ready to be processed and should do so before calling
342            // into either of the lower-level controllers. This `if`
343            // statement handles that case.
344            if let Some(response) = self.take_internal_response() {
345                self.readiness = Readiness::Internal(response);
346            } else {
347                // The underlying `ready` methods are cancellation safe, so it is
348                // safe to construct this `select!`.
349                tokio::select! {
350                    () = self.storage.ready() => {
351                        self.readiness = Readiness::Storage;
352                    }
353                    () = self.compute.ready() => {
354                        self.readiness = Readiness::Compute;
355                    }
356                    Some(metrics) = self.metrics_rx.recv() => {
357                        self.readiness = Readiness::Metrics(metrics);
358                    }
359                }
360            }
361        }
362    }
363
364    /// Returns the [Readiness] status of this controller.
365    pub fn get_readiness(&self) -> &Readiness<T> {
366        &self.readiness
367    }
368
369    /// Install a _watch set_ in the controller.
370    ///
371    /// A _watch set_ is a request to be informed by the controller when
372    /// all of the frontiers of a particular set of objects have advanced at
373    /// least to a particular timestamp.
374    ///
375    /// When all the objects in `objects` have advanced to `t`, the watchset id
376    /// is returned to the client on the next call to [`Self::process`].
377    pub fn install_compute_watch_set(
378        &mut self,
379        mut objects: BTreeSet<GlobalId>,
380        t: T,
381    ) -> WatchSetId {
382        let ws_id = self.watch_set_id_gen.allocate_id();
383
384        objects.retain(|id| {
385            let frontier = self
386                .compute
387                .collection_frontiers(*id, None)
388                .map(|f| f.write_frontier)
389                .expect("missing compute dependency");
390            frontier.less_equal(&t)
391        });
392        if objects.is_empty() {
393            self.immediate_watch_sets.push(ws_id);
394        } else {
395            for id in objects.iter() {
396                self.unfulfilled_watch_sets_by_object
397                    .entry(*id)
398                    .or_default()
399                    .insert(ws_id);
400            }
401            self.unfulfilled_watch_sets.insert(ws_id, (objects, t));
402        }
403
404        ws_id
405    }
406
407    /// Install a _watch set_ in the controller.
408    ///
409    /// A _watch set_ is a request to be informed by the controller when
410    /// all of the frontiers of a particular set of objects have advanced at
411    /// least to a particular timestamp.
412    ///
413    /// When all the objects in `objects` have advanced to `t`, the watchset id
414    /// is returned to the client on the next call to [`Self::process`].
415    pub fn install_storage_watch_set(
416        &mut self,
417        mut objects: BTreeSet<GlobalId>,
418        t: T,
419    ) -> WatchSetId {
420        let ws_id = self.watch_set_id_gen.allocate_id();
421
422        let uppers = self
423            .storage
424            .collections_frontiers(objects.iter().cloned().collect())
425            .expect("missing storage dependencies")
426            .into_iter()
427            .map(|(id, _since, upper)| (id, upper))
428            .collect::<BTreeMap<_, _>>();
429
430        objects.retain(|id| {
431            let upper = uppers.get(id).expect("missing collection");
432            upper.less_equal(&t)
433        });
434        if objects.is_empty() {
435            self.immediate_watch_sets.push(ws_id);
436        } else {
437            for id in objects.iter() {
438                self.unfulfilled_watch_sets_by_object
439                    .entry(*id)
440                    .or_default()
441                    .insert(ws_id);
442            }
443            self.unfulfilled_watch_sets.insert(ws_id, (objects, t));
444        }
445        ws_id
446    }
447
448    /// Uninstalls a previously installed WatchSetId. The method is a no-op if the watch set has
449    /// already finished and therefore it's safe to call this function unconditionally.
450    ///
451    /// # Panics
452    /// This method panics if called with a WatchSetId that was never returned by the function.
453    pub fn uninstall_watch_set(&mut self, ws_id: &WatchSetId) {
454        if let Some((obj_ids, _)) = self.unfulfilled_watch_sets.remove(ws_id) {
455            for obj_id in obj_ids {
456                let mut entry = match self.unfulfilled_watch_sets_by_object.entry(obj_id) {
457                    Entry::Occupied(entry) => entry,
458                    Entry::Vacant(_) => panic!("corrupted watchset state"),
459                };
460                entry.get_mut().remove(ws_id);
461                if entry.get().is_empty() {
462                    entry.remove();
463                }
464            }
465        }
466    }
467
468    /// Process a pending response from the storage controller. If necessary,
469    /// return a higher-level response to our client.
470    fn process_storage_response(
471        &mut self,
472        storage_metadata: &StorageMetadata,
473    ) -> Result<Option<ControllerResponse<T>>, anyhow::Error> {
474        let maybe_response = self.storage.process(storage_metadata)?;
475        Ok(maybe_response.and_then(
476            |mz_storage_client::controller::Response::FrontierUpdates(r)| {
477                self.handle_frontier_updates(&r)
478            },
479        ))
480    }
481
482    /// Process a pending response from the compute controller. If necessary,
483    /// return a higher-level response to our client.
484    fn process_compute_response(&mut self) -> Result<Option<ControllerResponse<T>>, anyhow::Error> {
485        let response = self.compute.process();
486
487        let response = response.and_then(|r| match r {
488            ComputeControllerResponse::PeekNotification(uuid, peek, otel_ctx) => {
489                Some(ControllerResponse::PeekNotification(uuid, peek, otel_ctx))
490            }
491            ComputeControllerResponse::SubscribeResponse(id, tail) => {
492                Some(ControllerResponse::SubscribeResponse(id, tail))
493            }
494            ComputeControllerResponse::CopyToResponse(id, tail) => {
495                Some(ControllerResponse::CopyToResponse(id, tail))
496            }
497            ComputeControllerResponse::FrontierUpper { id, upper } => {
498                self.handle_frontier_updates(&[(id, upper)])
499            }
500        });
501        Ok(response)
502    }
503
504    /// Processes the work queued by [`Controller::ready`].
505    ///
506    /// This method is guaranteed to return "quickly" unless doing so would
507    /// compromise the correctness of the system.
508    ///
509    /// This method is **not** guaranteed to be cancellation safe. It **must**
510    /// be awaited to completion.
511    #[mz_ore::instrument(level = "debug")]
512    pub fn process(
513        &mut self,
514        storage_metadata: &StorageMetadata,
515    ) -> Result<Option<ControllerResponse<T>>, anyhow::Error> {
516        match mem::take(&mut self.readiness) {
517            Readiness::NotReady => Ok(None),
518            Readiness::Storage => self.process_storage_response(storage_metadata),
519            Readiness::Compute => self.process_compute_response(),
520            Readiness::Metrics((id, metrics)) => self.process_replica_metrics(id, metrics),
521            Readiness::Internal(message) => Ok(Some(message)),
522        }
523    }
524
525    /// Record updates to frontiers, and propagate any necessary responses.
526    /// As of this writing (2/29/2024), the only response that can be generated
527    /// from a frontier update is `WatchSetCompleted`.
528    fn handle_frontier_updates(
529        &mut self,
530        updates: &[(GlobalId, Antichain<T>)],
531    ) -> Option<ControllerResponse<T>> {
532        let mut finished = vec![];
533        for (obj_id, antichain) in updates {
534            let ws_ids = self.unfulfilled_watch_sets_by_object.entry(*obj_id);
535            if let Entry::Occupied(mut ws_ids) = ws_ids {
536                ws_ids.get_mut().retain(|ws_id| {
537                    let mut entry = match self.unfulfilled_watch_sets.entry(*ws_id) {
538                        Entry::Occupied(entry) => entry,
539                        Entry::Vacant(_) => panic!("corrupted watchset state"),
540                    };
541                    // If this object has made more progress than required by this watchset we:
542                    if !antichain.less_equal(&entry.get().1) {
543                        // 1. Remove the object from the set of pending objects for the watchset
544                        entry.get_mut().0.remove(obj_id);
545                        // 2. Mark the watchset as finished if this was the last watched object
546                        if entry.get().0.is_empty() {
547                            entry.remove();
548                            finished.push(*ws_id);
549                        }
550                        // 3. Remove the watchset from the set of pending watchsets for the object
551                        false
552                    } else {
553                        // Otherwise we keep the watchset around to re-check in the future
554                        true
555                    }
556                });
557                // Clear the entry if this was the last watchset that was interested in obj_id
558                if ws_ids.get().is_empty() {
559                    ws_ids.remove();
560                }
561            }
562        }
563        (!(finished.is_empty())).then(|| ControllerResponse::WatchSetFinished(finished))
564    }
565
566    fn process_replica_metrics(
567        &mut self,
568        id: ReplicaId,
569        metrics: Vec<ServiceProcessMetrics>,
570    ) -> Result<Option<ControllerResponse<T>>, anyhow::Error> {
571        self.record_replica_metrics(id, &metrics);
572        Ok(None)
573    }
574
575    fn record_replica_metrics(&mut self, replica_id: ReplicaId, metrics: &[ServiceProcessMetrics]) {
576        if self.read_only() {
577            return;
578        }
579
580        let now = mz_ore::now::to_datetime((self.now)());
581        let now_tz = now.try_into().expect("must fit");
582
583        let replica_id = replica_id.to_string();
584        let mut row = Row::default();
585        let updates = metrics
586            .iter()
587            .enumerate()
588            .map(|(process_id, m)| {
589                row.packer().extend(&[
590                    Datum::String(&replica_id),
591                    Datum::UInt64(u64::cast_from(process_id)),
592                    m.cpu_nano_cores.into(),
593                    m.memory_bytes.into(),
594                    m.disk_usage_bytes.into(),
595                    Datum::TimestampTz(now_tz),
596                ]);
597                (row.clone(), mz_repr::Diff::ONE)
598            })
599            .collect();
600
601        self.storage
602            .append_introspection_updates(IntrospectionType::ReplicaMetricsHistory, updates);
603    }
604
605    /// Determine the "real-time recency" timestamp for all `ids`.
606    ///
607    /// Real-time recency is defined as the minimum value of `T` that all
608    /// objects can be queried at to return all data visible in the upstream
609    /// system the query was issued. In this case, "the upstream systems" are
610    /// any user sources that connect to objects outside of Materialize, such as
611    /// Kafka sources.
612    ///
613    /// If no items in `ids` connect to external systems, this function will
614    /// return `Ok(T::minimum)`.
615    pub async fn determine_real_time_recent_timestamp(
616        &self,
617        ids: BTreeSet<GlobalId>,
618        timeout: Duration,
619    ) -> Result<BoxFuture<'static, Result<T, StorageError<T>>>, StorageError<T>> {
620        self.storage.real_time_recent_timestamp(ids, timeout).await
621    }
622}
623
624impl<T> Controller<T>
625where
626    // Bounds needed by `StorageController` and/or `Controller`:
627    T: Timestamp
628        + Codec64
629        + From<EpochMillis>
630        + TimestampManipulation
631        + std::fmt::Display
632        + Into<mz_repr::Timestamp>,
633    // Bounds needed by `ComputeController`:
634    T: ComputeControllerTimestamp,
635{
636    /// Creates a new controller.
637    ///
638    /// For correctness, this function expects to have access to the mutations
639    /// to the `storage_txn` that occurred in [`prepare_initialization`].
640    ///
641    /// # Panics
642    /// If this function is called before [`prepare_initialization`].
643    #[instrument(name = "controller::new")]
644    pub async fn new(
645        config: ControllerConfig,
646        envd_epoch: NonZeroI64,
647        read_only: bool,
648        storage_txn: &dyn StorageTxn<T>,
649    ) -> Self {
650        if read_only {
651            tracing::info!("starting controllers in read-only mode!");
652        }
653
654        let now_fn = config.now.clone();
655        let wallclock_lag_fn = WallclockLagFn::new(now_fn);
656
657        let controller_metrics = ControllerMetrics::new(&config.metrics_registry);
658
659        let txns_metrics = Arc::new(TxnMetrics::new(&config.metrics_registry));
660        let collections_ctl = storage_collections::StorageCollectionsImpl::new(
661            config.persist_location.clone(),
662            Arc::clone(&config.persist_clients),
663            &config.metrics_registry,
664            config.now.clone(),
665            Arc::clone(&txns_metrics),
666            envd_epoch,
667            read_only,
668            config.connection_context.clone(),
669            storage_txn,
670        )
671        .await;
672
673        let collections_ctl: Arc<dyn StorageCollections<Timestamp = T> + Send + Sync> =
674            Arc::new(collections_ctl);
675
676        let storage_controller = mz_storage_controller::Controller::new(
677            config.build_info,
678            config.persist_location.clone(),
679            config.persist_clients,
680            config.now.clone(),
681            wallclock_lag_fn.clone(),
682            Arc::clone(&txns_metrics),
683            read_only,
684            &config.metrics_registry,
685            controller_metrics.clone(),
686            config.connection_context,
687            storage_txn,
688            Arc::clone(&collections_ctl),
689        )
690        .await;
691
692        let storage_collections = Arc::clone(&collections_ctl);
693        let compute_controller = ComputeController::new(
694            config.build_info,
695            storage_collections,
696            read_only,
697            &config.metrics_registry,
698            config.persist_location,
699            controller_metrics,
700            config.now.clone(),
701            wallclock_lag_fn,
702        );
703        let (metrics_tx, metrics_rx) = mpsc::unbounded_channel();
704
705        let this = Self {
706            storage: Box::new(storage_controller),
707            storage_collections: collections_ctl,
708            compute: compute_controller,
709            clusterd_image: config.clusterd_image,
710            init_container_image: config.init_container_image,
711            deploy_generation: config.deploy_generation,
712            read_only,
713            orchestrator: config.orchestrator.namespace("cluster"),
714            readiness: Readiness::NotReady,
715            metrics_tasks: BTreeMap::new(),
716            metrics_tx,
717            metrics_rx,
718            now: config.now,
719            persist_pubsub_url: config.persist_pubsub_url,
720            secrets_args: config.secrets_args,
721            unfulfilled_watch_sets_by_object: BTreeMap::new(),
722            unfulfilled_watch_sets: BTreeMap::new(),
723            watch_set_id_gen: Gen::default(),
724            immediate_watch_sets: Vec::new(),
725            dyncfg: mz_dyncfgs::all_dyncfgs(),
726        };
727
728        if !this.read_only {
729            this.remove_past_generation_replicas_in_background();
730        }
731
732        this
733    }
734}