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 store_api::storage::{RegionId, RegionNumber, 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;
30
31pub mod alter_database;
32pub mod alter_logical_tables;
33pub mod alter_table;
34pub mod comment_on;
35pub mod create_database;
36pub mod create_flow;
37pub mod create_logical_tables;
38pub mod create_table;
39mod create_table_template;
40pub(crate) use create_table_template::{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 encoded 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: HashMap<RegionNumber, String>,
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    /// Deregisters failure detectors for the given identifiers.
80    async fn deregister_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>);
81}
82
83/// A noop implementation of [`RegionFailureDetectorController`].
84#[derive(Debug, Clone)]
85pub struct NoopRegionFailureDetectorControl;
86
87#[async_trait::async_trait]
88impl RegionFailureDetectorController for NoopRegionFailureDetectorControl {
89    async fn register_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
90
91    async fn deregister_failure_detectors(&self, _detecting_regions: Vec<DetectingRegion>) {}
92}
93
94/// The context of ddl.
95#[derive(Clone)]
96pub struct DdlContext {
97    /// Sends querying and requests to nodes.
98    pub node_manager: NodeManagerRef,
99    /// Cache invalidation.
100    pub cache_invalidator: CacheInvalidatorRef,
101    /// Keep tracking operating regions.
102    pub memory_region_keeper: MemoryRegionKeeperRef,
103    /// The leader region registry.
104    pub leader_region_registry: LeaderRegionRegistryRef,
105    /// Table metadata manager.
106    pub table_metadata_manager: TableMetadataManagerRef,
107    /// Allocator for table metadata.
108    pub table_metadata_allocator: TableMetadataAllocatorRef,
109    /// Flow metadata manager.
110    pub flow_metadata_manager: FlowMetadataManagerRef,
111    /// Allocator for flow metadata.
112    pub flow_metadata_allocator: FlowMetadataAllocatorRef,
113    /// controller of region failure detector.
114    pub region_failure_detector_controller: RegionFailureDetectorControllerRef,
115}
116
117impl DdlContext {
118    /// Notifies the RegionSupervisor to register failure detector of new created regions.
119    ///
120    /// The datanode may crash without sending a heartbeat that contains information about newly created regions,
121    /// which may prevent the RegionSupervisor from detecting failures in these newly created regions.
122    pub async fn register_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>) {
123        self.region_failure_detector_controller
124            .register_failure_detectors(detecting_regions)
125            .await;
126    }
127
128    /// Notifies the RegionSupervisor to remove failure detectors.
129    ///
130    /// Once the regions were dropped, subsequent heartbeats no longer include these regions.
131    /// Therefore, we should remove the failure detectors for these dropped regions.
132    async fn deregister_failure_detectors(&self, detecting_regions: Vec<DetectingRegion>) {
133        self.region_failure_detector_controller
134            .deregister_failure_detectors(detecting_regions)
135            .await;
136    }
137}