common_meta/
ddl.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::collections::HashMap;
16use std::sync::Arc;
17
18use api::v1::meta::ProcedureDetailResponse;
19use common_telemetry::tracing_context::W3cTrace;
20use store_api::storage::{RegionId, RegionNumber, TableId};
21
22use crate::cache_invalidator::CacheInvalidatorRef;
23use crate::ddl::flow_meta::FlowMetadataAllocatorRef;
24use crate::ddl::table_meta::TableMetadataAllocatorRef;
25use crate::error::{Result, UnsupportedSnafu};
26use crate::key::flow::FlowMetadataManagerRef;
27use crate::key::table_route::PhysicalTableRouteValue;
28use crate::key::TableMetadataManagerRef;
29use crate::node_manager::NodeManagerRef;
30use crate::region_keeper::MemoryRegionKeeperRef;
31use crate::region_registry::LeaderRegionRegistryRef;
32use crate::rpc::ddl::{SubmitDdlTaskRequest, SubmitDdlTaskResponse};
33use crate::rpc::procedure::{
34    AddRegionFollowerRequest, MigrateRegionRequest, MigrateRegionResponse, ProcedureStateResponse,
35    RemoveRegionFollowerRequest,
36};
37use crate::DatanodeId;
38
39pub mod alter_database;
40pub mod alter_logical_tables;
41pub mod alter_table;
42pub mod create_database;
43pub mod create_flow;
44pub mod create_logical_tables;
45pub mod create_table;
46mod create_table_template;
47pub mod create_view;
48pub mod drop_database;
49pub mod drop_flow;
50pub mod drop_table;
51pub mod drop_view;
52pub mod flow_meta;
53pub mod table_meta;
54#[cfg(any(test, feature = "testing"))]
55pub mod test_util;
56#[cfg(test)]
57pub(crate) mod tests;
58pub mod truncate_table;
59pub mod utils;
60
61#[derive(Debug, Default)]
62pub struct ExecutorContext {
63    pub tracing_context: Option<W3cTrace>,
64}
65
66/// The procedure executor that accepts ddl, region migration task etc.
67#[async_trait::async_trait]
68pub trait ProcedureExecutor: Send + Sync {
69    /// Submit a ddl task
70    async fn submit_ddl_task(
71        &self,
72        ctx: &ExecutorContext,
73        request: SubmitDdlTaskRequest,
74    ) -> Result<SubmitDdlTaskResponse>;
75
76    /// Add a region follower
77    async fn add_region_follower(
78        &self,
79        _ctx: &ExecutorContext,
80        _request: AddRegionFollowerRequest,
81    ) -> Result<()> {
82        UnsupportedSnafu {
83            operation: "add_region_follower",
84        }
85        .fail()
86    }
87
88    /// Remove a region follower
89    async fn remove_region_follower(
90        &self,
91        _ctx: &ExecutorContext,
92        _request: RemoveRegionFollowerRequest,
93    ) -> Result<()> {
94        UnsupportedSnafu {
95            operation: "remove_region_follower",
96        }
97        .fail()
98    }
99
100    /// Submit a region migration task
101    async fn migrate_region(
102        &self,
103        ctx: &ExecutorContext,
104        request: MigrateRegionRequest,
105    ) -> Result<MigrateRegionResponse>;
106
107    /// Query the procedure state by its id
108    async fn query_procedure_state(
109        &self,
110        ctx: &ExecutorContext,
111        pid: &str,
112    ) -> Result<ProcedureStateResponse>;
113
114    async fn list_procedures(&self, ctx: &ExecutorContext) -> Result<ProcedureDetailResponse>;
115}
116
117pub type ProcedureExecutorRef = Arc<dyn ProcedureExecutor>;
118
119/// Metadata allocated to a table.
120#[derive(Default)]
121pub struct TableMetadata {
122    /// Table id.
123    pub table_id: TableId,
124    /// Route information for each region of the table.
125    pub table_route: PhysicalTableRouteValue,
126    /// The encoded wal options for regions of the table.
127    // If a region does not have an associated wal options, no key for the region would be found in the map.
128    pub region_wal_options: HashMap<RegionNumber, String>,
129}
130
131pub type RegionFailureDetectorControllerRef = Arc<dyn RegionFailureDetectorController>;
132
133pub type DetectingRegion = (DatanodeId, RegionId);
134
135/// Used for actively registering Region failure detectors.
136///
137/// Ensuring the Region Supervisor can detect Region failures without relying on the first heartbeat from the datanode.
138#[async_trait::async_trait]
139pub trait RegionFailureDetectorController: Send + Sync {
140    /// Registers failure detectors for the given identifiers.
141    async fn register_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
142
143    /// Deregisters failure detectors for the given identifiers.
144    async fn deregister_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
145}
146
147/// A noop implementation of [`RegionFailureDetectorController`].
148#[derive(Debug, Clone)]
149pub struct NoopRegionFailureDetectorControl;
150
151#[async_trait::async_trait]
152impl RegionFailureDetectorController for NoopRegionFailureDetectorControl {
153    async fn register_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
154
155    async fn deregister_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
156}
157
158/// The context of ddl.
159#[derive(Clone)]
160pub struct DdlContext {
161    /// Sends querying and requests to nodes.
162    pub node_manager: NodeManagerRef,
163    /// Cache invalidation.
164    pub cache_invalidator: CacheInvalidatorRef,
165    /// Keep tracking operating regions.
166    pub memory_region_keeper: MemoryRegionKeeperRef,
167    /// The leader region registry.
168    pub leader_region_registry: LeaderRegionRegistryRef,
169    /// Table metadata manager.
170    pub table_metadata_manager: TableMetadataManagerRef,
171    /// Allocator for table metadata.
172    pub table_metadata_allocator: TableMetadataAllocatorRef,
173    /// Flow metadata manager.
174    pub flow_metadata_manager: FlowMetadataManagerRef,
175    /// Allocator for flow metadata.
176    pub flow_metadata_allocator: FlowMetadataAllocatorRef,
177    /// controller of region failure detector.
178    pub region_failure_detector_controller: RegionFailureDetectorControllerRef,
179}
180
181impl DdlContext {
182    /// Notifies the RegionSupervisor to register failure detector of new created regions.
183    ///
184    /// The datanode may crash without sending a heartbeat that contains information about newly created regions,
185    /// which may prevent the RegionSupervisor from detecting failures in these newly created regions.
186    pub async fn register_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>) {
187        self.region_failure_detector_controller
188            .register_failure_detectors(detecting_regions)
189            .await;
190    }
191
192    /// Notifies the RegionSupervisor to remove failure detectors.
193    ///
194    /// Once the regions were dropped, subsequent heartbeats no longer include these regions.
195    /// Therefore, we should remove the failure detectors for these dropped regions.
196    async fn deregister_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>) {
197        self.region_failure_detector_controller
198            .deregister_failure_detectors(detecting_regions)
199            .await;
200    }
201}