meta_srv/service/admin/
procedure.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 common_meta::key::runtime_switch::RuntimeSwitchManagerRef;
18use common_telemetry::info;
19use serde::{Deserialize, Serialize};
20use snafu::ResultExt;
21use tonic::codegen::http;
22use tonic::codegen::http::Response;
23
24use crate::error::RuntimeSwitchManagerSnafu;
25use crate::service::admin::util::{to_json_response, to_not_found_response};
26use crate::service::admin::HttpHandler;
27
28#[derive(Clone)]
29pub struct ProcedureManagerHandler {
30    pub manager: RuntimeSwitchManagerRef,
31}
32
33#[derive(Debug, Serialize, Deserialize)]
34struct ProcedureManagerStatusResponse {
35    status: ProcedureManagerStatus,
36}
37
38#[derive(Debug, Serialize, Deserialize)]
39#[serde(rename_all = "snake_case")]
40enum ProcedureManagerStatus {
41    Paused,
42    Running,
43}
44
45impl ProcedureManagerHandler {
46    async fn pause_procedure_manager(&self) -> crate::Result<ProcedureManagerStatusResponse> {
47        self.manager
48            .pasue_procedure()
49            .await
50            .context(RuntimeSwitchManagerSnafu)?;
51        // TODO(weny): Add a record to the system events.
52        info!("Pause the procedure manager.");
53        Ok(ProcedureManagerStatusResponse {
54            status: ProcedureManagerStatus::Paused,
55        })
56    }
57
58    async fn resume_procedure_manager(&self) -> crate::Result<ProcedureManagerStatusResponse> {
59        self.manager
60            .resume_procedure()
61            .await
62            .context(RuntimeSwitchManagerSnafu)?;
63        // TODO(weny): Add a record to the system events.
64        info!("Resume the procedure manager.");
65        Ok(ProcedureManagerStatusResponse {
66            status: ProcedureManagerStatus::Running,
67        })
68    }
69
70    async fn get_procedure_manager_status(&self) -> crate::Result<ProcedureManagerStatusResponse> {
71        let is_paused = self
72            .manager
73            .is_procedure_paused()
74            .await
75            .context(RuntimeSwitchManagerSnafu)?;
76        let response = ProcedureManagerStatusResponse {
77            status: if is_paused {
78                ProcedureManagerStatus::Paused
79            } else {
80                ProcedureManagerStatus::Running
81            },
82        };
83
84        Ok(response)
85    }
86}
87
88#[async_trait::async_trait]
89impl HttpHandler for ProcedureManagerHandler {
90    async fn handle(
91        &self,
92        path: &str,
93        method: http::Method,
94        _: &HashMap<String, String>,
95    ) -> crate::Result<Response<String>> {
96        match method {
97            http::Method::GET => {
98                if path.ends_with("status") {
99                    let response = self.get_procedure_manager_status().await?;
100                    to_json_response(response)
101                } else {
102                    to_not_found_response()
103                }
104            }
105            http::Method::POST => {
106                if path.ends_with("pause") {
107                    let response = self.pause_procedure_manager().await?;
108                    to_json_response(response)
109                } else if path.ends_with("resume") {
110                    let response = self.resume_procedure_manager().await?;
111                    to_json_response(response)
112                } else {
113                    to_not_found_response()
114                }
115            }
116            _ => to_not_found_response(),
117        }
118    }
119}