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