Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int otherFollowerIdx = (leaderIdx + 2) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
DisconnectableZooKeeper zk = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// Try creating some data.
for (int i = 0; i < 5; i++) {
zk.create(nodePrefix + i, new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
long localSessionId = zk.getSessionId();
byte[] localSessionPwd = zk.getSessionPasswd().clone();
// Try connecting with the same session id on a different
// server. This should fail since it is a local sesion.
try {
watcher.reset();
DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(
hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
localSessionId, localSessionPwd);
zknew.create(nodePrefix + "5", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Assert.fail("Connection on the same session ID should fail.");
} catch (KeeperException.SessionExpiredException e) {
} catch (KeeperException.ConnectionLossException e) {
}
// If we're testing a follower, also check the session id on the
// leader. This should also fail
if (!testLeader) {
try {
watcher.reset();
DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(
hostPorts[leaderIdx], CONNECTION_TIMEOUT,
watcher, localSessionId, localSessionPwd);
zknew.create(nodePrefix + "5", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.fail("Connection on the same session ID should fail.");
} catch (KeeperException.SessionExpiredException e) {
} catch (KeeperException.ConnectionLossException e) {
}
}
// However, we should be able to disconnect and reconnect to the same
// server with the same session id (as long as we do it quickly
// before expiration).
zk.disconnect();
watcher.reset();
zk = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher,
localSessionId, localSessionPwd);
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create(nodePrefix + "6", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// If we explicitly close the session, then the session id should no
// longer be valid.
zk.close();
try {
watcher.reset();
zk = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher,
localSessionId, localSessionPwd);
zk.create(nodePrefix + "7", new byte[0],