risingwave_meta/hummock/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use risingwave_hummock_sdk::{HummockContextId, HummockSstableObjectId};
use risingwave_object_store::object::ObjectError;
use risingwave_rpc_client::error::ToTonicStatus;
use sea_orm::DbErr;
use thiserror::Error;

use crate::model::MetadataModelError;
use crate::storage::MetaStoreError;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("invalid hummock context {0}")]
    InvalidContext(HummockContextId),
    #[error("failed to access meta store")]
    MetaStore(
        #[source]
        #[backtrace]
        anyhow::Error,
    ),
    #[error(transparent)]
    ObjectStore(
        #[from]
        #[backtrace]
        ObjectError,
    ),
    #[error("compactor {0} is disconnected")]
    CompactorUnreachable(HummockContextId),
    #[error("compaction group error: {0}")]
    CompactionGroup(String),
    #[error("SST {0} is invalid")]
    InvalidSst(HummockSstableObjectId),
    #[error("time travel")]
    TimeTravel(
        #[source]
        #[backtrace]
        anyhow::Error,
    ),
    #[error(transparent)]
    Internal(
        #[from]
        #[backtrace]
        anyhow::Error,
    ),
}

impl Error {
    pub fn retryable(&self) -> bool {
        matches!(self, Error::MetaStore(_))
    }
}

impl From<MetaStoreError> for Error {
    fn from(error: MetaStoreError) -> Self {
        match error {
            MetaStoreError::ItemNotFound(err) => anyhow::anyhow!(err).into(),
            MetaStoreError::TransactionAbort() => {
                // TODO: need more concrete error from meta store.
                Error::Internal(anyhow::anyhow!("meta store transaction failed"))
            }
            // TODO: Currently MetaStoreError::Internal is equivalent to SqlError, which
            // includes both retryable and non-retryable. Need to expand MetaStoreError::Internal
            // to more detail meta_store errors.
            MetaStoreError::Internal(err) => Error::MetaStore(err),
        }
    }
}

impl From<MetadataModelError> for Error {
    fn from(err: MetadataModelError) -> Self {
        match err {
            MetadataModelError::MetaStoreError(e) => e.into(),
            e => anyhow::anyhow!(e).into(),
        }
    }
}

impl From<sea_orm::DbErr> for Error {
    fn from(value: DbErr) -> Self {
        MetadataModelError::from(value).into()
    }
}

impl From<Error> for tonic::Status {
    fn from(err: Error) -> Self {
        err.to_status(tonic::Code::Internal, "hummock")
    }
}