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