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

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

发表于: 2014-07-25   作者:bit1129   来源:转载   浏览:
摘要: 首先,会话超时是由Zookeeper服务端通知客户端会话已经超时,客户端不能自行决定会话已经超时,不过客户端可以通过调用Zookeeper.close()主动的发起会话结束请求,如下的代码输出内容 Created /zoo-739160015 CONNECTEDCONNECTED .............CONNECTEDCONNECTED CONNECTEDCLOSEDCLOSED

首先,会话超时是由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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
最近在工作中,为了完善公司集群服务的架构,提高可用性,降低运维成本,因此开始学习ZooKeeper。
ZooKeeper是什么? 高可用的高性能的分布式系统协调服务。局部不可用是分布式系统的固有特征,ZooKe
zookeeper功能点: 统一命名空间(Name Service) 配置推送 (Watch) 集群管理(Group membership) 统一
Unlike a typical file system, which is designed for storage, ZooKeeper data is kept in-memory
http://blog.csdn.net/shirdrn/article/details/7183503 参照前公司的目录结构组织dubbo应用的环境
虽然了解ZooKeeper的锁有一段时间了,但也只是到了会用的一个程度,其原理也大概知道是怎么一回事,
众所周知,在Java开发中,要创建一个单进程锁非常简单,使用JDK中自带的java.util.concurrent.locks
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号