mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-10 15:16:56 +00:00
+20
-12
@@ -33,8 +33,8 @@ impl Granted {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_locked(uid: &String) -> bool {
|
||||
uid.len() > 0
|
||||
fn is_locked(uid: &str) -> bool {
|
||||
!uid.is_empty()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -59,6 +59,14 @@ impl DRWMutex {
|
||||
lock_retry_min_interval: LOCK_RETRY_MIN_INTERVAL,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_locked(&self) -> bool {
|
||||
self.write_locks.iter().any(|w_lock| is_locked(w_lock))
|
||||
}
|
||||
|
||||
fn is_r_locked(&self) -> bool {
|
||||
self.read_locks.iter().any(|r_lock| is_locked(r_lock))
|
||||
}
|
||||
}
|
||||
|
||||
impl DRWMutex {
|
||||
@@ -134,7 +142,7 @@ impl DRWMutex {
|
||||
|
||||
async fn inner_lock(
|
||||
&mut self,
|
||||
locks: &mut Vec<String>,
|
||||
locks: &mut [String],
|
||||
id: &String,
|
||||
source: &String,
|
||||
is_read_lock: bool,
|
||||
@@ -201,7 +209,7 @@ impl DRWMutex {
|
||||
}
|
||||
|
||||
pub async fn un_lock(&mut self) {
|
||||
if self.write_locks.is_empty() || !self.write_locks.iter().any(|w_lock| is_locked(w_lock)) {
|
||||
if self.write_locks.is_empty() || !self.is_locked() {
|
||||
panic!("Trying to un_lock() while no lock() is active, write_locks: {:?}", self.write_locks)
|
||||
}
|
||||
|
||||
@@ -222,7 +230,7 @@ impl DRWMutex {
|
||||
}
|
||||
|
||||
pub async fn un_r_lock(&mut self) {
|
||||
if self.read_locks.is_empty() || !self.read_locks.iter().any(|r_lock| is_locked(r_lock)) {
|
||||
if self.read_locks.is_empty() || !self.is_r_locked() {
|
||||
panic!("Trying to un_r_lock() while no r_lock() is active, read_locks: {:?}", self.read_locks)
|
||||
}
|
||||
|
||||
@@ -242,14 +250,14 @@ impl DRWMutex {
|
||||
}
|
||||
}
|
||||
|
||||
async fn release_all(&mut self, tolerance: usize, locks: &mut Vec<String>, is_read_lock: bool) -> bool {
|
||||
async fn release_all(&mut self, tolerance: usize, locks: &mut [String], is_read_lock: bool) -> bool {
|
||||
for (index, locker) in self.lockers.iter_mut().enumerate() {
|
||||
if send_release(locker, &locks[index], &self.owner, &self.names, is_read_lock).await {
|
||||
locks[index] = "".to_string();
|
||||
}
|
||||
}
|
||||
|
||||
!check_failed_unlocks(&locks, tolerance)
|
||||
!check_failed_unlocks(locks, tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +285,7 @@ impl DRWMutex {
|
||||
// });
|
||||
// }
|
||||
|
||||
fn check_failed_unlocks(locks: &Vec<String>, tolerance: usize) -> bool {
|
||||
fn check_failed_unlocks(locks: &[String], tolerance: usize) -> bool {
|
||||
let mut un_locks_failed = 0;
|
||||
locks.iter().for_each(|lock| {
|
||||
if is_locked(lock) {
|
||||
@@ -292,15 +300,15 @@ fn check_failed_unlocks(locks: &Vec<String>, tolerance: usize) -> bool {
|
||||
un_locks_failed > tolerance
|
||||
}
|
||||
|
||||
async fn send_release(locker: &mut LockApi, uid: &String, owner: &String, names: &Vec<String>, is_read_lock: bool) -> bool {
|
||||
async fn send_release(locker: &mut LockApi, uid: &String, owner: &str, names: &[String], is_read_lock: bool) -> bool {
|
||||
if uid.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let args = LockArgs {
|
||||
uid: uid.to_string(),
|
||||
owner: owner.clone(),
|
||||
resources: names.clone(),
|
||||
owner: owner.to_owned(),
|
||||
resources: names.to_owned(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -335,7 +343,7 @@ async fn send_release(locker: &mut LockApi, uid: &String, owner: &String, names:
|
||||
true
|
||||
}
|
||||
|
||||
fn check_quorum_locked(locks: &Vec<String>, quorum: usize) -> bool {
|
||||
fn check_quorum_locked(locks: &[String], quorum: usize) -> bool {
|
||||
let mut count = 0;
|
||||
locks.iter().for_each(|lock| {
|
||||
if is_locked(lock) {
|
||||
|
||||
@@ -18,7 +18,7 @@ pub mod namespace_lock;
|
||||
pub mod remote_client;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref GLOBAL_LOCAL_SERVER: Arc<Box<RwLock<LocalLocker>>> = Arc::new(Box::new(RwLock::new(LocalLocker::new())));
|
||||
pub static ref GLOBAL_LOCAL_SERVER: Arc<RwLock<LocalLocker>> = Arc::new(RwLock::new(LocalLocker::new()));
|
||||
}
|
||||
|
||||
type LockClient = dyn Locker;
|
||||
|
||||
@@ -75,7 +75,7 @@ impl LocalLocker {
|
||||
};
|
||||
|
||||
self.lock_map.iter().for_each(|(_, value)| {
|
||||
if value.len() > 0 {
|
||||
if !value.is_empty() {
|
||||
if value[0].writer {
|
||||
st.writes += 1;
|
||||
} else {
|
||||
@@ -84,7 +84,7 @@ impl LocalLocker {
|
||||
}
|
||||
});
|
||||
|
||||
return st;
|
||||
st
|
||||
}
|
||||
|
||||
fn dump_lock_map(&mut self) -> HashMap<String, Vec<LockRequesterInfo>> {
|
||||
@@ -93,7 +93,7 @@ impl LocalLocker {
|
||||
lock_copy.insert(key.to_string(), value.to_vec());
|
||||
});
|
||||
|
||||
return lock_copy;
|
||||
lock_copy
|
||||
}
|
||||
|
||||
fn expire_old_locks(&mut self, interval: Duration) {
|
||||
@@ -109,8 +109,6 @@ impl LocalLocker {
|
||||
true
|
||||
});
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,9 +163,9 @@ impl Locker for LocalLocker {
|
||||
for resource in args.resources.iter() {
|
||||
match self.lock_map.get_mut(resource) {
|
||||
Some(lris) => {
|
||||
if !is_write_lock(&lris) {
|
||||
if !is_write_lock(lris) {
|
||||
if err_info.is_empty() {
|
||||
err_info = String::from(format!("unlock attempted on a read locked entity: {}", resource));
|
||||
err_info = format!("unlock attempted on a read locked entity: {}", resource);
|
||||
} else {
|
||||
err_info.push_str(&format!(", {}", resource));
|
||||
}
|
||||
@@ -184,7 +182,7 @@ impl Locker for LocalLocker {
|
||||
true
|
||||
});
|
||||
}
|
||||
if lris.len() == 0 {
|
||||
if lris.is_empty() {
|
||||
self.lock_map.remove(resource);
|
||||
}
|
||||
}
|
||||
@@ -250,7 +248,7 @@ impl Locker for LocalLocker {
|
||||
let resource = &args.resources[0];
|
||||
match self.lock_map.get_mut(resource) {
|
||||
Some(lris) => {
|
||||
if is_write_lock(&lris) {
|
||||
if is_write_lock(lris) {
|
||||
return Err(Error::from_string(format!("runlock attempted on a write locked entity: {}", resource)));
|
||||
} else {
|
||||
lris.retain(|lri| {
|
||||
@@ -265,12 +263,12 @@ impl Locker for LocalLocker {
|
||||
true
|
||||
});
|
||||
}
|
||||
if lris.len() == 0 {
|
||||
if lris.is_empty() {
|
||||
self.lock_map.remove(resource);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Ok(reply || true);
|
||||
return Ok(reply);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -291,18 +289,17 @@ impl Locker for LocalLocker {
|
||||
async fn force_unlock(&mut self, args: &LockArgs) -> Result<bool> {
|
||||
let mut reply: bool;
|
||||
if args.uid.is_empty() {
|
||||
args.resources.iter().for_each(|resource| match self.lock_map.get(resource) {
|
||||
Some(lris) => {
|
||||
args.resources.iter().for_each(|resource| {
|
||||
if let Some(lris) = self.lock_map.get(resource) {
|
||||
lris.iter().for_each(|lri| {
|
||||
let mut key = lri.uid.to_string();
|
||||
format_uuid(&mut key, &lri.idx);
|
||||
self.lock_uid.remove(&key);
|
||||
});
|
||||
if lris.len() == 0 {
|
||||
if lris.is_empty() {
|
||||
self.lock_map.remove(resource);
|
||||
}
|
||||
}
|
||||
None => (),
|
||||
});
|
||||
|
||||
return Ok(true);
|
||||
@@ -330,7 +327,7 @@ impl Locker for LocalLocker {
|
||||
});
|
||||
}
|
||||
idx += 1;
|
||||
if lris.len() == 0 {
|
||||
if lris.is_empty() {
|
||||
need_remove_resource.push(resource.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
+16
-26
@@ -17,13 +17,11 @@ impl LRWMutex {
|
||||
let id = self.id.read().await.clone();
|
||||
let source = self.source.read().await.clone();
|
||||
let timeout = Duration::from_secs(10000);
|
||||
let x = self
|
||||
.look_loop(
|
||||
&id, &source, &timeout, // big enough
|
||||
is_write,
|
||||
)
|
||||
.await;
|
||||
x
|
||||
self.look_loop(
|
||||
&id, &source, &timeout, // big enough
|
||||
is_write,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_lock(&self, id: &str, source: &str, timeout: &Duration) -> bool {
|
||||
@@ -36,13 +34,11 @@ impl LRWMutex {
|
||||
let id = self.id.read().await.clone();
|
||||
let source = self.source.read().await.clone();
|
||||
let timeout = Duration::from_secs(10000);
|
||||
let x = self
|
||||
.look_loop(
|
||||
&id, &source, &timeout, // big enough
|
||||
is_write,
|
||||
)
|
||||
.await;
|
||||
x
|
||||
self.look_loop(
|
||||
&id, &source, &timeout, // big enough
|
||||
is_write,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_r_lock(&self, id: &str, source: &str, timeout: &Duration) -> bool {
|
||||
@@ -61,11 +57,9 @@ impl LRWMutex {
|
||||
*self.is_write.write().await = true;
|
||||
locked = true;
|
||||
}
|
||||
} else {
|
||||
if !*self.is_write.read().await {
|
||||
*self.refrence.write().await += 1;
|
||||
locked = true;
|
||||
}
|
||||
} else if !*self.is_write.read().await {
|
||||
*self.refrence.write().await += 1;
|
||||
locked = true;
|
||||
}
|
||||
|
||||
locked
|
||||
@@ -112,13 +106,9 @@ impl LRWMutex {
|
||||
*self.is_write.write().await = false;
|
||||
unlocked = true;
|
||||
}
|
||||
} else {
|
||||
if !*self.is_write.read().await {
|
||||
if *self.refrence.read().await > 0 {
|
||||
*self.refrence.write().await -= 1;
|
||||
unlocked = true;
|
||||
}
|
||||
}
|
||||
} else if !*self.is_write.read().await && *self.refrence.read().await > 0 {
|
||||
*self.refrence.write().await -= 1;
|
||||
unlocked = true;
|
||||
}
|
||||
|
||||
unlocked
|
||||
|
||||
@@ -45,8 +45,8 @@ impl NsLockMap {
|
||||
&mut self,
|
||||
volume: &String,
|
||||
path: &String,
|
||||
lock_source: &String,
|
||||
ops_id: &String,
|
||||
lock_source: &str,
|
||||
ops_id: &str,
|
||||
read_lock: bool,
|
||||
timeout: Duration,
|
||||
) -> bool {
|
||||
@@ -58,12 +58,11 @@ impl NsLockMap {
|
||||
});
|
||||
nslk.reference += 1;
|
||||
|
||||
let locked: bool;
|
||||
if read_lock {
|
||||
locked = nslk.lock.get_r_lock(ops_id, lock_source, &timeout).await;
|
||||
let locked = if read_lock {
|
||||
nslk.lock.get_r_lock(ops_id, lock_source, &timeout).await
|
||||
} else {
|
||||
locked = nslk.lock.get_lock(ops_id, lock_source, &timeout).await;
|
||||
}
|
||||
nslk.lock.get_lock(ops_id, lock_source, &timeout).await
|
||||
};
|
||||
|
||||
if !locked {
|
||||
nslk.reference -= 1;
|
||||
@@ -72,7 +71,7 @@ impl NsLockMap {
|
||||
}
|
||||
}
|
||||
|
||||
return locked;
|
||||
locked
|
||||
}
|
||||
|
||||
async fn un_lock(&mut self, volume: &String, path: &String, read_lock: bool) {
|
||||
@@ -90,8 +89,6 @@ impl NsLockMap {
|
||||
if nslk.reference == 0 {
|
||||
w_lock_map.remove(&resource);
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,7 +135,8 @@ impl RWLocker for DistLockInstance {
|
||||
}
|
||||
|
||||
async fn un_lock(&mut self) -> Result<()> {
|
||||
Ok(self.lock.un_lock().await)
|
||||
self.lock.un_lock().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_u_lock(&mut self, opts: &Options) -> Result<bool> {
|
||||
@@ -148,7 +146,8 @@ impl RWLocker for DistLockInstance {
|
||||
}
|
||||
|
||||
async fn un_r_lock(&mut self) -> Result<()> {
|
||||
Ok(self.lock.un_r_lock().await)
|
||||
self.lock.un_r_lock().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.lock(request).await?.into_inner();
|
||||
@@ -42,7 +42,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.un_lock(request).await?.into_inner();
|
||||
@@ -59,7 +59,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.r_lock(request).await?.into_inner();
|
||||
@@ -76,7 +76,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.r_un_lock(request).await?.into_inner();
|
||||
@@ -93,7 +93,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.force_un_lock(request).await?.into_inner();
|
||||
@@ -110,7 +110,7 @@ impl Locker for RemoteClient {
|
||||
let args = serde_json::to_string(args)?;
|
||||
let mut client = node_service_time_out_client(&self.addr)
|
||||
.await
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err.to_string())))?;
|
||||
.map_err(|err| Error::from_string(format!("can not get client, err: {}", err)))?;
|
||||
let request = Request::new(GenerallyLockRequest { args });
|
||||
|
||||
let response = client.refresh(request).await?.into_inner();
|
||||
|
||||
Reference in New Issue
Block a user