metric_engine/
error.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16use std::sync::Arc;
17
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use datatypes::prelude::ConcreteDataType;
22use snafu::{Location, Snafu};
23use store_api::region_request::RegionRequest;
24use store_api::storage::{FileId, RegionId};
25
26#[derive(Snafu)]
27#[snafu(visibility(pub))]
28#[stack_trace_debug]
29pub enum Error {
30    #[snafu(display("Failed to create mito region, region type: {}", region_type))]
31    CreateMitoRegion {
32        region_type: String,
33        source: BoxedError,
34        #[snafu(implicit)]
35        location: Location,
36    },
37
38    #[snafu(display("Failed to open mito region, region type: {}", region_type))]
39    OpenMitoRegion {
40        region_type: String,
41        source: BoxedError,
42        #[snafu(implicit)]
43        location: Location,
44    },
45
46    #[snafu(display("Failed to batch open mito region"))]
47    BatchOpenMitoRegion {
48        source: BoxedError,
49        #[snafu(implicit)]
50        location: Location,
51    },
52
53    #[snafu(display("Failed to batch catchup mito region"))]
54    BatchCatchupMitoRegion {
55        source: BoxedError,
56        #[snafu(implicit)]
57        location: Location,
58    },
59
60    #[snafu(display("No open region result for region {}", region_id))]
61    NoOpenRegionResult {
62        region_id: RegionId,
63        #[snafu(implicit)]
64        location: Location,
65    },
66
67    #[snafu(display("Failed to close mito region, region id: {}", region_id))]
68    CloseMitoRegion {
69        region_id: RegionId,
70        source: BoxedError,
71        #[snafu(implicit)]
72        location: Location,
73    },
74
75    #[snafu(display("Failed to deserialize column metadata from {}", raw))]
76    DeserializeColumnMetadata {
77        raw: String,
78        #[snafu(source)]
79        error: serde_json::Error,
80        #[snafu(implicit)]
81        location: Location,
82    },
83
84    #[snafu(display("Failed to serialize column metadata"))]
85    SerializeColumnMetadata {
86        #[snafu(source)]
87        error: serde_json::Error,
88        #[snafu(implicit)]
89        location: Location,
90    },
91
92    #[snafu(display("Failed to serialize region manifest info"))]
93    SerializeRegionManifestInfo {
94        #[snafu(source)]
95        error: serde_json::Error,
96        #[snafu(implicit)]
97        location: Location,
98    },
99
100    #[snafu(display("Failed to decode base64 column value"))]
101    DecodeColumnValue {
102        #[snafu(source)]
103        error: base64::DecodeError,
104        #[snafu(implicit)]
105        location: Location,
106    },
107
108    #[snafu(display("Failed to parse region id from {}", raw))]
109    ParseRegionId {
110        raw: String,
111        #[snafu(source)]
112        error: <u64 as std::str::FromStr>::Err,
113        #[snafu(implicit)]
114        location: Location,
115    },
116
117    #[snafu(display("Failed to parse region options: {}", reason))]
118    ParseRegionOptions {
119        reason: String,
120        #[snafu(implicit)]
121        location: Location,
122    },
123
124    #[snafu(display("Mito read operation fails"))]
125    MitoReadOperation {
126        source: BoxedError,
127        #[snafu(implicit)]
128        location: Location,
129    },
130
131    #[snafu(display(
132        "Mito copy region from operation fails, source region id: {}, target region id: {}",
133        source_region_id,
134        target_region_id
135    ))]
136    MitoCopyRegionFromOperation {
137        source: BoxedError,
138        #[snafu(implicit)]
139        location: Location,
140        source_region_id: RegionId,
141        target_region_id: RegionId,
142    },
143
144    #[snafu(display("Mito edit region operation fails, region id: {}", region_id))]
145    MitoEditRegion {
146        region_id: RegionId,
147        source: BoxedError,
148        #[snafu(implicit)]
149        location: Location,
150    },
151
152    #[snafu(display("Failed to encode primary key"))]
153    EncodePrimaryKey {
154        source: mito_codec::error::Error,
155        #[snafu(implicit)]
156        location: Location,
157    },
158
159    #[snafu(display("Mito write operation fails"))]
160    MitoWriteOperation {
161        source: BoxedError,
162        #[snafu(implicit)]
163        location: Location,
164    },
165
166    #[snafu(display("Mito flush operation fails"))]
167    MitoFlushOperation {
168        source: BoxedError,
169        #[snafu(implicit)]
170        location: Location,
171    },
172
173    #[snafu(display("Mito sync operation fails"))]
174    MitoSyncOperation {
175        source: BoxedError,
176        #[snafu(implicit)]
177        location: Location,
178    },
179
180    #[snafu(display("Mito enter staging operation fails"))]
181    MitoEnterStagingOperation {
182        source: BoxedError,
183        #[snafu(implicit)]
184        location: Location,
185    },
186
187    #[snafu(display("Failed to collect record batch stream"))]
188    CollectRecordBatchStream {
189        source: common_recordbatch::error::Error,
190        #[snafu(implicit)]
191        location: Location,
192    },
193
194    #[snafu(display("Internal column {} is reserved", column))]
195    InternalColumnOccupied {
196        column: String,
197        #[snafu(implicit)]
198        location: Location,
199    },
200
201    #[snafu(display("Required table option is missing"))]
202    MissingRegionOption {
203        #[snafu(implicit)]
204        location: Location,
205    },
206
207    #[snafu(display("Region options are conflicted"))]
208    ConflictRegionOption {
209        #[snafu(implicit)]
210        location: Location,
211    },
212
213    #[snafu(display("Physical region {} not found", region_id))]
214    PhysicalRegionNotFound {
215        region_id: RegionId,
216        #[snafu(implicit)]
217        location: Location,
218    },
219
220    #[snafu(display("Logical region {} not found", region_id))]
221    LogicalRegionNotFound {
222        region_id: RegionId,
223        #[snafu(implicit)]
224        location: Location,
225    },
226
227    #[snafu(display("Column type mismatch. Expect {:?}, got {:?}", expect, actual))]
228    ColumnTypeMismatch {
229        expect: ConcreteDataType,
230        actual: ConcreteDataType,
231        #[snafu(implicit)]
232        location: Location,
233    },
234
235    #[snafu(display("Column {} not found in logical region {}", name, region_id))]
236    ColumnNotFound {
237        name: String,
238        region_id: RegionId,
239        #[snafu(implicit)]
240        location: Location,
241    },
242
243    #[snafu(display("Table id count mismatch. Expect {}, got {}", expected, actual))]
244    TableIdCountMismatch {
245        expected: usize,
246        actual: usize,
247        #[snafu(implicit)]
248        location: Location,
249    },
250
251    #[snafu(display("Alter request to physical region is forbidden"))]
252    ForbiddenPhysicalAlter {
253        #[snafu(implicit)]
254        location: Location,
255    },
256
257    #[snafu(display("Invalid region metadata"))]
258    InvalidMetadata {
259        source: store_api::metadata::MetadataError,
260        #[snafu(implicit)]
261        location: Location,
262    },
263
264    #[snafu(display(
265        "Physical region {} is busy, there are still some logical regions using it",
266        region_id
267    ))]
268    PhysicalRegionBusy {
269        region_id: RegionId,
270        #[snafu(implicit)]
271        location: Location,
272    },
273
274    #[snafu(display("Unsupported region request: {}", request))]
275    UnsupportedRegionRequest {
276        request: Box<RegionRequest>,
277        #[snafu(implicit)]
278        location: Location,
279    },
280
281    #[snafu(display("Unsupported remap manifests request for region {}", region_id))]
282    UnsupportedRemapManifestsRequest {
283        region_id: RegionId,
284        #[snafu(implicit)]
285        location: Location,
286    },
287
288    #[snafu(display("Unsupported sync region from request for region {}", region_id))]
289    UnsupportedSyncRegionFromRequest {
290        region_id: RegionId,
291        #[snafu(implicit)]
292        location: Location,
293    },
294
295    #[snafu(display("Missing file metas in region {}, file ids: {:?}", region_id, file_ids))]
296    MissingFiles {
297        region_id: RegionId,
298        #[snafu(implicit)]
299        location: Location,
300        file_ids: Vec<FileId>,
301    },
302
303    #[snafu(display("Unsupported alter kind: {}", kind))]
304    UnsupportedAlterKind {
305        kind: String,
306        #[snafu(implicit)]
307        location: Location,
308    },
309
310    #[snafu(display("Multiple field column found: {} and {}", previous, current))]
311    MultipleFieldColumn {
312        previous: String,
313        current: String,
314        #[snafu(implicit)]
315        location: Location,
316    },
317
318    #[snafu(display("Adding field column {} to physical table", name))]
319    AddingFieldColumn {
320        name: String,
321        #[snafu(implicit)]
322        location: Location,
323    },
324
325    #[snafu(display("No field column found"))]
326    NoFieldColumn {
327        #[snafu(implicit)]
328        location: Location,
329    },
330
331    #[snafu(display("Failed to set SKIPPING index option"))]
332    SetSkippingIndexOption {
333        source: datatypes::error::Error,
334        #[snafu(implicit)]
335        location: Location,
336    },
337
338    #[snafu(display("Unexpected request: {}", reason))]
339    UnexpectedRequest {
340        reason: String,
341        #[snafu(implicit)]
342        location: Location,
343    },
344
345    #[snafu(display("Expected metric manifest info, region: {}", region_id))]
346    MetricManifestInfo {
347        region_id: RegionId,
348        #[snafu(implicit)]
349        location: Location,
350    },
351
352    #[snafu(display("Failed to start repeated task: {}", name))]
353    StartRepeatedTask {
354        name: String,
355        source: common_runtime::error::Error,
356        #[snafu(implicit)]
357        location: Location,
358    },
359
360    #[snafu(display("Get value from cache"))]
361    CacheGet {
362        source: Arc<Error>,
363        #[snafu(implicit)]
364        location: Location,
365    },
366}
367
368pub type Result<T, E = Error> = std::result::Result<T, E>;
369
370impl ErrorExt for Error {
371    fn status_code(&self) -> StatusCode {
372        use Error::*;
373
374        match self {
375            InternalColumnOccupied { .. }
376            | MissingRegionOption { .. }
377            | ConflictRegionOption { .. }
378            | ColumnTypeMismatch { .. }
379            | TableIdCountMismatch { .. }
380            | PhysicalRegionBusy { .. }
381            | MultipleFieldColumn { .. }
382            | NoFieldColumn { .. }
383            | AddingFieldColumn { .. }
384            | ParseRegionOptions { .. }
385            | UnexpectedRequest { .. }
386            | UnsupportedAlterKind { .. }
387            | UnsupportedRemapManifestsRequest { .. }
388            | UnsupportedSyncRegionFromRequest { .. } => StatusCode::InvalidArguments,
389
390            ForbiddenPhysicalAlter { .. }
391            | UnsupportedRegionRequest { .. }
392            | MissingFiles { .. } => StatusCode::Unsupported,
393
394            DeserializeColumnMetadata { .. }
395            | SerializeColumnMetadata { .. }
396            | DecodeColumnValue { .. }
397            | ParseRegionId { .. }
398            | InvalidMetadata { .. }
399            | SetSkippingIndexOption { .. }
400            | SerializeRegionManifestInfo { .. }
401            | NoOpenRegionResult { .. } => StatusCode::Unexpected,
402
403            PhysicalRegionNotFound { .. } | LogicalRegionNotFound { .. } => {
404                StatusCode::RegionNotFound
405            }
406
407            ColumnNotFound { .. } => StatusCode::TableColumnNotFound,
408
409            CreateMitoRegion { source, .. }
410            | OpenMitoRegion { source, .. }
411            | CloseMitoRegion { source, .. }
412            | MitoReadOperation { source, .. }
413            | MitoWriteOperation { source, .. }
414            | MitoFlushOperation { source, .. }
415            | MitoSyncOperation { source, .. }
416            | MitoEnterStagingOperation { source, .. }
417            | BatchOpenMitoRegion { source, .. }
418            | BatchCatchupMitoRegion { source, .. }
419            | MitoCopyRegionFromOperation { source, .. }
420            | MitoEditRegion { source, .. } => source.status_code(),
421
422            EncodePrimaryKey { source, .. } => source.status_code(),
423
424            CollectRecordBatchStream { source, .. } => source.status_code(),
425
426            StartRepeatedTask { source, .. } => source.status_code(),
427
428            MetricManifestInfo { .. } => StatusCode::Internal,
429
430            CacheGet { source, .. } => source.status_code(),
431        }
432    }
433
434    fn as_any(&self) -> &dyn Any {
435        self
436    }
437}