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:
Client establishes a session and makes two consecutive asynchronous calls to create /tasks and /workers.
First session expires.
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.