mito2/
error.rs

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