use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ReadOnlyModeTest method testConnectionEvents.
/**
* Ensures that upon connection to a read-only server client receives
* ConnectedReadOnly state notification.
*/
@Test(timeout = 90000)
public void testConnectionEvents() throws Exception {
final List<KeeperState> states = new ArrayList<KeeperState>();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
states.add(event.getState());
}
}, true);
boolean success = false;
for (int i = 0; i < 30; i++) {
try {
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
success = true;
break;
} catch (KeeperException.ConnectionLossException e) {
Thread.sleep(1000);
}
}
Assert.assertTrue("Did not succeed in connecting in 30s", success);
// kill peer and wait no more than 5 seconds for read-only server
// to be started (which should take one tickTime (2 seconds))
qu.shutdown(2);
// Re-connect the client (in case we were connected to the shut down
// server and the local session was not persisted).
zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
states.add(event.getState());
}
}, true);
long start = Time.currentElapsedTime();
while (!(zk.getState() == States.CONNECTEDREADONLY)) {
Thread.sleep(200);
// FIXME this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this
Assert.assertTrue("Can't connect to the server", Time.currentElapsedTime() - start < 30000);
}
// At this point states list should contain, in the given order,
// SyncConnected, Disconnected, and ConnectedReadOnly states
Assert.assertTrue("ConnectedReadOnly event wasn't received", states.get(2) == KeeperState.ConnectedReadOnly);
zk.close();
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ReconfigTest method testPortChangeToBlockedPort.
private void testPortChangeToBlockedPort(boolean testLeader) throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
List<String> joiningServers = new ArrayList<String>();
int leaderIndex = getLeaderId(qu);
int followerIndex = leaderIndex == 1 ? 2 : 1;
int serverIndex = testLeader ? leaderIndex : followerIndex;
int reconfigIndex = testLeader ? followerIndex : leaderIndex;
// modify server's client port
int quorumPort = qu.getPeer(serverIndex).peer.getQuorumAddress().getPort();
int electionPort = qu.getPeer(serverIndex).peer.getElectionAddress().getPort();
int oldClientPort = qu.getPeer(serverIndex).peer.getClientPort();
int newClientPort = PortAssignment.unique();
try (ServerSocket ss = new ServerSocket()) {
ss.bind(new InetSocketAddress(getLoopbackAddress(), newClientPort));
joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort);
// create a /test znode and check that read/write works before
// any reconfig is invoked
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
// Reconfigure
reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1);
Thread.sleep(1000);
// The follower reconfiguration will have failed
zkArr[serverIndex].close();
zkArr[serverIndex] = new ZooKeeper("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[serverIndex].close();
zkAdminArr[serverIndex] = new ZooKeeperAdmin("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
try {
Thread.sleep(1000);
zkArr[serverIndex].setData("/test", "teststr".getBytes(), -1);
Assert.fail("New client connected to new client port!");
} catch (KeeperException.ConnectionLossException e) {
// Exception is expected
}
try (ServerSocket ss2 = new ServerSocket()) {
ss2.bind(new InetSocketAddress(getLoopbackAddress(), oldClientPort));
}
// Move back to the old port
joiningServers.clear();
joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + oldClientPort);
reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1);
zkArr[serverIndex].close();
zkArr[serverIndex] = new ZooKeeper("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
testServerHasConfig(zkArr[serverIndex], joiningServers, null);
Assert.assertEquals(oldClientPort, qu.getPeer(serverIndex).peer.getClientPort());
}
closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ZooKeeperTestClient method getEvent.
private WatchedEvent getEvent(int numTries) throws InterruptedException {
WatchedEvent event = null;
for (int i = 0; i < numTries; i++) {
System.out.println("i = " + i);
event = events.poll(10, TimeUnit.SECONDS);
if (event != null) {
break;
}
Thread.sleep(5000);
}
return event;
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ZooKeeperTestClient method enode_test_2.
private void enode_test_2() throws IOException, InterruptedException, KeeperException {
checkRoot();
String parentName = testDirOnZK;
String nodeName = parentName + "/enode_abc";
ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this);
Stat stat_parent = zk_1.exists(parentName, false);
if (stat_parent == null) {
try {
zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException ke) {
Assert.fail("Creating node " + parentName + ke.getMessage());
}
}
Stat stat_node = zk_1.exists(nodeName, false);
if (stat_node != null) {
try {
zk.delete(nodeName, -1);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
if (!valid) {
Assert.fail("Unexpected exception code for delete: " + ke.getMessage());
}
}
}
List<String> firstGen1 = zk_1.getChildren(parentName, true);
Stat stat = new Stat();
List<String> firstGen2 = zk_1.getChildren(parentName, true, stat);
if (!firstGen1.equals(firstGen2)) {
Assert.fail("children lists from getChildren()/getChildren2() do not match");
}
if (!stat_parent.equals(stat)) {
Assert.fail("stat from exists()/getChildren() do not match");
}
try {
zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NODEEXISTS;
if (!valid) {
Assert.fail("Unexpected exception code for createin: " + ke.getMessage());
}
}
Thread.sleep(5000);
WatchedEvent event = events.poll(10, TimeUnit.SECONDS);
if (event == null) {
throw new IOException("No event was delivered promptly");
}
if (event.getType() != EventType.NodeChildrenChanged || !event.getPath().equalsIgnoreCase(parentName)) {
Assert.fail("Unexpected event was delivered: " + event.toString());
}
stat_node = zk_1.exists(nodeName, false);
if (stat_node == null) {
Assert.fail("node " + nodeName + " should exist");
}
try {
zk.delete(parentName, -1);
Assert.fail("Should be impossible to delete a non-empty node " + parentName);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NOTEMPTY;
if (!valid) {
Assert.fail("Unexpected exception code for delete: " + code);
}
}
try {
zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Assert.fail("Should be impossible to create child off Ephemeral node " + nodeName);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS;
if (!valid) {
Assert.fail("Unexpected exception code for createin: " + code);
}
}
try {
List<String> children1 = zk.getChildren(nodeName, false);
List<String> children2 = zk.getChildren(nodeName, false, null);
if (!children1.equals(children2)) {
Assert.fail("children lists from getChildren()/getChildren2() does not match");
}
if (children1.size() > 0) {
Assert.fail("ephemeral node " + nodeName + " should not have children");
}
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NONODE;
if (!valid) {
Assert.fail("Unexpected exception code for createin: " + code);
}
}
firstGen1 = zk_1.getChildren(parentName, true);
firstGen2 = zk_1.getChildren(parentName, true, null);
if (!firstGen1.equals(firstGen2)) {
Assert.fail("children list from getChildren()/getChildren2() does not match");
}
stat_node = zk_1.exists(nodeName, true);
if (stat_node == null) {
Assert.fail("node " + nodeName + " should exist");
}
System.out.println("session id of zk: " + zk.getSessionId());
System.out.println("session id of zk_1: " + zk_1.getSessionId());
zk.close();
zk_1.exists("nosuchnode", false);
event = this.getEvent(10);
if (event == null) {
throw new Error("First event was not delivered promptly");
}
if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
Assert.fail("Unexpected first event was delivered: " + event.toString());
}
event = this.getEvent(10);
if (event == null) {
throw new Error("Second event was not delivered promptly");
}
if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
Assert.fail("Unexpected second event was delivered: " + event.toString());
}
firstGen1 = zk_1.getChildren(parentName, false);
stat_node = zk_1.exists(nodeName, false);
if (stat_node != null) {
Assert.fail("node " + nodeName + " should have been deleted");
}
if (firstGen1.contains(nodeName)) {
Assert.fail("node " + nodeName + " should not be a children");
}
deleteZKDir(zk_1, nodeName);
zk_1.close();
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ReconfigTest method testPortChange.
@Test
public void testPortChange() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
List<String> joiningServers = new ArrayList<String>();
int leaderIndex = getLeaderId(qu);
int followerIndex = leaderIndex == 1 ? 2 : 1;
// modify follower's client port
int quorumPort = qu.getPeer(followerIndex).peer.getQuorumAddress().getPort();
int electionPort = qu.getPeer(followerIndex).peer.getElectionAddress().getPort();
int oldClientPort = qu.getPeer(followerIndex).peer.getClientPort();
int newClientPort = PortAssignment.unique();
joiningServers.add("server." + followerIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort);
// create a /test znode and check that read/write works before
// any reconfig is invoked
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
reconfig(zkAdminArr[followerIndex], joiningServers, null, null, -1);
try {
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
}
} catch (KeeperException.ConnectionLossException e) {
Assert.fail("Existing client disconnected when client port changed!");
}
zkArr[followerIndex].close();
zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].close();
zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
Assert.fail("New client connected to old client port!");
} catch (KeeperException.ConnectionLossException e) {
}
}
zkArr[followerIndex].close();
zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].close();
zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
testServerHasConfig(zkArr[followerIndex], joiningServers, null);
Assert.assertEquals(newClientPort, qu.getPeer(followerIndex).peer.getClientPort());
joiningServers.clear();
// change leader's leading port - should renounce leadership
int newQuorumPort = PortAssignment.unique();
joiningServers.add("server." + leaderIndex + "=localhost:" + newQuorumPort + ":" + qu.getPeer(leaderIndex).peer.getElectionAddress().getPort() + ":participant;localhost:" + qu.getPeer(leaderIndex).peer.getClientPort());
reconfig(zkAdminArr[leaderIndex], joiningServers, null, null, -1);
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
Assert.assertTrue(qu.getPeer(leaderIndex).peer.getQuorumAddress().getPort() == newQuorumPort);
// the leader changed
Assert.assertTrue(getLeaderId(qu) != leaderIndex);
joiningServers.clear();
for (int i = 1; i <= 3; i++) {
joiningServers.add("server." + i + "=localhost:" + qu.getPeer(i).peer.getQuorumAddress().getPort() + ":" + PortAssignment.unique() + ":participant;localhost:" + qu.getPeer(i).peer.getClientPort());
}
reconfig(zkAdminArr[1], joiningServers, null, null, -1);
leaderIndex = getLeaderId(qu);
int follower1 = leaderIndex == 1 ? 2 : 1;
int follower2 = 1;
while (follower2 == leaderIndex || follower2 == follower1) follower2++;
// lets kill the leader and see if a new one is elected
qu.shutdown(getLeaderId(qu));
testNormalOperation(zkArr[follower2], zkArr[follower1]);
testServerHasConfig(zkArr[follower1], joiningServers, null);
testServerHasConfig(zkArr[follower2], joiningServers, null);
closeAllHandles(zkArr, zkAdminArr);
}
Aggregations