Search in sources :

Example 6 with LogSegmentMetadata

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

the class TestLogSegmentCache method testGapDetectionOnLogSegmentsWithoutLogSegmentSequenceNumber.

@Test(timeout = 60000)
public void testGapDetectionOnLogSegmentsWithoutLogSegmentSequenceNumber() throws Exception {
    LogSegmentCache cache = new LogSegmentCache("test-gap-detection");
    LogSegmentMetadata segment1 = DLMTestUtil.completedLogSegment("/segment-1", 1L, 1L, 100L, 100, 1L, 99L, 0L).mutator().setVersion(LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V1_ORIGINAL).build();
    cache.add(DLMTestUtil.completedLedgerZNodeNameWithLogSegmentSequenceNumber(1L), segment1);
    LogSegmentMetadata segment3 = DLMTestUtil.completedLogSegment("/segment-3", 3L, 3L, 300L, 100, 3L, 99L, 0L).mutator().setVersion(LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V2_LEDGER_SEQNO).build();
    cache.add(DLMTestUtil.completedLedgerZNodeNameWithLogSegmentSequenceNumber(3L), segment3);
    List<LogSegmentMetadata> expectedList = Lists.asList(segment1, new LogSegmentMetadata[] { segment3 });
    List<LogSegmentMetadata> resultList = cache.getLogSegments(LogSegmentMetadata.COMPARATOR);
    assertEquals(expectedList, resultList);
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 7 with LogSegmentMetadata

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

the class TestLogSegmentCache method testBasicOperations.

@Test(timeout = 60000)
public void testBasicOperations() {
    LogSegmentMetadata metadata = DLMTestUtil.completedLogSegment("/segment1", 1L, 1L, 100L, 100, 1L, 99L, 0L);
    String name = DLMTestUtil.completedLedgerZNodeNameWithLogSegmentSequenceNumber(1L);
    LogSegmentCache cache = new LogSegmentCache("test-basic-operations");
    assertNull("No log segment " + name + " should be cached", cache.get(name));
    cache.add(name, metadata);
    LogSegmentMetadata metadataRetrieved = cache.get(name);
    assertNotNull("log segment " + name + " should be cached", metadataRetrieved);
    assertEquals("Wrong log segment metadata returned for " + name, metadata, metadataRetrieved);
    LogSegmentMetadata metadataRemoved = cache.remove(name);
    assertNull("log segment " + name + " should be removed from cache", cache.get(name));
    assertEquals("Wrong log segment metadata removed for " + name, metadata, metadataRemoved);
    assertNull("No log segment " + name + " to be removed", cache.remove(name));
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 8 with LogSegmentMetadata

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

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);
    FutureUtils.result(dryrunUpdater.changeSequenceNumber(segment, 6L));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(5, segmentList.size());
    // Fix the inprogress log segments
    MetadataUpdater updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    FutureUtils.result(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.getLedgerId());
}
Also used : HashMap(java.util.HashMap) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 9 with LogSegmentMetadata

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

the class TestLogSegmentMetadataStoreUpdater method testChangeTruncationStatus.

@Test
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);
    FutureUtils.result(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);
    FutureUtils.result(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);
    FutureUtils.result(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);
    FutureUtils.result(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);
    FutureUtils.result(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(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 10 with LogSegmentMetadata

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

the class LogSegmentCache method remove.

/**
     * Remove log segment <code>name</code> from the cache.
     *
     * @param name
     *          name of the log segment.
     * @return log segment metadata.
     */
public LogSegmentMetadata remove(String name) {
    synchronized (logSegments) {
        LogSegmentMetadata metadata = logSegments.remove(name);
        if (null != metadata) {
            lid2LogSegments.remove(metadata.getLedgerId(), metadata);
            LOG.debug("Removed log segment ({} : {}) from cache.", name, metadata);
        }
        return metadata;
    }
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata)

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