llm-optimizer-processor 0.1.0

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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# Distributed State Quick Start Guide

This guide will help you get started with distributed state backends quickly.

## Table of Contents

- [Prerequisites]#prerequisites
- [Quick Start]#quick-start
- [Configuration Examples]#configuration-examples
- [Running Tests]#running-tests
- [Running Examples]#running-examples
- [Common Patterns]#common-patterns
- [Troubleshooting]#troubleshooting
- [Next Steps]#next-steps

## Prerequisites

### Required Services

For distributed state backends, you need to run the required services:

```bash
# Start Redis and PostgreSQL using Docker Compose
docker-compose up -d redis postgres

# Verify services are running
docker-compose ps

# Check Redis
redis-cli ping
# Expected output: PONG

# Check PostgreSQL
psql -h localhost -U optimizer -d optimizer -c "SELECT 1;"
# Expected output: 1
```

### Optional Monitoring Services

```bash
# Start monitoring services
docker-compose up -d redis-commander pgadmin

# Access UIs
# Redis Commander: http://localhost:8081
# pgAdmin: http://localhost:5050
```

## Quick Start

### 1. Memory Backend (No External Dependencies)

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

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create backend with 1-hour TTL
    let backend = MemoryStateBackend::new(Some(Duration::from_secs(3600)));

    // Store data
    backend.put(b"user:1001", b"Alice").await?;

    // Retrieve data
    if let Some(value) = backend.get(b"user:1001").await? {
        println!("Value: {}", String::from_utf8_lossy(&value));
    }

    Ok(())
}
```

**When to use**: Single instance, development, testing

### 2. Redis Backend (Distributed Cache)

```rust
use processor::state::{RedisConfig, RedisStateBackend, StateBackend};
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Configure Redis
    let config = RedisConfig::builder()
        .url("redis://localhost:6379")
        .key_prefix("myapp:")
        .default_ttl(Duration::from_secs(3600))
        .build()?;

    // Create backend
    let backend = RedisStateBackend::new(config).await?;

    // Health check
    assert!(backend.health_check().await?);

    // Store data
    backend.put(b"session:abc", b"session_data").await?;

    // Retrieve data
    if let Some(value) = backend.get(b"session:abc").await? {
        println!("Session: {}", String::from_utf8_lossy(&value));
    }

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

    Ok(())
}
```

**When to use**: Multi-instance, fast distributed cache, session state

### 3. PostgreSQL Backend (Persistent Storage)

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

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create backend
    let backend = PostgresStateBackend::new(
        "postgres://optimizer:password@localhost:5432/optimizer",
        "my_state_table"
    ).await?;

    // Health check
    assert!(backend.health_check().await?);

    // Store data
    backend.put(b"config:version", b"1.0.0").await?;

    // Retrieve data
    if let Some(value) = backend.get(b"config:version").await? {
        println!("Version: {}", String::from_utf8_lossy(&value));
    }

    // Get statistics
    let stats = backend.stats().await?;
    println!("Total keys: {}", stats.total_keys);

    Ok(())
}
```

**When to use**: Persistent storage, ACID transactions, audit logs

### 4. Tiered Cache (Memory + Redis + PostgreSQL)

```rust
use processor::state::{
    CachedStateBackend, MemoryStateBackend,
    RedisConfig, RedisStateBackend,
    PostgresStateBackend, StateBackend,
};
use std::sync::Arc;
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // L3: PostgreSQL (persistent)
    let postgres = Arc::new(
        PostgresStateBackend::new(
            "postgres://optimizer:password@localhost:5432/optimizer",
            "state"
        ).await?
    );

    // L2: Redis (distributed cache)
    let redis_config = RedisConfig::builder()
        .url("redis://localhost:6379")
        .key_prefix("cache:")
        .default_ttl(Duration::from_secs(300))
        .build()?;
    let redis = Arc::new(RedisStateBackend::new(redis_config).await?);

    // L1: Memory cache on top of Redis
    let cached = CachedStateBackend::new(
        redis.clone(),
        1000,                          // 1000 items max
        Some(Duration::from_secs(60))  // 1-minute cache
    );

    // Use the cached backend
    cached.put(b"key", b"value").await?;
    let value = cached.get(b"key").await?;

    // Check cache performance
    let stats = cached.cache_stats().await;
    println!("Hit rate: {:.2}%", stats.hit_rate * 100.0);

    Ok(())
}
```

