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
//! Complete Stream Processor State Management Example
//!
//! This example demonstrates all required features:
//! 1. Redis-based state persistence for window state
//! 2. Automatic checkpointing every 10 seconds
//! 3. State recovery on restart
//! 4. Connection failure handling with retry logic
//! 5. Connection pooling and health checks
//! 6. Comprehensive metrics and monitoring
//!
//! ## Prerequisites
//!
//! Start Redis:
//! ```bash
//! docker run -d --name redis-state -p 6379:6379 redis:7-alpine
//! ```
//!
//! ## Run Example
//!
//! ```bash
//! cargo run --example stream_processor_state_complete
//! ```

use processor::state::{
    CheckpointCoordinator, CheckpointOptions, RedisConfig, RedisStateBackend, StateBackend,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::signal;
use tokio::sync::RwLock;
use tokio::time::{interval, sleep};
use tracing::{error, info, warn};

/// Window state for stream processing
#[derive(Debug, Clone, Serialize, Deserialize)]
struct WindowState {
    window_id: String,
    start_time: i64,
    end_time: i64,
    event_count: u64,
    sum: f64,
    min: f64,
    max: f64,
    last_updated: i64,
}

impl WindowState {
    fn new(window_id: String, start_time: i64, end_time: i64) -> Self {
        Self {
            window_id,
            start_time,
            end_time,
            event_count: 0,
            sum: 0.0,
            min: f64::MAX,
            max: f64::MIN,
            last_updated: chrono::Utc::now().timestamp_millis(),
        }
    }

    fn add_event(&mut self, value: f64) {
        self.event_count += 1;
        self.sum += value;
        self.min = self.min.min(value);
        self.max = self.max.max(value);
        self.last_updated = chrono::Utc::now().timestamp_millis();
    }

    fn average(&self) -> f64 {
        if self.event_count > 0 {
            self.sum / self.event_count as f64
        } else {
            0.0
        }
    }

    fn serialize(&self) -> Vec<u8> {
        bincode::serialize(self).expect("Serialization should not fail")
    }

    fn deserialize(data: &[u8]) -> Self {
        bincode::deserialize(data).expect("Deserialization should not fail")
    }
}

/// Stream Processor with Redis state management
struct StreamProcessor {
    backend: Arc<RedisStateBackend>,
    coordinator: Arc<CheckpointCoordinator<RedisStateBackend>>,
    window_duration_ms: i64,
    active_windows: Arc<RwLock<Vec<String>>>,
}

impl StreamProcessor {
    /// Create a new stream processor with Redis state backend
    async fn new(redis_url: &str, checkpoint_dir: &str) -> anyhow::Result<Self> {
        info!("Initializing Stream Processor with Redis state backend");

        // Configure Redis backend
        let config = RedisConfig::builder()
            .url(redis_url)
            .key_prefix("stream_processor:")
            .default_ttl(Duration::from_secs(3600)) // 1 hour TTL for window state
            .pool_size(10, 50) // Min 10, max 50 connections
            .timeouts(
                Duration::from_secs(5),  // Connect timeout
                Duration::from_secs(3),  // Read timeout
                Duration::from_secs(3),  // Write timeout
            )
            .retries(
                3,                          // Max 3 retries
                Duration::from_millis(100), // Base delay
                Duration::from_secs(5),     // Max delay
            )
            .cluster_mode(false)
            .pipeline_batch_size(100)
            .build()?;

        info!("Connecting to Redis at {}", config.primary_url());
        let backend = Arc::new(RedisStateBackend::new(config).await?);

        // Verify connection health
        if !backend.health_check().await? {
            return Err(anyhow::anyhow!("Redis health check failed"));
        }
        info!("✓ Redis connection healthy");

        // Configure checkpoint coordinator
        let checkpoint_options = CheckpointOptions {
            checkpoint_dir: checkpoint_dir.into(),
            max_checkpoints: 10,                    // Keep last 10 checkpoints
            compress: true,                         // Enable compression
            min_interval: Duration::from_secs(10), // Min interval between checkpoints
        };

        let coordinator = Arc::new(CheckpointCoordinator::new(
            backend.clone(),
            Duration::from_secs(10), // Checkpoint every 10 seconds
            checkpoint_options,
        ));

        // Start periodic checkpointing
        coordinator.start().await?;
        info!("✓ Checkpoint coordinator started (interval: 10s)");

        Ok(Self {
            backend,
            coordinator,
            window_duration_ms: 5000, // 5-second windows
            active_windows: Arc::new(RwLock::new(Vec::new())),
        })
    }

    /// Attempt to restore state from latest checkpoint
    async fn restore_state(&self) -> anyhow::Result<()> {
        info!("Attempting to restore state from checkpoint...");

        match self.coordinator.restore_latest().await {
            Ok(checkpoint_id) => {
                info!("✓ State restored from checkpoint: {}", checkpoint_id);

                // Reload active windows list
                let window_keys = self.backend.list_keys(b"window:").await?;
                let mut active = self.active_windows.write().await;
                active.clear();

                for key in window_keys {
                    if let Ok(key_str) = String::from_utf8(key) {
                        if let Some(window_id) = key_str.strip_prefix("window:") {
                            active.push(window_id.to_string());
                        }
                    }
                }

                info!("✓ Restored {} active windows", active.len());
                Ok(())
            }
            Err(e) => {
                warn!("No checkpoint found or restore failed: {}. Starting fresh.", e);
                Ok(())
            }
        }
    }

    /// Create or update a window with new event
    async fn process_event(&self, timestamp: i64, value: f64) -> anyhow::Result<()> {
        // Calculate window boundaries
        let window_start = (timestamp / self.window_duration_ms) * self.window_duration_ms;
        let window_end = window_start + self.window_duration_ms;
        let window_id = format!("{}", window_start);
        let window_key = format!("window:{}", window_id).into_bytes();

        // Get or create window state
        let mut window = if let Some(data) = self.backend.get(&window_key).await? {
            WindowState::deserialize(&data)
        } else {
            info!("Creating new window: {}", window_id);
            let window = WindowState::new(window_id.clone(), window_start, window_end);

            // Track active window
            let mut active = self.active_windows.write().await;
            if !active.contains(&window_id) {
                active.push(window_id.clone());
            }

            window
        };

        // Add event to window
        window.add_event(value);

        // Store updated window state
        let serialized = window.serialize();
        self.backend.put(&window_key, &serialized).await?;

        Ok(())
    }

    /// Clean up old windows
    async fn cleanup_old_windows(&self, current_time: i64) -> anyhow::Result<usize> {
        let cutoff = current_time - (self.window_duration_ms * 10); // Keep last 10 windows
        let mut deleted = 0;

        let active = self.active_windows.read().await;
        let mut to_delete = Vec::new();

        for window_id in active.iter() {
            if let Ok(start_time) = window_id.parse::<i64>() {
                if start_time < cutoff {
                    to_delete.push(window_id.clone());
                }
            }
        }
        drop(active);

        if !to_delete.is_empty() {
            info!("Cleaning up {} old windows", to_delete.len());

            let keys: Vec<Vec<u8>> = to_delete
                .iter()
                .map(|id| format!("window:{}", id).into_bytes())
                .collect();

            let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
            deleted = self.backend.batch_delete(&key_refs).await?;

            // Update active windows list
            let mut active = self.active_windows.write().await;
            active.retain(|id| !to_delete.contains(id));
        }

        Ok(deleted)
    }

    /// Get current statistics
    async fn print_statistics(&self) {
        let stats = self.backend.stats().await;
        let checkpoint_stats = self.coordinator.stats().await;

        info!("=== Statistics ===");
        info!("State Backend:");
        info!("  Total operations: {}", stats.total_operations());
        info!("  GET operations: {}", stats.get_count);
        info!("  PUT operations: {}", stats.put_count);
        info!("  DELETE operations: {}", stats.delete_count);
        info!("  Hit rate: {:.2}%", stats.hit_rate() * 100.0);
        info!("  Error rate: {:.2}%", stats.error_rate() * 100.0);
        info!("  Retry count: {}", stats.retry_count);
        info!("  Avg latency: {}μs", stats.avg_latency_us);
        info!("  Bytes read: {}", stats.bytes_read);
        info!("  Bytes written: {}", stats.bytes_written);

        info!("Checkpointing:");
        info!("  Checkpoints created: {}", checkpoint_stats.checkpoints_created);
        info!("  Checkpoint failures: {}", checkpoint_stats.checkpoint_failures);
        info!("  Restores: {}", checkpoint_stats.restores);
        info!(
            "  Last checkpoint: {:?}",
            checkpoint_stats.last_checkpoint_time
        );
        info!(
            "  Last checkpoint duration: {:?}ms",
            checkpoint_stats.last_checkpoint_duration_ms
        );

        let active_count = self.active_windows.read().await.len();
        info!("Active windows: {}", active_count);
    }

    /// Simulate connection failure and recovery
    async fn simulate_connection_failure(&self) -> anyhow::Result<()> {
        info!("=== Simulating Connection Failure ===");

        // This will trigger retries automatically
        // Try to perform operations - retry logic will handle transient failures
        info!("Attempting operations during simulated failure...");

        match self.backend.get(b"test_key").await {
            Ok(_) => info!("✓ Operation succeeded (connection recovered)"),
            Err(e) => warn!("Operation failed after retries: {}", e),
        }

        Ok(())
    }

    /// Shutdown processor
    async fn shutdown(&self) -> anyhow::Result<()> {
        info!("Shutting down Stream Processor...");

        // Stop checkpoint coordinator (creates final checkpoint)
        self.coordinator.shutdown().await?;
        info!("✓ Final checkpoint created");

        // Print final statistics
        self.print_statistics().await;

        info!("✓ Shutdown complete");
        Ok(())
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize logging
    tracing_subscriber::fmt()
        .with_env_filter("info,stream_processor_state_complete=debug")
        .with_target(false)
        .with_thread_ids(false)
        .with_line_number(false)
        .init();

    info!("=== Stream Processor State Management Demo ===");

    // Create processor with Redis state backend
    let checkpoint_dir = std::env::temp_dir().join("stream_processor_checkpoints");
    std::fs::create_dir_all(&checkpoint_dir)?;

    let processor = StreamProcessor::new(
        "redis://localhost:6379",
        checkpoint_dir.to_str().unwrap(),
    )
    .await?;

    // Attempt to restore state from previous run
    processor.restore_state().await?;

    // Set up graceful shutdown
    let shutdown_signal = Arc::new(tokio::sync::Notify::new());
    let shutdown_signal_clone = shutdown_signal.clone();

    tokio::spawn(async move {
        signal::ctrl_c().await.expect("Failed to listen for Ctrl+C");
        info!("Received shutdown signal (Ctrl+C)");
        shutdown_signal_clone.notify_one();
    });

    // Simulate stream processing
    info!("Starting stream processing...");
    let mut event_interval = interval(Duration::from_millis(100));
    let mut stats_interval = interval(Duration::from_secs(30));
    let mut cleanup_interval = interval(Duration::from_secs(60));

    let mut event_count = 0u64;
    let start_time = chrono::Utc::now();

    loop {
        tokio::select! {
            _ = event_interval.tick() => {
                // Simulate incoming event
                let timestamp = chrono::Utc::now().timestamp_millis();
                let value = rand::random::<f64>() * 100.0;

                if let Err(e) = processor.process_event(timestamp, value).await {
                    error!("Failed to process event: {}", e);
                } else {
                    event_count += 1;

                    if event_count % 100 == 0 {
                        info!("Processed {} events", event_count);
                    }
                }
            }

            _ = stats_interval.tick() => {
                // Print statistics every 30 seconds
                processor.print_statistics().await;

                let elapsed = chrono::Utc::now().signed_duration_since(start_time);
                let events_per_sec = event_count as f64 / elapsed.num_seconds() as f64;
                info!("Throughput: {:.2} events/sec", events_per_sec);
            }

            _ = cleanup_interval.tick() => {
                // Clean up old windows every minute
                let current_time = chrono::Utc::now().timestamp_millis();
                match processor.cleanup_old_windows(current_time).await {
                    Ok(deleted) => {
                        if deleted > 0 {
                            info!("Cleaned up {} old windows", deleted);
                        }
                    }
                    Err(e) => error!("Failed to clean up windows: {}", e),
                }
            }

            _ = shutdown_signal.notified() => {
                // Graceful shutdown
                break;
            }
        }
    }

    // Shutdown processor
    processor.shutdown().await?;

    info!("=== Demo Complete ===");
    info!("Total events processed: {}", event_count);

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_window_state() {
        let mut window = WindowState::new("test".to_string(), 0, 5000);

        window.add_event(10.0);
        window.add_event(20.0);
        window.add_event(30.0);

        assert_eq!(window.event_count, 3);
        assert_eq!(window.sum, 60.0);
        assert_eq!(window.average(), 20.0);
        assert_eq!(window.min, 10.0);
        assert_eq!(window.max, 30.0);
    }

    #[tokio::test]
    async fn test_window_serialization() {
        let window = WindowState::new("test".to_string(), 0, 5000);
        let serialized = window.serialize();
        let deserialized = WindowState::deserialize(&serialized);

        assert_eq!(window.window_id, deserialized.window_id);
        assert_eq!(window.start_time, deserialized.start_time);
        assert_eq!(window.end_time, deserialized.end_time);
    }
}