Search in sources :

Example 1 with LogSegmentNamesListener

use of com.twitter.distributedlog.callback.LogSegmentNamesListener in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testRegisterListenerAfterLSMStoreClosed.

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

        @Override
        public void onSegmentsUpdated(List<String> segments) {
        // no-op;
        }
    });
    assertTrue("No listener is registered", lsmStore.listeners.isEmpty());
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(com.twitter.distributedlog.callback.LogSegmentNamesListener) Test(org.junit.Test)

Example 2 with LogSegmentNamesListener

use of com.twitter.distributedlog.callback.LogSegmentNamesListener in project distributedlog by twitter.

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);
    }
    FutureUtils.result(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(List<String> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments);
            numNotifications.incrementAndGet();
        }
    };
    lsmStore.registerLogSegmentListener(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).contains(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);
    }
    FutureUtils.result(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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(com.twitter.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Example 3 with LogSegmentNamesListener

use of com.twitter.distributedlog.callback.LogSegmentNamesListener in project distributedlog by twitter.

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);
    }
    FutureUtils.result(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(List<String> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments);
            numNotifications.incrementAndGet();
        }
    };
    lsmStore.registerLogSegmentListener(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).contains(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, DLUtils.getZKServersFromDLUri(uri), conf.getZKSessionTimeoutMilliseconds());
    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 same", children, secondSegmentList);
    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);
    }
    FutureUtils.result(anotherCreateTxn.execute());
    List<String> newChildren = zkc.get().getChildren(rootPath, false);
    Collections.sort(newChildren);
    logger.info("All log segments become {}", newChildren);
    while (numNotifications.get() < 3) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals("Should receive third segment list update", 3, numNotifications.get());
    List<String> thirdSegmentList = segmentLists.get(2);
    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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(com.twitter.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Example 4 with LogSegmentNamesListener

use of com.twitter.distributedlog.callback.LogSegmentNamesListener in project distributedlog by twitter.

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);
    }
    FutureUtils.result(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(List<String> segments) {
            logger.info("Received segments : {}", segments);
            segmentLists.add(segments);
            numNotifications.incrementAndGet();
        }
    };
    lsmStore.registerLogSegmentListener(rootPath, listener);
    assertEquals(1, lsmStore.listeners.size());
    assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
    assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).contains(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);
    }
    FutureUtils.result(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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) LogSegmentNamesListener(com.twitter.distributedlog.callback.LogSegmentNamesListener) List(java.util.List) Test(org.junit.Test)

Aggregations

LogSegmentMetadata (com.twitter.distributedlog.LogSegmentMetadata)4 LogSegmentNamesListener (com.twitter.distributedlog.callback.LogSegmentNamesListener)4 Test (org.junit.Test)4 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3