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 compute vector"))]
417    ComputeVector {
418        #[snafu(implicit)]
419        location: Location,
420        source: datatypes::error::Error,
421    },
422
423    #[snafu(display("Primary key length mismatch, expect: {}, actual: {}", expect, actual))]
424    PrimaryKeyLengthMismatch {
425        expect: usize,
426        actual: usize,
427        #[snafu(implicit)]
428        location: Location,
429    },
430
431    #[snafu(display("Invalid sender",))]
432    InvalidSender {
433        #[snafu(implicit)]
434        location: Location,
435    },
436
437    #[snafu(display("Invalid scheduler state"))]
438    InvalidSchedulerState {
439        #[snafu(implicit)]
440        location: Location,
441    },
442
443    #[snafu(display("Failed to stop scheduler"))]
444    StopScheduler {
445        #[snafu(source)]
446        error: JoinError,
447        #[snafu(implicit)]
448        location: Location,
449    },
450
451    #[snafu(display("Failed to delete SST file, file id: {}", file_id))]
452    DeleteSst {
453        file_id: FileId,
454        #[snafu(source)]
455        error: object_store::Error,
456        #[snafu(implicit)]
457        location: Location,
458    },
459
460    #[snafu(display("Failed to delete index file, file id: {}", file_id))]
461    DeleteIndex {
462        file_id: FileId,
463        #[snafu(source)]
464        error: object_store::Error,
465        #[snafu(implicit)]
466        location: Location,
467    },
468
469    #[snafu(display("Failed to flush region {}", region_id))]
470    FlushRegion {
471        region_id: RegionId,
472        source: Arc<Error>,
473        #[snafu(implicit)]
474        location: Location,
475    },
476
477    #[snafu(display("Region {} is dropped", region_id))]
478    RegionDropped {
479        region_id: RegionId,
480        #[snafu(implicit)]
481        location: Location,
482    },
483
484    #[snafu(display("Region {} is closed", region_id))]
485    RegionClosed {
486        region_id: RegionId,
487        #[snafu(implicit)]
488        location: Location,
489    },
490
491    #[snafu(display("Region {} is truncated", region_id))]
492    RegionTruncated {
493        region_id: RegionId,
494        #[snafu(implicit)]
495        location: Location,
496    },
497
498    #[snafu(display(
499        "Engine write buffer is full, rejecting write requests of region {}",
500        region_id,
501    ))]
502    RejectWrite {
503        region_id: RegionId,
504        #[snafu(implicit)]
505        location: Location,
506    },
507
508    #[snafu(display("Failed to compact region {}", region_id))]
509    CompactRegion {
510        region_id: RegionId,
511        source: Arc<Error>,
512        #[snafu(implicit)]
513        location: Location,
514    },
515
516    #[snafu(display(
517        "Failed to compat readers for region {}, reason: {}",
518        region_id,
519        reason,
520    ))]
521    CompatReader {
522        region_id: RegionId,
523        reason: String,
524        #[snafu(implicit)]
525        location: Location,
526    },
527
528    #[snafu(display("Invalue region req"))]
529    InvalidRegionRequest {
530        source: store_api::metadata::MetadataError,
531        #[snafu(implicit)]
532        location: Location,
533    },
534
535    #[snafu(display(
536        "Region {} is in {:?} state, which does not permit manifest updates.",
537        region_id,
538        state
539    ))]
540    UpdateManifest {
541        region_id: RegionId,
542        state: RegionRoleState,
543        #[snafu(implicit)]
544        location: Location,
545    },
546
547    #[snafu(display("Region {} is in {:?} state, expect: {:?}", region_id, state, expect))]
548    RegionState {
549        region_id: RegionId,
550        state: RegionRoleState,
551        expect: RegionRoleState,
552        #[snafu(implicit)]
553        location: Location,
554    },
555
556    #[snafu(display("Invalid options"))]
557    JsonOptions {
558        #[snafu(source)]
559        error: serde_json::Error,
560        #[snafu(implicit)]
561        location: Location,
562    },
563
564    #[snafu(display(
565        "Empty region directory, region_id: {}, region_dir: {}",
566        region_id,
567        region_dir,
568    ))]
569    EmptyRegionDir {
570        region_id: RegionId,
571        region_dir: String,
572        #[snafu(implicit)]
573        location: Location,
574    },
575
576    #[snafu(display("Empty manifest directory, manifest_dir: {}", manifest_dir,))]
577    EmptyManifestDir {
578        manifest_dir: String,
579        #[snafu(implicit)]
580        location: Location,
581    },
582
583    #[snafu(display("Failed to read arrow record batch from parquet file {}", path))]
584    ArrowReader {
585        path: String,
586        #[snafu(source)]
587        error: ArrowError,
588        #[snafu(implicit)]
589        location: Location,
590    },
591
592    #[snafu(display("Column not found, column: {column}"))]
593    ColumnNotFound {
594        column: String,
595        #[snafu(implicit)]
596        location: Location,
597    },
598
599    #[snafu(display("Failed to build index applier"))]
600    BuildIndexApplier {
601        source: index::inverted_index::error::Error,
602        #[snafu(implicit)]
603        location: Location,
604    },
605
606    #[snafu(display("Failed to build index asynchronously in region {}", region_id))]
607    BuildIndexAsync {
608        region_id: RegionId,
609        source: Arc<Error>,
610        #[snafu(implicit)]
611        location: Location,
612    },
613
614    #[snafu(display("Failed to convert value"))]
615    ConvertValue {
616        source: datatypes::error::Error,
617        #[snafu(implicit)]
618        location: Location,
619    },
620
621    #[snafu(display("Failed to apply inverted index"))]
622    ApplyInvertedIndex {
623        source: index::inverted_index::error::Error,
624        #[snafu(implicit)]
625        location: Location,
626    },
627
628    #[snafu(display("Failed to apply bloom filter index"))]
629    ApplyBloomFilterIndex {
630        source: index::bloom_filter::error::Error,
631        #[snafu(implicit)]
632        location: Location,
633    },
634
635    #[cfg(feature = "vector_index")]
636    #[snafu(display("Failed to apply vector index: {}", reason))]
637    ApplyVectorIndex {
638        reason: String,
639        #[snafu(implicit)]
640        location: Location,
641    },
642
643    #[snafu(display("Failed to push index value"))]
644    PushIndexValue {
645        source: index::inverted_index::error::Error,
646        #[snafu(implicit)]
647        location: Location,
648    },
649
650    #[snafu(display("Failed to write index completely"))]
651    IndexFinish {
652        source: index::inverted_index::error::Error,
653        #[snafu(implicit)]
654        location: Location,
655    },
656
657    #[snafu(display("Operate on aborted index"))]
658    OperateAbortedIndex {
659        #[snafu(implicit)]
660        location: Location,
661    },
662
663    #[snafu(display("Failed to read puffin blob"))]
664    PuffinReadBlob {
665        source: puffin::error::Error,
666        #[snafu(implicit)]
667        location: Location,
668    },
669
670    #[snafu(display("Failed to add blob to puffin file"))]
671    PuffinAddBlob {
672        source: puffin::error::Error,
673        #[snafu(implicit)]
674        location: Location,
675    },
676
677    #[snafu(display("Failed to clean dir {dir}"))]
678    CleanDir {
679        dir: String,
680        #[snafu(source)]
681        error: std::io::Error,
682        #[snafu(implicit)]
683        location: Location,
684    },
685
686    #[snafu(display("Invalid config, {reason}"))]
687    InvalidConfig {
688        reason: String,
689        #[snafu(implicit)]
690        location: Location,
691    },
692
693    #[snafu(display(
694        "Stale log entry found during replay, region: {}, flushed: {}, replayed: {}",
695        region_id,
696        flushed_entry_id,
697        unexpected_entry_id
698    ))]
699    StaleLogEntry {
700        region_id: RegionId,
701        flushed_entry_id: u64,
702        unexpected_entry_id: u64,
703    },
704
705    #[snafu(display(
706        "Failed to download file, region_id: {}, file_id: {}, file_type: {:?}",
707        region_id,
708        file_id,
709        file_type,
710    ))]
711    Download {
712        region_id: RegionId,
713        file_id: FileId,
714        file_type: FileType,
715        #[snafu(source)]
716        error: std::io::Error,
717        #[snafu(implicit)]
718        location: Location,
719    },
720
721    #[snafu(display(
722        "Failed to upload file, region_id: {}, file_id: {}, file_type: {:?}",
723        region_id,
724        file_id,
725        file_type,
726    ))]
727    Upload {
728        region_id: RegionId,
729        file_id: FileId,
730        file_type: FileType,
731        #[snafu(source)]
732        error: std::io::Error,
733        #[snafu(implicit)]
734        location: Location,
735    },
736
737    #[snafu(display("Failed to create directory {}", dir))]
738    CreateDir {
739        dir: String,
740        #[snafu(source)]
741        error: std::io::Error,
742    },
743
744    #[snafu(display("Record batch error"))]
745    RecordBatch {
746        source: common_recordbatch::error::Error,
747        #[snafu(implicit)]
748        location: Location,
749    },
750
751    #[snafu(display("BiErrors, first: {first}, second: {second}"))]
752    BiErrors {
753        first: Box<Error>,
754        second: Box<Error>,
755        #[snafu(implicit)]
756        location: Location,
757    },
758
759    #[snafu(display("Encode null value"))]
760    IndexEncodeNull {
761        #[snafu(implicit)]
762        location: Location,
763    },
764
765    #[snafu(display("Failed to encode memtable to Parquet bytes"))]
766    EncodeMemtable {
767        #[snafu(source)]
768        error: parquet::errors::ParquetError,
769        #[snafu(implicit)]
770        location: Location,
771    },
772
773    #[snafu(display("Partition {} out of range, {} in total", given, all))]
774    PartitionOutOfRange {
775        given: usize,
776        all: usize,
777        #[snafu(implicit)]
778        location: Location,
779    },
780
781    #[snafu(display("Failed to iter data part"))]
782    ReadDataPart {
783        #[snafu(implicit)]
784        location: Location,
785        #[snafu(source)]
786        error: parquet::errors::ParquetError,
787    },
788
789    #[snafu(display("Failed to read row group in memtable"))]
790    DecodeArrowRowGroup {
791        #[snafu(source)]
792        error: ArrowError,
793        #[snafu(implicit)]
794        location: Location,
795    },
796
797    #[snafu(display("Invalid region options, {}", reason))]
798    InvalidRegionOptions {
799        reason: String,
800        #[snafu(implicit)]
801        location: Location,
802    },
803
804    #[snafu(display("checksum mismatch (actual: {}, expected: {})", actual, expected))]
805    ChecksumMismatch { actual: u32, expected: u32 },
806
807    #[snafu(display(
808        "No checkpoint found, region: {}, last_version: {}",
809        region_id,
810        last_version
811    ))]
812    NoCheckpoint {
813        region_id: RegionId,
814        last_version: ManifestVersion,
815        #[snafu(implicit)]
816        location: Location,
817    },
818
819    #[snafu(display(
820        "No manifests found in range: [{}..{}), region: {}, last_version: {}",
821        start_version,
822        end_version,
823        region_id,
824        last_version
825    ))]
826    NoManifests {
827        region_id: RegionId,
828        start_version: ManifestVersion,
829        end_version: ManifestVersion,
830        last_version: ManifestVersion,
831        #[snafu(implicit)]
832        location: Location,
833    },
834
835    #[snafu(display(
836        "Failed to install manifest to {}, region: {}, available manifest version: {}, last version: {}",
837        target_version,
838        region_id,
839        available_version,
840        last_version
841    ))]
842    InstallManifestTo {
843        region_id: RegionId,
844        target_version: ManifestVersion,
845        available_version: ManifestVersion,
846        #[snafu(implicit)]
847        location: Location,
848        last_version: ManifestVersion,
849    },
850
851    #[snafu(display("Region {} is stopped", region_id))]
852    RegionStopped {
853        region_id: RegionId,
854        #[snafu(implicit)]
855        location: Location,
856    },
857
858    #[snafu(display(
859        "Time range predicate overflows, timestamp: {:?}, target unit: {}",
860        timestamp,
861        unit
862    ))]
863    TimeRangePredicateOverflow {
864        timestamp: Timestamp,
865        unit: TimeUnit,
866        #[snafu(implicit)]
867        location: Location,
868    },
869
870    #[snafu(display("Failed to open region"))]
871    OpenRegion {
872        #[snafu(implicit)]
873        location: Location,
874        source: Arc<Error>,
875    },
876
877    #[snafu(display("Failed to parse job id"))]
878    ParseJobId {
879        #[snafu(implicit)]
880        location: Location,
881        #[snafu(source)]
882        error: uuid::Error,
883    },
884
885    #[snafu(display("Operation is not supported: {}", err_msg))]
886    UnsupportedOperation {
887        err_msg: String,
888        #[snafu(implicit)]
889        location: Location,
890    },
891
892    #[snafu(display(
893        "Failed to remotely compact region {} by job {:?} due to {}",
894        region_id,
895        job_id,
896        reason
897    ))]
898    RemoteCompaction {
899        region_id: RegionId,
900        job_id: Option<JobId>,
901        reason: String,
902        #[snafu(implicit)]
903        location: Location,
904    },
905
906    #[snafu(display("Failed to initialize puffin stager"))]
907    PuffinInitStager {
908        source: puffin::error::Error,
909        #[snafu(implicit)]
910        location: Location,
911    },
912
913    #[snafu(display("Failed to purge puffin stager"))]
914    PuffinPurgeStager {
915        source: puffin::error::Error,
916        #[snafu(implicit)]
917        location: Location,
918    },
919
920    #[snafu(display("Failed to build puffin reader"))]
921    PuffinBuildReader {
922        source: puffin::error::Error,
923        #[snafu(implicit)]
924        location: Location,
925    },
926
927    #[snafu(display("Failed to retrieve index options from column metadata"))]
928    IndexOptions {
929        #[snafu(implicit)]
930        location: Location,
931        source: datatypes::error::Error,
932        column_name: String,
933    },
934
935    #[snafu(display("Failed to create fulltext index creator"))]
936    CreateFulltextCreator {
937        source: index::fulltext_index::error::Error,
938        #[snafu(implicit)]
939        location: Location,
940    },
941
942    #[snafu(display("Failed to cast vector of {from} to {to}"))]
943    CastVector {
944        #[snafu(implicit)]
945        location: Location,
946        from: ConcreteDataType,
947        to: ConcreteDataType,
948        source: datatypes::error::Error,
949    },
950
951    #[snafu(display("Failed to push text to fulltext index"))]
952    FulltextPushText {
953        source: index::fulltext_index::error::Error,
954        #[snafu(implicit)]
955        location: Location,
956    },
957
958    #[snafu(display("Failed to finalize fulltext index creator"))]
959    FulltextFinish {
960        source: index::fulltext_index::error::Error,
961        #[snafu(implicit)]
962        location: Location,
963    },
964
965    #[snafu(display("Failed to apply fulltext index"))]
966    ApplyFulltextIndex {
967        source: index::fulltext_index::error::Error,
968        #[snafu(implicit)]
969        location: Location,
970    },
971
972    #[snafu(display("SST file {} does not contain valid stats info", file_path))]
973    StatsNotPresent {
974        file_path: String,
975        #[snafu(implicit)]
976        location: Location,
977    },
978
979    #[snafu(display("Failed to decode stats of file {}", file_path))]
980    DecodeStats {
981        file_path: String,
982        #[snafu(implicit)]
983        location: Location,
984    },
985
986    #[snafu(display("Region {} is busy", region_id))]
987    RegionBusy {
988        region_id: RegionId,
989        #[snafu(implicit)]
990        location: Location,
991    },
992
993    #[snafu(display("Failed to get schema metadata"))]
994    GetSchemaMetadata {
995        source: common_meta::error::Error,
996        #[snafu(implicit)]
997        location: Location,
998    },
999
1000    #[snafu(display("Timeout"))]
1001    Timeout {
1002        #[snafu(source)]
1003        error: Elapsed,
1004        #[snafu(implicit)]
1005        location: Location,
1006    },
1007
1008    #[snafu(display("Failed to read file metadata"))]
1009    Metadata {
1010        #[snafu(source)]
1011        error: std::io::Error,
1012        #[snafu(implicit)]
1013        location: Location,
1014    },
1015
1016    #[snafu(display("Failed to push value to bloom filter"))]
1017    PushBloomFilterValue {
1018        source: index::bloom_filter::error::Error,
1019        #[snafu(implicit)]
1020        location: Location,
1021    },
1022
1023    #[snafu(display("Failed to finish bloom filter"))]
1024    BloomFilterFinish {
1025        source: index::bloom_filter::error::Error,
1026        #[snafu(implicit)]
1027        location: Location,
1028    },
1029
1030    #[cfg(feature = "vector_index")]
1031    #[snafu(display("Failed to build vector index: {}", reason))]
1032    VectorIndexBuild {
1033        reason: String,
1034        #[snafu(implicit)]
1035        location: Location,
1036    },
1037
1038    #[cfg(feature = "vector_index")]
1039    #[snafu(display("Failed to finish vector index: {}", reason))]
1040    VectorIndexFinish {
1041        reason: String,
1042        #[snafu(implicit)]
1043        location: Location,
1044    },
1045
1046    #[snafu(display("Manual compaction is override by following operations."))]
1047    ManualCompactionOverride {},
1048
1049    #[snafu(display("Compaction memory exhausted for region {region_id} (policy: {policy})",))]
1050    CompactionMemoryExhausted {
1051        region_id: RegionId,
1052        policy: String,
1053        #[snafu(source)]
1054        source: common_memory_manager::Error,
1055        #[snafu(implicit)]
1056        location: Location,
1057    },
1058
1059    #[snafu(display(
1060        "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: {}",
1061        global,
1062        region
1063    ))]
1064    IncompatibleWalProviderChange { global: String, region: String },
1065
1066    #[snafu(display("Expected mito manifest info"))]
1067    MitoManifestInfo {
1068        #[snafu(implicit)]
1069        location: Location,
1070    },
1071
1072    #[snafu(display("Failed to scan series"))]
1073    ScanSeries {
1074        #[snafu(implicit)]
1075        location: Location,
1076        source: Arc<Error>,
1077    },
1078
1079    #[snafu(display("Partition {} scan multiple times", partition))]
1080    ScanMultiTimes {
1081        partition: usize,
1082        #[snafu(implicit)]
1083        location: Location,
1084    },
1085
1086    #[snafu(display("Invalid partition expression: {}", expr))]
1087    InvalidPartitionExpr {
1088        expr: String,
1089        #[snafu(implicit)]
1090        location: Location,
1091        source: partition::error::Error,
1092    },
1093
1094    #[snafu(display("Failed to decode bulk wal entry"))]
1095    ConvertBulkWalEntry {
1096        #[snafu(implicit)]
1097        location: Location,
1098        source: common_grpc::Error,
1099    },
1100
1101    #[snafu(display("Failed to encode"))]
1102    Encode {
1103        #[snafu(implicit)]
1104        location: Location,
1105        source: mito_codec::error::Error,
1106    },
1107
1108    #[snafu(display("Failed to decode"))]
1109    Decode {
1110        #[snafu(implicit)]
1111        location: Location,
1112        source: mito_codec::error::Error,
1113    },
1114
1115    #[snafu(display("Unexpected: {reason}"))]
1116    Unexpected {
1117        reason: String,
1118        #[snafu(implicit)]
1119        location: Location,
1120    },
1121
1122    #[cfg(feature = "enterprise")]
1123    #[snafu(display("Failed to scan external range"))]
1124    ScanExternalRange {
1125        source: BoxedError,
1126        #[snafu(implicit)]
1127        location: Location,
1128    },
1129
1130    #[snafu(display(
1131        "Inconsistent timestamp column length, expect: {}, actual: {}",
1132        expected,
1133        actual
1134    ))]
1135    InconsistentTimestampLength {
1136        expected: usize,
1137        actual: usize,
1138        #[snafu(implicit)]
1139        location: Location,
1140    },
1141
1142    #[snafu(display(
1143        "Too many files to read concurrently: {}, max allowed: {}",
1144        actual,
1145        max
1146    ))]
1147    TooManyFilesToRead {
1148        actual: usize,
1149        max: usize,
1150        #[snafu(implicit)]
1151        location: Location,
1152    },
1153
1154    #[snafu(display("Duration out of range: {input:?}"))]
1155    DurationOutOfRange {
1156        input: std::time::Duration,
1157        #[snafu(source)]
1158        error: chrono::OutOfRangeError,
1159        #[snafu(implicit)]
1160        location: Location,
1161    },
1162
1163    #[snafu(display("GC job permit exhausted"))]
1164    TooManyGcJobs {
1165        #[snafu(implicit)]
1166        location: Location,
1167    },
1168
1169    #[snafu(display(
1170        "Staging partition expr mismatch, manifest: {:?}, request: {}",
1171        manifest_expr,
1172        request_expr
1173    ))]
1174    StagingPartitionExprMismatch {
1175        manifest_expr: Option<String>,
1176        request_expr: String,
1177        #[snafu(implicit)]
1178        location: Location,
1179    },
1180
1181    #[snafu(display(
1182        "Invalid source and target region, source: {}, target: {}",
1183        source_region_id,
1184        target_region_id
1185    ))]
1186    InvalidSourceAndTargetRegion {
1187        source_region_id: RegionId,
1188        target_region_id: RegionId,
1189        #[snafu(implicit)]
1190        location: Location,
1191    },
1192}
1193
1194pub type Result<T, E = Error> = std::result::Result<T, E>;
1195
1196impl Error {
1197    /// Returns true if we need to fill default value for a region.
1198    pub(crate) fn is_fill_default(&self) -> bool {
1199        matches!(self, Error::FillDefault { .. })
1200    }
1201
1202    /// Returns true if the file is not found on the object store.
1203    pub(crate) fn is_object_not_found(&self) -> bool {
1204        match self {
1205            Error::OpenDal { error, .. } => error.kind() == ErrorKind::NotFound,
1206            _ => false,
1207        }
1208    }
1209}
1210
1211impl ErrorExt for Error {
1212    fn status_code(&self) -> StatusCode {
1213        use Error::*;
1214
1215        match self {
1216            DataTypeMismatch { source, .. } => source.status_code(),
1217            OpenDal { .. } | ReadParquet { .. } => StatusCode::StorageUnavailable,
1218            WriteWal { source, .. } | ReadWal { source, .. } | DeleteWal { source, .. } => {
1219                source.status_code()
1220            }
1221            CompressObject { .. }
1222            | DecompressObject { .. }
1223            | SerdeJson { .. }
1224            | Utf8 { .. }
1225            | NewRecordBatch { .. }
1226            | RegionCorrupted { .. }
1227            | InconsistentFile { .. }
1228            | CreateDefault { .. }
1229            | InvalidParquet { .. }
1230            | OperateAbortedIndex { .. }
1231            | IndexEncodeNull { .. }
1232            | NoCheckpoint { .. }
1233            | NoManifests { .. }
1234            | FilesLost { .. }
1235            | InstallManifestTo { .. }
1236            | Unexpected { .. }
1237            | SerializeColumnMetadata { .. }
1238            | SerializeManifest { .. }
1239            | StagingPartitionExprMismatch { .. } => StatusCode::Unexpected,
1240
1241            RegionNotFound { .. } => StatusCode::RegionNotFound,
1242            ObjectStoreNotFound { .. }
1243            | InvalidScanIndex { .. }
1244            | InvalidMeta { .. }
1245            | InvalidRequest { .. }
1246            | FillDefault { .. }
1247            | ConvertColumnDataType { .. }
1248            | ColumnNotFound { .. }
1249            | InvalidMetadata { .. }
1250            | InvalidRegionOptions { .. }
1251            | InvalidWalReadRequest { .. }
1252            | PartitionOutOfRange { .. }
1253            | ParseJobId { .. }
1254            | DurationOutOfRange { .. }
1255            | MissingOldManifest { .. }
1256            | MissingNewManifest { .. }
1257            | MissingManifest { .. }
1258            | NoOldManifests { .. }
1259            | MissingPartitionExpr { .. }
1260            | SerializePartitionExpr { .. }
1261            | InvalidSourceAndTargetRegion { .. } => StatusCode::InvalidArguments,
1262
1263            RegionMetadataNotFound { .. }
1264            | Join { .. }
1265            | WorkerStopped { .. }
1266            | Recv { .. }
1267            | DecodeWal { .. }
1268            | ComputeArrow { .. }
1269            | BiErrors { .. }
1270            | StopScheduler { .. }
1271            | ComputeVector { .. }
1272            | EncodeMemtable { .. }
1273            | CreateDir { .. }
1274            | ReadDataPart { .. }
1275            | BuildEntry { .. }
1276            | Metadata { .. }
1277            | MitoManifestInfo { .. } => StatusCode::Internal,
1278
1279            FetchManifests { source, .. } => source.status_code(),
1280
1281            OpenRegion { source, .. } => source.status_code(),
1282
1283            WriteParquet { .. } => StatusCode::StorageUnavailable,
1284            WriteGroup { source, .. } => source.status_code(),
1285            EncodeSparsePrimaryKey { .. } => StatusCode::Unexpected,
1286            InvalidBatch { .. } => StatusCode::InvalidArguments,
1287            InvalidRecordBatch { .. } => StatusCode::InvalidArguments,
1288            ConvertVector { source, .. } => source.status_code(),
1289
1290            PrimaryKeyLengthMismatch { .. } => StatusCode::InvalidArguments,
1291            InvalidSender { .. } => StatusCode::InvalidArguments,
1292            InvalidSchedulerState { .. } => StatusCode::InvalidArguments,
1293            DeleteSst { .. } | DeleteIndex { .. } => StatusCode::StorageUnavailable,
1294            FlushRegion { source, .. } | BuildIndexAsync { source, .. } => source.status_code(),
1295            RegionDropped { .. } => StatusCode::Cancelled,
1296            RegionClosed { .. } => StatusCode::Cancelled,
1297            RegionTruncated { .. } => StatusCode::Cancelled,
1298            RejectWrite { .. } => StatusCode::StorageUnavailable,
1299            CompactRegion { source, .. } => source.status_code(),
1300            CompatReader { .. } => StatusCode::Unexpected,
1301            InvalidRegionRequest { source, .. } => source.status_code(),
1302            RegionState { .. } | UpdateManifest { .. } => StatusCode::RegionNotReady,
1303            JsonOptions { .. } => StatusCode::InvalidArguments,
1304            EmptyRegionDir { .. } | EmptyManifestDir { .. } => StatusCode::RegionNotFound,
1305            ArrowReader { .. } => StatusCode::StorageUnavailable,
1306            ConvertValue { source, .. } => source.status_code(),
1307            ApplyBloomFilterIndex { source, .. } => source.status_code(),
1308            InvalidPartitionExpr { source, .. } => source.status_code(),
1309            BuildIndexApplier { source, .. }
1310            | PushIndexValue { source, .. }
1311            | ApplyInvertedIndex { source, .. }
1312            | IndexFinish { source, .. } => source.status_code(),
1313            #[cfg(feature = "vector_index")]
1314            ApplyVectorIndex { .. } => StatusCode::Internal,
1315            PuffinReadBlob { source, .. }
1316            | PuffinAddBlob { source, .. }
1317            | PuffinInitStager { source, .. }
1318            | PuffinBuildReader { source, .. }
1319            | PuffinPurgeStager { source, .. } => source.status_code(),
1320            CleanDir { .. } => StatusCode::Unexpected,
1321            InvalidConfig { .. } => StatusCode::InvalidArguments,
1322            StaleLogEntry { .. } => StatusCode::Unexpected,
1323
1324            External { source, .. } => source.status_code(),
1325
1326            RecordBatch { source, .. } => source.status_code(),
1327
1328            Download { .. } | Upload { .. } => StatusCode::StorageUnavailable,
1329            ChecksumMismatch { .. } => StatusCode::Unexpected,
1330            RegionStopped { .. } => StatusCode::RegionNotReady,
1331            TimeRangePredicateOverflow { .. } => StatusCode::InvalidArguments,
1332            UnsupportedOperation { .. } => StatusCode::Unsupported,
1333            RemoteCompaction { .. } => StatusCode::Unexpected,
1334
1335            IndexOptions { source, .. } => source.status_code(),
1336            CreateFulltextCreator { source, .. } => source.status_code(),
1337            CastVector { source, .. } => source.status_code(),
1338            FulltextPushText { source, .. }
1339            | FulltextFinish { source, .. }
1340            | ApplyFulltextIndex { source, .. } => source.status_code(),
1341            DecodeStats { .. } | StatsNotPresent { .. } => StatusCode::Internal,
1342            RegionBusy { .. } => StatusCode::RegionBusy,
1343            GetSchemaMetadata { source, .. } => source.status_code(),
1344            Timeout { .. } => StatusCode::Cancelled,
1345
1346            DecodeArrowRowGroup { .. } => StatusCode::Internal,
1347
1348            PushBloomFilterValue { source, .. } | BloomFilterFinish { source, .. } => {
1349                source.status_code()
1350            }
1351
1352            #[cfg(feature = "vector_index")]
1353            VectorIndexBuild { .. } | VectorIndexFinish { .. } => StatusCode::Internal,
1354
1355            ManualCompactionOverride {} => StatusCode::Cancelled,
1356
1357            CompactionMemoryExhausted { source, .. } => source.status_code(),
1358
1359            IncompatibleWalProviderChange { .. } => StatusCode::InvalidArguments,
1360
1361            ScanSeries { source, .. } => source.status_code(),
1362
1363            ScanMultiTimes { .. } => StatusCode::InvalidArguments,
1364            ConvertBulkWalEntry { source, .. } => source.status_code(),
1365
1366            Encode { source, .. } | Decode { source, .. } => source.status_code(),
1367
1368            #[cfg(feature = "enterprise")]
1369            ScanExternalRange { source, .. } => source.status_code(),
1370
1371            InconsistentTimestampLength { .. } => StatusCode::InvalidArguments,
1372
1373            TooManyFilesToRead { .. } | TooManyGcJobs { .. } => StatusCode::RateLimited,
1374        }
1375    }
1376
1377    fn as_any(&self) -> &dyn Any {
1378        self
1379    }
1380}