risingwave_frontend/expr/function_impl/
rw_recovery_status.rs

1// Copyright 2025 RisingWave Labs
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::sync::Arc;
16
17use risingwave_expr::{ExprError, Result, capture_context, function};
18use risingwave_pb::meta::RecoveryStatus;
19
20use super::context::META_CLIENT;
21use crate::meta_client::FrontendMetaClient;
22
23#[function("rw_recovery_status() -> varchar", volatile)]
24async fn rw_recovery_status(writer: &mut impl std::fmt::Write) -> Result<()> {
25    writer
26        .write_str(
27            rw_recovery_status_impl_captured()
28                .await?
29                .as_str_name()
30                .strip_prefix("STATUS_")
31                .unwrap(),
32        )
33        .unwrap();
34    Ok(())
35}
36
37#[function("pg_is_in_recovery() -> boolean", volatile)]
38async fn pg_is_in_recovery() -> Result<bool> {
39    let status = rw_recovery_status_impl_captured().await?;
40    Ok(status != RecoveryStatus::StatusRunning)
41}
42
43#[capture_context(META_CLIENT)]
44async fn rw_recovery_status_impl(
45    meta_client: &Arc<dyn FrontendMetaClient>,
46) -> Result<RecoveryStatus> {
47    meta_client
48        .get_cluster_recovery_status()
49        .await
50        .map_err(|e| ExprError::Internal(e.into()))
51}