feat(lock): Optimize lock management performance in high-concurrency scenarios (#523)

Increase the size of the notification pool to reduce the thundering herd effect under high concurrency
Implement an adaptive timeout mechanism that dynamically adjusts based on system load and priority
Add a lock protection mechanism to prevent premature cleanup of active locks
Add lock acquisition methods for high-priority and critical-priority locks
Improve the cleanup strategy to be more conservative under high load
Add detailed debug logs to assist in diagnosing lock issues

Signed-off-by: junxiang Mu <1948535941@qq.com>
This commit is contained in:
guojidan
2025-09-12 18:17:07 +08:00
committed by GitHub
parent 124c31a68b
commit 9ce867f585
6 changed files with 1272 additions and 56 deletions
+68 -3
View File
@@ -27,7 +27,7 @@ use crate::fast_lock::{
/// High-performance object lock manager
#[derive(Debug)]
pub struct FastObjectLockManager {
shards: Vec<Arc<LockShard>>,
pub shards: Vec<Arc<LockShard>>,
shard_mask: usize,
config: LockConfig,
metrics: Arc<GlobalMetrics>,
@@ -66,7 +66,12 @@ impl FastObjectLockManager {
pub async fn acquire_lock(&self, request: ObjectLockRequest) -> Result<FastLockGuard, LockResult> {
let shard = self.get_shard(&request.key);
match shard.acquire_lock(&request).await {
Ok(()) => Ok(FastLockGuard::new(request.key, request.mode, request.owner, shard.clone())),
Ok(()) => {
let guard = FastLockGuard::new(request.key, request.mode, request.owner, shard.clone());
// Register guard to prevent premature cleanup
shard.register_guard(guard.guard_id());
Ok(guard)
}
Err(err) => Err(err),
}
}
@@ -117,6 +122,54 @@ impl FastObjectLockManager {
self.acquire_lock(request).await
}
/// Acquire high-priority read lock - optimized for database queries
pub async fn acquire_high_priority_read_lock(
&self,
bucket: impl Into<Arc<str>>,
object: impl Into<Arc<str>>,
owner: impl Into<Arc<str>>,
) -> Result<FastLockGuard, LockResult> {
let request =
ObjectLockRequest::new_read(bucket, object, owner).with_priority(crate::fast_lock::types::LockPriority::High);
self.acquire_lock(request).await
}
/// Acquire high-priority write lock - optimized for database queries
pub async fn acquire_high_priority_write_lock(
&self,
bucket: impl Into<Arc<str>>,
object: impl Into<Arc<str>>,
owner: impl Into<Arc<str>>,
) -> Result<FastLockGuard, LockResult> {
let request =
ObjectLockRequest::new_write(bucket, object, owner).with_priority(crate::fast_lock::types::LockPriority::High);
self.acquire_lock(request).await
}
/// Acquire critical priority read lock - for system operations
pub async fn acquire_critical_read_lock(
&self,
bucket: impl Into<Arc<str>>,
object: impl Into<Arc<str>>,
owner: impl Into<Arc<str>>,
) -> Result<FastLockGuard, LockResult> {
let request =
ObjectLockRequest::new_read(bucket, object, owner).with_priority(crate::fast_lock::types::LockPriority::Critical);
self.acquire_lock(request).await
}
/// Acquire critical priority write lock - for system operations
pub async fn acquire_critical_write_lock(
&self,
bucket: impl Into<Arc<str>>,
object: impl Into<Arc<str>>,
owner: impl Into<Arc<str>>,
) -> Result<FastLockGuard, LockResult> {
let request =
ObjectLockRequest::new_write(bucket, object, owner).with_priority(crate::fast_lock::types::LockPriority::Critical);
self.acquire_lock(request).await
}
/// Acquire multiple locks atomically - optimized version
pub async fn acquire_locks_batch(&self, batch_request: BatchLockRequest) -> BatchLockResult {
// Pre-sort requests by (shard_id, key) to avoid deadlocks
@@ -304,7 +357,7 @@ impl FastObjectLockManager {
}
/// Get shard for object key
fn get_shard(&self, key: &crate::fast_lock::types::ObjectKey) -> &Arc<LockShard> {
pub fn get_shard(&self, key: &crate::fast_lock::types::ObjectKey) -> &Arc<LockShard> {
let index = key.shard_index(self.shard_mask);
&self.shards[index]
}
@@ -362,6 +415,18 @@ impl Drop for FastObjectLockManager {
}
}
impl Clone for FastObjectLockManager {
fn clone(&self) -> Self {
Self {
shards: self.shards.clone(),
shard_mask: self.shard_mask,
config: self.config.clone(),
metrics: self.metrics.clone(),
cleanup_handle: RwLock::new(None), // Don't clone the cleanup task
}
}
}
#[async_trait::async_trait]
impl LockManager for FastObjectLockManager {
async fn acquire_lock(&self, request: ObjectLockRequest) -> Result<FastLockGuard, LockResult> {