perf(get): reuse reader paths and lock namespaces (#6015)

Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
houseme
2026-08-13 01:37:25 +08:00
committed by GitHub
parent 019e80a218
commit 59494d5089
7 changed files with 104 additions and 41 deletions
+6 -1
View File
@@ -479,7 +479,7 @@ pub struct DistributedLock {
/// Lock clients for this namespace
clients: Vec<Arc<dyn LockClient>>,
/// Namespace identifier
namespace: String,
namespace: Arc<str>,
/// Quorum size for exclusive/write operations
quorum: usize,
}
@@ -496,6 +496,11 @@ struct LockAcquireQuorumResult {
impl DistributedLock {
/// Create new distributed lock
pub fn new(namespace: String, clients: Vec<Arc<dyn LockClient>>, quorum: usize) -> Self {
Self::new_shared(namespace.into(), clients, quorum)
}
/// Create a distributed lock that shares an existing namespace allocation.
pub(crate) fn new_shared(namespace: Arc<str>, clients: Vec<Arc<dyn LockClient>>, quorum: usize) -> Self {
let q = if clients.len() <= 1 {
1
} else {
+6 -1
View File
@@ -28,12 +28,17 @@ pub struct LocalLock {
/// Global lock manager for fast local locks
manager: Arc<GlobalLockManager>,
/// Namespace identifier
namespace: String,
namespace: Arc<str>,
}
impl LocalLock {
/// Create new local lock
pub fn new(namespace: String, manager: Arc<GlobalLockManager>) -> Self {
Self::new_shared(namespace.into(), manager)
}
/// Create a local lock that shares an existing namespace allocation.
pub(crate) fn new_shared(namespace: Arc<str>, manager: Arc<GlobalLockManager>) -> Self {
Self { namespace, manager }
}
+10
View File
@@ -180,6 +180,11 @@ impl NamespaceLock {
Self::Local(LocalLock::new(namespace, manager))
}
/// Create a local namespace lock that shares an existing namespace allocation.
pub fn with_local_manager_shared(namespace: Arc<str>, manager: Arc<crate::GlobalLockManager>) -> Self {
Self::Local(LocalLock::new_shared(namespace, manager))
}
/// Create namespace lock with clients
/// Uses DistributedLock with appropriate quorum
pub fn with_clients(namespace: String, clients: Vec<Arc<dyn LockClient>>) -> Self {
@@ -195,6 +200,11 @@ impl NamespaceLock {
Self::Distributed(DistributedLock::new(namespace, clients, quorum))
}
/// Create a namespace lock that shares an existing namespace allocation.
pub fn with_clients_and_quorum_shared(namespace: Arc<str>, clients: Vec<Arc<dyn LockClient>>, quorum: usize) -> Self {
Self::Distributed(DistributedLock::new_shared(namespace, clients, quorum))
}
/// Get namespace identifier
pub fn namespace(&self) -> &str {
match self {
+10
View File
@@ -356,6 +356,16 @@ async fn test_namespace_lock_with_local_manager() {
assert_eq!(lock.namespace(), "local-ns");
}
#[tokio::test]
async fn namespace_lock_preserves_shared_namespace_storage() {
let namespace: Arc<str> = Arc::from("shared-namespace");
let namespace_ptr = Arc::as_ptr(&namespace);
let local = LocalLock::new_shared(namespace.clone(), Arc::new(GlobalLockManager::new()));
assert_eq!(local.namespace(), namespace.as_ref());
assert_eq!(local.namespace().as_ptr(), namespace_ptr.cast::<u8>());
}
#[tokio::test]
async fn test_namespace_lock_with_clients() {
let clients = vec![ClientFactory::create_local(), ClientFactory::create_local()];