Search in sources :

Example 11 with DLSN

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

the class TestBKLogSegmentEntryReader method testReadEntriesOnStateChange.

@Test(timeout = 60000)
public void testReadEntriesOnStateChange() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(20);
    confLocal.setMaxPrefetchEntriesPerLogSegment(20);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    AsyncLogWriter writer = createInprogressLogSegment(dlm, confLocal, 5);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    long expectedLastAddConfirmed = 8L;
    // wait until sending out all prefetch requests
    while (reader.readAheadEntries.size() < expectedLastAddConfirmed + 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(expectedLastAddConfirmed + 2, reader.getNextEntryId());
    long txId = 1L;
    long entryId = 0L;
    while (true) {
        Entry.Reader entryReader = Utils.ioResult(reader.readNext(1)).get(0);
        LogRecordWithDLSN record = entryReader.nextRecord();
        while (null != record) {
            if (!record.isControl()) {
                DLMTestUtil.verifyLogRecord(record);
                assertEquals(txId, record.getTransactionId());
                ++txId;
            }
            DLSN dlsn = record.getDlsn();
            assertEquals(1L, dlsn.getLogSegmentSequenceNo());
            assertEquals(entryId, dlsn.getEntryId());
            record = entryReader.nextRecord();
        }
        ++entryId;
        if (entryId == expectedLastAddConfirmed + 1) {
            break;
        }
    }
    assertEquals(6L, txId);
    CompletableFuture<List<Entry.Reader>> nextReadFuture = reader.readNext(1);
    // write another record to commit previous writes
    Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txId)));
    // the long poll will be satisfied
    List<Entry.Reader> nextReadEntries = Utils.ioResult(nextReadFuture);
    assertEquals(1, nextReadEntries.size());
    Entry.Reader entryReader = nextReadEntries.get(0);
    LogRecordWithDLSN record = entryReader.nextRecord();
    assertNotNull(record);
    assertTrue(record.isControl());
    assertNull(entryReader.nextRecord());
    // once the read is advanced, we will prefetch next record
    while (reader.getNextEntryId() <= entryId) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(entryId + 2, reader.getNextEntryId());
    assertEquals(1, reader.readAheadEntries.size());
    // advance the entry id
    ++entryId;
    // close the writer, the write will be committed
    Utils.close(writer);
    entryReader = Utils.ioResult(reader.readNext(1)).get(0);
    record = entryReader.nextRecord();
    assertNotNull(record);
    assertFalse(record.isControl());
    assertNull(entryReader.nextRecord());
    while (reader.getNextEntryId() <= entryId + 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(entryId + 2, reader.getNextEntryId());
    assertEquals(1, reader.readAheadEntries.size());
    // get the new log segment
    List<LogSegmentMetadata> newSegments = dlm.getLogSegments();
    assertEquals(1, newSegments.size());
    assertFalse(newSegments.get(0).isInProgress());
    reader.onLogSegmentMetadataUpdated(newSegments.get(0));
    // should be cancelled and end of log segment should be signaled correctly
    try {
        // when we closed the log segment, another control record will be
        // written, so we loop over the reader until we reach end of log segment.
        Utils.ioResult(reader.readNext(1));
        Utils.ioResult(reader.readNext(1));
        fail("Should reach end of log segment");
    } catch (EndOfLogSegmentException eol) {
    // expected
    }
    Utils.close(reader);
}
Also used : LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) EndOfLogSegmentException(org.apache.distributedlog.exceptions.EndOfLogSegmentException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) List(java.util.List) Test(org.junit.Test)

Example 12 with DLSN

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

the class TestBKLogSegmentEntryReader method testMaxPrefetchEntriesSmallSegment.

@Test(timeout = 60000)
public void testMaxPrefetchEntriesSmallSegment() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(10);
    confLocal.setMaxPrefetchEntriesPerLogSegment(20);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    generateCompletedLogSegments(dlm, confLocal, 1, 5);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    // wait for the read ahead entries to become available
    while (reader.readAheadEntries.size() < (reader.getLastAddConfirmed() + 1)) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    long txId = 1L;
    long entryId = 0L;
    assertEquals((reader.getLastAddConfirmed() + 1), reader.readAheadEntries.size());
    assertEquals((reader.getLastAddConfirmed() + 1), reader.getNextEntryId());
    // read first entry
    Entry.Reader entryReader = Utils.ioResult(reader.readNext(1)).get(0);
    LogRecordWithDLSN record = entryReader.nextRecord();
    while (null != record) {
        if (!record.isControl()) {
            DLMTestUtil.verifyLogRecord(record);
            assertEquals(txId, record.getTransactionId());
            ++txId;
        }
        DLSN dlsn = record.getDlsn();
        assertEquals(1L, dlsn.getLogSegmentSequenceNo());
        assertEquals(entryId, dlsn.getEntryId());
        record = entryReader.nextRecord();
    }
    ++entryId;
    assertEquals(2L, txId);
    assertEquals(reader.getLastAddConfirmed(), reader.readAheadEntries.size());
    assertEquals((reader.getLastAddConfirmed() + 1), reader.getNextEntryId());
    assertFalse(reader.hasCaughtUpOnInprogress());
    Utils.close(reader);
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 13 with DLSN

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

