use of org.apache.distributedlog.exceptions.ReadCancelledException in project bookkeeper by apache.
the class BKAsyncLogReader method asyncClose.
@Override
public CompletableFuture<Void> asyncClose() {
// Cancel the idle reader timeout task, interrupting if necessary
ReadCancelledException exception;
CompletableFuture<Void> closePromise;
synchronized (this) {
if (null != closeFuture) {
return closeFuture;
}
closePromise = closeFuture = new CompletableFuture<Void>();
exception = new ReadCancelledException(readHandler.getFullyQualifiedName(), "Reader was closed");
setLastException(exception);
releaseCurrentEntry();
}
// Do this after we have checked that the reader was not previously closed
cancelIdleReaderTask();
synchronized (scheduleLock) {
if (null != backgroundScheduleTask) {
backgroundScheduleTask.cancel(true);
}
}
cancelAllPendingReads(exception);
ReadAheadEntryReader readAheadReader = getReadAheadReader();
if (null != readAheadReader) {
readHandler.unregisterListener(readAheadReader);
readAheadReader.removeStateChangeNotification(this);
}
FutureUtils.proxyTo(Utils.closeSequence(bkDistributedLogManager.getScheduler(), true, readAheadReader, readHandler), closePromise);
return closePromise;
}
use of org.apache.distributedlog.exceptions.ReadCancelledException in project bookkeeper by apache.
the class TestBKLogSegmentEntryReader method testCloseReaderToCancelPendingReads.
@Test(timeout = 60000)
public void testCloseReaderToCancelPendingReads() throws Exception {
DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
confLocal.addConfiguration(conf);
confLocal.setNumPrefetchEntriesPerLogSegment(10);
confLocal.setMaxPrefetchEntriesPerLogSegment(10);
DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
DLMTestUtil.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);
List<CompletableFuture<List<Entry.Reader>>> futures = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
futures.add(reader.readNext(1));
}
assertFalse("Reader should not be closed yet", reader.isClosed());
Utils.close(reader);
for (CompletableFuture<List<Entry.Reader>> future : futures) {
try {
Utils.ioResult(future);
fail("The read request should be cancelled");
} catch (ReadCancelledException rce) {
// expected
}
}
assertFalse(reader.hasCaughtUpOnInprogress());
assertTrue("Reader should be closed yet", reader.isClosed());
}
use of org.apache.distributedlog.exceptions.ReadCancelledException in project bookkeeper by apache.
the class TestAsyncReaderWriter method testCancelReadRequestOnReaderClosed.
@Test(timeout = 60000)
public void testCancelReadRequestOnReaderClosed() throws Exception {
final String name = "distrlog-cancel-read-requests-on-reader-closed";
DistributedLogManager dlm = createNewDLM(testConf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.closeAndComplete();
final AsyncLogReader reader = dlm.getAsyncLogReader(DLSN.InitialDLSN);
LogRecordWithDLSN record = Utils.ioResult(reader.readNext());
assertEquals(1L, record.getTransactionId());
DLMTestUtil.verifyLogRecord(record);
final CountDownLatch readLatch = new CountDownLatch(1);
final AtomicBoolean receiveExpectedException = new AtomicBoolean(false);
Thread readThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Utils.ioResult(reader.readNext());
} catch (ReadCancelledException rce) {
receiveExpectedException.set(true);
} catch (Throwable t) {
LOG.error("Receive unexpected exception on reading stream {} : ", name, t);
}
readLatch.countDown();
}
}, "read-thread");
readThread.start();
Thread.sleep(1000);
// close reader should cancel the pending read next
Utils.close(reader);
readLatch.await();
readThread.join();
assertTrue("Read request should be cancelled.", receiveExpectedException.get());
// closed reader should reject any readNext
try {
Utils.ioResult(reader.readNext());
fail("Reader should reject readNext if it is closed.");
} catch (ReadCancelledException rce) {
// expected
}
dlm.close();
}
use of org.apache.distributedlog.exceptions.ReadCancelledException in project bookkeeper by apache.
the class BKLogSegmentEntryReader method asyncClose.
@Override
public CompletableFuture<Void> asyncClose() {
final CompletableFuture<Void> closeFuture;
ReadCancelledException exception;
LedgerHandle[] lhsToClose;
synchronized (this) {
if (null != closePromise) {
return closePromise;
}
closeFuture = closePromise = new CompletableFuture<Void>();
lhsToClose = openLedgerHandles.toArray(new LedgerHandle[openLedgerHandles.size()]);
// set the exception to cancel pending and subsequent reads
exception = new ReadCancelledException(getSegment().getZNodeName(), "Reader was closed");
completeExceptionally(exception, false);
}
// release the cached entries
releaseAllCachedEntries();
// cancel all pending reads
cancelAllPendingReads(exception);
// close all the open ledger
FutureUtils.proxyTo(BKUtils.closeLedgers(lhsToClose), closeFuture);
return closeFuture;
}
Aggregations