risingwave_storage/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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// 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_common::catalog::TableId;
use risingwave_object_store::object::ObjectError;
use thiserror::Error;
use thiserror_ext::AsReport;
use tokio::sync::oneshot::error::RecvError;

// TODO(error-handling): should prefer use error types than strings.
#[derive(Error, thiserror_ext::ReportDebug, thiserror_ext::Arc)]
#[thiserror_ext(newtype(name = HummockError, backtrace))]
pub enum HummockErrorInner {
    #[error("Magic number mismatch: expected {expected}, found: {found}")]
    MagicMismatch { expected: u32, found: u32 },
    #[error("Invalid format version: {0}")]
    InvalidFormatVersion(u32),
    #[error("Checksum mismatch: expected {expected}, found: {found}")]
    ChecksumMismatch { expected: u64, found: u64 },
    #[error("Invalid block")]
    InvalidBlock,
    #[error("Encode error: {0}")]
    EncodeError(String),
    #[error("Decode error: {0}")]
    DecodeError(String),
    #[error("ObjectStore failed with IO error: {0}")]
    ObjectIoError(
        #[from]
        #[backtrace]
        ObjectError,
    ),
    #[error("Meta error: {0}")]
    MetaError(String),
    #[error("SharedBuffer error: {0}")]
    SharedBufferError(String),
    #[error("Wait epoch error: {0}")]
    WaitEpoch(String),
    #[error("Barrier read is unavailable for now. Likely the cluster is recovering")]
    ReadCurrentEpoch,
    #[error("Expired Epoch: watermark {safe_epoch}, epoch {epoch}")]
    ExpiredEpoch {
        table_id: u32,
        safe_epoch: u64,
        epoch: u64,
    },
    #[error("CompactionExecutor error: {0}")]
    CompactionExecutor(String),
    #[error("FileCache error: {0}")]
    FileCache(String),
    #[error("SstObjectIdTracker error: {0}")]
    SstObjectIdTrackerError(String),
    #[error("CompactionGroup error: {0}")]
    CompactionGroupError(String),
    #[error("SstableUpload error: {0}")]
    SstableUploadError(String),
    #[error("Read backup error: {0}")]
    ReadBackupError(String),
    #[error("Foyer error: {0}")]
    FoyerError(anyhow::Error),
    #[error("Other error: {0}")]
    Other(String),
}

impl HummockError {
    pub fn invalid_format_version(v: u32) -> HummockError {
        HummockErrorInner::InvalidFormatVersion(v).into()
    }

    pub fn invalid_block() -> HummockError {
        HummockErrorInner::InvalidBlock.into()
    }

    pub fn encode_error(error: impl ToString) -> HummockError {
        HummockErrorInner::EncodeError(error.to_string()).into()
    }

    pub fn decode_error(error: impl ToString) -> HummockError {
        HummockErrorInner::DecodeError(error.to_string()).into()
    }

    pub fn magic_mismatch(expected: u32, found: u32) -> HummockError {
        HummockErrorInner::MagicMismatch { expected, found }.into()
    }

    pub fn checksum_mismatch(expected: u64, found: u64) -> HummockError {
        HummockErrorInner::ChecksumMismatch { expected, found }.into()
    }

    pub fn meta_error(error: impl ToString) -> HummockError {
        HummockErrorInner::MetaError(error.to_string()).into()
    }

    pub fn shared_buffer_error(error: impl ToString) -> HummockError {
        HummockErrorInner::SharedBufferError(error.to_string()).into()
    }

    pub fn wait_epoch(error: impl ToString) -> HummockError {
        HummockErrorInner::WaitEpoch(error.to_string()).into()
    }

    pub fn read_current_epoch() -> HummockError {
        HummockErrorInner::ReadCurrentEpoch.into()
    }

    pub fn expired_epoch(table_id: TableId, safe_epoch: u64, epoch: u64) -> HummockError {
        HummockErrorInner::ExpiredEpoch {
            table_id: table_id.table_id,
            safe_epoch,
            epoch,
        }
        .into()
    }

    pub fn is_expired_epoch(&self) -> bool {
        matches!(self.inner(), HummockErrorInner::ExpiredEpoch { .. })
    }

    pub fn is_meta_error(&self) -> bool {
        matches!(self.inner(), HummockErrorInner::MetaError(..))
    }

    pub fn is_object_error(&self) -> bool {
        matches!(self.inner(), HummockErrorInner::ObjectIoError { .. })
    }

    pub fn compaction_executor(error: impl ToString) -> HummockError {
        HummockErrorInner::CompactionExecutor(error.to_string()).into()
    }

    pub fn sst_object_id_tracker_error(error: impl ToString) -> HummockError {
        HummockErrorInner::SstObjectIdTrackerError(error.to_string()).into()
    }

    pub fn compaction_group_error(error: impl ToString) -> HummockError {
        HummockErrorInner::CompactionGroupError(error.to_string()).into()
    }

    pub fn file_cache(error: impl ToString) -> HummockError {
        HummockErrorInner::FileCache(error.to_string()).into()
    }

    pub fn sstable_upload_error(error: impl ToString) -> HummockError {
        HummockErrorInner::SstableUploadError(error.to_string()).into()
    }

    pub fn read_backup_error(error: impl ToString) -> HummockError {
        HummockErrorInner::ReadBackupError(error.to_string()).into()
    }

    pub fn foyer_error(error: anyhow::Error) -> HummockError {
        HummockErrorInner::FoyerError(error).into()
    }

    pub fn other(error: impl ToString) -> HummockError {
        HummockErrorInner::Other(error.to_string()).into()
    }
}

impl From<prost::DecodeError> for HummockError {
    fn from(error: prost::DecodeError) -> Self {
        HummockErrorInner::DecodeError(error.to_report_string()).into()
    }
}

impl From<RecvError> for HummockError {
    fn from(error: RecvError) -> Self {
        ObjectError::from(error).into()
    }
}

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