the class TestBKLogSegmentEntryReader method testReadEntriesFromCompleteLogSegment.

@Test(timeout = 60000)
public void testReadEntriesFromCompleteLogSegment() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(10);
    confLocal.setMaxPrefetchEntriesPerLogSegment(10);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    generateCompletedLogSegments(dlm, confLocal, 1, 20);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    boolean done = false;
    long txId = 1L;
    long entryId = 0L;
    while (!done) {
        Entry.Reader entryReader;
        try {
            entryReader = Utils.ioResult(reader.readNext(1)).get(0);
        } catch (EndOfLogSegmentException eol) {
            done = true;
            continue;
        }
        LogRecordWithDLSN record = entryReader.nextRecord();
        while (null != record) {
            if (!record.isControl()) {
                DLMTestUtil.verifyLogRecord(record);
                assertEquals(txId, record.getTransactionId());
                ++txId;
            }
            DLSN dlsn = record.getDlsn();
            assertEquals(1L, dlsn.getLogSegmentSequenceNo());
            assertEquals(entryId, dlsn.getEntryId());
            record = entryReader.nextRecord();
        }
        ++entryId;
    }
    assertEquals(21, txId);
    assertFalse(reader.hasCaughtUpOnInprogress());
    Utils.close(reader);
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) EndOfLogSegmentException(org.apache.distributedlog.exceptions.EndOfLogSegmentException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 14 with DLSN

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

the class TestLogRecordSelectors method testFirstDLSNNotLessThanSelector.

@Test(timeout = 60000)
public void testFirstDLSNNotLessThanSelector() {
    DLSN dlsn = new DLSN(5L, 5L, 0L);
    FirstDLSNNotLessThanSelector largerSelector = new FirstDLSNNotLessThanSelector(dlsn);
    for (int i = 0; i < 10; i++) {
        largerSelector.process(DLMTestUtil.getLogRecordWithDLSNInstance(new DLSN(4L, i, 0L), i));
    }
    assertNull(largerSelector.result());
    FirstDLSNNotLessThanSelector smallerSelector = new FirstDLSNNotLessThanSelector(dlsn);
    for (int i = 0; i < 10; i++) {
        smallerSelector.process(DLMTestUtil.getLogRecordWithDLSNInstance(new DLSN(6L, i, 0L), i));
    }
    assertEquals(new DLSN(6L, 0L, 0L), smallerSelector.result().getDlsn());
    FirstDLSNNotLessThanSelector selector = new FirstDLSNNotLessThanSelector(dlsn);
    for (int i = 0; i < 10; i++) {
        selector.process(DLMTestUtil.getLogRecordWithDLSNInstance(new DLSN(5L, i, 0L), i));
    }
    assertEquals(dlsn, selector.result().getDlsn());
}
Also used : DLSN(org.apache.distributedlog.DLSN) Test(org.junit.Test)

Example 15 with DLSN

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

the class TestDistributedLogTool method testToolTruncateStream.

@Test(timeout = 60000)
public void testToolTruncateStream() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setLogSegmentCacheEnabled(false);
    DistributedLogManager dlm = DLMTestUtil.createNewDLM("testToolTruncateStream", confLocal, defaultUri);
    DLMTestUtil.generateCompletedLogSegments(dlm, confLocal, 3, 1000);
    DLSN dlsn = new DLSN(2, 1, 0);
    TruncateStreamCommand cmd = new TruncateStreamCommand();
    cmd.setDlsn(dlsn);
    cmd.setUri(defaultUri);
    cmd.setStreamName("testToolTruncateStream");
    cmd.setForce(true);
    assertEquals(0, cmd.runCmd());
    LogReader reader = dlm.getInputStream(0);
    LogRecordWithDLSN record = reader.readNext(false);
    assertEquals(dlsn, record.getDlsn());
    reader.close();
    dlm.close();
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogReader(org.apache.distributedlog.api.LogReader) TruncateStreamCommand(org.apache.distributedlog.tools.DistributedLogTool.TruncateStreamCommand) Test(org.junit.Test)

Aggregations

DLSN (org.apache.distributedlog.DLSN)19 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)13 Test (org.junit.Test)13 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)11 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)9 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)9 Entry (org.apache.distributedlog.Entry)6 LogRecord (org.apache.distributedlog.LogRecord)4 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)4 Namespace (org.apache.distributedlog.api.namespace.Namespace)4 IOException (java.io.IOException)3 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)3 URI (java.net.URI)2 List (java.util.List)2 EndOfLogSegmentException (org.apache.distributedlog.exceptions.EndOfLogSegmentException)2 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)2 DryrunLogSegmentMetadataStoreUpdater (org.apache.distributedlog.metadata.DryrunLogSegmentMetadataStoreUpdater)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)1