use of org.apache.zookeeper.Watcher 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.Watcher 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.Watcher 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);
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class ReconfigTest method createHandles.
public static ZooKeeper[] createHandles(QuorumUtil qu) throws IOException {
// create an extra handle, so we can index the handles from 1 to qu.ALL
// using the server id.
ZooKeeper[] zkArr = new ZooKeeper[qu.ALL + 1];
// not used.
zkArr[0] = null;
for (int i = 1; i <= qu.ALL; i++) {
// server ids are 1, 2 and 3
zkArr[i] = new ZooKeeper("127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
}
return zkArr;
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class QuorumTest method testLeaderShutdown.
@Test
public void testLeaderShutdown() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = new DisconnectableZooKeeper(qb.hostPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zk.create("/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/blah/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Leader leader = qb.s1.leader;
if (leader == null)
leader = qb.s2.leader;
if (leader == null)
leader = qb.s3.leader;
if (leader == null)
leader = qb.s4.leader;
if (leader == null)
leader = qb.s5.leader;
Assert.assertNotNull(leader);
for (int i = 0; i < 5000; i++) {
zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() {
public void processResult(int rc, String path, Object ctx, Stat stat) {
counter++;
if (rc != 0) {
errors++;
}
}
}, null);
}
for (LearnerHandler f : leader.getForwardingFollowers()) {
f.getSocket().shutdownInput();
}
for (int i = 0; i < 5000; i++) {
zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() {
public void processResult(int rc, String path, Object ctx, Stat stat) {
counter++;
if (rc != 0) {
errors++;
}
}
}, null);
}
// check if all the followers are alive
Assert.assertTrue(qb.s1.isAlive());
Assert.assertTrue(qb.s2.isAlive());
Assert.assertTrue(qb.s3.isAlive());
Assert.assertTrue(qb.s4.isAlive());
Assert.assertTrue(qb.s5.isAlive());
zk.close();
}
Aggregations