**When to use**: Production deployments, optimal performance

### 5. Distributed Lock

```rust
use processor::state::{RedisLock, DistributedLock};
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let lock = RedisLock::new(
        "redis://localhost:6379",
        "my_resource_lock"
    ).await?;

    // Try to acquire lock
    if lock.try_lock(Duration::from_secs(30)).await? {
        println!("Lock acquired!");

        // Critical section
        // ... do work ...

        // Release lock
        lock.unlock().await?;
        println!("Lock released!");
    } else {
        println!("Could not acquire lock");
    }

    Ok(())
}
```

**When to use**: Leader election, distributed coordination, critical sections

## Configuration Examples

### Environment-Based Configuration

```rust
use std::env;

fn get_redis_config() -> anyhow::Result<RedisConfig> {
    let url = env::var("REDIS_URL")
        .unwrap_or_else(|_| "redis://localhost:6379".to_string());

    let prefix = env::var("REDIS_KEY_PREFIX")
        .unwrap_or_else(|_| "app:".to_string());

    let ttl_secs = env::var("REDIS_TTL_SECONDS")
        .unwrap_or_else(|_| "3600".to_string())
        .parse::<u64>()?;

    RedisConfig::builder()
        .url(url)
        .key_prefix(prefix)
        .default_ttl(Duration::from_secs(ttl_secs))
        .build()
}
```

### YAML Configuration

```yaml
# config.yaml
state:
  backend: redis
  redis:
    url: redis://localhost:6379
    key_prefix: optimizer:
    default_ttl_seconds: 3600
    pool:
      min_connections: 5
      max_connections: 50
    timeouts:
      connect_ms: 5000
      read_ms: 3000
      write_ms: 3000
  postgres:
    url: postgres://optimizer:password@localhost:5432/optimizer
    table_name: processor_state
    pool:
      max_connections: 20
```

Load configuration:

```rust
use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    state: StateConfig,
}

#[derive(Deserialize)]
struct StateConfig {
    backend: String,
    redis: Option<RedisSettings>,
    postgres: Option<PostgresSettings>,
}

fn load_config() -> anyhow::Result<Config> {
    let config_str = std::fs::read_to_string("config.yaml")?;
    Ok(serde_yaml::from_str(&config_str)?)
}
```

### Production Configuration (Redis)

```rust
let config = RedisConfig::builder()
    .url("redis://redis.production.svc.cluster.local:6379")
    .key_prefix("llm-optimizer:")
    .default_ttl(Duration::from_secs(3600))
    .pool_size(10, 100)
    .timeouts(
        Duration::from_secs(5),   // connect
        Duration::from_secs(3),   // read
        Duration::from_secs(3),   // write
    )
    .retries(3, Duration::from_millis(100), Duration::from_secs(5))
    .cluster_mode(false)
    .tls(true)
    .build()?;
```

### Production Configuration (PostgreSQL)

```rust
let database_url = format!(
    "postgres://{}:{}@{}:{}/{}",
    env::var("POSTGRES_USER")?,
    env::var("POSTGRES_PASSWORD")?,
    env::var("POSTGRES_HOST")?,
    env::var("POSTGRES_PORT")?,
    env::var("POSTGRES_DB")?,
);

let backend = PostgresStateBackend::new(
    &database_url,
    "processor_state"
).await?;
```

## Running Tests

### Unit Tests

```bash
# Run all tests
cargo test --package processor

# Run specific test file
cargo test --package processor --test state_integration_tests
```

### Integration Tests (Require Services)

```bash
# Start services
docker-compose up -d redis postgres

# Run integration tests
cargo test --package processor --test distributed_state_tests -- --ignored

# Run specific test
cargo test --package processor --test distributed_state_tests test_redis_basic_operations -- --ignored

# Run with single thread (for lock tests)
cargo test --package processor --test distributed_state_tests -- --ignored --test-threads=1

# Run with verbose output
cargo test --package processor --test distributed_state_tests -- --ignored --nocapture
```

### Performance Benchmarks

