Skip to main content

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::sync::Arc;
16use std::time::Duration;
17
18use store_api::storage::{RegionId, TableId};
19
20use crate::DatanodeId;
21use crate::cache_invalidator::CacheInvalidatorRef;
22use crate::ddl::flow_meta::FlowMetadataAllocatorRef;
23use crate::ddl::table_meta::TableMetadataAllocatorRef;
24use crate::key::TableMetadataManagerRef;
25use crate::key::flow::FlowMetadataManagerRef;
26use crate::key::table_route::PhysicalTableRouteValue;
27use crate::node_manager::NodeManagerRef;
28use crate::region_keeper::MemoryRegionKeeperRef;
29use crate::region_registry::LeaderRegionRegistryRef;
30use crate::wal_provider::RegionWalOptions;
31
32pub mod allocator;
33pub mod alter_database;
34pub mod alter_logical_tables;
35pub mod alter_table;
36pub mod comment_on;
37pub mod create_database;
38pub mod create_flow;
39pub mod create_logical_tables;
40pub mod create_table;
41pub(crate) use create_table::{CreateRequestBuilder, build_template_from_raw_table_info};
42pub mod create_view;
43pub mod drop_database;
44pub mod drop_flow;
45pub mod drop_table;
46pub mod drop_view;
47pub(crate) mod event;
48pub mod flow_meta;
49#[cfg(feature = "enterprise")]
50pub mod purge_dropped_table;
51pub mod table_meta;
52#[cfg(any(test, feature = "testing"))]
53pub mod test_util;
54#[cfg(test)]
55pub(crate) mod tests;
56pub mod truncate_table;
57#[cfg(feature = "enterprise")]
58pub mod undrop_table;
59pub mod utils;
60
61/// Metadata allocated to a table.
62#[derive(Default)]
63pub struct TableMetadata {
64    /// Table id.
65    pub table_id: TableId,
66    /// Route information for each region of the table.
67    pub table_route: PhysicalTableRouteValue,
68    /// The WAL options for regions of the table.
69    // If a region does not have an associated wal options, no key for the region would be found in the map.
70    pub region_wal_options: RegionWalOptions,
71}
72
73pub type RegionFailureDetectorControllerRef = Arc<dyn RegionFailureDetectorController>;
74
75pub type DetectingRegion = (DatanodeId, RegionId);
76
77/// Used for actively registering Region failure detectors.
78///
79/// Ensuring the Region Supervisor can detect Region failures without relying on the first heartbeat from the datanode.
80#[async_trait::async_trait]
81pub trait RegionFailureDetectorController: Send + Sync {
82    /// Registers failure detectors for the given identifiers.
83    async fn register_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
84
85    /// Resets failure detectors for the given identifiers.
86    async fn reset_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
87
88    /// Deregisters failure detectors for the given identifiers.
89    async fn deregister_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
90}
91
92/// A noop implementation of [`RegionFailureDetectorController`].
93#[derive(Debug, Clone)]
94pub struct NoopRegionFailureDetectorControl;
95
96#[async_trait::async_trait]
97impl RegionFailureDetectorController for NoopRegionFailureDetectorControl {
98    async fn register_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
99
100    async fn reset_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
101
102    async fn deregister_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
103}
104
105/// The context of ddl.
106#[derive(Clone)]
107pub struct DdlContext {
108    /// Sends querying and requests to nodes.
109    pub node_manager: NodeManagerRef,
110    /// Cache invalidation.
111    pub cache_invalidator: CacheInvalidatorRef,
112    /// Keep tracking operating regions.
113    pub memory_region_keeper: MemoryRegionKeeperRef,
114    /// The leader region registry.
115    pub leader_region_registry: LeaderRegionRegistryRef,
116    /// Table metadata manager.
117    pub table_metadata_manager: TableMetadataManagerRef,
118    /// Allocator for table metadata.
119    pub table_metadata_allocator: TableMetadataAllocatorRef,
120    /// Flow metadata manager.
121    pub flow_metadata_manager: FlowMetadataManagerRef,
122    /// Allocator for flow metadata.
123    pub flow_metadata_allocator: FlowMetadataAllocatorRef,
124    /// controller of region failure detector.
125    pub region_failure_detector_controller: RegionFailureDetectorControllerRef,
126    /// Whether table drops should stop after tombstoning metadata.
127    pub soft_drop_enabled: bool,
128    /// Fixed retention used to calculate new soft-drop deadlines.
129    pub soft_drop_retention: Option<Duration>,
130    /// Commits create-database metadata and the creator grant atomically.
131    pub create_database_metadata_committer:
132        Option<create_database::CreateDatabaseMetadataCommitterRef>,
133}
134
135impl DdlContext {
136    /// Notifies the RegionSupervisor to register failure detector of new created regions.
137    ///
138    /// The datanode may crash without sending a heartbeat that contains information about newly created regions,
139    /// which may prevent the RegionSupervisor from detecting failures in these newly created regions.
140    pub async fn register_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>) {
141        self.region_failure_detector_controller
142            .register_failure_detectors(detecting_regions)
143            .await;
144    }
145
146    /// Notifies the RegionSupervisor to remove failure detectors.
147    ///
148    /// Once the regions were dropped, subsequent heartbeats no longer include these regions.
149    /// Therefore, we should remove the failure detectors for these dropped regions.
150    pub(crate) async fn deregister_failure_detectors(
151        &self,
152        detecting_regions: Vec<DetectingRegion>,
153    ) {
154        self.region_failure_detector_controller
155            .deregister_failure_detectors(detecting_regions)
156            .await;
157    }
158}