Skip to main content

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