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