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
35use crate::flow_admin_args::AdminArg;
36
37/// A trait for handling table mutations in `QueryEngine`.
38#[async_trait]
39pub trait TableMutationHandler: Send + Sync {
40    /// Inserts rows into the table.
41    async fn insert(&self, request: InsertRequest, ctx: QueryContextRef) -> Result<Output>;
42
43    /// Delete rows from the table.
44    async fn delete(&self, request: DeleteRequest, ctx: QueryContextRef) -> Result<AffectedRows>;
45
46    /// Trigger a flush task for table.
47    async fn flush(&self, request: FlushTableRequest, ctx: QueryContextRef)
48    -> Result<AffectedRows>;
49
50    /// Trigger a compaction task for table.
51    async fn compact(
52        &self,
53        request: CompactTableRequest,
54        ctx: QueryContextRef,
55    ) -> Result<AffectedRows>;
56
57    /// Trigger an index build task for the table.
58    async fn build_index(
59        &self,
60        request: BuildIndexTableRequest,
61        ctx: QueryContextRef,
62    ) -> Result<AffectedRows>;
63
64    /// Trigger a flush task for a table region.
65    async fn flush_region(&self, region_id: RegionId, ctx: QueryContextRef)
66    -> Result<AffectedRows>;
67
68    /// Trigger a compaction task for a table region.
69    async fn compact_region(
70        &self,
71        region_id: RegionId,
72        ctx: QueryContextRef,
73    ) -> Result<AffectedRows>;
74
75    /// Cancels one identified compaction task on a table region.
76    async fn cancel_region_compaction(
77        &self,
78        region_id: RegionId,
79        task_id: String,
80        ctx: QueryContextRef,
81    ) -> Result<AffectedRows>;
82
83    /// Discard all unflushed data from a table region.
84    async fn discard_unflushed_data(
85        &self,
86        region_id: RegionId,
87        ctx: QueryContextRef,
88    ) -> Result<AffectedRows>;
89
90    /// Discard all unflushed data from all regions of a table.
91    async fn discard_unflushed_data_by_table(
92        &self,
93        table_name: TableName,
94        ctx: QueryContextRef,
95    ) -> Result<AffectedRows>;
96
97    /// Discards the pending WAL tails that datanode `node_id` wrote from a table region.
98    async fn discard_pending_tails(
99        &self,
100        region_id: RegionId,
101        node_id: u64,
102        ctx: QueryContextRef,
103    ) -> Result<AffectedRows>;
104
105    /// Discards the pending WAL tails that datanode `node_id` wrote from all regions of a
106    /// table.
107    async fn discard_pending_tails_by_table(
108        &self,
109        table_name: TableName,
110        node_id: u64,
111        ctx: QueryContextRef,
112    ) -> Result<AffectedRows>;
113}
114
115/// A trait for handling procedure service requests in `QueryEngine`.
116#[async_trait]
117pub trait ProcedureServiceHandler: Send + Sync {
118    /// Permanently purge a dropped table.
119    async fn purge_table(&self, query_ctx: QueryContextRef, table_name: TableName) -> Result<()>;
120
121    /// Migrate a region from source peer to target peer, returns the procedure id if success.
122    async fn migrate_region(
123        &self,
124        query_ctx: QueryContextRef,
125        request: MigrateRegionRequest,
126    ) -> Result<Option<String>>;
127
128    /// Reconcile a table, database or catalog, returns the procedure id if success.
129    async fn reconcile(&self, request: ReconcileRequest) -> Result<Option<String>>;
130
131    /// Query the procedure' state by its id
132    async fn query_procedure_state(&self, pid: &str) -> Result<ProcedureStateResponse>;
133
134    /// Manage a region follower to a region.
135    async fn manage_region_follower(
136        &self,
137        query_ctx: QueryContextRef,
138        request: ManageRegionFollowerRequest,
139    ) -> Result<()>;
140
141    /// Get the catalog manager
142    fn catalog_manager(&self) -> &CatalogManagerRef;
143
144    /// Manually trigger GC for specific regions.
145    async fn gc_regions(
146        &self,
147        query_ctx: QueryContextRef,
148        request: MetaGcRegionsRequest,
149    ) -> Result<MetaGcResponse>;
150
151    /// Manually trigger GC for a table.
152    async fn gc_table(
153        &self,
154        query_ctx: QueryContextRef,
155        request: MetaGcTableRequest,
156    ) -> Result<MetaGcResponse>;
157}
158
159#[async_trait]
160pub trait FlowServiceHandler: Send + Sync {
161    async fn flush(
162        &self,
163        catalog: &str,
164        flow: &str,
165        ctx: QueryContextRef,
166    ) -> Result<api::v1::flow::FlowResponse>;
167
168    /// Runs the flow-admin function `function` on the flownode that owns
169    /// `catalog.flow`, with the function's own `args` (in the order the function
170    /// documents).
171    ///
172    /// The route carries no per-function request: the flownode resolves
173    /// `function` itself and answers with its result in the
174    /// `flow_admin_result` extension of the response.
175    async fn admin(
176        &self,
177        catalog: &str,
178        flow: &str,
179        function: &str,
180        args: Vec<AdminArg>,
181        ctx: QueryContextRef,
182    ) -> Result<api::v1::flow::FlowResponse>;
183}
184
185pub type TableMutationHandlerRef = Arc<dyn TableMutationHandler>;
186
187pub type ProcedureServiceHandlerRef = Arc<dyn ProcedureServiceHandler>;
188
189pub type FlowServiceHandlerRef = Arc<dyn FlowServiceHandler>;