Distributed Lock
Introduction
Metasrv
provides the functionality of distributed locks via gRPC
.
How to use
The meta-client crate provides a Rust-based meta client implementation.
For example:
async fn do_some_work(meta_client: MetaClient) {
let name = b"lock_name".to_vec();
let expire_secs = 60;
let lock_req = LockRequest { name, expire_secs };
let lock_result = meta_client.lock(lock_req).await.unwrap();
let key = lock_result.key;
info!("do some work, take 3 seconds");
tokio::time::sleep(Duration::from_secs(3)).await;
let unlock_req = UnlockRequest { key };
meta_client.unlock(unlock_req).await.unwrap();
info!("unlock success!");
}
More examples here.
Because gRPC works across languages, it is easy to implement clients for other programming languages.
You can find the protocol buffer definition here.
Pay attention to the following points:
- The distributed lock will be automatically released if it exceeds its expiration time and is still being held.
- When using distributed locks, you must set a suitable expiration time, default: 10 seconds.
- Distributed lock’s expiration time should be shorter than the ‘gRPC’ channel’s timeout; otherwise, it may cause a ‘gRPC’ timeout error, resulting in lock failure.
Detailed design
The current implementation of distributed lock is based on etcd. We have defined the Lock
trait, and EtcdLock
is one of the available implementations.
Since etcd
maintains the state of the distributed lock, both the Metasrv
leader and its follower nodes can provide the Lock’s gRPC
service.
当前内容版权归 GreptimeDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 GreptimeDB .