llm-optimizer-processor 0.1.1

Data processing and transformation pipeline
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Integration tests for Stream Processor State Management
//!
//! Tests all required features:
//! 1. Redis-based state persistence
//! 2. Checkpointing every 10 seconds
//! 3. State recovery on restart
//! 4. Connection failure handling
//! 5. Connection pooling and health checks
//! 6. Comprehensive metrics

use processor::state::{
    CheckpointCoordinator, CheckpointOptions, RedisConfig, RedisStateBackend, StateBackend,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct WindowState {
    window_id: String,
    count: u64,
    sum: f64,
}

impl WindowState {
    fn new(window_id: String) -> Self {
        Self {
            window_id,
            count: 0,
            sum: 0.0,
        }
    }

    fn add_value(&mut self, value: f64) {
        self.count += 1;
        self.sum += value;
    }
}

/// Helper to create Redis backend for testing
async fn create_redis_backend() -> Option<RedisStateBackend> {
    let config = RedisConfig::builder()
        .url("redis://localhost:6379")
        .key_prefix("test_stream_processor:")
        .default_ttl(Duration::from_secs(3600))
        .pool_size(5, 20)
        .retries(3, Duration::from_millis(100), Duration::from_secs(5))
        .build()
        .ok()?;

    match RedisStateBackend::new(config).await {
        Ok(backend) => {
            // Clear any existing test data
            let _ = backend.clear().await;
            Some(backend)
        }
        Err(_) => None,
    }
}

#[tokio::test]
async fn test_redis_state_persistence() {
    if let Some(backend) = create_redis_backend().await {
        // Store window state
        let mut window = WindowState::new("window_1".to_string());
        window.add_value(10.0);
        window.add_value(20.0);

        let key = b"window:window_1";
        let value = bincode::serialize(&window).unwrap();
        backend.put(key, &value).await.unwrap();

        // Retrieve window state
        let retrieved = backend.get(key).await.unwrap().unwrap();
        let restored: WindowState = bincode::deserialize(&retrieved).unwrap();

        assert_eq!(restored.window_id, "window_1");
        assert_eq!(restored.count, 2);
        assert_eq!(restored.sum, 30.0);
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_connection_pooling() {
    if let Some(backend) = create_redis_backend().await {
        // Verify health check
        let healthy = backend.health_check().await.unwrap();
        assert!(healthy, "Redis connection should be healthy");

        // Perform multiple concurrent operations to test pooling
        let mut handles = vec![];

        for i in 0..20 {
            let backend = Arc::new(backend);
            let handle = tokio::spawn(async move {
                let key = format!("concurrent:key:{}", i).into_bytes();
                let value = format!("value:{}", i).into_bytes();
                backend.put(&key, &value).await.unwrap();
                backend.get(&key).await.unwrap()
            });
            handles.push(handle);
        }

        // Wait for all operations
        for handle in handles {
            let result = handle.await.unwrap();
            assert!(result.is_some());
        }
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_retry_logic() {
    if let Some(backend) = create_redis_backend().await {
        // Perform operations that will track retries
        backend.put(b"retry_test", b"value").await.unwrap();
        backend.get(b"retry_test").await.unwrap();

        let stats = backend.stats().await;

        // Operations should succeed without retries if Redis is healthy
        assert_eq!(stats.error_count, 0);
        assert!(stats.get_count > 0);
        assert!(stats.put_count > 0);
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_checkpoint_every_10_seconds() {
    if let Some(backend) = create_redis_backend().await {
        let temp_dir =
            std::env::temp_dir().join(format!("checkpoint_test_{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&temp_dir).unwrap();

        // Store some window state
        backend.put(b"window:1", b"state1").await.unwrap();
        backend.put(b"window:2", b"state2").await.unwrap();

        let options = CheckpointOptions {
            checkpoint_dir: temp_dir.clone(),
            max_checkpoints: 5,
            compress: true,
            min_interval: Duration::from_secs(1), // Shorter for testing
        };

        let coordinator = CheckpointCoordinator::new(
            Arc::new(backend),
            Duration::from_secs(2), // Checkpoint every 2 seconds for faster testing
            options,
        );

        // Start periodic checkpointing
        coordinator.start().await.unwrap();

        // Wait for multiple checkpoints
        sleep(Duration::from_secs(6)).await;

        // Shutdown to get stats
        coordinator.shutdown().await.unwrap();

        // Verify checkpoints were created
        let stats = coordinator.stats().await;
        assert!(
            stats.checkpoints_created >= 2,
            "Should have created at least 2 checkpoints"
        );
        assert_eq!(stats.checkpoint_failures, 0);

        // Cleanup
        std::fs::remove_dir_all(&temp_dir).ok();
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_state_recovery() {
    if let Some(backend) = create_redis_backend().await {
        let temp_dir =
            std::env::temp_dir().join(format!("recovery_test_{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&temp_dir).unwrap();

        // Phase 1: Create state and checkpoint
        {
            let backend_arc = Arc::new(backend);

            // Store window state
            for i in 1..=5 {
                let window = WindowState::new(format!("window_{}", i));
                let key = format!("window:window_{}", i).into_bytes();
                let value = bincode::serialize(&window).unwrap();
                backend_arc.put(&key, &value).await.unwrap();
            }

            let options = CheckpointOptions {
                checkpoint_dir: temp_dir.clone(),
                max_checkpoints: 3,
                compress: true,
                min_interval: Duration::from_secs(1),
            };

            let coordinator = CheckpointCoordinator::new(
                backend_arc.clone(),
                Duration::from_secs(60),
                options,
            );

            // Create checkpoint
            let checkpoint_id = coordinator.create_checkpoint().await.unwrap();
            assert!(!checkpoint_id.is_empty());

            // Clear backend to simulate restart
            backend_arc.clear().await.unwrap();
            assert_eq!(backend_arc.count().await.unwrap(), 0);
        }

        // Phase 2: Restore state from checkpoint
        {
            let backend = create_redis_backend().await.unwrap();
            let backend_arc = Arc::new(backend);

            let options = CheckpointOptions {
                checkpoint_dir: temp_dir.clone(),
                max_checkpoints: 3,
                compress: true,
                min_interval: Duration::from_secs(1),
            };

            let coordinator = CheckpointCoordinator::new(
                backend_arc.clone(),
                Duration::from_secs(60),
                options,
            );

            // Restore from checkpoint
            coordinator.restore_latest().await.unwrap();

            // Verify state was restored
            let count = backend_arc.count().await.unwrap();
            assert_eq!(count, 5, "Should have restored 5 windows");

            // Verify specific window
            let key = b"window:window_1";
            let data = backend_arc.get(key).await.unwrap().unwrap();
            let window: WindowState = bincode::deserialize(&data).unwrap();
            assert_eq!(window.window_id, "window_1");
        }

        // Cleanup
        std::fs::remove_dir_all(&temp_dir).ok();
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_batch_operations() {
    if let Some(backend) = create_redis_backend().await {
        // Prepare batch data
        let windows: Vec<WindowState> = (1..=10)
            .map(|i| WindowState::new(format!("window_{}", i)))
            .collect();

        // Batch put
        let pairs: Vec<(Vec<u8>, Vec<u8>)> = windows
            .iter()
            .map(|w| {
                let key = format!("window:{}", w.window_id).into_bytes();
                let value = bincode::serialize(w).unwrap();
                (key, value)
            })
            .collect();

        let pair_refs: Vec<(&[u8], &[u8])> = pairs
            .iter()
            .map(|(k, v)| (k.as_slice(), v.as_slice()))
            .collect();

        backend.batch_put(&pair_refs).await.unwrap();

        // Batch get
        let keys: Vec<Vec<u8>> = (1..=10)
            .map(|i| format!("window:window_{}", i).into_bytes())
            .collect();

        let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
        let values = backend.batch_get(&key_refs).await.unwrap();

        assert_eq!(values.len(), 10);
        for value in values {
            assert!(value.is_some());
        }

        // Batch delete
        let deleted = backend.batch_delete(&key_refs).await.unwrap();
        assert_eq!(deleted, 10);
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_ttl_management() {
    if let Some(backend) = create_redis_backend().await {
        // Store with custom TTL
        backend
            .put_with_ttl(b"temp_window", b"data", Duration::from_secs(2))
            .await
            .unwrap();

        // Verify key exists
        assert!(backend.contains(b"temp_window").await.unwrap());

        // Check TTL
        let ttl = backend.ttl(b"temp_window").await.unwrap();
        assert!(ttl.is_some());
        assert!(ttl.unwrap().as_secs() <= 2);

        // Wait for expiration
        sleep(Duration::from_secs(3)).await;

        // Key should be expired
        assert!(!backend.contains(b"temp_window").await.unwrap());
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_atomic_operations() {
    if let Some(backend) = create_redis_backend().await {
        // Test set if not exists
        let set1 = backend
            .set_if_not_exists(b"lock:window:1", b"processor_1", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(set1, "First set should succeed");

        let set2 = backend
            .set_if_not_exists(b"lock:window:1", b"processor_2", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(!set2, "Second set should fail");

        // Test get and delete atomically
        backend.put(b"temp_key", b"temp_value").await.unwrap();

        let value = backend.get_and_delete(b"temp_key").await.unwrap();
        assert_eq!(value, Some(b"temp_value".to_vec()));

        // Key should be deleted
        assert!(!backend.contains(b"temp_key").await.unwrap());
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_statistics_tracking() {
    if let Some(backend) = create_redis_backend().await {
        // Perform various operations
        backend.put(b"key1", b"value1").await.unwrap();
        backend.put(b"key2", b"value2").await.unwrap();

        backend.get(b"key1").await.unwrap(); // Hit
        backend.get(b"key1").await.unwrap(); // Hit
        backend.get(b"nonexistent").await.unwrap(); // Miss

        backend.delete(b"key1").await.unwrap();

        // Check statistics
        let stats = backend.stats().await;

        assert!(stats.put_count >= 2);
        assert!(stats.get_count >= 3);
        assert!(stats.delete_count >= 1);
        assert!(stats.hit_count >= 2);
        assert!(stats.miss_count >= 1);

        assert!(stats.hit_rate() > 0.0);
        assert!(stats.hit_rate() <= 1.0);

        assert!(stats.total_operations() > 0);
        assert!(stats.bytes_read > 0);
        assert!(stats.bytes_written > 0);
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_window_state_lifecycle() {
    if let Some(backend) = create_redis_backend().await {
        // Create window
        let mut window = WindowState::new("test_window".to_string());

        // Add events
        for i in 1..=100 {
            window.add_value(i as f64);
        }

        // Store window
        let key = b"window:test_window";
        let value = bincode::serialize(&window).unwrap();
        backend.put(key, &value).await.unwrap();

        // Update window
        window.add_value(101.0);
        let updated_value = bincode::serialize(&window).unwrap();
        backend.put(key, &updated_value).await.unwrap();

        // Retrieve and verify
        let retrieved = backend.get(key).await.unwrap().unwrap();
        let restored: WindowState = bincode::deserialize(&retrieved).unwrap();

        assert_eq!(restored.count, 101);
        assert_eq!(restored.sum, 5151.0); // Sum of 1..=101

        // Clean up
        backend.delete(key).await.unwrap();
        assert!(!backend.contains(key).await.unwrap());
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_concurrent_window_access() {
    if let Some(backend) = create_redis_backend().await {
        let backend = Arc::new(backend);
        let mut handles = vec![];

        // Spawn 10 tasks, each updating different windows
        for task_id in 0..10 {
            let backend = Arc::clone(&backend);
            let handle = tokio::spawn(async move {
                for i in 0..10 {
                    let mut window = WindowState::new(format!("task_{}_{}", task_id, i));
                    window.add_value(42.0);

                    let key = format!("window:task_{}_{}", task_id, i).into_bytes();
                    let value = bincode::serialize(&window).unwrap();
                    backend.put(&key, &value).await.unwrap();
                }
            });
            handles.push(handle);
        }

        // Wait for all tasks
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify all windows were created
        let all_windows = backend.list_keys(b"window:task_").await.unwrap();
        assert_eq!(all_windows.len(), 100);
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}

#[tokio::test]
async fn test_memory_usage_monitoring() {
    if let Some(backend) = create_redis_backend().await {
        // Get initial memory usage
        let memory = backend.memory_usage().await.unwrap();
        assert!(memory > 0, "Should report memory usage");

        // Add data
        for i in 0..100 {
            let key = format!("memory_test:{}", i).into_bytes();
            let value = vec![0u8; 1024]; // 1KB per key
            backend.put(&key, &value).await.unwrap();
        }

        // Memory usage should increase
        let new_memory = backend.memory_usage().await.unwrap();
        assert!(
            new_memory > memory,
            "Memory usage should increase after adding data"
        );

        // Clean up
        let keys: Vec<Vec<u8>> = (0..100)
            .map(|i| format!("memory_test:{}", i).into_bytes())
            .collect();
        let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
        backend.batch_delete(&key_refs).await.unwrap();
    } else {
        eprintln!("Skipping test: Redis not available");
    }
}