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