关于zookeeper:zookeeper-临时节点技术细节备注

4次阅读

共计 2178 个字符,预计需要花费 6 分钟才能阅读完成。

长期节点多久会被删除

zookeeper 的长期节点顾名思义是长期的, 也就是说在一个连贯 session 有效期内长期节点是沉闷的, 当连贯断开后,session 会天然的过期,session 中过期了那么长期节点就会被删除

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 之间

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.

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 工夫内咱们还能够救活一个长期节点, 当一个连贯被意外敞开或者网络起因断开连接后.

咱们怎么救活一个长期节点?

咱们能够马上有连上了在更新下这个长期节点,

# 创立一个长期节点
[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 不一样,咱们持续测试

# 创立一个长期节点
[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

更多无关的材料请看:session

正告: 这个知识点其实没有理论价值, 算是很冷门的小细节,也千万不要把这个细节用到生产环境中作为某种业务的逻辑实现来应用

参考:

  • zookeeper 原理篇 -Zookeeper 会话机制

长期节点能够有子节点吗

不行 , 长期节点不能够有子节点

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 长期节点技术细节备注

正文完
 0