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
669
670
//! PostgreSQL-based distributed locking implementation
//!
//! This module implements distributed locking using PostgreSQL advisory locks.
//! Advisory locks are a PostgreSQL feature that provides application-level locks
//! with excellent performance and automatic cleanup on connection termination.
//!
//! ## Features
//!
//! - **Advisory Locks**: Uses PostgreSQL's built-in advisory lock system
//! - **Session/Transaction Level**: Support for both session and transaction locks
//! - **Automatic Cleanup**: Locks automatically released on connection close
//! - **No External Dependencies**: Uses your existing PostgreSQL database
//! - **Deadlock Detection**: PostgreSQL handles deadlock detection automatically
//!
//! ## Lock Types
//!
//! PostgreSQL advisory locks come in two flavors:
//!
//! 1. **Session Locks**: Held until explicitly released or connection closes
//! 2. **Transaction Locks**: Automatically released at transaction end
//!
//! This implementation uses session locks for consistency with the trait API.
//!
//! ## Example
//!
//! ```rust,no_run
//! use processor::state::distributed_lock::{PostgresDistributedLock, DistributedLock};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let lock = PostgresDistributedLock::new("postgresql://localhost/mydb").await?;
//!
//!     // Acquire lock
//!     let guard = lock.acquire("checkpoint", Duration::from_secs(30)).await?;
//!
//!     // Do critical work
//!     println!("Performing checkpoint...");
//!
//!     // Lock automatically released on drop
//!     drop(guard);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Implementation Notes
//!
//! PostgreSQL advisory locks use a 64-bit integer key. We hash the resource name
//! to generate this key. While hash collisions are theoretically possible, they
//! are extremely rare with a good hash function (we use SipHash via the standard
//! library's hasher).

use async_trait::async_trait;
use chrono::Utc;
use sqlx::{postgres::PgPoolOptions, PgPool, Row};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, warn};

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

/// PostgreSQL-based distributed lock implementation
///
/// Uses PostgreSQL advisory locks for distributed coordination. Advisory locks
/// are fast, automatically cleaned up on connection termination, and support
/// both blocking and non-blocking acquisition.
pub struct PostgresDistributedLock {
    /// Database connection pool
    pool: PgPool,

    /// Lock metadata storage
    /// Maps resource name to (lock_id, token, expiry_time)
    metadata: Arc<RwLock<std::collections::HashMap<String, (i64, LockToken, i64)>>>,

    /// Background task handle for TTL monitoring
    _monitor_handle: Option<tokio::task::JoinHandle<()>>,
}

impl std::fmt::Debug for PostgresDistributedLock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PostgresDistributedLock")
            .field("metadata_count", &self.metadata.try_read().map(|m| m.len()))
            .finish()
    }
}

impl PostgresDistributedLock {
    /// Create a new PostgreSQL distributed lock
    ///
    /// # Arguments
    ///
    /// * `database_url` - PostgreSQL connection URL
    ///
    /// # Returns
    ///
    /// * `Ok(PostgresDistributedLock)` - Lock instance created
    /// * `Err(StateError)` - Failed to connect to database
    pub async fn new(database_url: &str) -> StateResult<Self> {
        Self::with_pool_size(database_url, 10).await
    }

