meta_srv/service/admin/
leader.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::collections::HashMap;
16
17use snafu::ResultExt;
18use tonic::codegen::http;
19
20use crate::error::{self, Result};
21use crate::metasrv::ElectionRef;
22use crate::service::admin::HttpHandler;
23
24pub struct LeaderHandler {
25    pub election: Option<ElectionRef>,
26}
27
28#[async_trait::async_trait]
29impl HttpHandler for LeaderHandler {
30    async fn handle(
31        &self,
32        _: &str,
33        _: http::Method,
34        _: &HashMap<String, String>,
35    ) -> Result<http::Response<String>> {
36        if let Some(election) = &self.election {
37            let leader_addr = election.leader().await?.0;
38            return http::Response::builder()
39                .status(http::StatusCode::OK)
40                .body(leader_addr)
41                .context(error::InvalidHttpBodySnafu);
42        }
43        http::Response::builder()
44            .status(http::StatusCode::OK)
45            .body("election info is None".to_string())
46            .context(error::InvalidHttpBodySnafu)
47    }
48}