```bash
# Run benchmark tests
cargo test --package processor --test distributed_state_tests bench_ -- --ignored --nocapture

# Example: Redis write throughput
cargo test --package processor --test distributed_state_tests bench_redis_write_throughput -- --ignored --nocapture
```

## Running Examples

### List Available Examples

```bash
ls crates/processor/examples/
```

### Run Examples

```bash
# Start services
docker-compose up -d redis postgres

# Redis state demo
cargo run --package processor --example redis_state_demo

# PostgreSQL state demo
cargo run --package processor --example postgres_state_demo

# Distributed lock demo
cargo run --package processor --example distributed_lock_demo

# Tiered cache demo
cargo run --package processor --example tiered_cache_demo

# Run multiple instances of lock demo (in separate terminals)
cargo run --package processor --example distributed_lock_demo
```

## Common Patterns

### Pattern 1: Cache-Aside

```rust
async fn get_user(
    cache: &impl StateBackend,
    db: &impl StateBackend,
    user_id: u64,
) -> anyhow::Result<Option<User>> {
    let key = format!("user:{}", user_id).into_bytes();

    // Try cache first
    if let Some(cached) = cache.get(&key).await? {
        return Ok(Some(serde_json::from_slice(&cached)?));
    }

    // Fetch from database
    if let Some(data) = db.get(&key).await? {
        // Update cache
        cache.put_with_ttl(&key, &data, Duration::from_secs(3600)).await?;
        return Ok(Some(serde_json::from_slice(&data)?));
    }

    Ok(None)
}
```

### Pattern 2: Write-Through Cache

```rust
async fn save_user(
    cache: &impl StateBackend,
    db: &impl StateBackend,
    user: &User,
) -> anyhow::Result<()> {
    let key = format!("user:{}", user.id).into_bytes();
    let data = serde_json::to_vec(user)?;

    // Write to database
    db.put(&key, &data).await?;

    // Update cache
    cache.put_with_ttl(&key, &data, Duration::from_secs(3600)).await?;

    Ok(())
}
```

### Pattern 3: Distributed Lock with Work

```rust
async fn process_with_lock(
    lock: &RedisLock,
    work: impl Future<Output = anyhow::Result<()>>,
) -> anyhow::Result<()> {
    lock.with_lock(Duration::from_secs(30), || async {
        work.await
    }).await
}
```

### Pattern 4: Leader Election

```rust
async fn leader_election_loop(
    lock: &RedisLock,
    leader_work: impl Fn() -> anyhow::Result<()>,
) -> anyhow::Result<()> {
    loop {
        if lock.try_lock(Duration::from_secs(10)).await? {
            println!("I am the leader!");

            // Do leader work
            leader_work()?;

            // Sleep before renewing
            tokio::time::sleep(Duration::from_secs(5)).await;

            // Renew lock
            lock.renew(Duration::from_secs(10)).await?;
        } else {
            println!("I am a follower");
            tokio::time::sleep(Duration::from_secs(5)).await;
        }
    }
}
```

### Pattern 5: Batch Operations

```rust
async fn batch_save_users(
    backend: &RedisStateBackend,
    users: Vec<User>,
) -> anyhow::Result<()> {
    // Prepare batch
    let pairs: Vec<(Vec<u8>, Vec<u8>)> = users
        .iter()
        .map(|user| {
            let key = format!("user:{}", user.id).into_bytes();
            let value = serde_json::to_vec(user).unwrap();
            (key, value)
        })
        .collect();

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

    // Batch insert
    backend.batch_put(&pair_refs).await?;

    Ok(())
}
```

## Troubleshooting

### Issue: Cannot Connect to Redis

**Error**: `Connection refused` or `Failed to connect to Redis`

**Solution**:
```bash
# Check if Redis is running
docker-compose ps redis

# Start Redis
docker-compose up -d redis

# Check Redis logs
docker-compose logs redis

# Test connection
redis-cli ping

# If using custom port/host
redis-cli -h localhost -p 6379 ping
```

### Issue: Cannot Connect to PostgreSQL

**Error**: `Connection refused` or `Authentication failed`

**Solution**:
```bash
# Check if PostgreSQL is running
docker-compose ps postgres

# Start PostgreSQL
docker-compose up -d postgres

# Check PostgreSQL logs
docker-compose logs postgres

# Test connection
psql -h localhost -U optimizer -d optimizer

# Check credentials in docker-compose.yml
```

