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(crate) fn init_logical_batcher(self: &Arc<Self>, options: &FrontendOptions) {
30 self.logical_batcher.get_or_init(|| {
31 let options_batcher = options.logical_batcher_options();
32 let enabled = options_batcher
33 .protocols
34 .iter()
35 .any(|protocol| match protocol {
36 BatchingProtocol::Prom => options.prom_store.enable,
37 BatchingProtocol::Otlp => options.otlp.enable,
38 _ => false,
39 });
40 if !options.prom_store.with_metric_engine
41 || !enabled
42 || !options_batcher.pending_rows_batching_enabled()
43 {
44 return None;
45 }
46 LogicalTablePendingRowsBatcher::try_new(
47 self.partition_manager().clone(),
48 self.node_manager().clone(),
49 self.catalog_manager().clone(),
50 self.table_flownode_set_cache().clone(),
51 true,
52 Arc::new(LogicalTables(Arc::downgrade(self))),
53 options_batcher.pending_rows_flush_interval,
54 options_batcher.max_batch_rows,
55 options_batcher.max_concurrent_flushes,
56 options_batcher.worker_channel_capacity,
57 options_batcher.max_inflight_requests,
58 options_batcher.flow_notification_queue_capacity,
59 )
60 });
61 }
62
63 pub(crate) fn logical_batcher(&self) -> Option<&Arc<LogicalTablePendingRowsBatcher>> {
64 self.logical_batcher.get().and_then(Option::as_ref)
65 }
66}
67
68struct LogicalTables(Weak<Instance>);
71
72#[async_trait]
73impl PendingRowsSchemaAlterer for LogicalTables {
74 async fn create_tables_if_missing_batch(
75 &self,
76 catalog: &str,
77 schema: &str,
78 tables: &[(&str, &[ColumnSchema])],
79 with_metric_engine: bool,
80 ctx: QueryContextRef,
81 ) -> Result<()> {
82 self.0
83 .upgrade()
84 .context(BatcherChannelClosedSnafu)?
85 .create_tables_if_missing_batch(catalog, schema, tables, with_metric_engine, ctx)
86 .await
87 }
88
89 async fn add_missing_prom_tag_columns_batch(
90 &self,
91 catalog: &str,
92 schema: &str,
93 tables: &[(&str, &[String])],
94 ctx: QueryContextRef,
95 ) -> Result<()> {
96 self.0
97 .upgrade()
98 .context(BatcherChannelClosedSnafu)?
99 .add_missing_prom_tag_columns_batch(catalog, schema, tables, ctx)
100 .await
101 }
102}