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("Alter request to physical region is forbidden"))]
244    ForbiddenPhysicalAlter {
245        #[snafu(implicit)]
246        location: Location,
247    },
248
249    #[snafu(display("Invalid region metadata"))]
250    InvalidMetadata {
251        source: store_api::metadata::MetadataError,
252        #[snafu(implicit)]
253        location: Location,
254    },
255
256    #[snafu(display(
257        "Physical region {} is busy, there are still some logical regions using it",
258        region_id
259    ))]
260    PhysicalRegionBusy {
261        region_id: RegionId,
262        #[snafu(implicit)]
263        location: Location,
264    },
265
266    #[snafu(display("Unsupported region request: {}", request))]
267    UnsupportedRegionRequest {
268        request: Box<RegionRequest>,
269        #[snafu(implicit)]
270        location: Location,
271    },
272
273    #[snafu(display("Unsupported remap manifests request for region {}", region_id))]
274    UnsupportedRemapManifestsRequest {
275        region_id: RegionId,
276        #[snafu(implicit)]
277        location: Location,
278    },
279
280    #[snafu(display("Unsupported sync region from request for region {}", region_id))]
281    UnsupportedSyncRegionFromRequest {
282        region_id: RegionId,
283        #[snafu(implicit)]
284        location: Location,
285    },
286
287    #[snafu(display("Missing file metas in region {}, file ids: {:?}", region_id, file_ids))]
288    MissingFiles {
289        region_id: RegionId,
290        #[snafu(implicit)]
291        location: Location,
292        file_ids: Vec<FileId>,
293    },
294
295    #[snafu(display("Unsupported alter kind: {}", kind))]
296    UnsupportedAlterKind {
297        kind: String,
298        #[snafu(implicit)]
299        location: Location,
300    },
301
302    #[snafu(display("Multiple field column found: {} and {}", previous, current))]
303    MultipleFieldColumn {
304        previous: String,
305        current: String,
306        #[snafu(implicit)]
307        location: Location,
308    },
309
310    #[snafu(display("Adding field column {} to physical table", name))]
311    AddingFieldColumn {
312        name: String,
313        #[snafu(implicit)]
314        location: Location,
315    },
316
317    #[snafu(display("No field column found"))]
318    NoFieldColumn {
319        #[snafu(implicit)]
320        location: Location,
321    },
322
323    #[snafu(display("Failed to set SKIPPING index option"))]
324    SetSkippingIndexOption {
325        source: datatypes::error::Error,
326        #[snafu(implicit)]
327        location: Location,
328    },
329
330    #[snafu(display("Unexpected request: {}", reason))]
331    UnexpectedRequest {
332        reason: String,
333        #[snafu(implicit)]
334        location: Location,
335    },
336
337    #[snafu(display("Expected metric manifest info, region: {}", region_id))]
338    MetricManifestInfo {
339        region_id: RegionId,
340        #[snafu(implicit)]
341        location: Location,
342    },
343
344    #[snafu(display("Failed to start repeated task: {}", name))]
345    StartRepeatedTask {
346        name: String,
347        source: common_runtime::error::Error,
348        #[snafu(implicit)]
349        location: Location,
350    },
351
352    #[snafu(display("Get value from cache"))]
353    CacheGet {
354        source: Arc<Error>,
355        #[snafu(implicit)]
356        location: Location,
357    },
358}
359
360pub type Result<T, E = Error> = std::result::Result<T, E>;
361
362impl ErrorExt for Error {
363    fn status_code(&self) -> StatusCode {
364        use Error::*;
365
366        match self {
367            InternalColumnOccupied { .. }
368            | MissingRegionOption { .. }
369            | ConflictRegionOption { .. }
370            | ColumnTypeMismatch { .. }
371            | PhysicalRegionBusy { .. }
372            | MultipleFieldColumn { .. }
373            | NoFieldColumn { .. }
374            | AddingFieldColumn { .. }
375            | ParseRegionOptions { .. }
376            | UnexpectedRequest { .. }
377            | UnsupportedAlterKind { .. }
378            | UnsupportedRemapManifestsRequest { .. }
379            | UnsupportedSyncRegionFromRequest { .. } => StatusCode::InvalidArguments,
380
381            ForbiddenPhysicalAlter { .. }
382            | UnsupportedRegionRequest { .. }
383            | MissingFiles { .. } => StatusCode::Unsupported,
384
385            DeserializeColumnMetadata { .. }
386            | SerializeColumnMetadata { .. }
387            | DecodeColumnValue { .. }
388            | ParseRegionId { .. }
389            | InvalidMetadata { .. }
390            | SetSkippingIndexOption { .. }
391            | SerializeRegionManifestInfo { .. }
392            | NoOpenRegionResult { .. } => StatusCode::Unexpected,
393
394            PhysicalRegionNotFound { .. } | LogicalRegionNotFound { .. } => {
395                StatusCode::RegionNotFound
396            }
397
398            ColumnNotFound { .. } => StatusCode::TableColumnNotFound,
399
400            CreateMitoRegion { source, .. }
401            | OpenMitoRegion { source, .. }
402            | CloseMitoRegion { source, .. }
403            | MitoReadOperation { source, .. }
404            | MitoWriteOperation { source, .. }
405            | MitoFlushOperation { source, .. }
406            | MitoSyncOperation { source, .. }
407            | MitoEnterStagingOperation { source, .. }
408            | BatchOpenMitoRegion { source, .. }
409            | BatchCatchupMitoRegion { source, .. }
410            | MitoCopyRegionFromOperation { source, .. }
411            | MitoEditRegion { source, .. } => source.status_code(),
412
413            EncodePrimaryKey { source, .. } => source.status_code(),
414
415            CollectRecordBatchStream { source, .. } => source.status_code(),
416
417            StartRepeatedTask { source, .. } => source.status_code(),
418
419            MetricManifestInfo { .. } => StatusCode::Internal,
420
421            CacheGet { source, .. } => source.status_code(),
422        }
423    }
424
425    fn as_any(&self) -> &dyn Any {
426        self
427    }
428}