    /// Create a new PostgreSQL distributed lock with custom pool size
    ///
    /// # Arguments
    ///
    /// * `database_url` - PostgreSQL connection URL
    /// * `max_connections` - Maximum number of connections in the pool
    pub async fn with_pool_size(database_url: &str, max_connections: u32) -> StateResult<Self> {
        let pool = PgPoolOptions::new()
            .max_connections(max_connections)
            .connect(database_url)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "PostgreSQL".to_string(),
                details: format!("Failed to connect to database: {}", e),
            })?;

        let metadata = Arc::new(RwLock::new(std::collections::HashMap::new()));

        // Start background task to monitor TTLs
        let monitor_handle = Self::start_ttl_monitor(metadata.clone(), pool.clone());

        Ok(Self {
            pool,
            metadata,
            _monitor_handle: Some(monitor_handle),
        })
    }

    /// Start background task to monitor and release expired locks
    fn start_ttl_monitor(
        metadata: Arc<RwLock<std::collections::HashMap<String, (i64, LockToken, i64)>>>,
        pool: PgPool,
    ) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(1));

            loop {
                interval.tick().await;

                let now = Utc::now().timestamp_millis();
                let mut metadata = metadata.write().await;
                let mut expired = Vec::new();

                // Find expired locks
                for (resource, (lock_id, token, expiry)) in metadata.iter() {
                    if *expiry <= now {
                        expired.push((resource.clone(), *lock_id, token.clone()));
                    }
                }

                // Release expired locks
                for (resource, lock_id, token) in expired {
                    debug!(
                        resource = %resource,
                        lock_id = lock_id,
                        token = %token,
                        "Releasing expired lock"
                    );

                    // Release the advisory lock
                    let result = sqlx::query("SELECT pg_advisory_unlock($1)")
                        .bind(lock_id)
                        .execute(&pool)
                        .await;

                    if let Err(e) = result {
                        warn!(
                            resource = %resource,
                            error = %e,
                            "Failed to release expired lock"
                        );
                    }

                    metadata.remove(&resource);
                }
            }
        })
    }

    /// Hash resource name to lock ID
    ///
    /// PostgreSQL advisory locks use a 64-bit integer. We hash the resource
    /// name to generate a consistent lock ID.
    fn resource_to_lock_id(resource: &str) -> i64 {
        let mut hasher = DefaultHasher::new();
        resource.hash(&mut hasher);
        hasher.finish() as i64
    }

    /// Calculate expiry timestamp from TTL
    fn calculate_expiry(ttl: Duration) -> i64 {
        Utc::now().timestamp_millis() + ttl.as_millis() as i64
    }
}

#[async_trait]
impl DistributedLock for PostgresDistributedLock {
    async fn acquire(&self, resource: &str, ttl: Duration) -> StateResult<LockGuard> {
        let lock_id = Self::resource_to_lock_id(resource);
        let token = LockToken::new();
        let expiry = Self::calculate_expiry(ttl);

        debug!(
            resource = %resource,
            lock_id = lock_id,
            token = %token,
            ttl_ms = ttl.as_millis(),
            "Attempting to acquire PostgreSQL advisory lock"
        );

        // Try to acquire the advisory lock (blocking)
        // pg_advisory_lock blocks until the lock is available
        let result = sqlx::query("SELECT pg_advisory_lock($1)")
            .bind(lock_id)
            .execute(&self.pool)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "PostgreSQL".to_string(),
                details: format!("Failed to acquire advisory lock: {}", e),
            })?;

        debug!(
            resource = %resource,
            lock_id = lock_id,
            "Advisory lock acquired"
        );

        // Store metadata
        {
            let mut metadata = self.metadata.write().await;
            metadata.insert(resource.to_string(), (lock_id, token.clone(), expiry));
        }

        let guard = LockGuard::new(
            resource.to_string(),
            token,
            Utc::now(),
            ttl,
            Arc::new(Self {
                pool: self.pool.clone(),
                metadata: self.metadata.clone(),
                _monitor_handle: None, // Don't duplicate monitor task
            }),
        );

        Ok(guard)
    }

    async fn try_acquire(&self, resource: &str, ttl: Duration) -> StateResult<Option<LockGuard>> {
        let lock_id = Self::resource_to_lock_id(resource);
        let token = LockToken::new();
        let expiry = Self::calculate_expiry(ttl);

        debug!(
            resource = %resource,
            lock_id = lock_id,
            token = %token,
            ttl_ms = ttl.as_millis(),
            "Attempting non-blocking lock acquisition"
        );

        // Try to acquire the advisory lock (non-blocking)
        // pg_try_advisory_lock returns immediately with true/false
        let row = sqlx::query("SELECT pg_try_advisory_lock($1) as acquired")
            .bind(lock_id)
            .fetch_one(&self.pool)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "PostgreSQL".to_string(),
                details: format!("Failed to try advisory lock: {}", e),
            })?;

        let acquired: bool = row.get("acquired");

        if acquired {
            debug!(
                resource = %resource,
                lock_id = lock_id,
                "Advisory lock acquired"
            );

            // Store metadata
            {
                let mut metadata = self.metadata.write().await;
                metadata.insert(resource.to_string(), (lock_id, token.clone(), expiry));
            }

            let guard = LockGuard::new(
                resource.to_string(),
                token,
                Utc::now(),
                ttl,
                Arc::new(Self {
                    pool: self.pool.clone(),
                    metadata: self.metadata.clone(),
                    _monitor_handle: None,
                }),
            );

            Ok(Some(guard))
        } else {
            debug!(resource = %resource, "Lock already held");
            Ok(None)
        }
    }

    async fn release_internal(&self, resource: &str, token: &LockToken) -> StateResult<()> {
        debug!(resource = %resource, token = %token, "Releasing lock");

        // Verify token and get lock ID
        let lock_id = {
            let mut metadata = self.metadata.write().await;

            match metadata.get(resource) {
                Some((lock_id, stored_token, _)) => {
                    if stored_token != token {
                        warn!(
                            resource = %resource,
                            token = %token,
                            "Token mismatch during release"
                        );
                        return Err(StateError::TransactionFailed {
                            operation: "release_lock".to_string(),
                            reason: "Token mismatch".to_string(),
                        });
                    }

                    let lock_id = *lock_id;
                    metadata.remove(resource);
                    lock_id
                }
                None => {
                    warn!(resource = %resource, "Lock not found in metadata");
                    return Err(StateError::KeyNotFound {
                        key: resource.to_string(),
                    });
                }
            }
        };

        // Release the advisory lock
        let row = sqlx::query("SELECT pg_advisory_unlock($1) as released")
            .bind(lock_id)
            .fetch_one(&self.pool)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "PostgreSQL".to_string(),
                details: format!("Failed to release advisory lock: {}", e),
            })?;

        let released: bool = row.get("released");

        if !released {
            warn!(
                resource = %resource,
                lock_id = lock_id,
                "Advisory lock was not held"
            );
            return Err(StateError::TransactionFailed {
                operation: "release_lock".to_string(),
                reason: "Lock was not held".to_string(),
            });
        }

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

    async fn is_locked(&self, resource: &str) -> StateResult<bool> {
        let metadata = self.metadata.read().await;
        let is_locked = metadata.contains_key(resource);

        // Also verify the lock hasn't expired
        if is_locked {
            if let Some((_, _, expiry)) = metadata.get(resource) {
                let now = Utc::now().timestamp_millis();
                return Ok(*expiry > now);
            }
        }

        Ok(is_locked)
    }

    async fn extend(&self, guard: &mut LockGuard, ttl: Duration) -> StateResult<()> {
        let new_expiry = Self::calculate_expiry(ttl);

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

        // Update metadata with new expiry
        {
            let mut metadata = self.metadata.write().await;

            match metadata.get_mut(guard.resource()) {
                Some((_, stored_token, expiry)) => {
                    if stored_token != guard.token() {
                        warn!(
                            resource = %guard.resource(),
                            "Token mismatch during extension"
                        );
                        return Err(StateError::TransactionFailed {
                            operation: "extend_lock".to_string(),
                            reason: "Token mismatch".to_string(),
                        });
                    }

                    *expiry = new_expiry;
                }
                None => {
                    warn!(resource = %guard.resource(), "Lock not found during extension");
                    return Err(StateError::KeyNotFound {
                        key: guard.resource().to_string(),
                    });
                }
            }
        }

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

    async fn get_lock_info(&self, resource: &str) -> StateResult<Option<LockToken>> {
        let metadata = self.metadata.read().await;

        if let Some((_, token, expiry)) = metadata.get(resource) {
            let now = Utc::now().timestamp_millis();
            if *expiry > now {
                return Ok(Some(token.clone()));
            }
        }

        Ok(None)
    }
}

