common_frontend/
selector.rs1use std::fmt::Debug;
16use std::time::Duration;
17
18use common_grpc::channel_manager::{ChannelConfig, ChannelManager};
19use common_meta::cluster::{ClusterInfo, NodeInfo, Role};
20use greptime_proto::v1::frontend::{
21 frontend_client, KillProcessRequest, KillProcessResponse, ListProcessRequest,
22 ListProcessResponse,
23};
24use meta_client::MetaClientRef;
25use snafu::ResultExt;
26use tonic::Response;
27
28use crate::error;
29use crate::error::{MetaSnafu, Result};
30
31pub type FrontendClientPtr = Box<dyn FrontendClient>;
32
33#[async_trait::async_trait]
34pub trait FrontendClient: Send + Debug {
35 async fn list_process(&mut self, req: ListProcessRequest) -> Result<ListProcessResponse>;
36
37 async fn kill_process(&mut self, req: KillProcessRequest) -> Result<KillProcessResponse>;
38}
39
40#[async_trait::async_trait]
41impl FrontendClient for frontend_client::FrontendClient<tonic::transport::channel::Channel> {
42 async fn list_process(&mut self, req: ListProcessRequest) -> Result<ListProcessResponse> {
43 frontend_client::FrontendClient::<tonic::transport::channel::Channel>::list_process(
44 self, req,
45 )
46 .await
47 .context(error::InvokeFrontendSnafu)
48 .map(Response::into_inner)
49 }
50
51 async fn kill_process(&mut self, req: KillProcessRequest) -> Result<KillProcessResponse> {
52 frontend_client::FrontendClient::<tonic::transport::channel::Channel>::kill_process(
53 self, req,
54 )
55 .await
56 .context(error::InvokeFrontendSnafu)
57 .map(Response::into_inner)
58 }
59}
60
61#[async_trait::async_trait]
62pub trait FrontendSelector {
63 async fn select<F>(&self, predicate: F) -> Result<Vec<FrontendClientPtr>>
64 where
65 F: Fn(&NodeInfo) -> bool + Send;
66}
67
68#[derive(Debug, Clone)]
69pub struct MetaClientSelector {
70 meta_client: MetaClientRef,
71 channel_manager: ChannelManager,
72}
73
74#[async_trait::async_trait]
75impl FrontendSelector for MetaClientSelector {
76 async fn select<F>(&self, predicate: F) -> Result<Vec<FrontendClientPtr>>
77 where
78 F: Fn(&NodeInfo) -> bool + Send,
79 {
80 let nodes = self
81 .meta_client
82 .list_nodes(Some(Role::Frontend))
83 .await
84 .map_err(Box::new)
85 .context(MetaSnafu)?;
86
87 nodes
88 .into_iter()
89 .filter(predicate)
90 .map(|node| {
91 let channel = self
92 .channel_manager
93 .get(node.peer.addr)
94 .context(error::CreateChannelSnafu)?;
95 let client = frontend_client::FrontendClient::new(channel);
96 Ok(Box::new(client) as FrontendClientPtr)
97 })
98 .collect::<Result<Vec<_>>>()
99 }
100}
101
102impl MetaClientSelector {
103 pub fn new(meta_client: MetaClientRef) -> Self {
104 let cfg = ChannelConfig::new()
105 .connect_timeout(Duration::from_secs(30))
106 .timeout(Duration::from_secs(30));
107 let channel_manager = ChannelManager::with_config(cfg);
108 Self {
109 meta_client,
110 channel_manager,
111 }
112 }
113}