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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Debug file writing
//!
//! This module handles writing Arrow and Protobuf debug files for inspection.
use crate::error::ZerobusError;
use crate::utils::file_rotation::rotate_file_if_needed;
use arrow::record_batch::RecordBatch;
use prost::Message;
use prost_types::DescriptorProto;
use std::io::Write;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tracing::{debug, info};
/// Debug file writer
///
/// Handles writing Arrow RecordBatch and Protobuf files to disk for debugging.
pub struct DebugWriter {
/// Output directory for debug files
#[allow(dead_code)]
output_dir: PathBuf,
/// Arrow IPC file writer
arrow_writer: Arc<tokio::sync::Mutex<Option<arrow::ipc::writer::FileWriter<std::fs::File>>>>,
/// Protobuf file writer
protobuf_writer: Arc<tokio::sync::Mutex<Option<std::fs::File>>>,
/// Arrow file path
arrow_file_path: PathBuf,
/// Protobuf file path
protobuf_file_path: PathBuf,
/// Flush interval
flush_interval: Duration,
/// Maximum file size before rotation
max_file_size: Option<u64>,
/// Timestamp of last flush
last_flush: Arc<Mutex<Instant>>,
}
impl DebugWriter {
/// Create a new debug writer
///
/// # Arguments
///
/// * `output_dir` - Output directory for debug files
/// * `flush_interval` - Interval for periodic flushing
/// * `max_file_size` - Maximum file size before rotation (optional)
///
/// # Returns
///
/// Returns debug writer instance, or error if initialization fails.
pub fn new(
output_dir: PathBuf,
table_name: String,
flush_interval: Duration,
max_file_size: Option<u64>,
) -> Result<Self, ZerobusError> {
// Create output directories
let arrow_dir = output_dir.join("zerobus/arrow");
let proto_dir = output_dir.join("zerobus/proto");
std::fs::create_dir_all(&arrow_dir).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create arrow output directory: {}",
e
))
})?;
std::fs::create_dir_all(&proto_dir).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create proto output directory: {}",
e
))
})?;
// Sanitize table name for filesystem (replace dots and slashes with underscores)
let sanitized_table_name = table_name.replace(['.', '/'], "_");
let arrow_file_path = arrow_dir.join(format!("{}.arrow", sanitized_table_name));
let protobuf_file_path = proto_dir.join(format!("{}.proto", sanitized_table_name));
Ok(Self {
output_dir,
arrow_writer: Arc::new(tokio::sync::Mutex::new(None)),
protobuf_writer: Arc::new(tokio::sync::Mutex::new(None)),
arrow_file_path,
protobuf_file_path,
flush_interval,
max_file_size,
last_flush: Arc::new(Mutex::new(Instant::now())),
})
}
/// Ensure Arrow writer is initialized
async fn ensure_arrow_writer(&self) -> Result<(), ZerobusError> {
let mut writer_guard = self.arrow_writer.lock().await;
if writer_guard.is_none() {
let file = std::fs::File::create(&self.arrow_file_path).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create Arrow debug file: {}",
e
))
})?;
let schema = arrow::datatypes::Schema::empty();
let writer = arrow::ipc::writer::FileWriter::try_new(file, &schema).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create Arrow IPC writer: {}",
e
))
})?;
*writer_guard = Some(writer);
}
Ok(())
}
/// Ensure Protobuf writer is initialized
async fn ensure_protobuf_writer(&self) -> Result<(), ZerobusError> {
let mut writer_guard = self.protobuf_writer.lock().await;
if writer_guard.is_none() {
let file = std::fs::File::create(&self.protobuf_file_path).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create Protobuf debug file: {}",
e
))
})?;
*writer_guard = Some(file);
}
Ok(())
}
/// Rotate Arrow file if needed
async fn rotate_arrow_file_if_needed(&self) -> Result<(), ZerobusError> {
if let Some(max_size) = self.max_file_size {
if let Some(new_path) =
rotate_file_if_needed(&self.arrow_file_path, max_size).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to check Arrow file size: {}",
e
))
})?
{
// Close current writer
let mut writer_guard = self.arrow_writer.lock().await;
if let Some(mut writer) = writer_guard.take() {
writer.finish().map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to finish Arrow writer: {}",
e
))
})?;
}
// Update file path and create new writer
// Note: We'd need to update arrow_file_path, but it's immutable
// For now, we'll create a new file with the rotated name
let file = std::fs::File::create(&new_path).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create rotated Arrow file: {}",
e
))
})?;
let schema = arrow::datatypes::Schema::empty();
let writer =
arrow::ipc::writer::FileWriter::try_new(file, &schema).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create rotated Arrow IPC writer: {}",
e
))
})?;
*writer_guard = Some(writer);
}
}
Ok(())
}
/// Rotate Protobuf file if needed
async fn rotate_protobuf_file_if_needed(&self) -> Result<(), ZerobusError> {
if let Some(max_size) = self.max_file_size {
if let Some(new_path) = rotate_file_if_needed(&self.protobuf_file_path, max_size)
.map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to check Protobuf file size: {}",
e
))
})?
{
// Close current writer
let mut writer_guard = self.protobuf_writer.lock().await;
if let Some(file) = writer_guard.take() {
file.sync_all().map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to sync Protobuf file: {}",
e
))
})?;
}
// Create new file
let file = std::fs::File::create(&new_path).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create rotated Protobuf file: {}",
e
))
})?;
*writer_guard = Some(file);
}
}
Ok(())
}
/// Write Arrow RecordBatch to debug file
///
/// # Arguments
///
/// * `batch` - RecordBatch to write
///
/// # Errors
///
/// Returns error if file writing fails.
pub async fn write_arrow(&self, batch: &RecordBatch) -> Result<(), ZerobusError> {
// Check if rotation is needed
self.rotate_arrow_file_if_needed().await?;
// Ensure writer is initialized
self.ensure_arrow_writer().await?;
// Write batch
let mut writer_guard = self.arrow_writer.lock().await;
if let Some(ref mut writer) = *writer_guard {
// Update schema if needed (first write)
if writer.schema().fields().is_empty() {
// Recreate writer with actual schema
drop(writer_guard);
let file = std::fs::File::create(&self.arrow_file_path).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to recreate Arrow file: {}",
e
))
})?;
let writer = arrow::ipc::writer::FileWriter::try_new(file, batch.schema().as_ref())
.map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create Arrow IPC writer with schema: {}",
e
))
})?;
let mut new_guard = self.arrow_writer.lock().await;
*new_guard = Some(writer);
writer_guard = new_guard;
}
if let Some(ref mut writer) = *writer_guard {
writer.write(batch).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to write Arrow RecordBatch: {}",
e
))
})?;
}
}
debug!("Wrote Arrow RecordBatch to debug file");
Ok(())
}
/// Write Protobuf bytes to debug file
///
/// # Arguments
///
/// * `protobuf_bytes` - Protobuf bytes to write
/// * `flush_immediately` - If true, flush to disk immediately after writing
///
/// # Errors
///
/// Returns error if file writing fails.
pub async fn write_protobuf(
&self,
protobuf_bytes: &[u8],
flush_immediately: bool,
) -> Result<(), ZerobusError> {
// Check if rotation is needed
self.rotate_protobuf_file_if_needed().await?;
// Ensure writer is initialized
self.ensure_protobuf_writer().await?;
// Write bytes
let mut writer_guard = self.protobuf_writer.lock().await;
if let Some(ref mut file) = *writer_guard {
file.write_all(protobuf_bytes).map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to write Protobuf bytes: {}", e))
})?;
// Write newline separator for readability (optional)
file.write_all(b"\n").map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to write Protobuf separator: {}",
e
))
})?;
// Flush immediately if requested (for per-batch flushing)
if flush_immediately {
file.sync_all().map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to flush Protobuf file: {}",
e
))
})?;
}
}
debug!(
"Wrote {} bytes to Protobuf debug file{}",
protobuf_bytes.len(),
if flush_immediately { " (flushed)" } else { "" }
);
Ok(())
}
/// Write Protobuf descriptor to file (once per table)
///
/// # Arguments
///
/// * `table_name` - Table name (used for filename)
/// * `descriptor` - Protobuf descriptor to write
///
/// # Errors
///
/// Returns error if file writing fails.
pub async fn write_descriptor(
&self,
table_name: &str,
descriptor: &DescriptorProto,
) -> Result<(), ZerobusError> {
// Create descriptors directory
let descriptors_dir = self.output_dir.join("zerobus/descriptors");
std::fs::create_dir_all(&descriptors_dir).map_err(|e| {
ZerobusError::ConfigurationError(format!(
"Failed to create descriptors directory: {}",
e
))
})?;
// Create filename from table name (sanitize for filesystem)
let sanitized_table_name = table_name.replace(['.', '/'], "_");
let descriptor_file_path = descriptors_dir.join(format!("{}.pb", sanitized_table_name));
// Check if file already exists (only write once per table)
if descriptor_file_path.exists() {
debug!(
"Descriptor file already exists for table {}: {}",
table_name,
descriptor_file_path.display()
);
return Ok(());
}
// Serialize descriptor to bytes
let mut descriptor_bytes = Vec::new();
descriptor.encode(&mut descriptor_bytes).map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to encode Protobuf descriptor: {}", e))
})?;
// Write to file
let mut file = std::fs::File::create(&descriptor_file_path).map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to create descriptor file: {}", e))
})?;
file.write_all(&descriptor_bytes).map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to write descriptor bytes: {}", e))
})?;
file.sync_all().map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to sync descriptor file: {}", e))
})?;
let descriptor_name = descriptor.name.as_deref().unwrap_or("unknown");
info!("✅ Wrote Protobuf descriptor for table '{}' to: {} (descriptor name: '{}', {} fields, {} nested types)",
table_name, descriptor_file_path.display(), descriptor_name,
descriptor.field.len(), descriptor.nested_type.len());
Ok(())
}
/// Flush all pending writes to disk
///
/// # Errors
///
/// Returns error if flush fails.
pub async fn flush(&self) -> Result<(), ZerobusError> {
// Flush Arrow writer
let arrow_guard = self.arrow_writer.lock().await;
if let Some(ref _writer) = *arrow_guard {
// Arrow FileWriter doesn't have explicit flush, but we can ensure it's written
// The writer buffers internally and writes on finish
}
drop(arrow_guard);
// Flush Protobuf writer
let mut proto_guard = self.protobuf_writer.lock().await;
if let Some(ref mut file) = *proto_guard {
file.sync_all().map_err(|e| {
ZerobusError::ConfigurationError(format!("Failed to sync Protobuf file: {}", e))
})?;
}
drop(proto_guard);
// Update last flush time
let mut last_flush = self.last_flush.lock().await;
*last_flush = Instant::now();
debug!("Flushed debug files to disk");
Ok(())
}
/// Check if flush is needed based on interval
///
/// # Returns
///
/// Returns true if flush interval has elapsed.
pub async fn should_flush(&self) -> bool {
let last_flush = self.last_flush.lock().await;
last_flush.elapsed() >= self.flush_interval
}
}