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