mz_catalog/durable/
error.rs1use std::fmt::Debug;
11
12use mz_persist_client::error::UpperMismatch;
13use mz_proto::TryFromProtoError;
14use mz_repr::Timestamp;
15use mz_sql::catalog::CatalogError as SqlCatalogError;
16use mz_storage_types::controller::StorageError;
17
18use crate::durable::Epoch;
19use crate::durable::persist::antichain_to_timestamp;
20
21#[derive(Debug, thiserror::Error)]
22pub enum CatalogError {
23 #[error(transparent)]
24 Catalog(#[from] SqlCatalogError),
25 #[error(transparent)]
26 Durable(#[from] DurableCatalogError),
27}
28
29impl From<TryFromProtoError> for CatalogError {
30 fn from(e: TryFromProtoError) -> Self {
31 Self::Durable(e.into())
32 }
33}
34
35impl From<FenceError> for CatalogError {
36 fn from(err: FenceError) -> Self {
37 let err: DurableCatalogError = err.into();
38 err.into()
39 }
40}
41
42#[derive(Debug, thiserror::Error)]
44pub enum DurableCatalogError {
45 #[error(transparent)]
47 Fence(#[from] FenceError),
48 #[error(
50 "incompatible Catalog version {found_version}, minimum: {min_catalog_version}, current: {catalog_version}"
51 )]
52 IncompatibleDataVersion {
53 found_version: u64,
54 min_catalog_version: u64,
55 catalog_version: u64,
56 },
57 #[error(
58 "incompatible persist version {found_version}, current: {catalog_version}, \
59 make sure to upgrade the catalog one major version forward at a time"
60 )]
61 IncompatiblePersistVersion {
62 found_version: semver::Version,
63 catalog_version: semver::Version,
64 },
65 #[error("uninitialized")]
67 Uninitialized,
68 #[error("{0}")]
70 NotWritable(String),
71 #[error("proto: {0}")]
73 Proto(TryFromProtoError),
74 #[error("duplicate key")]
76 DuplicateKey,
77 #[error("uniqueness violation")]
79 UniquenessViolation,
80 #[error(transparent)]
82 Storage(StorageError<Timestamp>),
83 #[error("Internal catalog error: {0}")]
85 Internal(String),
86}
87
88impl DurableCatalogError {
89 pub fn can_recover_with_write_mode(&self) -> bool {
91 match self {
92 DurableCatalogError::NotWritable(_) => true,
93 _ => false,
94 }
95 }
96}
97
98impl From<StorageError<Timestamp>> for DurableCatalogError {
99 fn from(e: StorageError<Timestamp>) -> Self {
100 DurableCatalogError::Storage(e)
101 }
102}
103
104impl From<TryFromProtoError> for DurableCatalogError {
105 fn from(e: TryFromProtoError) -> Self {
106 DurableCatalogError::Proto(e)
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
114pub enum FenceError {
115 #[error(
119 "current catalog deployment generation {current_generation} fenced by new catalog epoch {fence_generation}"
120 )]
121 DeployGeneration {
122 current_generation: u64,
123 fence_generation: u64,
124 },
125 #[error("current catalog epoch {current_epoch} fenced by new catalog epoch {fence_epoch}")]
128 Epoch {
129 current_epoch: Epoch,
130 fence_epoch: Epoch,
131 },
132 #[error(
135 "builtin table migration shard upper {expected_upper:?} fenced by new builtin table migration shard upper {actual_upper:?}"
136 )]
137 MigrationUpper {
138 expected_upper: Timestamp,
139 actual_upper: Timestamp,
140 },
141}
142
143impl FenceError {
144 pub fn migration(err: UpperMismatch<Timestamp>) -> Self {
145 Self::MigrationUpper {
146 expected_upper: antichain_to_timestamp(err.expected),
147 actual_upper: antichain_to_timestamp(err.current),
148 }
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use crate::durable::{Epoch, FenceError};
155
156 #[mz_ore::test]
157 fn test_fence_err_ord() {
158 let deploy_generation = FenceError::DeployGeneration {
159 current_generation: 90,
160 fence_generation: 91,
161 };
162 let epoch = FenceError::Epoch {
163 current_epoch: Epoch::new(80).expect("non zero"),
164 fence_epoch: Epoch::new(81).expect("non zero"),
165 };
166 assert!(deploy_generation < epoch);
167
168 let migration = FenceError::MigrationUpper {
169 expected_upper: 60.into(),
170 actual_upper: 61.into(),
171 };
172 assert!(epoch < migration);
173 }
174}