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