use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class WatchManager method getWatchesByPath.
/**
* Returns a watch report by path.
*
* @return watch report
* @see WatchesPathReport
*/
synchronized WatchesPathReport getWatchesByPath() {
Map<String, Set<Long>> path2ids = new HashMap<String, Set<Long>>();
for (Entry<String, Set<Watcher>> e : watchTable.entrySet()) {
Set<Long> ids = new HashSet<Long>(e.getValue().size());
path2ids.put(e.getKey(), ids);
for (Watcher watcher : e.getValue()) {
ids.add(((ServerCnxn) watcher).getSessionId());
}
}
return new WatchesPathReport(path2ids);
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class ClientTest method testNonExistingOpCode.
/**
* We create a perfectly valid 'exists' request, except that the opcode is wrong.
* @return
* @throws Exception
*/
@Test
public void testNonExistingOpCode() throws Exception {
final CountDownLatch clientDisconnected = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public synchronized void process(WatchedEvent event) {
if (event.getState() == KeeperState.Disconnected) {
clientDisconnected.countDown();
}
}
};
TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
final String path = "/m1";
RequestHeader h = new RequestHeader();
// This code does not exists
h.setType(888);
ExistsRequest request = new ExistsRequest();
request.setPath(path);
request.setWatch(false);
ExistsResponse response = new ExistsResponse();
ReplyHeader r = zk.submitRequest(h, request, response, null);
Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
// Sending a nonexisting opcode should cause the server to disconnect
Assert.assertTrue("failed to disconnect", clientDisconnected.await(5000, TimeUnit.MILLISECONDS));
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class QuorumZxidSyncTest method testLateLogs.
/**
* find out what happens when the latest state is in the snapshots not
* the logs.
*/
@Test
public void testLateLogs() throws Exception {
// crank up the epoch numbers
ClientBase.waitForServerUp(qb.hostPort, 10000);
ClientBase.waitForServerUp(qb.hostPort, 10000);
ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}
});
zk.create("/0", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.close();
qb.shutdownServers();
qb.startServers();
ClientBase.waitForServerUp(qb.hostPort, 10000);
qb.shutdownServers();
qb.startServers();
ClientBase.waitForServerUp(qb.hostPort, 10000);
zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}
});
zk.create("/1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.close();
qb.shutdownServers();
qb.startServers();
ClientBase.waitForServerUp(qb.hostPort, 10000);
qb.shutdownServers();
deleteLogs(qb.s1dir);
deleteLogs(qb.s2dir);
deleteLogs(qb.s3dir);
deleteLogs(qb.s4dir);
deleteLogs(qb.s5dir);
qb.startServers();
ClientBase.waitForServerUp(qb.hostPort, 10000);
zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}
});
zk.create("/2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.close();
qb.shutdownServers();
qb.startServers();
ClientBase.waitForServerUp(qb.hostPort, 10000);
zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}
});
boolean saw2 = false;
for (String child : zk.getChildren("/", false)) {
if (child.equals("2")) {
saw2 = true;
}
}
zk.close();
Assert.assertTrue("Didn't see /2 (went back in time)", saw2);
}
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);
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 InstanceManager method getStatus.
public String getStatus(String name, long timeout) throws KeeperException, InterruptedException {
Stat stat = new Stat();
byte[] data = null;
long endTime = Time.currentElapsedTime() + timeout;
KeeperException lastException = null;
for (int i = 0; i < maxTries && endTime > Time.currentElapsedTime(); i++) {
try {
data = zk.getData(reportsNode + '/' + name, false, stat);
if (LOG.isDebugEnabled()) {
LOG.debug("Got Data: " + ((data == null) ? "null" : new String(data)));
}
lastException = null;
break;
} catch (ConnectionLossException e) {
lastException = e;
} catch (NoNodeException e) {
final Object eventObj = new Object();
synchronized (eventObj) {
// wait for the node to appear
Stat eStat = zk.exists(reportsNode + '/' + name, new Watcher() {
public void process(WatchedEvent event) {
synchronized (eventObj) {
eventObj.notifyAll();
}
}
});
if (eStat == null) {
eventObj.wait(endTime - Time.currentElapsedTime());
}
}
lastException = e;
}
}
if (lastException != null) {
throw lastException;
}
return new String(data);
}
Aggregations