1pub mod cursor;
16pub mod end;
17pub mod executor;
18pub mod metadata;
19pub mod start;
20use std::any::Any;
21use std::fmt::Debug;
22
23use common_error::ext::BoxedError;
24use common_procedure::error::{ExternalSnafu, FromJsonSnafu, ToJsonSnafu};
25use common_procedure::{
26 Context as ProcedureContext, EventContext, EventTrigger, LockKey, Procedure,
27 Result as ProcedureResult, Status,
28};
29use futures::stream::{BoxStream, Peekable};
30use serde::{Deserialize, Serialize};
31use snafu::ResultExt;
32use tonic::async_trait;
33
34use self::start::DropDatabaseStart;
35use crate::ddl::DdlContext;
36use crate::ddl::event::database::{DROP_DATABASE_EVENT_TYPE, DatabaseDdlEvent};
37use crate::ddl::utils::map_to_procedure_error;
38use crate::error::Result;
39use crate::key::table_name::TableNameValue;
40use crate::lock_key::{CatalogLock, SchemaLock};
41
42pub struct DropDatabaseProcedure {
43 runtime_context: DdlContext,
45 context: DropDatabaseContext,
46
47 state: Box<dyn State>,
48}
49
50#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
52pub(crate) enum DropTableTarget {
53 Logical,
54 Physical,
55}
56
57type TableStream = Peekable<BoxStream<'static, Result<(String, TableNameValue)>>>;
58
59pub(crate) struct DropDatabaseContext {
61 catalog: String,
62 schema: String,
63 drop_if_exists: bool,
64 tables: Option<TableStream>,
65 retrying: bool,
66}
67
68#[async_trait::async_trait]
69#[typetag::serde(tag = "drop_database_state")]
70pub(crate) trait State: Send + Debug {
71 async fn next(
73 &mut self,
74 ddl_ctx: &DdlContext,
75 ctx: &mut DropDatabaseContext,
76 ) -> Result<(Box<dyn State>, Status)>;
77
78 fn recover(&mut self, _ddl_ctx: &DdlContext) -> Result<()> {
80 Ok(())
81 }
82
83 fn as_any(&self) -> &dyn Any;
85}
86
87impl DropDatabaseProcedure {
88 pub const TYPE_NAME: &'static str = "metasrv-procedure::DropDatabase";
89
90 pub fn new(catalog: String, schema: String, drop_if_exists: bool, context: DdlContext) -> Self {
91 Self {
92 runtime_context: context,
93 context: DropDatabaseContext {
94 catalog,
95 schema,
96 drop_if_exists,
97 tables: None,
98 retrying: false,
99 },
100 state: Box::new(DropDatabaseStart),
101 }
102 }
103
104 pub fn from_json(json: &str, runtime_context: DdlContext) -> ProcedureResult<Self> {
105 let DropDatabaseOwnedData {
106 catalog,
107 schema,
108 drop_if_exists,
109 state,
110 } = serde_json::from_str(json).context(FromJsonSnafu)?;
111
112 Ok(Self {
113 runtime_context,
114 context: DropDatabaseContext {
115 catalog,
116 schema,
117 drop_if_exists,
118 tables: None,
119 retrying: false,
120 },
121 state,
122 })
123 }
124
125 #[cfg(test)]
126 pub(crate) fn state(&self) -> &dyn State {
127 self.state.as_ref()
128 }
129}
130
131#[async_trait]
132impl Procedure for DropDatabaseProcedure {
133 fn type_name(&self) -> &str {
134 Self::TYPE_NAME
135 }
136
137 fn recover(&mut self) -> ProcedureResult<()> {
138 self.state
139 .recover(&self.runtime_context)
140 .map_err(BoxedError::new)
141 .context(ExternalSnafu {
142 clean_poisons: false,
143 })
144 }
145
146 async fn execute(&mut self, ctx: &ProcedureContext) -> ProcedureResult<Status> {
147 let state = &mut self.state;
148
149 self.context.retrying = ctx.is_retrying().await.unwrap_or(false);
150 let (next, status) = state
151 .next(&self.runtime_context, &mut self.context)
152 .await
153 .map_err(map_to_procedure_error)?;
154
155 *state = next;
156 Ok(status)
157 }
158
159 fn dump(&self) -> ProcedureResult<String> {
160 let data = DropDatabaseData {
161 catalog: &self.context.catalog,
162 schema: &self.context.schema,
163 drop_if_exists: self.context.drop_if_exists,
164 state: self.state.as_ref(),
165 };
166
167 serde_json::to_string(&data).context(ToJsonSnafu)
168 }
169
170 fn lock_key(&self) -> LockKey {
171 let lock_key = vec![
172 CatalogLock::Read(&self.context.catalog).into(),
173 SchemaLock::write(&self.context.catalog, &self.context.schema).into(),
174 ];
175
176 LockKey::new(lock_key)
177 }
178
179 fn event(&self, ctx: &EventContext<'_>) -> Option<Box<dyn common_event_recorder::Event>> {
180 if !ctx.event_type_filter.allows(DROP_DATABASE_EVENT_TYPE) {
181 return None;
182 }
183
184 let event = if matches!(&ctx.trigger, EventTrigger::Submitted) {
185 DatabaseDdlEvent::drop_submitted(
186 &self.context.catalog,
187 &self.context.schema,
188 self.context.drop_if_exists,
189 )
190 } else {
191 DatabaseDdlEvent::drop_lifecycle(&self.context.catalog, &self.context.schema)
192 };
193 Some(Box::new(event))
194 }
195}
196
197#[derive(Debug, Serialize)]
198struct DropDatabaseData<'a> {
199 catalog: &'a str,
201 schema: &'a str,
203 drop_if_exists: bool,
204 state: &'a dyn State,
205}
206
207#[derive(Debug, Deserialize)]
208struct DropDatabaseOwnedData {
209 catalog: String,
211 schema: String,
213 drop_if_exists: bool,
214 state: Box<dyn State>,
215}