use of org.apache.zookeeper.ZooKeeper in project helix by apache.
the class ZkUnitTestBase method simulateSessionExpiry.
protected void simulateSessionExpiry(ZkConnection zkConnection) throws IOException, InterruptedException {
ZooKeeper oldZookeeper = zkConnection.getZookeeper();
LOG.info("Old sessionId = " + oldZookeeper.getSessionId());
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
LOG.info("In New connection, process event:" + event);
}
};
ZooKeeper newZookeeper = new ZooKeeper(zkConnection.getServers(), oldZookeeper.getSessionTimeout(), watcher, oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
LOG.info("New sessionId = " + newZookeeper.getSessionId());
// Thread.sleep(3000);
newZookeeper.close();
Thread.sleep(10000);
oldZookeeper = zkConnection.getZookeeper();
LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}
use of org.apache.zookeeper.ZooKeeper in project bboxdb by jnidzwetzki.
the class ZookeeperClient method init.
/**
* Connect to zookeeper
*/
@Override
public void init() {
try {
serviceState.reset();
serviceState.dipatchToStarting();
usage = new Phaser(1);
final CountDownLatch connectLatch = new CountDownLatch(1);
zookeeper = new ZooKeeper(connectionString, ZOOKEEPER_SESSION_TIMEOUT, new Watcher() {
@Override
public void process(final WatchedEvent event) {
if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
connectLatch.countDown();
}
}
});
final boolean waitResult = connectLatch.await(ZOOKEEPER_CONNECT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
if (waitResult == false) {
throw new ZookeeperException("Unable to connect in " + ZOOKEEPER_CONNECT_TIMEOUT_IN_SEC + " seconds. Connect string is: " + connectionString);
}
createDirectoryStructureIfNeeded();
serviceState.dispatchToRunning();
} catch (Exception e) {
logger.warn("Got exception while connecting to zookeeper", e);
closeZookeeperConnectionNE();
serviceState.dispatchToFailed(e);
}
}
use of org.apache.zookeeper.ZooKeeper in project xian by happyyangyuan.
the class BasicTests method testExpiredSession.
@Test
public void testExpiredSession() throws Exception {
// see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4
final Timing timing = new Timing();
final CountDownLatch latch = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.Expired) {
latch.countDown();
}
}
};
final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
client.start();
try {
final AtomicBoolean firstTime = new AtomicBoolean(true);
RetryLoop.callWithRetry(client, new Callable<Object>() {
@Override
public Object call() throws Exception {
if (firstTime.compareAndSet(true, false)) {
try {
client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException ignore) {
// ignore
}
KillSession.kill(client.getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.awaitLatch(latch));
}
ZooKeeper zooKeeper = client.getZooKeeper();
client.blockUntilConnectedOrTimedOut();
Assert.assertNotNull(zooKeeper.exists("/foo", false));
return null;
}
});
} finally {
client.close();
}
}
use of org.apache.zookeeper.ZooKeeper in project accumulo by apache.
the class ZooCache method get.
/**
* Gets data at the given path, filling status information into the given <code>Stat</code> object. A watch is established by this call.
*
* @param zPath
* path to get
* @param status
* status object to populate
* @return path data, or null if non-existent
*/
public byte[] get(final String zPath, final ZcStat status) {
ZooRunnable<byte[]> zr = new ZooRunnable<byte[]>() {
@Override
public byte[] run() throws KeeperException, InterruptedException {
ZcStat zstat = null;
// only read volatile once so following code works with a consistent snapshot
ImmutableCacheCopies lic = immutableCache;
byte[] val = lic.cache.get(zPath);
if (val != null || lic.cache.containsKey(zPath)) {
if (status != null) {
zstat = lic.statCache.get(zPath);
copyStats(status, zstat);
}
return val;
}
/*
* The following call to exists() is important, since we are caching that a node does not exist. Once the node comes into existence, it will be added to
* the cache. But this notification of a node coming into existence will only be given if exists() was previously called. If the call to exists() is
* bypassed and only getData() is called with a special case that looks for Code.NONODE in the KeeperException, then non-existence can not be cached.
*/
cacheWriteLock.lock();
try {
final ZooKeeper zooKeeper = getZooKeeper();
Stat stat = zooKeeper.exists(zPath, watcher);
byte[] data = null;
if (stat == null) {
if (log.isTraceEnabled()) {
log.trace("zookeeper did not contain {}", zPath);
}
} else {
try {
data = zooKeeper.getData(zPath, watcher, stat);
zstat = new ZcStat(stat);
} catch (KeeperException.BadVersionException | KeeperException.NoNodeException e1) {
throw new ConcurrentModificationException();
}
if (log.isTraceEnabled()) {
log.trace("zookeeper contained {} {}", zPath, (data == null ? null : new String(data, UTF_8)));
}
}
put(zPath, data, zstat);
copyStats(status, zstat);
return data;
} finally {
cacheWriteLock.unlock();
}
}
};
return zr.retry();
}
use of org.apache.zookeeper.ZooKeeper in project accumulo by apache.
the class ZooCache method getChildren.
/**
* Gets the children of the given node. A watch is established by this call.
*
* @param zPath
* path of node
* @return children list, or null if node has no children or does not exist
*/
public List<String> getChildren(final String zPath) {
ZooRunnable<List<String>> zr = new ZooRunnable<List<String>>() {
@Override
public List<String> run() throws KeeperException, InterruptedException {
// only read volatile once for consistency
ImmutableCacheCopies lic = immutableCache;
if (lic.childrenCache.containsKey(zPath)) {
return lic.childrenCache.get(zPath);
}
cacheWriteLock.lock();
try {
if (childrenCache.containsKey(zPath)) {
return childrenCache.get(zPath);
}
final ZooKeeper zooKeeper = getZooKeeper();
List<String> children = zooKeeper.getChildren(zPath, watcher);
if (children != null) {
children = ImmutableList.copyOf(children);
}
childrenCache.put(zPath, children);
immutableCache = new ImmutableCacheCopies(++updateCount, immutableCache, childrenCache);
return children;
} catch (KeeperException ke) {
if (ke.code() != Code.NONODE) {
throw ke;
}
} finally {
cacheWriteLock.unlock();
}
return null;
}
};
return zr.retry();
}
Aggregations