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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! Result sink thread for parallel processing
//!
//! Handles output ordering and merges global state from workers.
use anyhow::{anyhow, Result};
use crossbeam_channel::Receiver;
use std::collections::HashMap;
use std::sync::atomic::Ordering;
use crate::formatters::GapTracker;
use crate::platform::{Ctrl, SHOULD_TERMINATE};
use crate::rhai_functions::file_ops;
use super::tracker::GlobalTracker;
use super::types::{BatchResult, ProcessedEvent};
/// Write CSV header if the output format requires it
pub(crate) fn write_csv_header_if_needed<W: std::io::Write>(
output: &mut W,
config: &crate::config::KeloraConfig,
) -> Result<()> {
// Only write headers for CSV formats that normally include headers
match config.output.format {
crate::config::OutputFormat::Csv | crate::config::OutputFormat::Tsv => {
// Create a temporary formatter to generate the header
let keys = config.output.get_effective_keys();
if keys.is_empty() {
return Err(anyhow::anyhow!(
"CSV output requires --keys to define column order, e.g. --keys ts,level,msg. Use -s to inspect available fields."
));
}
let formatter = match config.output.format {
crate::config::OutputFormat::Csv => crate::formatters::CsvFormatter::new(keys),
crate::config::OutputFormat::Tsv => crate::formatters::CsvFormatter::new_tsv(keys),
_ => unreachable!(),
};
// Generate and write the header
let header = formatter.format_header();
writeln!(output, "{}", header)?;
}
_ => {
// Non-CSV formats don't need headers
}
}
Ok(())
}
/// Pipeline result sink thread: handles output ordering and merges global state
/// Results are already formatted by the pipeline, so we just need to output them
pub(crate) fn pipeline_result_sink_thread<W: std::io::Write>(
result_receiver: Receiver<BatchResult>,
preserve_order: bool,
global_tracker: GlobalTracker,
output: &mut W,
config: &crate::config::KeloraConfig,
take_limit: Option<usize>,
ctrl_rx: Receiver<Ctrl>,
) -> Result<()> {
// Write CSV header if needed (before any worker results)
write_csv_header_if_needed(output, config)?;
let gap_marker_use_colors = crate::tty::should_use_colors_with_mode(&config.output.color);
let mut gap_tracker = if config.processing.quiet_events {
// Suppress gap markers when output is suppressed (stats-only, high quiet levels)
None
} else {
config
.output
.mark_gaps
.map(|threshold| GapTracker::new(threshold, gap_marker_use_colors))
};
if preserve_order {
pipeline_ordered_result_sink(
result_receiver,
global_tracker,
output,
take_limit,
&mut gap_tracker,
ctrl_rx,
config,
)
} else {
pipeline_unordered_result_sink(
result_receiver,
global_tracker,
output,
take_limit,
&mut gap_tracker,
ctrl_rx,
config,
)
}
}
/// Ordered result sink - maintains batch order for deterministic output
fn pipeline_ordered_result_sink<W: std::io::Write>(
result_receiver: Receiver<BatchResult>,
global_tracker: GlobalTracker,
output: &mut W,
take_limit: Option<usize>,
gap_tracker: &mut Option<GapTracker>,
_ctrl_rx: Receiver<Ctrl>,
_config: &crate::config::KeloraConfig,
) -> Result<()> {
let mut pending_batches: HashMap<u64, BatchResult> = HashMap::new();
let mut next_expected_id = 0u64;
let mut events_output = 0usize;
let mut termination_detected = false;
while let Ok(mut batch_result) = result_receiver.recv() {
// Check for termination signal, but don't break immediately
// Continue processing to collect final stats from workers
if SHOULD_TERMINATE.load(Ordering::Relaxed) {
termination_detected = true;
}
let batch_id = batch_result.batch_id;
let user_tracked_updates = std::mem::take(&mut batch_result.user_tracked_updates);
let internal_tracked_updates = std::mem::take(&mut batch_result.internal_tracked_updates);
// Merge global state and stats
global_tracker.merge_worker_state(user_tracked_updates, internal_tracked_updates)?;
global_tracker.merge_worker_stats(&batch_result.worker_stats)?;
// Handle special batches
if batch_id == u64::MAX {
// This is a final stats batch from a terminated worker
// If we're terminating, we might want to exit soon after collecting these
if termination_detected {
// Continue processing a bit more to collect other final stats
continue;
}
continue;
} else if batch_id == u64::MAX - 1 {
// This is a flush batch from a worker - process it immediately
if !termination_detected {
let remaining_limit = take_limit.map(|limit| limit.saturating_sub(events_output));
let events_this_batch = pipeline_output_batch_results(
output,
&batch_result.results,
remaining_limit,
gap_tracker,
)?;
events_output += events_this_batch;
// Check if we've reached the take limit
if let Some(limit) = take_limit {
if events_output >= limit {
// Set termination signal to stop further processing
SHOULD_TERMINATE.store(true, Ordering::Relaxed);
break;
}
}
}
continue;
}
// If terminating, skip output processing but continue stats collection
if termination_detected {
continue;
}
// Store batch for ordering
pending_batches.insert(batch_id, batch_result);
// Output all consecutive batches starting from next_expected_id
while let Some(batch) = pending_batches.remove(&next_expected_id) {
let remaining_limit = take_limit.map(|limit| limit.saturating_sub(events_output));
let events_this_batch = pipeline_output_batch_results(
output,
&batch.results,
remaining_limit,
gap_tracker,
)?;
events_output += events_this_batch;
next_expected_id += 1;
// Check if we've reached the take limit
if let Some(limit) = take_limit {
if events_output >= limit {
// Set termination signal to stop further processing
SHOULD_TERMINATE.store(true, Ordering::Relaxed);
break;
}
}
}
}
// Output any remaining batches (shouldn't happen with proper shutdown)
for (_, batch) in pending_batches {
let remaining_limit = take_limit.map(|limit| limit.saturating_sub(events_output));
events_output +=
pipeline_output_batch_results(output, &batch.results, remaining_limit, gap_tracker)?;
// Check if we've reached the take limit even in cleanup
if let Some(limit) = take_limit {
if events_output >= limit {
break;
}
}
}
Ok(())
}
/// Unordered result sink - outputs batches as they arrive
fn pipeline_unordered_result_sink<W: std::io::Write>(
result_receiver: Receiver<BatchResult>,
global_tracker: GlobalTracker,
output: &mut W,
take_limit: Option<usize>,
gap_tracker: &mut Option<GapTracker>,
ctrl_rx: Receiver<Ctrl>,
config: &crate::config::KeloraConfig,
) -> Result<()> {
let mut termination_detected = false;
let mut events_output = 0usize;
loop {
// Check for control messages first (non-blocking)
match ctrl_rx.try_recv() {
Ok(Ctrl::Shutdown { .. }) => {
// Handle shutdown in termination detection below
}
Ok(Ctrl::PrintStats) => {
// Print current parallel stats from coordinator
let mut current_stats = global_tracker.get_final_stats();
// Extract discovered keys/levels from current internal tracking
let internal_tracking = global_tracker.lock_internal_tracked().clone();
current_stats.extract_discovered_from_tracking(&internal_tracking);
let stats_message = config.format_stats_message(
¤t_stats.format_stats_for_signal(config.input.multiline.is_some(), false),
true, // Always show header for signal handler
);
let _ = crate::platform::SafeStderr::new().writeln(&stats_message);
}
Err(_) => {
// No control message or channel closed, continue
}
}
// Now handle result messages
let mut batch_result = match result_receiver.recv() {
Ok(result) => result,
Err(_) => break, // Channel closed
};
// Check for termination signal, but don't break immediately
// Continue processing to collect final stats from workers
if SHOULD_TERMINATE.load(Ordering::Relaxed) {
termination_detected = true;
}
// Merge global state and stats
let user_updates = std::mem::take(&mut batch_result.user_tracked_updates);
let internal_updates = std::mem::take(&mut batch_result.internal_tracked_updates);
global_tracker.merge_worker_state(user_updates, internal_updates)?;
global_tracker.merge_worker_stats(&batch_result.worker_stats)?;
// Handle special batches
if batch_result.batch_id == u64::MAX {
// This is a final stats batch from a terminated worker
if termination_detected {
// Continue processing a bit more to collect other final stats
continue;
}
continue;
} else if batch_result.batch_id == u64::MAX - 1 {
// This is a flush batch from a worker - process it immediately
if !termination_detected {
let remaining_limit = take_limit.map(|limit| limit.saturating_sub(events_output));
let events_this_batch = pipeline_output_batch_results(
output,
&batch_result.results,
remaining_limit,
gap_tracker,
)?;
events_output += events_this_batch;
// Check if we've reached the take limit
if let Some(limit) = take_limit {
if events_output >= limit {
// Set termination signal to stop further processing
SHOULD_TERMINATE.store(true, Ordering::Relaxed);
break;
}
}
}
continue;
}
// If terminating, skip output processing but continue stats collection
if termination_detected {
continue;
}
// Output immediately
let remaining_limit = take_limit.map(|limit| limit.saturating_sub(events_output));
let events_this_batch = pipeline_output_batch_results(
output,
&batch_result.results,
remaining_limit,
gap_tracker,
)?;
events_output += events_this_batch;
// Check if we've reached the take limit
if let Some(limit) = take_limit {
if events_output >= limit {
// Set termination signal to stop further processing
SHOULD_TERMINATE.store(true, Ordering::Relaxed);
break;
}
}
}
Ok(())
}
/// Output batch results to the writer
fn pipeline_output_batch_results<W: std::io::Write>(
output: &mut W,
results: &[ProcessedEvent],
remaining_limit: Option<usize>,
gap_tracker: &mut Option<GapTracker>,
) -> Result<usize> {
let mut events_output = 0usize;
for processed in results {
if let Err(err) = file_ops::execute_ops(&processed.file_ops) {
return Err(anyhow!(err));
}
// Check if we've reached the limit
if let Some(limit) = remaining_limit {
if events_output >= limit {
break;
}
}
// Output captured messages in order, preserving stdout/stderr streams
if !processed.captured_messages.is_empty() {
// Use the new ordered message system
for message in &processed.captured_messages {
match message {
crate::rhai_functions::strings::CapturedMessage::Stdout(msg) => {
println!("{}", msg);
}
crate::rhai_functions::strings::CapturedMessage::Stderr(msg) => {
eprintln!("{}", msg);
}
}
}
} else {
// Fallback to old system for compatibility
// First output any captured prints for this specific event (to stdout, not file)
for print_msg in &processed.captured_prints {
println!("{}", print_msg);
}
// Output any captured eprints for this specific event (to stderr)
for eprint_msg in &processed.captured_eprints {
eprintln!("{}", eprint_msg);
}
}
// Then output the event itself to the designated output, skip empty strings
if !processed.event.original_line.is_empty() {
let marker = match gap_tracker.as_mut() {
Some(tracker) => tracker.check(processed.timestamp),
None => None,
};
if let Some(marker_line) = marker {
writeln!(output, "{}", marker_line).unwrap_or(());
}
writeln!(output, "{}", &processed.event.original_line).unwrap_or(());
events_output += 1;
}
}
output.flush().unwrap_or(());
Ok(events_output)
}