use of org.apache.distributedlog.exceptions.EndOfLogSegmentException 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);
}
use of org.apache.distributedlog.exceptions.EndOfLogSegmentException 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);
}
use of org.apache.distributedlog.exceptions.EndOfLogSegmentException in project bookkeeper by apache.
the class BKLogSegmentEntryReader method readEntriesFromReadAheadCache.
private void readEntriesFromReadAheadCache(PendingReadRequest nextRequest) {
while (!nextRequest.hasReadEnoughEntries()) {
CacheEntry entry;
boolean hitEndOfLogSegment;
synchronized (this) {
entry = readAheadEntries.peek();
hitEndOfLogSegment = (null == entry) && isEndOfLogSegment();
}
// reach end of log segment
if (hitEndOfLogSegment) {
completeExceptionally(new EndOfLogSegmentException(getSegment().getZNodeName()), false);
return;
}
if (null == entry) {
return;
}
// entry is not complete yet.
if (!entry.isDone()) {
// we already reached end of the log segment
if (isEndOfLogSegment(entry.getEntryId())) {
completeExceptionally(new EndOfLogSegmentException(getSegment().getZNodeName()), false);
}
return;
}
if (entry.isSuccess()) {
CacheEntry removedEntry = readAheadEntries.poll();
try {
if (entry != removedEntry) {
DLIllegalStateException ise = new DLIllegalStateException("Unexpected condition at reading from " + getSegment());
completeExceptionally(ise, false);
return;
}
try {
// the reference is retained on `entry.getEntry()`.
// Entry.Reader is responsible for releasing it.
nextRequest.addEntry(processReadEntry(entry.getEntry()));
} catch (IOException e) {
completeExceptionally(e, false);
return;
}
} finally {
removedEntry.release();
}
} else if (skipBrokenEntries && BKException.Code.DigestMatchException == entry.getRc()) {
// skip this entry and move forward
skippedBrokenEntriesCounter.inc();
CacheEntry removedEntry = readAheadEntries.poll();
removedEntry.release();
continue;
} else {
completeExceptionally(new BKTransmitException("Encountered issue on reading entry " + entry.getEntryId() + " @ log segment " + getSegment(), entry.getRc()), false);
return;
}
}
}
Aggregations