datanode/
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_error::define_into_tonic_status;
19use common_error::ext::{BoxedError, ErrorExt};
20use common_error::status_code::StatusCode;
21use common_macro::stack_trace_debug;
22use snafu::{Location, Snafu};
23use store_api::storage::RegionId;
24use table::error::Error as TableError;
25use tokio::time::error::Elapsed;
26
27/// Business error of datanode.
28#[derive(Snafu)]
29#[snafu(visibility(pub))]
30#[stack_trace_debug]
31pub enum Error {
32    #[snafu(display("Failed to execute async task"))]
33    AsyncTaskExecute {
34        #[snafu(implicit)]
35        location: Location,
36        source: Arc<Error>,
37    },
38
39    #[snafu(display("Failed to watch change"))]
40    WatchAsyncTaskChange {
41        #[snafu(implicit)]
42        location: Location,
43        #[snafu(source)]
44        error: tokio::sync::watch::error::RecvError,
45    },
46
47    #[snafu(display("Failed to handle heartbeat response"))]
48    HandleHeartbeatResponse {
49        #[snafu(implicit)]
50        location: Location,
51        source: common_meta::error::Error,
52    },
53
54    #[snafu(display("Failed to get info from meta server"))]
55    GetMetadata {
56        #[snafu(implicit)]
57        location: Location,
58        source: common_meta::error::Error,
59    },
60
61    #[snafu(display("Failed to execute logical plan"))]
62    ExecuteLogicalPlan {
63        #[snafu(implicit)]
64        location: Location,
65        source: query::error::Error,
66    },
67
68    #[snafu(display("Failed to create plan decoder"))]
69    NewPlanDecoder {
70        #[snafu(implicit)]
71        location: Location,
72        source: query::error::Error,
73    },
74
75    #[snafu(display("Failed to decode logical plan"))]
76    DecodeLogicalPlan {
77        #[snafu(implicit)]
78        location: Location,
79        source: common_query::error::Error,
80    },
81
82    #[snafu(display("Catalog not found: {}", name))]
83    CatalogNotFound {
84        name: String,
85        #[snafu(implicit)]
86        location: Location,
87    },
88
89    #[snafu(display("Schema not found: {}", name))]
90    SchemaNotFound {
91        name: String,
92        #[snafu(implicit)]
93        location: Location,
94    },
95
96    #[snafu(display("Missing timestamp column in request"))]
97    MissingTimestampColumn {
98        #[snafu(implicit)]
99        location: Location,
100    },
101
102    #[snafu(display("Failed to delete value from table: {}", table_name))]
103    Delete {
104        table_name: String,
105        #[snafu(implicit)]
106        location: Location,
107        source: TableError,
108    },
109
110    #[snafu(display("Failed to start server"))]
111    StartServer {
112        #[snafu(implicit)]
113        location: Location,
114        source: servers::error::Error,
115    },
116
117    #[snafu(display("Failed to parse address {}", addr))]
118    ParseAddr {
119        addr: String,
120        #[snafu(source)]
121        error: std::net::AddrParseError,
122    },
123
124    #[snafu(display("Failed to create directory {}", dir))]
125    CreateDir {
126        dir: String,
127        #[snafu(source)]
128        error: std::io::Error,
129    },
130
131    #[snafu(display("Failed to remove directory {}", dir))]
132    RemoveDir {
133        dir: String,
134        #[snafu(source)]
135        error: std::io::Error,
136    },
137
138    #[snafu(display("Failed to open log store"))]
139    OpenLogStore {
140        #[snafu(implicit)]
141        location: Location,
142        source: Box<log_store::error::Error>,
143    },
144
145    #[snafu(display("Failed to init backend"))]
146    InitBackend {
147        #[snafu(source)]
148        error: object_store::Error,
149        #[snafu(implicit)]
150        location: Location,
151    },
152
153    #[snafu(display("Expect KvBackend but not found"))]
154    MissingKvBackend {
155        #[snafu(implicit)]
156        location: Location,
157    },
158
159    #[snafu(display("Invalid SQL, error: {}", msg))]
160    InvalidSql { msg: String },
161
162    #[snafu(display("Illegal primary keys definition: {}", msg))]
163    IllegalPrimaryKeysDef {
164        msg: String,
165        #[snafu(implicit)]
166        location: Location,
167    },
168
169    #[snafu(display("Schema {} already exists", name))]
170    SchemaExists {
171        name: String,
172        #[snafu(implicit)]
173        location: Location,
174    },
175
176    #[snafu(display("Failed to access catalog"))]
177    Catalog {
178        #[snafu(implicit)]
179        location: Location,
180        source: catalog::error::Error,
181    },
182
183    #[snafu(display("Failed to initialize meta client"))]
184    MetaClientInit {
185        #[snafu(implicit)]
186        location: Location,
187        source: meta_client::error::Error,
188    },
189
190    #[snafu(display("Missing node id in Datanode config"))]
191    MissingNodeId {
192        #[snafu(implicit)]
193        location: Location,
194    },
195
196    #[snafu(display("Failed to build http client"))]
197    BuildHttpClient {
198        #[snafu(implicit)]
199        location: Location,
200        #[snafu(source)]
201        error: reqwest::Error,
202    },
203
204    #[snafu(display("Missing required field: {}", name))]
205    MissingRequiredField {
206        name: String,
207        #[snafu(implicit)]
208        location: Location,
209    },
210
211    #[snafu(display(
212        "No valid default value can be built automatically, column: {}",
213        column,
214    ))]
215    ColumnNoneDefaultValue {
216        column: String,
217        #[snafu(implicit)]
218        location: Location,
219    },
220
221    #[snafu(display("Failed to shutdown server"))]
222    ShutdownServer {
223        #[snafu(implicit)]
224        location: Location,
225        source: servers::error::Error,
226    },
227
228    #[snafu(display("Failed to shutdown instance"))]
229    ShutdownInstance {
230        #[snafu(implicit)]
231        location: Location,
232        source: BoxedError,
233    },
234
235    #[snafu(display("Payload not exist"))]
236    PayloadNotExist {
237        #[snafu(implicit)]
238        location: Location,
239    },
240
241    #[snafu(display("Unexpected, violated: {}", violated))]
242    Unexpected {
243        violated: String,
244        #[snafu(implicit)]
245        location: Location,
246    },
247
248    #[snafu(display("Failed to handle request for region {}", region_id))]
249    HandleRegionRequest {
250        region_id: RegionId,
251        #[snafu(implicit)]
252        location: Location,
253        source: BoxedError,
254    },
255
256    #[snafu(display("Failed to open batch regions"))]
257    HandleBatchOpenRequest {
258        #[snafu(implicit)]
259        location: Location,
260        source: BoxedError,
261    },
262
263    #[snafu(display("Failed to handle batch ddl request, ddl_type: {}", ddl_type))]
264    HandleBatchDdlRequest {
265        #[snafu(implicit)]
266        location: Location,
267        source: BoxedError,
268        ddl_type: String,
269    },
270
271    #[snafu(display("RegionId {} not found", region_id))]
272    RegionNotFound {
273        region_id: RegionId,
274        #[snafu(implicit)]
275        location: Location,
276    },
277
278    #[snafu(display("Region {} not ready", region_id))]
279    RegionNotReady {
280        region_id: RegionId,
281        #[snafu(implicit)]
282        location: Location,
283    },
284
285    #[snafu(display("Region {} is busy", region_id))]
286    RegionBusy {
287        region_id: RegionId,
288        #[snafu(implicit)]
289        location: Location,
290    },
291
292    #[snafu(display("Region engine {} is not registered", name))]
293    RegionEngineNotFound {
294        name: String,
295        #[snafu(implicit)]
296        location: Location,
297    },
298
299    #[snafu(display("Unsupported output type, expected: {}", expected))]
300    UnsupportedOutput {
301        expected: String,
302        #[snafu(implicit)]
303        location: Location,
304    },
305
306    #[snafu(display("Failed to build region requests"))]
307    BuildRegionRequests {
308        #[snafu(implicit)]
309        location: Location,
310        source: store_api::metadata::MetadataError,
311    },
312
313    #[snafu(display("Failed to stop region engine {}", name))]
314    StopRegionEngine {
315        name: String,
316        #[snafu(implicit)]
317        location: Location,
318        source: BoxedError,
319    },
320
321    #[snafu(display(
322        "Failed to find logical regions in physical region {}",
323        physical_region_id
324    ))]
325    FindLogicalRegions {
326        physical_region_id: RegionId,
327        source: metric_engine::error::Error,
328        #[snafu(implicit)]
329        location: Location,
330    },
331
332    #[snafu(display("Failed to build mito engine"))]
333    BuildMitoEngine {
334        source: mito2::error::Error,
335        #[snafu(implicit)]
336        location: Location,
337    },
338
339    #[snafu(display("Failed to build metric engine"))]
340    BuildMetricEngine {
341        source: metric_engine::error::Error,
342        #[snafu(implicit)]
343        location: Location,
344    },
345
346    #[snafu(display("Failed to serialize options to TOML"))]
347    TomlFormat {
348        #[snafu(implicit)]
349        location: Location,
350        #[snafu(source(from(common_config::error::Error, Box::new)))]
351        source: Box<common_config::error::Error>,
352    },
353
354    #[snafu(display(
355        "Failed to get region metadata from engine {} for region_id {}",
356        engine,
357        region_id,
358    ))]
359    GetRegionMetadata {
360        engine: String,
361        region_id: RegionId,
362        #[snafu(implicit)]
363        location: Location,
364        source: BoxedError,
365    },
366
367    #[snafu(display("DataFusion"))]
368    DataFusion {
369        #[snafu(source)]
370        error: datafusion::error::DataFusionError,
371        #[snafu(implicit)]
372        location: Location,
373    },
374
375    #[snafu(display("Failed to acquire permit, source closed"))]
376    ConcurrentQueryLimiterClosed {
377        #[snafu(source)]
378        error: tokio::sync::AcquireError,
379        #[snafu(implicit)]
380        location: Location,
381    },
382
383    #[snafu(display("Failed to acquire permit under timeouts"))]
384    ConcurrentQueryLimiterTimeout {
385        #[snafu(source)]
386        error: Elapsed,
387        #[snafu(implicit)]
388        location: Location,
389    },
390
391    #[snafu(display("Cache not found in registry"))]
392    MissingCache {
393        #[snafu(implicit)]
394        location: Location,
395    },
396}
397
398pub type Result<T> = std::result::Result<T, Error>;
399
400impl ErrorExt for Error {
401    fn status_code(&self) -> StatusCode {
402        use Error::*;
403        match self {
404            NewPlanDecoder { source, .. } | ExecuteLogicalPlan { source, .. } => {
405                source.status_code()
406            }
407
408            BuildRegionRequests { source, .. } => source.status_code(),
409            HandleHeartbeatResponse { source, .. } | GetMetadata { source, .. } => {
410                source.status_code()
411            }
412
413            DecodeLogicalPlan { source, .. } => source.status_code(),
414
415            Delete { source, .. } => source.status_code(),
416
417            InvalidSql { .. }
418            | IllegalPrimaryKeysDef { .. }
419            | MissingTimestampColumn { .. }
420            | CatalogNotFound { .. }
421            | SchemaNotFound { .. }
422            | SchemaExists { .. }
423            | MissingNodeId { .. }
424            | ColumnNoneDefaultValue { .. }
425            | Catalog { .. }
426            | MissingRequiredField { .. }
427            | RegionEngineNotFound { .. }
428            | ParseAddr { .. }
429            | MissingKvBackend { .. }
430            | TomlFormat { .. } => StatusCode::InvalidArguments,
431
432            PayloadNotExist { .. }
433            | Unexpected { .. }
434            | WatchAsyncTaskChange { .. }
435            | BuildHttpClient { .. } => StatusCode::Unexpected,
436
437            AsyncTaskExecute { source, .. } => source.status_code(),
438
439            CreateDir { .. } | RemoveDir { .. } | ShutdownInstance { .. } | DataFusion { .. } => {
440                StatusCode::Internal
441            }
442
443            RegionNotFound { .. } => StatusCode::RegionNotFound,
444            RegionNotReady { .. } => StatusCode::RegionNotReady,
445            RegionBusy { .. } => StatusCode::RegionBusy,
446
447            StartServer { source, .. } | ShutdownServer { source, .. } => source.status_code(),
448
449            InitBackend { .. } => StatusCode::StorageUnavailable,
450
451            OpenLogStore { source, .. } => source.status_code(),
452            MetaClientInit { source, .. } => source.status_code(),
453            UnsupportedOutput { .. } => StatusCode::Unsupported,
454            HandleRegionRequest { source, .. }
455            | GetRegionMetadata { source, .. }
456            | HandleBatchOpenRequest { source, .. }
457            | HandleBatchDdlRequest { source, .. } => source.status_code(),
458            StopRegionEngine { source, .. } => source.status_code(),
459
460            FindLogicalRegions { source, .. } => source.status_code(),
461            BuildMitoEngine { source, .. } => source.status_code(),
462            BuildMetricEngine { source, .. } => source.status_code(),
463            ConcurrentQueryLimiterClosed { .. } | ConcurrentQueryLimiterTimeout { .. } => {
464                StatusCode::RegionBusy
465            }
466            MissingCache { .. } => StatusCode::Internal,
467        }
468    }
469
470    fn as_any(&self) -> &dyn Any {
471        self
472    }
473}
474
475define_into_tonic_status!(Error);