greptimedb 0.0.1

Synchronous, completion-driven, thread-affine Rust SDK for GreptimeDB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Application diagnostics, gRPC trailers, error classification, and retry decisions.

use anyhow::Result;
use greptimedb::hints::*;
use greptimedb::{Error, ErrorCode, RetryableError};
use greptimedb_proto::v1::{self, ResponseHeader, Status};
use std::{error::Error as _, io};

fn trailers(inner: Option<&str>, hints: &[&str]) -> Result<grpc::Metadata> {
    let mut metadata = grpc::Metadata::new();
    if let Some(inner) = inner {
        metadata.append_ascii("inner_error_msg", inner)?;
    }
    for hint in hints {
        metadata.append_ascii(GREPTIME_RETRY_HINT_HEADER, hint)?;
    }
    Ok(metadata)
}

fn status_error(metadata: &grpc::Metadata) -> Error {
    Error::from_grpc_status_and_trailers(
        grpc::Status::unavailable("original gRPC failure"),
        Some(metadata),
    )
}

fn metadata(entries: &[(&str, &str)]) -> Result<grpc::Metadata> {
    let mut metadata = grpc::Metadata::new();
    for (key, value) in entries {
        metadata.append_ascii(key, value)?;
    }
    Ok(metadata)
}

#[test]
fn application_code_mapping_preserves_every_category_and_unknown_code() {
    let codes = [
        (0, ErrorCode::Success),
        (1000, ErrorCode::Unknown),
        (1001, ErrorCode::Unsupported),
        (1002, ErrorCode::Unexpected),
        (1003, ErrorCode::Internal),
        (1004, ErrorCode::InvalidArguments),
        (1005, ErrorCode::Cancelled),
        (1006, ErrorCode::IllegalState),
        (2000, ErrorCode::Syntax),
        (2001, ErrorCode::Plan),
        (2002, ErrorCode::EngineExecute),
        (3000, ErrorCode::TableAlreadyExists),
        (3001, ErrorCode::TableNotFound),
        (3002, ErrorCode::TableColumnNotFound),
        (3003, ErrorCode::TableColumnExists),
        (3004, ErrorCode::DatabaseNotFound),
        (3005, ErrorCode::DatabaseAlreadyExists),
        (4000, ErrorCode::StorageUnavailable),
        (4001, ErrorCode::RegionNotFound),
        (4002, ErrorCode::RegionReadonly),
        (4003, ErrorCode::RegionBusy),
        (4004, ErrorCode::RateLimited),
        (4005, ErrorCode::FlowControl),
        (5000, ErrorCode::MetaSrv),
        (6000, ErrorCode::AuthFailed),
    ];
    for (raw, category) in codes {
        assert_eq!(ErrorCode::from_u32(raw), category);
        assert_eq!(category.as_u32(), raw);
    }
    for raw in [1, 1007, 5999, 7000, u32::MAX] {
        assert_eq!(ErrorCode::from_u32(raw), ErrorCode::Other(raw));
        assert_eq!(ErrorCode::from_u32(raw).as_u32(), raw);
    }
    assert_eq!(ErrorCode::from_u32(6999), ErrorCode::AuthFailed);
}

#[test]
fn response_headers_are_checked_even_when_grpc_succeeds() -> Result<()> {
    Error::from_response_header(None)?;
    Error::from_response_header(Some(&v1::ResponseHeader { status: None }))?;
    Error::from_response_header(Some(&v1::ResponseHeader {
        status: Some(v1::Status {
            status_code: 0,
            err_msg: String::new(),
            retry_hint: "true".into(),
        }),
    }))?;
    for hint in ["true", "false", "", "TRUE", "junk"] {
        let header = v1::ResponseHeader {
            status: Some(v1::Status {
                status_code: 6999,
                err_msg: "access denied".into(),
                retry_hint: hint.into(),
            }),
        };
        let error =
            Error::from_response_header(Some(&header)).expect_err("nonzero application status");
        assert!(
            matches!(&error, Error::ServerStatus { code: ErrorCode::AuthFailed, raw_code: 6999, message, retry_hint } if message == "access denied" && *retry_hint == (hint == "true"))
        );
        assert_eq!(error.is_retryable(), hint == "true");
        assert_eq!(error.retry_hint(), Some(hint == "true"));
    }
    Ok(())
}

