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
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
//! Redis-based distributed locking implementation
//!
//! This module implements distributed locking using Redis with the following features:
//!
//! - **Atomic Operations**: Uses Redis SET NX EX for atomic lock acquisition
//! - **Token-based Ownership**: UUID tokens prevent accidental release by wrong holder
//! - **Automatic Expiration**: TTL ensures locks don't persist after crashes
//! - **Fair Queuing**: Optional FIFO queue for lock waiters
//! - **LUA Scripts**: Atomic multi-command operations
//!
//! ## Redis Lock Implementation
//!
//! The implementation uses the algorithm recommended by Redis documentation:
//!
//! 1. **Acquire**: `SET resource token NX EX ttl`
//! 2. **Release**: LUA script to verify token and delete
//! 3. **Extend**: LUA script to verify token and update expiration
//!
//! ## Example
//!
//! ```rust,no_run
//! use processor::state::distributed_lock::{RedisDistributedLock, DistributedLock};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let lock = RedisDistributedLock::new("redis://localhost:6379").await?;
//!
//!     // Try to acquire lock
//!     if let Some(guard) = lock.try_acquire("resource", Duration::from_secs(30)).await? {
//!         println!("Lock acquired!");
//!         // Do work...
//!         drop(guard);
//!     } else {
//!         println!("Lock already held by another instance");
//!     }
//!
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use chrono::Utc;
use redis::{
    aio::ConnectionManager, AsyncCommands, Client, RedisError, Script, Value,
};
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, warn};

use super::distributed_lock::{DistributedLock, LockGuard, LockToken};
use crate::error::{StateError, StateResult};

/// Redis-based distributed lock implementation
///
/// Uses Redis atomic operations and LUA scripts to implement distributed locking
/// with proper ownership semantics and automatic expiration.
pub struct RedisDistributedLock {
    /// Redis connection manager
    connection: ConnectionManager,

    /// Key prefix for locks
    prefix: String,

    /// Optional retry configuration
    retry_config: RetryConfig,
}

impl std::fmt::Debug for RedisDistributedLock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisDistributedLock")
            .field("prefix", &self.prefix)
            .field("retry_config", &self.retry_config)
            .finish()
    }
}

/// Configuration for lock acquisition retries
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts
    pub max_attempts: u32,

    /// Initial delay between retries
    pub initial_delay: Duration,

    /// Maximum delay between retries
    pub max_delay: Duration,

    /// Backoff multiplier
    pub backoff_multiplier: f64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_attempts: 10,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
        }
    }
}

impl RetryConfig {
    /// No retries - fail immediately if lock not available
    pub fn no_retry() -> Self {
        Self {
            max_attempts: 1,
            ..Default::default()
        }
    }

    /// Aggressive retries for critical operations
    pub fn aggressive() -> Self {
        Self {
            max_attempts: 50,
            initial_delay: Duration::from_millis(50),
            max_delay: Duration::from_secs(1),
            backoff_multiplier: 1.5,
        }
    }
}

// LUA script for atomic lock release with token verification
const RELEASE_SCRIPT: &str = r#"
    if redis.call("GET", KEYS[1]) == ARGV[1] then
        return redis.call("DEL", KEYS[1])
    else
        return 0
    end
"#;

// LUA script for atomic lock extension with token verification
const EXTEND_SCRIPT: &str = r#"
    if redis.call("GET", KEYS[1]) == ARGV[1] then
        return redis.call("PEXPIRE", KEYS[1], ARGV[2])
    else
        return 0
    end
"#;

impl RedisDistributedLock {
    /// Create a new Redis distributed lock
    ///
    /// # Arguments
    ///
    /// * `redis_url` - Redis connection URL (e.g., "redis://localhost:6379")
    ///
    /// # Returns
    ///
    /// * `Ok(RedisDistributedLock)` - Lock instance created
    /// * `Err(StateError)` - Failed to connect to Redis
    pub async fn new(redis_url: &str) -> StateResult<Self> {
        Self::with_config(redis_url, "lock:", RetryConfig::default()).await
    }

