meta_srv/procedure/
repartition.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
15pub mod allocate_region;
16pub mod collect;
17pub mod deallocate_region;
18pub mod dispatch;
19pub mod group;
20pub mod plan;
21pub mod repartition_end;
22pub mod repartition_start;
23
24use std::any::Any;
25use std::fmt::Debug;
26
27use common_meta::cache_invalidator::CacheInvalidatorRef;
28use common_meta::key::TableMetadataManagerRef;
29use common_procedure::{Context as ProcedureContext, Status};
30use serde::{Deserialize, Serialize};
31use store_api::storage::TableId;
32
33use crate::error::Result;
34use crate::procedure::repartition::plan::RepartitionPlanEntry;
35use crate::service::mailbox::MailboxRef;
36
37#[cfg(test)]
38pub mod test_util;
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
41pub struct PersistentContext {
42    pub catalog_name: String,
43    pub schema_name: String,
44    pub table_name: String,
45    pub table_id: TableId,
46    pub plans: Vec<RepartitionPlanEntry>,
47}
48
49pub struct Context {
50    pub persistent_ctx: PersistentContext,
51    pub table_metadata_manager: TableMetadataManagerRef,
52    pub mailbox: MailboxRef,
53    pub server_addr: String,
54    pub cache_invalidator: CacheInvalidatorRef,
55}
56
57#[async_trait::async_trait]
58#[typetag::serde(tag = "repartition_state")]
59pub(crate) trait State: Sync + Send + Debug {
60    fn name(&self) -> &'static str {
61        let type_name = std::any::type_name::<Self>();
62        // short name
63        type_name.split("::").last().unwrap_or(type_name)
64    }
65
66    /// Yields the next [State] and [Status].
67    async fn next(
68        &mut self,
69        ctx: &mut Context,
70        procedure_ctx: &ProcedureContext,
71    ) -> Result<(Box<dyn State>, Status)>;
72
73    fn as_any(&self) -> &dyn Any;
74}