zookeeper 臨時節點技術細節備註

臨時節點多久會被刪除

zookeeper的臨時節點顧名思義是臨時的,也就是說在一個鏈接session有效期內臨時節點是活躍的,當鏈接斷開後,session會天然的過時,session中過時了那麼臨時節點就會被刪除html

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.

那麼session的時間默認是多少呢,答案是2xtickTime到20xtickTime之間node

Ticks When using multi-server ZooKeeper, servers use ticks to define timing of events such as status uploads, session timeouts, connection timeouts between peers, etc. The tick time is only indirectly exposed through the minimum session timeout (2 times the tick time); if a client requests a session timeout less than the minimum session timeout, the server will tell the client that the session timeout is actually the minimum session timeout.

the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.apache

One of the parameters to the ZooKeeper client library call to create a ZooKeeper session is the session timeout in milliseconds. The client sends a requested timeout, the server responds with the timeout that it can give the client. The current implementation requires that the timeout be a minimum of 2 times the tickTime (as set in the server configuration) and a maximum of 20 times the tickTime. The ZooKeeper client API allows access to the negotiated timeout.網絡

因此不是鏈接斷開就會致使臨時節點立刻被刪除,還須要等待一點點的時間,這意味這session時間內咱們還能夠救活一個臨時節點,當一個鏈接被意外關閉或者網絡緣由斷開鏈接後.session

咱們怎麼救活一個臨時節點?less

咱們能夠立刻有連上了在更新下這個臨時節點,測試

# 建立一個臨時節點
[zk: localhost:2181(CONNECTED) 4] create -e /e1 thinktik
Created /e1
# 斷開鏈接而後從新鏈接,這時查詢這個節點仍是能夠的
[zk: localhost:2181(CONNECTED) 5] get /e1
thinktik
# 過一段時間後,這個節點會消失
[zk: localhost:2181(CONNECTED) 6] 2021-02-15 22:36:26,256 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0

從新鏈接並不能救活臨時節點,由於2次鏈接的session不同,咱們繼續測試ui

# 建立一個臨時節點
[zk: localhost:2181(CONNECTED) 4] create -e /e1 thinktik
Created /e1
# 斷開鏈接而後從新鏈接,這時查詢這個節點仍是能夠的
[zk: localhost:2181(CONNECTED) 0] get /e1
thinktik
# 從新更新下這個臨時節點,就好了
[zk: localhost:2181(CONNECTED) 1] set /e1 think

更多有關的資料請看:sessionthis

警告: 這個知識點其實沒有實際價值,算是很冷門的小細節,也千萬不要把這個細節用到生產環境中做爲某種業務的邏輯實現來使用code

參考:

臨時節點能夠有子節點嗎

不行,臨時節點不能夠有子節點

Because of this behavior ephemeral znodes are not allowed to have children.

咱們能夠這樣驗證

[zk: localhost:2181(CONNECTED) 17] create -e /e1 thinktik
Created /e1
# 報錯,強調不能給臨時節點建立子節點
[zk: localhost:2181(CONNECTED) 18] create -e /e1/se1
Ephemerals cannot have children: /e1/se1

本文原創連接: zookeeper 臨時節點技術細節備註

相關文章
相關標籤/搜索