Search in sources :

Example 1 with EndOfStreamException

use of org.apache.distributedlog.exceptions.EndOfStreamException in project bookkeeper by apache.

the class BKSyncLogReader method doReadNext.

private LogRecordWithDLSN doReadNext(boolean nonBlocking) throws IOException {
    LogRecordWithDLSN record = null;
    do {
        // fetch one record until we don't find any entry available in the readahead cache
        while (null == record) {
            if (null == currentEntry) {
                currentEntry = readNextEntry(nonBlocking);
                if (null == currentEntry) {
                    return null;
                }
            }
            record = currentEntry.nextRecord();
            if (null == record) {
                currentEntry = null;
            }
        }
        // check if we reached the end of stream
        if (record.isEndOfStream()) {
            EndOfStreamException eos = new EndOfStreamException("End of Stream Reached for " + readHandler.getFullyQualifiedName());
            readerException.compareAndSet(null, eos);
            throw eos;
        }
        // skip control records
        if (record.isControl()) {
            record = null;
            continue;
        }
        if (!positioned) {
            if (record.getTransactionId() < startTransactionId.get()) {
                record = null;
                continue;
            } else {
                positioned = true;
                break;
            }
        } else {
            break;
        }
    } while (true);
    return record;
}
Also used : EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException)

Example 2 with EndOfStreamException

use of org.apache.distributedlog.exceptions.EndOfStreamException in project bookkeeper by apache.

the class TestBKDistributedLogManager method testMarkEndOfStream.

@Test(timeout = 60000)
public void testMarkEndOfStream() throws Exception {
    String name = "distrlog-mark-end-of-stream";
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    txid = writeAndMarkEndOfStream(dlm, txid);
    LogReader reader = dlm.getInputStream(1);
    long numTrans = 0;
    boolean exceptionEncountered = false;
    LogRecord record = null;
    try {
        record = reader.readNext(false);
        long expectedTxId = 1;
        while (null != record) {
            DLMTestUtil.verifyLogRecord(record);
            assertEquals(expectedTxId, record.getTransactionId());
            expectedTxId++;
            numTrans++;
            record = reader.readNext(false);
        }
    } catch (EndOfStreamException exc) {
        LOG.info("Encountered EndOfStream on reading records after {}", record);
        exceptionEncountered = true;
    }
    assertEquals((txid - 1), numTrans);
    assertTrue(exceptionEncountered);
    exceptionEncountered = false;
    try {
        reader.readNext(false);
    } catch (EndOfStreamException exc) {
        exceptionEncountered = true;
    }
    assertTrue(exceptionEncountered);
    reader.close();
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) LogReader(org.apache.distributedlog.api.LogReader) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) Test(org.junit.Test)

Example 3 with EndOfStreamException

use of org.apache.distributedlog.exceptions.EndOfStreamException in project bookkeeper by apache.

the class TestBKDistributedLogManager method markEndOfStreamOnEmptyLogSegment.

private void markEndOfStreamOnEmptyLogSegment(int numCompletedSegments) throws Exception {
    String name = "distrlog-mark-end-empty-" + numCompletedSegments;
    DistributedLogManager dlm = createNewDLM(conf, name);
    DLMTestUtil.generateCompletedLogSegments(dlm, conf, numCompletedSegments, DEFAULT_SEGMENT_SIZE);
    BKSyncLogWriter writer = (BKSyncLogWriter) dlm.startLogSegmentNonPartitioned();
    writer.markEndOfStream();
    LogReader reader = dlm.getInputStream(1);
    long numTrans = 0;
    boolean exceptionEncountered = false;
    try {
        LogRecord record = reader.readNext(false);
        long lastTxId = -1;
        while (null != record) {
            DLMTestUtil.verifyLogRecord(record);
            assert (lastTxId < record.getTransactionId());
            lastTxId = record.getTransactionId();
            numTrans++;
            record = reader.readNext(false);
        }
    } catch (EndOfStreamException exc) {
        exceptionEncountered = true;
    }
    assertEquals(numCompletedSegments * DEFAULT_SEGMENT_SIZE, numTrans);
    assertTrue(exceptionEncountered);
    exceptionEncountered = false;
    try {
        reader.readNext(false);
    } catch (EndOfStreamException exc) {
        exceptionEncountered = true;
    }
    assertTrue(exceptionEncountered);
    reader.close();
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) LogReader(org.apache.distributedlog.api.LogReader) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader)

Example 4 with EndOfStreamException

use of org.apache.distributedlog.exceptions.EndOfStreamException in project bookkeeper by apache.

the class TestBKDistributedLogManager method testWriteFailsAfterMarkEndOfStream.

@Test(timeout = 60000)
public void testWriteFailsAfterMarkEndOfStream() throws Exception {
    String name = "distrlog-mark-end-failure";
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    txid = writeAndMarkEndOfStream(dlm, txid);
    assertEquals(txid - 1, dlm.getLastTxId());
    LogRecord last = dlm.getLastLogRecord();
    assertEquals(txid - 1, last.getTransactionId());
    DLMTestUtil.verifyLogRecord(last);
    assertTrue(dlm.isEndOfStreamMarked());
    LogWriter writer = null;
    boolean exceptionEncountered = false;
    try {
        writer = dlm.startLogSegmentNonPartitioned();
        for (long j = 1; j <= DEFAULT_SEGMENT_SIZE / 2; j++) {
            writer.write(DLMTestUtil.getLogRecordInstance(txid++));
        }
    } catch (EndOfStreamException exc) {
        exceptionEncountered = true;
    }
    writer.close();
    assertTrue(exceptionEncountered);
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) Test(org.junit.Test)

Example 5 with EndOfStreamException

use of org.apache.distributedlog.exceptions.EndOfStreamException in project bookkeeper by apache.

the class TestAppendOnlyStreamReader method testSkipToSkipsBytesUntilEndOfStream.

@Test(timeout = 60000)
public void testSkipToSkipsBytesUntilEndOfStream() throws Exception {
    String name = testNames.getMethodName();
    DistributedLogManager dlmwrite = createNewDLM(conf, name);
    DistributedLogManager dlmreader = createNewDLM(conf, name);
    long txid = 1;
    AppendOnlyStreamWriter writer = dlmwrite.getAppendOnlyStreamWriter();
    writer.write(DLMTestUtil.repeatString("abc", 5).getBytes());
    writer.markEndOfStream();
    writer.force(false);
    writer.close();
    AppendOnlyStreamReader reader = dlmreader.getAppendOnlyStreamReader();
    byte[] bytesIn = new byte[9];
    int read = reader.read(bytesIn, 0, 9);
    assertEquals(9, read);
    assertTrue(Arrays.equals(DLMTestUtil.repeatString("abc", 3).getBytes(), bytesIn));
    assertTrue(reader.skipTo(15));
    try {
        read = reader.read(bytesIn, 0, 1);
        fail("Should have thrown");
    } catch (EndOfStreamException ex) {
    }
    assertTrue(reader.skipTo(0));
    try {
        reader.skipTo(16);
        fail("Should have thrown");
    } catch (EndOfStreamException ex) {
    }
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) Test(org.junit.Test)

Aggregations

EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)15 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)11 Test (org.junit.Test)9 LogReader (org.apache.distributedlog.api.LogReader)7 DLSN (org.apache.distributedlog.DLSN)4 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)2 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)2 Test (org.testng.annotations.Test)2 Stopwatch (com.google.common.base.Stopwatch)1 DLInputStream (com.twitter.heron.dlog.DLInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)1 LogWriter (org.apache.distributedlog.api.LogWriter)1