1use 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}