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};
33
34/// A trait for handling table mutations in `QueryEngine`.
35#[async_trait]
36pub trait TableMutationHandler: Send + Sync {
37    /// Inserts rows into the table.
38    async fn insert(&self, request: InsertRequest, ctx: QueryContextRef) -> Result<Output>;
39
40    /// Delete rows from the table.
41    async fn delete(&self, request: DeleteRequest, ctx: QueryContextRef) -> Result<AffectedRows>;
42
43    /// Trigger a flush task for table.
44    async fn flush(&self, request: FlushTableRequest, ctx: QueryContextRef)
45    -> Result<AffectedRows>;
46
47    /// Trigger a compaction task for table.
48    async fn compact(
49        &self,
50        request: CompactTableRequest,
51        ctx: QueryContextRef,
52    ) -> Result<AffectedRows>;
53
54    /// Trigger an index build task for the table.
55    async fn build_index(
56        &self,
57        request: BuildIndexTableRequest,
58        ctx: QueryContextRef,
59    ) -> Result<AffectedRows>;
60
61    /// Trigger a flush task for a table region.
62    async fn flush_region(&self, region_id: RegionId, ctx: QueryContextRef)
63    -> Result<AffectedRows>;
64
65    /// Trigger a compaction task for a table region.
66    async fn compact_region(
67        &self,
68        region_id: RegionId,
69        ctx: QueryContextRef,
70    ) -> Result<AffectedRows>;
71}
72
73/// A trait for handling procedure service requests in `QueryEngine`.
74#[async_trait]
75pub trait ProcedureServiceHandler: Send + Sync {
76    /// Migrate a region from source peer to target peer, returns the procedure id if success.
77    async fn migrate_region(&self, request: MigrateRegionRequest) -> Result<Option<String>>;
78
79    /// Reconcile a table, database or catalog, returns the procedure id if success.
80    async fn reconcile(&self, request: ReconcileRequest) -> Result<Option<String>>;
81
82    /// Query the procedure' state by its id
83    async fn query_procedure_state(&self, pid: &str) -> Result<ProcedureStateResponse>;
84
85    /// Manage a region follower to a region.
86    async fn manage_region_follower(&self, request: ManageRegionFollowerRequest) -> Result<()>;
87
88    /// Get the catalog manager
89    fn catalog_manager(&self) -> &CatalogManagerRef;
90
91    /// Manually trigger GC for specific regions.
92    async fn gc_regions(&self, request: MetaGcRegionsRequest) -> Result<MetaGcResponse>;
93
94    /// Manually trigger GC for a table.
95    async fn gc_table(&self, request: MetaGcTableRequest) -> Result<MetaGcResponse>;
96}
97
98/// This flow service handler is only use for flush flow for now.
99#[async_trait]
100pub trait FlowServiceHandler: Send + Sync {
101    async fn flush(
102        &self,
103        catalog: &str,
104        flow: &str,
105        ctx: QueryContextRef,
106    ) -> Result<api::v1::flow::FlowResponse>;
107}
108
109pub type TableMutationHandlerRef = Arc<dyn TableMutationHandler>;
110
111pub type ProcedureServiceHandlerRef = Arc<dyn ProcedureServiceHandler>;
112
113pub type FlowServiceHandlerRef = Arc<dyn FlowServiceHandler>;