#[test]
fn grpc_trailers_preserve_structured_status_and_explicit_retry_decisions() -> Result<()> {
    assert_eq!(GREPTIME_INNER_ERROR_HEADER, "INNER_ERROR_MSG");
    for hint in ["true", "false"] {
        let metadata = trailers(Some(r#"{"code":4003,"err_msg":"region busy"}"#), &[hint])?;
        let error = status_error(&metadata);
        assert!(
            matches!(&error, Error::ServerStatus { code: ErrorCode::RegionBusy, raw_code: 4003, message, retry_hint } if message == "region busy" && *retry_hint == (hint == "true"))
        );
        assert_eq!(error.retry_hint(), Some(hint == "true"));
    }
    let metadata = trailers(Some(r#"{"code":999999,"err_msg":"unknown failure"}"#), &[])?;
    assert!(matches!(
        status_error(&metadata),
        Error::ServerStatus {
            code: ErrorCode::Other(999999),
            raw_code: 999999,
            retry_hint: false,
            ..
        }
    ));
    Ok(())
}

#[test]
fn malformed_inner_errors_never_replace_the_original_grpc_status() -> Result<()> {
    for inner in [
        "not json",
        "[]",
        "null",
        r#"{"code":0,"err_msg":"success"}"#,
        r#"{"code":-1,"err_msg":"bad"}"#,
        r#"{"code":4294967296,"err_msg":"overflow"}"#,
        r#"{"code":"4003","err_msg":"string"}"#,
        r#"{"code":4003.5,"err_msg":"fraction"}"#,
        r#"{"code":4003}"#,
        r#"{"code":4003,"err_msg":2}"#,
    ] {
        match status_error(&trailers(Some(inner), &[])?) {
            Error::Grpc(status) => {
                assert_eq!(status.code(), grpc::core::Code::Unavailable);
                assert_eq!(status.message(), "original gRPC failure");
            }
            other => anyhow::bail!("malformed JSON replaced gRPC status: {other}"),
        }
    }
    assert!(matches!(
        Error::from_grpc_status_and_trailers(grpc::Status::unavailable("original"), None),
        Error::Grpc(_)
    ));
    Ok(())
}

#[test]
fn retry_hints_without_json_and_conflicting_duplicates_are_conservative() -> Result<()> {
    for hint in ["true", "false"] {
        let error = status_error(&trailers(Some("invalid JSON"), &[hint])?);
        assert_eq!(error.retry_hint(), Some(hint == "true"));
        assert!(
            matches!(error, Error::ServerStatus { code: ErrorCode::Unknown, raw_code: 1000, message, .. } if message == "original gRPC failure")
        );
    }
    for hints in [
        vec!["true", "false"],
        vec!["false", "true"],
        vec!["true", "junk"],
        vec!["junk", "true"],
    ] {
        assert!(
            !status_error(&trailers(
                Some(r#"{"code":4003,"err_msg":"busy"}"#),
                &hints
            )?)
            .is_retryable()
        );
    }
    let no_hint = status_error(&trailers(None, &[])?);
    assert_eq!(no_hint.retry_hint(), None);
    Ok(())
}

#[test]
fn retry_and_source_chains_do_not_authorize_uncertain_write_replay() {
    let transport = Error::from(io::Error::new(
        io::ErrorKind::TimedOut,
        "write outcome unknown",
    ));
    assert!(!transport.is_retryable());
    assert!(transport.source().is_some());
    let aggregate = Error::AllEndpointsFailed {
        attempts: 2,
        last_error: Box::new(Error::ServerStatus {
            code: ErrorCode::RegionBusy,
            raw_code: 4003,
            message: "busy".into(),
            retry_hint: true,
        }),
    };
    assert!(aggregate.is_retryable());
    assert_eq!(aggregate.retry_hint(), Some(true));
    assert!(aggregate.source().is_some());
    assert!(aggregate.to_string().contains("2 attempts"));
    for error in [
        Error::Stream("stream".into()),
        Error::InvalidConfig("config".into()),
        Error::SchemaViolation("schema".into()),
        Error::Encode("encode".into()),
        Error::Decode("decode".into()),
        Error::Protocol("protocol".into()),
        Error::CircuitOpen {
            endpoint: "localhost:4001".into(),
        },
    ] {
        assert!(!error.is_retryable());
    }
}

#[test]
fn mixed_case_or_numeric_retry_hints_never_authorize_replay() -> Result<()> {
    for hint in ["TRUE", "True", "1", " true", "true "] {
        for inner in [None, Some(r#"{"code":4003,"err_msg":"busy"}"#)] {
            let error = status_error(&trailers(inner, &[hint])?);
            assert!(!error.is_retryable(), "accepted {hint:?}");
        }
    }
    for raw_code in [4000, 4001, 4002, 4003, 4004, 4005, 5000] {
        let inner = format!(r#"{{"code":{raw_code},"err_msg":"transient"}}"#);
        for hint in [None, Some("true"), Some("false")] {
            let hints: Vec<_> = hint.into_iter().collect();
            let error = status_error(&trailers(Some(&inner), &hints)?);
            assert_eq!(error.is_retryable(), hint == Some("true"));
        }
    }
    for kind in [
        io::ErrorKind::TimedOut,
        io::ErrorKind::WouldBlock,
        io::ErrorKind::Interrupted,
        io::ErrorKind::ConnectionReset,
        io::ErrorKind::ConnectionRefused,
        io::ErrorKind::BrokenPipe,
    ] {
        assert!(!Error::Transport(io::Error::new(kind, "uncertain delivery")).is_retryable());
    }
    Ok(())
}

#[test]
fn numeric_error_trailers_require_consistent_nonzero_decimal_u32() -> Result<()> {
    for code in [
        "",
        "junk",
        "4294967296",
        "-1",
        "+4003",
        "0",
        "4003.0",
        " 4003",
        "4003 ",
    ] {
        let mut metadata = trailers(None, &[])?;
        metadata.append_ascii("x-greptime-err-code", code)?;
        assert!(
            matches!(status_error(&metadata), Error::Grpc(_)),
            "accepted {code:?}"
        );
    }
    for hint in [None, Some("true"), Some("false"), Some("TRUE")] {
        let hints: Vec<_> = hint.into_iter().collect();
        let mut metadata = trailers(None, &hints)?;
        metadata.append_ascii("x-greptime-err-code", "4003")?;
        let error = status_error(&metadata);
        assert!(
            matches!(&error, Error::ServerStatus { code: ErrorCode::RegionBusy, raw_code: 4003, message, .. } if message == "original gRPC failure")
        );
        assert_eq!(error.is_retryable(), hint == Some("true"));
    }
    for codes in [["4003", "4004"], ["4003", "invalid"], ["invalid", "4003"]] {
        let mut metadata = trailers(None, &[])?;
        for code in codes {
            metadata.append_ascii("x-greptime-err-code", code)?;
        }
        assert!(matches!(status_error(&metadata), Error::Grpc(_)));
    }
    let mut repeated = trailers(Some("invalid JSON"), &[])?;
    repeated.append_ascii("x-greptime-err-code", "4294967295")?;
    repeated.append_ascii("x-greptime-err-code", "4294967295")?;
    assert!(matches!(
        status_error(&repeated),
        Error::ServerStatus {
            raw_code: u32::MAX,
            code: ErrorCode::Other(u32::MAX),
            retry_hint: false,
            ..
        }
    ));
    let mut invalid = trailers(None, &["true"])?;
    invalid.append_ascii("x-greptime-err-code", "overflow")?;
    assert!(matches!(
        status_error(&invalid),
        Error::ServerStatus {
            code: ErrorCode::Unknown,
            raw_code: 1000,
            retry_hint: true,
            ..
        }
    ));
    let mut metadata = trailers(
        Some(r#"{"code":3001,"err_msg":"missing table"}"#),
        &["false"],
    )?;
    metadata.append_ascii("x-greptime-err-code", "4003")?;
    assert!(matches!(
        status_error(&metadata),
        Error::ServerStatus {
            code: ErrorCode::TableNotFound,
            raw_code: 3001,
            ..
        }
    ));
    Ok(())
}

#[test]
fn retry_hint_wire_vocabulary_is_shared_and_remains_strict() -> Result<()> {
    for (hint, expected) in [
        ("true", true),
        ("retryable", true),
        ("false", false),
        ("non_retryable", false),
        ("TRUE", false),
        ("Retryable", false),
        (" true ", false),
        (" retryable ", false),
        ("1", false),
    ] {
        let header = ResponseHeader {
            status: Some(Status {
                status_code: 4003,
                err_msg: "busy".into(),
                retry_hint: hint.into(),
            }),
        };
        let error = Error::from_response_header(Some(&header)).expect_err("application error");
        assert_eq!(error.is_retryable(), expected, "response hint {hint:?}");
        let trailers = metadata(&[
            ("x-greptime-err-code", "4003"),
            ("x-greptime-err-retry-hint", hint),
        ])?;
        let error = Error::from_grpc_status_and_trailers(
            grpc::Status::unavailable("busy"),
            Some(&trailers),
        );
        assert_eq!(error.is_retryable(), expected, "trailer hint {hint:?}");
    }
    for second in ["false", "non_retryable", "junk", "TRUE"] {
        let trailers = metadata(&[
            ("x-greptime-err-retry-hint", "retryable"),
            ("x-greptime-err-retry-hint", second),
        ])?;
        assert!(
            !Error::from_grpc_status_and_trailers(
                grpc::Status::unavailable("busy"),
                Some(&trailers)
            )
            .is_retryable()
        );
    }
    Ok(())
}

#[test]
fn ok_trailers_require_an_actual_application_failure() -> Result<()> {
    Error::check_ok_trailers(&grpc::Metadata::new())?;
    for hint in ["true", "false", "retryable", "non_retryable"] {
        Error::check_ok_trailers(&metadata(&[("x-greptime-err-retry-hint", hint)])?)?;
        Error::check_ok_trailers(&metadata(&[
            ("x-greptime-err-code", "0"),
            ("x-greptime-err-retry-hint", hint),
        ])?)?;
    }
    Error::check_ok_trailers(&metadata(&[(
        "inner_error_msg",
        r#"{"code":0,"err_msg":""}"#,
    )])?)?;
    let trailers = metadata(&[
        ("x-greptime-err-code", "4003"),
        ("x-greptime-err-msg", "region unavailable"),
    ])?;
    assert!(
        matches!(Error::check_ok_trailers(&trailers), Err(Error::ServerStatus { raw_code: 4003, message, .. }) if message == "region unavailable")
    );
    let trailers = metadata(&[(
        "inner_error_msg",
        r#"{"code":4000,"err_msg":"storage failed"}"#,
    )])?;
    assert!(
        matches!(Error::check_ok_trailers(&trailers), Err(Error::ServerStatus { raw_code: 4000, message, .. }) if message == "storage failed")
    );
    let trailers = metadata(&[("x-greptime-err-msg", "diagnostic without code")])?;
    assert!(Error::check_ok_trailers(&trailers).is_err());
    Ok(())
}

#[test]
fn v121_classifier_is_explicit_and_legacy_numbers_remain_compatible() {
    for (raw, code) in [
        (3000, ErrorCode::TableAlreadyExists),
        (4001, ErrorCode::RegionNotFound),
        (6001, ErrorCode::AuthFailed),
        (7000, ErrorCode::Other(7000)),
        (7002, ErrorCode::AuthFailed),
    ] {
        assert_eq!(ErrorCode::from_u32(raw), code);
    }
    assert_eq!(ErrorCode::TableAlreadyExists.as_u32(), 3000);
    assert_eq!(ErrorCode::RegionNotFound.as_u32(), 4001);
    assert_eq!(ErrorCode::AuthFailed.as_u32(), 6000);
    for (raw, code) in [
        (0, ErrorCode::Success),
        (3000, ErrorCode::Plan),
        (3001, ErrorCode::EngineExecute),
        (4000, ErrorCode::TableAlreadyExists),
        (4001, ErrorCode::TableNotFound),
        (4003, ErrorCode::RegionBusy),
        (4005, ErrorCode::RegionNotFound),
        (5000, ErrorCode::StorageUnavailable),
        (6000, ErrorCode::ResourceExhausted),
        (6001, ErrorCode::RateLimited),
        (7000, ErrorCode::AuthFailed),
        (7002, ErrorCode::AuthFailed),
        (7006, ErrorCode::AuthFailed),
        (u32::MAX, ErrorCode::Other(u32::MAX)),
    ] {
        assert_eq!(ErrorCode::from_v121_u32(raw), code, "wire code {raw}");
    }
}