drop common/error

This commit is contained in:
weisd
2025-06-06 16:34:59 +08:00
parent c589972fa7
commit f199718e3b
12 changed files with 81 additions and 72 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
pub mod error;
// pub mod error;
pub mod globals;
pub mod last_minute;
+17 -14
View File
@@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
use tokio::{sync::mpsc::Sender, time::sleep};
use tracing::{info, warn};
use crate::{lock_args::LockArgs, LockApi, Locker};
use crate::{LockApi, Locker, lock_args::LockArgs};
const DRW_MUTEX_REFRESH_INTERVAL: Duration = Duration::from_secs(10);
const LOCK_RETRY_MIN_INTERVAL: Duration = Duration::from_millis(250);
@@ -117,7 +117,10 @@ impl DRWMutex {
quorum += 1;
}
}
info!("lockBlocking {}/{} for {:?}: lockType readLock({}), additional opts: {:?}, quorum: {}, tolerance: {}, lockClients: {}\n", id, source, self.names, is_read_lock, opts, quorum, tolerance, locker_len);
info!(
"lockBlocking {}/{} for {:?}: lockType readLock({}), additional opts: {:?}, quorum: {}, tolerance: {}, lockClients: {}\n",
id, source, self.names, is_read_lock, opts, quorum, tolerance, locker_len
);
// Recalculate tolerance after potential quorum adjustment
// Use saturating_sub to prevent underflow
@@ -376,8 +379,8 @@ mod tests {
use super::*;
use crate::local_locker::LocalLocker;
use async_trait::async_trait;
use common::error::{Error, Result};
use std::collections::HashMap;
use std::io::{Error, Result};
use std::sync::{Arc, Mutex};
// Mock locker for testing
@@ -436,10 +439,10 @@ mod tests {
async fn lock(&mut self, args: &LockArgs) -> Result<bool> {
let mut state = self.state.lock().unwrap();
if state.should_fail {
return Err(Error::from_string("Mock lock failure"));
return Err(Error::other("Mock lock failure"));
}
if !state.is_online {
return Err(Error::from_string("Mock locker offline"));
return Err(Error::other("Mock locker offline"));
}
// Check if already locked
@@ -454,7 +457,7 @@ mod tests {
async fn unlock(&mut self, args: &LockArgs) -> Result<bool> {
let mut state = self.state.lock().unwrap();
if state.should_fail {
return Err(Error::from_string("Mock unlock failure"));
return Err(Error::other("Mock unlock failure"));
}
Ok(state.locks.remove(&args.uid).is_some())
@@ -463,10 +466,10 @@ mod tests {
async fn rlock(&mut self, args: &LockArgs) -> Result<bool> {
let mut state = self.state.lock().unwrap();
if state.should_fail {
return Err(Error::from_string("Mock rlock failure"));
return Err(Error::other("Mock rlock failure"));
}
if !state.is_online {
return Err(Error::from_string("Mock locker offline"));
return Err(Error::other("Mock locker offline"));
}
// Check if write lock exists
@@ -481,7 +484,7 @@ mod tests {
async fn runlock(&mut self, args: &LockArgs) -> Result<bool> {
let mut state = self.state.lock().unwrap();
if state.should_fail {
return Err(Error::from_string("Mock runlock failure"));
return Err(Error::other("Mock runlock failure"));
}
Ok(state.read_locks.remove(&args.uid).is_some())
@@ -490,7 +493,7 @@ mod tests {
async fn refresh(&mut self, _args: &LockArgs) -> Result<bool> {
let state = self.state.lock().unwrap();
if state.should_fail {
return Err(Error::from_string("Mock refresh failure"));
return Err(Error::other("Mock refresh failure"));
}
Ok(true)
}
@@ -880,8 +883,8 @@ mod tests {
// Case 1: Even number of lockers
let locks = vec!["uid1".to_string(), "uid2".to_string(), "uid3".to_string(), "uid4".to_string()];
let tolerance = 2; // locks.len() / 2 = 4 / 2 = 2
// locks.len() - tolerance = 4 - 2 = 2, which equals tolerance
// So the special case applies: un_locks_failed >= tolerance
// locks.len() - tolerance = 4 - 2 = 2, which equals tolerance
// So the special case applies: un_locks_failed >= tolerance
// All 4 failed unlocks
assert!(check_failed_unlocks(&locks, tolerance)); // 4 >= 2 = true
@@ -897,8 +900,8 @@ mod tests {
// Case 2: Odd number of lockers
let locks = vec!["uid1".to_string(), "uid2".to_string(), "uid3".to_string()];
let tolerance = 1; // locks.len() / 2 = 3 / 2 = 1
// locks.len() - tolerance = 3 - 1 = 2, which does NOT equal tolerance (1)
// So the normal case applies: un_locks_failed > tolerance
// locks.len() - tolerance = 3 - 1 = 2, which does NOT equal tolerance (1)
// So the normal case applies: un_locks_failed > tolerance
// 3 failed unlocks
assert!(check_failed_unlocks(&locks, tolerance)); // 3 > 1 = true
+1 -1
View File
@@ -3,11 +3,11 @@
use std::sync::Arc;
use async_trait::async_trait;
use common::error::Result;
use lazy_static::lazy_static;
use local_locker::LocalLocker;
use lock_args::LockArgs;
use remote_client::RemoteClient;
use std::io::Result;
use tokio::sync::RwLock;
pub mod drwmutex;
+9 -9
View File
@@ -1,11 +1,11 @@
use async_trait::async_trait;
use common::error::{Error, Result};
use std::io::{Error, Result};
use std::{
collections::HashMap,
time::{Duration, Instant},
};
use crate::{lock_args::LockArgs, Locker};
use crate::{Locker, lock_args::LockArgs};
const MAX_DELETE_LIST: usize = 1000;
@@ -116,7 +116,7 @@ impl LocalLocker {
impl Locker for LocalLocker {
async fn lock(&mut self, args: &LockArgs) -> Result<bool> {
if args.resources.len() > MAX_DELETE_LIST {
return Err(Error::from_string(format!(
return Err(Error::other(format!(
"internal error: LocalLocker.lock called with more than {} resources",
MAX_DELETE_LIST
)));
@@ -152,7 +152,7 @@ impl Locker for LocalLocker {
async fn unlock(&mut self, args: &LockArgs) -> Result<bool> {
if args.resources.len() > MAX_DELETE_LIST {
return Err(Error::from_string(format!(
return Err(Error::other(format!(
"internal error: LocalLocker.unlock called with more than {} resources",
MAX_DELETE_LIST
)));
@@ -197,7 +197,7 @@ impl Locker for LocalLocker {
async fn rlock(&mut self, args: &LockArgs) -> Result<bool> {
if args.resources.len() != 1 {
return Err(Error::from_string("internal error: localLocker.RLock called with more than one resource"));
return Err(Error::other("internal error: localLocker.RLock called with more than one resource"));
}
let resource = &args.resources[0];
@@ -241,7 +241,7 @@ impl Locker for LocalLocker {
async fn runlock(&mut self, args: &LockArgs) -> Result<bool> {
if args.resources.len() != 1 {
return Err(Error::from_string("internal error: localLocker.RLock called with more than one resource"));
return Err(Error::other("internal error: localLocker.RLock called with more than one resource"));
}
let mut reply = false;
@@ -249,7 +249,7 @@ impl Locker for LocalLocker {
match self.lock_map.get_mut(resource) {
Some(lris) => {
if is_write_lock(lris) {
return Err(Error::from_string(format!("runlock attempted on a write locked entity: {}", resource)));
return Err(Error::other(format!("runlock attempted on a write locked entity: {}", resource)));
} else {
lris.retain(|lri| {
if lri.uid == args.uid && (args.owner.is_empty() || lri.owner == args.owner) {
@@ -389,8 +389,8 @@ fn format_uuid(s: &mut String, idx: &usize) {
#[cfg(test)]
mod test {
use super::LocalLocker;
use crate::{lock_args::LockArgs, Locker};
use common::error::Result;
use crate::{Locker, lock_args::LockArgs};
use std::io::Result;
use tokio;
#[tokio::test]
+1 -1
View File
@@ -125,7 +125,7 @@ impl LRWMutex {
mod test {
use std::{sync::Arc, time::Duration};
use common::error::Result;
use std::io::Result;
use tokio::time::sleep;
use crate::lrwmutex::LRWMutex;
+4 -4
View File
@@ -5,11 +5,11 @@ use tokio::sync::RwLock;
use uuid::Uuid;
use crate::{
LockApi,
drwmutex::{DRWMutex, Options},
lrwmutex::LRWMutex,
LockApi,
};
use common::error::Result;
use std::io::Result;
pub type RWLockerImpl = Box<dyn RWLocker + Send + Sync>;
@@ -258,12 +258,12 @@ impl RWLocker for LocalLockInstance {
mod test {
use std::{sync::Arc, time::Duration};
use common::error::Result;
use std::io::Result;
use tokio::sync::RwLock;
use crate::{
drwmutex::Options,
namespace_lock::{new_nslock, NsLockMap},
namespace_lock::{NsLockMap, new_nslock},
};
#[tokio::test]
+20 -20
View File
@@ -1,10 +1,10 @@
use async_trait::async_trait;
use common::error::{Error, Result};
use protos::{node_service_time_out_client, proto_gen::node_service::GenerallyLockRequest};
use std::io::{Error, Result};
use tonic::Request;
use tracing::info;
use crate::{lock_args::LockArgs, Locker};
use crate::{Locker, lock_args::LockArgs};
#[derive(Debug, Clone)]
pub struct RemoteClient {
@@ -25,13 +25,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.lock(request).await?.into_inner();
let response = client.lock(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)
@@ -42,13 +42,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.un_lock(request).await?.into_inner();
let response = client.un_lock(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)
@@ -59,13 +59,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.r_lock(request).await?.into_inner();
let response = client.r_lock(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)
@@ -76,13 +76,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.r_un_lock(request).await?.into_inner();
let response = client.r_un_lock(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)
@@ -93,13 +93,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.refresh(request).await?.into_inner();
let response = client.refresh(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)
@@ -110,13 +110,13 @@ 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)))?;
.map_err(|err| Error::other(format!("can not get client, err: {}", err)))?;
let request = Request::new(GenerallyLockRequest { args });
let response = client.force_un_lock(request).await?.into_inner();
let response = client.force_un_lock(request).await.map_err(Error::other)?.into_inner();
if let Some(error_info) = response.error_info {
return Err(Error::from_string(error_info));
return Err(Error::other(error_info));
}
Ok(response.success)