Module distributed_lock

Module distributed_lock 

Source
Expand description

Distributed locking for coordinating state access across processor instances

This module provides distributed locking mechanisms to coordinate critical operations across multiple processor instances, preventing race conditions in distributed scenarios.

§Key Features

  • Multiple Backends: Redis and PostgreSQL implementations
  • RAII Pattern: Automatic lock release via LockGuard drop
  • Lock Extension: Extend TTL for long-running operations
  • Deadlock Detection: Automatic expiration via TTL
  • Fair Locking: FIFO queue for waiting locks (Redis)
  • Atomic Operations: LUA scripts and database transactions

§Use Cases

  • Checkpoint Coordination: Only one instance checkpoints at a time
  • State Compaction: Prevent concurrent compaction operations
  • Leader Election: Single coordinator for distributed tasks
  • Critical Section Protection: Serialize access to shared resources

§Example Usage

use processor::state::distributed_lock::{RedisDistributedLock, DistributedLock};
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create Redis-based distributed lock
    let lock = RedisDistributedLock::new("redis://localhost:6379").await?;

    // Acquire lock with 30-second TTL
    let guard = lock.acquire("checkpoint:state1", Duration::from_secs(30)).await?;

    // Perform critical operation
    println!("Performing checkpoint...");

    // Lock is automatically released when guard is dropped
    drop(guard);

    Ok(())
}

§Lock Extension

For long-running operations, extend the lock TTL:

let mut guard = lock.acquire("compaction:state1", Duration::from_secs(30)).await?;

// Start long-running operation
for i in 0..10 {
    // Do work...
    tokio::time::sleep(Duration::from_secs(10)).await;

    // Extend lock before it expires
    lock.extend(&mut guard, Duration::from_secs(30)).await?;
}

Structs§

LockGuard
RAII guard for distributed locks
LockToken
Lock ownership token

Traits§

DistributedLock
Distributed lock trait