sift_stream 0.8.2

A robust Sift telemetry streaming library
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use chrono::Utc;
use sift_error::prelude::*;
use sift_pbfs::chunk::PbfsChunk;
use sift_rs::ingest::v1::IngestWithConfigDataStreamRequest;
use std::path::PathBuf;
use tokio::fs::File;
use tokio::io::{AsyncWriteExt, BufWriter};

const BACKUP_FILE_BUFFER_WRITE_SIZE: usize = 128 * 1024;

/// Configuration for file writing operations.
#[derive(Clone)]
pub(crate) struct FileWriterConfig {
    /// The directory to store files in.
    pub(crate) directory: PathBuf,

    /// The prefix to add to all files.
    pub(crate) prefix: String,

    /// The maximum size of a file in bytes.
    pub(crate) max_size: usize,
}

/// Context for a file and the messages it contains.
#[derive(Debug, Default, Clone)]
pub(crate) struct FileContext {
    /// The number of messages in the file.
    pub(crate) message_count: usize,

    /// The path to the file (used for test assertions).
    #[allow(dead_code)]
    pub(crate) file_path: PathBuf,

    /// The estimated size of the file in bytes (based on the messages in the file).
    pub(crate) file_size: usize,
}

/// Manages writing IngestWithConfigDataStreamRequest messages to files with rotation support.
pub(crate) struct FileWriter {
    /// Configuration for file writing.
    pub(crate) config: FileWriterConfig,

    /// The current file context being written to.
    pub(crate) current_file_ctx: FileContext,

    /// The current file being written to.
    pub(crate) current_file: Option<BufWriter<File>>,

    /// Reusable buffer for encoding PbfsChunk data to avoid per-message allocations.
    pub(crate) chunk_encode_buffer: Vec<u8>,
}

impl FileWriter {
    /// Create a new FileWriter with the given configuration.
    pub(crate) fn new(config: FileWriterConfig) -> Self {
        Self {
            config,
            current_file_ctx: FileContext::default(),
            current_file: None,
            chunk_encode_buffer: Vec::new(),
        }
    }

    /// Write a request to the current file, creating a new file if needed.
    pub(crate) async fn write_request(
        &mut self,
        request: &IngestWithConfigDataStreamRequest,
    ) -> Result<()> {
        // Create a new file if one doesn't exist.
        if self.current_file.is_none() {
            let (path, file) = Self::create_file(&self.config).await?;
            self.current_file_ctx = FileContext {
                message_count: 0,
                file_path: path.clone(),
                file_size: 0,
            };
            self.current_file = Some(BufWriter::with_capacity(
                BACKUP_FILE_BUFFER_WRITE_SIZE,
                file,
            ));
        }

        let Some(file) = self.current_file.as_mut() else {
            return Err(Error::new_msg(
                ErrorKind::BackupsError,
                "current file is not set",
            ));
        };

        // Encode message into reusable buffer to avoid per-message allocations
        let encoded_chunk = PbfsChunk::encode_into(
            core::slice::from_ref(request),
            &mut self.chunk_encode_buffer,
        )?;

        // Write immediately
        file.write_all(encoded_chunk).await?;

        self.current_file_ctx.message_count += 1;
        self.current_file_ctx.file_size += encoded_chunk.len();

        Ok(())
    }

    /// Checks if the current file has reached the max size and should be rotated.
    pub(crate) fn should_rotate_file(&self) -> bool {
        self.current_file_ctx.file_size >= self.config.max_size
    }

    /// Rotates the current file by closing it and returning the file context.
    /// Returns None if there's no current file.
    pub(crate) async fn rotate_file(&mut self) -> Result<Option<FileContext>> {
        // Close out the current file by dropping it, if there is no file, there is nothing to do.
        match self.current_file.take() {
            Some(mut writer) => {
                // Flush the buffer to the file.
                writer
                    .flush()
                    .await
                    .map_err(|e| Error::new(ErrorKind::BackupsError, e))?;
                let file = writer.into_inner();

                // Ensure data is persisted to disk and not simply buffered in the kernel.
                if let Err(e) = file.sync_all().await {
                    #[cfg(feature = "tracing")]
                    tracing::warn!("unable to sync file, data may be lost: {e:?}");
                }

                // Return the file context
                let ctx = self.current_file_ctx.clone();
                self.current_file_ctx = FileContext::default();
                Ok(Some(ctx))
            }
            None => Ok(None),
        }
    }

