risingwave_storage/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_object_store::object::ObjectError;
16use thiserror::Error;
17use thiserror_ext::AsReport;
18use tokio::sync::oneshot::error::RecvError;
19
20// TODO(error-handling): should prefer use error types than strings.
21#[derive(Error, thiserror_ext::ReportDebug, thiserror_ext::Arc)]
22#[thiserror_ext(newtype(name = HummockError, backtrace))]
23pub enum HummockErrorInner {
24    #[error("Magic number mismatch: expected {expected}, found: {found}")]
25    MagicMismatch { expected: u32, found: u32 },
26    #[error("Invalid format version: {0}")]
27    InvalidFormatVersion(u32),
28    #[error("Checksum mismatch: expected {expected}, found: {found}")]
29    ChecksumMismatch { expected: u64, found: u64 },
30    #[error("Invalid block")]
31    InvalidBlock,
32    #[error("Encode error: {0}")]
33    EncodeError(String),
34    #[error("Decode error: {0}")]
35    DecodeError(String),
36    #[error("ObjectStore failed with IO error: {0}")]
37    ObjectIoError(
38        #[from]
39        #[backtrace]
40        ObjectError,
41    ),
42    #[error("Meta error: {0}")]
43    MetaError(String),
44    #[error("SharedBuffer error: {0}")]
45    SharedBufferError(String),
46    #[error("Wait epoch error: {0}")]
47    WaitEpoch(String),
48    #[error("Next epoch error: {0}")]
49    NextEpoch(String),
50    #[error("Barrier read is unavailable for now. Likely the cluster is recovering")]
51    ReadCurrentEpoch,
52    #[error("CompactionExecutor error: {0}")]
53    CompactionExecutor(String),
54    #[error("FileCache error: {0}")]
55    FileCache(String),
56    #[error("SstObjectIdTracker error: {0}")]
57    SstObjectIdTrackerError(String),
58    #[error("CompactionGroup error: {0}")]
59    CompactionGroupError(String),
60    #[error("SstableUpload error: {0}")]
61    SstableUploadError(String),
62    #[error("Read backup error: {0}")]
63    ReadBackupError(String),
64    #[error("Foyer error: {0}")]
65    FoyerError(#[from] foyer::Error),
66    #[error("Other error: {0}")]
67    Other(String),
68}
69
70impl HummockError {
71    pub fn invalid_format_version(v: u32) -> HummockError {
72        HummockErrorInner::InvalidFormatVersion(v).into()
73    }
74
75    pub fn invalid_block() -> HummockError {
76        HummockErrorInner::InvalidBlock.into()
77    }
78
79    pub fn encode_error(error: impl ToString) -> HummockError {
80        HummockErrorInner::EncodeError(error.to_string()).into()
81    }
82
83    pub fn decode_error(error: impl ToString) -> HummockError {
84        HummockErrorInner::DecodeError(error.to_string()).into()
85    }
86
87    pub fn magic_mismatch(expected: u32, found: u32) -> HummockError {
88        HummockErrorInner::MagicMismatch { expected, found }.into()
89    }
90
91    pub fn checksum_mismatch(expected: u64, found: u64) -> HummockError {
92        HummockErrorInner::ChecksumMismatch { expected, found }.into()
93    }
94
95    pub fn meta_error(error: impl ToString) -> HummockError {
96        HummockErrorInner::MetaError(error.to_string()).into()
97    }
98
99    pub fn shared_buffer_error(error: impl ToString) -> HummockError {
100        HummockErrorInner::SharedBufferError(error.to_string()).into()
101    }
102
103    pub fn wait_epoch(error: impl ToString) -> HummockError {
104        HummockErrorInner::WaitEpoch(error.to_string()).into()
105    }
106
107    pub fn next_epoch(error: impl ToString) -> HummockError {
108        HummockErrorInner::NextEpoch(error.to_string()).into()
109    }
110
111    pub fn read_current_epoch() -> HummockError {
112        HummockErrorInner::ReadCurrentEpoch.into()
113    }
114
115    pub fn is_meta_error(&self) -> bool {
116        matches!(self.inner(), HummockErrorInner::MetaError(..))
117    }
118
119    pub fn is_object_error(&self) -> bool {
120        matches!(self.inner(), HummockErrorInner::ObjectIoError { .. })
121    }
122
123    pub fn compaction_executor(error: impl ToString) -> HummockError {
124        HummockErrorInner::CompactionExecutor(error.to_string()).into()
125    }
126
127    pub fn sst_object_id_tracker_error(error: impl ToString) -> HummockError {
128        HummockErrorInner::SstObjectIdTrackerError(error.to_string()).into()
129    }
130
131    pub fn compaction_group_error(error: impl ToString) -> HummockError {
132        HummockErrorInner::CompactionGroupError(error.to_string()).into()
133    }
134
135    pub fn file_cache(error: impl ToString) -> HummockError {
136        HummockErrorInner::FileCache(error.to_string()).into()
137    }
138
139    pub fn sstable_upload_error(error: impl ToString) -> HummockError {
140        HummockErrorInner::SstableUploadError(error.to_string()).into()
141    }
142
143    pub fn read_backup_error(error: impl ToString) -> HummockError {
144        HummockErrorInner::ReadBackupError(error.to_string()).into()
145    }
146
147    pub fn foyer_error(error: foyer::Error) -> HummockError {
148        HummockErrorInner::FoyerError(error).into()
149    }
150
151    pub fn other(error: impl ToString) -> HummockError {
152        HummockErrorInner::Other(error.to_string()).into()
153    }
154}
155
156impl From<prost::DecodeError> for HummockError {
157    fn from(error: prost::DecodeError) -> Self {
158        HummockErrorInner::DecodeError(error.to_report_string()).into()
159    }
160}
161
162impl From<RecvError> for HummockError {
163    fn from(error: RecvError) -> Self {
164        ObjectError::from(error).into()
165    }
166}
167
168pub type HummockResult<T> = std::result::Result<T, HummockError>;