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;
}
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();
}
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();
}
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);
}
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) {
}
}
Aggregations