Search in sources :

Example 1 with NamespaceListener

use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.

the class TestZKNamespaceWatcher method testSessionExpired.

@Test(timeout = 60000)
public void testSessionExpired() throws Exception {
    URI uri = createDLMURI("/" + runtime.getMethodName());
    zkc.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    conf.addConfiguration(baseConf);
    ZKNamespaceWatcher watcher = new ZKNamespaceWatcher(conf, uri, zkc, scheduler);
    final CountDownLatch[] latches = new CountDownLatch[10];
    for (int i = 0; i < 10; i++) {
        latches[i] = new CountDownLatch(1);
    }
    final AtomicInteger numUpdates = new AtomicInteger(0);
    final AtomicReference<Set<String>> receivedLogs = new AtomicReference<Set<String>>(null);
    watcher.registerListener(new NamespaceListener() {

        @Override
        public void onStreamsChanged(Iterator<String> streams) {
            Set<String> streamSet = Sets.newHashSet(streams);
            int updates = numUpdates.incrementAndGet();
            receivedLogs.set(streamSet);
            latches[updates - 1].countDown();
        }
    });
    latches[0].await();
    createLogInNamespace(uri, "test1");
    latches[1].await();
    createLogInNamespace(uri, "test2");
    latches[2].await();
    assertEquals(2, receivedLogs.get().size());
    ZooKeeperClientUtils.expireSession(zkc, BKNamespaceDriver.getZKServersFromDLUri(uri), zkSessionTimeoutMs);
    latches[3].await();
    assertEquals(2, receivedLogs.get().size());
    createLogInNamespace(uri, "test3");
    latches[4].await();
    assertEquals(3, receivedLogs.get().size());
}
Also used : NamespaceListener(org.apache.distributedlog.callback.NamespaceListener) Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with NamespaceListener

use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.

the class TestZKNamespaceWatcher method testNamespaceListener.

@Test(timeout = 60000)
public void testNamespaceListener() throws Exception {
    URI uri = createDLMURI("/" + runtime.getMethodName());
    zkc.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    conf.addConfiguration(baseConf);
    ZKNamespaceWatcher watcher = new ZKNamespaceWatcher(conf, uri, zkc, scheduler);
    final CountDownLatch[] latches = new CountDownLatch[10];
    for (int i = 0; i < 10; i++) {
        latches[i] = new CountDownLatch(1);
    }
    final AtomicInteger numUpdates = new AtomicInteger(0);
    final AtomicReference<Set<String>> receivedLogs = new AtomicReference<Set<String>>(null);
    watcher.registerListener(new NamespaceListener() {

        @Override
        public void onStreamsChanged(Iterator<String> streams) {
            Set<String> streamSet = Sets.newHashSet(streams);
            int updates = numUpdates.incrementAndGet();
            receivedLogs.set(streamSet);
            latches[updates - 1].countDown();
        }
    });
    // first update
    final Set<String> expectedLogs = Sets.newHashSet();
    latches[0].await();
    validateReceivedLogs(expectedLogs, receivedLogs.get());
    // create test1
    expectedLogs.add("test1");
    createLogInNamespace(uri, "test1");
    latches[1].await();
    validateReceivedLogs(expectedLogs, receivedLogs.get());
    // create invalid log
    createLogInNamespace(uri, ".test1");
    latches[2].await();
    validateReceivedLogs(expectedLogs, receivedLogs.get());
    // create test2
    expectedLogs.add("test2");
    createLogInNamespace(uri, "test2");
    latches[3].await();
    validateReceivedLogs(expectedLogs, receivedLogs.get());
    // delete test1
    expectedLogs.remove("test1");
    deleteLogInNamespace(uri, "test1");
    latches[4].await();
    validateReceivedLogs(expectedLogs, receivedLogs.get());
}
Also used : NamespaceListener(org.apache.distributedlog.callback.NamespaceListener) Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 3 with NamespaceListener

use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.

the class TestBKDistributedLogNamespace method testNamespaceListener.

@Test(timeout = 60000)
public void testNamespaceListener() throws Exception {
    URI uri = createDLMURI("/" + runtime.getMethodName());
    zooKeeperClient.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    final CountDownLatch[] latches = new CountDownLatch[3];
    for (int i = 0; i < 3; i++) {
        latches[i] = new CountDownLatch(1);
    }
    final AtomicInteger numUpdates = new AtomicInteger(0);
    final AtomicInteger numFailures = new AtomicInteger(0);
    final AtomicReference<Collection<String>> receivedStreams = new AtomicReference<Collection<String>>(null);
    namespace.registerNamespaceListener(new NamespaceListener() {

        @Override
        public void onStreamsChanged(Iterator<String> streams) {
            Set<String> streamSet = Sets.newHashSet(streams);
            int updates = numUpdates.incrementAndGet();
            if (streamSet.size() != updates - 1) {
                numFailures.incrementAndGet();
            }
            receivedStreams.set(streamSet);
            latches[updates - 1].countDown();
        }
    });
    latches[0].await();
    namespace.createLog("test1");
    latches[1].await();
    namespace.createLog("test2");
    latches[2].await();
    assertEquals(0, numFailures.get());
    assertNotNull(receivedStreams.get());
    Set<String> streamSet = new HashSet<String>();
    streamSet.addAll(receivedStreams.get());
    assertEquals(2, receivedStreams.get().size());
    assertEquals(2, streamSet.size());
    assertTrue(streamSet.contains("test1"));
    assertTrue(streamSet.contains("test2"));
}
Also used : NamespaceListener(org.apache.distributedlog.callback.NamespaceListener) HashSet(java.util.HashSet) Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with NamespaceListener

use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.

the class ZKNamespaceWatcher method processResult.

@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
    if (KeeperException.Code.OK.intValue() == rc) {
        logger.info("Received updated logs under {} : {}", uri, children);
        List<String> result = new ArrayList<String>(children.size());
        for (String s : children) {
            if (isReservedStreamName(s)) {
                continue;
            }
            result.add(s);
        }
        for (NamespaceListener listener : listeners) {
            listener.onStreamsChanged(result.iterator());
        }
    } else {
        scheduleTask(this, conf.getZKSessionTimeoutMilliseconds());
    }
}
Also used : NamespaceListener(org.apache.distributedlog.callback.NamespaceListener) ArrayList(java.util.ArrayList)

Aggregations

NamespaceListener (org.apache.distributedlog.callback.NamespaceListener)4 URI (java.net.URI)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Test (org.junit.Test)3 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1