operator/batcher.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 arrow::record_batch::RecordBatch;
18use async_trait::async_trait;
19use common_query::Output;
20use session::context::QueryContextRef;
21use table::metadata::TableInfoRef;
22
23use crate::error::Result;
24
25/// Keeps a request's admission slot and conversion memory alive across its submissions.
26pub type PendingRowsPermit = Arc<dyn Send + Sync>;
27
28/// Reserves conversion memory without waiting so callers holding body permits never deadlock.
29pub trait ConversionMemoryLimiter: Send + Sync {
30 /// Returns the conversion memory limit, or zero when byte admission is disabled.
31 fn limit_bytes(&self) -> u64;
32
33 /// Checks and acquires additional conversion memory non-blockingly.
34 fn reserve(&self, estimated_bytes: usize, held_bytes: u64) -> Result<PendingRowsPermit>;
35}
36
37/// Accepts prepared table writes without coupling the inserter to a batcher implementation.
38/// Schema creation, alteration and default evaluation remain the caller's responsibility.
39#[async_trait]
40pub trait PendingRowsBatcher: Send + Sync {
41 /// Returns the conversion memory limit, or zero when byte admission is disabled.
42 fn memory_limit_bytes(&self) -> u64 {
43 0
44 }
45
46 /// Admits an original request before conversion, retaining its slot and estimated
47 /// Arrow memory across all table submissions. Memory acquisition must not wait
48 /// because the caller can already hold transport and decoded-row reservations.
49 /// `held_bytes` counts those reservations, which cannot be released during conversion.
50 async fn acquire(&self, estimated_bytes: usize, held_bytes: u64) -> Result<PendingRowsPermit>;
51
52 /// Submits rows according to the acknowledgement policy, retaining the slot until
53 /// writing completes even when the response acknowledges queue admission only.
54 /// Cancelling the response wait does not retract an already enqueued submission.
55 /// Early acknowledgements retain actual storage results in the output metadata.
56 async fn submit(
57 &self,
58 table_info: TableInfoRef,
59 batch: RecordBatch,
60 ctx: QueryContextRef,
61 permit: PendingRowsPermit,
62 ) -> Result<Output>;
63}