Search in sources :

Example 6 with LogRecordWithDLSN

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

the class TestLogSegmentMetadataStoreUpdater method testUpdateLastDLSN.

@Test(timeout = 60000)
public void testUpdateLastDLSN() throws Exception {
    String ledgerPath = "/testUpdateLastDLSN";
    zkc.get().create(ledgerPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Create 1 completed log segment
    LogSegmentMetadata completedLogSegment = DLMTestUtil.completedLogSegment(ledgerPath, 1L, 0L, 99L, 100, 1L, 99L, 0L);
    completedLogSegment.write(zkc);
    // Create 1 inprogress log segment
    LogSegmentMetadata inprogressLogSegment = DLMTestUtil.inprogressLogSegment(ledgerPath, 2L, 100L, 2L);
    inprogressLogSegment.write(zkc);
    DLSN badLastDLSN = new DLSN(99L, 0L, 0L);
    DLSN goodLastDLSN1 = new DLSN(1L, 100L, 0L);
    DLSN goodLastDLSN2 = new DLSN(2L, 200L, 0L);
    LogRecordWithDLSN badRecord = DLMTestUtil.getLogRecordWithDLSNInstance(badLastDLSN, 100L);
    LogRecordWithDLSN goodRecord1 = DLMTestUtil.getLogRecordWithDLSNInstance(goodLastDLSN1, 100L);
    LogRecordWithDLSN goodRecord2 = DLMTestUtil.getLogRecordWithDLSNInstance(goodLastDLSN2, 200L);
    // Dryrun
    MetadataUpdater dryrunUpdater = new DryrunLogSegmentMetadataStoreUpdater(conf, metadataStore);
    try {
        FutureUtils.result(dryrunUpdater.updateLastRecord(completedLogSegment, badRecord));
        fail("Should fail on updating dlsn that in different log segment");
    } catch (IllegalArgumentException iae) {
    // expected
    }
    try {
        FutureUtils.result(dryrunUpdater.updateLastRecord(inprogressLogSegment, goodRecord2));
        fail("Should fail on updating dlsn for an inprogress log segment");
    } catch (IllegalStateException ise) {
    // expected
    }
    LogSegmentMetadata updatedCompletedLogSegment = FutureUtils.result(dryrunUpdater.updateLastRecord(completedLogSegment, goodRecord1));
    assertEquals(goodLastDLSN1, updatedCompletedLogSegment.getLastDLSN());
    assertEquals(goodRecord1.getTransactionId(), updatedCompletedLogSegment.getLastTxId());
    assertTrue(updatedCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
    Map<Long, LogSegmentMetadata> segmentList = readLogSegments(ledgerPath);
    assertEquals(2, segmentList.size());
    LogSegmentMetadata readCompletedLogSegment = segmentList.get(1L);
    assertNotNull(readCompletedLogSegment);
    assertEquals(completedLogSegment, readCompletedLogSegment);
    LogSegmentMetadata readInprogressLogSegment = segmentList.get(2L);
    assertNotNull(readInprogressLogSegment);
    assertEquals(inprogressLogSegment, readInprogressLogSegment);
    // Fix the last dlsn
    MetadataUpdater updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
    try {
        FutureUtils.result(updater.updateLastRecord(completedLogSegment, badRecord));
        fail("Should fail on updating dlsn that in different log segment");
    } catch (IllegalArgumentException iae) {
    // expected
    }
    try {
        FutureUtils.result(updater.updateLastRecord(inprogressLogSegment, goodRecord2));
        fail("Should fail on updating dlsn for an inprogress log segment");
    } catch (IllegalStateException ise) {
    // expected
    }
    updatedCompletedLogSegment = FutureUtils.result(updater.updateLastRecord(completedLogSegment, goodRecord1));
    assertEquals(goodLastDLSN1, updatedCompletedLogSegment.getLastDLSN());
    assertEquals(goodRecord1.getTransactionId(), updatedCompletedLogSegment.getLastTxId());
    assertTrue(updatedCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
    segmentList = readLogSegments(ledgerPath);
    assertEquals(2, segmentList.size());
    readCompletedLogSegment = segmentList.get(1L);
    assertNotNull(readCompletedLogSegment);
    assertEquals(goodLastDLSN1, readCompletedLogSegment.getLastDLSN());
    assertEquals(goodRecord1.getTransactionId(), readCompletedLogSegment.getLastTxId());
    assertTrue(readCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
    assertEquals(updatedCompletedLogSegment, readCompletedLogSegment);
    assertEquals(completedLogSegment.getCompletionTime(), readCompletedLogSegment.getCompletionTime());
    assertEquals(completedLogSegment.getFirstTxId(), readCompletedLogSegment.getFirstTxId());
    assertEquals(completedLogSegment.getLedgerId(), readCompletedLogSegment.getLedgerId());
    assertEquals(completedLogSegment.getLogSegmentSequenceNumber(), readCompletedLogSegment.getLogSegmentSequenceNumber());
    assertEquals(completedLogSegment.getRegionId(), readCompletedLogSegment.getRegionId());
    assertEquals(completedLogSegment.getZkPath(), readCompletedLogSegment.getZkPath());
    assertEquals(completedLogSegment.getZNodeName(), readCompletedLogSegment.getZNodeName());
    readInprogressLogSegment = segmentList.get(2L);
    assertNotNull(readInprogressLogSegment);
    assertEquals(inprogressLogSegment, readInprogressLogSegment);
}
Also used : LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Aggregations

DLSN (com.twitter.distributedlog.DLSN)6 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)6 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)4 Test (org.junit.Test)4 AsyncLogReader (com.twitter.distributedlog.AsyncLogReader)3 LogReader (com.twitter.distributedlog.LogReader)2 LogSegmentMetadata (com.twitter.distributedlog.LogSegmentMetadata)2 Stopwatch (com.google.common.base.Stopwatch)1 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 LogRecord (com.twitter.distributedlog.LogRecord)1 DryrunLogSegmentMetadataStoreUpdater (com.twitter.distributedlog.metadata.DryrunLogSegmentMetadataStoreUpdater)1 TimeoutException (com.twitter.util.TimeoutException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Counter (org.apache.bookkeeper.stats.Counter)1 OpStatsLogger (org.apache.bookkeeper.stats.OpStatsLogger)1 Ignore (org.junit.Ignore)1