use of org.apache.curator.framework.api.CuratorEvent in project hadoop by apache.
the class TestCuratorService method testBackgroundDelete.
@Test
public void testBackgroundDelete() throws Throwable {
mkPath("/rm", CreateMode.PERSISTENT);
mkPath("/rm/child", CreateMode.PERSISTENT);
CuratorEventCatcher events = new CuratorEventCatcher();
curatorService.zkDelete("/rm", true, events);
CuratorEvent taken = events.take();
LOG.info("took {}", taken);
assertEquals(1, events.getCount());
}
use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.
the class AnnouncerTest method testSanity.
@Test(timeout = 60_000L)
public void testSanity() throws Exception {
curator.start();
curator.blockUntilConnected();
Announcer announcer = new Announcer(curator, exec);
final byte[] billy = "billy".getBytes();
final String testPath1 = "/test1";
final String testPath2 = "/somewhere/test2";
announcer.announce(testPath1, billy);
Assert.assertNull("/test1 does not exists", curator.checkExists().forPath(testPath1));
Assert.assertNull("/somewhere/test2 does not exists", curator.checkExists().forPath(testPath2));
announcer.start();
try {
Assert.assertArrayEquals("/test1 has data", billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertNull("/somewhere/test2 still does not exist", curator.checkExists().forPath(testPath2));
announcer.announce(testPath2, billy);
Assert.assertArrayEquals("/test1 still has data", billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals("/somewhere/test2 has data", billy, curator.getData().decompressed().forPath(testPath2));
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) {
latch.countDown();
}
}
});
curator.inTransaction().delete().forPath(testPath1).and().commit();
Assert.assertTrue("Wait for /test1 to be created", timing.forWaiting().awaitLatch(latch));
Assert.assertArrayEquals("expect /test1 data is restored", billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals("expect /somewhere/test2 is still there", billy, curator.getData().decompressed().forPath(testPath2));
announcer.unannounce(testPath1);
Assert.assertNull("expect /test1 unannounced", curator.checkExists().forPath(testPath1));
Assert.assertArrayEquals("expect /somewhere/test2 is still still there", billy, curator.getData().decompressed().forPath(testPath2));
} finally {
announcer.stop();
}
Assert.assertNull("expect /test1 remains unannounced", curator.checkExists().forPath(testPath1));
Assert.assertNull("expect /somewhere/test2 unannounced", curator.checkExists().forPath(testPath2));
}
use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.
the class AnnouncerTest method awaitAnnounce.
private void awaitAnnounce(final Announcer announcer, final String path, final byte[] bytes, boolean removeParentsIfCreated) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(path)) {
latch.countDown();
}
}
});
announcer.announce(path, bytes, removeParentsIfCreated);
latch.await();
}
use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.
the class CuratorInventoryManagerTest method testSanity.
@Test
public void testSanity() throws Exception {
final MapStrategy strategy = new MapStrategy();
CuratorInventoryManager<Map<String, Integer>, Integer> manager = new CuratorInventoryManager<Map<String, Integer>, Integer>(curator, new StringInventoryManagerConfig("/container", "/inventory"), exec, strategy);
curator.start();
curator.blockUntilConnected();
manager.start();
Assert.assertTrue(Iterables.isEmpty(manager.getInventory()));
CountDownLatch containerLatch = new CountDownLatch(1);
strategy.setNewContainerLatch(containerLatch);
curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/container/billy", new byte[] {});
Assert.assertTrue(timing.awaitLatch(containerLatch));
strategy.setNewContainerLatch(null);
final Iterable<Map<String, Integer>> inventory = manager.getInventory();
Assert.assertTrue(Iterables.getOnlyElement(inventory).isEmpty());
CountDownLatch inventoryLatch = new CountDownLatch(2);
strategy.setNewInventoryLatch(inventoryLatch);
curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/bob", Ints.toByteArray(2287));
Assert.assertTrue(timing.awaitLatch(inventoryLatch));
strategy.setNewInventoryLatch(null);
verifyInventory(manager);
CountDownLatch deleteLatch = new CountDownLatch(1);
strategy.setDeadInventoryLatch(deleteLatch);
curator.delete().forPath("/inventory/billy/1");
Assert.assertTrue(timing.awaitLatch(deleteLatch));
strategy.setDeadInventoryLatch(null);
Assert.assertEquals(1, manager.getInventoryValue("billy").size());
Assert.assertEquals(2287, manager.getInventoryValue("billy").get("bob").intValue());
inventoryLatch = new CountDownLatch(1);
strategy.setNewInventoryLatch(inventoryLatch);
curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
Assert.assertTrue(timing.awaitLatch(inventoryLatch));
strategy.setNewInventoryLatch(null);
verifyInventory(manager);
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected) {
latch.countDown();
}
}
});
server.stop();
Assert.assertTrue(timing.awaitLatch(latch));
verifyInventory(manager);
// Wait a bit
Thread.sleep(50);
verifyInventory(manager);
}
use of org.apache.curator.framework.api.CuratorEvent in project heron by twitter.
the class CuratorStateManager method getNodeData.
@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
final SettableFuture<M> future = SettableFuture.create();
Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
BackgroundCallback cb = new BackgroundCallback() {
@Override
// we don't know what M is until runtime
@SuppressWarnings("unchecked")
public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
byte[] data;
if (event != null & (data = event.getData()) != null) {
builder.mergeFrom(data);
future.set((M) builder.build());
} else {
future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
}
}
};
try {
client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
// Suppress it since forPath() throws Exception
// SUPPRESS CHECKSTYLE IllegalCatch
} catch (Exception e) {
future.setException(new RuntimeException("Could not getNodeData", e));
}
return future;
}
Aggregations