设为首页 收藏本站
查看: 2029|回复: 0

[经验分享] [Zookeeper学习笔记之四]Zookeeper Client Library会话重建

[复制链接]

尚未签到

发表于 2017-4-18 12:52:02 | 显示全部楼层 |阅读模式
  为了说明问题,先来看个简单的示例代码

package com.tom.zookeeper.book;
import com.tom.Host;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher;
import java.io.IOException;
public class Master implements Watcher {
ZooKeeper zk;
String hostPort;
Master(String hostPort) {
this.hostPort = hostPort;
}
void startZK() throws IOException {
System.out.println("Start to create the ZooKeeper instance");
zk = new ZooKeeper(hostPort, 15000, this);
System.out.println("Finish to create the ZooKeeper instance");
}
public void process(WatchedEvent e) {
System.out.println(e);
}
public static void main(String args[]) throws Exception {
Master m = new Master(Host.HOST);
m.startZK();
Thread.sleep(60*1000); //sleep 60s
}
}
  1.不启动Zookeeper服务器
  运行上面的代码,输出是
  Start to create the ZooKeeper instance
  Finish to create the ZooKeeper instance
  因为会话没有建立,所以,Watcher的回调方法process没有被调用
  2.启动Zookeeper服务器
  运行上面的代码,在运行结束前,然后关闭Zookeeper服务器,输出是
  Finish to create the ZooKeeper instance
WatchedEvent state:SyncConnected type:None path:null
WatchedEvent state:Disconnected type:None path:null
  3.启动Zookeeper服务器,运行代码,在运行结束前,关闭服务器和重启服务器,输出是
  Finish to create the ZooKeeper instance
WatchedEvent state:SyncConnected type:None path:null
WatchedEvent state:Disconnected type:None path:null
WatchedEvent state:SyncConnected type:None path:null
  对于第三种情况,客户端重新建立会话,原因是当Zookeeper服务器不可用时,客户端维持的是CONNECTING状态,当Zookeeper服务器又可用时,客户端会自动重建会话
  假如Zookeeper Client的超时时间是5秒钟,而Zookeeper服务器挂了10分钟,按理说,Zookeeper Client的连接早该超时了,可为什么能会话重建呢,为什么会有如下的事件发生呢?
  WatchedEvent state:Disconnected type:None path:null
WatchedEvent state:SyncConnected type:None path:null
  原因可以参考Zookeeper的官方FAQ,它就这个给出了答案:
  What happens to ZK sessions while the cluster is down?
Imagine that a client is connected to ZK with a 5 second session timeout, and the administrator brings the entire ZK cluster down for an upgrade. The cluster is down for several minutes, and then is restarted.
In this scenario, the client is able to reconnect and refresh its session. Because session timeouts are tracked by the leader, the session starts counting down again with a fresh timeout when the cluster is restarted. So, as long as the client connects within the first 5 seconds after a leader is elected, it will reconnect without an expiration, and any ephemeral nodes it had prior to the downtime will be maintained.

The same behavior is exhibited when the leader crashes and a new one is elected. In the limit, if the leader is flip-flopping back and forth quickly, sessions will never expire since their timers are getting constantly reset.
意思就是说,如果在Zookeeper挂了之前,session没有超时,那么Zookeeper重启后,会完成会话重建,至于重建后的session还有多少时间就要timeout了,这个里面并没有说的很清楚,仅用了fresh这个单词,

这个单词是说重建的session从重新开始计算累计存活时间,还是说,是把之前Zookeeper活着的时候,已经走过的session时间计算在内了?
  总结和问题:
  1.在Zookeeper服务器不可用时,客户端等维持CONNECTING状态多长时间?
  2.通过这个实例,我们在处理Disconnected事件时,不应该重新创建新的Zookeeper实例,试图创建新的会话,如果Zookeeper能够恢复,那么之前的Zookeeper实例在Zookeeper Client Library的帮助下可以完成会话重建,如果Zookeeper不能恢复,那么重新创建Zookeeper也是徒劳无功。



 

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-365959-1-1.html 上篇帖子: zookeeper 和 hbase 问题集锦 下篇帖子: zookeeper学习(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表