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