use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project coprhd-controller by CoprHD.
the class ZkConnection method build.
/**
* Builds zk connector. Note that this method does not initiate a connection. {@link ZkConnection#connect()} must be called to connect
* to cluster.
* <p/>
* This separation is provided so that callbacks can be setup separately prior to connection to cluster.
*/
public void build() {
try {
_zkConnection = CuratorFrameworkFactory.builder().connectString(_connectString).connectionTimeoutMs(DEFAULT_CONN_TIMEOUT).canBeReadOnly(true).sessionTimeoutMs(_timeoutMs).retryPolicy(new RetryUntilElapsed(_timeoutMs, RETRY_INTERVAL_MS)).build();
_zkConnection.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
_logger.warn("Unknown exception in curator stack", e);
}
});
_zkConnection.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
_logger.info("Current connection state {}", newState);
}
});
if (FileUtils.exists(siteIdFile)) {
siteId = new String(FileUtils.readDataFromFile(siteIdFile));
siteId = siteId.trim();
_logger.info("Current site id is {}", siteId);
}
} catch (Exception e) {
throw CoordinatorException.fatals.failedToBuildZKConnector(e);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.
the class TestFrameworkBackground method testErrorListener.
@Test
public void testErrorListener() throws Exception {
ACLProvider badAclProvider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
throw new UnsupportedOperationException();
}
@Override
public List<ACL> getAclForPath(String path) {
throw new UnsupportedOperationException();
}
};
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).aclProvider(badAclProvider).build();
try {
client.start();
final CountDownLatch errorLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof UnsupportedOperationException) {
errorLatch.countDown();
}
}
};
client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
Assert.assertTrue(new Timing().awaitLatch(errorLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.
the class TestFrameworkBackground method testShutdown.
/**
* CURATOR-126
* Shutdown the Curator client while there are still background operations running.
*/
@Test
public void testShutdown() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).maxCloseWaitMs(timing.forWaiting().milliseconds()).build();
try {
final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
((CuratorFrameworkImpl) client).debugUnhandledErrorListener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof IllegalStateException) {
hadIllegalStateException.set(true);
}
}
};
client.start();
final CountDownLatch operationReadyLatch = new CountDownLatch(1);
((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {
@Override
public void listen(OperationAndData<?> data) {
try {
operationReadyLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// queue a background operation that will block due to the debugListener
client.create().inBackground().forPath("/hey");
timing.sleepABit();
// close the client while the background is still blocked
client.close();
// unblock the background
operationReadyLatch.countDown();
timing.sleepABit();
// should not generate an exception
Assert.assertFalse(hadIllegalStateException.get());
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.
the class TestLeaderAcls method testAclErrorWithLeader.
@Test(description = "Validation test for CURATOR-365")
public void testAclErrorWithLeader() throws Exception {
ACLProvider provider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.OPEN_ACL_UNSAFE;
}
@Override
public List<ACL> getAclForPath(String path) {
if (path.equals("/base")) {
try {
String testDigest = DigestAuthenticationProvider.generateDigest("test:test");
return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", testDigest)));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return getDefaultAcl();
}
};
RetryPolicy retryPolicy = new ExponentialBackoffRetry(timing.milliseconds(), 3);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).authorization("digest", "test:test".getBytes());
CuratorFramework client = builder.build();
LeaderLatch latch = null;
try {
client.start();
latch = new LeaderLatch(client, "/base");
latch.start();
Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
latch.close();
latch = null;
CuratorFramework noAuthClient = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy);
try {
noAuthClient.start();
final CountDownLatch noAuthLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof KeeperException.NoAuthException) {
noAuthLatch.countDown();
}
}
};
noAuthClient.getUnhandledErrorListenable().addListener(listener);
// use a path below "base" as noAuthClient is not authorized to create nodes in "/base"
// but also making sure that the code goes through the backgroundCreateParentsThenNode() codepath
latch = new LeaderLatch(noAuthClient, "/base/second");
latch.start();
Assert.assertTrue(timing.awaitLatch(noAuthLatch));
} finally {
CloseableUtils.closeQuietly(noAuthClient);
}
} finally {
CloseableUtils.closeQuietly(latch);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.
the class TestNodeCache method testDeleteThenCreate.
@Test
public void testDeleteThenCreate() throws Exception {
NodeCache cache = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
try {
client.create().creatingParentsIfNeeded().forPath("/test/foo", "one".getBytes());
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
error.set(e);
}
});
final Semaphore semaphore = new Semaphore(0);
cache = new NodeCache(client, "/test/foo");
cache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
semaphore.release();
}
});
cache.start(true);
Assert.assertEquals(cache.getCurrentData().getData(), "one".getBytes());
client.delete().forPath("/test/foo");
Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
client.create().forPath("/test/foo", "two".getBytes());
Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
Throwable t = error.get();
if (t != null) {
Assert.fail("Assert", t);
}
Assert.assertEquals(cache.getCurrentData().getData(), "two".getBytes());
cache.close();
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
}
}
Aggregations