common_frontend/
selector.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;
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}