您现在的位置是:热点 >>正文
ZooKeeper 分布式锁 Curator 源码之三:可重入锁并发加锁
热点633700人已围观
简介...
前言
在了解了加锁和锁重入之后,最需要了解的还是在分布式场景下或者多线程并发加锁是如何处理的?
1并发加锁
先来看结果,在多线程对 /loZocks/lock_01 加锁时,是在后面又创建了新的临时节点。
这块在加锁方法 CreateBuilderImpl#pathInForegrounoKd 中已经介绍过
这里判断 /locks/lock_01 路径已经存在,会直接创建新的临时顺序节点。
真正判断锁是否获取成功,其实是在 LoceekInternals#attemptLock 方法中的 internalLockLoop 方法中。
加锁结果及监听
internalLockLoop 方pe法的主要作用是判断加锁结果,以及获取锁失败时,对其他节点的监听。
获取父节点 /locks/lock_01 下的所有子节点,按照从小到大排序r ,判断自己是不是获取到锁,没有获取到就监听自己前一个节点; 支持设置超时时间,超时直接返回失败; 不支持设置超时时间或者还没有超时,则分布直接 wait 等待。
是否获取锁的代码在 StandardLockInternalsDriver#getsTheLock
这块就是判断是否为式锁最小节点,因为在 getSortedChildren 中已经对所有节点排序,所以方法中的 List children 是有序的。
maxLeases C 是在 InterProcessMutex 初始化的时候,指定的值为 1。
最终这里的结果是,判断自己是不是最小,不是最小,就将 pathToWaurtch 设置为前一个节点。
只监听自己的前一个节点,可以避免羊群效应!
为什么要进行等待呢?
因为是为了防止无效自旋,因为这里有监听机制,会监at听上一个节点是否释放。
这块是 ZooKeeper 的 Watcher 监听机制,在节点释放的时候,会进行回调,然后使用 Java 的 notifyAlorl 方法通知所有的 wait 线程。然后这里的 while trye 会继续执行,重新检查是否获得锁等。
2总结
本文主要介绍了基于 ZooKeeper 的分布 源式锁框架 Curator 在并发场景下的锁竞争问题。
重点需要了解的是:
为了避免羊群效应,临时顺序节点,加锁失败后监听的是前一个节点; 为了避免无码之效自旋,这里使用了 Java 的 wait/notifyAll 机制; 可以看出,默认加锁就是公平锁。
Tags:
相关文章
解密机器学习:程序是如何“创造”自己的?
热点本文转载自公众号“读芯术”(ID:AI_Discovery) 现如今,人工智能和机器学习正成为一大热潮。每天都有成百上千的人用机器学习做各种事情:如线上象棋比赛、YouTube视频推荐等。但是机器学习...
【热点】
阅读更多SUSE云安全行业趋势报告亚太版:中国市场表现尤为积极
热点10月30日消息,全球开源解决方案领导者SUSE首次发布了《云安全行业趋势报告》亚太版 (以下简称“报告”),深入探讨了生成式人工智能(AI)时代下,亚太地区在云安全领域面临的挑战及生成式AI与边缘计...
【热点】
阅读更多莲藕排骨汤怎么做
热点怎么煮排骨莲藕汤-九州醉餐饮网把排骨和莲藕切好备用,先放入排骨再放入莲藕,之后小火慢煮即可。在做排骨莲藕汤之前,是需要把排骨进行焯水备用的。在焯水的过程中,要在排骨里边加。莲藕排骨汤怎么做-九州醉餐饮...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://zhidao.baidu.com/index/?action=QUERY&word=歌外推【tg:seogoogle987】,QFr
- https://blvdseafood.com/?s=歌外推【tg:seogoogle987】K6T
- https://www.youm7.com/Home/Search?allwords=歌外推【tg:seogoogle987】,aGe
- https://www.ussalabama.com/?action=CHECK&s=歌外推【tg:seogoogle987】,o3r
- https://www.lneg.pt/?cat=140&s=歌外推【tg:seogoogle987】,YBE
- https://www.oikos-tecnics.com/?s=歌外推【tg:seogoogle987】,nCp
- https://www.qidulp.com/search?query=歌外推【tg:seogoogle987】,IAL
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=歌外推【tg:seogoogle987】,fmv
- https://www.tumblr.com/tagged/歌外推【tg:seogoogle987】,551
- https://www.olx.ba/pretraga?trazilica=歌外推【tg:seogoogle987】,onH
- https://xehay.vn/tim-kiem/=歌外推【tg:seogoogle987】,HS9
- https://www2.ual.es/ProyectoLates/?s=歌外推【tg:seogoogle987】,Qcx
- https://www.swanagerailway.co.uk/search?q=歌外推【tg:seogoogle987】,VY8
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=歌外推【tg:seogoogle987】,lOL
- https://www.kingston.gov.uk/site-search/results/?q=歌外推【tg:seogoogle987】,bUX
- https://www.manga-news.com/index.php/recherche/?q=歌外推【tg:seogoogle987】,jug
- https://www.varian.com/en-au/search?cat=113&search=歌外推【tg:seogoogle987】,AJa
- https://www.zisha.com/shop/list.shtml?wap_keyword=歌外推【tg:seogoogle987】,Pdf
- https://www.lezhin.com/ko/tags/歌外推【tg:seogoogle987】,pJr
- https://www.pearson.com/en-us/search.html?aq=歌外推【tg:seogoogle987】,myy
- https://www.jusoen.com/addreng.asp?p1=歌外推【tg:seogoogle987】THF
- https://www.ons.gov.uk/search?q=歌外推【tg:seogoogle987】,0Dt
- https://www.ucycle.com/search/=歌外推【tg:seogoogle987】,nod
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=歌外推【tg:seogoogle987】,lRd
- https://www.unclefossil.com/search/歌外推【tg:seogoogle987】,z3Y
- https://www.openspaceinstitute.org/search?q=歌外推【tg:seogoogle987】,FqW
- https://www.tombow.com/search/歌外推【tg:seogoogle987】,k1L
- https://www.u-bourgogne.fr/?cat=117&s=歌外推【tg:seogoogle987】,k74
- https://www.pearlstonecenter.org/search/歌外推【tg:seogoogle987】,sPz
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=歌外推【tg:seogoogle987】,oaj
- https://www.supersauna.de/catalogsearch/result/?q=歌外推【tg:seogoogle987】,m7n
- https://blvdseafood.com/?s=歌外推【tg:seogoogle987】z6Z
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=歌外推【tg:seogoogle987】,6vB
- https://www.normani.de/search?q=歌外推【tg:seogoogle987】,iKa
- https://www.routesonline.com/search/?query=歌外推【tg:seogoogle987】,EDr
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=歌外推【tg:seogoogle987】,iDw
- https://www.wuv.edu/?s=歌外推【tg:seogoogle987】,ZkB
- https://www.xvideos.com/tags/歌外推【tg:seogoogle987】,mY3
- https://www.uctronics.com/catalogsearch/result/?q=歌外推【tg:seogoogle987】,8eH
- https://www.polar-tech.com/?action=QUERY&s=歌外推【tg:seogoogle987】,AUR
- https://www.temu.com/search_result.html?search_key=歌外推【tg:seogoogle987】Eca
- https://www.lyrics.com/lyrics/kkk3歌外推【tg:seogoogle987】,Vyr
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=歌外推【tg:seogoogle987】,jEY
- https://www.urmc.rochester.edu/news/search?q=歌外推【tg:seogoogle987】,2o4
- https://store.line.me/search/sticker/ja?q=歌外推【tg:seogoogle987】5Uv
- https://www.ncetm.org.uk/search?action=CHECK&q=歌外推【tg:seogoogle987】,suB
- https://www.maafushidive.com?collections?vendors?q=歌外推【tg:seogoogle987】,nQt
- https://www.solerpalau.com/en-en/catalogsearch/result/?q=歌外推【tg:seogoogle987】,0HS
- https://www.madebyoranges.com/nl/search/歌外推【tg:seogoogle987】,aCa
- https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=歌外推【tg:seogoogle987】,DGf