Skip to main content

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    /// Duration in seconds.
38    pub expire_after: Option<i64>,
39    pub eval_interval: Option<i64>,
40    pub comment: Option<String>,
41    pub sql: String,
42    pub flow_options: HashMap<String, String>,
43    pub query_ctx: Option<QueryContext>,
44    /// Typed schedule configuration for `EVAL INTERVAL` flows.
45    pub eval_schedule: Option<common_meta::key::flow::flow_info::FlowScheduleConfig>,
46}
47
48pub trait FlowEngine {
49    /// Create a flow using the provided arguments, return previous flow id if exists and is replaced
50    async fn create_flow(&self, args: CreateFlowArgs) -> Result<Option<FlowId>, Error>;
51    /// Remove a flow by its ID
52    async fn remove_flow(&self, flow_id: FlowId) -> Result<(), Error>;
53    /// Flush the flow, return the number of rows flushed
54    async fn flush_flow(&self, flow_id: FlowId) -> Result<usize, Error>;
55    /// Check if the flow exists
56    async fn flow_exist(&self, flow_id: FlowId) -> Result<bool, Error>;
57    /// List all flows
58    async fn list_flows(&self) -> Result<impl IntoIterator<Item = FlowId>, Error>;
59    /// Handle the insert requests for the flow
60    async fn handle_flow_inserts(
61        &self,
62        request: api::v1::region::InsertRequests,
63    ) -> Result<(), Error>;
64
65    async fn handle_mark_window_dirty(
66        &self,
67        req: api::v1::flow::DirtyWindowRequests,
68    ) -> Result<(), Error>;
69}
70
71/// Provides flow runtime statistics for information schema and heartbeat reporting.
72pub trait FlowStatProvider {
73    /// Returns current runtime stats of an engine.
74    async fn flow_stat(&self) -> FlowStat;
75}