Search in sources :

Example 31 with LogSegmentMetadata

use of com.twitter.distributedlog.LogSegmentMetadata in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testCreateDeleteLogSegmentSuccess.

@Test(timeout = 60000)
public void testCreateDeleteLogSegmentSuccess() throws Exception {
    LogSegmentMetadata segment1 = createLogSegment(1L);
    LogSegmentMetadata segment2 = createLogSegment(2L);
    // create log segment 1
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment1);
    FutureUtils.result(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment1 + " should be created", zkc.get().exists(segment1.getZkPath(), false));
    // delete log segment 1 and create log segment 2
    Transaction<Object> createDeleteTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createDeleteTxn, segment2);
    lsmStore.deleteLogSegment(createDeleteTxn, segment1);
    FutureUtils.result(createDeleteTxn.execute());
    // segment 1 should be deleted, segment 2 should be created
    assertNull("LogSegment " + segment1 + " should be deleted", zkc.get().exists(segment1.getZkPath(), false));
    assertNotNull("LogSegment " + segment2 + " should be created", zkc.get().exists(segment2.getZkPath(), false));
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 32 with LogSegmentMetadata

use of com.twitter.distributedlog.LogSegmentMetadata in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testDeleteNonExistentLogSegment.

@Test(timeout = 60000)
public void testDeleteNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> deleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(deleteTxn, segment);
    try {
        FutureUtils.result(deleteTxn.execute());
        fail("Should fail deletion if log segment doesn't exist");
    } catch (Throwable t) {
        assertTrue("Should throw NoNodeException if log segment doesn't exist", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NoNodeException if log segment doesn't exist", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 33 with LogSegmentMetadata

use of com.twitter.distributedlog.LogSegmentMetadata 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 34 with LogSegmentMetadata

use of com.twitter.distributedlog.LogSegmentMetadata in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testUpdateNonExistentLogSegment.

@Test(timeout = 60000)
public void testUpdateNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> updateTxn = lsmStore.transaction();
    lsmStore.updateLogSegment(updateTxn, segment);
    try {
        FutureUtils.result(updateTxn.execute());
        fail("Should fail update if log segment doesn't exist");
    } catch (Throwable t) {
        assertTrue("Should throw NoNodeException if log segment doesn't exist", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NoNodeException if log segment doesn't exist", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 35 with LogSegmentMetadata

use of com.twitter.distributedlog.LogSegmentMetadata 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)40 Test (org.junit.Test)22 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)6 LogSegmentNamesListener (com.twitter.distributedlog.callback.LogSegmentNamesListener)4 ZKException (com.twitter.distributedlog.exceptions.ZKException)4 IOException (java.io.IOException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DLSN (com.twitter.distributedlog.DLSN)3 HashMap (java.util.HashMap)3 List (java.util.List)3 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)2 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)2 Future (com.twitter.util.Future)2 ArrayList (java.util.ArrayList)2 BookKeeper (org.apache.bookkeeper.client.BookKeeper)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 Stopwatch (com.google.common.base.Stopwatch)1 BookKeeperClient (com.twitter.distributedlog.BookKeeperClient)1 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 ZooKeeperClient (com.twitter.distributedlog.ZooKeeperClient)1