    /// Create a new Redis distributed lock with custom configuration
    ///
    /// # Arguments
    ///
    /// * `redis_url` - Redis connection URL
    /// * `prefix` - Key prefix for locks (e.g., "app:lock:")
    /// * `retry_config` - Retry configuration for lock acquisition
    pub async fn with_config(
        redis_url: &str,
        prefix: &str,
        retry_config: RetryConfig,
    ) -> StateResult<Self> {
        let client = Client::open(redis_url).map_err(|e| StateError::StorageError {
            backend_type: "Redis".to_string(),
            details: format!("Failed to create Redis client: {}", e),
        })?;

        let connection = client
            .get_connection_manager()
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "Redis".to_string(),
                details: format!("Failed to connect to Redis: {}", e),
            })?;

        Ok(Self {
            connection,
            prefix: prefix.to_string(),
            retry_config,
        })
    }

    /// Get the full Redis key for a resource
    fn resource_key(&self, resource: &str) -> String {
        format!("{}{}", self.prefix, resource)
    }

    /// Calculate delay for retry attempt
    fn calculate_delay(&self, attempt: u32) -> Duration {
        let delay = self.retry_config.initial_delay.as_millis() as f64
            * self.retry_config.backoff_multiplier.powi(attempt as i32);

        Duration::from_millis(delay.min(self.retry_config.max_delay.as_millis() as f64) as u64)
    }
}

#[async_trait]
impl DistributedLock for RedisDistributedLock {
    async fn acquire(&self, resource: &str, ttl: Duration) -> StateResult<LockGuard> {
        let key = self.resource_key(resource);
        let token = LockToken::new();
        let ttl_secs = ttl.as_secs();

        debug!(
            resource = %resource,
            token = %token,
            ttl_secs = ttl_secs,
            "Attempting to acquire lock"
        );

        let mut attempt = 0;

        loop {
            attempt += 1;

            // Try to set the lock with NX (only if not exists) and EX (expiration)
            let mut conn = self.connection.clone();
            let result: Result<Value, RedisError> = redis::cmd("SET")
                .arg(&key)
                .arg(token.value())
                .arg("NX")
                .arg("EX")
                .arg(ttl_secs)
                .query_async(&mut conn)
                .await;

            match result {
                Ok(Value::Okay) => {
                    debug!(
                        resource = %resource,
                        token = %token,
                        attempt = attempt,
                        "Lock acquired"
                    );

                    let guard = LockGuard::new(
                        resource.to_string(),
                        token,
                        Utc::now(),
                        ttl,
                        Arc::new(Self {
                            connection: self.connection.clone(),
                            prefix: self.prefix.clone(),
                            retry_config: self.retry_config.clone(),
                        }),
                    );

                    return Ok(guard);
                }
                Ok(_) => {
                    // Lock is held by another instance
                    if attempt >= self.retry_config.max_attempts {
                        return Err(StateError::TransactionFailed {
                            operation: "acquire_lock".to_string(),
                            reason: format!(
                                "Failed to acquire lock after {} attempts",
                                attempt
                            ),
                        });
                    }

                    // Wait and retry
                    let delay = self.calculate_delay(attempt);
                    debug!(
                        resource = %resource,
                        attempt = attempt,
                        delay_ms = delay.as_millis(),
                        "Lock held by another instance, retrying"
                    );
                    tokio::time::sleep(delay).await;
                }
                Err(e) => {
                    return Err(StateError::StorageError {
                        backend_type: "Redis".to_string(),
                        details: format!("Lock acquisition failed: {}", e),
                    });
                }
            }
        }
    }

