frontend/instance/
logical_batcher.rs1use std::sync::{Arc, Weak};
16
17use api::v1::ColumnSchema;
18use async_trait::async_trait;
19use servers::batcher::BatchingProtocol;
20use servers::batcher::logical_table::{LogicalTablePendingRowsBatcher, PendingRowsSchemaAlterer};
21use servers::error::{BatcherChannelClosedSnafu, Result};
22use session::context::QueryContextRef;
23use snafu::OptionExt;
24
25use crate::frontend::FrontendOptions;
26use crate::instance::Instance;
27
28impl Instance {
29 pub fn init_logical_batcher(self: &Arc<Self>, options: &FrontendOptions) {
32 let options_batcher = options.logical_batcher_options();
33 let enabled = options_batcher
34 .protocols
35 .iter()
36 .any(|protocol| match protocol {
37 BatchingProtocol::Prom => options.prom_store.enable || options.postgres.enable,
38 BatchingProtocol::Otlp => options.otlp.enable,
39 _ => false,
40 });
41 if !options.prom_store.with_metric_engine
42 || !enabled
43 || !options_batcher.pending_rows_batching_enabled()
44 {
45 return;
46 }
47 self.logical_batcher.get_or_init(|| {
48 LogicalTablePendingRowsBatcher::try_new(
49 self.partition_manager().clone(),
50 self.node_manager().clone(),
51 self.inserter.write_dispatcher().clone(),
52 self.catalog_manager().clone(),
53 self.table_flownode_set_cache().clone(),
54 true,
55 Arc::new(LogicalTables(Arc::downgrade(self))),
56 options.auto_create_physical_table_timestamp_unit,
57 options_batcher.pending_rows_flush_interval,
58 options_batcher.max_batch_rows,
59 options_batcher.max_concurrent_flushes,
60 options_batcher.worker_channel_capacity,
61 options_batcher.max_inflight_requests,
62 options_batcher.flow_notification_queue_capacity,
63 )
64 });
65 }
66
67 pub(crate) fn logical_batcher(&self) -> Option<&Arc<LogicalTablePendingRowsBatcher>> {
68 self.logical_batcher.get().and_then(Option::as_ref)
69 }
70}
71
72struct LogicalTables(Weak<Instance>);
75
76#[async_trait]
77impl PendingRowsSchemaAlterer for LogicalTables {
78 async fn create_tables_if_missing_batch(
79 &self,
80 catalog: &str,
81 schema: &str,
82 tables: &[(&str, &[ColumnSchema])],
83 with_metric_engine: bool,
84 ctx: QueryContextRef,
85 ) -> Result<()> {
86 self.0
87 .upgrade()
88 .context(BatcherChannelClosedSnafu)?
89 .create_tables_if_missing_batch(catalog, schema, tables, with_metric_engine, ctx)
90 .await
91 }
92
93 async fn add_missing_prom_tag_columns_batch(
94 &self,
95 catalog: &str,
96 schema: &str,
97 tables: &[(&str, &[String])],
98 ctx: QueryContextRef,
99 ) -> Result<()> {
100 self.0
101 .upgrade()
102 .context(BatcherChannelClosedSnafu)?
103 .add_missing_prom_tag_columns_batch(catalog, schema, tables, ctx)
104 .await
105 }
106}