Skip to main content

mito2/
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_datasource::compression::CompressionType;
19use common_error::ext::{BoxedError, ErrorExt};
20use common_error::status_code::StatusCode;
21use common_macro::stack_trace_debug;
22use common_memory_manager;
23use common_runtime::JoinError;
24use common_time::Timestamp;
25use common_time::timestamp::TimeUnit;
26use datatypes::arrow::error::ArrowError;
27use datatypes::prelude::ConcreteDataType;
28use object_store::ErrorKind;
29use partition::error::Error as PartitionError;
30use prost::DecodeError;
31use snafu::{Location, Snafu};
32use store_api::ManifestVersion;
33use store_api::logstore::provider::Provider;
34use store_api::storage::{FileId, RegionId};
35use tokio::time::error::Elapsed;
36
37use crate::cache::file_cache::FileType;
38use crate::region::RegionRoleState;
39use crate::schedule::remote_job_scheduler::JobId;
40use crate::worker::WorkerId;
41
42#[derive(Snafu)]
43#[snafu(visibility(pub))]
44#[stack_trace_debug]
45pub enum Error {
46    #[snafu(display("Unexpected data type"))]
47    DataTypeMismatch {
48        source: datatypes::error::Error,
49        #[snafu(implicit)]
50        location: Location,
51    },
52
53    #[snafu(display("External error, context: {}", context))]
54    External {
55        source: BoxedError,
56        context: String,
57        #[snafu(implicit)]
58        location: Location,
59    },
60
61    #[snafu(display("Failed to encode sparse primary key, reason: {}", reason))]
62    EncodeSparsePrimaryKey {
63        reason: String,
64        #[snafu(implicit)]
65        location: Location,
66    },
67
68    #[snafu(display("OpenDAL operator failed"))]
69    OpenDal {
70        #[snafu(implicit)]
71        location: Location,
72        #[snafu(source)]
73        error: object_store::Error,
74    },
75
76    #[snafu(display("Fail to compress object by {}, path: {}", compress_type, path))]
77    CompressObject {
78        compress_type: CompressionType,
79        path: String,
80        #[snafu(source)]
81        error: std::io::Error,
82    },
83
84    #[snafu(display("Fail to decompress object by {}, path: {}", compress_type, path))]
85    DecompressObject {
86        compress_type: CompressionType,
87        path: String,
88        #[snafu(source)]
89        error: std::io::Error,
90    },
91
92    #[snafu(display("Failed to ser/de json object"))]
93    SerdeJson {
94        #[snafu(implicit)]
95        location: Location,
96        #[snafu(source)]
97        error: serde_json::Error,
98    },
99
100    #[snafu(display("Failed to serialize column metadata"))]
101    SerializeColumnMetadata {
102        #[snafu(source)]
103        error: serde_json::Error,
104        #[snafu(implicit)]
105        location: Location,
106    },
107
108    #[snafu(display("Failed to serialize manifest, region_id: {}", region_id))]
109    SerializeManifest {
110        region_id: RegionId,
111        #[snafu(source)]
112        error: serde_json::Error,
113        #[snafu(implicit)]
114        location: Location,
115    },
116
117    #[snafu(display("Invalid scan index, start: {}, end: {}", start, end))]
118    InvalidScanIndex {
119        start: ManifestVersion,
120        end: ManifestVersion,
121        #[snafu(implicit)]
122        location: Location,
123    },
124
125    #[snafu(display("Invalid UTF-8 content"))]
126    Utf8 {
127        #[snafu(implicit)]
128        location: Location,
129        #[snafu(source)]
130        error: std::str::Utf8Error,
131    },
132
133    #[snafu(display("Cannot find RegionMetadata"))]
134    RegionMetadataNotFound {
135        #[snafu(implicit)]
136        location: Location,
137    },
138
139    #[snafu(display("Failed to join handle"))]
140    Join {
141        #[snafu(source)]
142        error: common_runtime::JoinError,
143        #[snafu(implicit)]
144        location: Location,
145    },
146
147    #[snafu(display("Worker {} is stopped", id))]
148    WorkerStopped {
149        id: WorkerId,
150        #[snafu(implicit)]
151        location: Location,
152    },
153
154    #[snafu(display("Failed to recv result"))]
155    Recv {
156        #[snafu(source)]
157        error: tokio::sync::oneshot::error::RecvError,
158        #[snafu(implicit)]
159        location: Location,
160    },
161
162    #[snafu(display("Invalid metadata, {}", reason))]
163    InvalidMeta {
164        reason: String,
165        #[snafu(implicit)]
166        location: Location,
167    },
168
169    #[snafu(display("Invalid region metadata"))]
170    InvalidMetadata {
171        source: store_api::metadata::MetadataError,
172        #[snafu(implicit)]
173        location: Location,
174    },
175
176    #[snafu(display("Failed to create RecordBatch from vectors"))]
177    NewRecordBatch {
178        #[snafu(implicit)]
179        location: Location,
180        #[snafu(source)]
181        error: ArrowError,
182    },
183
184    #[snafu(display("Failed to read parquet file, path: {}", path))]
185    ReadParquet {
186        path: String,
187        #[snafu(source)]
188        error: parquet::errors::ParquetError,
189        #[snafu(implicit)]
190        location: Location,
191    },
192
193    #[snafu(display("Failed to write parquet file"))]
194    WriteParquet {
195        #[snafu(source)]
196        error: parquet::errors::ParquetError,
197        #[snafu(implicit)]
198        location: Location,
199    },
200
201    #[snafu(display("Region {} not found", region_id))]
202    RegionNotFound {
203        region_id: RegionId,
204        #[snafu(implicit)]
205        location: Location,
206    },
207
208    #[snafu(display("Object store not found: {}", object_store))]
209    ObjectStoreNotFound {
210        object_store: String,
211        #[snafu(implicit)]
212        location: Location,
213    },
214
215    #[snafu(display("Region {} is corrupted, reason: {}", region_id, reason))]
216    RegionCorrupted {
217        region_id: RegionId,
218        reason: String,
219        #[snafu(implicit)]
220        location: Location,
221    },
222
223    #[snafu(display("Invalid request to region {}, reason: {}", region_id, reason))]
224    InvalidRequest {
225        region_id: RegionId,
226        reason: String,
227        #[snafu(implicit)]
228        location: Location,
229    },
230
231    #[snafu(display("Old manifest missing for region {}", region_id))]
232    MissingOldManifest {
233        region_id: RegionId,
234        #[snafu(implicit)]
235        location: Location,
236    },
237
238    #[snafu(display("New manifest missing for region {}", region_id))]
239    MissingNewManifest {
240        region_id: RegionId,
241        #[snafu(implicit)]
242        location: Location,
243    },
244
245    #[snafu(display("Manifest missing for region {}", region_id))]
246    MissingManifest {
247        region_id: RegionId,
248        #[snafu(implicit)]
249        location: Location,
250    },
251
252    #[snafu(display("File consistency check failed for file {}: {}", file_id, reason))]
253    InconsistentFile {
254        file_id: FileId,
255        reason: String,
256        #[snafu(implicit)]
257        location: Location,
258    },
259
260    #[snafu(display("Files lost during remapping: old={}, new={}", old_count, new_count))]
261    FilesLost {
262        old_count: usize,
263        new_count: usize,
264        #[snafu(implicit)]
265        location: Location,
266    },
267
268    #[snafu(display("No old manifests provided (need at least one for template)"))]
269    NoOldManifests {
270        #[snafu(implicit)]
271        location: Location,
272    },
273
274    #[snafu(display("Failed to fetch manifests"))]
275    FetchManifests {
276        #[snafu(implicit)]
277        location: Location,
278        source: BoxedError,
279    },
280
281    #[snafu(display("Partition expression missing for region {}", region_id))]
282    MissingPartitionExpr {
283        region_id: RegionId,
284        #[snafu(implicit)]
285        location: Location,
286    },
287
288    #[snafu(display("Failed to serialize partition expression: {}", source))]
289    SerializePartitionExpr {
290        #[snafu(source)]
291        source: PartitionError,
292        #[snafu(implicit)]
293        location: Location,
294    },
295
296    #[snafu(display(
297        "Failed to convert ConcreteDataType to ColumnDataType, reason: {}",
298        reason
299    ))]
300    ConvertColumnDataType {
301        reason: String,
302        source: api::error::Error,
303        #[snafu(implicit)]
304        location: Location,
305    },
306
307    /// An error type to indicate that schema is changed and we need
308    /// to fill default values again.
309    #[snafu(display("Need to fill default value for region {}", region_id))]
310    FillDefault {
311        region_id: RegionId,
312        // The error is for internal use so we don't need a location.
313    },
314
315    #[snafu(display(
316        "Failed to create default value for column {} of region {}",
317        column,
318        region_id
319    ))]
320    CreateDefault {
321        region_id: RegionId,
322        column: String,
323        source: datatypes::Error,
324        #[snafu(implicit)]
325        location: Location,
326    },
327
328    #[snafu(display("Failed to build entry, region_id: {}", region_id))]
329    BuildEntry {
330        region_id: RegionId,
331        #[snafu(implicit)]
332        location: Location,
333        source: BoxedError,
334    },
335
336    #[snafu(display("Failed to write WAL"))]
337    WriteWal {
338        #[snafu(implicit)]
339        location: Location,
340        source: BoxedError,
341    },
342
343    #[snafu(display("Failed to read WAL, provider: {}", provider))]
344    ReadWal {
345        provider: Provider,
346        #[snafu(implicit)]
347        location: Location,
348        source: BoxedError,
349    },
350
351    #[snafu(display("Failed to decode WAL entry, region_id: {}", region_id))]
352    DecodeWal {
353        region_id: RegionId,
354        #[snafu(implicit)]
355        location: Location,
356        #[snafu(source)]
357        error: DecodeError,
358    },
359
360    #[snafu(display("Failed to delete WAL, region_id: {}", region_id))]
361    DeleteWal {
362        region_id: RegionId,
363        #[snafu(implicit)]
364        location: Location,
365        source: BoxedError,
366    },
367
368    // Shared error for each writer in the write group.
369    #[snafu(display("Failed to write region"))]
370    WriteGroup { source: Arc<Error> },
371
372    #[snafu(display("Invalid parquet SST file {}, reason: {}", file, reason))]
373    InvalidParquet {
374        file: String,
375        reason: String,
376        #[snafu(implicit)]
377        location: Location,
378    },
379
380    #[snafu(display("Invalid batch, {}", reason))]
381    InvalidBatch {
382        reason: String,
383        #[snafu(implicit)]
384        location: Location,
385    },
386
387    #[snafu(display("Invalid arrow record batch, {}", reason))]
388    InvalidRecordBatch {
389        reason: String,
390        #[snafu(implicit)]
391        location: Location,
392    },
393
394    #[snafu(display("Invalid wal read request, {}", reason))]
395    InvalidWalReadRequest {
396        reason: String,
397        #[snafu(implicit)]
398        location: Location,
399    },
400
401    #[snafu(display("Failed to convert array to vector"))]
402    ConvertVector {
403        #[snafu(implicit)]
404        location: Location,
405        source: datatypes::error::Error,
406    },
407
408    #[snafu(display("Failed to compute arrow arrays"))]
409    ComputeArrow {
410        #[snafu(implicit)]
411        location: Location,
412        #[snafu(source)]
413        error: datatypes::arrow::error::ArrowError,
414    },
415
416    #[snafu(display("Failed to evaluate partition filter"))]
417    EvalPartitionFilter {
418        #[snafu(implicit)]
419        location: Location,
420        #[snafu(source)]
421        error: datafusion::error::DataFusionError,
422    },
423
424    #[snafu(display("Failed to compute vector"))]
425    ComputeVector {
426        #[snafu(implicit)]
427        location: Location,
428        source: datatypes::error::Error,
429    },
430
431    #[snafu(display("Primary key length mismatch, expect: {}, actual: {}", expect, actual))]
432    PrimaryKeyLengthMismatch {
433        expect: usize,
434        actual: usize,
435        #[snafu(implicit)]
436        location: Location,
437    },
438
439    #[snafu(display("Invalid sender",))]
440    InvalidSender {
441        #[snafu(implicit)]
442        location: Location,
443    },
444
445    #[snafu(display("Invalid scheduler state"))]
446    InvalidSchedulerState {
447        #[snafu(implicit)]
448        location: Location,
449    },
450
451    #[snafu(display("Failed to stop scheduler"))]
452    StopScheduler {
453        #[snafu(source)]
454        error: JoinError,
455        #[snafu(implicit)]
456        location: Location,
457    },
458
459    #[snafu(display(
460        "Failed to batch delete SST files, region id: {}, file ids: {:?}",
461        region_id,
462        file_ids
463    ))]
464    DeleteSsts {
465        region_id: RegionId,
466        file_ids: Vec<FileId>,
467        #[snafu(source)]
468        error: object_store::Error,
469        #[snafu(implicit)]
470        location: Location,
471    },
472
473    #[snafu(display("Failed to delete index file, file id: {}", file_id))]
474    DeleteIndex {
475        file_id: FileId,
476        #[snafu(source)]
477        error: object_store::Error,
478        #[snafu(implicit)]
479        location: Location,
480    },
481
482    #[snafu(display("Failed to batch delete index files, file ids: {:?}", file_ids))]
483    DeleteIndexes {
484        file_ids: Vec<FileId>,
485        #[snafu(source)]
486        error: object_store::Error,
487        #[snafu(implicit)]
488        location: Location,
489    },
490
491    #[snafu(display("Failed to flush region {}", region_id))]
492    FlushRegion {
493        region_id: RegionId,
494        source: Arc<Error>,
495        #[snafu(implicit)]
496        location: Location,
497    },
498
499    #[snafu(display("Region {} is dropped", region_id))]
500    RegionDropped {
501        region_id: RegionId,
502        #[snafu(implicit)]
503        location: Location,
504    },
505
506    #[snafu(display("Region {} is closed", region_id))]
507    RegionClosed {
508        region_id: RegionId,
509        #[snafu(implicit)]
510        location: Location,
511    },
512
513    #[snafu(display("Region {} is truncated", region_id))]
514    RegionTruncated {
515        region_id: RegionId,
516        #[snafu(implicit)]
517        location: Location,
518    },
519
520    #[snafu(display(
521        "Engine write buffer is full, rejecting write requests of region {}",
522        region_id,
523    ))]
524    RejectWrite {
525        region_id: RegionId,
526        #[snafu(implicit)]
527        location: Location,
528    },
529
530    #[snafu(display("Failed to compact region {}", region_id))]
531    CompactRegion {
532        region_id: RegionId,
533        source: Arc<Error>,
534        #[snafu(implicit)]
535        location: Location,
536    },
537
538    #[snafu(display(
539        "Failed to compat readers for region {}, reason: {}",
540        region_id,
541        reason,
542    ))]
543    CompatReader {
544        region_id: RegionId,
545        reason: String,
546        #[snafu(implicit)]
547        location: Location,
548    },
549
550    #[snafu(display("Invalue region req"))]
551    InvalidRegionRequest {
552        source: store_api::metadata::MetadataError,
553        #[snafu(implicit)]
554        location: Location,
555    },
556
557    #[snafu(display(
558        "Region {} is in {:?} state, which does not permit manifest updates.",
559        region_id,
560        state
561    ))]
562    UpdateManifest {
563        region_id: RegionId,
564        state: RegionRoleState,
565        #[snafu(implicit)]
566        location: Location,
567    },
568
569    #[snafu(display("Region {} is in {:?} state, expect: {:?}", region_id, state, expect))]
570    RegionState {
571        region_id: RegionId,
572        state: RegionRoleState,
573        expect: RegionRoleState,
574        #[snafu(implicit)]
575        location: Location,
576    },
577
578    #[snafu(display(
579        "Partition expr version mismatch for region {}: request {}, expected {}",
580        region_id,
581        request_version,
582        expected_version
583    ))]
584    PartitionExprVersionMismatch {
585        region_id: RegionId,
586        request_version: u64,
587        expected_version: u64,
588        #[snafu(implicit)]
589        location: Location,
590    },
591
592    #[snafu(display("Invalid options"))]
593    JsonOptions {
594        #[snafu(source)]
595        error: serde_json::Error,
596        #[snafu(implicit)]
597        location: Location,
598    },
599
600    #[snafu(display(
601        "Empty region directory, region_id: {}, region_dir: {}",
602        region_id,
603        region_dir,
604    ))]
605    EmptyRegionDir {
606        region_id: RegionId,
607        region_dir: String,
608        #[snafu(implicit)]
609        location: Location,
610    },
611
612    #[snafu(display("Empty manifest directory, manifest_dir: {}", manifest_dir,))]
613    EmptyManifestDir {
614        manifest_dir: String,
615        #[snafu(implicit)]
616        location: Location,
617    },
618
619    #[snafu(display("Column not found, column: {column}"))]
620    ColumnNotFound {
621        column: String,
622        #[snafu(implicit)]
623        location: Location,
624    },
625
626    #[snafu(display("Failed to build index applier"))]
627    BuildIndexApplier {
628        source: index::inverted_index::error::Error,
629        #[snafu(implicit)]
630        location: Location,
631    },
632
633    #[snafu(display("Failed to build index asynchronously in region {}", region_id))]
634    BuildIndexAsync {
635        region_id: RegionId,
636        source: Arc<Error>,
637        #[snafu(implicit)]
638        location: Location,
639    },
640
641    #[snafu(display("Failed to convert value"))]
642    ConvertValue {
643        source: datatypes::error::Error,
644        #[snafu(implicit)]
645        location: Location,
646    },
647
648    #[snafu(display("Failed to apply inverted index"))]
649    ApplyInvertedIndex {
650        source: index::inverted_index::error::Error,
651        #[snafu(implicit)]
652        location: Location,
653    },
654
655    #[snafu(display("Failed to apply bloom filter index"))]
656    ApplyBloomFilterIndex {
657        source: index::bloom_filter::error::Error,
658        #[snafu(implicit)]
659        location: Location,
660    },
661
662    #[cfg(feature = "vector_index")]
663    #[snafu(display("Failed to apply vector index: {}", reason))]
664    ApplyVectorIndex {
665        reason: String,
666        #[snafu(implicit)]
667        location: Location,
668    },
669
670    #[snafu(display("Failed to push index value"))]
671    PushIndexValue {
672        source: index::inverted_index::error::Error,
673        #[snafu(implicit)]
674        location: Location,
675    },
676
677    #[snafu(display("Failed to write index completely"))]
678    IndexFinish {
679        source: index::inverted_index::error::Error,
680        #[snafu(implicit)]
681        location: Location,
682    },
683
684    #[snafu(display("Operate on aborted index"))]
685    OperateAbortedIndex {
686        #[snafu(implicit)]
687        location: Location,
688    },
689
690    #[snafu(display("Failed to read puffin blob"))]
691    PuffinReadBlob {
692        source: puffin::error::Error,
693        #[snafu(implicit)]
694        location: Location,
695    },
696
697    #[snafu(display("Failed to add blob to puffin file"))]
698    PuffinAddBlob {
699        source: puffin::error::Error,
700        #[snafu(implicit)]
701        location: Location,
702    },
703
704    #[snafu(display("Failed to clean dir {dir}"))]
705    CleanDir {
706        dir: String,
707        #[snafu(source)]
708        error: std::io::Error,
709        #[snafu(implicit)]
710        location: Location,
711    },
712
713    #[snafu(display("Invalid config, {reason}"))]
714    InvalidConfig {
715        reason: String,
716        #[snafu(implicit)]
717        location: Location,
718    },
719
720    #[snafu(display(
721        "Stale log entry found during replay, region: {}, flushed: {}, replayed: {}",
722        region_id,
723        flushed_entry_id,
724        unexpected_entry_id
725    ))]
726    StaleLogEntry {
727        region_id: RegionId,
728        flushed_entry_id: u64,
729        unexpected_entry_id: u64,
730    },
731
732    #[snafu(display(
733        "Failed to download file, region_id: {}, file_id: {}, file_type: {:?}",
734        region_id,
735        file_id,
736        file_type,
737    ))]
738    Download {
739        region_id: RegionId,
740        file_id: FileId,
741        file_type: FileType,
742        #[snafu(source)]
743        error: std::io::Error,
744        #[snafu(implicit)]
745        location: Location,
746    },
747
748    #[snafu(display(
749        "Failed to upload file, region_id: {}, file_id: {}, file_type: {:?}",
750        region_id,
751        file_id,
752        file_type,
753    ))]
754    Upload {
755        region_id: RegionId,
756        file_id: FileId,
757        file_type: FileType,
758        #[snafu(source)]
759        error: std::io::Error,
760        #[snafu(implicit)]
761        location: Location,
762    },
763
764    #[snafu(display("Failed to create directory {}", dir))]
765    CreateDir {
766        dir: String,
767        #[snafu(source)]
768        error: std::io::Error,
769    },
770
771    #[snafu(display("Record batch error"))]
772    RecordBatch {
773        source: common_recordbatch::error::Error,
774        #[snafu(implicit)]
775        location: Location,
776    },
777
778    #[snafu(display("BiErrors, first: {first}, second: {second}"))]
779    BiErrors {
780        first: Box<Error>,
781        second: Box<Error>,
782        #[snafu(implicit)]
783        location: Location,
784    },
785
786    #[snafu(display("Encode null value"))]
787    IndexEncodeNull {
788        #[snafu(implicit)]
789        location: Location,
790    },
791
792    #[snafu(display("Failed to encode memtable to Parquet bytes"))]
793    EncodeMemtable {
794        #[snafu(source)]
795        error: parquet::errors::ParquetError,
796        #[snafu(implicit)]
797        location: Location,
798    },
799
800    #[snafu(display("Partition {} out of range, {} in total", given, all))]
801    PartitionOutOfRange {
802        given: usize,
803        all: usize,
804        #[snafu(implicit)]
805        location: Location,
806    },
807
808    #[snafu(display("Failed to iter data part"))]
809    ReadDataPart {
810        #[snafu(implicit)]
811        location: Location,
812        #[snafu(source)]
813        error: parquet::errors::ParquetError,
814    },
815
816    #[snafu(display("Failed to read row group in memtable"))]
817    DecodeArrowRowGroup {
818        #[snafu(source)]
819        error: ArrowError,
820        #[snafu(implicit)]
821        location: Location,
822    },
823
824    #[snafu(display("Invalid region options, {}", reason))]
825    InvalidRegionOptions {
826        reason: String,
827        #[snafu(implicit)]
828        location: Location,
829    },
830
831    #[snafu(display("checksum mismatch (actual: {}, expected: {})", actual, expected))]
832    ChecksumMismatch { actual: u32, expected: u32 },
833
834    #[snafu(display(
835        "No checkpoint found, region: {}, last_version: {}",
836        region_id,
837        last_version
838    ))]
839    NoCheckpoint {
840        region_id: RegionId,
841        last_version: ManifestVersion,
842        #[snafu(implicit)]
843        location: Location,
844    },
845
846    #[snafu(display(
847        "No manifests found in range: [{}..{}), region: {}, last_version: {}",
848        start_version,
849        end_version,
850        region_id,
851        last_version
852    ))]
853    NoManifests {
854        region_id: RegionId,
855        start_version: ManifestVersion,
856        end_version: ManifestVersion,
857        last_version: ManifestVersion,
858        #[snafu(implicit)]
859        location: Location,
860    },
861
862    #[snafu(display(
863        "Failed to install manifest to {}, region: {}, available manifest version: {}, last version: {}",
864        target_version,
865        region_id,
866        available_version,
867        last_version
868    ))]
869    InstallManifestTo {
870        region_id: RegionId,
871        target_version: ManifestVersion,
872        available_version: ManifestVersion,
873        #[snafu(implicit)]
874        location: Location,
875        last_version: ManifestVersion,
876    },
877
878    #[snafu(display("Region {} is stopped", region_id))]
879    RegionStopped {
880        region_id: RegionId,
881        #[snafu(implicit)]
882        location: Location,
883    },
884
885    #[snafu(display(
886        "Time range predicate overflows, timestamp: {:?}, target unit: {}",
887        timestamp,
888        unit
889    ))]
890    TimeRangePredicateOverflow {
891        timestamp: Timestamp,
892        unit: TimeUnit,
893        #[snafu(implicit)]
894        location: Location,
895    },
896
897    #[snafu(display("Failed to open region"))]
898    OpenRegion {
899        #[snafu(implicit)]
900        location: Location,
901        source: Arc<Error>,
902    },
903
904    #[snafu(display("Failed to parse job id"))]
905    ParseJobId {
906        #[snafu(implicit)]
907        location: Location,
908        #[snafu(source)]
909        error: uuid::Error,
910    },
911
912    #[snafu(display("Operation is not supported: {}", err_msg))]
913    UnsupportedOperation {
914        err_msg: String,
915        #[snafu(implicit)]
916        location: Location,
917    },
918
919    #[snafu(display(
920        "Failed to remotely compact region {} by job {:?} due to {}",
921        region_id,
922        job_id,
923        reason
924    ))]
925    RemoteCompaction {
926        region_id: RegionId,
927        job_id: Option<JobId>,
928        reason: String,
929        #[snafu(implicit)]
930        location: Location,
931    },
932
933    #[snafu(display("Failed to initialize puffin stager"))]
934    PuffinInitStager {
935        source: puffin::error::Error,
936        #[snafu(implicit)]
937        location: Location,
938    },
939
940    #[snafu(display("Failed to purge puffin stager"))]
941    PuffinPurgeStager {
942        source: puffin::error::Error,
943        #[snafu(implicit)]
944        location: Location,
945    },
946
947    #[snafu(display("Failed to build puffin reader"))]
948    PuffinBuildReader {
949        source: puffin::error::Error,
950        #[snafu(implicit)]
951        location: Location,
952    },
953
954    #[snafu(display("Failed to retrieve index options from column metadata"))]
955    IndexOptions {
956        #[snafu(implicit)]
957        location: Location,
958        source: datatypes::error::Error,
959        column_name: String,
960    },
961
962    #[snafu(display("Failed to create fulltext index creator"))]
963    CreateFulltextCreator {
964        source: index::fulltext_index::error::Error,
965        #[snafu(implicit)]
966        location: Location,
967    },
968
969    #[snafu(display("Failed to cast vector of {from} to {to}"))]
970    CastVector {
971        #[snafu(implicit)]
972        location: Location,
973        from: ConcreteDataType,
974        to: ConcreteDataType,
975        source: datatypes::error::Error,
976    },
977
978    #[snafu(display("Failed to push text to fulltext index"))]
979    FulltextPushText {
980        source: index::fulltext_index::error::Error,
981        #[snafu(implicit)]
982        location: Location,
983    },
984
985    #[snafu(display("Failed to finalize fulltext index creator"))]
986    FulltextFinish {
987        source: index::fulltext_index::error::Error,
988        #[snafu(implicit)]
989        location: Location,
990    },
991
992    #[snafu(display("Failed to apply fulltext index"))]
993    ApplyFulltextIndex {
994        source: index::fulltext_index::error::Error,
995        #[snafu(implicit)]
996        location: Location,
997    },
998
999    #[snafu(display("SST file {} does not contain valid stats info", file_path))]
1000    StatsNotPresent {
1001        file_path: String,
1002        #[snafu(implicit)]
1003        location: Location,
1004    },
1005
1006    #[snafu(display("Failed to decode stats of file {}", file_path))]
1007    DecodeStats {
1008        file_path: String,
1009        #[snafu(implicit)]
1010        location: Location,
1011    },
1012
1013    #[snafu(display("Region {} is busy", region_id))]
1014    RegionBusy {
1015        region_id: RegionId,
1016        #[snafu(implicit)]
1017        location: Location,
1018    },
1019
1020    #[snafu(display("Failed to get schema metadata"))]
1021    GetSchemaMetadata {
1022        source: common_meta::error::Error,
1023        #[snafu(implicit)]
1024        location: Location,
1025    },
1026
1027    #[snafu(display("Timeout"))]
1028    Timeout {
1029        #[snafu(source)]
1030        error: Elapsed,
1031        #[snafu(implicit)]
1032        location: Location,
1033    },
1034
1035    #[snafu(display("Failed to read file metadata"))]
1036    Metadata {
1037        #[snafu(source)]
1038        error: std::io::Error,
1039        #[snafu(implicit)]
1040        location: Location,
1041    },
1042
1043    #[snafu(display("Failed to push value to bloom filter"))]
1044    PushBloomFilterValue {
1045        source: index::bloom_filter::error::Error,
1046        #[snafu(implicit)]
1047        location: Location,
1048    },
1049
1050    #[snafu(display("Failed to finish bloom filter"))]
1051    BloomFilterFinish {
1052        source: index::bloom_filter::error::Error,
1053        #[snafu(implicit)]
1054        location: Location,
1055    },
1056
1057    #[cfg(feature = "vector_index")]
1058    #[snafu(display("Failed to build vector index: {}", reason))]
1059    VectorIndexBuild {
1060        reason: String,
1061        #[snafu(implicit)]
1062        location: Location,
1063    },
1064
1065    #[cfg(feature = "vector_index")]
1066    #[snafu(display("Failed to finish vector index: {}", reason))]
1067    VectorIndexFinish {
1068        reason: String,
1069        #[snafu(implicit)]
1070        location: Location,
1071    },
1072
1073    #[snafu(display("Manual compaction is override by following operations."))]
1074    ManualCompactionOverride {},
1075
1076    #[snafu(display("Compaction memory exhausted for region {region_id} (policy: {policy})",))]
1077    CompactionMemoryExhausted {
1078        region_id: RegionId,
1079        policy: String,
1080        #[snafu(source)]
1081        source: common_memory_manager::Error,
1082        #[snafu(implicit)]
1083        location: Location,
1084    },
1085
1086    #[snafu(display(
1087        "Incompatible WAL provider change. This is typically caused by changing WAL provider in database config file without completely cleaning existing files. Global provider: {}, region provider: {}",
1088        global,
1089        region
1090    ))]
1091    IncompatibleWalProviderChange { global: String, region: String },
1092
1093    #[snafu(display("Expected mito manifest info"))]
1094    MitoManifestInfo {
1095        #[snafu(implicit)]
1096        location: Location,
1097    },
1098
1099    #[snafu(display("Failed to scan series"))]
1100    ScanSeries {
1101        #[snafu(implicit)]
1102        location: Location,
1103        source: Arc<Error>,
1104    },
1105
1106    #[snafu(display("Partition {} scan multiple times", partition))]
1107    ScanMultiTimes {
1108        partition: usize,
1109        #[snafu(implicit)]
1110        location: Location,
1111    },
1112
1113    #[snafu(display("Invalid partition expression: {}", expr))]
1114    InvalidPartitionExpr {
1115        expr: String,
1116        #[snafu(implicit)]
1117        location: Location,
1118        source: partition::error::Error,
1119    },
1120
1121    #[snafu(display("Failed to decode bulk wal entry"))]
1122    ConvertBulkWalEntry {
1123        #[snafu(implicit)]
1124        location: Location,
1125        source: common_grpc::Error,
1126    },
1127
1128    #[snafu(display("Failed to encode"))]
1129    Encode {
1130        #[snafu(implicit)]
1131        location: Location,
1132        source: mito_codec::error::Error,
1133    },
1134
1135    #[snafu(display("Failed to decode"))]
1136    Decode {
1137        #[snafu(implicit)]
1138        location: Location,
1139        source: mito_codec::error::Error,
1140    },
1141
1142    #[snafu(display("Unexpected: {reason}"))]
1143    Unexpected {
1144        reason: String,
1145        #[snafu(implicit)]
1146        location: Location,
1147    },
1148
1149    #[cfg(feature = "enterprise")]
1150    #[snafu(display("Failed to scan external range"))]
1151    ScanExternalRange {
1152        source: BoxedError,
1153        #[snafu(implicit)]
1154        location: Location,
1155    },
1156
1157    #[snafu(display(
1158        "Inconsistent timestamp column length, expect: {}, actual: {}",
1159        expected,
1160        actual
1161    ))]
1162    InconsistentTimestampLength {
1163        expected: usize,
1164        actual: usize,
1165        #[snafu(implicit)]
1166        location: Location,
1167    },
1168
1169    #[snafu(display(
1170        "Too many files to read concurrently: {}, max allowed: {}",
1171        actual,
1172        max
1173    ))]
1174    TooManyFilesToRead {
1175        actual: usize,
1176        max: usize,
1177        #[snafu(implicit)]
1178        location: Location,
1179    },
1180
1181    #[snafu(display("Duration out of range: {input:?}"))]
1182    DurationOutOfRange {
1183        input: std::time::Duration,
1184        #[snafu(source)]
1185        error: chrono::OutOfRangeError,
1186        #[snafu(implicit)]
1187        location: Location,
1188    },
1189
1190    #[snafu(display("GC job permit exhausted"))]
1191    TooManyGcJobs {
1192        #[snafu(implicit)]
1193        location: Location,
1194    },
1195
1196    #[snafu(display(
1197        "Staging partition expr mismatch, manifest: {:?}, request: {}",
1198        manifest_expr,
1199        request_expr
1200    ))]
1201    StagingPartitionExprMismatch {
1202        manifest_expr: Option<String>,
1203        request_expr: String,
1204        #[snafu(implicit)]
1205        location: Location,
1206    },
1207
1208    #[snafu(display(
1209        "Invalid source and target region, source: {}, target: {}",
1210        source_region_id,
1211        target_region_id
1212    ))]
1213    InvalidSourceAndTargetRegion {
1214        source_region_id: RegionId,
1215        target_region_id: RegionId,
1216        #[snafu(implicit)]
1217        location: Location,
1218    },
1219
1220    #[snafu(display("Failed to prune file"))]
1221    PruneFile {
1222        source: Arc<Error>,
1223        #[snafu(implicit)]
1224        location: Location,
1225    },
1226}
1227
1228pub type Result<T, E = Error> = std::result::Result<T, E>;
1229
1230impl Error {
1231    /// Returns true if we need to fill default value for a region.
1232    pub(crate) fn is_fill_default(&self) -> bool {
1233        matches!(self, Error::FillDefault { .. })
1234    }
1235
1236    /// Returns true if the file is not found on the object store.
1237    pub(crate) fn is_object_not_found(&self) -> bool {
1238        match self {
1239            Error::OpenDal { error, .. } => error.kind() == ErrorKind::NotFound,
1240            _ => false,
1241        }
1242    }
1243}
1244
1245impl ErrorExt for Error {
1246    fn status_code(&self) -> StatusCode {
1247        use Error::*;
1248
1249        match self {
1250            DataTypeMismatch { source, .. } => source.status_code(),
1251            OpenDal { .. } | ReadParquet { .. } => StatusCode::StorageUnavailable,
1252            WriteWal { source, .. } | ReadWal { source, .. } | DeleteWal { source, .. } => {
1253                source.status_code()
1254            }
1255            CompressObject { .. }
1256            | DecompressObject { .. }
1257            | SerdeJson { .. }
1258            | Utf8 { .. }
1259            | NewRecordBatch { .. }
1260            | RegionCorrupted { .. }
1261            | InconsistentFile { .. }
1262            | CreateDefault { .. }
1263            | InvalidParquet { .. }
1264            | OperateAbortedIndex { .. }
1265            | IndexEncodeNull { .. }
1266            | NoCheckpoint { .. }
1267            | NoManifests { .. }
1268            | FilesLost { .. }
1269            | InstallManifestTo { .. }
1270            | Unexpected { .. }
1271            | SerializeColumnMetadata { .. }
1272            | SerializeManifest { .. }
1273            | StagingPartitionExprMismatch { .. } => StatusCode::Unexpected,
1274
1275            RegionNotFound { .. } => StatusCode::RegionNotFound,
1276            ObjectStoreNotFound { .. }
1277            | InvalidScanIndex { .. }
1278            | InvalidMeta { .. }
1279            | InvalidRequest { .. }
1280            | PartitionExprVersionMismatch { .. }
1281            | FillDefault { .. }
1282            | ConvertColumnDataType { .. }
1283            | ColumnNotFound { .. }
1284            | InvalidMetadata { .. }
1285            | InvalidRegionOptions { .. }
1286            | InvalidWalReadRequest { .. }
1287            | PartitionOutOfRange { .. }
1288            | ParseJobId { .. }
1289            | DurationOutOfRange { .. }
1290            | MissingOldManifest { .. }
1291            | MissingNewManifest { .. }
1292            | MissingManifest { .. }
1293            | NoOldManifests { .. }
1294            | MissingPartitionExpr { .. }
1295            | SerializePartitionExpr { .. }
1296            | InvalidSourceAndTargetRegion { .. } => StatusCode::InvalidArguments,
1297
1298            RegionMetadataNotFound { .. }
1299            | Join { .. }
1300            | WorkerStopped { .. }
1301            | Recv { .. }
1302            | DecodeWal { .. }
1303            | ComputeArrow { .. }
1304            | EvalPartitionFilter { .. }
1305            | BiErrors { .. }
1306            | StopScheduler { .. }
1307            | ComputeVector { .. }
1308            | EncodeMemtable { .. }
1309            | CreateDir { .. }
1310            | ReadDataPart { .. }
1311            | BuildEntry { .. }
1312            | Metadata { .. }
1313            | MitoManifestInfo { .. } => StatusCode::Internal,
1314
1315            FetchManifests { source, .. } => source.status_code(),
1316
1317            OpenRegion { source, .. } => source.status_code(),
1318
1319            WriteParquet { .. } => StatusCode::StorageUnavailable,
1320            WriteGroup { source, .. } => source.status_code(),
1321            EncodeSparsePrimaryKey { .. } => StatusCode::Unexpected,
1322            InvalidBatch { .. } => StatusCode::InvalidArguments,
1323            InvalidRecordBatch { .. } => StatusCode::InvalidArguments,
1324            ConvertVector { source, .. } => source.status_code(),
1325
1326            PrimaryKeyLengthMismatch { .. } => StatusCode::InvalidArguments,
1327            InvalidSender { .. } => StatusCode::InvalidArguments,
1328            InvalidSchedulerState { .. } => StatusCode::InvalidArguments,
1329            DeleteSsts { .. } | DeleteIndex { .. } | DeleteIndexes { .. } => {
1330                StatusCode::StorageUnavailable
1331            }
1332            FlushRegion { source, .. } | BuildIndexAsync { source, .. } => source.status_code(),
1333            RegionDropped { .. } => StatusCode::Cancelled,
1334            RegionClosed { .. } => StatusCode::Cancelled,
1335            RegionTruncated { .. } => StatusCode::Cancelled,
1336            RejectWrite { .. } => StatusCode::StorageUnavailable,
1337            CompactRegion { source, .. } => source.status_code(),
1338            CompatReader { .. } => StatusCode::Unexpected,
1339            InvalidRegionRequest { source, .. } => source.status_code(),
1340            RegionState { .. } | UpdateManifest { .. } => StatusCode::RegionNotReady,
1341            JsonOptions { .. } => StatusCode::InvalidArguments,
1342            EmptyRegionDir { .. } | EmptyManifestDir { .. } => StatusCode::RegionNotFound,
1343            ConvertValue { source, .. } => source.status_code(),
1344            ApplyBloomFilterIndex { source, .. } => source.status_code(),
1345            InvalidPartitionExpr { source, .. } => source.status_code(),
1346            BuildIndexApplier { source, .. }
1347            | PushIndexValue { source, .. }
1348            | ApplyInvertedIndex { source, .. }
1349            | IndexFinish { source, .. } => source.status_code(),
1350            #[cfg(feature = "vector_index")]
1351            ApplyVectorIndex { .. } => StatusCode::Internal,
1352            PuffinReadBlob { source, .. }
1353            | PuffinAddBlob { source, .. }
1354            | PuffinInitStager { source, .. }
1355            | PuffinBuildReader { source, .. }
1356            | PuffinPurgeStager { source, .. } => source.status_code(),
1357            CleanDir { .. } => StatusCode::Unexpected,
1358            InvalidConfig { .. } => StatusCode::InvalidArguments,
1359            StaleLogEntry { .. } => StatusCode::Unexpected,
1360
1361            External { source, .. } => source.status_code(),
1362
1363            RecordBatch { source, .. } => source.status_code(),
1364
1365            Download { .. } | Upload { .. } => StatusCode::StorageUnavailable,
1366            ChecksumMismatch { .. } => StatusCode::Unexpected,
1367            RegionStopped { .. } => StatusCode::RegionNotReady,
1368            TimeRangePredicateOverflow { .. } => StatusCode::InvalidArguments,
1369            UnsupportedOperation { .. } => StatusCode::Unsupported,
1370            RemoteCompaction { .. } => StatusCode::Unexpected,
1371
1372            IndexOptions { source, .. } => source.status_code(),
1373            CreateFulltextCreator { source, .. } => source.status_code(),
1374            CastVector { source, .. } => source.status_code(),
1375            FulltextPushText { source, .. }
1376            | FulltextFinish { source, .. }
1377            | ApplyFulltextIndex { source, .. } => source.status_code(),
1378            DecodeStats { .. } | StatsNotPresent { .. } => StatusCode::Internal,
1379            RegionBusy { .. } => StatusCode::RegionBusy,
1380            GetSchemaMetadata { source, .. } => source.status_code(),
1381            Timeout { .. } => StatusCode::Cancelled,
1382
1383            DecodeArrowRowGroup { .. } => StatusCode::Internal,
1384
1385            PushBloomFilterValue { source, .. } | BloomFilterFinish { source, .. } => {
1386                source.status_code()
1387            }
1388
1389            #[cfg(feature = "vector_index")]
1390            VectorIndexBuild { .. } | VectorIndexFinish { .. } => StatusCode::Internal,
1391
1392            ManualCompactionOverride {} => StatusCode::Cancelled,
1393
1394            CompactionMemoryExhausted { source, .. } => source.status_code(),
1395
1396            IncompatibleWalProviderChange { .. } => StatusCode::InvalidArguments,
1397
1398            ScanSeries { source, .. } => source.status_code(),
1399
1400            ScanMultiTimes { .. } => StatusCode::InvalidArguments,
1401            ConvertBulkWalEntry { source, .. } => source.status_code(),
1402
1403            Encode { source, .. } | Decode { source, .. } => source.status_code(),
1404
1405            #[cfg(feature = "enterprise")]
1406            ScanExternalRange { source, .. } => source.status_code(),
1407
1408            InconsistentTimestampLength { .. } => StatusCode::InvalidArguments,
1409
1410            TooManyFilesToRead { .. } | TooManyGcJobs { .. } => StatusCode::RateLimited,
1411
1412            PruneFile { source, .. } => source.status_code(),
1413        }
1414    }
1415
1416    fn as_any(&self) -> &dyn Any {
1417        self
1418    }
1419}