Skip to main content

common_function/
handlers.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 api::v1::meta::ReconcileRequest;
18use async_trait::async_trait;
19use catalog::CatalogManagerRef;
20use common_base::AffectedRows;
21use common_meta::rpc::procedure::{
22    GcRegionsRequest as MetaGcRegionsRequest, GcResponse as MetaGcResponse,
23    GcTableRequest as MetaGcTableRequest, ManageRegionFollowerRequest, MigrateRegionRequest,
24    ProcedureStateResponse,
25};
26use common_query::Output;
27use common_query::error::Result;
28use session::context::QueryContextRef;
29use store_api::storage::RegionId;
30use table::requests::{
31    BuildIndexTableRequest, CompactTableRequest, DeleteRequest, FlushTableRequest, InsertRequest,
32};
33use table::table_name::TableName;
34
35/// A trait for handling table mutations in `QueryEngine`.
36#[async_trait]
37pub trait TableMutationHandler: Send + Sync {
38    /// Inserts rows into the table.
39    async fn insert(&self, request: InsertRequest, ctx: QueryContextRef) -> Result<Output>;
40
41    /// Delete rows from the table.
42    async fn delete(&self, request: DeleteRequest, ctx: QueryContextRef) -> Result<AffectedRows>;
43
44    /// Trigger a flush task for table.
45    async fn flush(&self, request: FlushTableRequest, ctx: QueryContextRef)
46    -> Result<AffectedRows>;
47
48    /// Trigger a compaction task for table.
49    async fn compact(
50        &self,
51        request: CompactTableRequest,
52        ctx: QueryContextRef,
53    ) -> Result<AffectedRows>;
54
55    /// Trigger an index build task for the table.
56    async fn build_index(
57        &self,
58        request: BuildIndexTableRequest,
59        ctx: QueryContextRef,
60    ) -> Result<AffectedRows>;
61
62    /// Trigger a flush task for a table region.
63    async fn flush_region(&self, region_id: RegionId, ctx: QueryContextRef)
64    -> Result<AffectedRows>;
65
66    /// Trigger a compaction task for a table region.
67    async fn compact_region(
68        &self,
69        region_id: RegionId,
70        ctx: QueryContextRef,
71    ) -> Result<AffectedRows>;
72
73    /// Cancels one identified compaction task on a table region.
74    async fn cancel_region_compaction(
75        &self,
76        region_id: RegionId,
77        task_id: String,
78        ctx: QueryContextRef,
79    ) -> Result<AffectedRows>;
80
81    /// Discard all unflushed data from a table region.
82    async fn discard_unflushed_data(
83        &self,
84        region_id: RegionId,
85        ctx: QueryContextRef,
86    ) -> Result<AffectedRows>;
87
88    /// Discard all unflushed data from all regions of a table.
89    async fn discard_unflushed_data_by_table(
90        &self,
91        table_name: TableName,
92        ctx: QueryContextRef,
93    ) -> Result<AffectedRows>;
94
95    /// Discards the pending WAL tails that datanode `node_id` wrote from a table region.
96    async fn discard_pending_tails(
97        &self,
98        region_id: RegionId,
99        node_id: u64,
100        ctx: QueryContextRef,
101    ) -> Result<AffectedRows>;
102
103    /// Discards the pending WAL tails that datanode `node_id` wrote from all regions of a
104    /// table.
105    async fn discard_pending_tails_by_table(
106        &self,
107        table_name: TableName,
108        node_id: u64,
109        ctx: QueryContextRef,
110    ) -> Result<AffectedRows>;
111}
112
113/// A trait for handling procedure service requests in `QueryEngine`.
114#[async_trait]
115pub trait ProcedureServiceHandler: Send + Sync {
116    /// Permanently purge a dropped table.
117    async fn purge_table(&self, query_ctx: QueryContextRef, table_name: TableName) -> Result<()>;
118
119    /// Migrate a region from source peer to target peer, returns the procedure id if success.
120    async fn migrate_region(
121        &self,
122        query_ctx: QueryContextRef,
123        request: MigrateRegionRequest,
124    ) -> Result<Option<String>>;
125
126    /// Reconcile a table, database or catalog, returns the procedure id if success.
127    async fn reconcile(&self, request: ReconcileRequest) -> Result<Option<String>>;
128
129    /// Query the procedure' state by its id
130    async fn query_procedure_state(&self, pid: &str) -> Result<ProcedureStateResponse>;
131
132    /// Manage a region follower to a region.
133    async fn manage_region_follower(
134        &self,
135        query_ctx: QueryContextRef,
136        request: ManageRegionFollowerRequest,
137    ) -> Result<()>;
138
139    /// Get the catalog manager
140    fn catalog_manager(&self) -> &CatalogManagerRef;
141
142    /// Manually trigger GC for specific regions.
143    async fn gc_regions(
144        &self,
145        query_ctx: QueryContextRef,
146        request: MetaGcRegionsRequest,
147    ) -> Result<MetaGcResponse>;
148
149    /// Manually trigger GC for a table.
150    async fn gc_table(
151        &self,
152        query_ctx: QueryContextRef,
153        request: MetaGcTableRequest,
154    ) -> Result<MetaGcResponse>;
155}
156
157/// This flow service handler is only use for flush flow for now.
158#[async_trait]
159pub trait FlowServiceHandler: Send + Sync {
160    async fn flush(
161        &self,
162        catalog: &str,
163        flow: &str,
164        ctx: QueryContextRef,
165    ) -> Result<api::v1::flow::FlowResponse>;
166}
167
168pub type TableMutationHandlerRef = Arc<dyn TableMutationHandler>;
169
170pub type ProcedureServiceHandlerRef = Arc<dyn ProcedureServiceHandler>;
171
172pub type FlowServiceHandlerRef = Arc<dyn FlowServiceHandler>;