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 session::context::QueryContext;
20use table::metadata::TableId;
21
22use crate::Error;
23// TODO(discord9): refactor common types for flow to a separate module
24/// FlowId is a unique identifier for a flow task
25pub type FlowId = u64;
26pub type TableName = [String; 3];
27
28#[derive(Clone)]
29pub struct FlowAuthHeader {
30    auth_schema: api::v1::auth_header::AuthScheme,
31}
32
33impl std::fmt::Debug for FlowAuthHeader {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self.auth() {
36            api::v1::auth_header::AuthScheme::Basic(basic) => f
37                .debug_struct("Basic")
38                .field("username", &basic.username)
39                .field("password", &"<RETRACTED>")
40                .finish(),
41            api::v1::auth_header::AuthScheme::Token(_) => f
42                .debug_struct("Token")
43                .field("token", &"<RETRACTED>")
44                .finish(),
45        }
46    }
47}
48
49impl FlowAuthHeader {
50    pub fn from_user_pwd(username: &str, pwd: &str) -> Self {
51        Self {
52            auth_schema: api::v1::auth_header::AuthScheme::Basic(api::v1::Basic {
53                username: username.to_string(),
54                password: pwd.to_string(),
55            }),
56        }
57    }
58
59    pub fn auth(&self) -> &api::v1::auth_header::AuthScheme {
60        &self.auth_schema
61    }
62}
63
64/// The arguments to create a flow
65#[derive(Debug, Clone)]
66pub struct CreateFlowArgs {
67    pub flow_id: FlowId,
68    pub sink_table_name: TableName,
69    pub source_table_ids: Vec<TableId>,
70    pub create_if_not_exists: bool,
71    pub or_replace: bool,
72    pub expire_after: Option<i64>,
73    pub comment: Option<String>,
74    pub sql: String,
75    pub flow_options: HashMap<String, String>,
76    pub query_ctx: Option<QueryContext>,
77}
78
79pub trait FlowEngine {
80    /// Create a flow using the provided arguments, return previous flow id if exists and is replaced
81    async fn create_flow(&self, args: CreateFlowArgs) -> Result<Option<FlowId>, Error>;
82    /// Remove a flow by its ID
83    async fn remove_flow(&self, flow_id: FlowId) -> Result<(), Error>;
84    /// Flush the flow, return the number of rows flushed
85    async fn flush_flow(&self, flow_id: FlowId) -> Result<usize, Error>;
86    /// Check if the flow exists
87    async fn flow_exist(&self, flow_id: FlowId) -> Result<bool, Error>;
88    /// List all flows
89    async fn list_flows(&self) -> Result<impl IntoIterator<Item = FlowId>, Error>;
90    /// Handle the insert requests for the flow
91    async fn handle_flow_inserts(
92        &self,
93        request: api::v1::region::InsertRequests,
94    ) -> Result<(), Error>;
95}