llm-optimizer-processor 0.1.1

Data processing and transformation pipeline
Documentation
# State Management Module

Production-ready state persistence for stream processing with support for in-memory and persistent storage backends.

## Quick Start

### In-Memory State

```rust
use processor::state::{MemoryStateBackend, StateBackend};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let backend = MemoryStateBackend::new(None);

    backend.put(b"window:123", b"aggregated_state").await?;
    let data = backend.get(b"window:123").await?;

    Ok(())
}
```

### Persistent State

```rust
use processor::state::{SledStateBackend, SledConfig, StateBackend};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = SledConfig::new("/var/lib/optimizer/state");
    let backend = SledStateBackend::open(config).await?;

    backend.put(b"window:123", b"state").await?;
    backend.flush().await?;  // Persist to disk

    Ok(())
}
```

### With Checkpointing

```rust
use processor::state::{CheckpointCoordinator, CheckpointOptions, MemoryStateBackend};
use std::sync::Arc;
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let backend = Arc::new(MemoryStateBackend::new(None));

    let options = CheckpointOptions {
        checkpoint_dir: "/var/lib/optimizer/checkpoints".into(),
        max_checkpoints: 10,
        compress: true,
        min_interval: Duration::from_secs(60),
    };

    let coordinator = CheckpointCoordinator::new(
        backend.clone(),
        Duration::from_secs(300),  // Checkpoint every 5 min
        options,
    );

    coordinator.start().await?;

    // State is now automatically checkpointed every 5 minutes

    coordinator.shutdown().await?;
    Ok(())
}
```

## Features

- **Multiple Backends**: In-memory (DashMap) and persistent (Sled)
- **TTL Support**: Automatic expiration of old state
- **Checkpointing**: Periodic snapshots for fault tolerance
- **Async-First**: All operations are non-blocking
- **Thread-Safe**: Concurrent access from multiple tasks
- **Statistics**: Track hit rates, operation counts, memory usage
- **Production-Ready**: Comprehensive error handling and testing

## Architecture

```
StateBackend (trait)
├── MemoryStateBackend   - In-memory, lock-free, optional TTL
└── SledStateBackend     - Persistent, ACID, compression

CheckpointCoordinator    - Periodic snapshots, retention policy
```

## API Reference

### StateBackend Trait

All backends implement this core interface:

```rust
#[async_trait]
pub trait StateBackend: Send + Sync {
    async fn get(&self, key: &[u8]) -> StateResult<Option<Vec<u8>>>;
    async fn put(&self, key: &[u8], value: &[u8]) -> StateResult<()>;
    async fn delete(&self, key: &[u8]) -> StateResult<()>;
    async fn list_keys(&self, prefix: &[u8]) -> StateResult<Vec<Vec<u8>>>;
}
```

### MemoryStateBackend

High-performance in-memory storage:

```rust
// No TTL - entries never expire
let backend = MemoryStateBackend::new(None);

// With TTL - entries expire after 1 hour
let backend = MemoryStateBackend::new(Some(Duration::from_secs(3600)));

// Get statistics
let stats = backend.stats().await;
println!("Hit rate: {:.2}%",
    100.0 * stats.hit_count as f64 / stats.get_count as f64);

// Manual cleanup of expired entries
let removed = backend.cleanup_expired().await;

// Automatic cleanup every 5 minutes
let _task = backend.start_cleanup_task(Duration::from_secs(300));
```

### SledStateBackend

Persistent embedded database:

```rust
let config = SledConfig::new("/var/lib/state")
    .with_cache_capacity(256 * 1024 * 1024)  // 256MB cache
    .with_compression(true)
    .with_flush_every(1000);  // Flush every 1000 ops

let backend = SledStateBackend::open(config).await?;

// Flush to ensure durability
backend.flush().await?;

// Get database size
let size = backend.size_on_disk().await?;

// Create checkpoint
let checkpoint = backend.checkpoint().await?;

// Restore from checkpoint
backend.restore(checkpoint).await?;
```

### CheckpointCoordinator

Automated state snapshots:

```rust
let coordinator = CheckpointCoordinator::new(
    backend.clone(),
    Duration::from_secs(300),  // Checkpoint interval
    options,
);

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

// Manual checkpoint
let checkpoint_id = coordinator.create_checkpoint().await?;

// Restore from latest
coordinator.restore_latest().await?;

// Get statistics
let stats = coordinator.stats().await;

// Shutdown with final checkpoint
coordinator.shutdown().await?;
```

## Performance

### Memory Backend

- **Get**: ~100ns (p50), ~500ns (p99)
- **Put**: ~150ns (p50), ~800ns (p99)
- **Throughput**: 6-10M ops/sec per core

### Sled Backend

- **Get (cached)**: ~200ns (p50), ~1μs (p99)
- **Get (disk)**: ~50μs (p50), ~200μs (p99)
- **Put**: ~10μs (p50), ~100μs (p99)
- **Throughput**: 100K writes/sec, 5M cached reads/sec

### Checkpointing

| State Size  | Checkpoint | Restore | Disk (compressed) |
|-------------|-----------|---------|-------------------|
| 1K entries  | 5ms       | 3ms     | 50 KB             |
| 100K entries| 500ms     | 300ms   | 5 MB              |
| 1M entries  | 5s        | 3s      | 50 MB             |

## Testing

Run unit tests:
```bash
cargo test -p processor --lib state::
```

Run integration tests:
```bash
cargo test -p processor --test state_integration_tests
```

Run demo:
```bash
cargo run --example state_demo -p processor
```

## Production Deployment

### Recommended Setup

**Stateless (ephemeral state):**
```rust
let backend = Arc::new(MemoryStateBackend::new(Some(Duration::from_secs(3600))));
let _cleanup = backend.start_cleanup_task(Duration::from_secs(600));
```

**Stateful (persistent state):**
```rust
let backend = Arc::new(SledStateBackend::open(config).await?);

let coordinator = CheckpointCoordinator::new(
    backend.clone(),
    Duration::from_secs(3600),  // Hourly checkpoints
    CheckpointOptions {
        checkpoint_dir: "/var/lib/checkpoints".into(),
        max_checkpoints: 24,  // 24 hours
        compress: true,
        min_interval: Duration::from_secs(300),
    },
);

coordinator.start().await?;
```

### Monitoring

Key metrics to track:

1. **Hit Rate**: `hit_count / (hit_count + miss_count)`
2. **Memory Usage**: `memory_usage()`
3. **Checkpoint Success Rate**: `checkpoints_created / (checkpoints_created + checkpoint_failures)`
4. **Restore Time**: `last_checkpoint_duration_ms`

## Error Handling

All operations return `StateResult<T>`:

```rust
match backend.get(b"key").await {
    Ok(Some(value)) => println!("Found: {:?}", value),
    Ok(None) => println!("Not found"),
    Err(StateError::StorageError { details, .. }) => {
        eprintln!("Storage error: {}", details);
    }
    Err(e) => eprintln!("Error: {}", e),
}
```

## License

Apache-2.0

## See Also

- [Full Implementation Summary]../STATE_MODULE_SUMMARY.md
- [Example Program]../../examples/state_demo.rs
- [Integration Tests]../../tests/state_integration_tests.rs