client/
client_manager.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::fmt::{Debug, Formatter};
16use std::sync::Arc;
17use std::time::Duration;
18
19use common_grpc::channel_manager::{ChannelConfig, ChannelManager};
20use common_meta::node_manager::{DatanodeRef, FlownodeRef, NodeManager};
21use common_meta::peer::Peer;
22use moka::future::{Cache, CacheBuilder};
23
24use crate::flow::FlowRequester;
25use crate::region::RegionRequester;
26use crate::Client;
27
28pub struct NodeClients {
29    channel_manager: ChannelManager,
30    clients: Cache<Peer, Client>,
31}
32
33impl Default for NodeClients {
34    fn default() -> Self {
35        Self::new(ChannelConfig::new())
36    }
37}
38
39impl Debug for NodeClients {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("NodeClients")
42            .field("channel_manager", &self.channel_manager)
43            .finish()
44    }
45}
46
47#[async_trait::async_trait]
48impl NodeManager for NodeClients {
49    async fn datanode(&self, datanode: &Peer) -> DatanodeRef {
50        let client = self.get_client(datanode).await;
51
52        let ChannelConfig {
53            send_compression,
54            accept_compression,
55            ..
56        } = self.channel_manager.config();
57        Arc::new(RegionRequester::new(
58            client,
59            *send_compression,
60            *accept_compression,
61        ))
62    }
63
64    async fn flownode(&self, flownode: &Peer) -> FlownodeRef {
65        let client = self.get_client(flownode).await;
66
67        Arc::new(FlowRequester::new(client))
68    }
69}
70
71impl NodeClients {
72    pub fn new(config: ChannelConfig) -> Self {
73        Self {
74            channel_manager: ChannelManager::with_config(config),
75            clients: CacheBuilder::new(1024)
76                .time_to_live(Duration::from_secs(30 * 60))
77                .time_to_idle(Duration::from_secs(5 * 60))
78                .build(),
79        }
80    }
81
82    pub async fn get_client(&self, datanode: &Peer) -> Client {
83        self.clients
84            .get_with_by_ref(datanode, async move {
85                Client::with_manager_and_urls(
86                    self.channel_manager.clone(),
87                    vec![datanode.addr.clone()],
88                )
89            })
90            .await
91    }
92
93    #[cfg(feature = "testing")]
94    pub async fn insert_client(&self, datanode: Peer, client: Client) {
95        self.clients.insert(datanode, client).await
96    }
97}