sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.53k stars 1.12k forks source link

分布式锁中fencingToken需要KV在服务端check这个在实现中没看到 #182

Closed lixusign closed 5 years ago

lixusign commented 5 years ago

提供一个getFencingToken() 方法给客户端感觉没啥用啊

fengjiachun commented 5 years ago

https://github.com/sofastack/sofa-jraft/wiki/JRaft-RheaKV-%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97#distributedlock

http://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html

你好,可以看下上面两个链接

lixusign commented 5 years ago

你好,我不太理解,按照martin的说法,最终需要storage自身做token有效性check,那么岂不是无法实现了

lixusign commented 5 years ago

我已经进入到临界区以内了 这时候锁服务端过期了,我该怎么办?

lixusign commented 5 years ago

在吗?

killme2008 commented 5 years ago

token 最主要的作用是让你在临界区里做互斥,但是如果你的临界区里的资源没有有效的唯一性的校验,这个 token 对你也是没有用的。

killme2008 commented 5 years ago

那篇博文里已经提了

Note this requires the storage server to take an active role in checking tokens, 
and rejecting any writes on which the token has gone backwards.
lixusign commented 5 years ago

我的意思是说:如果我在临界区内拿到了当前的token比我之前lock给的大,就怎么怎么样。但是这有什么意义呢?我所有的check都是客户端的。

lixusign commented 5 years ago

哦 明白了 就是提供一个互斥因子,还是得自己实现具体的互斥策略,是吧 @killme2008

fengjiachun commented 5 years ago

哦 明白了 就是提供一个互斥因子,还是得自己实现具体的互斥策略,是吧 @killme2008

是的,没其他问题先关闭了

lixusign commented 5 years ago

這裡面對分佈式鎖和分佈式事務有混淆

lixusign commented 5 years ago

Note this requires the storage server to take an active role in checking tokens, and rejecting any writes on which the token has gone backwards.

checking token 這個感覺沒意義,因為客戶端已經出現因為stop the world event 跳出了臨界區,這個時刻臨界區資源除非自給可以實現cas, 否則還是存在誤操作問題,導致鎖失效,但是自己實現了cas,也沒法保證鎖裡面全部的動作都是合法的,這本質上必須有分佈式事務,否則鎖沒有任何意義(除非只是簡單的count++這樣的動作)

lixusign commented 5 years ago

http://zhangtielei.com/posts/blog-redlock-reasoning-part2.html

lixusign commented 5 years ago

比如A,B兩個進程執行如下:

1、A trylock 2、A 進入臨界區 3、A 做 curr token > pre token success 4、A 剛要做事情之前 發生 stop the world 5、A 在服務端失去了臨界區,B 這個時候更新了 curr token ++ 6、A 啥也不知道 還是做了事情 導致了錯誤。

反過來說:如果資源本身能夠甄別遞增唯一性,並且線性判斷,那麼這個 和Cas 有啥區別,如果是這樣 那我資源本身就設計成CAS了 鎖又有什麼意義?

所以分佈式鎖就不應該有租約這種機制(即使有很大風險),否則本質上沒辦法完全保證

lixusign commented 5 years ago

@killme2008 @fengjiachun 我理解的對嗎