`
bit1129
  • 浏览: 1070019 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[Zookeeper学习笔记之五]Zookeeper连接丢失和会话超时

阅读更多

Zookeeper的会话状态变迁图:


 

Connection Loss:
CONNECTION_LOSS意味着客户端和服务器端的连接断开,比如,客户端创建一个Zookeeper实例,开始客户端和服务端的会话,然后进行一系列的操作。如果客户端挂了,网络出现异常或者服务器端挂了,都会导致
客户端和服务器端的连接断开。连接断开时,如果客户端进程正常工作,它将收到一个Disconnected事件,收到此事件,客户端不能假设是服务器端挂了还是网络出现问题,同样,服务器如果仍然正常工作,也不能假设是客户端挂了还是出现了网络问题。

 

在Connection Loss的情况下,客户端不能假设它之前发出的请求是否已经成功执行,例如客户端发起一个创建一个znode的操作,这个请求的处理可能存在如下几种情况

1. 请求发送到服务器端,服务端执行完,返回的过程中,连接断开

2. 请求尚未发送到服务器端,因此请求压根没有执行

3. 请求发送到服务器端,请求在执行过程中,服务器端挂了

 

第三种情况是一种极限的情况,对于一致性要求很高的场景,这个请求已经执行的部分操作应该全部失败,服务器端的状态应该是请求未执行前的状态,Zookeeper的读写操作都是原子操作,因此可以保证不会部分读取和部分写入的情况,这就保证了数据一致性。

 

客户端与服务器断开链接后,客户端不能确定是网络链接问题还是Zookeeper服务器挂了,因此,客户端在受到COLLECTION_LOSS事件后,

1.客户端不需要重新创建一个Zookeeper会话,客户端在Zookeeper Client Library的帮助下会持续处于CONNECTING状态,不会出现会话超时的情况(虽然会话超时时间在客户端创建Zookeeper时指定,但是Zookeeper Client Libarary不会检测会话超时)

2.客户端需要检测上次上次操作的执行情况,比如通过检查znode是否存在以判断znode是否创建成功,检查znode的数据以判断znode是否更新成功

3.在1中提到,在服务器不可用的情况,客户端在Zookeeper Client Library的帮助下会持续处于CONNECTING状态,当Zookeeper服务器恢复可用的情况下,Zookeeper尝试于Zookeeper服务器恢复链接,加入在session超时之前,恢复链接,那么对于客户端来说,会话恢复,包括已经注册的watcher,客户端会受到一个SyncConnection事件;如果超时,那么客户端会收到一个Session Expired事件。

 

假如session的超时时间是10s,而session持续3s的时候链接断开,那么当链接恢复时,在7s内完成就会恢复会话,如果超过7s,那么客户端会收到session expired事件(这个计算方式是否正确??)

 

2. How should I handle the CONNECTION_LOSS error?

CONNECTION_LOSS means the link between the client and server was broken. It doesn't necessarily mean that the request failed. If you are doing a create request and the link was broken after the request reached the server and before the response was returned, the create request will succeed. If the link was broken before the packet went onto the wire, the create request failed. Unfortunately, there is no way for the client library to know, so it returns CONNECTION_LOSS. The programmer must figure out if the request succeeded or needs to be retried. Usually this is done in an application specific way. Examples of success detection include checking for the presence of a file to be created or checking the value of a znode to be modified.

When a client (session) becomes partitioned from the ZK serving cluster it will begin searching the list of servers that were specified during session creation. Eventually, when connectivity between the client and at least one of the servers is re-established, the session will either again transition to the "connected" state (if reconnected within the session timeout value) or it will transition to the "expired" state (if reconnected after the session timeout). The ZK client library will handle reconnect for you automatically. In particular we have heuristics built into the client library to handle things like "herd effect", etc... Only create a new session when you are notified of session expiration (mandatory).
 

Session Expired

客户端收到会话超时的事件后,表明这个Zookeeper对象已经不可再使用,需要重新初始化一个

客户端长时间不做增删改查znode操作,客户端并没有收到会话超时,原因是客户端会定时的向Zookeeper服务器端发送心跳包,以保持会话有效

客户端链接到一个Zookeeper集群中,如果它链接的server挂了,Zookeeper Client Library会自动将它与其它server链接,只要会话还没有超时,那么session会保持到原来的状态,包括已经在该session上注册的watcher

 

 

 

 How should I handle SESSION_EXPIRED?

SESSION_EXPIRED automatically closes the ZooKeeper handle. In a correctly operating cluster, you should never see SESSION_EXPIRED. It means that the client was partitioned off from the ZooKeeper service for more the the session timeout and ZooKeeper decided that the client died. Because the ZooKeeper service is ground truth, the client should consider itself dead and go into recovery. If the client is only reading state from ZooKeeper, recovery means just reconnecting. In more complex applications, recovery means recreating ephemeral nodes, vying for leadership roles, and reconstructing published state.

Library writers should be conscious of the severity of the expired state and not try to recover from it. Instead libraries should return a fatal error. Even if the library is simply reading from ZooKeeper, the user of the library may also be doing other things with ZooKeeper that requires more complex recovery.

Session expiration is managed by the ZooKeeper cluster itself, not by the client. When the ZK client establishes a session with the cluster it provides a "timeout" value. This value is used by the cluster to determine when the client's session expires. Expirations happens when the cluster does not hear from the client within the specified session timeout period (i.e. no heartbeat). At session expiration the cluster will delete any/all ephemeral nodes owned by that session and immediately notify any/all connected clients of the change (anyone watching those znodes). At this point the client of the expired session is still disconnected from the cluster, it will not be notified of the session expiration until/unless it is able to re-establish a connection to the cluster. The client will stay in disconnected state until the TCP connection is re-established with the cluster, at which point the watcher of the expired session will receive the "session expired" notification.

Example state transitions for an expired session as seen by the expired session's watcher:

  1. 'connected' : session is established and client is communicating with cluster (client/server communication is operating properly)
  2. .... client is partitioned from the cluster
  3. 'disconnected' : client has lost connectivity with the cluster
  4. .... time elapses, after 'timeout' period the cluster expires the session, nothing is seen by client as it is disconnected from cluster
  5. .... time elapses, the client regains network level connectivity with the cluster
  6. 'expired' : eventually the client reconnects to the cluster, it is then notified of the expiration

 

 参考

1. Zookeeper FAQ

http://wiki.apache.org/hadoop/ZooKeeper/FAQ#1

2. Zookeeper关于超时和链接断开的邮件组讨论

http://markmail.org/message/p5j7rvy5zf5qjsje#query:+page:1+mid:s4d7dnsxulv5yieh+state:results

3.关于Session Timeout和Connection Lost的Blog

http://www.ngdata.com/so-you-want-to-be-a-zookeeper/

 

 

 

 

  • 大小: 49.1 KB
分享到:
评论

相关推荐

    ZooKeeper会话超时以及重连机制.pdf

    通过上述分析可以看出,ZooKeeper针对会话超时和重连机制的设计非常完善,能够有效保证分布式系统的稳定性和高可用性。无论是会话超时的检测与处理,还是客户端的重连流程,都是经过精心设计和优化的。这些机制使得...

    zookeeper学习笔记

    ### Zookeeper 学习笔记 #### 一、Zookeeper 简介与安装配置 **Zookeeper** 是一个分布式协调服务框架,它提供了一种高效可靠的机制来维护集群中的配置信息、命名服务以及提供分布式锁等功能,使得开发人员能够...

    2021尚硅谷技术之Zookeeper笔记

    当会话超时或服务器断开连接时,会话失效。Watches是ZooKeeper的重要特性,允许客户端订阅ZNode的变更事件。当ZNode的状态改变时,所有注册的Watches都会被触发,发送一个单次的通知到客户端。 **5. 集群搭建** ...

    Zookeeper学习笔记.pdf

    总的来说,Zookeeper通过其独特的数据模型、原子操作、节点类型、顺序节点和Watch机制,为分布式环境提供了高效、可靠的协调服务,广泛应用于分布式应用的各个层面,确保了系统的稳定性和一致性。

    zookeeper客户端连接工具: zktools

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着集群中...通过学习和熟练掌握zktools的使用,可以更有效地管理和优化基于ZooKeeper的分布式系统,提升整体的系统稳定性和效率。

    Zookeeper学习笔记

    【Zookeeper学习笔记】 Zookeeper是一个分布式协调服务,它的核心目标是简化分布式环境下的数据管理与系统协调。作为Apache Hadoop和HBase的重要组件,Zookeeper提供了简单的原语集,支持分布式应用程序之间的通信...

    Zookeeper学习笔记.docx

    ZooKeeper 学习笔记 ZooKeeper 是一个开源的分布式协调服务,由 Hadoop 的创始人 Doug Cutting 等人开发。它为分布式应用提供了一个高效、可靠的协调机制,帮助开发者快速构建高可用、可扩展的分布式系统。 ...

    zookeeper连接工具

    - **会话(Session)**: 客户端与Zookeeper服务器之间的连接,会话期间客户端可以接收服务器的通知和保持状态。 - **watcher事件**: 客户端注册在特定Znode上的监听器,当Znode有变化时,Zookeeper会触发事件通知...

    ZooKeeper学习笔记

    - **会话管理**:客户端与ZooKeeper服务器之间的连接称为会话。每次客户端连接到一个新的服务器时,都会创建一个新的会话。会话的生命周期决定了客户端能够访问ZooKeeper服务的时间范围。 #### 集群架构 - **选举...

    zookeeper客户端会话频繁超时解决方案 broker无法连接客户端触发自动关闭解决方案

    记录一次自己碰到的问题, 亲测有效 zookeeper客户端会话频繁超时解决方案 broker无法连接客户端触发自动关闭解决方案 zookeeper客户端会话频繁超时解决方案 broker无法连接客户端触发自动关闭解决方案

    基于Java语言的Zookeeper学习笔记设计源码

    该项目为基于Java语言的Zookeeper学习笔记设计源码,总计包含20个文件,具体构成包括15个Java源文件、3个PNG图片文件以及1个Markdown文件。此外,项目还包含1个XML配置文件,适用于学习和实践Zookeeper相关知识。

    zookeeper学习笔记.pptx

    本文适合但不限于软件开发人员阅读。本文档能够使阅读者对zookeeper有一个宏观且全面的了解,内容主要包含zookeeper架构、数据模型、读写及工作原理、典型应用场景、指令汇总等,

    zookeeper连接工具zktools

    《Zookeeper连接工具ZkTools详解》 Zookeeper作为一个分布式协调服务,在云原生环境中扮演着至关重要的角色。它提供了一种可靠的方式来管理和维护配置信息、命名服务、集群同步、分布式锁等。为了方便开发者与...

    Zookeeper学习资源和笔记(附代码)

    Zookeeper是Apache Hadoop项目下的一个子项目,它是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现。...通过深入学习和实践,可以更好地应对分布式环境中的各种挑战。

    hadoop,hbase,zookeeper安装笔记

    hadoop,hbase,zookeeper安装笔记hadoop,hbase,zookeeper安装笔记hadoop,hbase,zookeeper安装笔记

    zookeeper笔记和搭建

    资源名称:zookeeper笔记和搭建 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

    zookeeper笔记.pdf

    ZooKeeper笔记 ZooKeeper是一个分布式应用程序协调服务,提供了一个树形命名空间,用于存储和管理数据。 ZooKeeper主要用于分布式应用程序的配置管理、名称服务、分布式同步和提供组服务等。 ZooKeeper命令 * ...

    zookeeper笔记

    ### Zookeeper概述 Zookeeper是一种分布式协调服务框架,它的核心任务是为用户的分布式应用程序提供一系列的协调...无论是从其提供的服务还是从其内部的机制来看,Zookeeper都是值得深入学习和掌握的重要技术之一。

    zookeeper查看工具

    2. **会话和临时节点管理**:展示客户端的会话信息,包括会话ID、存活时间、拥有节点等,以及对临时节点的创建、删除操作。 3. **节点数据查看与编辑**:用户可以查看和修改Zookeeper中的任何节点数据,包括二进制...

    zookeeper完整学习笔记

    Apache ZooKeeper 是一款开源的分布式协调服务,设计用于管理和简化分布式环境中的数据协调任务。它提供了一种简单、高可用且容错的机制,使得开发者可以专注于核心业务逻辑,而不是复杂的分布式问题。Zookeeper ...

Global site tag (gtag.js) - Google Analytics