risingwave_meta/hummock/
error.rs

1// Copyright 2022 RisingWave Labs
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 risingwave_hummock_sdk::{HummockContextId, HummockSstableObjectId};
16use risingwave_object_store::object::ObjectError;
17use risingwave_rpc_client::error::ToTonicStatus;
18use sea_orm::DbErr;
19use thiserror::Error;
20
21use crate::model::MetadataModelError;
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Error, Debug)]
26pub enum Error {
27    #[error("invalid hummock context {0}")]
28    InvalidContext(HummockContextId),
29    #[error("failed to access meta store")]
30    MetaStore(
31        #[source]
32        #[backtrace]
33        anyhow::Error,
34    ),
35    #[error(transparent)]
36    ObjectStore(
37        #[from]
38        #[backtrace]
39        ObjectError,
40    ),
41    #[error("compactor {0} is disconnected")]
42    CompactorUnreachable(HummockContextId),
43    #[error("compaction group error: {0}")]
44    CompactionGroup(String),
45    #[error("SST {0} is invalid")]
46    InvalidSst(HummockSstableObjectId),
47    #[error("invalid manual compaction option: {0}")]
48    InvalidManualCompactionOption(String),
49    #[error("time travel")]
50    TimeTravel(
51        #[source]
52        #[backtrace]
53        anyhow::Error,
54    ),
55    #[error(transparent)]
56    Internal(
57        #[from]
58        #[backtrace]
59        anyhow::Error,
60    ),
61}
62
63impl Error {
64    pub fn retryable(&self) -> bool {
65        matches!(self, Error::MetaStore(_))
66    }
67}
68
69impl From<MetadataModelError> for Error {
70    fn from(err: MetadataModelError) -> Self {
71        anyhow::anyhow!(err).into()
72    }
73}
74
75impl From<sea_orm::DbErr> for Error {
76    fn from(value: DbErr) -> Self {
77        MetadataModelError::from(value).into()
78    }
79}
80
81impl From<Error> for tonic::Status {
82    fn from(err: Error) -> Self {
83        let code = match &err {
84            Error::InvalidManualCompactionOption(_) => tonic::Code::InvalidArgument,
85            _ => tonic::Code::Internal,
86        };
87        err.to_status(code, "hummock")
88    }
89}