Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Etcd to implement Distributed Lock API #127

Closed
seeflood opened this issue Jul 8, 2021 · 2 comments · Fixed by #128
Closed

Use Etcd to implement Distributed Lock API #127

seeflood opened this issue Jul 8, 2021 · 2 comments · Fixed by #128
Assignees

Comments

@seeflood
Copy link
Member

seeflood commented Jul 8, 2021

What would you like to be added:
Use Etcd to implement Distributed Lock API

Why is this needed:
see #104

@stulzq
Copy link
Member

stulzq commented Jul 8, 2021

想了一下设计的几个点,目前的 api 比较简单 TryLock UnLock 还有个 expire 机制,
以下的 k 表示 ResourceId,v 表示 LockOwner
expire: 使用租约机制(不续约,如果租约到期表示超时释放锁)
TryLock: 首先创建租约,然后使用 etcd clientv3 txn,compare key revision=0, then put k/v and lease, else get key. txn commit 成功则表示抢到锁
UnLock: 同样使用 txn, compare v=req.LockOwner, then del key ,else get key, txn commit 成功表示删除锁成功,如果失败可以根据 txn responsee 来得到是什么原因 LOCK_UNEXIST or LOCK_BELONG_TO_OTHERS

@seeflood
Copy link
Member Author

seeflood commented Jul 8, 2021

想了一下设计的几个点,目前的 api 比较简单 TryLock UnLock 还有个 expire 机制,
以下的 k 表示 ResourceId,v 表示 LockOwner
expire: 使用租约机制(不续约,如果租约到期表示超时释放锁)
TryLock: 首先创建租约,然后使用 etcd clientv3 txn,compare key revision=0, then put k/v and lease, else get key. txn commit 成功则表示抢到锁
UnLock: 同样使用 txn, compare v=req.LockOwner, then del key ,else get key, txn commit 成功表示删除锁成功,如果失败可以根据 txn responsee 来得到是什么原因 LOCK_UNEXIST or LOCK_BELONG_TO_OTHERS

感谢您对Layotto的支持,已assign!
etcd支持服务端到时间自动删除节点,这点比zookeeper方便多了(zookeeper的ttl node特性默认是关闭的,我和@ZLBer 同学纠结了好长时间) :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants