当前位置:首页 > 开发 > 开源软件 > 正文

[Zookeeper学习笔记之四]Zookeeper Client Library会话重建

发表于: 2014-07-26   作者:bit1129   来源:转载   浏览:
摘要: 为了说明问题,先来看个简单的示例代码:   package com.tom.zookeeper.book; import com.tom.Host; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.Wat

为了说明问题,先来看个简单的示例代码

 

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也是徒劳无功。

 

 
 

 

 

 

[Zookeeper学习笔记之四]Zookeeper Client Library会话重建

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
如果我们要写一个ZooKeeper的client程序,那么有一个相关的配置:connect string,它通常以用逗号分
如果我们要写一个ZooKeeper的client程序,那么有一个相关的配置:connect string,它通常以用逗号分
首先来了解一下ZooKeeper的数据模型,源代码中会有很多诸如zxid, czxid等这样的变量,那这些到底是
ZooKeeper 的功能特性通过 ZooKeeper 配置文件来进行控制管理( zoo.cfg 配置文件)。 ZooKeeper 这
最近在工作中,为了完善公司集群服务的架构,提高可用性,降低运维成本,因此开始学习ZooKeeper。
ZooKeeper是什么? 高可用的高性能的分布式系统协调服务。局部不可用是分布式系统的固有特征,ZooKe
zookeeper功能点: 统一命名空间(Name Service) 配置推送 (Watch) 集群管理(Group membership) 统一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号