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