1mod builtin_schema_migration;
13
14use std::collections::{BTreeMap, BTreeSet};
15use std::num::NonZeroU32;
16use std::sync::Arc;
17use std::time::{Duration, Instant};
18
19use futures::future::{BoxFuture, FutureExt};
20use itertools::{Either, Itertools};
21use mz_adapter_types::bootstrap_builtin_cluster_config::BootstrapBuiltinClusterConfig;
22use mz_adapter_types::dyncfgs::{ENABLE_CONTINUAL_TASK_BUILTINS, ENABLE_EXPRESSION_CACHE};
23use mz_audit_log::{
24 CreateOrDropClusterReplicaReasonV1, EventDetails, EventType, ObjectType, VersionedEvent,
25};
26use mz_auth::hash::scram256_hash;
27use mz_catalog::SYSTEM_CONN_ID;
28use mz_catalog::builtin::{
29 BUILTIN_CLUSTER_REPLICAS, BUILTIN_CLUSTERS, BUILTIN_PREFIXES, BUILTIN_ROLES, BUILTINS, Builtin,
30 Fingerprint, MZ_CATALOG_RAW, RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL,
31};
32use mz_catalog::config::StateConfig;
33use mz_catalog::durable::objects::{
34 SystemObjectDescription, SystemObjectMapping, SystemObjectUniqueIdentifier,
35};
36use mz_catalog::durable::{ClusterReplica, ClusterVariant, ClusterVariantManaged, Transaction};
37use mz_catalog::expr_cache::{
38 ExpressionCacheConfig, ExpressionCacheHandle, GlobalExpressions, LocalExpressions,
39};
40use mz_catalog::memory::error::{Error, ErrorKind};
41use mz_catalog::memory::objects::{
42 BootstrapStateUpdateKind, CommentsMap, DefaultPrivileges, RoleAuth, StateUpdate,
43};
44use mz_controller::clusters::ReplicaLogging;
45use mz_controller_types::ClusterId;
46use mz_ore::cast::usize_to_u64;
47use mz_ore::collections::HashSet;
48use mz_ore::now::{SYSTEM_TIME, to_datetime};
49use mz_ore::{instrument, soft_assert_no_log};
50use mz_repr::adt::mz_acl_item::PrivilegeMap;
51use mz_repr::namespaces::is_unstable_schema;
52use mz_repr::{CatalogItemId, Diff, GlobalId, Timestamp};
53use mz_sql::catalog::{
54 BuiltinsConfig, CatalogError as SqlCatalogError, CatalogItemType, RoleMembership, RoleVars,
55};
56use mz_sql::func::OP_IMPLS;
57use mz_sql::names::CommentObjectId;
58use mz_sql::rbac;
59use mz_sql::session::user::{MZ_SYSTEM_ROLE_ID, SYSTEM_USER};
60use mz_sql::session::vars::{SessionVars, SystemVars, VarError, VarInput};
61use mz_storage_client::controller::{StorageMetadata, StorageTxn};
62use mz_storage_client::storage_collections::StorageCollections;
63use tracing::{Instrument, info, warn};
64use uuid::Uuid;
65
66use crate::AdapterError;
68use crate::catalog::migrate::{self, get_migration_version, set_migration_version};
69use crate::catalog::state::LocalExpressionCache;
70use crate::catalog::{
71 BuiltinTableUpdate, Catalog, CatalogPlans, CatalogState, Config, is_reserved_name,
72};
73
74pub struct InitializeStateResult {
75 pub state: CatalogState,
77 pub migrated_storage_collections_0dt: BTreeSet<CatalogItemId>,
79 pub new_builtin_collections: BTreeSet<GlobalId>,
81 pub builtin_table_updates: Vec<BuiltinTableUpdate>,
83 pub last_seen_version: String,
85 pub expr_cache_handle: Option<ExpressionCacheHandle>,
87 pub cached_global_exprs: BTreeMap<GlobalId, GlobalExpressions>,
89 pub uncached_local_exprs: BTreeMap<GlobalId, LocalExpressions>,
91}
92
93pub struct OpenCatalogResult {
94 pub catalog: Catalog,
96 pub migrated_storage_collections_0dt: BTreeSet<CatalogItemId>,
98 pub new_builtin_collections: BTreeSet<GlobalId>,
100 pub builtin_table_updates: Vec<BuiltinTableUpdate>,
102 pub cached_global_exprs: BTreeMap<GlobalId, GlobalExpressions>,
104 pub uncached_local_exprs: BTreeMap<GlobalId, LocalExpressions>,
106}
107
108impl Catalog {
109 pub async fn initialize_state<'a>(
113 config: StateConfig,
114 storage: &'a mut Box<dyn mz_catalog::durable::DurableCatalogState>,
115 ) -> Result<InitializeStateResult, AdapterError> {
116 for builtin_role in BUILTIN_ROLES {
117 assert!(
118 is_reserved_name(builtin_role.name),
119 "builtin role {builtin_role:?} must start with one of the following prefixes {}",
120 BUILTIN_PREFIXES.join(", ")
121 );
122 }
123 for builtin_cluster in BUILTIN_CLUSTERS {
124 assert!(
125 is_reserved_name(builtin_cluster.name),
126 "builtin cluster {builtin_cluster:?} must start with one of the following prefixes {}",
127 BUILTIN_PREFIXES.join(", ")
128 );
129 }
130
131 let mut system_configuration = SystemVars::new().set_unsafe(config.unsafe_mode);
132 if config.all_features {
133 system_configuration.enable_all_feature_flags_by_default();
134 }
135
136 let mut state = CatalogState {
137 database_by_name: imbl::OrdMap::new(),
138 database_by_id: imbl::OrdMap::new(),
139 entry_by_id: imbl::OrdMap::new(),
140 entry_by_global_id: imbl::OrdMap::new(),
141 ambient_schemas_by_name: imbl::OrdMap::new(),
142 ambient_schemas_by_id: imbl::OrdMap::new(),
143 clusters_by_name: imbl::OrdMap::new(),
144 clusters_by_id: imbl::OrdMap::new(),
145 roles_by_name: imbl::OrdMap::new(),
146 roles_by_id: imbl::OrdMap::new(),
147 network_policies_by_id: imbl::OrdMap::new(),
148 role_auth_by_id: imbl::OrdMap::new(),
149 network_policies_by_name: imbl::OrdMap::new(),
150 system_configuration: Arc::new(system_configuration),
151 default_privileges: Arc::new(DefaultPrivileges::default()),
152 system_privileges: Arc::new(PrivilegeMap::default()),
153 comments: Arc::new(CommentsMap::default()),
154 source_references: imbl::OrdMap::new(),
155 storage_metadata: Arc::new(StorageMetadata::default()),
156 temporary_schemas: imbl::OrdMap::new(),
157 mock_authentication_nonce: Default::default(),
158 config: mz_sql::catalog::CatalogConfig {
159 start_time: to_datetime((config.now)()),
160 start_instant: Instant::now(),
161 nonce: rand::random(),
162 environment_id: config.environment_id,
163 session_id: Uuid::new_v4(),
164 build_info: config.build_info,
165 now: config.now.clone(),
166 connection_context: config.connection_context,
167 builtins_cfg: BuiltinsConfig {
168 include_continual_tasks: get_dyncfg_val_from_defaults_and_remote(
172 &config.system_parameter_defaults,
173 config.remote_system_parameters.as_ref(),
174 &ENABLE_CONTINUAL_TASK_BUILTINS,
175 ),
176 },
177 helm_chart_version: config.helm_chart_version,
178 },
179 cluster_replica_sizes: config.cluster_replica_sizes,
180 availability_zones: config.availability_zones,
181 egress_addresses: config.egress_addresses,
182 aws_principal_context: config.aws_principal_context,
183 aws_privatelink_availability_zones: config.aws_privatelink_availability_zones,
184 http_host_name: config.http_host_name,
185 license_key: config.license_key,
186 };
187
188 let deploy_generation = storage.get_deployment_generation().await?;
189
190 let mut updates: Vec<_> = storage.sync_to_current_updates().await?;
191 assert!(!updates.is_empty(), "initial catalog snapshot is missing");
192 let mut txn = storage.transaction().await?;
193
194 let new_builtin_collections = {
196 migrate::durable_migrate(
197 &mut txn,
198 state.config.environment_id.organization_id(),
199 config.boot_ts,
200 )?;
201 if let Some(remote_system_parameters) = config.remote_system_parameters {
204 for (name, value) in remote_system_parameters {
205 txn.upsert_system_config(&name, value)?;
206 }
207 txn.set_system_config_synced_once()?;
208 }
209 let new_builtin_collections =
211 add_new_remove_old_builtin_items_migration(&state.config().builtins_cfg, &mut txn)?;
212 let builtin_bootstrap_cluster_config_map = BuiltinBootstrapClusterConfigMap {
213 system_cluster: config.builtin_system_cluster_config,
214 catalog_server_cluster: config.builtin_catalog_server_cluster_config,
215 probe_cluster: config.builtin_probe_cluster_config,
216 support_cluster: config.builtin_support_cluster_config,
217 analytics_cluster: config.builtin_analytics_cluster_config,
218 };
219 add_new_remove_old_builtin_clusters_migration(
220 &mut txn,
221 &builtin_bootstrap_cluster_config_map,
222 )?;
223 add_new_remove_old_builtin_introspection_source_migration(&mut txn)?;
224 add_new_remove_old_builtin_cluster_replicas_migration(
225 &mut txn,
226 &builtin_bootstrap_cluster_config_map,
227 )?;
228 add_new_remove_old_builtin_roles_migration(&mut txn)?;
229 remove_invalid_config_param_role_defaults_migration(&mut txn)?;
230 remove_pending_cluster_replicas_migration(&mut txn, config.boot_ts)?;
231
232 new_builtin_collections
233 };
234
235 let op_updates = txn.get_and_commit_op_updates();
236 updates.extend(op_updates);
237
238 let mut builtin_table_updates = Vec::new();
239
240 {
242 for (name, value) in config.system_parameter_defaults {
245 match state.set_system_configuration_default(&name, VarInput::Flat(&value)) {
246 Ok(_) => (),
247 Err(Error {
248 kind: ErrorKind::VarError(VarError::UnknownParameter(name)),
249 }) => {
250 warn!(%name, "cannot load unknown system parameter from catalog storage to set default parameter");
251 }
252 Err(e) => return Err(e.into()),
253 };
254 }
255 state.create_temporary_schema(&SYSTEM_CONN_ID, MZ_SYSTEM_ROLE_ID)?;
256 }
257
258 let mut updates = into_consolidatable_updates_startup(updates, config.boot_ts);
261 differential_dataflow::consolidation::consolidate_updates(&mut updates);
262 soft_assert_no_log!(
263 updates.iter().all(|(_, _, diff)| *diff == Diff::ONE),
264 "consolidated updates should be positive during startup: {updates:?}"
265 );
266
267 let mut pre_item_updates = Vec::new();
268 let mut system_item_updates = Vec::new();
269 let mut item_updates = Vec::new();
270 let mut post_item_updates = Vec::new();
271 let mut audit_log_updates = Vec::new();
272 for (kind, ts, diff) in updates {
273 match kind {
274 BootstrapStateUpdateKind::Role(_)
275 | BootstrapStateUpdateKind::RoleAuth(_)
276 | BootstrapStateUpdateKind::Database(_)
277 | BootstrapStateUpdateKind::Schema(_)
278 | BootstrapStateUpdateKind::DefaultPrivilege(_)
279 | BootstrapStateUpdateKind::SystemPrivilege(_)
280 | BootstrapStateUpdateKind::SystemConfiguration(_)
281 | BootstrapStateUpdateKind::Cluster(_)
282 | BootstrapStateUpdateKind::NetworkPolicy(_)
283 | BootstrapStateUpdateKind::ClusterReplica(_) => {
284 pre_item_updates.push(StateUpdate {
285 kind: kind.into(),
286 ts,
287 diff: diff.try_into().expect("valid diff"),
288 })
289 }
290 BootstrapStateUpdateKind::IntrospectionSourceIndex(_)
291 | BootstrapStateUpdateKind::SystemObjectMapping(_) => {
292 system_item_updates.push(StateUpdate {
293 kind: kind.into(),
294 ts,
295 diff: diff.try_into().expect("valid diff"),
296 })
297 }
298 BootstrapStateUpdateKind::Item(_) => item_updates.push(StateUpdate {
299 kind: kind.into(),
300 ts,
301 diff: diff.try_into().expect("valid diff"),
302 }),
303 BootstrapStateUpdateKind::Comment(_)
304 | BootstrapStateUpdateKind::StorageCollectionMetadata(_)
305 | BootstrapStateUpdateKind::SourceReferences(_)
306 | BootstrapStateUpdateKind::UnfinalizedShard(_) => {
307 post_item_updates.push((kind, ts, diff));
308 }
309 BootstrapStateUpdateKind::AuditLog(_) => {
310 audit_log_updates.push(StateUpdate {
311 kind: kind.into(),
312 ts,
313 diff: diff.try_into().expect("valid diff"),
314 });
315 }
316 }
317 }
318
319 let (builtin_table_update, _catalog_updates) = state
320 .apply_updates(pre_item_updates, &mut LocalExpressionCache::Closed)
321 .await;
322 builtin_table_updates.extend(builtin_table_update);
323
324 {
328 if let Some(password) = config.external_login_password_mz_system {
329 let role_auth = RoleAuth {
330 role_id: MZ_SYSTEM_ROLE_ID,
331 password_hash: Some(
334 scram256_hash(&password, &NonZeroU32::new(600_000).expect("known valid"))
335 .map_err(|_| {
336 AdapterError::Internal("Failed to hash mz_system password.".to_owned())
337 })?,
338 ),
339 updated_at: SYSTEM_TIME(),
340 };
341 state
342 .role_auth_by_id
343 .insert(MZ_SYSTEM_ROLE_ID, role_auth.clone());
344 let builtin_table_update = state.generate_builtin_table_update(
345 mz_catalog::memory::objects::StateUpdateKind::RoleAuth(role_auth.into()),
346 mz_catalog::memory::objects::StateDiff::Addition,
347 );
348 builtin_table_updates.extend(builtin_table_update);
349 }
350 }
351
352 let expr_cache_start = Instant::now();
353 info!("startup: coordinator init: catalog open: expr cache open beginning");
354 let enable_expr_cache_dyncfg = ENABLE_EXPRESSION_CACHE.get(state.system_config().dyncfgs());
357 let expr_cache_enabled = config
358 .enable_expression_cache_override
359 .unwrap_or(enable_expr_cache_dyncfg);
360 let (expr_cache_handle, cached_local_exprs, cached_global_exprs) = if expr_cache_enabled {
361 info!(
362 ?config.enable_expression_cache_override,
363 ?enable_expr_cache_dyncfg,
364 "using expression cache for startup"
365 );
366 let current_ids = txn
367 .get_items()
368 .flat_map(|item| {
369 let gid = item.global_id.clone();
370 let gids: Vec<_> = item.extra_versions.values().cloned().collect();
371 std::iter::once(gid).chain(gids.into_iter())
372 })
373 .chain(
374 txn.get_system_object_mappings()
375 .map(|som| som.unique_identifier.global_id),
376 )
377 .collect();
378 let dyncfgs = config.persist_client.dyncfgs().clone();
379 let build_version = if config.build_info.is_dev() {
380 config
383 .build_info
384 .semver_version_build()
385 .expect("build ID is not available on your platform!")
386 } else {
387 config.build_info.semver_version()
388 };
389 let expr_cache_config = ExpressionCacheConfig {
390 build_version,
391 shard_id: txn
392 .get_expression_cache_shard()
393 .expect("expression cache shard should exist for opened catalogs"),
394 persist: config.persist_client,
395 current_ids,
396 remove_prior_versions: !config.read_only,
397 compact_shard: config.read_only,
398 dyncfgs,
399 };
400 let (expr_cache_handle, cached_local_exprs, cached_global_exprs) =
401 ExpressionCacheHandle::spawn_expression_cache(expr_cache_config).await;
402 (
403 Some(expr_cache_handle),
404 cached_local_exprs,
405 cached_global_exprs,
406 )
407 } else {
408 (None, BTreeMap::new(), BTreeMap::new())
409 };
410 let mut local_expr_cache = LocalExpressionCache::new(cached_local_exprs);
411 info!(
412 "startup: coordinator init: catalog open: expr cache open complete in {:?}",
413 expr_cache_start.elapsed()
414 );
415
416 let (builtin_table_update, _catalog_updates) = state
422 .apply_updates(system_item_updates, &mut local_expr_cache)
423 .await;
424 builtin_table_updates.extend(builtin_table_update);
425
426 let last_seen_version =
427 get_migration_version(&txn).map_or_else(|| "new".into(), |v| v.to_string());
428
429 let mz_authentication_mock_nonce =
430 txn.get_authentication_mock_nonce().ok_or_else(|| {
431 Error::new(ErrorKind::SettingError("authentication nonce".to_string()))
432 })?;
433
434 state.mock_authentication_nonce = Some(mz_authentication_mock_nonce);
435
436 let (builtin_table_update, _catalog_updates) = if !config.skip_migrations {
438 let migrate_result = migrate::migrate(
439 &mut state,
440 &mut txn,
441 &mut local_expr_cache,
442 item_updates,
443 config.now,
444 config.boot_ts,
445 )
446 .await
447 .map_err(|e| {
448 Error::new(ErrorKind::FailedCatalogMigration {
449 last_seen_version: last_seen_version.clone(),
450 this_version: config.build_info.version,
451 cause: e.to_string(),
452 })
453 })?;
454 if !migrate_result.post_item_updates.is_empty() {
455 post_item_updates.extend(migrate_result.post_item_updates);
458 if let Some(max_ts) = post_item_updates.iter().map(|(_, ts, _)| ts).max().cloned() {
460 for (_, ts, _) in &mut post_item_updates {
461 *ts = max_ts;
462 }
463 }
464 differential_dataflow::consolidation::consolidate_updates(&mut post_item_updates);
465 }
466
467 (
468 migrate_result.builtin_table_updates,
469 migrate_result.catalog_updates,
470 )
471 } else {
472 state
473 .apply_updates(item_updates, &mut local_expr_cache)
474 .await
475 };
476 builtin_table_updates.extend(builtin_table_update);
477
478 let post_item_updates = post_item_updates
479 .into_iter()
480 .map(|(kind, ts, diff)| StateUpdate {
481 kind: kind.into(),
482 ts,
483 diff: diff.try_into().expect("valid diff"),
484 })
485 .collect();
486 let (builtin_table_update, _catalog_updates) = state
487 .apply_updates(post_item_updates, &mut local_expr_cache)
488 .await;
489 builtin_table_updates.extend(builtin_table_update);
490
491 for audit_log_update in audit_log_updates {
495 builtin_table_updates.extend(
496 state.generate_builtin_table_update(audit_log_update.kind, audit_log_update.diff),
497 );
498 }
499
500 let schema_migration_result = builtin_schema_migration::run(
502 config.build_info,
503 deploy_generation,
504 &mut txn,
505 config.builtin_item_migration_config,
506 )
507 .await?;
508
509 let state_updates = txn.get_and_commit_op_updates();
510
511 let (table_updates, _catalog_updates) = state
517 .apply_updates(state_updates, &mut local_expr_cache)
518 .await;
519 builtin_table_updates.extend(table_updates);
520 let builtin_table_updates = state.resolve_builtin_table_updates(builtin_table_updates);
521
522 set_migration_version(&mut txn, config.build_info.semver_version())?;
524
525 txn.commit(config.boot_ts).await?;
526
527 schema_migration_result.cleanup_action.await;
529
530 Ok(InitializeStateResult {
531 state,
532 migrated_storage_collections_0dt: schema_migration_result.replaced_items,
533 new_builtin_collections: new_builtin_collections.into_iter().collect(),
534 builtin_table_updates,
535 last_seen_version,
536 expr_cache_handle,
537 cached_global_exprs,
538 uncached_local_exprs: local_expr_cache.into_uncached_exprs(),
539 })
540 }
541
542 #[instrument(name = "catalog::open")]
553 pub fn open(config: Config<'_>) -> BoxFuture<'static, Result<OpenCatalogResult, AdapterError>> {
554 async move {
555 let mut storage = config.storage;
556
557 let InitializeStateResult {
558 state,
559 migrated_storage_collections_0dt,
560 new_builtin_collections,
561 mut builtin_table_updates,
562 last_seen_version: _,
563 expr_cache_handle,
564 cached_global_exprs,
565 uncached_local_exprs,
566 } =
567 Self::initialize_state(config.state, &mut storage)
571 .instrument(tracing::info_span!("catalog::initialize_state"))
572 .boxed()
573 .await?;
574
575 let catalog = Catalog {
576 state,
577 plans: CatalogPlans::default(),
578 expr_cache_handle,
579 transient_revision: 1,
580 storage: Arc::new(tokio::sync::Mutex::new(storage)),
581 };
582
583 for (op, func) in OP_IMPLS.iter() {
586 match func {
587 mz_sql::func::Func::Scalar(impls) => {
588 for imp in impls {
589 builtin_table_updates.push(catalog.state.resolve_builtin_table_update(
590 catalog.state.pack_op_update(op, imp.details(), Diff::ONE),
591 ));
592 }
593 }
594 _ => unreachable!("all operators must be scalar functions"),
595 }
596 }
597
598 for ip in &catalog.state.egress_addresses {
599 builtin_table_updates.push(
600 catalog
601 .state
602 .resolve_builtin_table_update(catalog.state.pack_egress_ip_update(ip)?),
603 );
604 }
605
606 if !catalog.state.license_key.id.is_empty() {
607 builtin_table_updates.push(
608 catalog.state.resolve_builtin_table_update(
609 catalog
610 .state
611 .pack_license_key_update(&catalog.state.license_key)?,
612 ),
613 );
614 }
615
616 catalog.storage().await.mark_bootstrap_complete().await;
617
618 Ok(OpenCatalogResult {
619 catalog,
620 migrated_storage_collections_0dt,
621 new_builtin_collections,
622 builtin_table_updates,
623 cached_global_exprs,
624 uncached_local_exprs,
625 })
626 }
627 .instrument(tracing::info_span!("catalog::open"))
628 .boxed()
629 }
630
631 async fn initialize_storage_state(
638 &mut self,
639 storage_collections: &Arc<
640 dyn StorageCollections<Timestamp = mz_repr::Timestamp> + Send + Sync,
641 >,
642 ) -> Result<(), mz_catalog::durable::CatalogError> {
643 let collections = self
644 .entries()
645 .filter(|entry| entry.item().is_storage_collection())
646 .flat_map(|entry| entry.global_ids())
647 .collect();
648
649 let mut state = self.state.clone();
652
653 let mut storage = self.storage().await;
654 let shard_id = storage.shard_id();
655 let mut txn = storage.transaction().await?;
656
657 let item_id = self.resolve_builtin_storage_collection(&MZ_CATALOG_RAW);
660 let global_id = self.get_entry(&item_id).latest_global_id();
661 match txn.get_collection_metadata().get(&global_id) {
662 None => {
663 txn.insert_collection_metadata([(global_id, shard_id)].into())
664 .map_err(mz_catalog::durable::DurableCatalogError::from)?;
665 }
666 Some(id) => assert_eq!(*id, shard_id),
667 }
668
669 storage_collections
670 .initialize_state(&mut txn, collections)
671 .await
672 .map_err(mz_catalog::durable::DurableCatalogError::from)?;
673
674 let updates = txn.get_and_commit_op_updates();
675 let (builtin_updates, catalog_updates) = state
676 .apply_updates(updates, &mut LocalExpressionCache::Closed)
677 .await;
678 assert!(
679 builtin_updates.is_empty(),
680 "storage is not allowed to generate catalog changes that would cause changes to builtin tables"
681 );
682 assert!(
683 catalog_updates.is_empty(),
684 "storage is not allowed to generate catalog changes that would change the catalog or controller state"
685 );
686 let commit_ts = txn.upper();
687 txn.commit(commit_ts).await?;
688 drop(storage);
689
690 self.state = state;
692 Ok(())
693 }
694
695 pub async fn initialize_controller(
698 &mut self,
699 config: mz_controller::ControllerConfig,
700 envd_epoch: core::num::NonZeroI64,
701 read_only: bool,
702 ) -> Result<mz_controller::Controller<mz_repr::Timestamp>, mz_catalog::durable::CatalogError>
703 {
704 let controller_start = Instant::now();
705 info!("startup: controller init: beginning");
706
707 let controller = {
708 let mut storage = self.storage().await;
709 let mut tx = storage.transaction().await?;
710 mz_controller::prepare_initialization(&mut tx)
711 .map_err(mz_catalog::durable::DurableCatalogError::from)?;
712 let updates = tx.get_and_commit_op_updates();
713 assert!(
714 updates.is_empty(),
715 "initializing controller should not produce updates: {updates:?}"
716 );
717 let commit_ts = tx.upper();
718 tx.commit(commit_ts).await?;
719
720 let read_only_tx = storage.transaction().await?;
721
722 mz_controller::Controller::new(config, envd_epoch, read_only, &read_only_tx).await
723 };
724
725 self.initialize_storage_state(&controller.storage_collections)
726 .await?;
727
728 info!(
729 "startup: controller init: complete in {:?}",
730 controller_start.elapsed()
731 );
732
733 Ok(controller)
734 }
735
736 pub async fn expire(self) {
738 if let Some(storage) = Arc::into_inner(self.storage) {
741 let storage = storage.into_inner();
742 storage.expire().await;
743 }
744 }
745}
746
747impl CatalogState {
748 fn set_system_configuration_default(
750 &mut self,
751 name: &str,
752 value: VarInput,
753 ) -> Result<(), Error> {
754 Ok(Arc::make_mut(&mut self.system_configuration).set_default(name, value)?)
755 }
756}
757
758fn add_new_remove_old_builtin_items_migration(
762 builtins_cfg: &BuiltinsConfig,
763 txn: &mut mz_catalog::durable::Transaction<'_>,
764) -> Result<Vec<GlobalId>, mz_catalog::durable::CatalogError> {
765 let mut new_builtin_mappings = Vec::new();
766 let mut builtin_descs = HashSet::new();
768
769 let mut builtins = Vec::new();
772 for builtin in BUILTINS::iter(builtins_cfg) {
773 let desc = SystemObjectDescription {
774 schema_name: builtin.schema().to_string(),
775 object_type: builtin.catalog_item_type(),
776 object_name: builtin.name().to_string(),
777 };
778 if !builtin_descs.insert(desc.clone()) {
780 panic!(
781 "duplicate builtin description: {:?}, {:?}",
782 SystemObjectDescription {
783 schema_name: builtin.schema().to_string(),
784 object_type: builtin.catalog_item_type(),
785 object_name: builtin.name().to_string(),
786 },
787 builtin
788 );
789 }
790 builtins.push((desc, builtin));
791 }
792
793 let mut system_object_mappings: BTreeMap<_, _> = txn
794 .get_system_object_mappings()
795 .map(|system_object_mapping| {
796 (
797 system_object_mapping.description.clone(),
798 system_object_mapping,
799 )
800 })
801 .collect();
802
803 let (existing_builtins, new_builtins): (Vec<_>, Vec<_>) =
804 builtins.into_iter().partition_map(|(desc, builtin)| {
805 let fingerprint = match builtin.runtime_alterable() {
806 false => builtin.fingerprint(),
807 true => RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL.into(),
808 };
809 match system_object_mappings.remove(&desc) {
810 Some(system_object_mapping) => {
811 Either::Left((builtin, system_object_mapping, fingerprint))
812 }
813 None => Either::Right((builtin, fingerprint)),
814 }
815 });
816 let new_builtin_ids = txn.allocate_system_item_ids(usize_to_u64(new_builtins.len()))?;
817 let new_builtins: Vec<_> = new_builtins
818 .into_iter()
819 .zip_eq(new_builtin_ids.clone())
820 .collect();
821
822 for ((builtin, fingerprint), (catalog_id, global_id)) in new_builtins.iter().cloned() {
824 new_builtin_mappings.push(SystemObjectMapping {
825 description: SystemObjectDescription {
826 schema_name: builtin.schema().to_string(),
827 object_type: builtin.catalog_item_type(),
828 object_name: builtin.name().to_string(),
829 },
830 unique_identifier: SystemObjectUniqueIdentifier {
831 catalog_id,
832 global_id,
833 fingerprint,
834 },
835 });
836
837 let handled_runtime_alterable = match builtin {
843 Builtin::Connection(c) if c.runtime_alterable => {
844 let mut acl_items = vec![rbac::owner_privilege(
845 mz_sql::catalog::ObjectType::Connection,
846 c.owner_id.clone(),
847 )];
848 acl_items.extend_from_slice(c.access);
849 let versions = BTreeMap::new();
851
852 txn.insert_item(
853 catalog_id,
854 c.oid,
855 global_id,
856 mz_catalog::durable::initialize::resolve_system_schema(c.schema).id,
857 c.name,
858 c.sql.into(),
859 *c.owner_id,
860 acl_items,
861 versions,
862 )?;
863 true
864 }
865 _ => false,
866 };
867 assert_eq!(
868 builtin.runtime_alterable(),
869 handled_runtime_alterable,
870 "runtime alterable object was not handled by migration",
871 );
872 }
873 txn.set_system_object_mappings(new_builtin_mappings)?;
874
875 let builtins_with_catalog_ids = existing_builtins
877 .iter()
878 .map(|(b, m, _)| (*b, m.unique_identifier.catalog_id))
879 .chain(
880 new_builtins
881 .into_iter()
882 .map(|((b, _), (catalog_id, _))| (b, catalog_id)),
883 );
884
885 for (builtin, id) in builtins_with_catalog_ids {
886 let (comment_id, desc, comments) = match builtin {
887 Builtin::Source(s) => (CommentObjectId::Source(id), &s.desc, &s.column_comments),
888 Builtin::View(v) => (CommentObjectId::View(id), &v.desc, &v.column_comments),
889 Builtin::Table(t) => (CommentObjectId::Table(id), &t.desc, &t.column_comments),
890 Builtin::MaterializedView(mv) => (
891 CommentObjectId::MaterializedView(id),
892 &mv.desc,
893 &mv.column_comments,
894 ),
895 Builtin::Log(_)
896 | Builtin::Type(_)
897 | Builtin::Func(_)
898 | Builtin::ContinualTask(_)
899 | Builtin::Index(_)
900 | Builtin::Connection(_) => continue,
901 };
902 txn.drop_comments(&BTreeSet::from_iter([comment_id]))?;
903
904 let mut comments = comments.clone();
905 for (col_idx, name) in desc.iter_names().enumerate() {
906 if let Some(comment) = comments.remove(name.as_str()) {
907 txn.update_comment(comment_id, Some(col_idx + 1), Some(comment.to_owned()))?;
909 }
910 }
911 assert!(
912 comments.is_empty(),
913 "builtin object contains dangling comments that don't correspond to columns {comments:?}"
914 );
915 }
916
917 let mut deleted_system_objects = BTreeSet::new();
920 let mut deleted_runtime_alterable_system_ids = BTreeSet::new();
921 let mut deleted_comments = BTreeSet::new();
922 for (desc, mapping) in system_object_mappings {
923 deleted_system_objects.insert(mapping.description);
924 if mapping.unique_identifier.fingerprint == RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL {
925 deleted_runtime_alterable_system_ids.insert(mapping.unique_identifier.catalog_id);
926 }
927
928 let id = mapping.unique_identifier.catalog_id;
929 let comment_id = match desc.object_type {
930 CatalogItemType::Table => CommentObjectId::Table(id),
931 CatalogItemType::Source => CommentObjectId::Source(id),
932 CatalogItemType::View => CommentObjectId::View(id),
933 CatalogItemType::MaterializedView => CommentObjectId::MaterializedView(id),
934 CatalogItemType::Sink
935 | CatalogItemType::Index
936 | CatalogItemType::Type
937 | CatalogItemType::Func
938 | CatalogItemType::Secret
939 | CatalogItemType::Connection
940 | CatalogItemType::ContinualTask => continue,
941 };
942 deleted_comments.insert(comment_id);
943 }
944 let delete_exceptions: HashSet<SystemObjectDescription> = [].into();
950 assert!(
954 deleted_system_objects
955 .iter()
956 .filter(|object| object.object_type != CatalogItemType::Index)
958 .all(
959 |deleted_object| is_unstable_schema(&deleted_object.schema_name)
960 || delete_exceptions.contains(deleted_object)
961 ),
962 "only objects in unstable schemas can be deleted, deleted objects: {:?}",
963 deleted_system_objects
964 );
965 txn.drop_comments(&deleted_comments)?;
966 txn.remove_items(&deleted_runtime_alterable_system_ids)?;
967 txn.remove_system_object_mappings(deleted_system_objects)?;
968
969 let new_builtin_collections = new_builtin_ids
971 .into_iter()
972 .map(|(_catalog_id, global_id)| global_id)
973 .collect();
974
975 Ok(new_builtin_collections)
976}
977
978fn add_new_remove_old_builtin_clusters_migration(
979 txn: &mut mz_catalog::durable::Transaction<'_>,
980 builtin_cluster_config_map: &BuiltinBootstrapClusterConfigMap,
981) -> Result<(), mz_catalog::durable::CatalogError> {
982 let mut durable_clusters: BTreeMap<_, _> = txn
983 .get_clusters()
984 .filter(|cluster| cluster.id.is_system())
985 .map(|cluster| (cluster.name.to_string(), cluster))
986 .collect();
987
988 for builtin_cluster in BUILTIN_CLUSTERS {
990 if durable_clusters.remove(builtin_cluster.name).is_none() {
991 let cluster_config = builtin_cluster_config_map.get_config(builtin_cluster.name)?;
992
993 txn.insert_system_cluster(
994 builtin_cluster.name,
995 vec![],
996 builtin_cluster.privileges.to_vec(),
997 builtin_cluster.owner_id.to_owned(),
998 mz_catalog::durable::ClusterConfig {
999 variant: mz_catalog::durable::ClusterVariant::Managed(ClusterVariantManaged {
1000 size: cluster_config.size,
1001 availability_zones: vec![],
1002 replication_factor: cluster_config.replication_factor,
1003 logging: default_logging_config(),
1004 optimizer_feature_overrides: Default::default(),
1005 schedule: Default::default(),
1006 }),
1007 workload_class: None,
1008 },
1009 &HashSet::new(),
1010 )?;
1011 }
1012 }
1013
1014 let old_clusters = durable_clusters
1016 .values()
1017 .map(|cluster| cluster.id)
1018 .collect();
1019 txn.remove_clusters(&old_clusters)?;
1020
1021 Ok(())
1022}
1023
1024fn add_new_remove_old_builtin_introspection_source_migration(
1025 txn: &mut mz_catalog::durable::Transaction<'_>,
1026) -> Result<(), AdapterError> {
1027 let mut new_indexes = Vec::new();
1028 let mut removed_indexes = BTreeSet::new();
1029 for cluster in txn.get_clusters() {
1030 let mut introspection_source_index_ids = txn.get_introspection_source_indexes(cluster.id);
1031
1032 let mut new_logs = Vec::new();
1033
1034 for log in BUILTINS::logs() {
1035 if introspection_source_index_ids.remove(log.name).is_none() {
1036 new_logs.push(log);
1037 }
1038 }
1039
1040 for log in new_logs {
1041 let (item_id, gid) =
1042 Transaction::allocate_introspection_source_index_id(&cluster.id, log.variant);
1043 new_indexes.push((cluster.id, log.name.to_string(), item_id, gid));
1044 }
1045
1046 removed_indexes.extend(
1049 introspection_source_index_ids
1050 .into_keys()
1051 .map(|name| (cluster.id, name.to_string())),
1052 );
1053 }
1054 txn.insert_introspection_source_indexes(new_indexes, &HashSet::new())?;
1055 txn.remove_introspection_source_indexes(removed_indexes)?;
1056 Ok(())
1057}
1058
1059fn add_new_remove_old_builtin_roles_migration(
1060 txn: &mut mz_catalog::durable::Transaction<'_>,
1061) -> Result<(), mz_catalog::durable::CatalogError> {
1062 let mut durable_roles: BTreeMap<_, _> = txn
1063 .get_roles()
1064 .filter(|role| role.id.is_system() || role.id.is_predefined())
1065 .map(|role| (role.name.to_string(), role))
1066 .collect();
1067
1068 for builtin_role in BUILTIN_ROLES {
1070 if durable_roles.remove(builtin_role.name).is_none() {
1071 txn.insert_builtin_role(
1072 builtin_role.id,
1073 builtin_role.name.to_string(),
1074 builtin_role.attributes.clone(),
1075 RoleMembership::new(),
1076 RoleVars::default(),
1077 builtin_role.oid,
1078 )?;
1079 }
1080 }
1081
1082 let old_roles = durable_roles.values().map(|role| role.id).collect();
1084 txn.remove_roles(&old_roles)?;
1085
1086 Ok(())
1087}
1088
1089fn add_new_remove_old_builtin_cluster_replicas_migration(
1090 txn: &mut Transaction<'_>,
1091 builtin_cluster_config_map: &BuiltinBootstrapClusterConfigMap,
1092) -> Result<(), AdapterError> {
1093 let cluster_lookup: BTreeMap<_, _> = txn
1094 .get_clusters()
1095 .map(|cluster| (cluster.name.clone(), cluster.clone()))
1096 .collect();
1097
1098 let mut durable_replicas: BTreeMap<ClusterId, BTreeMap<String, ClusterReplica>> = txn
1099 .get_cluster_replicas()
1100 .filter(|replica| replica.replica_id.is_system())
1101 .fold(BTreeMap::new(), |mut acc, replica| {
1102 acc.entry(replica.cluster_id)
1103 .or_insert_with(BTreeMap::new)
1104 .insert(replica.name.to_string(), replica);
1105 acc
1106 });
1107
1108 for builtin_replica in BUILTIN_CLUSTER_REPLICAS {
1110 let cluster = cluster_lookup
1111 .get(builtin_replica.cluster_name)
1112 .expect("builtin cluster replica references non-existent cluster");
1113 let mut empty_map: BTreeMap<String, ClusterReplica> = BTreeMap::new();
1115 let replica_names = durable_replicas
1116 .get_mut(&cluster.id)
1117 .unwrap_or(&mut empty_map);
1118
1119 let builtin_cluster_bootstrap_config =
1120 builtin_cluster_config_map.get_config(builtin_replica.cluster_name)?;
1121 if replica_names.remove(builtin_replica.name).is_none()
1122 && builtin_cluster_bootstrap_config.replication_factor > 0
1126 {
1127 let replica_size = match cluster.config.variant {
1128 ClusterVariant::Managed(ClusterVariantManaged { ref size, .. }) => size.clone(),
1129 ClusterVariant::Unmanaged => builtin_cluster_bootstrap_config.size,
1130 };
1131
1132 let config = builtin_cluster_replica_config(replica_size);
1133 txn.insert_cluster_replica(
1134 cluster.id,
1135 builtin_replica.name,
1136 config,
1137 MZ_SYSTEM_ROLE_ID,
1138 )?;
1139 }
1140 }
1141
1142 let old_replicas = durable_replicas
1144 .values()
1145 .flat_map(|replicas| replicas.values().map(|replica| replica.replica_id))
1146 .collect();
1147 txn.remove_cluster_replicas(&old_replicas)?;
1148
1149 Ok(())
1150}
1151
1152fn remove_invalid_config_param_role_defaults_migration(
1159 txn: &mut Transaction<'_>,
1160) -> Result<(), AdapterError> {
1161 static BUILD_INFO: mz_build_info::BuildInfo = mz_build_info::build_info!();
1162
1163 let roles_to_migrate: BTreeMap<_, _> = txn
1164 .get_roles()
1165 .filter_map(|mut role| {
1166 let session_vars = SessionVars::new_unchecked(&BUILD_INFO, SYSTEM_USER.clone(), None);
1171
1172 let mut invalid_roles_vars = BTreeMap::new();
1174 for (name, value) in &role.vars.map {
1175 let Ok(session_var) = session_vars.inspect(name) else {
1177 invalid_roles_vars.insert(name.clone(), value.clone());
1178 continue;
1179 };
1180 if session_var.check(value.borrow()).is_err() {
1181 invalid_roles_vars.insert(name.clone(), value.clone());
1182 }
1183 }
1184
1185 if invalid_roles_vars.is_empty() {
1187 return None;
1188 }
1189
1190 tracing::warn!(?role, ?invalid_roles_vars, "removing invalid role vars");
1191
1192 for (name, _value) in invalid_roles_vars {
1194 role.vars.map.remove(&name);
1195 }
1196 Some(role)
1197 })
1198 .map(|role| (role.id, role))
1199 .collect();
1200
1201 txn.update_roles_without_auth(roles_to_migrate)?;
1202
1203 Ok(())
1204}
1205
1206fn remove_pending_cluster_replicas_migration(
1209 tx: &mut Transaction,
1210 boot_ts: mz_repr::Timestamp,
1211) -> Result<(), anyhow::Error> {
1212 let cluster_names: BTreeMap<_, _> = tx.get_clusters().map(|c| (c.id, c.name)).collect();
1214
1215 let occurred_at = boot_ts.into();
1216
1217 for replica in tx.get_cluster_replicas().collect::<Vec<_>>() {
1218 if let mz_catalog::durable::ReplicaLocation::Managed { pending: true, .. } =
1219 replica.config.location
1220 {
1221 let cluster_name = cluster_names
1222 .get(&replica.cluster_id)
1223 .cloned()
1224 .unwrap_or_else(|| "<unknown>".to_string());
1225
1226 info!(
1227 "removing pending cluster replica '{}' from cluster '{}'",
1228 replica.name, cluster_name,
1229 );
1230
1231 tx.remove_cluster_replica(replica.replica_id)?;
1232
1233 let audit_id = tx.allocate_audit_log_id()?;
1237 tx.insert_audit_log_event(VersionedEvent::new(
1238 audit_id,
1239 EventType::Drop,
1240 ObjectType::ClusterReplica,
1241 EventDetails::DropClusterReplicaV3(mz_audit_log::DropClusterReplicaV3 {
1242 cluster_id: replica.cluster_id.to_string(),
1243 cluster_name,
1244 replica_id: Some(replica.replica_id.to_string()),
1245 replica_name: replica.name,
1246 reason: CreateOrDropClusterReplicaReasonV1::System,
1247 scheduling_policies: None,
1248 }),
1249 None,
1250 occurred_at,
1251 ));
1252 }
1253 }
1254 Ok(())
1255}
1256
1257pub(crate) fn builtin_cluster_replica_config(
1258 replica_size: String,
1259) -> mz_catalog::durable::ReplicaConfig {
1260 mz_catalog::durable::ReplicaConfig {
1261 location: mz_catalog::durable::ReplicaLocation::Managed {
1262 availability_zone: None,
1263 billed_as: None,
1264 pending: false,
1265 internal: false,
1266 size: replica_size,
1267 },
1268 logging: default_logging_config(),
1269 }
1270}
1271
1272fn default_logging_config() -> ReplicaLogging {
1273 ReplicaLogging {
1274 log_logging: false,
1275 interval: Some(Duration::from_secs(1)),
1276 }
1277}
1278
1279#[derive(Debug)]
1280pub struct BuiltinBootstrapClusterConfigMap {
1281 pub system_cluster: BootstrapBuiltinClusterConfig,
1283 pub catalog_server_cluster: BootstrapBuiltinClusterConfig,
1285 pub probe_cluster: BootstrapBuiltinClusterConfig,
1287 pub support_cluster: BootstrapBuiltinClusterConfig,
1289 pub analytics_cluster: BootstrapBuiltinClusterConfig,
1291}
1292
1293impl BuiltinBootstrapClusterConfigMap {
1294 fn get_config(
1296 &self,
1297 cluster_name: &str,
1298 ) -> Result<BootstrapBuiltinClusterConfig, mz_catalog::durable::CatalogError> {
1299 let cluster_config = if cluster_name == mz_catalog::builtin::MZ_SYSTEM_CLUSTER.name {
1300 &self.system_cluster
1301 } else if cluster_name == mz_catalog::builtin::MZ_CATALOG_SERVER_CLUSTER.name {
1302 &self.catalog_server_cluster
1303 } else if cluster_name == mz_catalog::builtin::MZ_PROBE_CLUSTER.name {
1304 &self.probe_cluster
1305 } else if cluster_name == mz_catalog::builtin::MZ_SUPPORT_CLUSTER.name {
1306 &self.support_cluster
1307 } else if cluster_name == mz_catalog::builtin::MZ_ANALYTICS_CLUSTER.name {
1308 &self.analytics_cluster
1309 } else {
1310 return Err(mz_catalog::durable::CatalogError::Catalog(
1311 SqlCatalogError::UnexpectedBuiltinCluster(cluster_name.to_owned()),
1312 ));
1313 };
1314 Ok(cluster_config.clone())
1315 }
1316}
1317
1318pub(crate) fn into_consolidatable_updates_startup(
1335 updates: Vec<StateUpdate>,
1336 ts: Timestamp,
1337) -> Vec<(BootstrapStateUpdateKind, Timestamp, Diff)> {
1338 updates
1339 .into_iter()
1340 .map(|StateUpdate { kind, ts: _, diff }| {
1341 let kind: BootstrapStateUpdateKind = kind
1342 .try_into()
1343 .unwrap_or_else(|e| panic!("temporary items do not exist during bootstrap: {e:?}"));
1344 (kind, ts, Diff::from(diff))
1345 })
1346 .collect()
1347}
1348
1349fn get_dyncfg_val_from_defaults_and_remote<T: mz_dyncfg::ConfigDefault>(
1350 defaults: &BTreeMap<String, String>,
1351 remote: Option<&BTreeMap<String, String>>,
1352 cfg: &mz_dyncfg::Config<T>,
1353) -> T::ConfigType {
1354 let mut val = T::into_config_type(cfg.default().clone());
1355 let get_fn = |map: &BTreeMap<String, String>| {
1356 let val = map.get(cfg.name())?;
1357 match <T::ConfigType as mz_dyncfg::ConfigType>::parse(val) {
1358 Ok(x) => Some(x),
1359 Err(err) => {
1360 tracing::warn!("could not parse {} value [{}]: {}", cfg.name(), val, err);
1361 None
1362 }
1363 }
1364 };
1365 if let Some(x) = get_fn(defaults) {
1366 val = x;
1367 }
1368 if let Some(x) = remote.and_then(get_fn) {
1369 val = x;
1370 }
1371 val
1372}