Skip to main content

flow/batching_mode/
batching_execution.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//! Optional execution collaborator for batching tasks.
16
17use std::sync::Arc;
18
19use datafusion_expr::LogicalPlan;
20use query::QueryEngineRef;
21use table::TableRef;
22
23use crate::Result;
24use crate::batching_mode::frontend_client::FrontendClient;
25use crate::batching_mode::task::{BatchingExecutionGuard, BatchingTask, ExecuteOnceOutcome};
26
27#[async_trait::async_trait]
28pub trait BatchingExecution: Send + Sync + 'static {
29    /// Execute one round while retaining the guard through all task-state updates.
30    /// An implementation that continues after caller cancellation must retain the
31    /// guard with that work and make it stoppable through [`Self::stop`].
32    async fn execute_once(
33        self: Arc<Self>,
34        guard: BatchingExecutionGuard,
35        task: &BatchingTask,
36        engine: &QueryEngineRef,
37        frontend: &Arc<FrontendClient>,
38        max_window_cnt: Option<usize>,
39    ) -> ExecuteOnceOutcome;
40
41    /// Rewrite the completed query plan after incremental merging and before execution.
42    fn rewrite_plan(&self, _task: &BatchingTask, plan: LogicalPlan) -> Result<LogicalPlan> {
43        Ok(plan)
44    }
45
46    /// Retire this execution instance, rejecting new work and requesting that any
47    /// retained local work stop. This is not an acknowledgement of remote quiescence.
48    fn stop(&self) {}
49}
50
51#[async_trait::async_trait]
52pub trait BatchingExecutionFactory: Send + Sync + 'static {
53    async fn create(
54        &self,
55        task: &BatchingTask,
56        sink: TableRef,
57        engine: &QueryEngineRef,
58        frontend: &Arc<FrontendClient>,
59    ) -> Result<Option<Arc<dyn BatchingExecution>>>;
60}