use of org.apache.zookeeper.WatchedEvent in project otter by alibaba.
the class ZooKeeperClientTest method testClient.
@Test
public void testClient() {
ZkClientx zk = ZooKeeperClient.getInstance();
// 强制获取zk中的地址信息
final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper();
ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp);
HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
want.number(serverAddrs.size()).isEqualTo(3);
String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort();
want.string(s1).isEqualTo(cluster1);
Stat stat = new Stat();
try {
zkp.getChildren("/otter/channel/304/388", false, stat);
System.out.println(stat.getCversion());
} catch (KeeperException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// 测试下session timeout
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
public void run() {
try {
zkp.getChildren("/", false);
} catch (KeeperException e1) {
want.fail();
} catch (InterruptedException e1) {
want.fail();
}
int sessionTimeout = zkp.getSessionTimeout();
long sessionId = zkp.getSessionId();
byte[] passwd = zkp.getSessionPasswd();
try {
ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
// do nothing
}
}, sessionId, passwd);
// 用老的sessionId连接上去,进行一次close操作后,让原先正在使用的出现SESSION_EXPIRED
newZk.close();
} catch (IOException e) {
want.fail();
} catch (InterruptedException e) {
want.fail();
}
latch.countDown();
}
}.start();
try {
latch.await();
} catch (InterruptedException e) {
want.fail();
}
zk.getChildren("/");
}
use of org.apache.zookeeper.WatchedEvent in project hbase by apache.
the class HBaseTestingUtil method expireSession.
/**
* Expire a ZooKeeper session as recommended in ZooKeeper documentation
* http://hbase.apache.org/book.html#trouble.zookeeper
* <p/>
* There are issues when doing this:
* <ol>
* <li>http://www.mail-archive.com/dev@zookeeper.apache.org/msg01942.html</li>
* <li>https://issues.apache.org/jira/browse/ZOOKEEPER-1105</li>
* </ol>
* @param nodeZK - the ZK watcher to expire
* @param checkStatus - true to check if we can create a Table with the current configuration.
*/
public void expireSession(ZKWatcher nodeZK, boolean checkStatus) throws Exception {
Configuration c = new Configuration(this.conf);
String quorumServers = ZKConfig.getZKQuorumServersString(c);
ZooKeeper zk = nodeZK.getRecoverableZooKeeper().getZooKeeper();
byte[] password = zk.getSessionPasswd();
long sessionID = zk.getSessionId();
// Expiry seems to be asynchronous (see comment from P. Hunt in [1]),
// so we create a first watcher to be sure that the
// event was sent. We expect that if our watcher receives the event
// other watchers on the same machine will get is as well.
// When we ask to close the connection, ZK does not close it before
// we receive all the events, so don't have to capture the event, just
// closing the connection should be enough.
ZooKeeper monitor = new ZooKeeper(quorumServers, 1000, new org.apache.zookeeper.Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
LOG.info("Monitor ZKW received event=" + watchedEvent);
}
}, sessionID, password);
// Making it expire
ZooKeeper newZK = new ZooKeeper(quorumServers, 1000, EmptyWatcher.instance, sessionID, password);
// ensure that we have connection to the server before closing down, otherwise
// the close session event will be eaten out before we start CONNECTING state
long start = EnvironmentEdgeManager.currentTime();
while (newZK.getState() != States.CONNECTED && EnvironmentEdgeManager.currentTime() - start < 1000) {
Thread.sleep(1);
}
newZK.close();
LOG.info("ZK Closed Session 0x" + Long.toHexString(sessionID));
// Now closing & waiting to be sure that the clients get it.
monitor.close();
if (checkStatus) {
getConnection().getTable(TableName.META_TABLE_NAME).close();
}
}
use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithExistWatch.
@Test
public void testSymlinkWithExistWatch() throws InterruptedException, ExecutionException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch.countDown();
}
};
Watcher existWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeCreated);
_zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
}
};
AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.NONODE);
latch2.countDown();
}
};
// symlink: /foo/$link/newNode -> /foo/bar/newNode
_zkClient.getZooKeeper().exists("/foo/$link/newNode", existWatch, existCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<>());
}
use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatch.
@Test
public void testSymlinkWithChildrenWatch() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.ChildrenCallback childrenCallback = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
Assert.assertEquals(children.size(), 11);
latch.countDown();
}
};
Watcher childrenWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, childrenCallback, null);
}
};
AsyncCallback.ChildrenCallback childrenCallback2 = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch2.countDown();
}
};
// symlink: /foo/$link -> /foo/bar
_zkClient.getZooKeeper().getChildren("/foo/$link", childrenWatch, childrenCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<>());
}
use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatcher3.
// test children2Callback watcher
@Test
public void testSymlinkWithChildrenWatcher3() throws ExecutionException, InterruptedException, KeeperException {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Stat expectedStat1 = _zkClient.getZooKeeper().exists("/foo/bar", false);
Stat expectedStat2 = _zkClient.getZooKeeper().exists("/bar/foo", false);
final AsyncCallback.Children2Callback callback2 = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(path, "/foo/$link");
Assert.assertEquals(children.size(), 5);
Assert.assertEquals(stat, expectedStat2);
latch2.countDown();
}
};
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null);
}
};
AsyncCallback.Children2Callback callback = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(stat, expectedStat1);
latch1.countDown();
}
};
// set watcher to /foo/$link
_zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null);
latch1.await(30, TimeUnit.SECONDS);
// update symlink
_zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<>());
latch2.await(30, TimeUnit.SECONDS);
FutureCallback<None> fcb = new FutureCallback<>();
// restore symlink
_zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb);
fcb.get();
}
Aggregations