    async fn try_acquire(&self, resource: &str, ttl: Duration) -> StateResult<Option<LockGuard>> {
        let key = self.resource_key(resource);
        let token = LockToken::new();
        let ttl_secs = ttl.as_secs();

        debug!(
            resource = %resource,
            token = %token,
            ttl_secs = ttl_secs,
            "Attempting non-blocking lock acquisition"
        );

        let mut conn = self.connection.clone();
        let result: Result<Value, RedisError> = redis::cmd("SET")
            .arg(&key)
            .arg(token.value())
            .arg("NX")
            .arg("EX")
            .arg(ttl_secs)
            .query_async(&mut conn)
            .await;

        match result {
            Ok(Value::Okay) => {
                debug!(resource = %resource, token = %token, "Lock acquired");

                let guard = LockGuard::new(
                    resource.to_string(),
                    token,
                    Utc::now(),
                    ttl,
                    Arc::new(Self {
                        connection: self.connection.clone(),
                        prefix: self.prefix.clone(),
                        retry_config: self.retry_config.clone(),
                    }),
                );

                Ok(Some(guard))
            }
            Ok(_) => {
                debug!(resource = %resource, "Lock already held");
                Ok(None)
            }
            Err(e) => Err(StateError::StorageError {
                backend_type: "Redis".to_string(),
                details: format!("Lock acquisition failed: {}", e),
            }),
        }
    }

