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::{DatanodeManager, DatanodeRef, FlownodeManager, FlownodeRef};
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 DatanodeManager 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
65#[async_trait::async_trait]
66impl FlownodeManager for NodeClients {
67    async fn flownode(&self, flownode: &Peer) -> FlownodeRef {
68        let client = self.get_client(flownode).await;
69
70        Arc::new(FlowRequester::new(client))
71    }
72}
73
74impl NodeClients {
75    pub fn new(config: ChannelConfig) -> Self {
76        Self {
77            channel_manager: ChannelManager::with_config(config),
78            clients: CacheBuilder::new(1024)
79                .time_to_live(Duration::from_secs(30 * 60))
80                .time_to_idle(Duration::from_secs(5 * 60))
81                .build(),
82        }
83    }
84
85    pub async fn get_client(&self, datanode: &Peer) -> Client {
86        self.clients
87            .get_with_by_ref(datanode, async move {
88                Client::with_manager_and_urls(
89                    self.channel_manager.clone(),
90                    vec![datanode.addr.clone()],
91                )
92            })
93            .await
94    }
95
96    #[cfg(feature = "testing")]
97    pub async fn insert_client(&self, datanode: Peer, client: Client) {
98        self.clients.insert(datanode, client).await
99    }
100}