Skip to main content

common_wal/
kafka.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 common_error::ext::RetryHint;
16
17/// Maps an rskafka client error to a conservative retry hint.
18///
19/// rskafka already retries most transient Kafka/network errors internally. This helper only
20/// marks errors retryable when rskafka has exhausted its own retry loop or returns an explicit
21/// client timeout.
22pub fn rskafka_client_error_to_retry_hint(error: &rskafka::client::error::Error) -> RetryHint {
23    use rskafka::client::error::Error;
24
25    match error {
26        Error::RetryFailed(_)
27        | Error::Timeout
28        | Error::Connection(rskafka::ConnectionError::RetryFailed(_)) => RetryHint::Retryable,
29        _ => RetryHint::NonRetryable,
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use std::time::Duration;
36
37    use common_error::ext::RetryHint;
38    use rskafka::BackoffError;
39    use rskafka::client::error::{Error as KafkaClientError, ProtocolError, RequestContext};
40
41    use super::*;
42
43    fn retry_failed_error() -> KafkaClientError {
44        KafkaClientError::RetryFailed(BackoffError::DeadlineExceded {
45            deadline: Duration::from_secs(1),
46            source: Box::new(std::io::Error::other("retry failed")),
47        })
48    }
49
50    #[test]
51    fn test_retry_failed_hint_is_retryable() {
52        assert_eq!(
53            rskafka_client_error_to_retry_hint(&retry_failed_error()),
54            RetryHint::Retryable
55        );
56    }
57
58    #[test]
59    fn test_timeout_hint_is_retryable() {
60        assert_eq!(
61            rskafka_client_error_to_retry_hint(&KafkaClientError::Timeout),
62            RetryHint::Retryable
63        );
64    }
65
66    #[test]
67    fn test_connection_retry_failed_hint_is_retryable() {
68        let err = KafkaClientError::Connection(rskafka::ConnectionError::RetryFailed(
69            BackoffError::DeadlineExceded {
70                deadline: Duration::from_secs(1),
71                source: Box::new(std::io::Error::other("retry failed")),
72            },
73        ));
74
75        assert_eq!(
76            rskafka_client_error_to_retry_hint(&err),
77            RetryHint::Retryable
78        );
79    }
80
81    #[test]
82    fn test_raw_protocol_error_hint_is_non_retryable() {
83        let err = KafkaClientError::ServerError {
84            protocol_error: ProtocolError::NetworkException,
85            error_message: None,
86            request: RequestContext::Topic("test_topic".to_string()),
87            response: None,
88            is_virtual: false,
89        };
90
91        assert_eq!(
92            rskafka_client_error_to_retry_hint(&err),
93            RetryHint::NonRetryable
94        );
95    }
96}