Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId = zk.getSessionId();
// 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);
}
// Now, try an ephemeral node. This should fail since we
// cannot create ephemeral nodes on a local session.
try {
zk.create(nodePrefix + "ephemeral", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Assert.fail("Ephemeral node creation should fail.");
} catch (KeeperException.EphemeralOnLocalSessionException e) {
}
// Close the session.
zk.close();
// Validate data on both follower and leader
HashMap<String, Integer> peers = new HashMap<String, Integer>();
peers.put("leader", leaderIdx);
peers.put("follower", followerIdx);
for (Entry<String, Integer> entry: peers.entrySet()) {
watcher.reset();
// Try reconnecting with a new session.
// The data should be persisted, even though the session was not.
zk = qb.createClient(watcher, hostPorts[entry.getValue()],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long newSessionId = zk.getSessionId();
Assert.assertFalse(newSessionId == localSessionId);
for (int i = 0; i < 5; i++) {