mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-21 20:06:37 +00:00
@@ -11,7 +11,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use common::error::Result;
|
use common::error::Result;
|
||||||
|
|
||||||
pub type RWLockerImpl = Box<dyn RWLocker + Send>;
|
pub type RWLockerImpl = Box<dyn RWLocker + Send + Sync>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait RWLocker {
|
pub trait RWLocker {
|
||||||
@@ -93,22 +93,33 @@ impl NsLockMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct WrapperLocker(pub Arc<RwLock<RWLockerImpl>>);
|
||||||
|
|
||||||
|
impl Drop for WrapperLocker {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let inner = self.0.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let _ = inner.write().await.un_lock().await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn new_nslock(
|
pub async fn new_nslock(
|
||||||
ns: Arc<RwLock<NsLockMap>>,
|
ns: Arc<RwLock<NsLockMap>>,
|
||||||
owner: String,
|
owner: String,
|
||||||
volume: String,
|
volume: String,
|
||||||
paths: Vec<String>,
|
paths: Vec<String>,
|
||||||
lockers: Vec<LockApi>,
|
lockers: Vec<LockApi>,
|
||||||
) -> RWLockerImpl {
|
) -> WrapperLocker {
|
||||||
if ns.read().await.is_dist_erasure {
|
if ns.read().await.is_dist_erasure {
|
||||||
let names = paths
|
let names = paths
|
||||||
.iter()
|
.iter()
|
||||||
.map(|path| Path::new(&volume).join(path).to_str().unwrap().to_string())
|
.map(|path| Path::new(&volume).join(path).to_str().unwrap().to_string())
|
||||||
.collect();
|
.collect();
|
||||||
return Box::new(DistLockInstance::new(owner, names, lockers));
|
return WrapperLocker(Arc::new(RwLock::new(Box::new(DistLockInstance::new(owner, names, lockers)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::new(LocalLockInstance::new(ns, volume, paths))
|
WrapperLocker(Arc::new(RwLock::new(Box::new(LocalLockInstance::new(ns, volume, paths)))))
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DistLockInstance {
|
struct DistLockInstance {
|
||||||
@@ -258,7 +269,7 @@ mod test {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_local_instance() -> Result<()> {
|
async fn test_local_instance() -> Result<()> {
|
||||||
let ns_lock_map = Arc::new(RwLock::new(NsLockMap::default()));
|
let ns_lock_map = Arc::new(RwLock::new(NsLockMap::default()));
|
||||||
let mut ns = new_nslock(
|
let ns = new_nslock(
|
||||||
Arc::clone(&ns_lock_map),
|
Arc::clone(&ns_lock_map),
|
||||||
"local".to_string(),
|
"local".to_string(),
|
||||||
"test".to_string(),
|
"test".to_string(),
|
||||||
@@ -267,7 +278,7 @@ mod test {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let result = ns
|
let result = ns.0.write().await
|
||||||
.get_lock(&Options {
|
.get_lock(&Options {
|
||||||
timeout: Duration::from_secs(5),
|
timeout: Duration::from_secs(5),
|
||||||
retry_interval: Duration::from_secs(1),
|
retry_interval: Duration::from_secs(1),
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
|||||||
let url = url::Url::parse("http://127.0.0.1:9000/data")?;
|
let url = url::Url::parse("http://127.0.0.1:9000/data")?;
|
||||||
let locker = new_lock_api(false, Some(url));
|
let locker = new_lock_api(false, Some(url));
|
||||||
let ns_mutex = Arc::new(RwLock::new(NsLockMap::new(true)));
|
let ns_mutex = Arc::new(RwLock::new(NsLockMap::new(true)));
|
||||||
let mut ns = new_nslock(
|
let ns = new_nslock(
|
||||||
Arc::clone(&ns_mutex),
|
Arc::clone(&ns_mutex),
|
||||||
"local".to_string(),
|
"local".to_string(),
|
||||||
"dandan".to_string(),
|
"dandan".to_string(),
|
||||||
@@ -59,7 +59,7 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ns.get_lock(&Options {
|
ns.0.write().await.get_lock(&Options {
|
||||||
timeout: Duration::from_secs(5),
|
timeout: Duration::from_secs(5),
|
||||||
retry_interval: Duration::from_secs(1),
|
retry_interval: Duration::from_secs(1),
|
||||||
})
|
})
|
||||||
@@ -68,6 +68,6 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
ns.un_lock().await.unwrap();
|
ns.0.write().await.un_lock().await.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user