Search in sources :

Example 16 with LogSegmentMetadata

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

the class TestLogSegmentMetadataStoreUpdater method testChangeSequenceNumber.

@Test(timeout = 60000)
public void testChangeSequenceNumber() throws Exception {
    String ledgerPath = "/testChangeSequenceNumber";
    zkc.get().create(ledgerPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Map<Long, LogSegmentMetadata> completedLogSegments = new HashMap<Long, LogSegmentMetadata>();
    // Create 5 completed log segments
    for (int i = 1; i <= 5; i++) {
        LogSegmentMetadata segment = DLMTestUtil.completedLogSegment(ledgerPath, i, (i - 1) * 100, i * 100 - 1, 100, i, 100, 0);
        completedLogSegments.put(((long) i), segment);
        LOG.info("Create completed segment {} : {}", segment.getZkPath(), segment);
        segment.write(zkc);
    }
    // Create a smaller inprogress log segment
    long inprogressSeqNo = 3;
    LogSegmentMetadata segment = DLMTestUtil.inprogressLogSegment(ledgerPath, inprogressSeqNo, 5 * 100, inprogressSeqNo);
    LOG.info("Create inprogress segment {} : {}", segment.getZkPath(), segment);
    segment.write(zkc);
    Map<Long, LogSegmentMetadata> segmentList = readLogSegments(ledgerPath);
    assertEquals(5, segmentList.size());
    // Dryrun
    MetadataUpdater dryrunUpdater = new DryrunLogSegmentMetadataStoreUpdater(conf, metadataStore);
    Utils.ioResult(dryrunUpdater.changeSequenceNumber(segment, 6L));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(5, segmentList.size());
    // Fix the inprogress log segments
    MetadataUpdater updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    Utils.ioResult(updater.changeSequenceNumber(segment, 6L));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(6, segmentList.size());
    // check first 5 log segments
    for (int i = 1; i <= 5; i++) {
        LogSegmentMetadata s = segmentList.get((long) i);
        assertNotNull(s);
        assertEquals(completedLogSegments.get((long) i), s);
    }
    // get log segment 6
    LogSegmentMetadata segmentChanged = segmentList.get(6L);
    assertNotNull(segmentChanged);
    assertEquals(6L, segmentChanged.getLogSegmentSequenceNumber());
    assertTrue(segmentChanged.isInProgress());
    assertEquals(5 * 100, segmentChanged.getFirstTxId());
    assertEquals(3L, segmentChanged.getLogSegmentId());
}
Also used : HashMap(java.util.HashMap) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 17 with LogSegmentMetadata

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

the class TestLogSegmentMetadataStoreUpdater method testChangeTruncationStatus.

@Test(timeout = 60000)
public void testChangeTruncationStatus() throws Exception {
    String ledgerPath = "/ledgers2";
    zkc.get().create(ledgerPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Map<Long, LogSegmentMetadata> completedLogSegments = new HashMap<Long, LogSegmentMetadata>();
    // Create 5 completed log segments
    for (int i = 1; i <= 5; i++) {
        LogSegmentMetadata segment = DLMTestUtil.completedLogSegment(ledgerPath, i, (i - 1) * 100, i * 100 - 1, 100, i, 100, 0);
        completedLogSegments.put(((long) i), segment);
        LOG.info("Create completed segment {} : {}", segment.getZkPath(), segment);
        segment.write(zkc);
    }
    Map<Long, LogSegmentMetadata> segmentList = readLogSegments(ledgerPath);
    assertEquals(5, segmentList.size());
    long segmentToModify = 1L;
    // Dryrun
    MetadataUpdater dryrunUpdater = new DryrunLogSegmentMetadataStoreUpdater(conf, metadataStore);
    Utils.ioResult(dryrunUpdater.setLogSegmentTruncated(segmentList.get(segmentToModify)));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(false, segmentList.get(segmentToModify).isTruncated());
    // change truncation for the 1st log segment
    MetadataUpdater updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    Utils.ioResult(updater.setLogSegmentTruncated(segmentList.get(segmentToModify)));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(true, segmentList.get(segmentToModify).isTruncated());
    assertEquals(false, segmentList.get(segmentToModify).isPartiallyTruncated());
    updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    Utils.ioResult(updater.setLogSegmentActive(segmentList.get(segmentToModify)));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(false, segmentList.get(segmentToModify).isTruncated());
    assertEquals(false, segmentList.get(segmentToModify).isPartiallyTruncated());
    updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    Utils.ioResult(updater.setLogSegmentPartiallyTruncated(segmentList.get(segmentToModify), segmentList.get(segmentToModify).getFirstDLSN()));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(false, segmentList.get(segmentToModify).isTruncated());
    assertEquals(true, segmentList.get(segmentToModify).isPartiallyTruncated());
    updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    Utils.ioResult(updater.setLogSegmentActive(segmentList.get(segmentToModify)));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(false, segmentList.get(segmentToModify).isTruncated());
    assertEquals(false, segmentList.get(segmentToModify).isPartiallyTruncated());
}
Also used : HashMap(java.util.HashMap) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 18 with LogSegmentMetadata

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

the class TestZKLogSegmentMetadataStore method testCreateLogSegment.

@Test(timeout = 60000)
public void testCreateLogSegment() 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));
    LogSegmentMetadata segment2 = createLogSegment(1L);
    Transaction<Object> createTxn2 = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn2, segment2, null);
    try {
        Utils.ioResult(createTxn2.execute());
        fail("Should fail if log segment exists");
    } catch (Throwable t) {
        // expected
        assertTrue("Should throw NodeExistsException if log segment exists", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NodeExistsException if log segment exists", KeeperException.Code.NODEEXISTS, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 19 with LogSegmentMetadata

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

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

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 {
        Utils.ioResult(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(org.apache.distributedlog.exceptions.ZKException) 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