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