From 5f3885b64a255d925937b323af088ab2eb48e825 Mon Sep 17 00:00:00 2001 From: NiwakaDev Date: Thu, 10 Oct 2024 07:29:36 +0900 Subject: [PATCH] feat: initial implementation of AlterDatabaseProcedure --- src/common/meta/src/ddl.rs | 1 + src/common/meta/src/ddl/alter_database.rs | 105 ++++++++++++++++++++++ src/common/meta/src/ddl_manager.rs | 2 + 3 files changed, 108 insertions(+) create mode 100644 src/common/meta/src/ddl/alter_database.rs diff --git a/src/common/meta/src/ddl.rs b/src/common/meta/src/ddl.rs index 7186997906e3..3c17bb19a82a 100644 --- a/src/common/meta/src/ddl.rs +++ b/src/common/meta/src/ddl.rs @@ -31,6 +31,7 @@ use crate::rpc::ddl::{SubmitDdlTaskRequest, SubmitDdlTaskResponse}; use crate::rpc::procedure::{MigrateRegionRequest, MigrateRegionResponse, ProcedureStateResponse}; use crate::{ClusterId, DatanodeId}; +pub mod alter_database; pub mod alter_logical_tables; pub mod alter_table; pub mod create_database; diff --git a/src/common/meta/src/ddl/alter_database.rs b/src/common/meta/src/ddl/alter_database.rs new file mode 100644 index 000000000000..7a269553b21b --- /dev/null +++ b/src/common/meta/src/ddl/alter_database.rs @@ -0,0 +1,105 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use async_trait::async_trait; +use common_procedure::error::{FromJsonSnafu, Result as ProcedureResult, ToJsonSnafu}; +use common_procedure::{Context, LockKey, Procedure, Status}; +use serde::{Deserialize, Serialize}; +use snafu::ResultExt; + +use super::utils::handle_retry_error; +use crate::ddl::DdlContext; +use crate::error::Result; +use crate::lock_key::{CatalogLock, SchemaLock}; +use crate::ClusterId; + +pub struct AlterDatabaseProcedure { + _context: DdlContext, + data: AlterDatabaseData, +} + +impl AlterDatabaseProcedure { + pub const TYPE_NAME: &'static str = "metasrv-procedure::AlterDatabase"; + + pub fn new( + _cluster_id: ClusterId, + context: DdlContext, + catalog: String, + schema: String, + ) -> Self { + AlterDatabaseProcedure { + _context: context, + data: AlterDatabaseData { + state: AlterDatabaseState::Prepare, + catalog, + schema, + }, + } + } + + pub fn from_json(json: &str, context: DdlContext) -> ProcedureResult { + let data = serde_json::from_str(json).context(FromJsonSnafu)?; + + Ok(Self { + _context: context, + data, + }) + } + + async fn on_prepare(&mut self) -> Result { + todo!(); + } + + async fn on_update_metadata(&mut self) -> Result { + todo!(); + } +} + +#[async_trait] +impl Procedure for AlterDatabaseProcedure { + fn type_name(&self) -> &str { + Self::TYPE_NAME + } + async fn execute(&mut self, _ctx: &Context) -> ProcedureResult { + let state = &self.data.state; + match state { + AlterDatabaseState::Prepare => self.on_prepare().await, + AlterDatabaseState::UpdateMetadata => self.on_update_metadata().await, + } + .map_err(handle_retry_error) + } + fn lock_key(&self) -> LockKey { + let lock_key = vec![ + CatalogLock::Read(&self.data.catalog).into(), + SchemaLock::write(&self.data.catalog, &self.data.schema).into(), + ]; + LockKey::new(lock_key) + } + fn dump(&self) -> common_procedure::Result { + serde_json::to_string(&self.data).context(ToJsonSnafu) + } +} + +#[derive(Debug, Serialize, Deserialize)] +enum AlterDatabaseState { + Prepare, + UpdateMetadata, +} + +#[derive(Debug, Serialize, Deserialize)] +struct AlterDatabaseData { + state: AlterDatabaseState, + catalog: String, + schema: String, +} diff --git a/src/common/meta/src/ddl_manager.rs b/src/common/meta/src/ddl_manager.rs index 1b561b30430a..dbea6e2bd0ee 100644 --- a/src/common/meta/src/ddl_manager.rs +++ b/src/common/meta/src/ddl_manager.rs @@ -23,6 +23,7 @@ use derive_builder::Builder; use snafu::{ensure, OptionExt, ResultExt}; use store_api::storage::TableId; +use crate::ddl::alter_database::AlterDatabaseProcedure; use crate::ddl::alter_logical_tables::AlterLogicalTablesProcedure; use crate::ddl::alter_table::AlterTableProcedure; use crate::ddl::create_database::CreateDatabaseProcedure; @@ -126,6 +127,7 @@ impl DdlManager { CreateLogicalTablesProcedure, CreateViewProcedure, CreateFlowProcedure, + AlterDatabaseProcedure, AlterTableProcedure, AlterLogicalTablesProcedure, DropTableProcedure,