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