4

使用 ZooKeeper 实现的分布式锁如何避免“错误地认为自己还是锁的持有者”?

 2 years ago
source link: https://www.v2ex.com/t/855820
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

使用 ZooKeeper 实现的分布式锁如何避免“错误地认为自己还是锁的持有者”?

  JasonLaw · 14 小时 57 分钟前 · 804 次点击

Lecture 9: More Replication, CRAQ - YouTube中,教授使用 ZooKeeper 实现了分布式锁,伪代码如下:

1. create seq "f" - ephem=t
2. list f*
3. if no lower # file, return
4. if exists(next lower #, watch=t)
5.    wait
6. goto 2.

假设 A 获取到了 lock ,然后它跟 ZooKeeper 出现了 network partition ,最后会导致 f1 被删除,然后 B 就会获取到 lock 。

我的疑问是:怎么避免 A 继续认为自己还是获取到 lock 的?是在某个时间范围内没有收到 ZooKeeper 的“你还是占有这个锁”回应吗?那么这个时间范围应该是比 ZooKeeper 自动删除的时间短吧?

第 1 条附言  ·  4 小时 34 分钟前

在Stack Overflow上找到了一个一模一样的问题😂

Concerns about zookeeper's lock-recipe - Stack Overflow

13 条回复    2022-05-28 23:45:50 +08:00
bigbyto

bigbyto      11 小时 25 分钟前

zkclient 和 server 之间需要创建一个 session ,对于 Ephemeral 节点而言,它的生命周期是 session 级,即随着 session 的销毁而销毁。当 A 和 zkserver 之间出现网络问题导致 session expired ,session 就会被销毁; 如果此时 B 创建了一个新的 Ephemeral 节点,那么这个节点的拥有者(ephemeralOwner)就是 B ,A 在执行释放锁操作(delete ephemeral node)时就会报错。

相关术语
ephemeralOwner: The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.

Ephemeral Nodes: ZooKeeper also has the notion of ephemeral nodes. These znodes exists as long as the session that created the znode is active. When the session ends the znode is deleted. Because of this behavior ephemeral znodes are not allowed to have children.

Ref: https://zookeeper.apache.org/doc/r3.8.0/zookeeperProgrammers.html
cubecube

cubecube      9 小时 48 分钟前

持有的临时节点被销毁会有通知到 client
JasonLaw

JasonLaw      9 小时 46 分钟前 via iPhone

@bigbyto #1 谢谢,也就是 A 释放的时候会发现,然后做对应的动作?
JasonLaw

JasonLaw      9 小时 44 分钟前 via iPhone

@cubecube #2 关键是出现了 network partition ,通知不到 client 。
cubecube

cubecube      9 小时 20 分钟前

@JasonLaw 没心跳,锁就自动释放了呀
JasonLaw

JasonLaw      9 小时 13 分钟前 via iPhone

@cubecube #5 ZooKeeper 会自动删除 ephemeral node ,但是 A 不知道发生了这件事🤐
microxiaoxiao

microxiaoxiao      8 小时 37 分钟前

虽然没怎么玩过 zoopeeker, 但是做过一点点集群的东西。基本都是民主选举共识决,连不上集群,肯定剔出决策权了。如果是偶数节点还会出现脑裂或者都失效的情况。
JasonLaw

JasonLaw      4 小时 33 分钟前

@microxiaoxiao #7 这个问题不是关于 leader election 的。
luoqeng

luoqeng      3 小时 25 分钟前

解决不了,记得好像已经证明了没有安全的分布式锁。
分布式锁在异步系统中是一个根本不安全的概念。
分布式锁只能在正确性和活跃性二选一。

通俗来讲,如果进程在持有锁时崩溃,为了不造成死锁,会设置锁失效时间。
但是,如果进程实际上并没有死,而只是暂停或无法访问,那么锁失效会导致它被其他进程持有。暂停的进程恢复运行会以为自己还持有锁。

https://jepsen.io/analyses/etcd-3.4.3
luoqeng

luoqeng      3 小时 14 分钟前

[client 自己需要主动去检查 lock 的有效性] 这是一个异步操作,发生请求检查锁有效,到接收到锁有效返回,这个是有网络延迟的,你接收的锁有效的消息可能是过期的,锁已经超时释放掉了。
luoqeng

luoqeng      3 小时 11 分钟前

at any snapshot in time no two clients think they hold the same lock is based on the following assumptions: bounded network delay, bounded process pauses and bounded clock error.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK