Search in sources :

Example 36 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testDeleteLogSegment.

@Test(timeout = 60000)
public void testDeleteLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment + " should be created", zkc.get().exists(segment.getZkPath(), false));
    Transaction<Object> deleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(deleteTxn, segment, null);
    Utils.ioResult(deleteTxn.execute());
    assertNull("LogSegment " + segment + " should be deleted", zkc.get().exists(segment.getZkPath(), false));
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 37 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata 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 38 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata 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)

Example 39 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

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, null);
    try {
        Utils.ioResult(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(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 40 with LogSegmentMetadata

use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testUpdateLogSegment.

@Test(timeout = 60000)
public void testUpdateLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L, 99L);
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment + " should be created", zkc.get().exists(segment.getZkPath(), false));
    LogSegmentMetadata modifiedSegment = createLogSegment(1L, 999L);
    Transaction<Object> updateTxn = lsmStore.transaction();
    lsmStore.updateLogSegment(updateTxn, modifiedSegment);
    Utils.ioResult(updateTxn.execute());
    // the log segment should be updated
    LogSegmentMetadata readSegment = Utils.ioResult(LogSegmentMetadata.read(zkc, segment.getZkPath(), true));
    assertEquals("Last entry id should be changed from 99L to 999L", 999L, readSegment.getLastEntryId());
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Aggregations

LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)50 Test (org.junit.Test)31 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)12 List (java.util.List)9 DLSN (org.apache.distributedlog.DLSN)9 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)9 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)8 Entry (org.apache.distributedlog.Entry)7 Versioned (org.apache.bookkeeper.versioning.Versioned)5 LogSegmentNamesListener (org.apache.distributedlog.callback.LogSegmentNamesListener)5 ZKException (org.apache.distributedlog.exceptions.ZKException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)2 DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)2