1use std::fmt::Debug;
14
15use mz_repr::Diff;
16use serde::{Deserialize, Serialize};
17use serde_plain::{derive_display_from_serialize, derive_fromstr_from_deserialize};
18
19use crate::durable::CatalogError;
20use crate::durable::objects::serialization::proto;
21use crate::durable::objects::state_update::StateUpdateKind;
22use crate::durable::persist::{Timestamp, UnopenedPersistCatalogState};
23
24pub trait Collection: Debug {
27 type Key;
29 type Value;
31
32 fn collection_type() -> CollectionType;
34
35 fn collection_trace(trace: Trace) -> CollectionTrace<Self>;
37
38 fn update(key: Self::Key, value: Self::Value) -> StateUpdateKind;
40
41 fn name() -> String {
43 Self::collection_type().to_string()
44 }
45}
46
47#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum CollectionType {
56 AuditLog,
57 ComputeInstance,
58 ComputeIntrospectionSourceIndex,
59 ComputeReplicas,
60 ClusterReplicaSize,
61 Comments,
62 Config,
63 Database,
64 DefaultPrivileges,
65 IdAlloc,
66 IndexColumns,
67 Item,
68 ItemColumns,
69 NetworkPolicy,
70 QueryPolicy,
71 Role,
72 RoleAuth,
73 Schema,
74 Setting,
75 SourceReferences,
76 SystemConfiguration,
77 ClusterSystemConfiguration,
78 ReplicaSystemConfiguration,
79 SystemGidMapping,
80 SystemPrivileges,
81 StorageCollectionMetadata,
82 UnfinalizedShard,
83 TxnWalShard,
84}
85
86derive_display_from_serialize!(CollectionType);
87derive_fromstr_from_deserialize!(CollectionType);
88
89macro_rules! collection_impl {
99 ({
100 name: $name:ident,
101 key: $key:ty,
102 value: $value:ty,
103 collection_type: $collection_type:expr,
104 trace_field: $trace_field:ident,
105 update: $update:expr,
106}) => {
107 #[derive(Debug, Clone, PartialEq, Eq)]
108 pub struct $name {}
109
110 impl Collection for $name {
111 type Key = $key;
112 type Value = $value;
113
114 fn collection_type() -> CollectionType {
115 $collection_type
116 }
117
118 fn collection_trace(trace: Trace) -> CollectionTrace<Self> {
119 trace.$trace_field
120 }
121
122 fn update(key: Self::Key, value: Self::Value) -> StateUpdateKind {
123 $update(key, value)
124 }
125 }
126 };
127}
128
129collection_impl!({
130 name: AuditLogCollection,
131 key: proto::AuditLogKey,
132 value: (),
133 collection_type: CollectionType::AuditLog,
134 trace_field: audit_log,
135 update: StateUpdateKind::AuditLog,
136});
137collection_impl!({
138 name: ClusterCollection,
139 key: proto::ClusterKey,
140 value: proto::ClusterValue,
141 collection_type: CollectionType::ComputeInstance,
142 trace_field: clusters,
143 update: StateUpdateKind::Cluster,
144});
145collection_impl!({
146 name: ClusterIntrospectionSourceIndexCollection,
147 key: proto::ClusterIntrospectionSourceIndexKey,
148 value: proto::ClusterIntrospectionSourceIndexValue,
149 collection_type: CollectionType::ComputeIntrospectionSourceIndex,
150 trace_field: introspection_sources,
151 update: StateUpdateKind::IntrospectionSourceIndex,
152});
153collection_impl!({
154 name: ClusterReplicaCollection,
155 key: proto::ClusterReplicaKey,
156 value: proto::ClusterReplicaValue,
157 collection_type: CollectionType::ComputeReplicas,
158 trace_field: cluster_replicas,
159 update: StateUpdateKind::ClusterReplica,
160});
161collection_impl!({
162 name: ClusterReplicaSizeCollection,
163 key: proto::ClusterReplicaSizeKey,
164 value: proto::ClusterReplicaSizeValue,
165 collection_type: CollectionType::ClusterReplicaSize,
166 trace_field: cluster_replica_sizes,
167 update: StateUpdateKind::ClusterReplicaSize,
168});
169collection_impl!({
170 name: CommentCollection,
171 key: proto::CommentKey,
172 value: proto::CommentValue,
173 collection_type: CollectionType::Comments,
174 trace_field: comments,
175 update: StateUpdateKind::Comment,
176});
177collection_impl!({
178 name: ConfigCollection,
179 key: proto::ConfigKey,
180 value: proto::ConfigValue,
181 collection_type: CollectionType::Config,
182 trace_field: configs,
183 update: StateUpdateKind::Config,
184});
185collection_impl!({
186 name: DatabaseCollection,
187 key: proto::DatabaseKey,
188 value: proto::DatabaseValue,
189 collection_type: CollectionType::Database,
190 trace_field: databases,
191 update: StateUpdateKind::Database,
192});
193collection_impl!({
194 name: DefaultPrivilegeCollection,
195 key: proto::DefaultPrivilegesKey,
196 value: proto::DefaultPrivilegesValue,
197 collection_type: CollectionType::DefaultPrivileges,
198 trace_field: default_privileges,
199 update: StateUpdateKind::DefaultPrivilege,
200});
201collection_impl!({
202 name: IdAllocatorCollection,
203 key: proto::IdAllocKey,
204 value: proto::IdAllocValue,
205 collection_type: CollectionType::IdAlloc,
206 trace_field: id_allocator,
207 update: StateUpdateKind::IdAllocator,
208});
209collection_impl!({
210 name: ItemCollection,
211 key: proto::ItemKey,
212 value: proto::ItemValue,
213 collection_type: CollectionType::Item,
214 trace_field: items,
215 update: StateUpdateKind::Item,
216});
217collection_impl!({
218 name: ItemColumnsCollection,
219 key: proto::ItemColumnsKey,
220 value: proto::ItemColumnsValue,
221 collection_type: CollectionType::ItemColumns,
222 trace_field: item_columns,
223 update: StateUpdateKind::ItemColumns,
224});
225collection_impl!({
226 name: IndexColumnsCollection,
227 key: proto::IndexColumnsKey,
228 value: proto::IndexColumnsValue,
229 collection_type: CollectionType::IndexColumns,
230 trace_field: index_columns,
231 update: StateUpdateKind::IndexColumns,
232});
233collection_impl!({
234 name: NetworkPolicyCollection,
235 key: proto::NetworkPolicyKey,
236 value: proto::NetworkPolicyValue,
237 collection_type: CollectionType::NetworkPolicy,
238 trace_field: network_policies,
239 update: StateUpdateKind::NetworkPolicy,
240});
241collection_impl!({
242 name: QueryPolicyCollection,
243 key: proto::QueryPolicyKey,
244 value: proto::QueryPolicyValue,
245 collection_type: CollectionType::QueryPolicy,
246 trace_field: query_policies,
247 update: StateUpdateKind::QueryPolicy,
248});
249collection_impl!({
250 name: RoleCollection,
251 key: proto::RoleKey,
252 value: proto::RoleValue,
253 collection_type: CollectionType::Role,
254 trace_field: roles,
255 update: StateUpdateKind::Role,
256});
257collection_impl!({
258 name: RoleAuthCollection,
259 key: proto::RoleAuthKey,
260 value: proto::RoleAuthValue,
261 collection_type: CollectionType::RoleAuth,
262 trace_field: role_auth,
263 update: StateUpdateKind::RoleAuth,
264});
265collection_impl!({
266 name: SchemaCollection,
267 key: proto::SchemaKey,
268 value: proto::SchemaValue,
269 collection_type: CollectionType::Schema,
270 trace_field: schemas,
271 update: StateUpdateKind::Schema,
272});
273collection_impl!({
274 name: SettingCollection,
275 key: proto::SettingKey,
276 value: proto::SettingValue,
277 collection_type: CollectionType::Setting,
278 trace_field: settings,
279 update: StateUpdateKind::Setting,
280});
281collection_impl!({
282 name: SourceReferencesCollection,
283 key: proto::SourceReferencesKey,
284 value: proto::SourceReferencesValue,
285 collection_type: CollectionType::SourceReferences,
286 trace_field: source_references,
287 update: StateUpdateKind::SourceReferences,
288});
289collection_impl!({
290 name: SystemConfigurationCollection,
291 key: proto::ServerConfigurationKey,
292 value: proto::ServerConfigurationValue,
293 collection_type: CollectionType::SystemConfiguration,
294 trace_field: system_configurations,
295 update: StateUpdateKind::SystemConfiguration,
296});
297collection_impl!({
298 name: ClusterSystemConfigurationCollection,
299 key: proto::ClusterSystemConfigurationKey,
300 value: proto::ClusterSystemConfigurationValue,
301 collection_type: CollectionType::ClusterSystemConfiguration,
302 trace_field: cluster_system_configurations,
303 update: StateUpdateKind::ClusterSystemConfiguration,
304});
305collection_impl!({
306 name: ReplicaSystemConfigurationCollection,
307 key: proto::ReplicaSystemConfigurationKey,
308 value: proto::ReplicaSystemConfigurationValue,
309 collection_type: CollectionType::ReplicaSystemConfiguration,
310 trace_field: replica_system_configurations,
311 update: StateUpdateKind::ReplicaSystemConfiguration,
312});
313collection_impl!({
314 name: SystemItemMappingCollection,
315 key: proto::GidMappingKey,
316 value: proto::GidMappingValue,
317 collection_type: CollectionType::SystemGidMapping,
318 trace_field: system_object_mappings,
319 update: StateUpdateKind::SystemObjectMapping,
320});
321collection_impl!({
322 name: SystemPrivilegeCollection,
323 key: proto::SystemPrivilegesKey,
324 value: proto::SystemPrivilegesValue,
325 collection_type: CollectionType::SystemPrivileges,
326 trace_field: system_privileges,
327 update: StateUpdateKind::SystemPrivilege,
328});
329
330collection_impl!({
331 name: StorageCollectionMetadataCollection,
332 key: proto::StorageCollectionMetadataKey,
333 value: proto::StorageCollectionMetadataValue,
334 collection_type: CollectionType::StorageCollectionMetadata,
335 trace_field: storage_collection_metadata,
336 update: StateUpdateKind::StorageCollectionMetadata,
337});
338collection_impl!({
339 name: UnfinalizedShardsCollection,
340 key: proto::UnfinalizedShardKey,
341 value: (),
342 collection_type: CollectionType::UnfinalizedShard,
343 trace_field: unfinalized_shards,
344 update: StateUpdateKind::UnfinalizedShard,
345});
346collection_impl!({
347 name: TxnWalShardCollection,
348 key: (),
349 value: proto::TxnWalShardValue,
350 collection_type: CollectionType::TxnWalShard,
351 trace_field: txn_wal_shard,
352 update: StateUpdateKind::TxnWalShard,
353});
354
355#[derive(Debug, Clone, PartialEq, Eq)]
360pub struct CollectionTrace<T: Collection + ?Sized> {
361 pub values: Vec<((T::Key, T::Value), Timestamp, Diff)>,
362}
363
364impl<T: Collection> CollectionTrace<T> {
365 fn new() -> CollectionTrace<T> {
366 CollectionTrace { values: Vec::new() }
367 }
368}
369
370impl<T: Collection> CollectionTrace<T>
371where
372 T: Collection,
373 T::Key: Ord,
374 T::Value: Ord,
375{
376 fn sort(&mut self) {
377 self.values
378 .sort_by(|(x1, ts1, d1), (x2, ts2, d2)| ts1.cmp(ts2).then(d1.cmp(d2)).then(x1.cmp(x2)));
379 }
380}
381
382#[derive(Debug, Clone, PartialEq, Eq)]
384pub struct Trace {
385 pub audit_log: CollectionTrace<AuditLogCollection>,
386 pub clusters: CollectionTrace<ClusterCollection>,
387 pub introspection_sources: CollectionTrace<ClusterIntrospectionSourceIndexCollection>,
388 pub cluster_replicas: CollectionTrace<ClusterReplicaCollection>,
389 pub cluster_replica_sizes: CollectionTrace<ClusterReplicaSizeCollection>,
390 pub comments: CollectionTrace<CommentCollection>,
391 pub configs: CollectionTrace<ConfigCollection>,
392 pub databases: CollectionTrace<DatabaseCollection>,
393 pub default_privileges: CollectionTrace<DefaultPrivilegeCollection>,
394 pub id_allocator: CollectionTrace<IdAllocatorCollection>,
395 pub items: CollectionTrace<ItemCollection>,
396 pub item_columns: CollectionTrace<ItemColumnsCollection>,
397 pub index_columns: CollectionTrace<IndexColumnsCollection>,
398 pub network_policies: CollectionTrace<NetworkPolicyCollection>,
399 pub query_policies: CollectionTrace<QueryPolicyCollection>,
400 pub roles: CollectionTrace<RoleCollection>,
401 pub role_auth: CollectionTrace<RoleAuthCollection>,
402 pub schemas: CollectionTrace<SchemaCollection>,
403 pub settings: CollectionTrace<SettingCollection>,
404 pub source_references: CollectionTrace<SourceReferencesCollection>,
405 pub system_object_mappings: CollectionTrace<SystemItemMappingCollection>,
406 pub system_configurations: CollectionTrace<SystemConfigurationCollection>,
407 pub cluster_system_configurations: CollectionTrace<ClusterSystemConfigurationCollection>,
408 pub replica_system_configurations: CollectionTrace<ReplicaSystemConfigurationCollection>,
409 pub system_privileges: CollectionTrace<SystemPrivilegeCollection>,
410 pub storage_collection_metadata: CollectionTrace<StorageCollectionMetadataCollection>,
411 pub unfinalized_shards: CollectionTrace<UnfinalizedShardsCollection>,
412 pub txn_wal_shard: CollectionTrace<TxnWalShardCollection>,
413}
414
415impl Trace {
416 pub(crate) fn new() -> Trace {
417 Trace {
418 audit_log: CollectionTrace::new(),
419 clusters: CollectionTrace::new(),
420 introspection_sources: CollectionTrace::new(),
421 cluster_replicas: CollectionTrace::new(),
422 cluster_replica_sizes: CollectionTrace::new(),
423 comments: CollectionTrace::new(),
424 configs: CollectionTrace::new(),
425 databases: CollectionTrace::new(),
426 default_privileges: CollectionTrace::new(),
427 id_allocator: CollectionTrace::new(),
428 items: CollectionTrace::new(),
429 item_columns: CollectionTrace::new(),
430 index_columns: CollectionTrace::new(),
431 network_policies: CollectionTrace::new(),
432 query_policies: CollectionTrace::new(),
433 roles: CollectionTrace::new(),
434 role_auth: CollectionTrace::new(),
435 schemas: CollectionTrace::new(),
436 settings: CollectionTrace::new(),
437 source_references: CollectionTrace::new(),
438 system_object_mappings: CollectionTrace::new(),
439 system_configurations: CollectionTrace::new(),
440 cluster_system_configurations: CollectionTrace::new(),
441 replica_system_configurations: CollectionTrace::new(),
442 system_privileges: CollectionTrace::new(),
443 storage_collection_metadata: CollectionTrace::new(),
444 unfinalized_shards: CollectionTrace::new(),
445 txn_wal_shard: CollectionTrace::new(),
446 }
447 }
448
449 pub fn sort(&mut self) {
450 let Trace {
451 audit_log,
452 clusters,
453 introspection_sources,
454 cluster_replicas,
455 cluster_replica_sizes,
456 comments,
457 configs,
458 databases,
459 default_privileges,
460 id_allocator,
461 items,
462 item_columns,
463 index_columns,
464 network_policies,
465 query_policies,
466 roles,
467 role_auth,
468 schemas,
469 settings,
470 source_references,
471 system_object_mappings,
472 system_configurations,
473 cluster_system_configurations,
474 replica_system_configurations,
475 system_privileges,
476 storage_collection_metadata,
477 unfinalized_shards,
478 txn_wal_shard,
479 } = self;
480 audit_log.sort();
481 clusters.sort();
482 introspection_sources.sort();
483 cluster_replicas.sort();
484 cluster_replica_sizes.sort();
485 comments.sort();
486 configs.sort();
487 databases.sort();
488 default_privileges.sort();
489 id_allocator.sort();
490 items.sort();
491 item_columns.sort();
492 index_columns.sort();
493 network_policies.sort();
494 query_policies.sort();
495 roles.sort();
496 role_auth.sort();
497 schemas.sort();
498 settings.sort();
499 source_references.sort();
500 system_object_mappings.sort();
501 system_configurations.sort();
502 cluster_system_configurations.sort();
503 replica_system_configurations.sort();
504 system_privileges.sort();
505 storage_collection_metadata.sort();
506 unfinalized_shards.sort();
507 txn_wal_shard.sort();
508 }
509}
510
511pub struct DebugCatalogState(pub(crate) UnopenedPersistCatalogState);
512
513impl DebugCatalogState {
514 pub async fn edit<T: Collection>(
516 &mut self,
517 key: T::Key,
518 value: T::Value,
519 ) -> Result<Option<T::Value>, CatalogError>
520 where
521 T::Key: PartialEq + Eq + Debug + Clone,
522 T::Value: Debug + Clone,
523 {
524 self.0.debug_edit::<T>(key, value).await
525 }
526
527 pub async fn delete<T: Collection>(&mut self, key: T::Key) -> Result<(), CatalogError>
529 where
530 T::Key: PartialEq + Eq + Debug + Clone,
531 T::Value: Debug,
532 {
533 self.0.debug_delete::<T>(key).await
534 }
535}