Skip to main content

mito2/
error.rs

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