diff --git a/config/config.md b/config/config.md
index 74978423121..b038c0960d4 100644
--- a/config/config.md
+++ b/config/config.md
@@ -82,6 +82,7 @@
| `prom_store.max_concurrent_flushes` | Integer | `256` | Max number of concurrent batch flushes. |
| `prom_store.worker_channel_capacity` | Integer | `65526` | Capacity of the pending batch worker channel. |
| `prom_store.max_inflight_requests` | Integer | `3000` | Max inflight write requests before backpressure. |
+| `prom_store.flow_notification_queue_capacity` | Integer | `1024` | Maximum number of logical-table flow notifications waiting in the shared queue. |
| `wal` | -- | -- | The WAL options. |
| `wal.provider` | String | `raft_engine` | The provider of the WAL.
- `raft_engine`: the wal is stored in the local file system by raft-engine.
- `kafka`: it's remote wal that data is stored in Kafka. |
| `wal.dir` | String | Unset | The directory to store the WAL files.
**It's only used when the provider is `raft_engine`**. |
@@ -314,6 +315,7 @@
| `prom_store.max_concurrent_flushes` | Integer | `256` | Max number of concurrent batch flushes. |
| `prom_store.worker_channel_capacity` | Integer | `65526` | Capacity of the pending batch worker channel. |
| `prom_store.max_inflight_requests` | Integer | `3000` | Max inflight write requests before backpressure. |
+| `prom_store.flow_notification_queue_capacity` | Integer | `1024` | Maximum number of logical-table flow notifications waiting in the shared queue. |
| `meta_client` | -- | -- | The metasrv client options. |
| `meta_client.metasrv_addrs` | Array | -- | The addresses of the metasrv. |
| `meta_client.timeout` | String | `3s` | Operation timeout. |
diff --git a/config/frontend.example.toml b/config/frontend.example.toml
index 3deadcf6a43..aa2bf97267e 100644
--- a/config/frontend.example.toml
+++ b/config/frontend.example.toml
@@ -251,6 +251,8 @@ with_metric_engine = true
#+worker_channel_capacity = 65526
## Max inflight write requests before backpressure.
#+max_inflight_requests = 3000
+## Maximum number of logical-table flow notifications waiting in the shared queue.
+#+flow_notification_queue_capacity = 1024
## The metasrv client options.
[meta_client]
diff --git a/config/standalone.example.toml b/config/standalone.example.toml
index 1d612a2ff6a..0a0ee02d072 100644
--- a/config/standalone.example.toml
+++ b/config/standalone.example.toml
@@ -218,6 +218,8 @@ with_metric_engine = true
#+worker_channel_capacity = 65526
## Max inflight write requests before backpressure.
#+max_inflight_requests = 3000
+## Maximum number of logical-table flow notifications waiting in the shared queue.
+#+flow_notification_queue_capacity = 1024
## The WAL options.
[wal]
diff --git a/src/frontend/src/server.rs b/src/frontend/src/server.rs
index 34d5e78ec56..835be4fd600 100644
--- a/src/frontend/src/server.rs
+++ b/src/frontend/src/server.rs
@@ -138,6 +138,7 @@ where
opts.prom_store.max_concurrent_flushes,
opts.prom_store.worker_channel_capacity,
opts.prom_store.max_inflight_requests,
+ opts.prom_store.flow_notification_queue_capacity,
)
} else {
None
diff --git a/src/frontend/src/service_config/prom_store.rs b/src/frontend/src/service_config/prom_store.rs
index 99f1eada6d4..9751b9ee2c1 100644
--- a/src/frontend/src/service_config/prom_store.rs
+++ b/src/frontend/src/service_config/prom_store.rs
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+use std::num::NonZeroUsize;
use std::time::Duration;
use serde::{Deserialize, Serialize};
@@ -30,6 +31,9 @@ pub struct PromStoreOptions {
pub worker_channel_capacity: usize,
#[serde(default = "default_max_inflight_requests")]
pub max_inflight_requests: usize,
+ /// Maximum number of logical-table flow notifications waiting in the shared queue.
+ #[serde(default = "default_flow_notification_queue_capacity")]
+ pub flow_notification_queue_capacity: NonZeroUsize,
}
fn default_max_batch_rows() -> usize {
@@ -48,6 +52,10 @@ fn default_max_inflight_requests() -> usize {
3000
}
+fn default_flow_notification_queue_capacity() -> NonZeroUsize {
+ NonZeroUsize::new(1024).unwrap_or(NonZeroUsize::MIN)
+}
+
impl Default for PromStoreOptions {
fn default() -> Self {
Self {
@@ -58,6 +66,7 @@ impl Default for PromStoreOptions {
max_concurrent_flushes: default_max_concurrent_flushes(),
worker_channel_capacity: default_worker_channel_capacity(),
max_inflight_requests: default_max_inflight_requests(),
+ flow_notification_queue_capacity: default_flow_notification_queue_capacity(),
}
}
}
@@ -68,7 +77,8 @@ mod tests {
use super::PromStoreOptions;
use crate::service_config::prom_store::{
- default_max_batch_rows, default_max_concurrent_flushes, default_max_inflight_requests,
+ default_flow_notification_queue_capacity, default_max_batch_rows,
+ default_max_concurrent_flushes, default_max_inflight_requests,
default_worker_channel_capacity,
};
@@ -91,5 +101,9 @@ mod tests {
default.max_inflight_requests,
default_max_inflight_requests()
);
+ assert_eq!(
+ default.flow_notification_queue_capacity,
+ default_flow_notification_queue_capacity()
+ );
}
}
diff --git a/src/servers/src/metrics.rs b/src/servers/src/metrics.rs
index 838cc445594..41a16bfea23 100644
--- a/src/servers/src/metrics.rs
+++ b/src/servers/src/metrics.rs
@@ -177,6 +177,12 @@ lazy_static! {
"Total pending rows flush failures"
)
.unwrap();
+ pub static ref FLOW_NOTIFICATION_DROPPED: IntCounterVec = register_int_counter_vec!(
+ "greptime_prom_store_flow_notification_dropped_total",
+ "Total flow notifications dropped by the pending rows batcher",
+ &["reason"]
+ )
+ .unwrap();
pub static ref PENDING_ROWS_BATCH_INGEST_STAGE_ELAPSED: HistogramVec = register_histogram_vec!(
"greptime_prom_store_pending_rows_batch_ingest_stage_elapsed",
"Elapsed time of pending rows batch ingestion stages in seconds",
diff --git a/src/servers/src/pending_rows_batcher.rs b/src/servers/src/pending_rows_batcher.rs
index c509e486553..8acb6da66cd 100644
--- a/src/servers/src/pending_rows_batcher.rs
+++ b/src/servers/src/pending_rows_batcher.rs
@@ -13,6 +13,7 @@
// limitations under the License.
use std::collections::{HashMap, HashSet};
+use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::{Duration, Instant};
@@ -22,6 +23,7 @@ use api::v1::region::{
BulkInsertRequest, RegionRequest, RegionRequestHeader, bulk_insert_request, region_request,
};
use api::v1::{ArrowIpc, ColumnSchema, RowInsertRequests, Rows};
+#[cfg(test)]
use arrow::array::Array;
use arrow::compute::{concat_batches, filter_record_batch};
use arrow::datatypes::{DataType as ArrowDataType, Schema as ArrowSchema, TimeUnit};
@@ -51,9 +53,9 @@ use tokio::sync::{OwnedSemaphorePermit, Semaphore, broadcast, mpsc, oneshot};
use crate::error;
use crate::error::{Error, Result};
use crate::metrics::{
- FLUSH_DROPPED_ROWS, FLUSH_ELAPSED, FLUSH_FAILURES, FLUSH_ROWS, FLUSH_TOTAL, PENDING_BATCHES,
- PENDING_ROWS, PENDING_ROWS_BATCH_FLUSH_STAGE_ELAPSED, PENDING_ROWS_BATCH_INGEST_STAGE_ELAPSED,
- PENDING_WORKERS,
+ FLOW_NOTIFICATION_DROPPED, FLUSH_DROPPED_ROWS, FLUSH_ELAPSED, FLUSH_FAILURES, FLUSH_ROWS,
+ FLUSH_TOTAL, PENDING_BATCHES, PENDING_ROWS, PENDING_ROWS_BATCH_FLUSH_STAGE_ELAPSED,
+ PENDING_ROWS_BATCH_INGEST_STAGE_ELAPSED, PENDING_WORKERS,
};
use crate::prom_row_builder::{
build_prom_create_table_schema_from_proto, identify_missing_columns_from_proto,
@@ -335,7 +337,7 @@ pub struct PendingRowsBatcher {
partition_manager: PartitionRuleManagerRef,
node_manager: NodeManagerRef,
catalog_manager: CatalogManagerRef,
- table_flownode_set_cache: TableFlownodeSetCacheRef,
+ flow_notification_tx: mpsc::Sender,
flush_semaphore: Arc,
inflight_semaphore: Arc,
worker_channel_capacity: usize,
@@ -359,6 +361,7 @@ impl PendingRowsBatcher {
max_concurrent_flushes: usize,
worker_channel_capacity: usize,
max_inflight_requests: usize,
+ flow_notification_queue_capacity: NonZeroUsize,
) -> Option> {
// Disable the batcher if flush is disabled or configuration is invalid.
// Zero values for these knobs either cause panics (e.g., zero-capacity channels)
@@ -380,6 +383,13 @@ impl PendingRowsBatcher {
.unwrap_or(true);
let workers = Arc::new(DashMap::new());
PENDING_WORKERS.set(workers.len() as i64);
+ let (flow_notification_tx, flow_notification_rx) =
+ mpsc::channel(flow_notification_queue_capacity.get());
+ start_flow_notification_worker(
+ flow_notification_rx,
+ table_flownode_set_cache,
+ node_manager.clone(),
+ );
Some(Arc::new(Self {
workers,
@@ -388,7 +398,7 @@ impl PendingRowsBatcher {
partition_manager,
node_manager,
catalog_manager,
- table_flownode_set_cache,
+ flow_notification_tx,
prom_store_with_metric_engine,
schema_alterer,
flush_semaphore: Arc::new(Semaphore::new(max_concurrent_flushes)),
@@ -860,7 +870,7 @@ impl PendingRowsBatcher {
self.partition_manager.clone(),
self.node_manager.clone(),
self.catalog_manager.clone(),
- self.table_flownode_set_cache.clone(),
+ self.flow_notification_tx.clone(),
self.flush_interval,
worker_idle_timeout,
self.max_batch_rows,
@@ -917,7 +927,7 @@ fn start_worker(
partition_manager: PartitionRuleManagerRef,
node_manager: NodeManagerRef,
catalog_manager: CatalogManagerRef,
- table_flownode_set_cache: TableFlownodeSetCacheRef,
+ flow_notification_tx: mpsc::Sender,
flush_interval: Duration,
worker_idle_timeout: Duration,
max_batch_rows: usize,
@@ -959,7 +969,7 @@ fn start_worker(
partition_manager.clone(),
node_manager.clone(),
catalog_manager.clone(),
- table_flownode_set_cache.clone(),
+ flow_notification_tx.clone(),
flush_semaphore.clone(),
).await;
}
@@ -971,7 +981,7 @@ fn start_worker(
partition_manager.clone(),
node_manager.clone(),
catalog_manager.clone(),
- table_flownode_set_cache.clone(),
+ flow_notification_tx.clone(),
).await;
}
break;
@@ -1007,7 +1017,7 @@ fn start_worker(
partition_manager.clone(),
node_manager.clone(),
catalog_manager.clone(),
- table_flownode_set_cache.clone(),
+ flow_notification_tx.clone(),
flush_semaphore.clone(),
).await;
}
@@ -1019,7 +1029,7 @@ fn start_worker(
partition_manager.clone(),
node_manager.clone(),
catalog_manager.clone(),
- table_flownode_set_cache.clone(),
+ flow_notification_tx.clone(),
).await;
}
break;
@@ -1080,7 +1090,7 @@ async fn spawn_flush(
partition_manager: PartitionRuleManagerRef,
node_manager: NodeManagerRef,
catalog_manager: CatalogManagerRef,
- table_flownode_set_cache: TableFlownodeSetCacheRef,
+ flow_notification_tx: mpsc::Sender,
semaphore: Arc,
) {
match semaphore.acquire_owned().await {
@@ -1092,7 +1102,7 @@ async fn spawn_flush(
partition_manager,
node_manager,
catalog_manager,
- table_flownode_set_cache,
+ flow_notification_tx,
)
.await;
});
@@ -1104,7 +1114,7 @@ async fn spawn_flush(
partition_manager,
node_manager,
catalog_manager,
- table_flownode_set_cache,
+ flow_notification_tx,
)
.await;
}
@@ -1296,7 +1306,7 @@ async fn flush_batch_with_managers(
partition_manager: PartitionRuleManagerRef,
node_manager: NodeManagerRef,
catalog_manager: CatalogManagerRef,
- table_flownode_set_cache: TableFlownodeSetCacheRef,
+ flow_notification_tx: mpsc::Sender,
) {
let partition_provider = PartitionManagerPhysicalFlushAdapter { partition_manager };
let node_requester = NodeManagerPhysicalFlushAdapter {
@@ -1308,8 +1318,7 @@ async fn flush_batch_with_managers(
&partition_provider,
&node_requester,
&catalog_provider,
- node_manager,
- table_flownode_set_cache,
+ flow_notification_tx,
)
.await;
}
@@ -1319,8 +1328,7 @@ async fn flush_batch(
partition_manager: &(impl PhysicalFlushPartitionProvider + ?Sized),
node_manager: &(impl PhysicalFlushNodeRequester + ?Sized),
catalog_manager: &(impl PhysicalFlushCatalogProvider + ?Sized),
- flow_node_manager: NodeManagerRef,
- table_flownode_set_cache: TableFlownodeSetCacheRef,
+ flow_notification_tx: mpsc::Sender,
) {
let FlushBatch {
table_batches,
@@ -1363,12 +1371,8 @@ async fn flush_batch(
.with_label_values(&[db_string.as_str()])
.inc_by(affected_rows as u64);
- notify_flow_dirty_windows_after_flush(
- table_batches,
- table_flownode_set_cache,
- flow_node_manager,
- );
notify_waiters(waiters, Ok(()));
+ enqueue_flow_notifications(table_batches, &flow_notification_tx);
}
Err(err) => {
FLUSH_FAILURES.inc();
@@ -1406,11 +1410,15 @@ struct FlowNotification {
table_id: TableId,
timestamps: Vec,
time_ranges: Vec,
+ min_timestamp: i64,
+ max_timestamp: i64,
}
fn compact_flow_notification(table_batch: TableBatch) -> Option {
let mut timestamps = Vec::new();
let mut time_ranges = Vec::new();
+ let mut min_timestamp: Option = None;
+ let mut max_timestamp: Option = None;
for batch in table_batch.batches {
let timestamp_column = batch.batch.column(batch.timestamp_index);
@@ -1436,6 +1444,8 @@ fn compact_flow_notification(table_batch: TableBatch) -> Option Option,
- time_ranges: Vec,
- peers: HashSet,
+fn try_enqueue_flow_notification(
+ tx: &mpsc::Sender,
+ notification: FlowNotification,
+) -> bool {
+ match tx.try_send(notification) {
+ Ok(()) => true,
+ Err(mpsc::error::TrySendError::Full(notification)) => {
+ FLOW_NOTIFICATION_DROPPED.with_label_values(&["full"]).inc();
+ warn!(
+ "Dropping flow notification because queue is full, table_id: {}, queue_capacity: {}, min_timestamp: {}, max_timestamp: {}",
+ notification.table_id,
+ tx.max_capacity(),
+ notification.min_timestamp,
+ notification.max_timestamp
+ );
+ false
+ }
+ Err(mpsc::error::TrySendError::Closed(notification)) => {
+ FLOW_NOTIFICATION_DROPPED
+ .with_label_values(&["closed"])
+ .inc();
+ error!(
+ "Dropping flow notification because queue is closed, table_id: {}, queue_capacity: {}, min_timestamp: {}, max_timestamp: {}",
+ notification.table_id,
+ tx.max_capacity(),
+ notification.min_timestamp,
+ notification.max_timestamp
+ );
+ false
+ }
+ }
+}
+
+fn enqueue_flow_notifications(table_batches: Vec, tx: &mpsc::Sender) {
+ for table_batch in table_batches {
+ if let Some(notification) = compact_flow_notification(table_batch) {
+ try_enqueue_flow_notification(tx, notification);
+ }
+ }
+}
+
+async fn handle_flow_notification(
+ notification: FlowNotification,
+ table_flownode_set_cache: TableFlownodeSetCacheRef,
node_manager: NodeManagerRef,
) {
+ let table_id = notification.table_id;
+ let flownodes = match table_flownode_set_cache.get(table_id).await {
+ Ok(Some(flownodes)) => flownodes,
+ Ok(None) => return,
+ Err(e) => {
+ error!(e; "Failed to get flownodes for table id: {}", table_id);
+ return;
+ }
+ };
+ let peers = flownodes.values().cloned().collect::>();
+
for peer in peers {
if let Err(e) = node_manager
.flownode(&peer)
@@ -1474,8 +1536,8 @@ async fn notify_flow_dirty_windows_for_table(
.handle_mark_window_dirty(DirtyWindowRequests {
requests: vec![DirtyWindowRequest {
table_id,
- timestamps: timestamps.clone(),
- time_ranges: time_ranges.clone(),
+ timestamps: notification.timestamps.clone(),
+ time_ranges: notification.time_ranges.clone(),
}],
})
.await
@@ -1491,61 +1553,31 @@ async fn notify_flow_dirty_windows_for_table(
}
}
+fn start_flow_notification_worker(
+ notification_rx: mpsc::Receiver,
+ table_flownode_set_cache: TableFlownodeSetCacheRef,
+ node_manager: NodeManagerRef,
+) {
+ common_runtime::spawn_global(async move {
+ tokio_stream::wrappers::ReceiverStream::new(notification_rx)
+ .for_each_concurrent(MAX_CONCURRENT_FLOW_NOTIFICATIONS, |notification| {
+ let table_flownode_set_cache = table_flownode_set_cache.clone();
+ let node_manager = node_manager.clone();
+ handle_flow_notification(notification, table_flownode_set_cache, node_manager)
+ })
+ .await;
+ });
+}
+
+#[cfg(test)]
fn notify_flow_dirty_windows_after_flush(
table_batches: Vec,
table_flownode_set_cache: TableFlownodeSetCacheRef,
node_manager: NodeManagerRef,
) {
- common_runtime::spawn_global(async move {
- let (notification_tx, notification_rx) = mpsc::channel(table_batches.len().max(1));
- let resolve_flownodes = async move {
- futures::stream::iter(table_batches)
- .for_each_concurrent(MAX_CONCURRENT_FLOW_NOTIFICATIONS, |table_batch| {
- let table_flownode_set_cache = table_flownode_set_cache.clone();
- let notification_tx = notification_tx.clone();
- async move {
- let Some(notification) = compact_flow_notification(table_batch) else {
- return;
- };
-
- let table_id = notification.table_id;
- let flownodes = match table_flownode_set_cache.get(table_id).await {
- Ok(Some(flownodes)) => flownodes,
- Ok(None) => return,
- Err(e) => {
- error!(e; "Failed to get flownodes for table id: {}", table_id);
- return;
- }
- };
- let peers = flownodes.values().cloned().collect::>();
- let _ = notification_tx
- .send((
- table_id,
- notification.timestamps,
- notification.time_ranges,
- peers,
- ))
- .await;
- }
- })
- .await;
- };
- let notify_flownodes = tokio_stream::wrappers::ReceiverStream::new(notification_rx)
- .for_each_concurrent(
- MAX_CONCURRENT_FLOW_NOTIFICATIONS,
- |(table_id, timestamps, time_ranges, peers)| {
- notify_flow_dirty_windows_for_table(
- table_id,
- timestamps,
- time_ranges,
- peers,
- node_manager.clone(),
- )
- },
- );
-
- futures::future::join(resolve_flownodes, notify_flownodes).await;
- });
+ let (tx, rx) = mpsc::channel(table_batches.len().max(1));
+ start_flow_notification_worker(rx, table_flownode_set_cache, node_manager);
+ enqueue_flow_notifications(table_batches, &tx);
}
/// Flushes a batch of logical table rows by transforming them into the physical table format
@@ -1938,10 +1970,12 @@ mod tests {
extract_timestamps, flush_batch, flush_batch_physical, flush_region_writes_concurrently,
greptime_timestamp, notify_flow_dirty_windows_after_flush, plan_region_batches,
remove_worker_if_same_channel, should_close_worker_on_idle_timeout,
- should_dispatch_concurrently, strip_partition_columns_from_batch,
- transform_logical_batches_to_physical,
+ should_dispatch_concurrently, start_flow_notification_worker,
+ strip_partition_columns_from_batch, transform_logical_batches_to_physical,
+ try_enqueue_flow_notification,
};
use crate::error;
+ use crate::metrics::FLOW_NOTIFICATION_DROPPED;
use crate::prom_row_builder::rows_to_aligned_record_batch;
fn mock_rows(row_count: usize, schema_name: &str) -> Rows {
@@ -2196,6 +2230,28 @@ mod tests {
}
}
+ #[test]
+ fn test_flow_notification_queue_drops_when_full() {
+ let (tx, mut rx) = mpsc::channel(1);
+ let notification = |table_id| {
+ compact_flow_notification(TableBatch {
+ table_name: format!("table-{table_id}"),
+ table_id,
+ row_count: 1,
+ batches: vec![mock_timestamp_batch(vec![Some(table_id as i64)])],
+ })
+ .unwrap()
+ };
+ let dropped = FLOW_NOTIFICATION_DROPPED.with_label_values(&["full"]);
+ let dropped_before = dropped.get();
+
+ assert!(try_enqueue_flow_notification(&tx, notification(1)));
+ assert!(!try_enqueue_flow_notification(&tx, notification(2)));
+
+ assert_eq!(1, rx.try_recv().unwrap().table_id);
+ assert_eq!(dropped_before + 1, dropped.get());
+ }
+
fn mock_physical_table_metadata(table_id: TableId) -> PhysicalTableMetadata {
let schema = Arc::new(
DtSchema::try_new(vec![
@@ -2586,6 +2642,15 @@ mod tests {
cache
}
+ fn mock_flow_notification_sender(
+ cache: TableFlownodeSetCacheRef,
+ node_manager: NodeManagerRef,
+ ) -> mpsc::Sender {
+ let (tx, rx) = mpsc::channel(16);
+ start_flow_notification_worker(rx, cache, node_manager);
+ tx
+ }
+
struct BlockingRangeKvBackend {
range_started: Mutex