1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// SPDX-License-Identifier: BUSL-1.1
//! Aggregate mode: GROUP BY / time-bucket across memtable + partitions.
//!
//! Uses the grouped_scan engine with SIMD bitmask filters, direct-index Vec
//! for low-cardinality symbol GROUP BY, parallel partition processing via
//! std::thread::scope, sparse index block-level skip, and single metadata
//! read per partition.
use crate::bridge::envelope::Response;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::task::ExecutionTask;
use crate::engine::timeseries::grouped_scan::{
GroupedAggResult, PartitionAggParams, aggregate_memtable, aggregate_partition,
};
impl CoreLoop {
/// Aggregate mode: GROUP BY / time-bucket across memtable + partitions.
#[allow(clippy::too_many_arguments)]
pub(in crate::data::executor) fn execute_ts_aggregate(
&mut self,
task: &ExecutionTask,
tid: crate::types::TenantId,
collection: &str,
time_range: (i64, i64),
limit: usize,
filter_predicates: &[crate::bridge::scan_filter::ScanFilter],
bucket_interval_ms: i64,
group_by: &[String],
aggregates: &[(String, String)],
gap_fill: &str,
needed_columns: &[String],
) -> Response {
let key = (tid, collection.to_string());
let num_aggs = aggregates.len();
// Phase 1: Aggregate memtable (on TPC core).
let mut merged = if let Some(mt) = self.columnar_memtables.get(&key)
&& !mt.is_empty()
{
aggregate_memtable(
mt,
group_by,
aggregates,
filter_predicates,
time_range,
bucket_interval_ms,
)
.unwrap_or_else(|| GroupedAggResult::new(num_aggs))
} else {
GroupedAggResult::new(num_aggs)
};
// Phase 2: Aggregate disk partitions (parallel).
if let Some(registry) = self.ts_registries.get(&key) {
let query_range = nodedb_types::timeseries::TimeRange::new(time_range.0, time_range.1);
let entries: Vec<_> = registry.query_partitions(&query_range);
if !entries.is_empty() {
let data_dir = &self.data_dir;
let partition_dirs: Vec<std::path::PathBuf> = entries
.iter()
.map(|e| data_dir.join("ts").join(collection).join(&e.dir_name))
.filter(|p| p.exists())
.collect();
if partition_dirs.len() <= 1 {
// Single partition — use io_uring batched reads on TPC core.
let io_priority = Some(task.request.priority);
let io_metrics: &crate::data::io::IoMetrics = &self.io_metrics;
for dir in &partition_dirs {
if let Some(part_result) = aggregate_partition(PartitionAggParams {
partition_dir: dir,
group_by,
aggregates,
filters: filter_predicates,
time_range,
needed_columns,
bucket_interval_ms,
uring_reader: self.uring_reader.as_mut(),
io_priority,
io_metrics: Some(io_metrics),
}) {
merged.merge(&part_result);
}
}
} else {
// Multiple partitions — parallel via std::thread::scope.
let group_by_owned: Vec<String> = group_by.to_vec();
let agg_owned: Vec<(String, String)> = aggregates.to_vec();
let filters_owned: Vec<crate::bridge::scan_filter::ScanFilter> =
filter_predicates.to_vec();
let needed_owned: Vec<String> = needed_columns.to_vec();
let available = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
let thread_count = available.min(partition_dirs.len()).min(8);
let chunk_size = partition_dirs.len().div_ceil(thread_count);
let partition_results: Vec<GroupedAggResult> = std::thread::scope(|s| {
let handles: Vec<_> = partition_dirs
.chunks(chunk_size)
.map(|chunk| {
let gb = &group_by_owned;
let ag = &agg_owned;
let fl = &filters_owned;
let nc = &needed_owned;
s.spawn(move || {
let mut local = GroupedAggResult::new(ag.len());
for dir in chunk {
// Parallel threads: no io_uring (fadvise fallback).
if let Some(r) = aggregate_partition(PartitionAggParams {
partition_dir: dir,
group_by: gb,
aggregates: ag,
filters: fl,
time_range,
needed_columns: nc,
bucket_interval_ms,
uring_reader: None,
io_priority: None,
io_metrics: None,
}) {
local.merge(&r);
}
}
local
})
})
.collect();
handles.into_iter().filter_map(|h| h.join().ok()).collect()
});
for r in &partition_results {
merged.merge(r);
}
}
}
}
// Phase 3: Apply gap-fill if requested (bucketed results only).
let merged = if !gap_fill.is_empty() && bucket_interval_ms > 0 {
super::super::timeseries_gap_fill::apply_gap_fill_to_grouped(
merged,
time_range,
bucket_interval_ms,
gap_fill,
group_by,
aggregates,
)
} else {
merged
};
// Phase 4: Encode response (MessagePack, no serde_json intermediate).
let payload = super::encode::encode_grouped_results(
&merged,
group_by,
aggregates,
limit,
bucket_interval_ms,
);
self.response_with_payload(task, payload)
}
}