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

[Zookeeper学习笔记之三]Zookeeper会话超时机制

 
阅读更多

首先,会话超时是由Zookeeper服务端通知客户端会话已经超时,客户端不能自行决定会话已经超时,不过客户端可以通过调用Zookeeper.close()主动的发起会话结束请求,如下的代码输出内容

Created /zoo-739160015

CONNECTED
CONNECTED

.............
CONNECTED
CONNECTED

CONNECTED
CLOSED
CLOSED

 

上述代码是针对standalone mode下的zookeeper server运行的,虽然代码中有意的让会话超时,可实际上会话并没有超时,一直处于CONNECTED状态,服务器端的znode /zoo-739160015也一直存在,现在大概了解Zookeeper有会话超时自动重建的功能,是否与此有关,待验证后再来更新这段

 

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

public class CreateGroup implements Watcher {
    private static final int SESSION_TIMEOUT = 3000;
    private volatile static boolean shutdown;
    private ZooKeeper zk;
    private CountDownLatch connectedSignal = new CountDownLatch(1);

    public void connect(String hosts) throws IOException, InterruptedException {
        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
        connectedSignal.await();
    }

    @Override
    public void process(WatchedEvent event) { // Watcher interface
        if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
            connectedSignal.countDown();
        }
    }

    public void create(String groupName) throws KeeperException,
            InterruptedException {
        String path = "/" + groupName;
        String createdPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL); //The znode will be deleted upon the session is closed.
        System.out.println("Created " + createdPath);
    }

    public void close() throws InterruptedException {
        zk.close();
    }

    public static void main(String[] args) throws Exception {
        final CreateGroup createGroup = new CreateGroup();
        String groupName = "zoo" + ThreadLocalRandom.current().nextInt();
        createGroup.connect(Host.HOST);
        createGroup.create(groupName);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(!shutdown) {
                   ZooKeeper.States s =  createGroup.zk.getState();
                    System.out.println(s);
                    try {
                        Thread.sleep(5*1000); //Session should be timeout since the session timeout is set to 3 ms
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        Thread.sleep(60*1000);
        createGroup.close();
        Thread.sleep(10*1000);
        shutdown = true;
    }
}

 

Zookeeper会话管理的特性:

1.在一个会话过程中,如果客户端发现zookeeper server已经不可达,zookeeper client library会自动寻找zookeeper集群server中的另一个server,然后建立会话,因此,即使有台zookeeper server不可用,会话状态仍然可以继续

2.Zookeeper会话具有请求顺序性保证,同一个会话的多次请求会排队,zookeeper按照FIFO的顺序进行处理客户端的请求,例如一个会话中,客户端连续发起修改znode和删除znode的请求,那么在zookeeper服务器端,zookeeper会先更新后删除,而不会先删除后更新

3.如果客户端一个Zookeeper实例对应多个会话,那么,2提到的顺序性将不能保证。何为一个zookeeper对应多个会话?目前不明白,先把书中的原话放到这里

 

Sessions offer order guarantees, which means that requests in a session are executed in FIFO (first in, first out) order. Typically, a client has only a single session open, so its requests are all executed in FIFO order. If a client has multiple concurrent sessions, FIFO ordering is not necessarily preserved across the sessions. Consecutive sessions of the same client, even if they don’t overlap in time, also do not necessarily preserve FIFO order. Here is how it can happen in this case:

  1. Client establishes a session and makes two consecutive asynchronous calls to create /tasks and /workers.
  2. First session expires.
  3. Client establishes another session and makes an asynchronous call to create /assign.

In this sequence of calls, it is possible that only /tasks and /assign have been created, which preserves FIFO ordering for the first session but violates it across sessions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
6 楼 bit1129 2014-08-09  
swingseagull 写道
开篇博客有点让人摸不着头脑哦,一上来就是笔记三,接下来那篇怎么又成笔记二了 

第一次听说zk,完全不懂它是个什么东东。对博主的第三点有关多session的说明,根据英文连猜带蒙,意思应该是一般情况下,客户端都只有一个session,但也有可能同时建立多个session,或是连续建立多个session的情况



等我弄清楚了再来整理,到时就有头脑了。。。zk是分布式系统的标配了,作为集群的协调管理服务器
5 楼 swingseagull 2014-08-09  
开篇博客有点让人摸不着头脑哦,一上来就是笔记三,接下来那篇怎么又成笔记二了 

第一次听说zk,完全不懂它是个什么东东。对博主的第三点有关多session的说明,根据英文连猜带蒙,意思应该是一般情况下,客户端都只有一个session,但也有可能同时建立多个session,或是连续建立多个session的情况
4 楼 bit1129 2014-07-31  
josico 写道
麻烦着重讲一下 zk主要用在哪 哪样的实例场景下会使用 为什么zk现在可以基本算作集群的标配 有些我还不是太明白 请赐教了  哈哈
bit1129 写道
josico 写道
麻烦问一下  zookeeper 学习笔记 一  在哪


学习笔记一还在草稿箱里,呵呵。。还没写好,关于分布式的一些基础




呵呵,好,这个后面会总结,:-)
3 楼 josico 2014-07-31  
麻烦着重讲一下 zk主要用在哪 哪样的实例场景下会使用 为什么zk现在可以基本算作集群的标配 有些我还不是太明白 请赐教了  哈哈
bit1129 写道
josico 写道
麻烦问一下  zookeeper 学习笔记 一  在哪


学习笔记一还在草稿箱里,呵呵。。还没写好,关于分布式的一些基础

2 楼 bit1129 2014-07-30  
josico 写道
麻烦问一下  zookeeper 学习笔记 一  在哪


学习笔记一还在草稿箱里,呵呵。。还没写好,关于分布式的一些基础
1 楼 josico 2014-07-30  
麻烦问一下  zookeeper 学习笔记 一  在哪

相关推荐

Global site tag (gtag.js) - Google Analytics