Signed-off-by: root <root@PC.localdomain>
This commit is contained in:
root
2025-07-06 21:52:28 +08:00
committed by junxiang Mu
parent 4ccdeb9d2a
commit dc44cde081
24 changed files with 3308 additions and 4772 deletions

View File

@@ -13,10 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use rustfs_lock::{lock_args::LockArgs, namespace::NsLockMap};
use rustfs_lock::{create_namespace_lock, LockArgs, NamespaceLockManager};
use rustfs_protos::{node_service_time_out_client, proto_gen::node_service::GenerallyLockRequest};
use std::{error::Error, sync::Arc, time::Duration};
use tokio::sync::RwLock;
use std::{error::Error, time::Duration};
use tokio::time::sleep;
use tonic::Request;
const CLUSTER_ADDR: &str = "http://localhost:9000";
@@ -56,14 +56,423 @@ async fn test_lock_unlock_rpc() -> Result<(), Box<dyn Error>> {
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
let url = url::Url::parse("http://127.0.0.1:9000/data")?;
let ns_mutex = Arc::new(RwLock::new(NsLockMap::new(true, None)));
let ns_lock = ns_mutex.read().await.new_nslock(Some(url)).await?;
let ns_lock = create_namespace_lock("test".to_string(), true);
let resources = vec!["foo".to_string()];
let result = ns_lock.lock_batch(&resources, "dandan", Duration::from_secs(5)).await?;
assert!(result);
let result = ns_lock.lock_batch(&resources, "dandan", Duration::from_secs(5)).await;
match &result {
Ok(success) => println!("Lock result: {}", success),
Err(e) => println!("Lock error: {}", e),
}
let result = result?;
assert!(result, "Lock should succeed, but got: {}", result);
ns_lock.unlock_batch(&resources, "dandan").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_concurrent_lock_attempts() -> Result<(), Box<dyn Error>> {
let ns_lock = create_namespace_lock("test".to_string(), true);
let resource = vec!["concurrent_resource".to_string()];
// First lock should succeed
println!("Attempting first lock...");
let result1 = ns_lock.lock_batch(&resource, "owner1", Duration::from_secs(5)).await?;
println!("First lock result: {}", result1);
assert!(result1, "First lock should succeed");
// Second lock should fail (resource already locked)
println!("Attempting second lock...");
let result2 = ns_lock.lock_batch(&resource, "owner2", Duration::from_secs(1)).await?;
println!("Second lock result: {}", result2);
assert!(!result2, "Second lock should fail");
// Unlock by first owner
println!("Unlocking first lock...");
ns_lock.unlock_batch(&resource, "owner1").await?;
println!("First lock unlocked");
// Now second owner should be able to lock
println!("Attempting third lock...");
let result3 = ns_lock.lock_batch(&resource, "owner2", Duration::from_secs(5)).await?;
println!("Third lock result: {}", result3);
assert!(result3, "Lock should succeed after unlock");
// Clean up
println!("Cleaning up...");
ns_lock.unlock_batch(&resource, "owner2").await?;
println!("Test completed");
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_read_write_lock_compatibility() -> Result<(), Box<dyn Error>> {
let ns_lock = create_namespace_lock("test_rw".to_string(), true);
let resource = vec!["rw_resource".to_string()];
// First read lock should succeed
let result1 = ns_lock.rlock_batch(&resource, "reader1", Duration::from_secs(5)).await?;
assert!(result1, "First read lock should succeed");
// Second read lock should also succeed (read locks are compatible)
let result2 = ns_lock.rlock_batch(&resource, "reader2", Duration::from_secs(5)).await?;
assert!(result2, "Second read lock should succeed");
// Write lock should fail (read locks are held)
let result3 = ns_lock.lock_batch(&resource, "writer1", Duration::from_secs(1)).await?;
assert!(!result3, "Write lock should fail when read locks are held");
// Release read locks
ns_lock.runlock_batch(&resource, "reader1").await?;
ns_lock.runlock_batch(&resource, "reader2").await?;
// Now write lock should succeed
let result4 = ns_lock.lock_batch(&resource, "writer1", Duration::from_secs(5)).await?;
assert!(result4, "Write lock should succeed after read locks released");
// Clean up
ns_lock.unlock_batch(&resource, "writer1").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_lock_timeout() -> Result<(), Box<dyn Error>> {
let ns_lock = create_namespace_lock("test_timeout".to_string(), true);
let resource = vec!["timeout_resource".to_string()];
// First lock with short timeout
let result1 = ns_lock.lock_batch(&resource, "owner1", Duration::from_secs(2)).await?;
assert!(result1, "First lock should succeed");
// Wait for lock to expire
sleep(Duration::from_secs(3)).await;
// Second lock should succeed after timeout
let result2 = ns_lock.lock_batch(&resource, "owner2", Duration::from_secs(5)).await?;
assert!(result2, "Lock should succeed after timeout");
// Clean up
ns_lock.unlock_batch(&resource, "owner2").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_batch_lock_operations() -> Result<(), Box<dyn Error>> {
let ns_lock = create_namespace_lock("test_batch".to_string(), true);
let resources = vec![
"batch_resource1".to_string(),
"batch_resource2".to_string(),
"batch_resource3".to_string(),
];
// Lock all resources
let result = ns_lock.lock_batch(&resources, "batch_owner", Duration::from_secs(5)).await?;
assert!(result, "Batch lock should succeed");
// Try to lock one of the resources with different owner - should fail
let single_resource = vec!["batch_resource2".to_string()];
let result2 = ns_lock.lock_batch(&single_resource, "other_owner", Duration::from_secs(1)).await?;
assert!(!result2, "Lock should fail for already locked resource");
// Unlock all resources
ns_lock.unlock_batch(&resources, "batch_owner").await?;
// Now should be able to lock single resource
let result3 = ns_lock.lock_batch(&single_resource, "other_owner", Duration::from_secs(5)).await?;
assert!(result3, "Lock should succeed after batch unlock");
// Clean up
ns_lock.unlock_batch(&single_resource, "other_owner").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_multiple_namespaces() -> Result<(), Box<dyn Error>> {
let ns_lock1 = create_namespace_lock("namespace1".to_string(), true);
let ns_lock2 = create_namespace_lock("namespace2".to_string(), true);
let resource = vec!["shared_resource".to_string()];
// Lock same resource in different namespaces - both should succeed
let result1 = ns_lock1.lock_batch(&resource, "owner1", Duration::from_secs(5)).await?;
assert!(result1, "Lock in namespace1 should succeed");
let result2 = ns_lock2.lock_batch(&resource, "owner2", Duration::from_secs(5)).await?;
assert!(result2, "Lock in namespace2 should succeed");
// Clean up
ns_lock1.unlock_batch(&resource, "owner1").await?;
ns_lock2.unlock_batch(&resource, "owner2").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_rpc_read_lock() -> Result<(), Box<dyn Error>> {
let args = LockArgs {
uid: "2222".to_string(),
resources: vec!["read_resource".to_string()],
owner: "reader1".to_string(),
source: "".to_string(),
quorum: 3,
};
let args_str = serde_json::to_string(&args)?;
let mut client = node_service_time_out_client(&CLUSTER_ADDR.to_string()).await?;
// First read lock
let request = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response = client.r_lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not get read lock: {error_info}");
}
// Second read lock with different owner should also succeed
let args2 = LockArgs {
uid: "3333".to_string(),
resources: vec!["read_resource".to_string()],
owner: "reader2".to_string(),
source: "".to_string(),
quorum: 3,
};
let args2_str = serde_json::to_string(&args2)?;
let request2 = Request::new(GenerallyLockRequest { args: args2_str });
let response2 = client.r_lock(request2).await?.into_inner();
if let Some(error_info) = response2.error_info {
panic!("can not get second read lock: {error_info}");
}
// Unlock both
let request = Request::new(GenerallyLockRequest { args: args_str });
let response = client.r_un_lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not unlock read lock: {error_info}");
}
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_lock_refresh() -> Result<(), Box<dyn Error>> {
let args = LockArgs {
uid: "4444".to_string(),
resources: vec!["refresh_resource".to_string()],
owner: "refresh_owner".to_string(),
source: "".to_string(),
quorum: 3,
};
let args_str = serde_json::to_string(&args)?;
let mut client = node_service_time_out_client(&CLUSTER_ADDR.to_string()).await?;
// Acquire lock
let request = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response = client.lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not get lock: {error_info}");
}
// Refresh lock
let request = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response = client.refresh(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not refresh lock: {error_info}");
}
assert!(response.success, "Lock refresh should succeed");
// Unlock
let request = Request::new(GenerallyLockRequest { args: args_str });
let response = client.un_lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not unlock: {error_info}");
}
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_force_unlock() -> Result<(), Box<dyn Error>> {
let args = LockArgs {
uid: "5555".to_string(),
resources: vec!["force_resource".to_string()],
owner: "force_owner".to_string(),
source: "".to_string(),
quorum: 3,
};
let args_str = serde_json::to_string(&args)?;
let mut client = node_service_time_out_client(&CLUSTER_ADDR.to_string()).await?;
// Acquire lock
let request = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response = client.lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not get lock: {error_info}");
}
// Force unlock (even by different owner)
let force_args = LockArgs {
uid: "5555".to_string(),
resources: vec!["force_resource".to_string()],
owner: "admin".to_string(),
source: "".to_string(),
quorum: 3,
};
let force_args_str = serde_json::to_string(&force_args)?;
let request = Request::new(GenerallyLockRequest { args: force_args_str });
let response = client.force_un_lock(request).await?.into_inner();
if let Some(error_info) = response.error_info {
panic!("can not force unlock: {error_info}");
}
assert!(response.success, "Force unlock should succeed");
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_concurrent_rpc_lock_attempts() -> Result<(), Box<dyn Error>> {
let args1 = LockArgs {
uid: "concurrent_test_1".to_string(),
resources: vec!["concurrent_rpc_resource".to_string()],
owner: "owner1".to_string(),
source: "".to_string(),
quorum: 3,
};
let args1_str = serde_json::to_string(&args1)?;
let args2 = LockArgs {
uid: "concurrent_test_2".to_string(),
resources: vec!["concurrent_rpc_resource".to_string()],
owner: "owner2".to_string(),
source: "".to_string(),
quorum: 3,
};
let args2_str = serde_json::to_string(&args2)?;
let mut client = node_service_time_out_client(&CLUSTER_ADDR.to_string()).await?;
// First lock should succeed
println!("Attempting first RPC lock...");
let request1 = Request::new(GenerallyLockRequest { args: args1_str.clone() });
let response1 = client.lock(request1).await?.into_inner();
println!("First RPC lock response: success={}, error={:?}", response1.success, response1.error_info);
assert!(response1.success && response1.error_info.is_none(), "First lock should succeed");
// Second lock should fail (resource already locked)
println!("Attempting second RPC lock...");
let request2 = Request::new(GenerallyLockRequest { args: args2_str });
let response2 = client.lock(request2).await?.into_inner();
println!("Second RPC lock response: success={}, error={:?}", response2.success, response2.error_info);
assert!(!response2.success, "Second lock should fail");
// Unlock by first owner
println!("Unlocking first RPC lock...");
let unlock_request = Request::new(GenerallyLockRequest { args: args1_str });
let unlock_response = client.un_lock(unlock_request).await?.into_inner();
println!("Unlock response: success={}, error={:?}", unlock_response.success, unlock_response.error_info);
assert!(unlock_response.success && unlock_response.error_info.is_none(), "Unlock should succeed");
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_global_lock_map_sharing() -> Result<(), Box<dyn Error>> {
// Create two separate NsLockMap instances
let lock_map1 = rustfs_lock::NsLockMap::new(false, None);
let lock_map2 = rustfs_lock::NsLockMap::new(false, None);
let resource = vec!["global_test_resource".to_string()];
// First instance acquires lock
println!("First lock map attempting to acquire lock...");
let result1 = lock_map1.lock_batch_with_ttl(&resource, "owner1", std::time::Duration::from_secs(5), Some(std::time::Duration::from_secs(30))).await?;
println!("First lock result: {}", result1);
assert!(result1, "First lock should succeed");
// Second instance should fail to acquire the same lock
println!("Second lock map attempting to acquire lock...");
let result2 = lock_map2.lock_batch_with_ttl(&resource, "owner2", std::time::Duration::from_secs(1), Some(std::time::Duration::from_secs(30))).await?;
println!("Second lock result: {}", result2);
assert!(!result2, "Second lock should fail because resource is already locked");
// Release lock from first instance
println!("First lock map releasing lock...");
lock_map1.unlock_batch(&resource, "owner1").await?;
// Now second instance should be able to acquire lock
println!("Second lock map attempting to acquire lock again...");
let result3 = lock_map2.lock_batch_with_ttl(&resource, "owner2", std::time::Duration::from_secs(5), Some(std::time::Duration::from_secs(30))).await?;
println!("Third lock result: {}", result3);
assert!(result3, "Lock should succeed after first lock is released");
// Clean up
lock_map2.unlock_batch(&resource, "owner2").await?;
Ok(())
}
#[tokio::test]
#[ignore = "requires running RustFS server at localhost:9000"]
async fn test_sequential_rpc_lock_calls() -> Result<(), Box<dyn Error>> {
let args = LockArgs {
uid: "sequential_test".to_string(),
resources: vec!["sequential_resource".to_string()],
owner: "sequential_owner".to_string(),
source: "".to_string(),
quorum: 3,
};
let args_str = serde_json::to_string(&args)?;
let mut client = node_service_time_out_client(&CLUSTER_ADDR.to_string()).await?;
// First lock should succeed
println!("First lock attempt...");
let request1 = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response1 = client.lock(request1).await?.into_inner();
println!("First response: success={}, error={:?}", response1.success, response1.error_info);
assert!(response1.success && response1.error_info.is_none(), "First lock should succeed");
// Second lock with same owner should also succeed (re-entrant)
println!("Second lock attempt with same owner...");
let request2 = Request::new(GenerallyLockRequest { args: args_str.clone() });
let response2 = client.lock(request2).await?.into_inner();
println!("Second response: success={}, error={:?}", response2.success, response2.error_info);
// Different owner should fail
let args2 = LockArgs {
uid: "sequential_test_2".to_string(),
resources: vec!["sequential_resource".to_string()],
owner: "different_owner".to_string(),
source: "".to_string(),
quorum: 3,
};
let args2_str = serde_json::to_string(&args2)?;
println!("Third lock attempt with different owner...");
let request3 = Request::new(GenerallyLockRequest { args: args2_str });
let response3 = client.lock(request3).await?.into_inner();
println!("Third response: success={}, error={:?}", response3.success, response3.error_info);
assert!(!response3.success, "Lock with different owner should fail");
// Unlock
println!("Unlocking...");
let unlock_request = Request::new(GenerallyLockRequest { args: args_str });
let unlock_response = client.un_lock(unlock_request).await?.into_inner();
println!("Unlock response: success={}, error={:?}", unlock_response.success, unlock_response.error_info);
Ok(())
}

View File

@@ -22,4 +22,4 @@ pub use http_auth::{build_auth_headers, verify_rpc_signature};
pub use peer_rest_client::PeerRestClient;
pub use peer_s3_client::{LocalPeerS3Client, PeerS3Client, RemotePeerS3Client, S3PeerSys};
pub use remote_disk::RemoteDisk;
pub use tonic_service::make_server;
pub use tonic_service::{make_server, NodeService};

View File

@@ -34,7 +34,6 @@ use crate::{
};
use futures::{Stream, StreamExt};
use futures_util::future::join_all;
use rustfs_lock::{GLOBAL_LOCAL_SERVER, core::local::LocalLockManager, lock_args::LockArgs};
use rustfs_common::globals::GLOBAL_Local_Node_Name;
@@ -81,11 +80,16 @@ type ResponseStream<T> = Pin<Box<dyn Stream<Item = Result<T, tonic::Status>> + S
#[derive(Debug)]
pub struct NodeService {
local_peer: LocalPeerS3Client,
lock_manager: rustfs_lock::NsLockMap,
}
pub fn make_server() -> NodeService {
let local_peer = LocalPeerS3Client::new(None, None);
NodeService { local_peer }
let lock_manager = rustfs_lock::NsLockMap::new(false, None);
NodeService {
local_peer,
lock_manager,
}
}
impl NodeService {
@@ -1526,176 +1530,199 @@ impl Node for NodeService {
async fn lock(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(args) => {
let resource = match args.resources.first() {
Some(r) => r,
None => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
};
let timeout = std::time::Duration::from_secs(30);
match GLOBAL_LOCAL_SERVER.write().await.lock(resource, &args.owner, timeout).await {
Ok(result) => Ok(tonic::Response::new(GenerallyLockResponse {
success: result,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not lock, args: {args}, err: {err}")),
})),
}
// Parse the request to extract resource and owner
let args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
};
let resource = args["resources"][0].as_str().unwrap_or("");
let owner = args["owner"].as_str().unwrap_or("");
if resource.is_empty() {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
match self
.lock_manager
.lock_batch_with_ttl(&[resource.to_string()], owner, std::time::Duration::from_secs(30), Some(std::time::Duration::from_secs(30)))
.await
{
Ok(result) => Ok(tonic::Response::new(GenerallyLockResponse {
success: result,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
error_info: Some(format!("can not lock, resource: {resource}, owner: {owner}, err: {err}")),
})),
}
}
async fn un_lock(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(args) => {
let resource = match args.resources.first() {
Some(r) => r,
None => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
};
match GLOBAL_LOCAL_SERVER.write().await.unlock(resource, &args.owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not unlock, args: {args}, err: {err}")),
})),
}
let args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
};
let resource = args["resources"][0].as_str().unwrap_or("");
let owner = args["owner"].as_str().unwrap_or("");
if resource.is_empty() {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
match self.lock_manager.unlock_batch(&[resource.to_string()], owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
error_info: Some(format!("can not unlock, resource: {resource}, owner: {owner}, err: {err}")),
})),
}
}
async fn r_lock(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(args) => {
let resource = match args.resources.first() {
Some(r) => r,
None => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
};
let timeout = std::time::Duration::from_secs(30);
match GLOBAL_LOCAL_SERVER.write().await.rlock(resource, &args.owner, timeout).await {
Ok(result) => Ok(tonic::Response::new(GenerallyLockResponse {
success: result,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not rlock, args: {args}, err: {err}")),
})),
}
let args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
};
let resource = args["resources"][0].as_str().unwrap_or("");
let owner = args["owner"].as_str().unwrap_or("");
if resource.is_empty() {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
match self
.lock_manager
.rlock_batch_with_ttl(&[resource.to_string()], owner, std::time::Duration::from_secs(30), Some(std::time::Duration::from_secs(30)))
.await
{
Ok(result) => Ok(tonic::Response::new(GenerallyLockResponse {
success: result,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
error_info: Some(format!("can not rlock, resource: {resource}, owner: {owner}, err: {err}")),
})),
}
}
async fn r_un_lock(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(args) => {
let resource = match args.resources.first() {
Some(r) => r,
None => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
};
match GLOBAL_LOCAL_SERVER.write().await.runlock(resource, &args.owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not runlock, args: {args}, err: {err}")),
})),
}
let args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
};
let resource = args["resources"][0].as_str().unwrap_or("");
let owner = args["owner"].as_str().unwrap_or("");
if resource.is_empty() {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
match self.lock_manager.runlock_batch(&[resource.to_string()], owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
error_info: Some(format!("can not runlock, resource: {resource}, owner: {owner}, err: {err}")),
})),
}
}
async fn force_un_lock(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(args) => {
let resource = match args.resources.first() {
Some(r) => r,
None => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
};
match GLOBAL_LOCAL_SERVER.write().await.unlock(resource, &args.owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not force_unlock, args: {args}, err: {err}")),
})),
}
let args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
};
let resource = args["resources"][0].as_str().unwrap_or("");
let owner = args["owner"].as_str().unwrap_or("");
if resource.is_empty() {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some("No resource specified".to_string()),
}));
}
match self.lock_manager.unlock_batch(&[resource.to_string()], owner).await {
Ok(_) => Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
})),
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
error_info: Some(format!("can not force_unlock, resource: {resource}, owner: {owner}, err: {err}")),
})),
}
}
async fn refresh(&self, request: Request<GenerallyLockRequest>) -> Result<Response<GenerallyLockResponse>, Status> {
let request = request.into_inner();
match &serde_json::from_str::<LockArgs>(&request.args) {
Ok(_args) => {
// 本地锁不需要刷新
Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
}))
let _args: serde_json::Value = match serde_json::from_str(&request.args) {
Ok(args) => args,
Err(err) => {
return Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
}));
}
Err(err) => Ok(tonic::Response::new(GenerallyLockResponse {
success: false,
error_info: Some(format!("can not decode args, err: {err}")),
})),
}
};
Ok(tonic::Response::new(GenerallyLockResponse {
success: true,
error_info: None,
}))
}
async fn local_storage_info(
@@ -3683,15 +3710,15 @@ mod tests {
// Note: signal_service test is skipped because it contains todo!() and would panic
#[test]
fn test_node_service_debug() {
#[tokio::test]
async fn test_node_service_debug() {
let service = create_test_node_service();
let debug_str = format!("{service:?}");
assert!(debug_str.contains("NodeService"));
}
#[test]
fn test_node_service_creation() {
#[tokio::test]
async fn test_node_service_creation() {
let service1 = make_server();
let service2 = make_server();

View File

@@ -83,7 +83,7 @@ use rustfs_filemeta::{
headers::{AMZ_OBJECT_TAGGING, AMZ_STORAGE_CLASS},
merge_file_meta_versions,
};
use rustfs_lock::{LockApi, NsLockMap};
use rustfs_lock::{NamespaceLockManager, NsLockMap};
use rustfs_madmin::heal_commands::{HealDriveInfo, HealResultItem};
use rustfs_rio::{EtagResolvable, HashReader, TryGetIndex as _, WarpReader};
use rustfs_utils::{
@@ -123,7 +123,7 @@ pub const MAX_PARTS_COUNT: usize = 10000;
#[derive(Clone, Debug)]
pub struct SetDisks {
pub lockers: Vec<LockApi>,
pub lockers: Vec<Arc<rustfs_lock::NamespaceLock>>,
pub locker_owner: String,
pub ns_mutex: Arc<NsLockMap>,
pub disks: Arc<RwLock<Vec<Option<DiskStore>>>>,
@@ -138,7 +138,7 @@ pub struct SetDisks {
impl SetDisks {
#[allow(clippy::too_many_arguments)]
pub async fn new(
lockers: Vec<LockApi>,
lockers: Vec<Arc<rustfs_lock::NamespaceLock>>,
locker_owner: String,
ns_mutex: Arc<NsLockMap>,
disks: Arc<RwLock<Vec<Option<DiskStore>>>>,
@@ -4066,7 +4066,6 @@ impl ObjectIO for SetDisks {
async fn put_object(&self, bucket: &str, object: &str, data: &mut PutObjReader, opts: &ObjectOptions) -> Result<ObjectInfo> {
let disks = self.disks.read().await;
// 获取对象锁
let mut _ns = None;
if !opts.no_lock {
let paths = vec![object.to_string()];
@@ -4076,7 +4075,6 @@ impl ObjectIO for SetDisks {
.await
.map_err(|err| Error::other(err.to_string()))?;
// 尝试获取锁
let lock_acquired = ns_lock
.lock_batch(&paths, &self.locker_owner, std::time::Duration::from_secs(5))
.await
@@ -4293,7 +4291,6 @@ impl ObjectIO for SetDisks {
self.delete_all(RUSTFS_META_TMP_BUCKET, &tmp_dir).await?;
// 释放对象锁
if let Some(ns_lock) = _ns {
let paths = vec![object.to_string()];
if let Err(err) = ns_lock.unlock_batch(&paths, &self.locker_owner).await {

View File

@@ -40,10 +40,11 @@ use crate::{
store_init::{check_format_erasure_values, get_format_erasure_in_quorum, load_format_erasure_all, save_format_file},
};
use futures::future::join_all;
use futures_util::FutureExt;
use http::HeaderMap;
use rustfs_common::globals::GLOBAL_Local_Node_Name;
use rustfs_filemeta::FileInfo;
use rustfs_lock::{LockApi, NsLockMap, new_lock_api};
use rustfs_lock::NamespaceLock;
use rustfs_madmin::heal_commands::{HealDriveInfo, HealResultItem};
use rustfs_utils::{crc_hash, path::path_join_buf, sip_hash};
use tokio::sync::RwLock;
@@ -60,7 +61,7 @@ pub struct Sets {
pub id: Uuid,
// pub sets: Vec<Objects>,
// pub disk_set: Vec<Vec<Option<DiskStore>>>, // [set_count_idx][set_drive_count_idx] = disk_idx
pub lockers: Vec<Vec<LockApi>>,
pub lockers: Vec<Vec<Arc<NamespaceLock>>>,
pub disk_set: Vec<Arc<SetDisks>>, // [set_count_idx][set_drive_count_idx] = disk_idx
pub pool_idx: usize,
pub endpoints: PoolEndpoints,
@@ -93,23 +94,42 @@ impl Sets {
let set_count = fm.erasure.sets.len();
let set_drive_count = fm.erasure.sets[0].len();
let mut unique: Vec<Vec<String>> = vec![vec![]; set_count];
let mut lockers: Vec<Vec<LockApi>> = vec![vec![]; set_count];
endpoints.endpoints.as_ref().iter().enumerate().for_each(|(idx, endpoint)| {
let mut unique: Vec<Vec<String>> = (0..set_count).map(|_| vec![]).collect();
let mut lockers: Vec<Vec<Arc<NamespaceLock>>> = (0..set_count).map(|_| vec![]).collect();
for (idx, endpoint) in endpoints.endpoints.as_ref().iter().enumerate() {
let set_idx = idx / set_drive_count;
if endpoint.is_local && !unique[set_idx].contains(&"local".to_string()) {
unique[set_idx].push("local".to_string());
lockers[set_idx].push(new_lock_api(true, None));
let local_manager = rustfs_lock::NsLockMap::new(false, None);
let local_lock = Arc::new(local_manager.new_nslock(None).await.unwrap_or_else(|_| {
// If creation fails, create an empty lock manager
rustfs_lock::NsLockMap::new(false, None)
.new_nslock(None)
.now_or_never()
.unwrap()
.unwrap()
}));
lockers[set_idx].push(local_lock);
}
if !endpoint.is_local {
let host_port = format!("{}:{}", endpoint.url.host_str().unwrap(), endpoint.url.port().unwrap());
if !unique[set_idx].contains(&host_port) {
unique[set_idx].push(host_port);
lockers[set_idx].push(new_lock_api(false, Some(endpoint.url.clone())));
let dist_manager = rustfs_lock::NsLockMap::new(true, None);
let dist_lock = Arc::new(dist_manager.new_nslock(Some(endpoint.url.clone())).await.unwrap_or_else(|_| {
// If creation fails, create an empty lock manager
rustfs_lock::NsLockMap::new(true, None)
.new_nslock(Some(endpoint.url.clone()))
.now_or_never()
.unwrap()
.unwrap()
}));
lockers[set_idx].push(dist_lock);
}
}
});
}
let mut disk_set = Vec::with_capacity(set_count);
@@ -170,7 +190,7 @@ impl Sets {
let set_disks = SetDisks::new(
locker.clone(),
GLOBAL_Local_Node_Name.read().await.to_string(),
Arc::new(NsLockMap::new(is_dist_erasure().await, None)),
Arc::new(rustfs_lock::NsLockMap::new(is_dist_erasure().await, None)),
Arc::new(RwLock::new(set_drive)),
set_drive_count,
parity_count,
@@ -543,7 +563,7 @@ impl StorageAPI for Sets {
objects: Vec<ObjectToDelete>,
opts: ObjectOptions,
) -> Result<(Vec<DeletedObject>, Vec<Option<Error>>)> {
// 默认返回值
// Default return value
let mut del_objects = vec![DeletedObject::default(); objects.len()];
let mut del_errs = Vec::with_capacity(objects.len());
@@ -602,7 +622,7 @@ impl StorageAPI for Sets {
// del_errs.extend(errs);
// }
// TODO: 并发
// TODO: Implement concurrency
for (k, v) in set_obj_map {
let disks = self.get_disks(k);
let objs: Vec<ObjectToDelete> = v.iter().map(|v| v.obj.clone()).collect();

View File

@@ -43,3 +43,9 @@ thiserror.workspace = true
once_cell.workspace = true
lru.workspace = true
dashmap.workspace = true
[features]
default = []
distributed = []
metrics = []
tracing = []

View File

@@ -12,279 +12,51 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use dashmap::DashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::{
client::LockClient,
deadlock_detector::DeadlockDetector,
error::Result,
types::{
DeadlockDetectionResult, LockId, LockInfo, LockRequest, LockResponse, LockStats, LockStatus, LockType,
WaitQueueItem,
},
local::LocalLockMap,
types::{LockId, LockInfo, LockMetadata, LockPriority, LockRequest, LockResponse, LockStats, LockType},
};
/// Local lock client
///
/// Uses global singleton LocalLockMap to ensure all clients access the same lock instance
#[derive(Debug, Clone)]
pub struct LocalClient {
/// Lock storage
locks: Arc<DashMap<String, LockInfo>>,
/// Deadlock detector
deadlock_detector: Arc<Mutex<DeadlockDetector>>,
/// Wait queues: resource -> wait queue
wait_queues: Arc<DashMap<String, Vec<WaitQueueItem>>>,
/// Statistics
stats: Arc<Mutex<LockStats>>,
}
pub struct LocalClient;
impl LocalClient {
/// Create new local client
pub fn new() -> Self {
Self {
locks: Arc::new(DashMap::new()),
deadlock_detector: Arc::new(Mutex::new(DeadlockDetector::new())),
wait_queues: Arc::new(DashMap::new()),
stats: Arc::new(Mutex::new(LockStats::default())),
Self
}
/// Get global lock map instance
pub fn get_lock_map(&self) -> Arc<LocalLockMap> {
crate::get_global_lock_map()
}
/// Convert LockRequest to batch operation
async fn request_to_batch(&self, request: LockRequest) -> Result<bool> {
let lock_map = self.get_lock_map();
let resources = vec![request.resource];
let timeout = request.timeout;
match request.lock_type {
LockType::Exclusive => lock_map.lock_batch(&resources, &request.owner, timeout, None).await,
LockType::Shared => lock_map.rlock_batch(&resources, &request.owner, timeout, None).await,
}
}
/// Acquire lock with priority and deadlock detection
async fn acquire_lock_with_priority(&self, request: LockRequest, lock_type: LockType) -> Result<LockResponse> {
let _start_time = std::time::SystemTime::now();
let lock_key = crate::utils::generate_lock_key(&request.resource, lock_type);
// Check deadlock detection
if request.deadlock_detection {
if let Ok(detection_result) = self.check_deadlock(&request).await {
if detection_result.has_deadlock {
return Ok(LockResponse::failure(
format!("Deadlock detected: {:?}", detection_result.deadlock_cycle),
crate::utils::duration_between(_start_time, std::time::SystemTime::now()),
));
}
}
}
// Atomic check + insert
match self.locks.entry(lock_key) {
dashmap::mapref::entry::Entry::Occupied(mut entry) => {
let existing = entry.get();
if existing.owner != request.owner {
// Add to wait queue
let wait_item = WaitQueueItem::new(&request.owner, lock_type, request.priority);
self.add_to_wait_queue(&request.resource, wait_item).await;
// Update deadlock detector
self.update_deadlock_detector(&request, &existing.owner).await;
// Check wait timeout
if let Some(wait_timeout) = request.wait_timeout {
if crate::utils::duration_between(_start_time, std::time::SystemTime::now()) > wait_timeout {
self.remove_from_wait_queue(&request.resource, &request.owner).await;
return Ok(LockResponse::failure(
"Wait timeout exceeded".to_string(),
crate::utils::duration_between(_start_time, std::time::SystemTime::now()),
));
}
}
let position = self.get_wait_position(&request.resource, &request.owner).await;
return Ok(LockResponse::waiting(
crate::utils::duration_between(_start_time, std::time::SystemTime::now()),
position,
));
}
// Update lock info (same owner can re-acquire)
let mut lock_info = existing.clone();
lock_info.last_refreshed = std::time::SystemTime::now();
lock_info.expires_at = std::time::SystemTime::now() + request.timeout;
lock_info.priority = request.priority;
entry.insert(lock_info.clone());
Ok(LockResponse::success(
lock_info,
crate::utils::duration_between(_start_time, std::time::SystemTime::now()),
))
}
dashmap::mapref::entry::Entry::Vacant(entry) => {
// Insert new lock
let lock_info = LockInfo {
id: LockId::new(),
resource: request.resource.clone(),
lock_type,
status: LockStatus::Acquired,
owner: request.owner.clone(),
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + request.timeout,
last_refreshed: std::time::SystemTime::now(),
metadata: request.metadata.clone(),
priority: request.priority,
wait_start_time: None,
};
entry.insert(lock_info.clone());
// Update deadlock detector
self.update_deadlock_detector(&request, "").await;
Ok(LockResponse::success(
lock_info,
crate::utils::duration_between(_start_time, std::time::SystemTime::now()),
))
}
}
}
/// Check for deadlock
async fn check_deadlock(&self, _request: &LockRequest) -> Result<DeadlockDetectionResult> {
let mut detector = self.deadlock_detector.lock().await;
Ok(detector.detect_deadlock())
}
/// Update deadlock detector
async fn update_deadlock_detector(&self, request: &LockRequest, current_owner: &str) {
let mut detector = self.deadlock_detector.lock().await;
if !current_owner.is_empty() {
// Add wait relationship
detector.add_wait_relationship(
&request.owner,
&request.resource,
vec![], // TODO: Get currently held resources
request.priority,
);
}
// Update resource holder
detector.update_resource_holder(&request.resource, &request.owner);
}
/// Add to wait queue
async fn add_to_wait_queue(&self, resource: &str, item: WaitQueueItem) {
let mut queue = self.wait_queues.entry(resource.to_string()).or_default();
queue.push(item);
// Sort by priority
queue.sort_by(|a, b| b.priority.cmp(&a.priority));
}
/// Remove from wait queue
async fn remove_from_wait_queue(&self, resource: &str, owner: &str) {
if let Some(mut queue) = self.wait_queues.get_mut(resource) {
queue.retain(|item| item.owner != owner);
}
}
/// Get wait position
async fn get_wait_position(&self, resource: &str, owner: &str) -> usize {
if let Some(queue) = self.wait_queues.get(resource) {
for (i, item) in queue.iter().enumerate() {
if item.owner == owner {
return i;
}
}
}
0
}
/// Process wait queue
async fn process_wait_queue(&self, resource: &str) {
// Simple implementation to avoid never_loop warning
if let Some(mut queue) = self.wait_queues.get_mut(resource) {
if !queue.is_empty() {
let _next_item = queue.remove(0);
// TODO: Process next item in queue
}
}
}
/// Acquire multiple locks atomically
pub async fn acquire_multiple_atomic(&self, requests: Vec<LockRequest>) -> Result<Vec<LockResponse>> {
let mut responses = Vec::new();
let mut acquired_locks = Vec::new();
for request in requests {
match self.acquire_lock_with_priority(request.clone(), LockType::Exclusive).await {
Ok(response) => {
if response.is_success() {
acquired_locks.push(request.resource.clone());
}
responses.push(response);
}
Err(e) => {
// Rollback acquired locks
for resource in acquired_locks {
let _ = self.force_release_by_resource(&resource).await;
}
return Err(e);
}
}
}
Ok(responses)
}
/// Release multiple locks atomically
pub async fn release_multiple_atomic(&self, lock_ids: Vec<LockId>) -> Result<Vec<bool>> {
let mut results = Vec::new();
for lock_id in lock_ids {
results.push(self.release(&lock_id).await?);
}
Ok(results)
}
/// Force release by resource
async fn force_release_by_resource(&self, resource: &str) -> Result<bool> {
let lock_key = crate::utils::generate_lock_key(resource, LockType::Exclusive);
if let Some((_, lock_info)) = self.locks.remove(&lock_key) {
// Update statistics
let mut stats = self.stats.lock().await;
stats.total_releases += 1;
stats.total_hold_time += crate::utils::duration_between(lock_info.acquired_at, std::time::SystemTime::now());
Ok(true)
} else {
Ok(false)
}
}
/// Check multiple lock status
pub async fn check_multiple_status(&self, lock_ids: Vec<LockId>) -> Result<Vec<Option<LockInfo>>> {
let mut results = Vec::new();
for lock_id in lock_ids {
results.push(self.check_status(&lock_id).await?);
}
Ok(results)
}
/// Refresh multiple locks atomically
pub async fn refresh_multiple_atomic(&self, lock_ids: Vec<LockId>) -> Result<Vec<bool>> {
let mut results = Vec::new();
for lock_id in lock_ids {
results.push(self.refresh(&lock_id).await?);
}
Ok(results)
}
/// Get deadlock statistics
pub async fn get_deadlock_stats(&self) -> Result<(usize, std::time::SystemTime)> {
let detector = self.deadlock_detector.lock().await;
let (count, time) = detector.get_stats();
Ok((count, time))
}
/// Detect deadlock
pub async fn detect_deadlock(&self) -> Result<DeadlockDetectionResult> {
let mut detector = self.deadlock_detector.lock().await;
Ok(detector.detect_deadlock())
}
/// Cleanup expired waits
pub async fn cleanup_expired_waits(&self, max_wait_time: std::time::Duration) {
let now = std::time::SystemTime::now();
for mut queue in self.wait_queues.iter_mut() {
queue.retain(|item| now.duration_since(item.wait_start_time).unwrap_or_default() <= max_wait_time);
}
/// Convert LockId to resource for release
async fn lock_id_to_batch_release(&self, lock_id: &LockId) -> Result<()> {
let lock_map = self.get_lock_map();
// For simplicity, we'll use the lock_id as resource name
// In a real implementation, you might want to maintain a mapping
let resources = vec![lock_id.as_str().to_string()];
lock_map.unlock_batch(&resources, "unknown").await
}
}
@@ -295,62 +67,71 @@ impl Default for LocalClient {
}
#[async_trait::async_trait]
impl super::LockClient for LocalClient {
impl LockClient for LocalClient {
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse> {
self.acquire_lock_with_priority(request, LockType::Exclusive).await
let lock_map = self.get_lock_map();
let success = lock_map
.lock_with_ttl_id(&request.resource, &request.owner, request.timeout, None)
.await
.map_err(|e| crate::error::LockError::internal(format!("Lock acquisition failed: {e}")))?;
if success {
let lock_info = LockInfo {
id: crate::types::LockId::new_deterministic(&request.resource),
resource: request.resource.clone(),
lock_type: LockType::Exclusive,
status: crate::types::LockStatus::Acquired,
owner: request.owner.clone(),
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + request.timeout,
last_refreshed: std::time::SystemTime::now(),
metadata: request.metadata.clone(),
priority: request.priority,
wait_start_time: None,
};
Ok(LockResponse::success(lock_info, std::time::Duration::ZERO))
} else {
Ok(LockResponse::failure("Lock acquisition failed".to_string(), std::time::Duration::ZERO))
}
}
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse> {
self.acquire_lock_with_priority(request, LockType::Shared).await
let lock_map = self.get_lock_map();
let success = lock_map
.rlock_with_ttl_id(&request.resource, &request.owner, request.timeout, None)
.await
.map_err(|e| crate::error::LockError::internal(format!("Shared lock acquisition failed: {e}")))?;
if success {
let lock_info = LockInfo {
id: crate::types::LockId::new_deterministic(&request.resource),
resource: request.resource.clone(),
lock_type: LockType::Shared,
status: crate::types::LockStatus::Acquired,
owner: request.owner.clone(),
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + request.timeout,
last_refreshed: std::time::SystemTime::now(),
metadata: request.metadata.clone(),
priority: request.priority,
wait_start_time: None,
};
Ok(LockResponse::success(lock_info, std::time::Duration::ZERO))
} else {
Ok(LockResponse::failure("Lock acquisition failed".to_string(), std::time::Duration::ZERO))
}
}
async fn release(&self, lock_id: &LockId) -> Result<bool> {
let _start_time = std::time::SystemTime::now();
// Find and remove the lock
let mut found = false;
let mut lock_info_opt = None;
for entry in self.locks.iter() {
if entry.id == *lock_id {
lock_info_opt = Some(entry.clone());
found = true;
break;
}
}
if found {
let lock_key = crate::utils::generate_lock_key(
&lock_info_opt.as_ref().unwrap().resource,
lock_info_opt.as_ref().unwrap().lock_type,
);
if let Some((_, lock_info)) = self.locks.remove(&lock_key) {
// Update statistics
let mut stats = self.stats.lock().await;
stats.total_releases += 1;
stats.total_hold_time += crate::utils::duration_between(lock_info.acquired_at, std::time::SystemTime::now());
// Process wait queue
self.process_wait_queue(&lock_info.resource).await;
Ok(true)
} else {
Ok(false)
}
} else {
Ok(false)
}
let lock_map = self.get_lock_map();
lock_map
.unlock_by_id(lock_id)
.await
.map_err(|e| crate::error::LockError::internal(format!("Release failed: {e}")))?;
Ok(true)
}
async fn refresh(&self, lock_id: &LockId) -> Result<bool> {
for mut entry in self.locks.iter_mut() {
if entry.id == *lock_id {
entry.last_refreshed = std::time::SystemTime::now();
entry.expires_at = std::time::SystemTime::now() + std::time::Duration::from_secs(30);
return Ok(true);
}
}
Ok(false)
async fn refresh(&self, _lock_id: &LockId) -> Result<bool> {
// For local locks, refresh is not needed as they don't expire automatically
Ok(true)
}
async fn force_release(&self, lock_id: &LockId) -> Result<bool> {
@@ -358,44 +139,41 @@ impl super::LockClient for LocalClient {
}
async fn check_status(&self, lock_id: &LockId) -> Result<Option<LockInfo>> {
for entry in self.locks.iter() {
if entry.id == *lock_id {
// Check if lock has expired
if entry.expires_at < std::time::SystemTime::now() {
// Lock has expired, remove it
let lock_key = crate::utils::generate_lock_key(&entry.resource, entry.lock_type);
let _ = self.locks.remove(&lock_key);
return Ok(None);
}
return Ok(Some(entry.clone()));
let lock_map = self.get_lock_map();
if let Some((resource, owner)) = lock_map.lockid_map.get(lock_id).map(|v| v.clone()) {
let is_locked = lock_map.is_locked(&resource).await;
if is_locked {
Ok(Some(LockInfo {
id: lock_id.clone(),
resource,
lock_type: LockType::Exclusive, // 这里可进一步完善
status: crate::types::LockStatus::Acquired,
owner,
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + std::time::Duration::from_secs(30),
last_refreshed: std::time::SystemTime::now(),
metadata: LockMetadata::default(),
priority: LockPriority::Normal,
wait_start_time: None,
}))
} else {
Ok(None)
}
} else {
Ok(None)
}
Ok(None)
}
async fn get_stats(&self) -> Result<LockStats> {
let mut stats = self.stats.lock().await;
stats.total_locks = self.locks.len();
stats.total_wait_queues = self.wait_queues.len();
// Calculate average hold time
if stats.total_releases > 0 {
stats.average_hold_time =
std::time::Duration::from_secs(stats.total_hold_time.as_secs() / stats.total_releases as u64);
}
Ok(stats.clone())
Ok(LockStats::default())
}
async fn close(&self) -> Result<()> {
// Cleanup all locks
self.locks.clear();
self.wait_queues.clear();
Ok(())
}
async fn is_online(&self) -> bool {
true // Local client is always online
true
}
async fn is_local(&self) -> bool {
@@ -406,21 +184,13 @@ impl super::LockClient for LocalClient {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{LockMetadata, LockPriority, LockType};
use crate::types::LockType;
#[tokio::test]
async fn test_local_client_acquire_exclusive() {
let client = LocalClient::new();
let request = LockRequest {
resource: "test_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "test_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let request =
LockRequest::new("test-resource", LockType::Exclusive, "test-owner").with_timeout(std::time::Duration::from_secs(30));
let response = client.acquire_exclusive(request).await.unwrap();
assert!(response.is_success());
@@ -429,16 +199,8 @@ mod tests {
#[tokio::test]
async fn test_local_client_acquire_shared() {
let client = LocalClient::new();
let request = LockRequest {
resource: "test_resource".to_string(),
lock_type: LockType::Shared,
owner: "test_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let request =
LockRequest::new("test-resource", LockType::Shared, "test-owner").with_timeout(std::time::Duration::from_secs(30));
let response = client.acquire_shared(request).await.unwrap();
assert!(response.is_success());
@@ -447,426 +209,25 @@ mod tests {
#[tokio::test]
async fn test_local_client_release() {
let client = LocalClient::new();
let request = LockRequest {
resource: "test_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "test_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(request).await.unwrap();
assert!(response.is_success());
let lock_id = &response.lock_info().unwrap().id;
let result = client.release(lock_id).await.unwrap();
assert!(result);
}
#[tokio::test]
async fn test_local_client_concurrent_access() {
let client = Arc::new(LocalClient::new());
let mut handles = vec![];
for i in 0..10 {
let client_clone = client.clone();
let handle = tokio::spawn(async move {
let request = LockRequest {
resource: "concurrent_resource".to_string(),
lock_type: LockType::Exclusive,
owner: format!("owner_{i}"),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client_clone.acquire_exclusive(request).await.unwrap();
if response.is_success() {
let lock_id = &response.lock_info().unwrap().id;
let _ = client_clone.release(lock_id).await;
}
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
}
#[tokio::test]
async fn test_dashmap_performance() {
let client = LocalClient::new();
let start_time = std::time::Instant::now();
// Simulate high concurrent access
let mut handles = vec![];
for i in 0..100 {
let client_clone = Arc::new(client.clone());
let handle = tokio::spawn(async move {
let request = LockRequest {
resource: format!("resource_{i}"),
lock_type: LockType::Exclusive,
owner: format!("owner_{i}"),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client_clone.acquire_exclusive(request).await.unwrap();
if response.is_success() {
let lock_id = &response.lock_info().unwrap().id;
let _ = client_clone.release(lock_id).await;
}
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
let duration = start_time.elapsed();
println!("DashMap performance test completed in {duration:?}");
assert!(duration < std::time::Duration::from_secs(5));
}
#[tokio::test]
async fn test_atomic_operations() {
let client = LocalClient::new();
let request = LockRequest {
resource: "atomic_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "test_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
// Test atomic acquire
let response = client.acquire_exclusive(request).await.unwrap();
assert!(response.is_success());
// Test concurrent access to same resource
let client_clone = Arc::new(client);
let mut handles = vec![];
for i in 0..5 {
let client_clone = client_clone.clone();
let handle = tokio::spawn(async move {
let request = LockRequest {
resource: "atomic_resource".to_string(),
lock_type: LockType::Exclusive,
owner: format!("owner_{i}"),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client_clone.acquire_exclusive(request).await.unwrap();
response.is_waiting() // Should be waiting due to atomic operation
});
handles.push(handle);
}
for handle in handles {
let result = handle.await.unwrap();
assert!(result);
}
}
#[tokio::test]
async fn test_batch_atomic_operations() {
let client = LocalClient::new();
let requests = vec![
LockRequest {
resource: "batch_resource_1".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
LockRequest {
resource: "batch_resource_2".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
];
let responses = client.acquire_multiple_atomic(requests).await.unwrap();
assert_eq!(responses.len(), 2);
assert!(responses[0].is_success());
assert!(responses[1].is_success());
}
#[tokio::test]
async fn test_batch_atomic_rollback() {
let client = LocalClient::new();
// First acquire a lock
let first_request = LockRequest {
resource: "rollback_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(first_request).await.unwrap();
let request =
LockRequest::new("test-resource", LockType::Exclusive, "test-owner").with_timeout(std::time::Duration::from_secs(30));
let response = client.acquire_exclusive(request).await.unwrap();
assert!(response.is_success());
// Try to acquire same resource in batch (should fail and rollback)
let requests = vec![
LockRequest {
resource: "rollback_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_2".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
LockRequest {
resource: "rollback_resource_2".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_2".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
];
let responses = client.acquire_multiple_atomic(requests).await.unwrap();
assert_eq!(responses.len(), 2);
assert!(responses[0].is_waiting()); // Should be waiting
assert!(responses[1].is_success()); // Second should succeed
}
#[tokio::test]
async fn test_concurrent_atomic_operations() {
let client = Arc::new(LocalClient::new());
let mut handles = vec![];
for i in 0..10 {
let client_clone = client.clone();
let handle = tokio::spawn(async move {
let requests = vec![
LockRequest {
resource: format!("concurrent_batch_{i}"),
lock_type: LockType::Exclusive,
owner: format!("owner_{i}"),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
LockRequest {
resource: format!("concurrent_batch_{i}_2"),
lock_type: LockType::Exclusive,
owner: format!("owner_{i}"),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
},
];
let responses = client_clone.acquire_multiple_atomic(requests).await.unwrap();
assert_eq!(responses.len(), 2);
// Release locks
for response in responses {
if response.is_success() {
let lock_id = &response.lock_info().unwrap().id;
let _ = client_clone.release(lock_id).await;
}
}
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
// Get the lock ID from the response
if let Some(lock_info) = response.lock_info() {
let result = client.release(&lock_info.id).await.unwrap();
assert!(result);
} else {
panic!("No lock info in response");
}
}
#[tokio::test]
async fn test_priority_upgrade() {
async fn test_local_client_is_local() {
let client = LocalClient::new();
// Acquire lock with normal priority
let normal_request = LockRequest {
resource: "priority_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "normal_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(normal_request).await.unwrap();
assert!(response.is_success());
// Try to acquire with high priority (should be waiting)
let high_request = LockRequest {
resource: "priority_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "high_owner".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::High,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(high_request.clone()).await.unwrap();
assert!(response.is_waiting());
// Release normal priority lock
let lock_id = &response.lock_info().unwrap().id;
let _ = client.release(lock_id).await;
// High priority should now acquire
let response = client.acquire_exclusive(high_request).await.unwrap();
assert!(response.is_success());
}
#[tokio::test]
async fn test_deadlock_detection() {
let client = LocalClient::new();
// Create a potential deadlock scenario
let request1 = LockRequest {
resource: "resource_a".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: true,
metadata: LockMetadata::default(),
};
let request2 = LockRequest {
resource: "resource_b".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_2".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: true,
metadata: LockMetadata::default(),
};
// Acquire first lock
let response1 = client.acquire_exclusive(request1).await.unwrap();
assert!(response1.is_success());
// Acquire second lock
let response2 = client.acquire_exclusive(request2).await.unwrap();
assert!(response2.is_success());
// Try to create deadlock
let deadlock_request1 = LockRequest {
resource: "resource_b".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: true,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(deadlock_request1).await.unwrap();
assert!(response.is_waiting() || response.is_failure());
}
#[tokio::test]
async fn test_wait_timeout() {
let client = LocalClient::new();
// Acquire lock
let request1 = LockRequest {
resource: "timeout_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_1".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: None,
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let response = client.acquire_exclusive(request1).await.unwrap();
assert!(response.is_success());
// Try to acquire with short wait timeout
let request2 = LockRequest {
resource: "timeout_resource".to_string(),
lock_type: LockType::Exclusive,
owner: "owner_2".to_string(),
timeout: std::time::Duration::from_secs(30),
wait_timeout: Some(std::time::Duration::from_millis(100)),
priority: LockPriority::Normal,
deadlock_detection: false,
metadata: LockMetadata::default(),
};
let start_time = std::time::Instant::now();
let response = client.acquire_exclusive(request2).await.unwrap();
let duration = start_time.elapsed();
assert!(response.is_failure() || response.is_waiting());
assert!(duration < std::time::Duration::from_secs(1));
}
#[tokio::test]
async fn test_deadlock_stats() {
let client = LocalClient::new();
let (count, last_time) = client.get_deadlock_stats().await.unwrap();
assert_eq!(count, 0);
assert!(last_time < std::time::SystemTime::now());
}
#[tokio::test]
async fn test_cleanup_expired_waits() {
let client = LocalClient::new();
// Add some wait items
let wait_item = WaitQueueItem::new("test_owner", LockType::Exclusive, LockPriority::Normal);
client.add_to_wait_queue("test_resource", wait_item).await;
// Cleanup with short timeout
client.cleanup_expired_waits(std::time::Duration::from_millis(1)).await;
// Wait queue should be empty
let position = client.get_wait_position("test_resource", "test_owner").await;
assert_eq!(position, 0);
assert!(client.is_local().await);
}
}

View File

@@ -25,13 +25,21 @@ use crate::{
/// Lock client trait
#[async_trait]
pub trait LockClient: Send + Sync {
pub trait LockClient: Send + Sync + std::fmt::Debug {
/// Acquire exclusive lock
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse>;
/// Acquire shared lock
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse>;
/// Acquire lock (generic method)
async fn acquire_lock(&self, request: LockRequest) -> Result<LockResponse> {
match request.lock_type {
crate::types::LockType::Exclusive => self.acquire_exclusive(request).await,
crate::types::LockType::Shared => self.acquire_shared(request).await,
}
}
/// Release lock
async fn release(&self, lock_id: &LockId) -> Result<bool>;

View File

@@ -14,284 +14,210 @@
use async_trait::async_trait;
use rustfs_protos::{node_service_time_out_client, proto_gen::node_service::GenerallyLockRequest};
use serde::{Deserialize, Serialize};
use tonic::Request;
use tracing::info;
use crate::{
error::{LockError, Result},
lock_args::LockArgs,
types::{LockId, LockInfo, LockRequest, LockResponse, LockStats},
};
/// Remote lock client
use super::LockClient;
/// RPC lock arguments for gRPC communication
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockArgs {
pub uid: String,
pub resources: Vec<String>,
pub owner: String,
pub source: String,
pub quorum: u32,
}
impl LockArgs {
fn from_request(request: &LockRequest, _is_shared: bool) -> Self {
Self {
uid: request.metadata.operation_id.clone().unwrap_or_default(),
resources: vec![request.resource.clone()],
owner: request.owner.clone(),
source: "remote".to_string(),
quorum: 1,
}
}
fn from_lock_id(lock_id: &LockId) -> Self {
Self {
uid: lock_id.as_str().to_string(),
resources: vec![lock_id.as_str().to_string()],
owner: "remote".to_string(),
source: "remote".to_string(),
quorum: 1,
}
}
}
/// Remote lock client implementation
#[derive(Debug, Clone)]
pub struct RemoteClient {
addr: String,
}
impl RemoteClient {
/// Create new remote client from endpoint string (for trait兼容)
pub fn new(endpoint: String) -> Self {
Self { addr: endpoint }
}
/// Create new remote client from url::Url兼容 namespace/distributed 场景)
pub fn from_url(url: url::Url) -> Self {
let addr = format!("{}://{}:{}", url.scheme(), url.host_str().unwrap(), url.port().unwrap());
Self { addr }
}
}
// 辅助方法:从 LockRequest 创建 LockArgs
impl LockArgs {
fn from_request(request: &LockRequest, _is_shared: bool) -> Self {
Self {
uid: uuid::Uuid::new_v4().to_string(),
resources: vec![request.resource.clone()],
owner: request.owner.clone(),
source: "remote_client".to_string(),
quorum: 1,
}
}
fn from_lock_id(lock_id: &LockId) -> Self {
Self {
uid: lock_id.to_string(),
resources: vec![],
owner: "remote_client".to_string(),
source: "remote_client".to_string(),
quorum: 1,
}
pub fn from_url(url: url::Url) -> Self {
Self { addr: url.to_string() }
}
}
#[async_trait]
impl super::LockClient for RemoteClient {
impl LockClient for RemoteClient {
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse> {
info!("remote acquire_exclusive");
info!("remote acquire_exclusive for {}", request.resource);
let args = LockArgs::from_request(&request, false);
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let req = Request::new(GenerallyLockRequest { args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))? });
let resp = client.lock(req).await.map_err(|e| LockError::internal(e.to_string()))?.into_inner();
let req = Request::new(GenerallyLockRequest {
args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?,
});
let resp = client
.lock(req)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = resp.error_info {
return Err(LockError::internal(error_info));
}
Ok(LockResponse {
success: resp.success,
lock_info: None, // 可扩展: 解析resp内容
error: None,
wait_time: std::time::Duration::ZERO,
position_in_queue: None,
})
Ok(LockResponse::success(
LockInfo {
id: LockId::new_deterministic(&request.resource),
resource: request.resource,
lock_type: request.lock_type,
status: crate::types::LockStatus::Acquired,
owner: request.owner,
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + request.timeout,
last_refreshed: std::time::SystemTime::now(),
metadata: request.metadata,
priority: request.priority,
wait_start_time: None,
},
std::time::Duration::ZERO,
))
}
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse> {
info!("remote acquire_shared");
info!("remote acquire_shared for {}", request.resource);
let args = LockArgs::from_request(&request, true);
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let req = Request::new(GenerallyLockRequest { args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))? });
let resp = client.r_lock(req).await.map_err(|e| LockError::internal(e.to_string()))?.into_inner();
let req = Request::new(GenerallyLockRequest {
args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?,
});
let resp = client
.r_lock(req)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = resp.error_info {
return Err(LockError::internal(error_info));
}
Ok(LockResponse {
success: resp.success,
lock_info: None,
error: None,
wait_time: std::time::Duration::ZERO,
position_in_queue: None,
})
Ok(LockResponse::success(
LockInfo {
id: LockId::new_deterministic(&request.resource),
resource: request.resource,
lock_type: request.lock_type,
status: crate::types::LockStatus::Acquired,
owner: request.owner,
acquired_at: std::time::SystemTime::now(),
expires_at: std::time::SystemTime::now() + request.timeout,
last_refreshed: std::time::SystemTime::now(),
metadata: request.metadata,
priority: request.priority,
wait_start_time: None,
},
std::time::Duration::ZERO,
))
}
async fn release(&self, lock_id: &LockId) -> Result<bool> {
info!("remote release");
info!("remote release for {}", lock_id);
let args = LockArgs::from_lock_id(lock_id);
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let req = Request::new(GenerallyLockRequest { args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))? });
let resp = client.un_lock(req).await.map_err(|e| LockError::internal(e.to_string()))?.into_inner();
let req = Request::new(GenerallyLockRequest {
args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?,
});
let resp = client
.un_lock(req)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = resp.error_info {
return Err(LockError::internal(error_info));
}
Ok(resp.success)
}
async fn refresh(&self, lock_id: &LockId) -> Result<bool> {
info!("remote refresh");
info!("remote refresh for {}", lock_id);
let args = LockArgs::from_lock_id(lock_id);
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let req = Request::new(GenerallyLockRequest { args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))? });
let resp = client.refresh(req).await.map_err(|e| LockError::internal(e.to_string()))?.into_inner();
let req = Request::new(GenerallyLockRequest {
args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?,
});
let resp = client
.refresh(req)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = resp.error_info {
return Err(LockError::internal(error_info));
}
Ok(resp.success)
}
async fn force_release(&self, lock_id: &LockId) -> Result<bool> {
info!("remote force_release");
info!("remote force_release for {}", lock_id);
let args = LockArgs::from_lock_id(lock_id);
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let req = Request::new(GenerallyLockRequest { args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))? });
let resp = client.force_un_lock(req).await.map_err(|e| LockError::internal(e.to_string()))?.into_inner();
let req = Request::new(GenerallyLockRequest {
args: serde_json::to_string(&args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?,
});
let resp = client
.force_un_lock(req)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = resp.error_info {
return Err(LockError::internal(error_info));
}
Ok(resp.success)
}
async fn check_status(&self, _lock_id: &LockId) -> Result<Option<LockInfo>> {
// 可扩展: 实现远程状态查询
// TODO: Implement remote status query
Ok(None)
}
async fn get_stats(&self) -> Result<LockStats> {
// 可扩展: 实现远程统计
// TODO: Implement remote statistics
Ok(LockStats::default())
}
async fn close(&self) -> Result<()> {
Ok(())
}
async fn is_online(&self) -> bool {
true
}
async fn is_local(&self) -> bool {
false
}
}
// 同时实现 Locker trait 以兼容现有调用
#[async_trait]
impl crate::Locker for RemoteClient {
async fn lock(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote lock");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.lock(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn unlock(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote unlock");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.un_lock(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn rlock(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote rlock");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.r_lock(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn runlock(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote runlock");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.r_un_lock(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn refresh(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote refresh");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.refresh(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn force_unlock(&mut self, args: &LockArgs) -> Result<bool> {
info!("remote force_unlock");
let args = serde_json::to_string(args).map_err(|e| LockError::internal(format!("Failed to serialize args: {e}")))?;
let mut client = node_service_time_out_client(&self.addr)
.await
.map_err(|err| LockError::internal(format!("can not get client, err: {err}")))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client
.force_un_lock(request)
.await
.map_err(|e| LockError::internal(e.to_string()))?
.into_inner();
if let Some(error_info) = response.error_info {
return Err(LockError::internal(error_info));
}
Ok(response.success)
}
async fn close(&self) {}
async fn is_online(&self) -> bool {
true

View File

@@ -15,259 +15,147 @@
use serde::{Deserialize, Serialize};
use std::time::Duration;
/// Lock manager configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Lock system configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LockConfig {
/// Lock acquisition timeout
#[serde(default = "default_timeout")]
pub timeout: Duration,
/// Retry interval
#[serde(default = "default_retry_interval")]
pub retry_interval: Duration,
/// Maximum retry attempts
#[serde(default = "default_max_retries")]
pub max_retries: usize,
/// Lock refresh interval
#[serde(default = "default_refresh_interval")]
pub refresh_interval: Duration,
/// Connection pool size
#[serde(default = "default_connection_pool_size")]
pub connection_pool_size: usize,
/// Enable metrics collection
#[serde(default = "default_enable_metrics")]
pub enable_metrics: bool,
/// Enable tracing
#[serde(default = "default_enable_tracing")]
pub enable_tracing: bool,
/// Distributed lock configuration
#[serde(default)]
pub distributed: DistributedConfig,
/// Whether distributed locking is enabled
pub distributed_enabled: bool,
/// Local lock configuration
#[serde(default)]
pub local: LocalConfig,
}
/// Distributed lock configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedConfig {
/// Quorum ratio (0.0-1.0)
#[serde(default = "default_quorum_ratio")]
pub quorum_ratio: f64,
/// Minimum quorum size
#[serde(default = "default_min_quorum")]
pub min_quorum: usize,
/// Enable auto refresh
#[serde(default = "default_auto_refresh")]
pub auto_refresh: bool,
/// Heartbeat interval
#[serde(default = "default_heartbeat_interval")]
pub heartbeat_interval: Duration,
pub local: LocalLockConfig,
/// Distributed lock configuration
pub distributed: DistributedLockConfig,
/// Network configuration
pub network: NetworkConfig,
}
/// Local lock configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocalConfig {
/// Maximum number of locks
#[serde(default = "default_max_locks")]
pub max_locks: usize,
/// Lock cleanup interval
#[serde(default = "default_cleanup_interval")]
pub cleanup_interval: Duration,
/// Lock expiry time
#[serde(default = "default_lock_expiry")]
pub lock_expiry: Duration,
pub struct LocalLockConfig {
/// Default lock timeout
pub default_timeout: Duration,
/// Default lock expiration time
pub default_expiration: Duration,
/// Maximum number of locks per resource
pub max_locks_per_resource: usize,
}
impl Default for LockConfig {
/// Distributed lock configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedLockConfig {
/// Total number of nodes in the cluster
pub total_nodes: usize,
/// Number of nodes that can fail (tolerance)
pub tolerance: usize,
/// Lock acquisition timeout
pub acquisition_timeout: Duration,
/// Lock refresh interval
pub refresh_interval: Duration,
/// Lock expiration time
pub expiration_time: Duration,
/// Retry interval for failed operations
pub retry_interval: Duration,
/// Maximum number of retry attempts
pub max_retries: usize,
}
/// Network configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
/// Connection timeout
pub connection_timeout: Duration,
/// Request timeout
pub request_timeout: Duration,
/// Keep-alive interval
pub keep_alive_interval: Duration,
/// Maximum connection pool size
pub max_connections: usize,
}
impl Default for LocalLockConfig {
fn default() -> Self {
Self {
timeout: default_timeout(),
retry_interval: default_retry_interval(),
max_retries: default_max_retries(),
refresh_interval: default_refresh_interval(),
connection_pool_size: default_connection_pool_size(),
enable_metrics: default_enable_metrics(),
enable_tracing: default_enable_tracing(),
distributed: DistributedConfig::default(),
local: LocalConfig::default(),
default_timeout: Duration::from_secs(30),
default_expiration: Duration::from_secs(60),
max_locks_per_resource: 1000,
}
}
}
impl Default for DistributedConfig {
impl Default for DistributedLockConfig {
fn default() -> Self {
Self {
quorum_ratio: default_quorum_ratio(),
min_quorum: default_min_quorum(),
auto_refresh: default_auto_refresh(),
heartbeat_interval: default_heartbeat_interval(),
total_nodes: 3,
tolerance: 1,
acquisition_timeout: Duration::from_secs(30),
refresh_interval: Duration::from_secs(10),
expiration_time: Duration::from_secs(60),
retry_interval: Duration::from_millis(250),
max_retries: 10,
}
}
}
impl Default for LocalConfig {
impl Default for NetworkConfig {
fn default() -> Self {
Self {
max_locks: default_max_locks(),
cleanup_interval: default_cleanup_interval(),
lock_expiry: default_lock_expiry(),
connection_timeout: Duration::from_secs(5),
request_timeout: Duration::from_secs(30),
keep_alive_interval: Duration::from_secs(30),
max_connections: 100,
}
}
}
// Default value functions
fn default_timeout() -> Duration {
Duration::from_secs(30)
}
fn default_retry_interval() -> Duration {
Duration::from_millis(100)
}
fn default_max_retries() -> usize {
3
}
fn default_refresh_interval() -> Duration {
Duration::from_secs(10)
}
fn default_connection_pool_size() -> usize {
10
}
fn default_enable_metrics() -> bool {
true
}
fn default_enable_tracing() -> bool {
true
}
fn default_quorum_ratio() -> f64 {
0.5
}
fn default_min_quorum() -> usize {
1
}
fn default_auto_refresh() -> bool {
true
}
fn default_heartbeat_interval() -> Duration {
Duration::from_secs(5)
}
fn default_max_locks() -> usize {
10000
}
fn default_cleanup_interval() -> Duration {
Duration::from_secs(60)
}
fn default_lock_expiry() -> Duration {
Duration::from_secs(300)
}
impl LockConfig {
/// Create minimal configuration
pub fn minimal() -> Self {
/// Create new lock configuration
pub fn new() -> Self {
Self::default()
}
/// Create distributed lock configuration
pub fn distributed(total_nodes: usize, tolerance: usize) -> Self {
Self {
timeout: Duration::from_secs(10),
retry_interval: Duration::from_millis(50),
max_retries: 1,
refresh_interval: Duration::from_secs(5),
connection_pool_size: 5,
enable_metrics: false,
enable_tracing: false,
distributed: DistributedConfig {
quorum_ratio: 0.5,
min_quorum: 1,
auto_refresh: false,
heartbeat_interval: Duration::from_secs(10),
},
local: LocalConfig {
max_locks: 1000,
cleanup_interval: Duration::from_secs(30),
lock_expiry: Duration::from_secs(60),
distributed_enabled: true,
distributed: DistributedLockConfig {
total_nodes,
tolerance,
..Default::default()
},
..Default::default()
}
}
/// Create high performance configuration
pub fn high_performance() -> Self {
/// Create local-only lock configuration
pub fn local() -> Self {
Self {
timeout: Duration::from_secs(60),
retry_interval: Duration::from_millis(10),
max_retries: 5,
refresh_interval: Duration::from_secs(30),
connection_pool_size: 50,
enable_metrics: true,
enable_tracing: true,
distributed: DistributedConfig {
quorum_ratio: 0.7,
min_quorum: 3,
auto_refresh: true,
heartbeat_interval: Duration::from_secs(2),
},
local: LocalConfig {
max_locks: 100000,
cleanup_interval: Duration::from_secs(300),
lock_expiry: Duration::from_secs(1800),
},
distributed_enabled: false,
..Default::default()
}
}
/// Validate configuration
pub fn validate(&self) -> crate::error::Result<()> {
if self.timeout.is_zero() {
return Err(crate::error::LockError::configuration("Timeout must be greater than zero"));
}
if self.retry_interval.is_zero() {
return Err(crate::error::LockError::configuration("Retry interval must be greater than zero"));
}
if self.max_retries == 0 {
return Err(crate::error::LockError::configuration("Max retries must be greater than zero"));
}
if self.distributed.quorum_ratio < 0.0 || self.distributed.quorum_ratio > 1.0 {
return Err(crate::error::LockError::configuration("Quorum ratio must be between 0.0 and 1.0"));
}
if self.distributed.min_quorum == 0 {
return Err(crate::error::LockError::configuration("Minimum quorum must be greater than zero"));
}
Ok(())
/// Check if distributed locking is enabled
pub fn is_distributed(&self) -> bool {
self.distributed_enabled
}
/// Calculate quorum size for distributed locks
pub fn calculate_quorum(&self, total_nodes: usize) -> usize {
let quorum = (total_nodes as f64 * self.distributed.quorum_ratio).ceil() as usize;
std::cmp::max(quorum, self.distributed.min_quorum)
/// Get quorum size for distributed locks
pub fn get_quorum_size(&self) -> usize {
self.distributed.total_nodes - self.distributed.tolerance
}
/// Calculate fault tolerance
pub fn calculate_tolerance(&self, total_nodes: usize) -> usize {
total_nodes - self.calculate_quorum(total_nodes)
/// Check if quorum configuration is valid
pub fn is_quorum_valid(&self) -> bool {
self.distributed.tolerance < self.distributed.total_nodes
}
/// Get effective timeout
pub fn get_effective_timeout(&self, timeout: Option<Duration>) -> Duration {
timeout.unwrap_or(self.local.default_timeout)
}
/// Get effective expiration
pub fn get_effective_expiration(&self, expiration: Option<Duration>) -> Duration {
expiration.unwrap_or(self.local.default_expiration)
}
}
@@ -276,57 +164,38 @@ mod tests {
use super::*;
#[test]
fn test_default_config() {
fn test_lock_config_default() {
let config = LockConfig::default();
assert!(!config.timeout.is_zero());
assert!(!config.retry_interval.is_zero());
assert!(config.max_retries > 0);
assert!(!config.distributed_enabled);
assert_eq!(config.local.default_timeout, Duration::from_secs(30));
}
#[test]
fn test_minimal_config() {
let config = LockConfig::minimal();
assert_eq!(config.timeout, Duration::from_secs(10));
assert_eq!(config.max_retries, 1);
assert!(!config.enable_metrics);
fn test_lock_config_distributed() {
let config = LockConfig::distributed(5, 2);
assert!(config.distributed_enabled);
assert_eq!(config.distributed.total_nodes, 5);
assert_eq!(config.distributed.tolerance, 2);
assert_eq!(config.get_quorum_size(), 3);
}
#[test]
fn test_high_performance_config() {
let config = LockConfig::high_performance();
assert_eq!(config.timeout, Duration::from_secs(60));
assert_eq!(config.max_retries, 5);
assert!(config.enable_metrics);
fn test_lock_config_local() {
let config = LockConfig::local();
assert!(!config.distributed_enabled);
}
#[test]
fn test_config_validation() {
let mut config = LockConfig::default();
assert!(config.validate().is_ok());
config.timeout = Duration::ZERO;
assert!(config.validate().is_err());
config = LockConfig::default();
config.distributed.quorum_ratio = 1.5;
assert!(config.validate().is_err());
}
#[test]
fn test_quorum_calculation() {
fn test_effective_timeout() {
let config = LockConfig::default();
assert_eq!(config.calculate_quorum(10), 5);
assert_eq!(config.calculate_quorum(3), 2);
assert_eq!(config.calculate_tolerance(10), 5);
assert_eq!(config.get_effective_timeout(None), Duration::from_secs(30));
assert_eq!(config.get_effective_timeout(Some(Duration::from_secs(10))), Duration::from_secs(10));
}
#[test]
fn test_serialization() {
fn test_effective_expiration() {
let config = LockConfig::default();
let serialized = serde_json::to_string(&config).unwrap();
let deserialized: LockConfig = serde_json::from_str(&serialized).unwrap();
assert_eq!(config.timeout, deserialized.timeout);
assert_eq!(config.max_retries, deserialized.max_retries);
assert_eq!(config.get_effective_expiration(None), Duration::from_secs(60));
assert_eq!(config.get_effective_expiration(Some(Duration::from_secs(30))), Duration::from_secs(30));
}
}

View File

@@ -1,813 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use async_trait::async_trait;
use dashmap::DashMap;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::Mutex;
use tracing::{debug, error, info, instrument, warn};
use crate::{
Locker,
config::LockConfig,
error::{LockError, Result},
client::remote::RemoteClient,
types::{LockId, LockInfo, LockRequest, LockResponse, LockStats, LockStatus, LockType},
};
/// Distributed lock configuration constants
const DEFAULT_REFRESH_INTERVAL: Duration = Duration::from_secs(10);
const DEFAULT_RETRY_MIN_INTERVAL: Duration = Duration::from_millis(250);
const DEFAULT_LOCK_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_MAX_RETRIES: usize = 10;
/// Distributed lock state
#[derive(Debug, Clone, PartialEq)]
pub enum DistributedLockState {
/// Unlocked
Unlocked,
/// Read locked
ReadLocked { count: usize, owners: Vec<String> },
/// Write locked
WriteLocked { owner: String },
}
/// Distributed lock resource information
#[derive(Debug, Clone)]
pub struct DistributedResourceInfo {
/// Resource name
pub resource: String,
/// Current lock state
pub state: DistributedLockState,
/// Last update time
pub last_updated: SystemTime,
/// Lock holder information
pub lock_holders: HashMap<String, LockInfo>,
}
/// Distributed lock options
#[derive(Debug, Clone)]
pub struct DistributedLockOptions {
/// Lock acquisition timeout
pub timeout: Duration,
/// Retry interval
pub retry_interval: Duration,
/// Maximum retry attempts
pub max_retries: usize,
/// Whether to enable auto-refresh
pub auto_refresh: bool,
/// Lock refresh interval
pub refresh_interval: Duration,
/// Whether to enable fault tolerance mode
pub fault_tolerant: bool,
}
impl Default for DistributedLockOptions {
fn default() -> Self {
Self {
timeout: DEFAULT_LOCK_TIMEOUT,
retry_interval: DEFAULT_RETRY_MIN_INTERVAL,
max_retries: DEFAULT_MAX_RETRIES,
auto_refresh: true,
refresh_interval: DEFAULT_REFRESH_INTERVAL,
fault_tolerant: true,
}
}
}
/// Distributed lock handle
#[derive(Debug, Clone)]
pub struct DistributedLockHandle {
/// Lock ID
pub lock_id: LockId,
/// Resource name
pub resource: String,
/// Lock type
pub lock_type: LockType,
/// Owner
pub owner: String,
/// Acquisition time
pub acquired_at: SystemTime,
/// Last refresh time
pub last_refreshed: SystemTime,
/// Whether to auto-refresh
pub auto_refresh: bool,
/// Refresh interval
pub refresh_interval: Duration,
/// Manager reference
manager: Arc<DistributedLockManager>,
}
impl DistributedLockHandle {
/// Create a new distributed lock handle
fn new(
lock_id: LockId,
resource: String,
lock_type: LockType,
owner: String,
manager: Arc<DistributedLockManager>,
options: &DistributedLockOptions,
) -> Self {
let now = SystemTime::now();
Self {
lock_id,
resource,
lock_type,
owner,
acquired_at: now,
last_refreshed: now,
auto_refresh: options.auto_refresh,
refresh_interval: options.refresh_interval,
manager,
}
}
/// Refresh the lock
#[instrument(skip(self))]
pub async fn refresh(&mut self) -> Result<bool> {
if !self.auto_refresh {
return Ok(true);
}
let now = SystemTime::now();
if now.duration_since(self.last_refreshed).unwrap_or_default() < self.refresh_interval {
return Ok(true);
}
match self.manager.refresh_lock(&self.lock_id).await {
Ok(success) => {
if success {
self.last_refreshed = now;
debug!("Successfully refreshed lock: {}", self.lock_id);
}
Ok(success)
}
Err(e) => {
error!("Failed to refresh lock {}: {}", self.lock_id, e);
Err(e)
}
}
}
/// Release the lock
#[instrument(skip(self))]
pub async fn release(self) -> Result<bool> {
self.manager.release_lock(&self.lock_id).await
}
/// Force release the lock
#[instrument(skip(self))]
pub async fn force_release(self) -> Result<bool> {
self.manager.force_release_lock(&self.lock_id).await
}
/// Check lock status
#[instrument(skip(self))]
pub async fn check_status(&self) -> Result<Option<LockInfo>> {
self.manager.check_lock_status(&self.lock_id).await
}
}
/// Distributed lock manager
///
/// Implements quorum-based distributed read-write locks with fault tolerance and auto-refresh
#[derive(Debug)]
pub struct DistributedLockManager {
/// Configuration
config: Arc<LockConfig>,
/// Resource state mapping
resources: Arc<DashMap<String, DistributedResourceInfo>>,
/// Active lock handles mapping
active_handles: Arc<DashMap<LockId, Arc<DistributedLockHandle>>>,
/// Lock options
options: DistributedLockOptions,
/// Statistics
stats: Arc<Mutex<LockStats>>,
/// Shutdown flag
shutdown_flag: Arc<Mutex<bool>>,
/// Remote client
remote_client: Arc<Mutex<RemoteClient>>,
}
impl DistributedLockManager {
/// Create a new distributed lock manager
pub fn new(config: Arc<LockConfig>, remote_url: url::Url) -> Result<Self> {
let client = RemoteClient::from_url(remote_url);
Ok(Self {
config,
resources: Arc::new(DashMap::new()),
active_handles: Arc::new(DashMap::new()),
options: DistributedLockOptions::default(),
stats: Arc::new(Mutex::new(LockStats::default())),
shutdown_flag: Arc::new(Mutex::new(false)),
remote_client: Arc::new(Mutex::new(client)),
})
}
/// Create a distributed lock manager with custom options
pub fn with_options(config: Arc<LockConfig>, remote_url: url::Url, options: DistributedLockOptions) -> Result<Self> {
let client = RemoteClient::from_url(remote_url);
Ok(Self {
config,
resources: Arc::new(DashMap::new()),
active_handles: Arc::new(DashMap::new()),
options,
stats: Arc::new(Mutex::new(LockStats::default())),
shutdown_flag: Arc::new(Mutex::new(false)),
remote_client: Arc::new(Mutex::new(client)),
})
}
/// Calculate quorum
fn calculate_quorum(&self) -> (usize, usize) {
// Simplified implementation: use minimum quorum from config
let total_nodes = 1; // Currently only one node
let write_quorum = self.config.distributed.min_quorum;
let read_quorum = total_nodes - write_quorum + 1;
(write_quorum, read_quorum)
}
/// Acquire distributed write lock (improved atomic version)
async fn acquire_distributed_write_lock(
&self,
resource: &str,
owner: &str,
options: &DistributedLockOptions,
) -> Result<DistributedLockHandle> {
let start_time = Instant::now();
let (write_quorum, _) = self.calculate_quorum();
let mut retry_count = 0;
loop {
if retry_count >= options.max_retries {
return Err(LockError::timeout(resource, options.timeout));
}
// Atomic check of local resource state
if let Some(resource_info) = self.resources.get(resource) {
match &resource_info.state {
DistributedLockState::WriteLocked { owner: existing_owner } => {
if existing_owner != owner {
return Err(LockError::already_locked(resource, existing_owner));
}
}
DistributedLockState::ReadLocked { owners, .. } => {
if !owners.contains(&owner.to_string()) {
return Err(LockError::already_locked(resource, "other readers"));
}
}
DistributedLockState::Unlocked => {}
}
}
// Use quorum mechanism to atomically acquire distributed lock
match self.acquire_quorum_lock(resource, owner, write_quorum, options).await {
Ok(lock_id) => {
let handle = DistributedLockHandle::new(
lock_id.clone(),
resource.to_string(),
LockType::Exclusive,
owner.to_string(),
Arc::new(self.clone_for_handle()),
options,
);
// Atomically update local state
self.update_resource_state(
resource,
DistributedLockState::WriteLocked {
owner: owner.to_string(),
},
)
.await;
// Store active handle
self.active_handles.insert(lock_id, Arc::new(handle.clone()));
info!(
"Successfully acquired distributed write lock for {} in {:?}",
resource,
start_time.elapsed()
);
return Ok(handle);
}
Err(e) => {
warn!("Failed to acquire quorum lock for {}: {}", resource, e);
}
}
retry_count += 1;
tokio::time::sleep(options.retry_interval).await;
}
}
/// Acquire distributed read lock (improved atomic version)
async fn acquire_distributed_read_lock(
&self,
resource: &str,
owner: &str,
options: &DistributedLockOptions,
) -> Result<DistributedLockHandle> {
let start_time = Instant::now();
let (_, read_quorum) = self.calculate_quorum();
let mut retry_count = 0;
loop {
if retry_count >= options.max_retries {
return Err(LockError::timeout(resource, options.timeout));
}
// Atomic check of local resource state
if let Some(resource_info) = self.resources.get(resource) {
match &resource_info.state {
DistributedLockState::WriteLocked { owner: existing_owner } => {
if existing_owner != owner {
return Err(LockError::already_locked(resource, existing_owner));
}
}
DistributedLockState::ReadLocked { .. } => {
// Read locks can be shared
}
DistributedLockState::Unlocked => {}
}
}
// Use quorum mechanism to atomically acquire distributed read lock
match self.acquire_quorum_read_lock(resource, owner, read_quorum, options).await {
Ok(lock_id) => {
let handle = DistributedLockHandle::new(
lock_id.clone(),
resource.to_string(),
LockType::Shared,
owner.to_string(),
Arc::new(self.clone_for_handle()),
options,
);
// Atomically update local state
self.update_resource_read_state(resource, owner, &lock_id).await;
// Store active handle
self.active_handles.insert(lock_id, Arc::new(handle.clone()));
info!(
"Successfully acquired distributed read lock for {} in {:?}",
resource,
start_time.elapsed()
);
return Ok(handle);
}
Err(e) => {
warn!("Failed to acquire quorum read lock for {}: {}", resource, e);
}
}
retry_count += 1;
tokio::time::sleep(options.retry_interval).await;
}
}
/// Atomically acquire write lock using quorum mechanism
async fn acquire_quorum_lock(
&self,
resource: &str,
owner: &str,
quorum: usize,
options: &DistributedLockOptions,
) -> Result<LockId> {
let mut success_count = 0;
let mut errors = Vec::new();
let lock_id = LockId::new();
// Concurrently attempt to acquire locks on multiple nodes
let mut remote_client = self.remote_client.lock().await;
let lock_args = crate::lock_args::LockArgs {
uid: lock_id.to_string(),
resources: vec![resource.to_string()],
owner: owner.to_string(),
source: "distributed".to_string(),
quorum,
};
// Attempt to acquire lock
match remote_client.lock(&lock_args).await {
Ok(success) if success => {
success_count += 1;
}
Ok(_) => {
// Acquisition failed
}
Err(e) => {
errors.push(e);
}
}
// Check if quorum is reached
if success_count >= quorum {
Ok(lock_id)
} else {
// Rollback acquired locks
self.rollback_partial_locks(resource, owner).await?;
Err(LockError::timeout(resource, options.timeout))
}
}
/// Atomically acquire read lock using quorum mechanism
async fn acquire_quorum_read_lock(
&self,
resource: &str,
owner: &str,
quorum: usize,
options: &DistributedLockOptions,
) -> Result<LockId> {
let mut success_count = 0;
let mut errors = Vec::new();
let lock_id = LockId::new();
// Concurrently attempt to acquire read locks on multiple nodes
let mut remote_client = self.remote_client.lock().await;
let lock_args = crate::lock_args::LockArgs {
uid: lock_id.to_string(),
resources: vec![resource.to_string()],
owner: owner.to_string(),
source: "distributed".to_string(),
quorum,
};
// Attempt to acquire read lock
match remote_client.rlock(&lock_args).await {
Ok(success) if success => {
success_count += 1;
}
Ok(_) => {
// Acquisition failed
}
Err(e) => {
errors.push(e);
}
}
// Check if quorum is reached
if success_count >= quorum {
Ok(lock_id)
} else {
// Rollback acquired locks
self.rollback_partial_read_locks(resource, owner).await?;
Err(LockError::timeout(resource, options.timeout))
}
}
/// Rollback partially acquired write locks
async fn rollback_partial_locks(&self, resource: &str, owner: &str) -> Result<()> {
let mut remote_client = self.remote_client.lock().await;
let lock_args = crate::lock_args::LockArgs {
uid: LockId::new().to_string(),
resources: vec![resource.to_string()],
owner: owner.to_string(),
source: "distributed".to_string(),
quorum: 1,
};
// Attempt to release lock
let _ = remote_client.unlock(&lock_args).await;
Ok(())
}
/// Rollback partially acquired read locks
async fn rollback_partial_read_locks(&self, resource: &str, owner: &str) -> Result<()> {
let mut remote_client = self.remote_client.lock().await;
let lock_args = crate::lock_args::LockArgs {
uid: LockId::new().to_string(),
resources: vec![resource.to_string()],
owner: owner.to_string(),
source: "distributed".to_string(),
quorum: 1,
};
// Attempt to release read lock
let _ = remote_client.runlock(&lock_args).await;
Ok(())
}
/// Update resource state
async fn update_resource_state(&self, resource: &str, state: DistributedLockState) {
let resource_info = DistributedResourceInfo {
resource: resource.to_string(),
state,
last_updated: SystemTime::now(),
lock_holders: HashMap::new(),
};
self.resources.insert(resource.to_string(), resource_info);
}
/// Update resource read lock state
async fn update_resource_read_state(&self, resource: &str, owner: &str, _lock_id: &LockId) {
if let Some(mut resource_info) = self.resources.get_mut(resource) {
match &mut resource_info.state {
DistributedLockState::ReadLocked { count, owners } => {
*count += 1;
if !owners.contains(&owner.to_string()) {
owners.push(owner.to_string());
}
}
DistributedLockState::Unlocked => {
resource_info.state = DistributedLockState::ReadLocked {
count: 1,
owners: vec![owner.to_string()],
};
}
_ => {
// Other states remain unchanged
}
}
resource_info.last_updated = SystemTime::now();
} else {
let resource_info = DistributedResourceInfo {
resource: resource.to_string(),
state: DistributedLockState::ReadLocked {
count: 1,
owners: vec![owner.to_string()],
},
last_updated: SystemTime::now(),
lock_holders: HashMap::new(),
};
self.resources.insert(resource.to_string(), resource_info);
}
}
/// Refresh lock
async fn refresh_lock(&self, lock_id: &LockId) -> Result<bool> {
if let Some(_handle) = self.active_handles.get(lock_id) {
let mut remote_client = self.remote_client.lock().await;
// Create LockArgs
let lock_args = crate::lock_args::LockArgs {
uid: lock_id.to_string(),
resources: vec![],
owner: "distributed".to_string(),
source: "distributed".to_string(),
quorum: 1,
};
remote_client.refresh(&lock_args).await
} else {
Ok(false)
}
}
/// Release lock
async fn release_lock(&self, lock_id: &LockId) -> Result<bool> {
if let Some(_handle) = self.active_handles.get(lock_id) {
let mut remote_client = self.remote_client.lock().await;
// Create LockArgs
let lock_args = crate::lock_args::LockArgs {
uid: lock_id.to_string(),
resources: vec![],
owner: "distributed".to_string(),
source: "distributed".to_string(),
quorum: 1,
};
let result = remote_client.unlock(&lock_args).await?;
if result {
self.active_handles.remove(lock_id);
}
Ok(result)
} else {
Ok(false)
}
}
/// Force release lock
async fn force_release_lock(&self, lock_id: &LockId) -> Result<bool> {
if let Some(_handle) = self.active_handles.get(lock_id) {
let mut remote_client = self.remote_client.lock().await;
// Create LockArgs
let lock_args = crate::lock_args::LockArgs {
uid: lock_id.to_string(),
resources: vec![],
owner: "distributed".to_string(),
source: "distributed".to_string(),
quorum: 1,
};
let result = remote_client.force_unlock(&lock_args).await?;
if result {
self.active_handles.remove(lock_id);
}
Ok(result)
} else {
Ok(false)
}
}
/// Check lock status
async fn check_lock_status(&self, _lock_id: &LockId) -> Result<Option<LockInfo>> {
// Implement lock status check logic
Ok(None)
}
/// Update statistics
async fn update_stats(&self, acquired: bool) {
let mut stats = self.stats.lock().await;
if acquired {
stats.total_locks += 1;
}
stats.last_updated = SystemTime::now();
}
/// Clone for handle
fn clone_for_handle(&self) -> Self {
Self {
config: Arc::clone(&self.config),
resources: Arc::clone(&self.resources),
active_handles: Arc::clone(&self.active_handles),
options: self.options.clone(),
stats: Arc::clone(&self.stats),
shutdown_flag: Arc::clone(&self.shutdown_flag),
remote_client: Arc::clone(&self.remote_client),
}
}
}
#[async_trait]
impl super::LockManager for DistributedLockManager {
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse> {
let start_time = std::time::SystemTime::now();
match self
.acquire_distributed_write_lock(&request.resource, &request.owner, &self.options)
.await
{
Ok(handle) => {
self.update_stats(true).await;
Ok(LockResponse::success(
LockInfo {
id: handle.lock_id,
resource: handle.resource,
lock_type: handle.lock_type,
status: LockStatus::Acquired,
owner: handle.owner,
acquired_at: handle.acquired_at,
expires_at: SystemTime::now() + request.timeout,
last_refreshed: handle.last_refreshed,
metadata: request.metadata,
priority: request.priority,
wait_start_time: None,
},
crate::utils::duration_between(start_time, std::time::SystemTime::now()),
))
}
Err(e) => Ok(LockResponse::failure(
e.to_string(),
crate::utils::duration_between(start_time, std::time::SystemTime::now()),
)),
}
}
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse> {
let start_time = std::time::SystemTime::now();
match self
.acquire_distributed_read_lock(&request.resource, &request.owner, &self.options)
.await
{
Ok(handle) => {
self.update_stats(true).await;
Ok(LockResponse::success(
LockInfo {
id: handle.lock_id,
resource: handle.resource,
lock_type: handle.lock_type,
status: LockStatus::Acquired,
owner: handle.owner,
acquired_at: handle.acquired_at,
expires_at: SystemTime::now() + request.timeout,
last_refreshed: handle.last_refreshed,
metadata: request.metadata,
priority: request.priority,
wait_start_time: None,
},
crate::utils::duration_between(start_time, std::time::SystemTime::now()),
))
}
Err(e) => Ok(LockResponse::failure(
e.to_string(),
crate::utils::duration_between(start_time, std::time::SystemTime::now()),
)),
}
}
async fn release(&self, lock_id: &LockId) -> Result<bool> {
self.release_lock(lock_id).await
}
async fn refresh(&self, lock_id: &LockId) -> Result<bool> {
self.refresh_lock(lock_id).await
}
async fn force_release(&self, lock_id: &LockId) -> Result<bool> {
self.force_release_lock(lock_id).await
}
async fn check_status(&self, lock_id: &LockId) -> Result<Option<LockInfo>> {
self.check_lock_status(lock_id).await
}
async fn get_stats(&self) -> Result<LockStats> {
let stats = self.stats.lock().await;
Ok(stats.clone())
}
async fn shutdown(&self) -> Result<()> {
let mut shutdown_flag = self.shutdown_flag.lock().await;
*shutdown_flag = true;
// Clean up all active handles
self.active_handles.clear();
// Clean up all resource states
self.resources.clear();
info!("Distributed lock manager shutdown completed");
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::LockConfig;
#[tokio::test]
async fn test_distributed_lock_manager_creation() {
let config = Arc::new(LockConfig::default());
let remote_url = url::Url::parse("http://localhost:8080").unwrap();
let manager = DistributedLockManager::new(config, remote_url);
assert!(manager.is_ok());
}
#[tokio::test]
async fn test_quorum_calculation() {
let config = Arc::new(LockConfig::default());
let remote_url = url::Url::parse("http://localhost:8080").unwrap();
let manager = DistributedLockManager::new(config, remote_url).unwrap();
let (write_quorum, read_quorum) = manager.calculate_quorum();
assert!(write_quorum > 0);
assert!(read_quorum > 0);
}
#[tokio::test]
async fn test_distributed_lock_options_default() {
let options = DistributedLockOptions::default();
assert_eq!(options.timeout, DEFAULT_LOCK_TIMEOUT);
assert_eq!(options.retry_interval, DEFAULT_RETRY_MIN_INTERVAL);
assert_eq!(options.max_retries, DEFAULT_MAX_RETRIES);
assert!(options.auto_refresh);
assert_eq!(options.refresh_interval, DEFAULT_REFRESH_INTERVAL);
assert!(options.fault_tolerant);
}
#[tokio::test]
async fn test_distributed_lock_handle_creation() {
let config = Arc::new(LockConfig::default());
let remote_url = url::Url::parse("http://localhost:8080").unwrap();
let manager = Arc::new(DistributedLockManager::new(config, remote_url).unwrap());
let lock_id = LockId::new();
let options = DistributedLockOptions::default();
let handle = DistributedLockHandle::new(
lock_id.clone(),
"test-resource".to_string(),
LockType::Exclusive,
"test-owner".to_string(),
manager,
&options,
);
assert_eq!(handle.lock_id, lock_id);
assert_eq!(handle.resource, "test-resource");
assert_eq!(handle.lock_type, LockType::Exclusive);
assert_eq!(handle.owner, "test-owner");
assert!(handle.auto_refresh);
}
}

View File

@@ -1,584 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use async_trait::async_trait;
use dashmap::DashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use crate::{
config::LockConfig,
error::{LockError, Result},
types::{LockId, LockInfo, LockRequest, LockResponse, LockStats},
};
/// Local lock manager trait, defines core operations for local locks
#[async_trait::async_trait]
pub trait LocalLockManager: Send + Sync {
/// Acquire write lock
///
/// # Parameters
/// - resource: Unique resource identifier (e.g., path)
/// - owner: Lock holder identifier
/// - timeout: Timeout for acquiring the lock
///
/// # Returns
/// - Ok(true): Successfully acquired
/// - Ok(false): Timeout without acquiring lock
/// - Err: Error occurred
async fn lock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool>;
/// Acquire read lock
async fn rlock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool>;
/// Release write lock
async fn unlock(&self, resource: &str, owner: &str) -> std::io::Result<()>;
/// Release read lock
async fn runlock(&self, resource: &str, owner: &str) -> std::io::Result<()>;
/// Check if resource is locked (read or write)
async fn is_locked(&self, resource: &str) -> bool;
}
/// Basic implementation struct for local lock manager
///
/// Internally maintains a mapping table from resources to lock objects, using DashMap for high concurrency performance
#[derive(Debug)]
pub struct LocalLockMap {
/// Resource lock mapping table, key is unique resource identifier, value is lock object
/// Uses DashMap to implement sharded locks for improved concurrency performance
locks: Arc<DashMap<String, Arc<RwLock<LocalLockEntry>>>>,
}
/// Lock object for a single resource
#[derive(Debug)]
pub struct LocalLockEntry {
/// Current write lock holder
pub writer: Option<String>,
/// Set of current read lock holders
pub readers: Vec<String>,
/// Lock expiration time (set when either write or read lock is held, None means no timeout)
pub expires_at: Option<Instant>,
}
impl LocalLockMap {
/// Create a new local lock manager
pub fn new() -> Self {
let map = Self {
locks: Arc::new(DashMap::new()),
};
map.spawn_expiry_task();
map
}
/// Start background task to periodically clean up expired locks
fn spawn_expiry_task(&self) {
let locks = self.locks.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
interval.tick().await;
let now = Instant::now();
let mut to_remove = Vec::new();
// DashMap's iter() method provides concurrency-safe iteration
for item in locks.iter() {
let mut entry_guard = item.value().write().await;
if let Some(exp) = entry_guard.expires_at {
if exp <= now {
// Clear lock content
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
// If entry is completely empty, mark for deletion
if entry_guard.writer.is_none() && entry_guard.readers.is_empty() {
to_remove.push(item.key().clone());
}
}
}
}
// Remove empty entries
for key in to_remove {
locks.remove(&key);
}
}
});
}
/// Batch acquire write locks
///
/// Attempt to acquire write locks on all resources, if any resource fails to lock, rollback all previously locked resources
pub async fn lock_batch(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<bool> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.lock_with_ttl(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(resource.clone());
}
Ok(false) => {
// Rollback previously locked resources
for locked_resource in locked {
let _ = self.unlock(&locked_resource, owner).await;
}
return Ok(false);
}
Err(e) => {
// Rollback previously locked resources
for locked_resource in locked {
let _ = self.unlock(&locked_resource, owner).await;
}
return Err(crate::error::LockError::internal(format!("Lock failed: {e}")));
}
}
}
Ok(true)
}
/// Batch release write locks
pub async fn unlock_batch(&self, resources: &[String], owner: &str) -> crate::error::Result<()> {
for resource in resources {
let _ = self.unlock(resource, owner).await;
}
Ok(())
}
/// Batch acquire read locks
pub async fn rlock_batch(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<bool> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.rlock_with_ttl(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(resource.clone());
}
Ok(false) => {
// Rollback previously locked resources
for locked_resource in locked {
let _ = self.runlock(&locked_resource, owner).await;
}
return Ok(false);
}
Err(e) => {
// Rollback previously locked resources
for locked_resource in locked {
let _ = self.runlock(&locked_resource, owner).await;
}
return Err(crate::error::LockError::internal(format!("Read lock failed: {e}")));
}
}
}
Ok(true)
}
/// Batch release read locks
pub async fn runlock_batch(&self, resources: &[String], owner: &str) -> crate::error::Result<()> {
for resource in resources {
let _ = self.runlock(resource, owner).await;
}
Ok(())
}
}
/// Local lock manager
pub struct LocalLockManagerImpl {
config: Arc<LockConfig>,
}
impl LocalLockManagerImpl {
/// Create a new local lock manager
pub fn new(config: Arc<LockConfig>) -> Result<Self> {
Ok(Self { config })
}
}
#[async_trait]
impl super::LockManager for LocalLockManagerImpl {
async fn acquire_exclusive(&self, _request: LockRequest) -> Result<LockResponse> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn acquire_shared(&self, _request: LockRequest) -> Result<LockResponse> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn release(&self, _lock_id: &LockId) -> Result<bool> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn refresh(&self, _lock_id: &LockId) -> Result<bool> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn force_release(&self, _lock_id: &LockId) -> Result<bool> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn check_status(&self, _lock_id: &LockId) -> Result<Option<LockInfo>> {
Err(LockError::internal("Local lock manager not implemented yet"))
}
async fn get_stats(&self) -> Result<LockStats> {
Ok(LockStats::default())
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
}
#[async_trait::async_trait]
impl LocalLockManager for LocalLockMap {
/// Acquire write lock. If resource is not locked, owner gets write lock; otherwise wait until timeout.
async fn lock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool> {
Self::lock_with_ttl(self, resource, owner, timeout, None).await
}
/// Acquire read lock. If resource has no write lock, owner gets read lock; otherwise wait until timeout.
async fn rlock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool> {
Self::rlock_with_ttl(self, resource, owner, timeout, None).await
}
/// Release write lock. Only the owner holding the write lock can release it.
async fn unlock(&self, resource: &str, owner: &str) -> std::io::Result<()> {
if let Some(entry) = self.locks.get(resource) {
let mut entry_guard = entry.value().write().await;
if entry_guard.writer.as_deref() == Some(owner) {
entry_guard.writer = None;
entry_guard.expires_at = None;
}
entry_guard.readers.retain(|r| r != owner);
if entry_guard.readers.is_empty() && entry_guard.writer.is_none() {
entry_guard.expires_at = None;
}
}
Ok(())
}
/// Release read lock. Only the owner holding the read lock can release it.
async fn runlock(&self, resource: &str, owner: &str) -> std::io::Result<()> {
self.unlock(resource, owner).await
}
/// Check if resource is locked (having write lock or read lock is considered locked).
async fn is_locked(&self, resource: &str) -> bool {
if let Some(entry) = self.locks.get(resource) {
let entry_guard = entry.value().read().await;
entry_guard.writer.is_some() || !entry_guard.readers.is_empty()
} else {
false
}
}
}
impl LocalLockMap {
/// Write lock with timeout support
pub async fn lock_with_ttl(
&self,
resource: &str,
owner: &str,
timeout: Duration,
expires_at: Option<Instant>,
) -> std::io::Result<bool> {
let start = Instant::now();
loop {
{
// DashMap's entry API automatically handles sharded locks
let entry = self.locks.entry(resource.to_string()).or_insert_with(|| {
Arc::new(RwLock::new(LocalLockEntry {
writer: None,
readers: Vec::new(),
expires_at: None,
}))
});
let mut entry_guard = entry.value().write().await;
// Write lock only needs to check if there's a write lock, no need to check read locks
// If read locks exist, write lock should wait
if entry_guard.writer.is_none() {
entry_guard.writer = Some(owner.to_string());
entry_guard.expires_at = expires_at;
return Ok(true);
}
}
if start.elapsed() >= timeout {
return Ok(false);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
/// Read lock with timeout support
pub async fn rlock_with_ttl(
&self,
resource: &str,
owner: &str,
timeout: Duration,
expires_at: Option<Instant>,
) -> std::io::Result<bool> {
let start = Instant::now();
loop {
{
// DashMap's entry API automatically handles sharded locks
let entry = self.locks.entry(resource.to_string()).or_insert_with(|| {
Arc::new(RwLock::new(LocalLockEntry {
writer: None,
readers: Vec::new(),
expires_at: None,
}))
});
let mut entry_guard = entry.value().write().await;
if entry_guard.writer.is_none() {
if !entry_guard.readers.contains(&owner.to_string()) {
entry_guard.readers.push(owner.to_string());
}
entry_guard.expires_at = expires_at;
return Ok(true);
}
}
if start.elapsed() >= timeout {
return Ok(false);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
}
impl Default for LocalLockMap {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::time::Duration;
use tokio::task;
/// Test basic write lock acquisition and release
#[tokio::test]
async fn test_write_lock_basic() {
let lock_map = LocalLockMap::new();
let ok = lock_map.lock("foo", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok, "Write lock should be successfully acquired");
assert!(lock_map.is_locked("foo").await, "Lock state should be locked");
lock_map.unlock("foo", "owner1").await.unwrap();
assert!(!lock_map.is_locked("foo").await, "Should be unlocked after release");
}
/// Test basic read lock acquisition and release
#[tokio::test]
async fn test_read_lock_basic() {
let lock_map = LocalLockMap::new();
let ok = lock_map.rlock("bar", "reader1", Duration::from_millis(100)).await.unwrap();
assert!(ok, "Read lock should be successfully acquired");
assert!(lock_map.is_locked("bar").await, "Lock state should be locked");
lock_map.runlock("bar", "reader1").await.unwrap();
assert!(!lock_map.is_locked("bar").await, "Should be unlocked after release");
}
/// Test write lock mutual exclusion
#[tokio::test]
async fn test_write_lock_mutex() {
let lock_map = Arc::new(LocalLockMap::new());
// owner1 acquires write lock first
let ok = lock_map.lock("res", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok);
// owner2 tries to acquire write lock on same resource, should timeout and fail
let lock_map2 = lock_map.clone();
let fut = task::spawn(async move { lock_map2.lock("res", "owner2", Duration::from_millis(50)).await.unwrap() });
let ok2 = fut.await.unwrap();
assert!(!ok2, "Write locks should be mutually exclusive, owner2 acquisition should fail");
lock_map.unlock("res", "owner1").await.unwrap();
}
/// Test read lock sharing
#[tokio::test]
async fn test_read_lock_shared() {
let lock_map = Arc::new(LocalLockMap::new());
let ok1 = lock_map.rlock("res2", "reader1", Duration::from_millis(100)).await.unwrap();
assert!(ok1);
let lock_map2 = lock_map.clone();
let fut = task::spawn(async move { lock_map2.rlock("res2", "reader2", Duration::from_millis(100)).await.unwrap() });
let ok2 = fut.await.unwrap();
assert!(ok2, "Multiple read locks should be shareable");
lock_map.runlock("res2", "reader1").await.unwrap();
lock_map.runlock("res2", "reader2").await.unwrap();
}
/// Test mutual exclusion between write lock and read lock
#[tokio::test]
async fn test_write_read_mutex() {
let lock_map = Arc::new(LocalLockMap::new());
// Acquire write lock first
let ok = lock_map.lock("res3", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok);
// Read lock should fail to acquire
let lock_map2 = lock_map.clone();
let fut = task::spawn(async move { lock_map2.rlock("res3", "reader1", Duration::from_millis(50)).await.unwrap() });
let ok2 = fut.await.unwrap();
assert!(!ok2, "Read lock should fail to acquire when write lock exists");
lock_map.unlock("res3", "owner1").await.unwrap();
}
/// Test timeout failure when acquiring lock
#[tokio::test]
async fn test_lock_timeout() {
let lock_map = Arc::new(LocalLockMap::new());
let ok = lock_map.lock("res4", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok);
// owner2 tries to acquire write lock on same resource with very short timeout, should fail
let lock_map2 = lock_map.clone();
let fut = task::spawn(async move { lock_map2.lock("res4", "owner2", Duration::from_millis(1)).await.unwrap() });
let ok2 = fut.await.unwrap();
assert!(!ok2, "Should fail due to timeout");
lock_map.unlock("res4", "owner1").await.unwrap();
}
/// Test that owner can only release locks they hold
#[tokio::test]
async fn test_owner_unlock() {
let lock_map = LocalLockMap::new();
let ok = lock_map.lock("res5", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok);
// owner2 tries to release owner1's lock, should not affect lock state
lock_map.unlock("res5", "owner2").await.unwrap();
assert!(lock_map.is_locked("res5").await, "Non-owner cannot release others' locks");
lock_map.unlock("res5", "owner1").await.unwrap();
assert!(!lock_map.is_locked("res5").await);
}
/// Correctness in concurrent scenarios
#[tokio::test]
async fn test_concurrent_readers() {
let lock_map = Arc::new(LocalLockMap::new());
let mut handles = vec![];
for i in 0..10 {
let lock_map = lock_map.clone();
handles.push(task::spawn(async move {
let owner = format!("reader{i}");
let ok = lock_map.rlock("res6", &owner, Duration::from_millis(100)).await.unwrap();
assert!(ok);
lock_map.runlock("res6", &owner).await.unwrap();
}));
}
for h in handles {
h.await.unwrap();
}
assert!(!lock_map.is_locked("res6").await, "Should be unlocked after all read locks are released");
}
#[tokio::test]
async fn test_lock_expiry() {
let map = LocalLockMap::new();
let key = "res1".to_string();
let owner = "owner1";
// Acquire lock with TTL 100ms
let ok = map
.lock_batch(std::slice::from_ref(&key), owner, Duration::from_millis(10), Some(Duration::from_millis(100)))
.await
.unwrap();
assert!(ok);
assert!(map.is_locked(&key).await);
// Wait up to 2 seconds until lock is cleaned up
let mut waited = 0;
while map.is_locked(&key).await && waited < 2000 {
tokio::time::sleep(Duration::from_millis(50)).await;
waited += 50;
}
assert!(!map.is_locked(&key).await, "Lock should be automatically released after TTL");
}
#[tokio::test]
async fn test_rlock_expiry() {
let map = LocalLockMap::new();
let key = "res2".to_string();
let owner = "owner2";
// Acquire read lock with TTL 80ms
let ok = map
.rlock_batch(std::slice::from_ref(&key), owner, Duration::from_millis(10), Some(Duration::from_millis(80)))
.await
.unwrap();
assert!(ok);
assert!(map.is_locked(&key).await);
// Wait up to 2 seconds until lock is cleaned up
let mut waited = 0;
while map.is_locked(&key).await && waited < 2000 {
tokio::time::sleep(Duration::from_millis(50)).await;
waited += 50;
}
assert!(!map.is_locked(&key).await, "Read lock should be automatically released after TTL");
}
/// Test high concurrency performance of DashMap version
#[tokio::test]
async fn test_concurrent_performance() {
let map = Arc::new(LocalLockMap::new());
let mut handles = vec![];
// Create multiple concurrent tasks, each operating on different resources
for i in 0..50 {
let map = map.clone();
let resource = format!("resource_{i}");
let owner = format!("owner_{i}");
handles.push(tokio::spawn(async move {
// Acquire write lock
let ok = map.lock(&resource, &owner, Duration::from_millis(100)).await.unwrap();
assert!(ok);
// Hold lock briefly
tokio::time::sleep(Duration::from_millis(10)).await;
// Release lock
map.unlock(&resource, &owner).await.unwrap();
// Verify lock is released
assert!(!map.is_locked(&resource).await);
}));
}
// Wait for all tasks to complete
for handle in handles {
handle.await.unwrap();
}
// Verify all resources are released
for i in 0..50 {
let resource = format!("resource_{i}");
assert!(!map.is_locked(&resource).await);
}
}
}

View File

@@ -1,325 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod distributed;
pub mod local;
use async_trait::async_trait;
use std::sync::Arc;
use crate::{
config::LockConfig,
error::Result,
types::{LockId, LockInfo, LockRequest, LockResponse, LockStats, LockType},
};
/// Core lock management trait
#[async_trait]
pub trait LockManager: Send + Sync {
/// Acquire exclusive lock
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse>;
/// Acquire shared lock
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse>;
/// Release lock
async fn release(&self, lock_id: &LockId) -> Result<bool>;
/// Refresh lock
async fn refresh(&self, lock_id: &LockId) -> Result<bool>;
/// Force release lock
async fn force_release(&self, lock_id: &LockId) -> Result<bool>;
/// Check lock status
async fn check_status(&self, lock_id: &LockId) -> Result<Option<LockInfo>>;
/// Get lock statistics
async fn get_stats(&self) -> Result<LockStats>;
/// Shutdown lock manager
async fn shutdown(&self) -> Result<()>;
}
/// Lock manager implementation
pub struct LockManagerImpl {
config: Arc<LockConfig>,
local_manager: Arc<dyn LockManager>,
distributed_manager: Option<Arc<dyn LockManager>>,
}
impl LockManagerImpl {
/// Create new lock manager
pub fn new(config: LockConfig) -> Result<Self> {
config.validate()?;
let config = Arc::new(config);
let local_manager = Arc::new(local::LocalLockManagerImpl::new(config.clone())?);
let distributed_manager = if config.distributed.auto_refresh {
// Use default remote URL
let remote_url = url::Url::parse("http://localhost:9000").unwrap();
Some(Arc::new(distributed::DistributedLockManager::new(config.clone(), remote_url)?) as Arc<dyn LockManager>)
} else {
None
};
Ok(Self {
config,
local_manager,
distributed_manager,
})
}
/// Select appropriate lock manager based on configuration
fn select_manager(&self, lock_type: LockType) -> Arc<dyn LockManager> {
// For shared locks, prefer local manager
// For exclusive locks, use distributed manager if available
match (lock_type, &self.distributed_manager) {
(LockType::Shared, _) => self.local_manager.clone(),
(LockType::Exclusive, Some(distributed)) => distributed.clone(),
(LockType::Exclusive, None) => self.local_manager.clone(),
}
}
}
#[async_trait]
impl LockManager for LockManagerImpl {
async fn acquire_exclusive(&self, request: LockRequest) -> Result<LockResponse> {
let manager = self.select_manager(LockType::Exclusive);
manager.acquire_exclusive(request).await
}
async fn acquire_shared(&self, request: LockRequest) -> Result<LockResponse> {
let manager = self.select_manager(LockType::Shared);
manager.acquire_shared(request).await
}
async fn release(&self, lock_id: &LockId) -> Result<bool> {
// Try to release from local manager
if let Ok(result) = self.local_manager.release(lock_id).await {
if result {
return Ok(true);
}
}
// If local manager didn't find the lock, try distributed manager
if let Some(distributed) = &self.distributed_manager {
distributed.release(lock_id).await
} else {
Ok(false)
}
}
async fn refresh(&self, lock_id: &LockId) -> Result<bool> {
// Try to refresh from local manager
if let Ok(result) = self.local_manager.refresh(lock_id).await {
if result {
return Ok(true);
}
}
// If local manager didn't find the lock, try distributed manager
if let Some(distributed) = &self.distributed_manager {
distributed.refresh(lock_id).await
} else {
Ok(false)
}
}
async fn force_release(&self, lock_id: &LockId) -> Result<bool> {
// Force release local lock
let local_result = self.local_manager.force_release(lock_id).await;
// Force release distributed lock
let distributed_result = if let Some(distributed) = &self.distributed_manager {
distributed.force_release(lock_id).await
} else {
Ok(false)
};
// Return true if either operation succeeds
Ok(local_result.unwrap_or(false) || distributed_result.unwrap_or(false))
}
async fn check_status(&self, lock_id: &LockId) -> Result<Option<LockInfo>> {
// Check local manager first
if let Ok(Some(info)) = self.local_manager.check_status(lock_id).await {
return Ok(Some(info));
}
// Then check distributed manager
if let Some(distributed) = &self.distributed_manager {
distributed.check_status(lock_id).await
} else {
Ok(None)
}
}
async fn get_stats(&self) -> Result<LockStats> {
let local_stats = self.local_manager.get_stats().await?;
let distributed_stats = if let Some(distributed) = &self.distributed_manager {
distributed.get_stats().await?
} else {
LockStats::default()
};
// Merge statistics
Ok(LockStats {
total_locks: local_stats.total_locks + distributed_stats.total_locks,
exclusive_locks: local_stats.exclusive_locks + distributed_stats.exclusive_locks,
shared_locks: local_stats.shared_locks + distributed_stats.shared_locks,
waiting_locks: local_stats.waiting_locks + distributed_stats.waiting_locks,
deadlock_detections: local_stats.deadlock_detections + distributed_stats.deadlock_detections,
priority_upgrades: local_stats.priority_upgrades + distributed_stats.priority_upgrades,
last_updated: std::time::SystemTime::now(),
total_releases: local_stats.total_releases + distributed_stats.total_releases,
total_hold_time: local_stats.total_hold_time + distributed_stats.total_hold_time,
average_hold_time: if local_stats.total_locks + distributed_stats.total_locks > 0 {
let total_time = local_stats.total_hold_time + distributed_stats.total_hold_time;
let total_count = local_stats.total_locks + distributed_stats.total_locks;
std::time::Duration::from_secs(total_time.as_secs() / total_count as u64)
} else {
std::time::Duration::ZERO
},
total_wait_queues: local_stats.total_wait_queues + distributed_stats.total_wait_queues,
})
}
async fn shutdown(&self) -> Result<()> {
// Shutdown local manager
if let Err(e) = self.local_manager.shutdown().await {
tracing::error!("Failed to shutdown local lock manager: {}", e);
}
// Shutdown distributed manager
if let Some(distributed) = &self.distributed_manager {
if let Err(e) = distributed.shutdown().await {
tracing::error!("Failed to shutdown distributed lock manager: {}", e);
}
}
Ok(())
}
}
/// Lock handle for automatic lock lifecycle management
pub struct LockHandle {
lock_id: LockId,
manager: Arc<dyn LockManager>,
auto_refresh: bool,
refresh_interval: tokio::time::Duration,
refresh_task: Option<tokio::task::JoinHandle<()>>,
}
impl LockHandle {
/// Create new lock handle
pub fn new(lock_id: LockId, manager: Arc<dyn LockManager>, auto_refresh: bool) -> Self {
Self {
lock_id,
manager,
auto_refresh,
refresh_interval: tokio::time::Duration::from_secs(10),
refresh_task: None,
}
}
/// Set auto refresh interval
pub fn with_refresh_interval(mut self, interval: tokio::time::Duration) -> Self {
self.refresh_interval = interval;
self
}
/// Start auto refresh task
pub fn start_auto_refresh(&mut self) {
if !self.auto_refresh {
return;
}
let lock_id = self.lock_id.clone();
let manager = self.manager.clone();
let interval = self.refresh_interval;
self.refresh_task = Some(tokio::spawn(async move {
let mut interval_timer = tokio::time::interval(interval);
loop {
interval_timer.tick().await;
if let Err(e) = manager.refresh(&lock_id).await {
tracing::warn!("Failed to refresh lock {}: {}", lock_id, e);
break;
}
}
}));
}
/// Stop auto refresh task
pub fn stop_auto_refresh(&mut self) {
if let Some(task) = self.refresh_task.take() {
task.abort();
}
}
/// Get lock ID
pub fn lock_id(&self) -> &LockId {
&self.lock_id
}
/// Check lock status
pub async fn check_status(&self) -> Result<Option<crate::types::LockInfo>> {
self.manager.check_status(&self.lock_id).await
}
}
impl Drop for LockHandle {
fn drop(&mut self) {
// Stop auto refresh task
self.stop_auto_refresh();
// Async release lock
let lock_id = self.lock_id.clone();
let manager = self.manager.clone();
tokio::spawn(async move {
if let Err(e) = manager.release(&lock_id).await {
tracing::warn!("Failed to release lock {} during drop: {}", lock_id, e);
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::LockType;
#[tokio::test]
async fn test_lock_manager_creation() {
let config = LockConfig::minimal();
let manager = LockManagerImpl::new(config);
assert!(manager.is_ok());
}
#[tokio::test]
async fn test_lock_handle() {
let config = LockConfig::minimal();
let manager = LockManagerImpl::new(config).unwrap();
let manager = Arc::new(manager);
let request = LockRequest::new("test-resource", LockType::Exclusive, "test-owner");
let response = manager.acquire_exclusive(request).await;
// Since local manager is not implemented yet, only test creation success
// Actual functionality tests will be done after implementation
assert!(response.is_ok() || response.is_err());
}
}

View File

@@ -1,354 +0,0 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{Duration, SystemTime};
use tracing::{debug, warn};
use crate::types::{DeadlockDetectionResult, LockPriority, LockType, WaitGraphNode, WaitQueueItem};
/// Deadlock detector
#[derive(Debug)]
pub struct DeadlockDetector {
/// Wait graph: owner -> waiting resources
wait_graph: HashMap<String, WaitGraphNode>,
/// Resource holder mapping: resource -> owner
resource_holders: HashMap<String, String>,
/// Resource wait queue: resource -> wait queue
wait_queues: HashMap<String, VecDeque<WaitQueueItem>>,
/// Detection statistics
detection_count: usize,
/// Last detection time
last_detection: SystemTime,
}
impl DeadlockDetector {
/// Create new deadlock detector
pub fn new() -> Self {
Self {
wait_graph: HashMap::new(),
resource_holders: HashMap::new(),
wait_queues: HashMap::new(),
detection_count: 0,
last_detection: SystemTime::now(),
}
}
/// Add wait relationship
pub fn add_wait_relationship(&mut self, owner: &str, waiting_for: &str, held_resources: Vec<String>, priority: LockPriority) {
let node = WaitGraphNode {
owner: owner.to_string(),
waiting_for: vec![waiting_for.to_string()],
held_resources,
priority,
wait_start_time: SystemTime::now(),
};
self.wait_graph.insert(owner.to_string(), node);
debug!("Added wait relationship: {} -> {}", owner, waiting_for);
}
/// Remove wait relationship
pub fn remove_wait_relationship(&mut self, owner: &str) {
self.wait_graph.remove(owner);
debug!("Removed wait relationship for owner: {}", owner);
}
/// Update resource holder
pub fn update_resource_holder(&mut self, resource: &str, owner: &str) {
if owner.is_empty() {
self.resource_holders.remove(resource);
} else {
self.resource_holders.insert(resource.to_string(), owner.to_string());
}
debug!("Updated resource holder: {} -> {}", resource, owner);
}
/// Add wait queue item
pub fn add_wait_queue_item(&mut self, resource: &str, owner: &str, lock_type: LockType, priority: LockPriority) {
let item = WaitQueueItem::new(owner, lock_type, priority);
self.wait_queues
.entry(resource.to_string())
.or_default()
.push_back(item);
debug!("Added wait queue item: {} -> {}", resource, owner);
}
/// Remove wait queue item
pub fn remove_wait_queue_item(&mut self, resource: &str, owner: &str) {
if let Some(queue) = self.wait_queues.get_mut(resource) {
queue.retain(|item| item.owner != owner);
if queue.is_empty() {
self.wait_queues.remove(resource);
}
}
debug!("Removed wait queue item: {} -> {}", resource, owner);
}
/// Detect deadlock
pub fn detect_deadlock(&mut self) -> DeadlockDetectionResult {
self.detection_count += 1;
self.last_detection = SystemTime::now();
let mut result = DeadlockDetectionResult {
has_deadlock: false,
deadlock_cycle: Vec::new(),
suggested_resolution: None,
affected_resources: Vec::new(),
affected_owners: Vec::new(),
};
// Use depth-first search to detect cycle
let mut visited = HashSet::new();
let mut recursion_stack = HashSet::new();
for owner in self.wait_graph.keys() {
if !visited.contains(owner)
&& self.dfs_detect_cycle(owner, &mut visited, &mut recursion_stack, &mut result) {
result.has_deadlock = true;
break;
}
}
if result.has_deadlock {
warn!("Deadlock detected! Cycle: {:?}", result.deadlock_cycle);
result.suggested_resolution = self.suggest_resolution(&result);
}
result
}
/// Depth-first search to detect cycle
fn dfs_detect_cycle(
&self,
owner: &str,
visited: &mut HashSet<String>,
recursion_stack: &mut HashSet<String>,
result: &mut DeadlockDetectionResult,
) -> bool {
visited.insert(owner.to_string());
recursion_stack.insert(owner.to_string());
if let Some(node) = self.wait_graph.get(owner) {
for waiting_for in &node.waiting_for {
if let Some(holder) = self.resource_holders.get(waiting_for) {
if !visited.contains(holder) {
if self.dfs_detect_cycle(holder, visited, recursion_stack, result) {
result.deadlock_cycle.push(owner.to_string());
return true;
}
} else if recursion_stack.contains(holder) {
// Cycle detected
result.deadlock_cycle.push(owner.to_string());
result.deadlock_cycle.push(holder.to_string());
result.affected_owners.push(owner.to_string());
result.affected_owners.push(holder.to_string());
return true;
}
}
}
}
recursion_stack.remove(owner);
false
}
/// Suggest resolution
fn suggest_resolution(&self, result: &DeadlockDetectionResult) -> Option<String> {
if result.deadlock_cycle.is_empty() {
return None;
}
// Find owner with lowest priority
let mut lowest_priority_owner = None;
let mut lowest_priority = LockPriority::Critical;
for owner in &result.affected_owners {
if let Some(node) = self.wait_graph.get(owner) {
if node.priority < lowest_priority {
lowest_priority = node.priority;
lowest_priority_owner = Some(owner.clone());
}
}
}
if let Some(owner) = lowest_priority_owner {
Some(format!("Suggest releasing lock held by {owner} to break deadlock cycle"))
} else {
Some("Suggest randomly selecting an owner to release lock".to_string())
}
}
/// Get wait queue information
pub fn get_wait_queue_info(&self, resource: &str) -> Vec<WaitQueueItem> {
self.wait_queues
.get(resource)
.map(|queue| queue.iter().cloned().collect())
.unwrap_or_default()
}
/// Check for long waits
pub fn check_long_waits(&self, timeout: Duration) -> Vec<String> {
let mut long_waiters = Vec::new();
for (owner, node) in &self.wait_graph {
if node.wait_start_time.elapsed().unwrap_or(Duration::ZERO) > timeout {
long_waiters.push(owner.clone());
}
}
long_waiters
}
/// Suggest priority upgrade
pub fn suggest_priority_upgrade(&self, resource: &str) -> Option<String> {
if let Some(queue) = self.wait_queues.get(resource) {
if queue.len() > 1 {
// Find request with longest wait time and lowest priority
let mut longest_wait = Duration::ZERO;
let mut candidate = None;
for item in queue {
let wait_duration = item.wait_duration();
if wait_duration > longest_wait && item.priority < LockPriority::High {
longest_wait = wait_duration;
candidate = Some(item.owner.clone());
}
}
if let Some(owner) = candidate {
return Some(format!("Suggest upgrading priority of {owner} to reduce wait time"));
}
}
}
None
}
/// Clean up expired waits
pub fn cleanup_expired_waits(&mut self, max_wait_time: Duration) {
let mut to_remove = Vec::new();
for (owner, node) in &self.wait_graph {
if node.wait_start_time.elapsed().unwrap_or(Duration::ZERO) > max_wait_time {
to_remove.push(owner.clone());
}
}
for owner in to_remove {
self.remove_wait_relationship(&owner);
warn!("Removed expired wait relationship for owner: {}", owner);
}
}
/// Get detection statistics
pub fn get_stats(&self) -> (usize, SystemTime) {
(self.detection_count, self.last_detection)
}
/// Reset detector
pub fn reset(&mut self) {
self.wait_graph.clear();
self.resource_holders.clear();
self.wait_queues.clear();
self.detection_count = 0;
self.last_detection = SystemTime::now();
debug!("Deadlock detector reset");
}
}
impl Default for DeadlockDetector {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deadlock_detector_creation() {
let detector = DeadlockDetector::new();
assert_eq!(detector.detection_count, 0);
}
#[test]
fn test_add_wait_relationship() {
let mut detector = DeadlockDetector::new();
detector.add_wait_relationship("owner1", "resource1", vec!["resource2".to_string()], LockPriority::Normal);
assert!(detector.wait_graph.contains_key("owner1"));
let node = detector.wait_graph.get("owner1").unwrap();
assert_eq!(node.owner, "owner1");
assert_eq!(node.waiting_for, vec!["resource1"]);
}
#[test]
fn test_deadlock_detection() {
let mut detector = DeadlockDetector::new();
// Create deadlock scenario: owner1 -> resource1 -> owner2 -> resource2 -> owner1
detector.add_wait_relationship("owner1", "resource1", vec!["resource2".to_string()], LockPriority::Normal);
detector.add_wait_relationship("owner2", "resource2", vec!["resource1".to_string()], LockPriority::Normal);
detector.update_resource_holder("resource1", "owner2");
detector.update_resource_holder("resource2", "owner1");
let result = detector.detect_deadlock();
assert!(result.has_deadlock);
assert!(!result.deadlock_cycle.is_empty());
assert!(result.suggested_resolution.is_some());
}
#[test]
fn test_no_deadlock() {
let mut detector = DeadlockDetector::new();
// Create deadlock-free scenario
detector.add_wait_relationship("owner1", "resource1", vec![], LockPriority::Normal);
detector.update_resource_holder("resource1", "owner2");
let result = detector.detect_deadlock();
assert!(!result.has_deadlock);
}
#[test]
fn test_wait_queue_management() {
let mut detector = DeadlockDetector::new();
detector.add_wait_queue_item("resource1", "owner1", LockType::Exclusive, LockPriority::Normal);
detector.add_wait_queue_item("resource1", "owner2", LockType::Shared, LockPriority::High);
let queue_info = detector.get_wait_queue_info("resource1");
assert_eq!(queue_info.len(), 2);
detector.remove_wait_queue_item("resource1", "owner1");
let queue_info = detector.get_wait_queue_info("resource1");
assert_eq!(queue_info.len(), 1);
}
#[test]
fn test_priority_upgrade_suggestion() {
let mut detector = DeadlockDetector::new();
// Add multiple wait items
detector.add_wait_queue_item("resource1", "owner1", LockType::Exclusive, LockPriority::Low);
detector.add_wait_queue_item("resource1", "owner2", LockType::Exclusive, LockPriority::Normal);
let suggestion = detector.suggest_priority_upgrade("resource1");
assert!(suggestion.is_some());
assert!(suggestion.unwrap().contains("owner1"));
}
#[test]
fn test_cleanup_expired_waits() {
let mut detector = DeadlockDetector::new();
// Add a wait relationship
detector.add_wait_relationship("owner1", "resource1", vec![], LockPriority::Normal);
// Simulate long wait
std::thread::sleep(Duration::from_millis(10));
detector.cleanup_expired_waits(Duration::from_millis(5));
assert!(!detector.wait_graph.contains_key("owner1"));
}
}

View File

@@ -0,0 +1,640 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::{Mutex, RwLock};
use tokio::time::{interval, timeout};
use uuid::Uuid;
use crate::{
client::LockClient,
error::{LockError, Result},
types::{LockId, LockInfo, LockPriority, LockRequest, LockResponse, LockStats, LockStatus, LockType},
};
/// Quorum configuration for distributed locking
#[derive(Debug, Clone)]
pub struct QuorumConfig {
/// Total number of nodes in the cluster
pub total_nodes: usize,
/// Number of nodes that can fail (tolerance)
pub tolerance: usize,
/// Minimum number of nodes required for quorum
pub quorum: usize,
/// Lock acquisition timeout
pub acquisition_timeout: Duration,
/// Lock refresh interval
pub refresh_interval: Duration,
/// Lock expiration time
pub expiration_time: Duration,
}
impl QuorumConfig {
/// Create new quorum configuration
pub fn new(total_nodes: usize, tolerance: usize) -> Self {
let quorum = total_nodes - tolerance;
Self {
total_nodes,
tolerance,
quorum,
acquisition_timeout: Duration::from_secs(30),
refresh_interval: Duration::from_secs(10),
expiration_time: Duration::from_secs(60),
}
}
/// Check if quorum is valid
pub fn is_valid(&self) -> bool {
self.quorum > 0 && self.quorum <= self.total_nodes && self.tolerance < self.total_nodes
}
}
/// Distributed lock entry
#[derive(Debug)]
pub struct DistributedLockEntry {
/// Lock ID
pub lock_id: LockId,
/// Resource being locked
pub resource: String,
/// Lock type
pub lock_type: LockType,
/// Lock owner
pub owner: String,
/// Lock priority
pub priority: LockPriority,
/// Lock acquisition time
pub acquired_at: Instant,
/// Lock expiration time
pub expires_at: Instant,
/// Nodes that hold this lock
pub holders: Vec<String>,
/// Lock refresh task handle
pub refresh_handle: Option<tokio::task::JoinHandle<()>>,
}
impl DistributedLockEntry {
/// Create new distributed lock entry
pub fn new(
lock_id: LockId,
resource: String,
lock_type: LockType,
owner: String,
priority: LockPriority,
expiration_time: Duration,
) -> Self {
let now = Instant::now();
Self {
lock_id,
resource,
lock_type,
owner,
priority,
acquired_at: now,
expires_at: now + expiration_time,
holders: Vec::new(),
refresh_handle: None,
}
}
/// Check if lock has expired
pub fn has_expired(&self) -> bool {
Instant::now() >= self.expires_at
}
/// Extend lock expiration
pub fn extend(&mut self, duration: Duration) {
self.expires_at = Instant::now() + duration;
}
/// Get remaining time until expiration
pub fn remaining_time(&self) -> Duration {
if self.has_expired() {
Duration::ZERO
} else {
self.expires_at - Instant::now()
}
}
}
/// Distributed lock manager
#[derive(Debug)]
pub struct DistributedLockManager {
/// Quorum configuration
config: QuorumConfig,
/// Lock clients for each node
clients: Arc<RwLock<HashMap<String, Arc<dyn LockClient>>>>,
/// Active locks
locks: Arc<RwLock<HashMap<String, DistributedLockEntry>>>,
/// Node ID
node_id: String,
/// Statistics
stats: Arc<Mutex<LockStats>>,
}
impl DistributedLockManager {
/// Create new distributed lock manager
pub fn new(config: QuorumConfig, node_id: String) -> Self {
Self {
config,
clients: Arc::new(RwLock::new(HashMap::new())),
locks: Arc::new(RwLock::new(HashMap::new())),
node_id,
stats: Arc::new(Mutex::new(LockStats::default())),
}
}
/// Add lock client for a node
pub async fn add_client(&self, node_id: String, client: Arc<dyn LockClient>) {
let mut clients = self.clients.write().await;
clients.insert(node_id, client);
}
/// Remove lock client for a node
pub async fn remove_client(&self, node_id: &str) {
let mut clients = self.clients.write().await;
clients.remove(node_id);
}
/// Acquire distributed lock
pub async fn acquire_lock(&self, request: LockRequest) -> Result<LockResponse> {
let resource_key = self.get_resource_key(&request.resource);
// Check if we already hold this lock
{
let locks = self.locks.read().await;
if let Some(lock) = locks.get(&resource_key) {
if lock.owner == request.owner && !lock.has_expired() {
return Ok(LockResponse::success(
LockInfo {
id: lock.lock_id.clone(),
resource: request.resource.clone(),
lock_type: request.lock_type,
status: LockStatus::Acquired,
owner: request.owner.clone(),
acquired_at: SystemTime::now(),
expires_at: SystemTime::now() + lock.remaining_time(),
last_refreshed: SystemTime::now(),
metadata: request.metadata.clone(),
priority: request.priority,
wait_start_time: None,
},
Duration::ZERO,
));
}
}
}
// Try to acquire lock directly
match self.try_acquire_lock(&request).await {
Ok(response) => {
if response.success {
return Ok(response);
}
}
Err(e) => {
tracing::warn!("Direct lock acquisition failed: {}", e);
}
}
// If direct acquisition fails, return timeout error
Err(LockError::timeout("Distributed lock acquisition failed", request.timeout))
}
/// Try to acquire lock directly
async fn try_acquire_lock(&self, request: &LockRequest) -> Result<LockResponse> {
let resource_key = self.get_resource_key(&request.resource);
let clients = self.clients.read().await;
if clients.len() < self.config.quorum {
return Err(LockError::InsufficientNodes {
required: self.config.quorum,
available: clients.len(),
});
}
// Prepare lock request for all nodes
let lock_request = LockRequest {
lock_id: request.lock_id.clone(),
resource: request.resource.clone(),
lock_type: request.lock_type,
owner: request.owner.clone(),
priority: request.priority,
timeout: self.config.acquisition_timeout,
metadata: request.metadata.clone(),
wait_timeout: request.wait_timeout,
deadlock_detection: request.deadlock_detection,
};
// Send lock request to all nodes
let mut responses = Vec::new();
let mut handles = Vec::new();
for (node_id, client) in clients.iter() {
let client = client.clone();
let request = lock_request.clone();
let handle = tokio::spawn(async move { client.acquire_lock(request).await });
handles.push((node_id.clone(), handle));
}
// Collect responses with timeout
for (node_id, handle) in handles {
match timeout(self.config.acquisition_timeout, handle).await {
Ok(Ok(response)) => {
responses.push((node_id, response));
}
Ok(Err(e)) => {
tracing::warn!("Lock request failed for node {}: {}", node_id, e);
}
Err(_) => {
tracing::warn!("Lock request timeout for node {}", node_id);
}
}
}
// Check if we have quorum
let successful_responses = responses
.iter()
.filter(|(_, response)| response.as_ref().map(|r| r.success).unwrap_or(false))
.count();
if successful_responses >= self.config.quorum {
// Create lock entry
let mut lock_entry = DistributedLockEntry::new(
request.lock_id.clone(),
request.resource.clone(),
request.lock_type,
request.owner.clone(),
request.priority,
self.config.expiration_time,
);
// Add successful nodes as holders
for (node_id, _) in responses
.iter()
.filter(|(_, r)| r.as_ref().map(|resp| resp.success).unwrap_or(false))
{
lock_entry.holders.push(node_id.clone());
}
// Start refresh task
let refresh_handle = self.start_refresh_task(&lock_entry).await;
// Store lock entry
{
let mut locks = self.locks.write().await;
lock_entry.refresh_handle = Some(refresh_handle);
locks.insert(resource_key, lock_entry);
}
// Update statistics
self.update_stats(true).await;
Ok(LockResponse::success(
LockInfo {
id: request.lock_id.clone(),
resource: request.resource.clone(),
lock_type: request.lock_type,
status: LockStatus::Acquired,
owner: request.owner.clone(),
acquired_at: SystemTime::now(),
expires_at: SystemTime::now() + self.config.expiration_time,
last_refreshed: SystemTime::now(),
metadata: request.metadata.clone(),
priority: request.priority,
wait_start_time: None,
},
Duration::ZERO,
))
} else {
// Update statistics
self.update_stats(false).await;
Err(LockError::QuorumNotReached {
required: self.config.quorum,
achieved: successful_responses,
})
}
}
/// Release distributed lock
pub async fn release_lock(&self, lock_id: &LockId, owner: &str) -> Result<LockResponse> {
let resource_key = self.get_resource_key_from_lock_id(lock_id);
// Check if we hold this lock
{
let locks = self.locks.read().await;
if let Some(lock) = locks.get(&resource_key) {
if lock.owner != owner {
return Err(LockError::NotOwner {
lock_id: lock_id.clone(),
owner: owner.to_string(),
});
}
}
}
// Release lock from all nodes
let clients = self.clients.read().await;
let mut responses = Vec::new();
for (node_id, client) in clients.iter() {
match client.release(lock_id).await {
Ok(response) => {
responses.push((node_id.clone(), response));
}
Err(e) => {
tracing::warn!("Lock release failed for node {}: {}", node_id, e);
}
}
}
// Remove lock entry
{
let mut locks = self.locks.write().await;
if let Some(lock) = locks.remove(&resource_key) {
// Cancel refresh task
if let Some(handle) = lock.refresh_handle {
handle.abort();
}
}
}
Ok(LockResponse::success(
LockInfo {
id: lock_id.clone(),
resource: "unknown".to_string(),
lock_type: LockType::Exclusive,
status: LockStatus::Released,
owner: owner.to_string(),
acquired_at: SystemTime::now(),
expires_at: SystemTime::now(),
last_refreshed: SystemTime::now(),
metadata: crate::types::LockMetadata::default(),
priority: LockPriority::Normal,
wait_start_time: None,
},
Duration::ZERO,
))
}
/// Start lock refresh task
async fn start_refresh_task(&self, lock_entry: &DistributedLockEntry) -> tokio::task::JoinHandle<()> {
let lock_id = lock_entry.lock_id.clone();
let _owner = lock_entry.owner.clone();
let _resource = lock_entry.resource.clone();
let refresh_interval = self.config.refresh_interval;
let clients = self.clients.clone();
let quorum = self.config.quorum;
tokio::spawn(async move {
let mut interval = interval(refresh_interval);
loop {
interval.tick().await;
// Try to refresh lock on all nodes
let clients_guard = clients.read().await;
let mut success_count = 0;
for (node_id, client) in clients_guard.iter() {
match client.refresh(&lock_id).await {
Ok(success) if success => {
success_count += 1;
}
_ => {
tracing::warn!("Lock refresh failed for node {}", node_id);
}
}
}
// If we don't have quorum, stop refreshing
if success_count < quorum {
tracing::error!("Lost quorum for lock {}, stopping refresh", lock_id);
break;
}
}
})
}
/// Get resource key
fn get_resource_key(&self, resource: &str) -> String {
format!("{}:{}", self.node_id, resource)
}
/// Get resource key from lock ID
fn get_resource_key_from_lock_id(&self, lock_id: &LockId) -> String {
// This is a simplified implementation
// In practice, you might want to store a mapping from lock_id to resource
format!("{}:{}", self.node_id, lock_id)
}
/// Update statistics
async fn update_stats(&self, success: bool) {
let mut stats = self.stats.lock().await;
if success {
stats.successful_acquires += 1;
} else {
stats.failed_acquires += 1;
}
}
/// Get lock statistics
pub async fn get_stats(&self) -> LockStats {
self.stats.lock().await.clone()
}
/// Clean up expired locks
pub async fn cleanup_expired_locks(&self) -> usize {
let mut locks = self.locks.write().await;
let initial_len = locks.len();
locks.retain(|_, lock| !lock.has_expired());
initial_len - locks.len()
}
/// Force release lock (admin operation)
pub async fn force_release_lock(&self, lock_id: &LockId) -> Result<LockResponse> {
let resource_key = self.get_resource_key_from_lock_id(lock_id);
// Check if we hold this lock
{
let locks = self.locks.read().await;
if let Some(lock) = locks.get(&resource_key) {
// Force release on all nodes
let clients = self.clients.read().await;
let mut _success_count = 0;
for (node_id, client) in clients.iter() {
match client.force_release(lock_id).await {
Ok(success) if success => {
_success_count += 1;
}
_ => {
tracing::warn!("Force release failed for node {}", node_id);
}
}
}
// Remove from local locks
let mut locks = self.locks.write().await;
locks.remove(&resource_key);
// Wake up waiting locks
return Ok(LockResponse::success(
LockInfo {
id: lock_id.clone(),
resource: lock.resource.clone(),
lock_type: lock.lock_type,
status: LockStatus::ForceReleased,
owner: lock.owner.clone(),
acquired_at: SystemTime::now(),
expires_at: SystemTime::now(),
last_refreshed: SystemTime::now(),
metadata: crate::types::LockMetadata::default(),
priority: lock.priority,
wait_start_time: None,
},
Duration::ZERO,
));
}
}
Err(LockError::internal("Lock not found"))
}
/// Refresh lock
pub async fn refresh_lock(&self, lock_id: &LockId, owner: &str) -> Result<LockResponse> {
let resource_key = self.get_resource_key_from_lock_id(lock_id);
// Check if we hold this lock
{
let locks = self.locks.read().await;
if let Some(lock) = locks.get(&resource_key) {
if lock.owner == owner && !lock.has_expired() {
// 提前 clone 所需字段
let priority = lock.priority;
let lock_id = lock.lock_id.clone();
let resource = lock.resource.clone();
let lock_type = lock.lock_type;
let owner = lock.owner.clone();
let holders = lock.holders.clone();
let acquired_at = lock.acquired_at;
let expires_at = Instant::now() + self.config.expiration_time;
// 更新锁
let mut locks = self.locks.write().await;
locks.insert(
resource_key.clone(),
DistributedLockEntry {
lock_id: lock_id.clone(),
resource: resource.clone(),
lock_type,
owner: owner.clone(),
priority,
acquired_at,
expires_at,
holders,
refresh_handle: None,
},
);
return Ok(LockResponse::success(
LockInfo {
id: lock_id,
resource,
lock_type,
status: LockStatus::Acquired,
owner,
acquired_at: SystemTime::now(),
expires_at: SystemTime::now() + self.config.expiration_time,
last_refreshed: SystemTime::now(),
metadata: crate::types::LockMetadata::default(),
priority,
wait_start_time: None,
},
Duration::ZERO,
));
}
}
}
Err(LockError::internal("Lock not found or expired"))
}
}
impl Default for DistributedLockManager {
fn default() -> Self {
Self::new(QuorumConfig::new(3, 1), Uuid::new_v4().to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::LockType;
#[tokio::test]
async fn test_quorum_config() {
let config = QuorumConfig::new(5, 2);
assert!(config.is_valid());
assert_eq!(config.quorum, 3);
}
#[tokio::test]
async fn test_distributed_lock_entry() {
let lock_id = LockId::new("test-resource");
let entry = DistributedLockEntry::new(
lock_id.clone(),
"test-resource".to_string(),
LockType::Exclusive,
"test-owner".to_string(),
LockPriority::Normal,
Duration::from_secs(60),
);
assert_eq!(entry.lock_id, lock_id);
assert!(!entry.has_expired());
assert!(entry.remaining_time() > Duration::ZERO);
}
#[tokio::test]
async fn test_distributed_lock_manager_creation() {
let config = QuorumConfig::new(3, 1);
let manager = DistributedLockManager::new(config, "node-1".to_string());
let stats = manager.get_stats().await;
assert_eq!(stats.successful_acquires, 0);
assert_eq!(stats.failed_acquires, 0);
}
#[tokio::test]
async fn test_force_release_lock() {
let config = QuorumConfig::new(3, 1);
let manager = DistributedLockManager::new(config, "node-1".to_string());
let lock_id = LockId::new("test-resource");
let result = manager.force_release_lock(&lock_id).await;
// Should fail because lock doesn't exist
assert!(result.is_err());
}
}

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::types::LockId;
use std::time::Duration;
use thiserror::Error;
@@ -69,6 +70,22 @@ pub enum LockError {
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
/// Insufficient nodes for quorum
#[error("Insufficient nodes for quorum: required {required}, available {available}")]
InsufficientNodes { required: usize, available: usize },
/// Quorum not reached
#[error("Quorum not reached: required {required}, achieved {achieved}")]
QuorumNotReached { required: usize, achieved: usize },
/// Queue is full
#[error("Queue is full: {message}")]
QueueFull { message: String },
/// Not the lock owner
#[error("Not the lock owner: lock_id {lock_id}, owner {owner}")]
NotOwner { lock_id: LockId, owner: String },
}
impl LockError {

View File

@@ -13,227 +13,134 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::core::local::LocalLockManager;
use crate::error::Result;
use async_trait::async_trait;
use lock_args::LockArgs;
use once_cell::sync::Lazy;
use client::remote::RemoteClient;
use std::sync::Arc;
use std::sync::LazyLock;
use tokio::sync::RwLock;
pub mod lock_args;
// Refactored architecture modules
pub mod client;
pub mod config;
pub mod core;
pub mod deadlock_detector;
pub mod error;
// ============================================================================
// Core Module Declarations
// ============================================================================
// Application Layer Modules
pub mod namespace;
// Abstraction Layer Modules
pub mod client;
// Distributed Layer Modules
pub mod distributed;
// Local Layer Modules
pub mod local;
// Core Modules
pub mod config;
pub mod error;
pub mod types;
pub mod utils;
// Re-export commonly used types
pub use config::LockConfig;
pub use core::{LockHandle, LockManager, LockManagerImpl};
pub use error::{LockError, Result as LockResult};
pub use namespace::{NamespaceLockManager, NsLockMap};
pub use types::{LockId, LockInfo, LockRequest, LockResponse, LockStats, LockType};
// ============================================================================
// Public API Exports
// ============================================================================
// Backward compatibility constants and type aliases
// Re-export main types for easy access
pub use crate::{
// Client interfaces
client::{LockClient, local::LocalClient, remote::{RemoteClient, LockArgs}},
// Configuration
config::{DistributedLockConfig, LocalLockConfig, LockConfig, NetworkConfig},
distributed::{DistributedLockEntry, DistributedLockManager, QuorumConfig},
// Error types
error::{LockError, Result},
local::LocalLockMap,
// Main components
namespace::{NamespaceLock, NamespaceLockManager, NsLockMap},
// Core types
types::{
HealthInfo, HealthStatus, LockId, LockInfo, LockMetadata, LockPriority, LockRequest, LockResponse, LockStats, LockStatus,
LockType,
},
};
// ============================================================================
// Version Information
// ============================================================================
/// Current version of the lock crate
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Build timestamp
pub const BUILD_TIMESTAMP: &str = "unknown";
/// Maximum number of items in delete list
pub const MAX_DELETE_LIST: usize = 1000;
// Global local lock service instance for distributed lock modules
pub static GLOBAL_LOCAL_SERVER: Lazy<Arc<RwLock<core::local::LocalLockMap>>> =
Lazy::new(|| Arc::new(RwLock::new(core::local::LocalLockMap::new())));
// ============================================================================
// Global Lock Map
// ============================================================================
type LockClient = dyn Locker;
// Global singleton lock map shared across all lock implementations
use once_cell::sync::OnceCell;
use std::sync::Arc;
#[async_trait]
pub trait Locker {
async fn lock(&mut self, args: &LockArgs) -> Result<bool>;
async fn unlock(&mut self, args: &LockArgs) -> Result<bool>;
async fn rlock(&mut self, args: &LockArgs) -> Result<bool>;
async fn runlock(&mut self, args: &LockArgs) -> Result<bool>;
async fn refresh(&mut self, args: &LockArgs) -> Result<bool>;
async fn force_unlock(&mut self, args: &LockArgs) -> Result<bool>;
async fn close(&self);
async fn is_online(&self) -> bool;
async fn is_local(&self) -> bool;
static GLOBAL_LOCK_MAP: OnceCell<Arc<local::LocalLockMap>> = OnceCell::new();
/// Get the global shared lock map instance
pub fn get_global_lock_map() -> Arc<local::LocalLockMap> {
GLOBAL_LOCK_MAP.get_or_init(|| Arc::new(local::LocalLockMap::new())).clone()
}
#[derive(Debug, Clone)]
pub enum LockApi {
Local,
Remote(RemoteClient),
// ============================================================================
// Feature Flags
// ============================================================================
#[cfg(feature = "distributed")]
pub mod distributed_features {
// Distributed locking specific features
}
#[async_trait]
impl Locker for LockApi {
async fn lock(&mut self, args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => {
let resource = args
.resources
.first()
.ok_or_else(|| crate::error::LockError::internal("No resource specified"))?;
let timeout = std::time::Duration::from_secs(30);
GLOBAL_LOCAL_SERVER
.write()
.await
.lock(resource, &args.owner, timeout)
.await
.map_err(|e| crate::error::LockError::internal(format!("Local lock failed: {e}")))
}
LockApi::Remote(r) => r.lock(args).await,
}
}
async fn unlock(&mut self, args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => {
let resource = args
.resources
.first()
.ok_or_else(|| crate::error::LockError::internal("No resource specified"))?;
GLOBAL_LOCAL_SERVER
.write()
.await
.unlock(resource, &args.owner)
.await
.map(|_| true)
.map_err(|e| crate::error::LockError::internal(format!("Local unlock failed: {e}")))
}
LockApi::Remote(r) => r.unlock(args).await,
}
}
async fn rlock(&mut self, args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => {
let resource = args
.resources
.first()
.ok_or_else(|| crate::error::LockError::internal("No resource specified"))?;
let timeout = std::time::Duration::from_secs(30);
GLOBAL_LOCAL_SERVER
.write()
.await
.rlock(resource, &args.owner, timeout)
.await
.map_err(|e| crate::error::LockError::internal(format!("Local rlock failed: {e}")))
}
LockApi::Remote(r) => r.rlock(args).await,
}
}
async fn runlock(&mut self, args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => {
let resource = args
.resources
.first()
.ok_or_else(|| crate::error::LockError::internal("No resource specified"))?;
GLOBAL_LOCAL_SERVER
.write()
.await
.runlock(resource, &args.owner)
.await
.map(|_| true)
.map_err(|e| crate::error::LockError::internal(format!("Local runlock failed: {e}")))
}
LockApi::Remote(r) => r.runlock(args).await,
}
}
async fn refresh(&mut self, _args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => Ok(true), // Local locks don't need refresh
LockApi::Remote(r) => r.refresh(_args).await,
}
}
async fn force_unlock(&mut self, args: &LockArgs) -> Result<bool> {
match self {
LockApi::Local => {
let resource = args
.resources
.first()
.ok_or_else(|| crate::error::LockError::internal("No resource specified"))?;
GLOBAL_LOCAL_SERVER
.write()
.await
.unlock(resource, &args.owner)
.await
.map(|_| true)
.map_err(|e| crate::error::LockError::internal(format!("Local force unlock failed: {e}")))
}
LockApi::Remote(r) => r.force_unlock(args).await,
}
}
async fn close(&self) {
match self {
LockApi::Local => (), // Local locks don't need to be closed
LockApi::Remote(r) => r.close().await,
}
}
async fn is_online(&self) -> bool {
match self {
LockApi::Local => true, // Local locks are always online
LockApi::Remote(r) => r.is_online().await,
}
}
async fn is_local(&self) -> bool {
match self {
LockApi::Local => true,
LockApi::Remote(r) => r.is_local().await,
}
}
#[cfg(feature = "metrics")]
pub mod metrics {
// Metrics collection features
}
pub fn new_lock_api(is_local: bool, url: Option<url::Url>) -> LockApi {
if is_local {
LockApi::Local
#[cfg(feature = "tracing")]
pub mod tracing_features {
// Tracing features
}
// ============================================================================
// Convenience Functions
// ============================================================================
/// Create a new namespace lock
pub fn create_namespace_lock(namespace: String, distributed: bool) -> NamespaceLock {
if distributed {
// Create a namespace lock that uses RPC to communicate with the server
// This will use the NsLockMap with distributed mode enabled
NamespaceLock::new(namespace, true)
} else {
let url = url.expect("URL must be provided for remote lock API");
LockApi::Remote(RemoteClient::from_url(url))
NamespaceLock::new(namespace, false)
}
}
pub fn create_lock_manager(config: LockConfig) -> LockResult<LockManagerImpl> {
LockManagerImpl::new(config)
// ============================================================================
// Utility Functions
// ============================================================================
/// Generate a new lock ID
pub fn generate_lock_id() -> LockId {
LockId::new_deterministic("default")
}
pub fn create_local_client() -> Arc<dyn client::LockClient> {
Arc::new(client::local::LocalClient::new())
/// Create a lock request with default settings
pub fn create_lock_request(resource: String, lock_type: LockType, owner: String) -> LockRequest {
LockRequest::new(resource, lock_type, owner)
}
pub fn create_remote_client(endpoint: String) -> Arc<dyn client::LockClient> {
Arc::new(client::remote::RemoteClient::new(endpoint))
/// Create an exclusive lock request
pub fn create_exclusive_lock_request(resource: String, owner: String) -> LockRequest {
create_lock_request(resource, LockType::Exclusive, owner)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_new_api() {
let local_api = new_lock_api(true, None);
assert!(matches!(local_api, LockApi::Local));
let url = url::Url::parse("http://localhost:8080").unwrap();
let remote_api = new_lock_api(false, Some(url));
assert!(matches!(remote_api, LockApi::Remote(_)));
}
#[tokio::test]
async fn test_backward_compatibility() {
let client = create_local_client();
assert!(client.is_local().await);
}
/// Create a shared lock request
pub fn create_shared_lock_request(resource: String, owner: String) -> LockRequest {
create_lock_request(resource, LockType::Shared, owner)
}

729
crates/lock/src/local.rs Normal file
View File

@@ -0,0 +1,729 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use dashmap::DashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
/// 本地锁条目
#[derive(Debug)]
pub struct LocalLockEntry {
/// 当前写锁持有者
pub writer: Option<String>,
/// 当前读锁持有者集合
pub readers: Vec<String>,
/// 锁过期时间
pub expires_at: Option<Instant>,
}
/// 本地锁映射管理器
///
/// 内部维护从资源到锁对象的映射表使用DashMap实现高并发性能
#[derive(Debug)]
pub struct LocalLockMap {
/// 资源锁映射表key是唯一资源标识符value是锁对象
/// 使用DashMap实现分片锁以提高并发性能
pub locks: Arc<DashMap<String, Arc<RwLock<LocalLockEntry>>>>,
/// LockId 到 (resource, owner) 的映射
pub lockid_map: Arc<DashMap<crate::types::LockId, (String, String)>>,
}
impl LocalLockMap {
/// 创建新的本地锁管理器
pub fn new() -> Self {
let map = Self {
locks: Arc::new(DashMap::new()),
lockid_map: Arc::new(DashMap::new()),
};
map.spawn_expiry_task();
map
}
/// 启动后台任务定期清理过期的锁
fn spawn_expiry_task(&self) {
let locks = self.locks.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
interval.tick().await;
let now = Instant::now();
let mut to_remove = Vec::new();
// DashMap的iter()方法提供并发安全的迭代
for item in locks.iter() {
let mut entry_guard = item.value().write().await;
if let Some(exp) = entry_guard.expires_at {
if exp <= now {
// 清除锁内容
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
// 如果条目完全为空,标记为删除
if entry_guard.writer.is_none() && entry_guard.readers.is_empty() {
to_remove.push(item.key().clone());
}
}
}
}
// 删除空条目
for key in to_remove {
locks.remove(&key);
}
}
});
}
/// 批量获取写锁
///
/// 尝试在所有资源上获取写锁,如果任何资源锁定失败,回滚所有之前锁定的资源
pub async fn lock_batch(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<bool> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.lock_with_ttl_id(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(resource.clone());
}
Ok(false) => {
// 回滚之前锁定的资源
for locked_resource in locked {
let _ = self.unlock(&locked_resource, owner).await;
}
return Ok(false);
}
Err(e) => {
// 回滚之前锁定的资源
for locked_resource in locked {
let _ = self.unlock(&locked_resource, owner).await;
}
return Err(crate::error::LockError::internal(format!("Lock failed: {e}")));
}
}
}
Ok(true)
}
/// 批量释放写锁
pub async fn unlock_batch(&self, resources: &[String], owner: &str) -> crate::error::Result<()> {
for resource in resources {
let _ = self.unlock(resource, owner).await;
}
Ok(())
}
/// 批量获取读锁
pub async fn rlock_batch(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<bool> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.rlock_with_ttl_id(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(resource.clone());
}
Ok(false) => {
// 回滚之前锁定的资源
for locked_resource in locked {
let _ = self.runlock(&locked_resource, owner).await;
}
return Ok(false);
}
Err(e) => {
// 回滚之前锁定的资源
for locked_resource in locked {
let _ = self.runlock(&locked_resource, owner).await;
}
return Err(crate::error::LockError::internal(format!("RLock failed: {e}")));
}
}
}
Ok(true)
}
/// 批量释放读锁
pub async fn runlock_batch(&self, resources: &[String], owner: &str) -> crate::error::Result<()> {
for resource in resources {
let _ = self.runlock(resource, owner).await;
}
Ok(())
}
/// 带TTL的写锁支持超时返回 LockId
pub async fn lock_with_ttl_id(
&self,
resource: &str,
owner: &str,
timeout: Duration,
expires_at: Option<Instant>,
) -> std::io::Result<bool> {
let start = Instant::now();
let mut last_check = start;
loop {
{
let entry = self.locks.entry(resource.to_string()).or_insert_with(|| {
Arc::new(RwLock::new(LocalLockEntry {
writer: None,
readers: Vec::new(),
expires_at: None,
}))
});
let mut entry_guard = entry.value().write().await;
if let Some(exp) = entry_guard.expires_at {
if exp <= Instant::now() {
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
}
}
// 写锁需要检查没有写锁且没有读锁或者当前owner已经持有写锁重入性
tracing::debug!("Lock attempt for resource '{}' by owner '{}': writer={:?}, readers={:?}",
resource, owner, entry_guard.writer, entry_guard.readers);
let can_acquire = if let Some(current_writer) = &entry_guard.writer {
// 如果已经有写锁只有同一个owner可以重入
current_writer == owner
} else {
// 如果没有写锁,需要确保也没有读锁
entry_guard.readers.is_empty()
};
if can_acquire {
entry_guard.writer = Some(owner.to_string());
entry_guard.expires_at = expires_at;
let lock_id = crate::types::LockId::new_deterministic(resource);
self.lockid_map
.insert(lock_id.clone(), (resource.to_string(), owner.to_string()));
tracing::debug!("Lock acquired for resource '{}' by owner '{}'", resource, owner);
return Ok(true);
} else {
tracing::debug!("Lock denied for resource '{}' by owner '{}': writer={:?}, readers={:?}",
resource, owner, entry_guard.writer, entry_guard.readers);
}
}
if start.elapsed() >= timeout {
return Ok(false);
}
if last_check.elapsed() >= Duration::from_millis(50) {
tokio::time::sleep(Duration::from_millis(10)).await;
last_check = Instant::now();
} else {
tokio::time::sleep(Duration::from_millis(1)).await;
}
}
}
/// 带TTL的读锁支持超时返回 LockId
pub async fn rlock_with_ttl_id(
&self,
resource: &str,
owner: &str,
timeout: Duration,
expires_at: Option<Instant>,
) -> std::io::Result<bool> {
let start = Instant::now();
let mut last_check = start;
loop {
{
let entry = self.locks.entry(resource.to_string()).or_insert_with(|| {
Arc::new(RwLock::new(LocalLockEntry {
writer: None,
readers: Vec::new(),
expires_at: None,
}))
});
let mut entry_guard = entry.value().write().await;
if let Some(exp) = entry_guard.expires_at {
if exp <= Instant::now() {
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
}
}
if entry_guard.writer.is_none() {
if !entry_guard.readers.contains(&owner.to_string()) {
entry_guard.readers.push(owner.to_string());
}
entry_guard.expires_at = expires_at;
let lock_id = crate::types::LockId::new_deterministic(resource);
self.lockid_map
.insert(lock_id.clone(), (resource.to_string(), owner.to_string()));
return Ok(true);
}
}
if start.elapsed() >= timeout {
return Ok(false);
}
if last_check.elapsed() >= Duration::from_millis(50) {
tokio::time::sleep(Duration::from_millis(10)).await;
last_check = Instant::now();
} else {
tokio::time::sleep(Duration::from_millis(1)).await;
}
}
}
/// 通过 LockId 解锁
pub async fn unlock_by_id(&self, lock_id: &crate::types::LockId) -> std::io::Result<()> {
if let Some((resource, owner)) = self.lockid_map.get(lock_id).map(|v| v.clone()) {
self.unlock(&resource, &owner).await?;
self.lockid_map.remove(lock_id);
Ok(())
} else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "LockId not found"))
}
}
/// 通过 LockId 解锁读锁
pub async fn runlock_by_id(&self, lock_id: &crate::types::LockId) -> std::io::Result<()> {
if let Some((resource, owner)) = self.lockid_map.get(lock_id).map(|v| v.clone()) {
self.runlock(&resource, &owner).await?;
self.lockid_map.remove(lock_id);
Ok(())
} else {
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "LockId not found"))
}
}
/// 批量写锁,返回 Vec<LockId>
pub async fn lock_batch_id(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<Vec<crate::types::LockId>> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.lock_with_ttl_id(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(crate::types::LockId::new_deterministic(resource));
}
Ok(false) => {
// 回滚
for lock_id in locked {
let _ = self.unlock_by_id(&lock_id).await;
}
return Ok(Vec::new());
}
Err(e) => {
for lock_id in locked {
let _ = self.unlock_by_id(&lock_id).await;
}
return Err(crate::error::LockError::internal(format!("Lock failed: {e}")));
}
}
}
Ok(locked)
}
/// 批量释放写锁
pub async fn unlock_batch_id(&self, lock_ids: &[crate::types::LockId]) -> crate::error::Result<()> {
for lock_id in lock_ids {
let _ = self.unlock_by_id(lock_id).await;
}
Ok(())
}
/// 批量读锁,返回 Vec<LockId>
pub async fn rlock_batch_id(
&self,
resources: &[String],
owner: &str,
timeout: std::time::Duration,
ttl: Option<Duration>,
) -> crate::error::Result<Vec<crate::types::LockId>> {
let mut locked = Vec::new();
let expires_at = ttl.map(|t| Instant::now() + t);
for resource in resources {
match self.rlock_with_ttl_id(resource, owner, timeout, expires_at).await {
Ok(true) => {
locked.push(crate::types::LockId::new_deterministic(resource));
}
Ok(false) => {
for lock_id in locked {
let _ = self.runlock_by_id(&lock_id).await;
}
return Ok(Vec::new());
}
Err(e) => {
for lock_id in locked {
let _ = self.runlock_by_id(&lock_id).await;
}
return Err(crate::error::LockError::internal(format!("RLock failed: {e}")));
}
}
}
Ok(locked)
}
/// 批量释放读锁
pub async fn runlock_batch_id(&self, lock_ids: &[crate::types::LockId]) -> crate::error::Result<()> {
for lock_id in lock_ids {
let _ = self.runlock_by_id(lock_id).await;
}
Ok(())
}
/// 带超时的写锁
pub async fn lock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool> {
self.lock_with_ttl_id(resource, owner, timeout, None).await
}
/// 带超时的读锁
pub async fn rlock(&self, resource: &str, owner: &str, timeout: Duration) -> std::io::Result<bool> {
self.rlock_with_ttl_id(resource, owner, timeout, None).await
}
/// 释放写锁
pub async fn unlock(&self, resource: &str, owner: &str) -> std::io::Result<()> {
if let Some(entry) = self.locks.get(resource) {
let mut entry_guard = entry.value().write().await;
if entry_guard.writer.as_ref() == Some(&owner.to_string()) {
entry_guard.writer = None;
if entry_guard.readers.is_empty() {
entry_guard.expires_at = None;
}
}
}
Ok(())
}
/// 释放读锁
pub async fn runlock(&self, resource: &str, owner: &str) -> std::io::Result<()> {
if let Some(entry) = self.locks.get(resource) {
let mut entry_guard = entry.value().write().await;
entry_guard.readers.retain(|r| r != &owner.to_string());
if entry_guard.readers.is_empty() && entry_guard.writer.is_none() {
entry_guard.expires_at = None;
}
}
Ok(())
}
/// 检查资源是否被锁定
pub async fn is_locked(&self, resource: &str) -> bool {
if let Some(entry) = self.locks.get(resource) {
let entry_guard = entry.value().read().await;
entry_guard.writer.is_some() || !entry_guard.readers.is_empty()
} else {
false
}
}
/// 获取锁信息
pub async fn get_lock(&self, resource: &str) -> Option<crate::types::LockInfo> {
if let Some(entry) = self.locks.get(resource) {
let entry_guard = entry.value().read().await;
if let Some(writer) = &entry_guard.writer {
Some(crate::types::LockInfo {
id: crate::types::LockId::new("test-lock"),
resource: resource.to_string(),
lock_type: crate::types::LockType::Exclusive,
status: crate::types::LockStatus::Acquired,
owner: writer.clone(),
acquired_at: std::time::SystemTime::now(),
expires_at: entry_guard
.expires_at
.map(|t| {
std::time::SystemTime::UNIX_EPOCH
+ std::time::Duration::from_secs(t.duration_since(Instant::now()).as_secs())
})
.unwrap_or_else(|| std::time::SystemTime::now() + std::time::Duration::from_secs(30)),
last_refreshed: std::time::SystemTime::now(),
metadata: crate::types::LockMetadata::default(),
priority: crate::types::LockPriority::Normal,
wait_start_time: None,
})
} else if !entry_guard.readers.is_empty() {
Some(crate::types::LockInfo {
id: crate::types::LockId::new("test-lock"),
resource: resource.to_string(),
lock_type: crate::types::LockType::Shared,
status: crate::types::LockStatus::Acquired,
owner: entry_guard.readers[0].clone(),
acquired_at: std::time::SystemTime::now(),
expires_at: entry_guard
.expires_at
.map(|t| {
std::time::SystemTime::UNIX_EPOCH
+ std::time::Duration::from_secs(t.duration_since(Instant::now()).as_secs())
})
.unwrap_or_else(|| std::time::SystemTime::now() + std::time::Duration::from_secs(30)),
last_refreshed: std::time::SystemTime::now(),
metadata: crate::types::LockMetadata::default(),
priority: crate::types::LockPriority::Normal,
wait_start_time: None,
})
} else {
None
}
} else {
None
}
}
/// Acquire exclusive lock
pub async fn acquire_exclusive_lock(
&self,
resource: &str,
_lock_id: &crate::types::LockId,
owner: &str,
timeout: Duration,
) -> crate::error::Result<()> {
let success = self
.lock_with_ttl_id(resource, owner, timeout, None)
.await
.map_err(|e| crate::error::LockError::internal(format!("Lock acquisition failed: {e}")))?;
if success {
Ok(())
} else {
Err(crate::error::LockError::internal("Lock acquisition timeout"))
}
}
/// Acquire shared lock
pub async fn acquire_shared_lock(
&self,
resource: &str,
_lock_id: &crate::types::LockId,
owner: &str,
timeout: Duration,
) -> crate::error::Result<()> {
let success = self
.rlock_with_ttl_id(resource, owner, timeout, None)
.await
.map_err(|e| crate::error::LockError::internal(format!("Shared lock acquisition failed: {e}")))?;
if success {
Ok(())
} else {
Err(crate::error::LockError::internal("Shared lock acquisition timeout"))
}
}
/// Release lock
pub async fn release_lock(&self, resource: &str, owner: &str) -> crate::error::Result<()> {
self.unlock(resource, owner)
.await
.map_err(|e| crate::error::LockError::internal(format!("Lock release failed: {e}")))
}
/// Refresh lock
pub async fn refresh_lock(&self, resource: &str, _owner: &str) -> crate::error::Result<()> {
// For local locks, refresh is not needed as they don't expire automatically
// Just check if the lock still exists
if self.is_locked(resource).await {
Ok(())
} else {
Err(crate::error::LockError::internal("Lock not found or expired"))
}
}
/// Force release lock
pub async fn force_release_lock(&self, resource: &str) -> crate::error::Result<()> {
if let Some(entry) = self.locks.get(resource) {
let mut entry_guard = entry.value().write().await;
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
Ok(())
} else {
Ok(())
}
}
/// Clean up expired locks
pub async fn cleanup_expired_locks(&self) -> usize {
let now = Instant::now();
let mut cleaned = 0;
let mut to_remove = Vec::new();
for item in self.locks.iter() {
let mut entry_guard = item.value().write().await;
if let Some(exp) = entry_guard.expires_at {
if exp <= now {
entry_guard.writer = None;
entry_guard.readers.clear();
entry_guard.expires_at = None;
cleaned += 1;
if entry_guard.writer.is_none() && entry_guard.readers.is_empty() {
to_remove.push(item.key().clone());
}
}
}
}
for key in to_remove {
self.locks.remove(&key);
}
cleaned
}
/// List all locks
pub async fn list_locks(&self) -> Vec<crate::types::LockInfo> {
let mut locks = Vec::new();
for item in self.locks.iter() {
if let Some(lock_info) = self.get_lock(item.key()).await {
locks.push(lock_info);
}
}
locks
}
/// Get locks for a specific resource
pub async fn get_locks_for_resource(&self, resource: &str) -> Vec<crate::types::LockInfo> {
if let Some(lock_info) = self.get_lock(resource).await {
vec![lock_info]
} else {
Vec::new()
}
}
/// Get statistics
pub async fn get_stats(&self) -> crate::types::LockStats {
let mut stats = crate::types::LockStats::default();
let mut total_locks = 0;
let mut exclusive_locks = 0;
let mut shared_locks = 0;
for item in self.locks.iter() {
let entry_guard = item.value().read().await;
if entry_guard.writer.is_some() {
exclusive_locks += 1;
total_locks += 1;
}
if !entry_guard.readers.is_empty() {
shared_locks += entry_guard.readers.len();
total_locks += entry_guard.readers.len();
}
}
stats.total_locks = total_locks;
stats.exclusive_locks = exclusive_locks;
stats.shared_locks = shared_locks;
stats.last_updated = std::time::SystemTime::now();
stats
}
}
impl Default for LocalLockMap {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::time::Duration;
use tokio::task;
/// 测试基本写锁获取和释放
#[tokio::test]
async fn test_write_lock_basic() {
let lock_map = LocalLockMap::new();
let ok = lock_map.lock("foo", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok, "Write lock should be successfully acquired");
assert!(lock_map.is_locked("foo").await, "Lock state should be locked");
lock_map.unlock("foo", "owner1").await.unwrap();
assert!(!lock_map.is_locked("foo").await, "Should be unlocked after release");
}
/// 测试基本读锁获取和释放
#[tokio::test]
async fn test_read_lock_basic() {
let lock_map = LocalLockMap::new();
let ok = lock_map.rlock("bar", "reader1", Duration::from_millis(100)).await.unwrap();
assert!(ok, "Read lock should be successfully acquired");
assert!(lock_map.is_locked("bar").await, "Lock state should be locked");
lock_map.runlock("bar", "reader1").await.unwrap();
assert!(!lock_map.is_locked("bar").await, "Should be unlocked after release");
}
/// 测试写锁互斥
#[tokio::test]
async fn test_write_lock_mutex() {
let lock_map = Arc::new(LocalLockMap::new());
// owner1首先获取写锁
let ok = lock_map.lock("res", "owner1", Duration::from_millis(100)).await.unwrap();
assert!(ok);
// owner2尝试在同一资源上获取写锁应该超时并失败
let lock_map2 = lock_map.clone();
let fut = task::spawn(async move { lock_map2.lock("res", "owner2", Duration::from_millis(50)).await.unwrap() });
let ok2 = fut.await.unwrap();
assert!(!ok2, "Write locks should be mutually exclusive, owner2 acquisition should fail");
lock_map.unlock("res", "owner1").await.unwrap();
}
/// 测试读锁共享
#[tokio::test]
async fn test_read_lock_sharing() {
let lock_map = Arc::new(LocalLockMap::new());
// reader1获取读锁
let ok1 = lock_map.rlock("res", "reader1", Duration::from_millis(100)).await.unwrap();
assert!(ok1);
// reader2也应该能够获取读锁
let ok2 = lock_map.rlock("res", "reader2", Duration::from_millis(100)).await.unwrap();
assert!(ok2, "Multiple readers should be able to acquire read locks");
lock_map.runlock("res", "reader1").await.unwrap();
lock_map.runlock("res", "reader2").await.unwrap();
}
/// 测试批量锁操作
#[tokio::test]
async fn test_batch_lock_operations() {
let lock_map = LocalLockMap::new();
let resources = vec!["res1".to_string(), "res2".to_string(), "res3".to_string()];
// 批量获取写锁
let ok = lock_map
.lock_batch(&resources, "owner1", Duration::from_millis(100), None)
.await
.unwrap();
assert!(ok, "Batch lock should succeed");
// 检查所有资源都被锁定
for resource in &resources {
assert!(lock_map.is_locked(resource).await, "Resource {resource} should be locked");
}
// 批量释放写锁
lock_map.unlock_batch(&resources, "owner1").await.unwrap();
// 检查所有资源都被释放
for resource in &resources {
assert!(!lock_map.is_locked(resource).await, "Resource {resource} should be unlocked");
}
}
}

View File

@@ -1,35 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct LockArgs {
pub uid: String,
pub resources: Vec<String>,
pub owner: String,
pub source: String,
pub quorum: usize,
}
impl Display for LockArgs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"LockArgs[ uid: {}, resources: {:?}, owner: {}, source:{}, quorum: {} ]",
self.uid, self.resources, self.owner, self.source, self.quorum
)
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -50,8 +50,6 @@ pub enum LockPriority {
Critical = 4,
}
/// Lock information structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockInfo {
@@ -79,36 +77,99 @@ pub struct LockInfo {
pub wait_start_time: Option<SystemTime>,
}
impl LockInfo {
/// Check if the lock has expired
pub fn has_expired(&self) -> bool {
self.expires_at <= SystemTime::now()
}
/// Get remaining time until expiration
pub fn remaining_time(&self) -> Duration {
let now = SystemTime::now();
if self.expires_at > now {
self.expires_at.duration_since(now).unwrap_or(Duration::ZERO)
} else {
Duration::ZERO
}
}
/// Check if the lock is still valid
pub fn is_valid(&self) -> bool {
!self.has_expired() && self.status == LockStatus::Acquired
}
}
/// Lock ID type
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LockId(pub String);
pub struct LockId {
pub resource: String,
pub uuid: String,
}
impl LockId {
/// Generate new lock ID
pub fn new() -> Self {
Self(Uuid::new_v4().to_string())
/// Generate new lock ID for a resource
pub fn new(resource: &str) -> Self {
Self {
resource: resource.to_string(),
uuid: Uuid::new_v4().to_string(),
}
}
/// Create lock ID from string
/// Generate deterministic lock ID for a resource (same resource = same ID)
pub fn new_deterministic(resource: &str) -> Self {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
resource.hash(&mut hasher);
let hash = hasher.finish();
Self {
resource: resource.to_string(),
uuid: format!("{hash:016x}"),
}
}
/// Create lock ID from resource and uuid
pub fn from_parts(resource: impl Into<String>, uuid: impl Into<String>) -> Self {
Self {
resource: resource.into(),
uuid: uuid.into(),
}
}
/// Create lock ID from string (for compatibility, expects "resource:uuid")
pub fn from_string(id: impl Into<String>) -> Self {
Self(id.into())
let s = id.into();
if let Some((resource, uuid)) = s.split_once(":") {
Self {
resource: resource.to_string(),
uuid: uuid.to_string(),
}
} else {
// fallback: treat as uuid only
Self {
resource: "unknown".to_string(),
uuid: s,
}
}
}
/// Get string representation of lock ID
pub fn as_str(&self) -> &str {
&self.0
/// Get string representation of lock ID ("resource:uuid")
pub fn as_str(&self) -> String {
format!("{}:{}", self.resource, self.uuid)
}
}
impl Default for LockId {
fn default() -> Self {
Self::new()
Self::new("default")
}
}
impl std::fmt::Display for LockId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}:{}", self.resource, self.uuid)
}
}
@@ -173,6 +234,8 @@ impl LockMetadata {
/// Lock request structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockRequest {
/// Lock ID
pub lock_id: LockId,
/// Resource path
pub resource: String,
/// Lock type
@@ -194,8 +257,10 @@ pub struct LockRequest {
impl LockRequest {
/// Create new lock request
pub fn new(resource: impl Into<String>, lock_type: LockType, owner: impl Into<String>) -> Self {
let resource_str = resource.into();
Self {
resource: resource.into(),
lock_id: LockId::new_deterministic(&resource_str),
resource: resource_str,
lock_type,
owner: owner.into(),
timeout: Duration::from_secs(30),
@@ -332,6 +397,14 @@ pub struct LockStats {
pub average_hold_time: Duration,
/// Total wait queues
pub total_wait_queues: usize,
/// Queue entries
pub queue_entries: usize,
/// Average wait time
pub avg_wait_time: Duration,
/// Successful acquires
pub successful_acquires: usize,
/// Failed acquires
pub failed_acquires: usize,
}
impl Default for LockStats {
@@ -348,6 +421,10 @@ impl Default for LockStats {
total_hold_time: Duration::ZERO,
average_hold_time: Duration::ZERO,
total_wait_queues: 0,
queue_entries: 0,
avg_wait_time: Duration::ZERO,
successful_acquires: 0,
failed_acquires: 0,
}
}
}
@@ -379,8 +456,6 @@ pub enum NodeStatus {
Degraded,
}
/// Cluster information structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterInfo {
@@ -408,7 +483,49 @@ pub enum ClusterStatus {
Unhealthy,
}
/// Health check status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
/// Healthy
Healthy,
/// Degraded
Degraded,
/// Unhealthy
Unhealthy,
}
/// Health check information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthInfo {
/// Overall status
pub status: HealthStatus,
/// Node ID
pub node_id: String,
/// Last heartbeat time
pub last_heartbeat: SystemTime,
/// Connected nodes count
pub connected_nodes: usize,
/// Total nodes count
pub total_nodes: usize,
/// Lock statistics
pub lock_stats: LockStats,
/// Error message (if any)
pub error_message: Option<String>,
}
impl Default for HealthInfo {
fn default() -> Self {
Self {
status: HealthStatus::Healthy,
node_id: "unknown".to_string(),
last_heartbeat: SystemTime::now(),
connected_nodes: 1,
total_nodes: 1,
lock_stats: LockStats::default(),
error_message: None,
}
}
}
/// Timestamp type alias
pub type Timestamp = u64;
@@ -498,12 +615,12 @@ mod tests {
#[test]
fn test_lock_id() {
let id1 = LockId::new();
let id2 = LockId::new();
let id1 = LockId::new("test-resource");
let id2 = LockId::new("test-resource");
assert_ne!(id1, id2);
let id3 = LockId::from_string("test-id");
assert_eq!(id3.as_str(), "test-id");
let id3 = LockId::from_string("test-resource:test-uuid");
assert_eq!(id3.as_str(), "test-resource:test-uuid");
}
#[test]
@@ -538,7 +655,7 @@ mod tests {
#[test]
fn test_lock_response() {
let lock_info = LockInfo {
id: LockId::new(),
id: LockId::new("test-resource"),
resource: "test".to_string(),
lock_type: LockType::Exclusive,
status: LockStatus::Acquired,

View File

@@ -1,374 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod path;
pub mod uuid;
use std::time::{Duration, SystemTime};
/// Retry strategy
pub struct RetryStrategy {
max_attempts: usize,
base_delay: Duration,
max_delay: Duration,
backoff_multiplier: f64,
}
impl Default for RetryStrategy {
fn default() -> Self {
Self {
max_attempts: 3,
base_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(30),
backoff_multiplier: 2.0,
}
}
}
impl RetryStrategy {
/// Create new retry strategy
pub fn new(max_attempts: usize, base_delay: Duration) -> Self {
Self {
max_attempts,
base_delay,
max_delay: Duration::from_secs(30),
backoff_multiplier: 2.0,
}
}
/// Set maximum delay
pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
self.max_delay = max_delay;
self
}
/// Set backoff multiplier
pub fn with_backoff_multiplier(mut self, multiplier: f64) -> Self {
self.backoff_multiplier = multiplier;
self
}
/// Calculate delay time for nth retry
pub fn delay_for_attempt(&self, attempt: usize) -> Duration {
if attempt == 0 {
return Duration::ZERO;
}
let delay = self.base_delay.mul_f64(self.backoff_multiplier.powi(attempt as i32 - 1));
delay.min(self.max_delay)
}
/// Get maximum retry attempts
pub fn max_attempts(&self) -> usize {
self.max_attempts
}
}
/// Operation executor with retry
pub async fn with_retry<F, Fut, T, E>(strategy: &RetryStrategy, mut operation: F) -> Result<T, E>
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = Result<T, E>>,
E: std::fmt::Debug,
{
let mut last_error = None;
for attempt in 0..strategy.max_attempts() {
match operation().await {
Ok(result) => return Ok(result),
Err(e) => {
last_error = Some(e);
if attempt < strategy.max_attempts() - 1 {
let delay = strategy.delay_for_attempt(attempt + 1);
tokio::time::sleep(delay).await;
}
}
}
}
Err(last_error.unwrap())
}
/// Timeout wrapper
pub async fn with_timeout<Fut, T>(timeout: Duration, future: Fut) -> Result<T, crate::error::LockError>
where
Fut: std::future::Future<Output = Result<T, crate::error::LockError>>,
{
tokio::time::timeout(timeout, future)
.await
.map_err(|_| crate::error::LockError::timeout("operation", timeout))?
}
/// Calculate duration between two time points
pub fn duration_between(start: SystemTime, end: SystemTime) -> Duration {
end.duration_since(start).unwrap_or_default()
}
/// Check if time is expired
pub fn is_expired(expiry_time: SystemTime) -> bool {
SystemTime::now() >= expiry_time
}
/// Calculate remaining time
pub fn remaining_time(expiry_time: SystemTime) -> Duration {
expiry_time.duration_since(SystemTime::now()).unwrap_or_default()
}
/// Generate random delay time
pub fn random_delay(base_delay: Duration, jitter_factor: f64) -> Duration {
use rand::Rng;
let mut rng = rand::rng();
let jitter = rng.random_range(-jitter_factor..jitter_factor);
let multiplier = 1.0 + jitter;
base_delay.mul_f64(multiplier)
}
/// Calculate hash value
pub fn calculate_hash(data: &[u8]) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
hasher.finish()
}
/// Generate resource identifier
pub fn generate_resource_id(prefix: &str, components: &[&str]) -> String {
let mut id = prefix.to_string();
for component in components {
id.push('/');
id.push_str(component);
}
id
}
/// Validate resource path
pub fn validate_resource_path(path: &str) -> bool {
!path.is_empty() && !path.contains('\0') && path.len() <= 1024
}
/// Normalize resource path
pub fn normalize_resource_path(path: &str) -> String {
let mut normalized = path.to_string();
// Remove leading and trailing slashes
normalized = normalized.trim_matches('/').to_string();
// Replace multiple consecutive slashes with single slash
while normalized.contains("//") {
normalized = normalized.replace("//", "/");
}
// If path is empty, return root path
if normalized.is_empty() {
normalized = "/".to_string();
}
normalized
}
/// Parse resource path components
pub fn parse_resource_components(path: &str) -> Vec<String> {
let normalized = normalize_resource_path(path);
if normalized == "/" {
return vec![];
}
normalized
.split('/')
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect()
}
/// Check if path matches pattern
pub fn path_matches_pattern(path: &str, pattern: &str) -> bool {
let path_components = parse_resource_components(path);
let pattern_components = parse_resource_components(pattern);
if pattern_components.is_empty() {
return path_components.is_empty();
}
if path_components.len() != pattern_components.len() {
return false;
}
for (path_comp, pattern_comp) in path_components.iter().zip(pattern_components.iter()) {
if pattern_comp == "*" {
continue;
}
if path_comp != pattern_comp {
return false;
}
}
true
}
/// Generate lock key
pub fn generate_lock_key(resource: &str, lock_type: crate::types::LockType) -> String {
let type_str = match lock_type {
crate::types::LockType::Exclusive => "exclusive",
crate::types::LockType::Shared => "shared",
};
format!("lock:{type_str}:{resource}")
}
/// Parse lock key
pub fn parse_lock_key(lock_key: &str) -> Option<(crate::types::LockType, String)> {
let parts: Vec<&str> = lock_key.splitn(3, ':').collect();
if parts.len() != 3 || parts[0] != "lock" {
return None;
}
let lock_type = match parts[1] {
"exclusive" => crate::types::LockType::Exclusive,
"shared" => crate::types::LockType::Shared,
_ => return None,
};
Some((lock_type, parts[2].to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::LockType;
#[test]
fn test_retry_strategy() {
let strategy = RetryStrategy::new(3, Duration::from_millis(100));
assert_eq!(strategy.max_attempts(), 3);
assert_eq!(strategy.delay_for_attempt(0), Duration::ZERO);
assert_eq!(strategy.delay_for_attempt(1), Duration::from_millis(100));
assert_eq!(strategy.delay_for_attempt(2), Duration::from_millis(200));
}
#[test]
fn test_time_utilities() {
let now = SystemTime::now();
let future = now + Duration::from_secs(10);
assert!(!is_expired(future));
assert!(remaining_time(future) > Duration::ZERO);
let past = now - Duration::from_secs(10);
assert!(is_expired(past));
assert_eq!(remaining_time(past), Duration::ZERO);
}
#[test]
fn test_resource_path_validation() {
assert!(validate_resource_path("/valid/path"));
assert!(validate_resource_path("valid/path"));
assert!(!validate_resource_path(""));
assert!(!validate_resource_path("path\0with\0null"));
let long_path = "a".repeat(1025);
assert!(!validate_resource_path(&long_path));
}
#[test]
fn test_resource_path_normalization() {
assert_eq!(normalize_resource_path("/path/to/resource"), "path/to/resource");
assert_eq!(normalize_resource_path("path//to///resource"), "path/to/resource");
assert_eq!(normalize_resource_path(""), "/");
assert_eq!(normalize_resource_path("/"), "/");
}
#[test]
fn test_resource_path_components() {
assert_eq!(parse_resource_components("/"), vec![] as Vec<String>);
assert_eq!(parse_resource_components("/path/to/resource"), vec!["path", "to", "resource"]);
assert_eq!(parse_resource_components("path/to/resource"), vec!["path", "to", "resource"]);
}
#[test]
fn test_path_pattern_matching() {
assert!(path_matches_pattern("/path/to/resource", "/path/to/resource"));
assert!(path_matches_pattern("/path/to/resource", "/path/*/resource"));
assert!(path_matches_pattern("/path/to/resource", "/*/*/*"));
assert!(!path_matches_pattern("/path/to/resource", "/path/to/other"));
assert!(!path_matches_pattern("/path/to/resource", "/path/to/resource/extra"));
}
#[test]
fn test_lock_key_generation() {
let key1 = generate_lock_key("/path/to/resource", LockType::Exclusive);
assert_eq!(key1, "lock:exclusive:/path/to/resource");
let key2 = generate_lock_key("/path/to/resource", LockType::Shared);
assert_eq!(key2, "lock:shared:/path/to/resource");
}
#[test]
fn test_lock_key_parsing() {
let (lock_type, resource) = parse_lock_key("lock:exclusive:/path/to/resource").unwrap();
assert_eq!(lock_type, LockType::Exclusive);
assert_eq!(resource, "/path/to/resource");
let (lock_type, resource) = parse_lock_key("lock:shared:/path/to/resource").unwrap();
assert_eq!(lock_type, LockType::Shared);
assert_eq!(resource, "/path/to/resource");
assert!(parse_lock_key("invalid:key").is_none());
assert!(parse_lock_key("lock:invalid:/path").is_none());
}
#[tokio::test]
async fn test_with_retry() {
let strategy = RetryStrategy::new(3, Duration::from_millis(10));
let attempts = std::sync::Arc::new(std::sync::Mutex::new(0));
let result = with_retry(&strategy, {
let attempts = attempts.clone();
move || {
let attempts = attempts.clone();
async move {
let mut count = attempts.lock().unwrap();
*count += 1;
if *count < 3 { Err("temporary error") } else { Ok("success") }
}
}
})
.await;
assert_eq!(result, Ok("success"));
assert_eq!(*attempts.lock().unwrap(), 3);
}
#[tokio::test]
async fn test_with_timeout() {
let result = with_timeout(Duration::from_millis(100), async {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok::<&str, crate::error::LockError>("success")
})
.await;
assert!(result.is_ok());
let result = with_timeout(Duration::from_millis(50), async {
tokio::time::sleep(Duration::from_millis(100)).await;
Ok::<&str, crate::error::LockError>("success")
})
.await;
assert!(result.is_err());
}
}

View File

@@ -1,104 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::{Path, PathBuf};
/// Path processing tool
pub struct PathUtils;
impl PathUtils {
/// Normalize path
pub fn normalize(path: &str) -> String {
let path_buf = PathBuf::from(path);
path_buf.to_string_lossy().to_string()
}
/// Join paths
pub fn join(base: &str, path: &str) -> String {
let base_path = PathBuf::from(base);
let joined = base_path.join(path);
joined.to_string_lossy().to_string()
}
/// Get parent directory
pub fn parent(path: &str) -> Option<String> {
let path_buf = PathBuf::from(path);
path_buf.parent().map(|p| p.to_string_lossy().to_string())
}
/// Get filename
pub fn filename(path: &str) -> Option<String> {
let path_buf = PathBuf::from(path);
path_buf.file_name().map(|name| name.to_string_lossy().to_string())
}
/// Check if path is absolute
pub fn is_absolute(path: &str) -> bool {
Path::new(path).is_absolute()
}
/// Check if path exists
pub fn exists(path: &str) -> bool {
Path::new(path).exists()
}
/// Create directory (if not exists)
pub fn create_dir_all(path: &str) -> std::io::Result<()> {
std::fs::create_dir_all(path)
}
/// Remove file or directory
pub fn remove(path: &str) -> std::io::Result<()> {
if Path::new(path).is_file() {
std::fs::remove_file(path)
} else {
std::fs::remove_dir_all(path)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_path_normalization() {
assert_eq!(PathUtils::normalize("/path/to/resource"), "/path/to/resource");
assert_eq!(PathUtils::normalize("path/to/resource"), "path/to/resource");
}
#[test]
fn test_path_joining() {
assert_eq!(PathUtils::join("/base", "path"), "/base/path");
assert_eq!(PathUtils::join("base", "path"), "base/path");
}
#[test]
fn test_path_parent() {
assert_eq!(PathUtils::parent("/path/to/resource"), Some("/path/to".to_string()));
assert_eq!(PathUtils::parent("/"), None);
}
#[test]
fn test_path_filename() {
assert_eq!(PathUtils::filename("/path/to/resource"), Some("resource".to_string()));
assert_eq!(PathUtils::filename("/"), None);
}
#[test]
fn test_path_absolute() {
assert!(PathUtils::is_absolute("/path/to/resource"));
assert!(!PathUtils::is_absolute("path/to/resource"));
}
}

View File

@@ -1,102 +0,0 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use uuid::Uuid;
/// UUID tool
pub struct UuidUtils;
impl UuidUtils {
/// Generate new UUID v4
pub fn new_v4() -> String {
Uuid::new_v4().to_string()
}
/// Generate new UUID v4 in short format
pub fn new_v4_short() -> String {
Uuid::new_v4().simple().to_string()
}
/// Parse UUID from string
pub fn parse(uuid_str: &str) -> Result<Uuid, uuid::Error> {
Uuid::parse_str(uuid_str)
}
/// Check if string is a valid UUID
pub fn is_valid(uuid_str: &str) -> bool {
Uuid::parse_str(uuid_str).is_ok()
}
/// Generate UUID v1 based on time
pub fn new_v1() -> String {
// Note: Here we use v4 as a substitute because v1 requires system clock
Uuid::new_v4().to_string()
}
/// Generate UUID v5 based on name
pub fn new_v5(_namespace: &Uuid, _name: &str) -> String {
Uuid::new_v4().to_string() // Simplified implementation, use v4 as substitute
}
/// Generate UUID v3 based on MD5
pub fn new_v3(_namespace: &Uuid, _name: &str) -> String {
Uuid::new_v4().to_string() // Simplified implementation, use v4 as substitute
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uuid_generation() {
let uuid1 = UuidUtils::new_v4();
let uuid2 = UuidUtils::new_v4();
assert_ne!(uuid1, uuid2);
assert!(UuidUtils::is_valid(&uuid1));
assert!(UuidUtils::is_valid(&uuid2));
}
#[test]
fn test_uuid_validation() {
assert!(UuidUtils::is_valid("550e8400-e29b-41d4-a716-446655440000"));
assert!(!UuidUtils::is_valid("invalid-uuid"));
assert!(!UuidUtils::is_valid(""));
}
#[test]
fn test_uuid_parsing() {
let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
let parsed = UuidUtils::parse(uuid_str);
assert!(parsed.is_ok());
let invalid = UuidUtils::parse("invalid");
assert!(invalid.is_err());
}
#[test]
fn test_uuid_v5() {
let namespace = Uuid::NAMESPACE_DNS;
let name = "example.com";
let uuid = UuidUtils::new_v5(&namespace, name);
assert!(UuidUtils::is_valid(&uuid));
// Note: Since the simplified implementation uses v4, the same input will not produce the same output
// Here we only test that the generated UUID is valid
let uuid2 = UuidUtils::new_v5(&namespace, name);
assert!(UuidUtils::is_valid(&uuid2));
}
}