Skip to main content

common_options/
datanode.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::time::Duration;
16
17use common_base::readable_size::ReadableSize;
18use common_grpc::channel_manager::{self, ChannelConfig};
19use serde::{Deserialize, Serialize};
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
22pub struct DatanodeClientOptions {
23    pub client: ClientOptions,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(default)]
28pub struct ClientOptions {
29    #[serde(with = "humantime_serde")]
30    pub timeout: Duration,
31    #[serde(with = "humantime_serde")]
32    pub connect_timeout: Duration,
33    pub tcp_nodelay: bool,
34    /// Maximum size of a message received from a datanode.
35    pub max_recv_message_size: ReadableSize,
36    /// Maximum size of a message sent to a datanode.
37    pub max_send_message_size: ReadableSize,
38}
39
40impl Default for ClientOptions {
41    fn default() -> Self {
42        Self {
43            timeout: Duration::from_secs(channel_manager::DEFAULT_GRPC_REQUEST_TIMEOUT_SECS),
44            connect_timeout: Duration::from_secs(
45                channel_manager::DEFAULT_GRPC_CONNECT_TIMEOUT_SECS,
46            ),
47            tcp_nodelay: true,
48            max_recv_message_size: channel_manager::DEFAULT_MAX_GRPC_RECV_MESSAGE_SIZE,
49            max_send_message_size: channel_manager::DEFAULT_MAX_GRPC_SEND_MESSAGE_SIZE,
50        }
51    }
52}
53
54impl ClientOptions {
55    /// Creates a gRPC [`ChannelConfig`] from these datanode client options.
56    pub fn channel_config(&self) -> ChannelConfig {
57        ChannelConfig {
58            timeout: Some(self.timeout),
59            connect_timeout: Some(self.connect_timeout),
60            tcp_nodelay: self.tcp_nodelay,
61            max_recv_message_size: self.max_recv_message_size,
62            max_send_message_size: self.max_send_message_size,
63            ..Default::default()
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use std::time::Duration;
71
72    use common_base::readable_size::ReadableSize;
73    use serde_json::json;
74
75    use super::ClientOptions;
76
77    #[test]
78    fn test_client_options_backward_compatibility() {
79        let options: ClientOptions = serde_json::from_value(json!({
80            "timeout": "10s",
81            "connect_timeout": "5s",
82            "tcp_nodelay": false
83        }))
84        .unwrap();
85
86        assert_eq!(ReadableSize::mb(512), options.max_recv_message_size);
87        assert_eq!(ReadableSize::mb(512), options.max_send_message_size);
88    }
89
90    #[test]
91    fn test_client_options_channel_config() {
92        let options: ClientOptions = serde_json::from_value(json!({
93            "timeout": "20s",
94            "connect_timeout": "8s",
95            "tcp_nodelay": false,
96            "max_recv_message_size": "1GB",
97            "max_send_message_size": "2GB"
98        }))
99        .unwrap();
100
101        let channel_config = options.channel_config();
102        assert_eq!(Some(Duration::from_secs(20)), channel_config.timeout);
103        assert_eq!(Some(Duration::from_secs(8)), channel_config.connect_timeout);
104        assert!(!channel_config.tcp_nodelay);
105        assert_eq!(ReadableSize::gb(1), channel_config.max_recv_message_size);
106        assert_eq!(ReadableSize::gb(2), channel_config.max_send_message_size);
107    }
108}