-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement the drop database procedure (#3541)
* refactor: remove Sync trait of Procedure * refactor: remove unnecessary async * feat: implement the drop database procedure * refactor: refactor DdlManager register_loaders * feat: register the DropDatabaseProcedureLoader * chore: fmt toml * feat: support to submit DropDatabaseTask * feat: support drop database stmt * fix: empty the tables stream * fix: ensure the factory always exists * test: update sqlness results * chore: correct comments * test: update sqlness results * test: update sqlness results * chore: apply suggestions from CR * chore: apply suggestions from CR
- Loading branch information
Showing
31 changed files
with
903 additions
and
150 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// 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. | ||
|
||
pub mod cursor; | ||
pub mod end; | ||
pub mod executor; | ||
pub mod metadata; | ||
pub mod start; | ||
use std::fmt::Debug; | ||
|
||
use common_procedure::error::{Error as ProcedureError, FromJsonSnafu, ToJsonSnafu}; | ||
use common_procedure::{ | ||
Context as ProcedureContext, LockKey, Procedure, Result as ProcedureResult, Status, | ||
}; | ||
use futures::stream::BoxStream; | ||
use serde::{Deserialize, Serialize}; | ||
use snafu::ResultExt; | ||
use tonic::async_trait; | ||
|
||
use self::start::DropDatabaseStart; | ||
use crate::ddl::DdlContext; | ||
use crate::error::Result; | ||
use crate::key::table_name::TableNameValue; | ||
use crate::lock_key::{CatalogLock, SchemaLock}; | ||
|
||
pub struct DropDatabaseProcedure { | ||
/// The context of procedure runtime. | ||
runtime_context: DdlContext, | ||
context: DropDatabaseContext, | ||
|
||
state: Box<dyn State>, | ||
} | ||
|
||
/// Target of dropping tables. | ||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] | ||
pub enum DropTableTarget { | ||
Logical, | ||
Physical, | ||
} | ||
|
||
/// Context of [DropDatabaseProcedure] execution. | ||
pub struct DropDatabaseContext { | ||
catalog: String, | ||
schema: String, | ||
drop_if_exists: bool, | ||
tables: Option<BoxStream<'static, Result<(String, TableNameValue)>>>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
#[typetag::serde(tag = "drop_database_state")] | ||
pub(crate) trait State: Send + Debug { | ||
/// Yields the next [State] and [Status]. | ||
async fn next( | ||
&mut self, | ||
ddl_ctx: &DdlContext, | ||
ctx: &mut DropDatabaseContext, | ||
) -> Result<(Box<dyn State>, Status)>; | ||
} | ||
|
||
impl DropDatabaseProcedure { | ||
pub const TYPE_NAME: &'static str = "metasrv-procedure::DropDatabase"; | ||
|
||
pub fn new(catalog: String, schema: String, drop_if_exists: bool, context: DdlContext) -> Self { | ||
Self { | ||
runtime_context: context, | ||
context: DropDatabaseContext { | ||
catalog, | ||
schema, | ||
drop_if_exists, | ||
tables: None, | ||
}, | ||
state: Box::new(DropDatabaseStart), | ||
} | ||
} | ||
|
||
pub fn from_json(json: &str, runtime_context: DdlContext) -> ProcedureResult<Self> { | ||
let DropDatabaseOwnedData { | ||
catalog, | ||
schema, | ||
drop_if_exists, | ||
state, | ||
} = serde_json::from_str(json).context(FromJsonSnafu)?; | ||
|
||
Ok(Self { | ||
runtime_context, | ||
context: DropDatabaseContext { | ||
catalog, | ||
schema, | ||
drop_if_exists, | ||
tables: None, | ||
}, | ||
state, | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Procedure for DropDatabaseProcedure { | ||
fn type_name(&self) -> &str { | ||
Self::TYPE_NAME | ||
} | ||
|
||
async fn execute(&mut self, _ctx: &ProcedureContext) -> ProcedureResult<Status> { | ||
let state = &mut self.state; | ||
|
||
let (next, status) = state | ||
.next(&self.runtime_context, &mut self.context) | ||
.await | ||
.map_err(|e| { | ||
if e.is_retry_later() { | ||
ProcedureError::retry_later(e) | ||
} else { | ||
ProcedureError::external(e) | ||
} | ||
})?; | ||
|
||
*state = next; | ||
Ok(status) | ||
} | ||
|
||
fn dump(&self) -> ProcedureResult<String> { | ||
let data = DropDatabaseData { | ||
catalog: &self.context.catalog, | ||
schema: &self.context.schema, | ||
drop_if_exists: self.context.drop_if_exists, | ||
state: self.state.as_ref(), | ||
}; | ||
|
||
serde_json::to_string(&data).context(ToJsonSnafu) | ||
} | ||
|
||
fn lock_key(&self) -> LockKey { | ||
let lock_key = vec![ | ||
CatalogLock::Read(&self.context.catalog).into(), | ||
SchemaLock::write(&self.context.catalog, &self.context.schema).into(), | ||
]; | ||
|
||
LockKey::new(lock_key) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
struct DropDatabaseData<'a> { | ||
// The catalog name | ||
catalog: &'a str, | ||
// The schema name | ||
schema: &'a str, | ||
drop_if_exists: bool, | ||
state: &'a dyn State, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct DropDatabaseOwnedData { | ||
// The catalog name | ||
catalog: String, | ||
// The schema name | ||
schema: String, | ||
drop_if_exists: bool, | ||
state: Box<dyn State>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// 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 common_procedure::Status; | ||
use futures::TryStreamExt; | ||
use serde::{Deserialize, Serialize}; | ||
use snafu::OptionExt; | ||
use table::metadata::TableId; | ||
|
||
use super::executor::DropDatabaseExecutor; | ||
use super::metadata::DropDatabaseRemoveMetadata; | ||
use super::DropTableTarget; | ||
use crate::ddl::drop_database::{DropDatabaseContext, State}; | ||
use crate::ddl::DdlContext; | ||
use crate::error::{self, Result}; | ||
use crate::key::table_route::TableRouteValue; | ||
use crate::key::DeserializedValueWithBytes; | ||
use crate::table_name::TableName; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct DropDatabaseCursor { | ||
target: DropTableTarget, | ||
} | ||
|
||
impl DropDatabaseCursor { | ||
/// Returns a new [DropDatabaseCursor]. | ||
pub fn new(target: DropTableTarget) -> Self { | ||
Self { target } | ||
} | ||
|
||
fn handle_reach_end( | ||
&mut self, | ||
ctx: &mut DropDatabaseContext, | ||
) -> Result<(Box<dyn State>, Status)> { | ||
match self.target { | ||
DropTableTarget::Logical => { | ||
// Consumes the tables stream. | ||
ctx.tables.take(); | ||
|
||
Ok(( | ||
Box::new(DropDatabaseCursor::new(DropTableTarget::Physical)), | ||
Status::executing(true), | ||
)) | ||
} | ||
DropTableTarget::Physical => Ok(( | ||
Box::new(DropDatabaseRemoveMetadata), | ||
Status::executing(true), | ||
)), | ||
} | ||
} | ||
|
||
async fn handle_table( | ||
&mut self, | ||
ddl_ctx: &DdlContext, | ||
ctx: &mut DropDatabaseContext, | ||
table_name: String, | ||
table_id: TableId, | ||
table_route_value: DeserializedValueWithBytes<TableRouteValue>, | ||
) -> Result<(Box<dyn State>, Status)> { | ||
match (self.target, table_route_value.get_inner_ref()) { | ||
(DropTableTarget::Logical, TableRouteValue::Logical(_)) | ||
| (DropTableTarget::Physical, TableRouteValue::Physical(_)) => { | ||
// TODO(weny): Maybe we can drop the table without fetching the `TableInfoValue` | ||
let table_info_value = ddl_ctx | ||
.table_metadata_manager | ||
.table_info_manager() | ||
.get(table_id) | ||
.await? | ||
.context(error::TableNotFoundSnafu { | ||
table_name: &table_name, | ||
})?; | ||
Ok(( | ||
Box::new(DropDatabaseExecutor::new( | ||
TableName::new(&ctx.catalog, &ctx.schema, &table_name), | ||
table_id, | ||
table_info_value, | ||
table_route_value, | ||
self.target, | ||
)), | ||
Status::executing(true), | ||
)) | ||
} | ||
_ => Ok(( | ||
Box::new(DropDatabaseCursor::new(self.target)), | ||
Status::executing(false), | ||
)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
#[typetag::serde] | ||
impl State for DropDatabaseCursor { | ||
async fn next( | ||
&mut self, | ||
ddl_ctx: &DdlContext, | ||
ctx: &mut DropDatabaseContext, | ||
) -> Result<(Box<dyn State>, Status)> { | ||
if ctx.tables.as_deref().is_none() { | ||
let tables = ddl_ctx | ||
.table_metadata_manager | ||
.table_name_manager() | ||
.tables(&ctx.catalog, &ctx.schema); | ||
ctx.tables = Some(tables); | ||
} | ||
// Safety: must exist | ||
match ctx.tables.as_mut().unwrap().try_next().await? { | ||
Some((table_name, table_name_value)) => { | ||
let table_id = table_name_value.table_id(); | ||
match ddl_ctx | ||
.table_metadata_manager | ||
.table_route_manager() | ||
.table_route_storage() | ||
.get_raw(table_id) | ||
.await? | ||
{ | ||
Some(table_route_value) => { | ||
self.handle_table(ddl_ctx, ctx, table_name, table_id, table_route_value) | ||
.await | ||
} | ||
None => Ok(( | ||
Box::new(DropDatabaseCursor::new(self.target)), | ||
Status::executing(false), | ||
)), | ||
} | ||
} | ||
None => self.handle_reach_end(ctx), | ||
} | ||
} | ||
} |
Oops, something went wrong.