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 Consul to implement Distributed Lock API #129

Closed
seeflood opened this issue Jul 9, 2021 · 10 comments · Fixed by #140
Closed

Use Consul to implement Distributed Lock API #129

seeflood opened this issue Jul 9, 2021 · 10 comments · Fixed by #140
Assignees
Labels

Comments

@seeflood
Copy link
Member

seeflood commented Jul 9, 2021

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

Why is this needed:
see #104

@seeflood
Copy link
Member Author

seeflood commented Jul 9, 2021

@ZLBer hi,please reply here and then i can assign it to you

@ZLBer
Copy link
Member

ZLBer commented Jul 9, 2021

@seeflood
fine, i will try .

@ZLBer
Copy link
Member

ZLBer commented Jul 15, 2021

@seeflood
关于consul实现的探讨:

  1. consul api封装了lock 接口, 可以实现阻塞锁,对session不断renew续约达到持续持有锁的目的,功能比较丰富,可以设置重试次数,获取锁等待时间等。但sidecar情形下,client断开,lock在sidecar端还会不断进性renew,导致锁不能释放。
  2. 抛弃lock实现,利用session+acquire+release实现(consul lock内部也是如此), 每次trylock 都新建session,令session ttl=expire time, 不对session进性renew,只让其存活ttl时间,保证锁的正确性。

@ZLBer
Copy link
Member

ZLBer commented Jul 16, 2021

@seeflood
遇到了点问题:
1.acquire和release操作要求必须是同一个session,那就没法每次lock都新建一个session了。
2. 想着直接用put 操作,但consul的put会直接更新kv, 没有类似setnx的操作。

@seeflood
Copy link
Member Author

seeflood commented Jul 17, 2021

@seeflood
关于consul实现的探讨:

  1. consul api封装了lock 接口, 可以实现阻塞锁,对session不断renew续约达到持续持有锁的目的,功能比较丰富,可以设置重试次数,获取锁等待时间等。但sidecar情形下,client断开,lock在sidecar端还会不断进性renew,导致锁不能释放。

"client断开,lock在sidecar端还会不断进性renew,导致锁不能释放。"
这个死锁问题还好,目前的API是tryLock传expire,其实sidecar起个新协程、expire到期后关闭session即可,不至于出现死锁。
但是这个方案和zk面临一样的问题:如果用户申请的是一把“粗粒度锁”,expire传的比较大,相比之下consul的session ttl很短,那可能sidecar心跳很快就断掉、丢锁,但是app还以为自己持有锁

方案2对粗粒度锁稍微好一些(前提是调研清楚,consul对session ttl传入的值有没有限制,会不会限制不让传太大)

@seeflood
Copy link
Member Author

seeflood commented Jul 17, 2021

@seeflood
遇到了点问题:
1.acquire和release操作要求必须是同一个session,那就没法每次lock都新建一个session了。

这个在内存里维护一个session池即可(即维护一个map)
抢锁成功后往map里放session.key是resource_id,value是session和owner_id等信息。
需要注意并发控制,需要注意session过期后能自动删除map里的session,免得内存泄漏

  1. 想着直接用put 操作,但consul的put会直接更新kv, 没有类似setnx的操作。

这个要查一下了 我搜下 你也可以先查下文档

@seeflood
Copy link
Member Author

seeflood commented Jul 17, 2021

@ZLBer 我查了下consul有check-and-set和Transactions功能,看下这个能用不
https://alesnosek.com/blog/2017/07/25/check-and-set-operation-and-transactions-in-consul/

@ZLBer
Copy link
Member

ZLBer commented Jul 17, 2021

@seeflood
consul的session ttl到期不会自动删除key,所以CAS这个貌似也会存在问题,client崩溃没法删除节点。session ttl 到期只会使lock失效,那还得借助consul的lock机制(acquire和release)。

@ZLBer ZLBer mentioned this issue Jul 17, 2021
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity in the last 30 days. It will be closed in the next 7 days unless it is tagged (pinned, good first issue or help wanted) or other activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Oct 24, 2021
@github-actions
Copy link

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as pinned, good first issue or help wanted. Thank you for your contributions.

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

Successfully merging a pull request may close this issue.

2 participants