    /// Flush and sync the current file if it exists.
    pub(crate) async fn flush(&mut self) -> Result<()> {
        if let Some(writer) = self.current_file.as_mut() {
            writer
                .flush()
                .await
                .map_err(|e| Error::new(ErrorKind::BackupsError, e))?;
        }
        Ok(())
    }

    /// Sync the current file to disk if it exists.
    pub(crate) async fn sync(&mut self) -> Result<()> {
        if let Some(writer) = self.current_file.take() {
            let file = writer.into_inner();
            file.sync_all()
                .await
                .map_err(|e| Error::new(ErrorKind::BackupsError, e))?;
            self.current_file = Some(BufWriter::with_capacity(
                BACKUP_FILE_BUFFER_WRITE_SIZE,
                file,
            ));
        }
        Ok(())
    }

    /// Create a new file with a timestamp-based name.
    async fn create_file(config: &FileWriterConfig) -> Result<(PathBuf, File)> {
        let file_path = config.directory.join(format!(
            "{}-{}",
            config.prefix,
            Utc::now().timestamp_micros()
        ));
        let file = File::create(&file_path)
            .await
            .map_err(|e| Error::new(ErrorKind::BackupsError, e))
            .context("failed to generate file")
            .help("please contact Sift")?;

        Ok((file_path, file))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sift_rs::ingest::v1::IngestWithConfigDataStreamRequest;
    use tempdir::TempDir;
    use uuid::Uuid;

    fn create_test_request(flow: &str) -> IngestWithConfigDataStreamRequest {
        IngestWithConfigDataStreamRequest {
            ingestion_config_id: Uuid::new_v4().to_string(),
            flow: flow.to_string(),
            timestamp: None,
            channel_values: vec![],
            run_id: Uuid::new_v4().to_string(),
            end_stream_on_validation_error: false,
            organization_id: Uuid::new_v4().to_string(),
        }
    }

    #[tokio::test]
    async fn test_file_writer_new() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024,
        };

        let writer = FileWriter::new(config);
        assert_eq!(writer.current_file_ctx.message_count, 0);
        assert_eq!(writer.current_file_ctx.file_size, 0);
        assert!(!writer.should_rotate_file());
    }

    #[tokio::test]
    async fn test_file_writer_write_request_creates_file() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024 * 1024, // 1MB
        };

        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        // Initially no file should exist
        assert!(writer.current_file.is_none());

        // Write a request - should create a file
        assert!(writer.write_request(&request).await.is_ok());

        // File should now exist
        assert!(writer.current_file.is_some());
        assert_eq!(writer.current_file_ctx.message_count, 1);
        assert!(writer.current_file_ctx.file_size > 0);
        assert!(writer.current_file_ctx.file_path.exists());
    }

    #[tokio::test]
    async fn test_file_writer_write_multiple_requests() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024 * 1024, // 1MB
        };

        let mut writer = FileWriter::new(config);

        // Write multiple requests
        for i in 0..5 {
            let request = create_test_request(&format!("flow_{}", i));
            assert!(writer.write_request(&request).await.is_ok());
        }

        assert_eq!(writer.current_file_ctx.message_count, 5);
        assert!(writer.current_file_ctx.file_size > 0);
    }

    #[tokio::test]
    async fn test_file_writer_should_rotate_file() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 100, // Very small max size
        };

        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        // Write a request
        assert!(writer.write_request(&request).await.is_ok());

        // Should not rotate yet (unless the encoded chunk is >= 100 bytes)
        let should_rotate = writer.should_rotate_file();

        // If the encoded chunk is small, write more until we exceed max_size
        if !should_rotate {
            // Write many more requests to exceed max_size
            for _ in 0..100 {
                assert!(writer.write_request(&request).await.is_ok());
                if writer.should_rotate_file() {
                    break;
                }
            }
        }

        // At some point we should need to rotate
        assert!(writer.should_rotate_file());
    }

    #[tokio::test]
    async fn test_file_writer_rotate_file() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 100, // Very small max size
        };

        let max_size = config.max_size;
        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        // Write requests until we need to rotate
        for _ in 0..100 {
            assert!(writer.write_request(&request).await.is_ok());
            if writer.should_rotate_file() {
                break;
            }
        }

        // Rotate the file
        let rotated_ctx = writer.rotate_file().await.expect("failed to rotate file");

        // Should return a context if a file was rotated
        if writer.current_file_ctx.file_size >= max_size {
            assert!(rotated_ctx.is_some());
            let ctx = rotated_ctx.unwrap();
            assert!(ctx.message_count > 0);
            assert!(ctx.file_size > 0);
            assert!(ctx.file_path.exists());

            // After rotation, current file context should be reset
            assert_eq!(writer.current_file_ctx.message_count, 0);
            assert_eq!(writer.current_file_ctx.file_size, 0);
        }
    }

    #[tokio::test]
    async fn test_file_writer_rotate_file_no_file() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024,
        };

        let mut writer = FileWriter::new(config);

        // Try to rotate when no file exists
        let result = writer.rotate_file().await.expect("failed to rotate file");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_file_writer_flush() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024,
        };

        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        // Flush when no file exists should succeed
        assert!(writer.flush().await.is_ok());

        // Write a request
        assert!(writer.write_request(&request).await.is_ok());

        // Flush should succeed
        assert!(writer.flush().await.is_ok());
    }

    #[tokio::test]
    async fn test_file_writer_sync() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024,
        };

        let mut writer = FileWriter::new(config);

        // Sync when no file exists should succeed
        assert!(writer.sync().await.is_ok());

        let request = create_test_request("test_flow");

        // Write a request
        assert!(writer.write_request(&request).await.is_ok());

        // Sync should succeed
        assert!(writer.sync().await.is_ok());

        // File should still exist after sync
        assert!(writer.current_file.is_some());
    }

    #[tokio::test]
    async fn test_file_writer_current_file_context() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 1024,
        };

        let writer = FileWriter::new(config);
        assert_eq!(writer.current_file_ctx.message_count, 0);
        assert_eq!(writer.current_file_ctx.file_size, 0);
    }

    #[tokio::test]
    async fn test_file_writer_file_naming() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "backup".to_string(),
            max_size: 1024,
        };

        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        assert!(writer.write_request(&request).await.is_ok());

        let file_path = writer.current_file_ctx.file_path.clone();
        let file_name = file_path.file_name().unwrap().to_string_lossy();

        // File name should start with prefix
        assert!(file_name.starts_with("backup-"));
    }

    #[tokio::test]
    async fn test_file_writer_multiple_rotations() {
        let temp_dir = TempDir::new("test_file_writer").unwrap();
        let config = FileWriterConfig {
            directory: temp_dir.path().to_path_buf(),
            prefix: "test".to_string(),
            max_size: 100, // Very small max size
        };

        let mut writer = FileWriter::new(config);
        let request = create_test_request("test_flow");

        let mut rotated_files = Vec::new();

        // Write and rotate multiple times
        for _ in 0..3 {
            // Write requests until we need to rotate
            for _ in 0..100 {
                assert!(writer.write_request(&request).await.is_ok());
                if writer.should_rotate_file() {
                    break;
                }
            }

            if writer.should_rotate_file() {
                if let Some(ctx) = writer.rotate_file().await.expect("failed to rotate file") {
                    rotated_files.push(ctx.file_path);
                }
            }
        }

        // Should have created multiple files
        if !rotated_files.is_empty() {
            assert!(rotated_files.len() > 0);
            for file_path in &rotated_files {
                assert!(file_path.exists());
            }
        }
    }
}