### Issue: Tests Failing

**Error**: `Some tests failed`

**Solution**:
```bash
# Ensure services are running
docker-compose up -d redis postgres

# Wait for services to be ready
sleep 5

# Clean up test data
redis-cli FLUSHDB
psql -h localhost -U optimizer -d optimizer -c "DROP TABLE IF EXISTS test_state;"

# Run tests again
cargo test --package processor --test distributed_state_tests -- --ignored
```

### Issue: Low Cache Hit Rate

**Symptoms**: Poor performance, many cache misses

**Diagnosis**:
```rust
let stats = backend.stats().await;
println!("Hit rate: {:.2}%", stats.hit_rate() * 100.0);
```

**Solution**:
- Increase cache size
- Increase TTL
- Warm up cache on startup
- Analyze access patterns

### Issue: Memory Pressure

**Symptoms**: OOM errors, high memory usage

**Solution**:
```rust
// Reduce cache size
let cached = CachedStateBackend::new(
    backend,
    500,  // Reduced from 1000
    Some(Duration::from_secs(30))  // Shorter TTL
);

// Or implement eviction
backend.clear().await?; // Periodic cleanup
```

### Issue: Connection Pool Exhausted

**Error**: `Too many connections`

**Solution**:
```rust
// Increase pool size
let config = RedisConfig::builder()
    .pool_size(20, 100)  // Increased from 10, 50
    .build()?;

// Or reduce connection lifetime
// Close connections promptly after use
```

## Next Steps

### Learn More

- Read the [Architecture Documentation]crates/processor/src/state/DISTRIBUTED_STATE.md
- Explore the [examples directory]crates/processor/examples/
- Review the [API documentation]https://docs.rs/processor

### Production Deployment

1. **Enable Monitoring**:
   ```rust
   // Implement metrics collection
   let stats = backend.stats().await;
   metrics::gauge!("state.hit_rate", stats.hit_rate());
   metrics::histogram!("state.latency_us", stats.avg_latency_us as f64);
   ```

2. **Configure Alerts**:
   - Low hit rate (< 70%)
   - High latency (> 10ms)
   - Connection pool exhaustion
   - Memory pressure

3. **Setup High Availability**:
   - Use Redis Cluster or Redis Sentinel
   - Configure PostgreSQL replication
   - Implement health checks
   - Setup automatic failover

4. **Performance Testing**:
   ```bash
   # Load testing with k6 or similar
   cargo build --release
   ./target/release/benchmark --backend redis --duration 60s
   ```

5. **Backup and Recovery**:
   ```bash
   # Redis backup
   redis-cli BGSAVE

   # PostgreSQL backup
   pg_dump -h localhost -U optimizer optimizer > backup.sql

   # Restore
   psql -h localhost -U optimizer optimizer < backup.sql
   ```

### Get Help

- GitHub Issues: https://github.com/llm-devops/llm-auto-optimizer/issues
- Discussions: https://github.com/llm-devops/llm-auto-optimizer/discussions
- Documentation: https://docs.rs/processor

---

## Quick Reference

### Service URLs

| Service         | URL                                        |
|----------------|---------------------------------------------|
| Redis          | redis://localhost:6379                      |
| PostgreSQL     | postgres://optimizer:password@localhost:5432/optimizer |
| Redis Commander| http://localhost:8081                       |
| pgAdmin        | http://localhost:5050                       |

### Common Commands

```bash
# Start all services
docker-compose up -d

# Stop all services
docker-compose down

# View logs
docker-compose logs -f [service]

# Restart service
docker-compose restart [service]

# Run tests
cargo test --package processor --test distributed_state_tests -- --ignored

# Run example
cargo run --package processor --example [example_name]

# Clean up
docker-compose down -v  # Remove volumes too
```

### Backend Selection

| Use Case                  | Recommended Backend     |
|---------------------------|------------------------|
| Development               | Memory                 |
| Single Instance           | Sled                   |
| Multi-Instance Cache      | Redis                  |
| Persistent Storage        | PostgreSQL             |
| Optimal Performance       | Memory + Redis + PostgreSQL |
| Distributed Coordination  | Redis Lock             |