impl Drop for PostgresDistributedLock {
    fn drop(&mut self) {
        // Monitor task will be automatically cancelled when handle is dropped
    }
}

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

    // Helper to create a test PostgreSQL lock (requires PostgreSQL running)
    async fn create_test_lock() -> Option<PostgresDistributedLock> {
        // Try to connect to PostgreSQL
        match PostgresDistributedLock::new("postgresql://localhost/test").await {
            Ok(lock) => Some(lock),
            Err(_) => {
                eprintln!("Skipping PostgreSQL tests - PostgreSQL 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::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();

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

        // 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::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_extension() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", uuid::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_lock_expiration() {
        let Some(lock) = create_test_lock().await else {
            return;
        };

        let resource = format!("test:resource:{}", uuid::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 + monitor cycle
        tokio::time::sleep(Duration::from_secs(3)).await;

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

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

        let resource = format!("test:resource:{}", uuid::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_resource_to_lock_id() {
        let id1 = PostgresDistributedLock::resource_to_lock_id("resource1");
        let id2 = PostgresDistributedLock::resource_to_lock_id("resource2");
        let id3 = PostgresDistributedLock::resource_to_lock_id("resource1");

        // Same resource should give same ID
        assert_eq!(id1, id3);

        // Different resources should give different IDs (with high probability)
        assert_ne!(id1, id2);
    }

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

        let resource = format!("test:resource:{}", uuid::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;
            }
        }

        // At least one should have succeeded
        assert!(successful >= 1);
    }
}