Search in sources :

Example 56 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project heron by twitter.

the class CuratorStateManagerTest method testGetNodeData.

/**
 * Test getNodeData method
 * @throws Exception
 */
@Test
public void testGetNodeData() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    final CuratorFramework mockClient = mock(CuratorFramework.class);
    GetDataBuilder mockGetBuilder = mock(GetDataBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
    final CuratorEvent mockEvent = mock(CuratorEvent.class);
    Message.Builder mockBuilder = mock(Message.Builder.class);
    Message mockMessage = mock(Message.class);
    final byte[] data = "wy_1989".getBytes();
    doReturn(mockMessage).when(mockBuilder).build();
    doReturn(data).when(mockEvent).getData();
    doReturn(PATH).when(mockEvent).getPath();
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockGetBuilder).when(mockClient).getData();
    doReturn(mockBackPathable).when(mockGetBuilder).usingWatcher(any(Watcher.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objests = invocationOnMock.getArguments();
            // the first object is the BackgroundCallback
            ((BackgroundCallback) objests[0]).processResult(mockClient, mockEvent);
            return null;
        }
    }).when(mockBackPathable).inBackground(any(BackgroundCallback.class));
    spyStateManager.initialize(config);
    // Verify the data on node is fetched correctly
    ListenableFuture<Message> result = spyStateManager.getNodeData(null, PATH, mockBuilder);
    assertTrue(result.get().equals(mockMessage));
}
Also used : Message(com.google.protobuf.Message) Watcher(org.apache.zookeeper.Watcher) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) GetDataBuilder(org.apache.curator.framework.api.GetDataBuilder) CuratorFramework(org.apache.curator.framework.CuratorFramework) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TimeUnit(java.util.concurrent.TimeUnit) BackgroundPathable(org.apache.curator.framework.api.BackgroundPathable) Test(org.junit.Test)

Example 57 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.

the class AnnouncerTest method testSessionKilled.

@Test(timeout = 60_000L)
public void testSessionKilled() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    try {
        curator.inTransaction().create().forPath("/somewhere").and().commit();
        announcer.start();
        final byte[] billy = StringUtils.toUtf8("billy");
        final String testPath1 = "/test1";
        final String testPath2 = "/somewhere/test2";
        final Set<String> paths = Sets.newHashSet(testPath1, testPath2);
        announcer.announce(testPath1, billy);
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(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) {
                if (event.getType() == CuratorEventType.CREATE) {
                    paths.remove(event.getPath());
                    if (paths.isEmpty()) {
                        latch.countDown();
                    }
                }
            }
        });
        KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
        announcer.stop();
        while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) {
            Thread.sleep(100);
        }
        Assert.assertNull(curator.checkExists().forPath(testPath1));
        Assert.assertNull(curator.checkExists().forPath(testPath2));
    } finally {
        announcer.stop();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with CuratorEvent

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) {
            if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(path)) {
                latch.countDown();
            }
        }
    });
    announcer.announce(path, bytes, removeParentsIfCreated);
    latch.await();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 59 with CuratorEvent

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);
    announcer.initializeAddedChildren();
    final byte[] billy = StringUtils.toUtf8("billy");
    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();
    while (!announcer.getAddedChildren().contains("/test1")) {
        Thread.sleep(100);
    }
    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) {
                if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) {
                    latch.countDown();
                }
            }
        });
        final CuratorOp deleteOp = curator.transactionOp().delete().forPath(testPath1);
        final Collection<CuratorTransactionResult> results = curator.transaction().forOperations(deleteOp);
        Assert.assertEquals(1, results.size());
        final CuratorTransactionResult result = results.iterator().next();
        // assert delete
        Assert.assertEquals(Code.OK.intValue(), result.getError());
        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));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorOp(org.apache.curator.framework.api.transaction.CuratorOp) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.junit.Test)

Example 60 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project Singularity by HubSpot.

the class CuratorAsyncManager method getAsyncThrows.

private <T> Map<String, T> getAsyncThrows(final String pathNameForLogs, final Collection<String> paths, final Transcoder<T> transcoder, final Optional<ZkCache<T>> cache) throws Exception {
    final Map<String, T> objects = new HashMap<>(paths.size());
    if (cache.isPresent()) {
        for (Iterator<String> itr = paths.iterator(); itr.hasNext(); ) {
            final String path = itr.next();
            final Optional<T> fromCache = cache.get().get(path);
            if (fromCache.isPresent()) {
                objects.put(path, fromCache.get());
                itr.remove();
            }
        }
    }
    if (paths.isEmpty()) {
        return objects;
    }
    final Map<String, T> synchronizedObjects = Collections.synchronizedMap(objects);
    final CountDownLatch latch = new CountDownLatch(paths.size());
    final AtomicInteger bytes = new AtomicInteger();
    final BackgroundCallback callback = new BackgroundCallback() {

        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            try {
                if (event.getData() == null || event.getData().length == 0) {
                    LOG.trace("Expected active node {} but it wasn't there", event.getPath());
                    return;
                }
                bytes.getAndAdd(event.getData().length);
                final T object = transcoder.fromBytes(event.getData());
                synchronizedObjects.put(event.getPath(), object);
                if (cache.isPresent()) {
                    cache.get().set(event.getPath(), object);
                }
            } catch (Exception e) {
                LOG.error("Exception processing curator result", e);
            } finally {
                latch.countDown();
            }
        }
    };
    return queryAndReturnResultsThrows(objects, paths, callback, latch, pathNameForLogs, bytes, CuratorQueryMethod.GET_DATA);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)60 CuratorFramework (org.apache.curator.framework.CuratorFramework)57 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)36 CountDownLatch (java.util.concurrent.CountDownLatch)34 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 Test (org.testng.annotations.Test)21 RetryOneTime (org.apache.curator.retry.RetryOneTime)18 KeeperException (org.apache.zookeeper.KeeperException)14 Test (org.junit.Test)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Watcher (org.apache.zookeeper.Watcher)8 ArrayList (java.util.ArrayList)4 Timing (org.apache.curator.test.Timing)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (com.google.protobuf.Message)3 IOException (java.io.IOException)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3