common_meta/reconciliation/reconcile_catalog/
start.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::any::Any;
16
17use common_procedure::{Context as ProcedureContext, Status};
18use serde::{Deserialize, Serialize};
19use snafu::ensure;
20
21use crate::error::{self, Result};
22use crate::key::catalog_name::CatalogNameKey;
23use crate::reconciliation::reconcile_catalog::reconcile_databases::ReconcileDatabases;
24use crate::reconciliation::reconcile_catalog::{ReconcileCatalogContext, State};
25
26#[derive(Debug, Serialize, Deserialize)]
27pub(crate) struct ReconcileCatalogStart;
28
29#[async_trait::async_trait]
30#[typetag::serde]
31impl State for ReconcileCatalogStart {
32    async fn next(
33        &mut self,
34        ctx: &mut ReconcileCatalogContext,
35        _procedure_ctx: &ProcedureContext,
36    ) -> Result<(Box<dyn State>, Status)> {
37        let exists = ctx
38            .table_metadata_manager
39            .catalog_manager()
40            .exists(CatalogNameKey {
41                catalog: &ctx.persistent_ctx.catalog,
42            })
43            .await?;
44
45        ensure!(
46            exists,
47            error::CatalogNotFoundSnafu {
48                catalog: &ctx.persistent_ctx.catalog
49            },
50        );
51
52        Ok((Box::new(ReconcileDatabases), Status::executing(true)))
53    }
54
55    fn as_any(&self) -> &dyn Any {
56        self
57    }
58}