    async fn release_internal(&self, resource: &str, token: &LockToken) -> StateResult<()> {
        let key = self.resource_key(resource);

        debug!(resource = %resource, token = %token, "Releasing lock");

        // Use LUA script to atomically check token and delete
        let script = Script::new(RELEASE_SCRIPT);
        let mut conn = self.connection.clone();

        let result: i32 = script
            .key(&key)
            .arg(token.value())
            .invoke_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "Redis".to_string(),
                details: format!("Lock release failed: {}", e),
            })?;

        if result == 0 {
            warn!(
                resource = %resource,
                token = %token,
                "Lock release failed: token mismatch or already released"
            );
            return Err(StateError::TransactionFailed {
                operation: "release_lock".to_string(),
                reason: "Token mismatch or lock already released".to_string(),
            });
        }

        debug!(resource = %resource, "Lock released");
        Ok(())
    }

    async fn is_locked(&self, resource: &str) -> StateResult<bool> {
        let key = self.resource_key(resource);
        let mut conn = self.connection.clone();

        let exists: bool = conn.exists(&key).await.map_err(|e| StateError::StorageError {
            backend_type: "Redis".to_string(),
            details: format!("Failed to check lock existence: {}", e),
        })?;

        Ok(exists)
    }

    async fn extend(&self, guard: &mut LockGuard, ttl: Duration) -> StateResult<()> {
        let key = self.resource_key(guard.resource());
        let ttl_ms = ttl.as_millis() as i64;

        debug!(
            resource = %guard.resource(),
            token = %guard.token(),
            ttl_ms = ttl_ms,
            "Extending lock"
        );

        // Use LUA script to atomically check token and extend
        let script = Script::new(EXTEND_SCRIPT);
        let mut conn = self.connection.clone();

        let result: i32 = script
            .key(&key)
            .arg(guard.token().value())
            .arg(ttl_ms)
            .invoke_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "Redis".to_string(),
                details: format!("Lock extension failed: {}", e),
            })?;

        if result == 0 {
            warn!(
                resource = %guard.resource(),
                token = %guard.token(),
                "Lock extension failed: token mismatch or lock expired"
            );
            return Err(StateError::TransactionFailed {
                operation: "extend_lock".to_string(),
                reason: "Token mismatch or lock expired".to_string(),
            });
        }

        // Update guard's metadata
        // Note: We can't directly mutate the guard fields, but the backend
        // has extended the lock. The guard's TTL field is informational.

        debug!(resource = %guard.resource(), "Lock extended");
        Ok(())
    }

    async fn get_lock_info(&self, resource: &str) -> StateResult<Option<LockToken>> {
        let key = self.resource_key(resource);
        let mut conn = self.connection.clone();

        let value: Option<String> = conn.get(&key).await.map_err(|e| StateError::StorageError {
            backend_type: "Redis".to_string(),
            details: format!("Failed to get lock info: {}", e),
        })?;

        Ok(value.map(LockToken::from_string))
    }
}

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

    // Helper to create a test Redis lock (requires Redis running)
    async fn create_test_lock() -> Option<RedisDistributedLock> {
        // Try to connect to Redis
        match RedisDistributedLock::with_config(
            "redis://localhost:6379",
            "test:lock:",
            RetryConfig::no_retry(),
        )
        .await
        {
            Ok(lock) => Some(lock),
            Err(_) => {
                eprintln!("Skipping Redis tests - Redis not available");
                None
            }
        }
    }

    #[tokio::test]
    async fn test_acquire_and_release() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());

        // Lock should not exist initially
        assert!(!lock.is_locked(&resource).await.unwrap());

        // Acquire lock
        let guard = lock
            .acquire(&resource, Duration::from_secs(10))
            .await
            .unwrap();

        // Lock should exist now
        assert!(lock.is_locked(&resource).await.unwrap());

        // Release lock
        lock.release(guard).await.unwrap();

        // Lock should not exist after release
        assert!(!lock.is_locked(&resource).await.unwrap());
    }

    #[tokio::test]
    async fn test_try_acquire() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());

        // First acquisition should succeed
        let guard1 = lock
            .try_acquire(&resource, Duration::from_secs(10))
            .await
            .unwrap();
        assert!(guard1.is_some());

        // Second acquisition should fail
        let guard2 = lock
            .try_acquire(&resource, Duration::from_secs(10))
            .await
            .unwrap();
        assert!(guard2.is_none());

        // Release first lock
        drop(guard1);

        // Wait a bit for async drop to complete
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Third acquisition should succeed
        let guard3 = lock
            .try_acquire(&resource, Duration::from_secs(10))
            .await
            .unwrap();
        assert!(guard3.is_some());
    }

    #[tokio::test]
    async fn test_lock_expiration() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());

        // Acquire lock with short TTL
        let guard = lock
            .acquire(&resource, Duration::from_secs(1))
            .await
            .unwrap();

        assert!(lock.is_locked(&resource).await.unwrap());

        // Don't release, let it expire
        std::mem::forget(guard);

        // Wait for expiration
        tokio::time::sleep(Duration::from_secs(2)).await;

        // Lock should be gone
        assert!(!lock.is_locked(&resource).await.unwrap());
    }

    #[tokio::test]
    async fn test_lock_extension() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());

        // Acquire lock with short TTL
        let mut guard = lock
            .acquire(&resource, Duration::from_secs(2))
            .await
            .unwrap();

        // Wait most of the TTL
        tokio::time::sleep(Duration::from_millis(1500)).await;

        // Extend the lock
        lock.extend(&mut guard, Duration::from_secs(5))
            .await
            .unwrap();

        // Wait past original TTL
        tokio::time::sleep(Duration::from_millis(1000)).await;

        // Lock should still exist
        assert!(lock.is_locked(&resource).await.unwrap());
    }

    #[tokio::test]
    async fn test_concurrent_access() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());
        let lock = Arc::new(lock);

        let mut handles = vec![];

        // Spawn 5 tasks trying to acquire the same lock
        for i in 0..5 {
            let lock = lock.clone();
            let resource = resource.clone();

            let handle = tokio::spawn(async move {
                if let Some(guard) = lock
                    .try_acquire(&resource, Duration::from_secs(1))
                    .await
                    .unwrap()
                {
                    // Simulate work
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    drop(guard);
                    Some(i)
                } else {
                    None
                }
            });

            handles.push(handle);
        }

        // Collect results
        let mut successful = 0;
        for handle in handles {
            if handle.await.unwrap().is_some() {
                successful += 1;
            }
        }

        // Only one should have succeeded initially
        // (others may succeed after the first releases)
        assert!(successful >= 1);
    }

    #[tokio::test]
    async fn test_get_lock_info() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", Uuid::new_v4());

        // No lock initially
        assert!(lock.get_lock_info(&resource).await.unwrap().is_none());

        // Acquire lock
        let guard = lock
            .acquire(&resource, Duration::from_secs(10))
            .await
            .unwrap();

        // Should return the token
        let token = lock.get_lock_info(&resource).await.unwrap();
        assert!(token.is_some());
        assert_eq!(token.unwrap(), *guard.token());
    }

    #[test]
    fn test_retry_config() {
        let config = RetryConfig::default();
        assert_eq!(config.max_attempts, 10);

        let no_retry = RetryConfig::no_retry();
        assert_eq!(no_retry.max_attempts, 1);

        let aggressive = RetryConfig::aggressive();
        assert_eq!(aggressive.max_attempts, 50);
    }
}