Search in sources :

Example 1 with LogSegmentNamesListener

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

the class TestZKLogSegmentMetadataStore method testRegisterListenerAfterLSMStoreClosed.

@Test(timeout = 60000)
public void testRegisterListenerAfterLSMStoreClosed() throws Exception {
    lsmStore.close();
    LogSegmentMetadata segment = createLogSegment(1L);
    lsmStore.getLogSegmentNames(segment.getZkPath(), new LogSegmentNamesListener() {

        @Override
        public void onSegmentsUpdated(Versioned<List<String>> segments) {
        // no-op;
        }

        @Override
        public void onLogStreamDeleted() {
        // no-op;
        }
    });
    assertTrue("No listener is registered", lsmStore.listeners.isEmpty());
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Example 2 with LogSegmentNamesListener

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

the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListener.

@Test(timeout = 60000)
public void testLogSegmentNamesListener() throws Exception {
    int numSegments = 3;
    Transaction<Object> createTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(createTxn, segment, null);
    }
    Utils.ioResult(createTxn.execute());
    String rootPath = "/" + runtime.getMethodName();
    List<String> children = zkc.get().getChildren(rootPath, false);
    Collections.sort(children);
    final AtomicInteger numNotifications = new AtomicInteger(0);
    final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
    LogSegmentNamesListener listener = new LogSegmentNamesListener() {

        @Override
        public void onSegmentsUpdated(Versioned<List<String>> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments.getValue());
            numNotifications.incrementAndGet();
        }

        @Override
        public void onLogStreamDeleted() {
        // no-op;
        }
    };
    lsmStore.getLogSegmentNames(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
    while (numNotifications.get() < 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive one segment list update", 1, numNotifications.get());
    List<String> firstSegmentList = segmentLists.get(0);
    Collections.sort(firstSegmentList);
    assertEquals("List of segments should be same", children, firstSegmentList);
    logger.info("Create another {} segments.", numSegments);
    // create another log segment, it should trigger segment list updated
    Transaction<Object> anotherCreateTxn = lsmStore.transaction();
    for (int i = numSegments; i < 2 * numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(anotherCreateTxn, segment, null);
    }
    Utils.ioResult(anotherCreateTxn.execute());
    List<String> newChildren = zkc.get().getChildren(rootPath, false);
    Collections.sort(newChildren);
    logger.info("All log segments become {}", newChildren);
    while (numNotifications.get() < 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive second segment list update", 2, numNotifications.get());
    List<String> secondSegmentList = segmentLists.get(1);
    Collections.sort(secondSegmentList);
    assertEquals("List of segments should be updated", 2 * numSegments, secondSegmentList.size());
    assertEquals("List of segments should be updated", newChildren, secondSegmentList);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Example 3 with LogSegmentNamesListener

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

the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListenerOnDeletion.

@Test(timeout = 60000)
public void testLogSegmentNamesListenerOnDeletion() throws Exception {
    int numSegments = 3;
    Transaction<Object> createTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(createTxn, segment, null);
    }
    Utils.ioResult(createTxn.execute());
    String rootPath = "/" + runtime.getMethodName();
    List<String> children = zkc.get().getChildren(rootPath, false);
    Collections.sort(children);
    final AtomicInteger numNotifications = new AtomicInteger(0);
    final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
    LogSegmentNamesListener listener = new LogSegmentNamesListener() {

        @Override
        public void onSegmentsUpdated(Versioned<List<String>> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments.getValue());
            numNotifications.incrementAndGet();
        }

        @Override
        public void onLogStreamDeleted() {
        // no-op;
        }
    };
    lsmStore.getLogSegmentNames(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
    while (numNotifications.get() < 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive one segment list update", 1, numNotifications.get());
    List<String> firstSegmentList = segmentLists.get(0);
    Collections.sort(firstSegmentList);
    assertEquals("List of segments should be same", children, firstSegmentList);
    // delete all log segments, it should trigger segment list updated
    Transaction<Object> deleteTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.deleteLogSegment(deleteTxn, segment, null);
    }
    Utils.ioResult(deleteTxn.execute());
    List<String> newChildren = zkc.get().getChildren(rootPath, false);
    Collections.sort(newChildren);
    while (numNotifications.get() < 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive second segment list update", 2, numNotifications.get());
    List<String> secondSegmentList = segmentLists.get(1);
    Collections.sort(secondSegmentList);
    assertEquals("List of segments should be updated", 0, secondSegmentList.size());
    assertEquals("List of segments should be updated", newChildren, secondSegmentList);
    // delete the root path
    zkc.get().delete(rootPath, -1);
    while (!lsmStore.listeners.isEmpty()) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertTrue("listener should be removed after root path is deleted", lsmStore.listeners.isEmpty());
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Example 4 with LogSegmentNamesListener

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

the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListenerOnDeletingLogStream.

@Test(timeout = 60000)
public void testLogSegmentNamesListenerOnDeletingLogStream() throws Exception {
    int numSegments = 3;
    Transaction<Object> createTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(createTxn, segment, null);
    }
    Utils.ioResult(createTxn.execute());
    String rootPath = "/" + runtime.getMethodName();
    List<String> children = zkc.get().getChildren(rootPath, false);
    Collections.sort(children);
    final AtomicInteger numNotifications = new AtomicInteger(0);
    final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
    final CountDownLatch deleteLatch = new CountDownLatch(1);
    LogSegmentNamesListener listener = new LogSegmentNamesListener() {

        @Override
        public void onSegmentsUpdated(Versioned<List<String>> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments.getValue());
            numNotifications.incrementAndGet();
        }

        @Override
        public void onLogStreamDeleted() {
            deleteLatch.countDown();
        }
    };
    lsmStore.getLogSegmentNames(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
    while (numNotifications.get() < 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive one segment list update", 1, numNotifications.get());
    List<String> firstSegmentList = segmentLists.get(0);
    Collections.sort(firstSegmentList);
    assertEquals("List of segments should be same", children, firstSegmentList);
    // delete all log segments, it should trigger segment list updated
    Transaction<Object> deleteTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.deleteLogSegment(deleteTxn, segment, null);
    }
    Utils.ioResult(deleteTxn.execute());
    List<String> newChildren = zkc.get().getChildren(rootPath, false);
    Collections.sort(newChildren);
    while (numNotifications.get() < 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive second segment list update", 2, numNotifications.get());
    List<String> secondSegmentList = segmentLists.get(1);
    Collections.sort(secondSegmentList);
    assertEquals("List of segments should be updated", 0, secondSegmentList.size());
    assertEquals("List of segments should be updated", newChildren, secondSegmentList);
    // delete the root path
    zkc.get().delete(rootPath, -1);
    while (!lsmStore.listeners.isEmpty()) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertTrue("listener should be removed after root path is deleted", lsmStore.listeners.isEmpty());
    deleteLatch.await();
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with LogSegmentNamesListener

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

the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListenerOnSessionExpired.

@Test(timeout = 60000)
public void testLogSegmentNamesListenerOnSessionExpired() throws Exception {
    int numSegments = 3;
    Transaction<Object> createTxn = lsmStore.transaction();
    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(createTxn, segment, null);
    }
    Utils.ioResult(createTxn.execute());
    String rootPath = "/" + runtime.getMethodName();
    List<String> children = zkc.get().getChildren(rootPath, false);
    Collections.sort(children);
    final AtomicInteger numNotifications = new AtomicInteger(0);
    final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
    LogSegmentNamesListener listener = new LogSegmentNamesListener() {

        @Override
        public void onSegmentsUpdated(Versioned<List<String>> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments.getValue());
            numNotifications.incrementAndGet();
        }

        @Override
        public void onLogStreamDeleted() {
        // no-op;
        }
    };
    lsmStore.getLogSegmentNames(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
    while (numNotifications.get() < 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive one segment list update", 1, numNotifications.get());
    List<String> firstSegmentList = segmentLists.get(0);
    Collections.sort(firstSegmentList);
    assertEquals("List of segments should be same", children, firstSegmentList);
    ZooKeeperClientUtils.expireSession(zkc, BKNamespaceDriver.getZKServersFromDLUri(uri), conf.getZKSessionTimeoutMilliseconds());
    logger.info("Create another {} segments.", numSegments);
    // create another log segment, it should trigger segment list updated
    Transaction<Object> anotherCreateTxn = lsmStore.transaction();
    for (int i = numSegments; i < 2 * numSegments; i++) {
        LogSegmentMetadata segment = createLogSegment(i);
        lsmStore.createLogSegment(anotherCreateTxn, segment, null);
    }
    Utils.ioResult(anotherCreateTxn.execute());
    List<String> newChildren = zkc.get().getChildren(rootPath, false);
    Collections.sort(newChildren);
    logger.info("All log segments become {}", newChildren);
    while (numNotifications.get() < 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive third segment list update", 2, numNotifications.get());
    List<String> thirdSegmentList = segmentLists.get(1);
    Collections.sort(thirdSegmentList);
    assertEquals("List of segments should be updated", 2 * numSegments, thirdSegmentList.size());
    assertEquals("List of segments should be updated", newChildren, thirdSegmentList);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Aggregations

LogSegmentNamesListener (org.apache.distributedlog.callback.LogSegmentNamesListener)6 List (java.util.List)5 Versioned (org.apache.bookkeeper.versioning.Versioned)5 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)5 Test (org.junit.Test)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Watcher (org.apache.zookeeper.Watcher)1