flow/engine.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
15//! Define a trait for flow engine, which is used by both streaming engine and batch engine
16
17use std::collections::HashMap;
18
19use common_meta::key::flow::flow_state::FlowStat;
20use session::context::QueryContext;
21use table::metadata::TableId;
22
23use crate::Error;
24// TODO(discord9): refactor common types for flow to a separate module
25/// FlowId is a unique identifier for a flow task
26pub type FlowId = u64;
27pub type TableName = [String; 3];
28
29/// The arguments to create a flow
30#[derive(Debug, Clone)]
31pub struct CreateFlowArgs {
32 pub flow_id: FlowId,
33 pub sink_table_name: TableName,
34 pub source_table_ids: Vec<TableId>,
35 pub create_if_not_exists: bool,
36 pub or_replace: bool,
37 pub expire_after: Option<i64>,
38 pub eval_interval: Option<i64>,
39 pub comment: Option<String>,
40 pub sql: String,
41 pub flow_options: HashMap<String, String>,
42 pub query_ctx: Option<QueryContext>,
43 /// Typed schedule configuration for `EVAL INTERVAL` flows.
44 pub eval_schedule: Option<common_meta::key::flow::flow_info::FlowScheduleConfig>,
45}
46
47pub trait FlowEngine {
48 /// Create a flow using the provided arguments, return previous flow id if exists and is replaced
49 async fn create_flow(&self, args: CreateFlowArgs) -> Result<Option<FlowId>, Error>;
50 /// Remove a flow by its ID
51 async fn remove_flow(&self, flow_id: FlowId) -> Result<(), Error>;
52 /// Flush the flow, return the number of rows flushed
53 async fn flush_flow(&self, flow_id: FlowId) -> Result<usize, Error>;
54 /// Check if the flow exists
55 async fn flow_exist(&self, flow_id: FlowId) -> Result<bool, Error>;
56 /// List all flows
57 async fn list_flows(&self) -> Result<impl IntoIterator<Item = FlowId>, Error>;
58 /// Handle the insert requests for the flow
59 async fn handle_flow_inserts(
60 &self,
61 request: api::v1::region::InsertRequests,
62 ) -> Result<(), Error>;
63
64 async fn handle_mark_window_dirty(
65 &self,
66 req: api::v1::flow::DirtyWindowRequests,
67 ) -> Result<(), Error>;
68}
69
70/// Provides flow runtime statistics for information schema and heartbeat reporting.
71pub trait FlowStatProvider {
72 /// Returns current runtime stats of an engine.
73 async fn flow_stat(&self) -> FlowStat;
74}