use of com.twitter.distributedlog.exceptions.ReadCancelledException in project distributedlog by twitter.
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 = Await.result(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 {
Await.result(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 {
Await.result(reader.readNext());
fail("Reader should reject readNext if it is closed.");
} catch (ReadCancelledException rce) {
// expected
}
dlm.close();
}
use of com.twitter.distributedlog.exceptions.ReadCancelledException in project distributedlog by twitter.
the class BKAsyncLogReaderDLSN method asyncClose.
@Override
public Future<Void> asyncClose() {
// Cancel the idle reader timeout task, interrupting if necessary
ReadCancelledException exception;
Promise<Void> closePromise;
synchronized (this) {
if (null != closeFuture) {
return closeFuture;
}
closePromise = closeFuture = new Promise<Void>();
exception = new ReadCancelledException(bkLedgerManager.getFullyQualifiedName(), "Reader was closed");
setLastException(exception);
}
// Do this after we have checked that the reader was not previously closed
try {
if (null != idleReaderTimeoutTask) {
idleReaderTimeoutTask.cancel(true);
}
} catch (Exception exc) {
LOG.info("{}: Failed to cancel the background idle reader timeout task", bkLedgerManager.getFullyQualifiedName());
}
synchronized (scheduleLock) {
if (null != backgroundScheduleTask) {
backgroundScheduleTask.cancel(true);
}
}
cancelAllPendingReads(exception);
bkLedgerManager.unregister(sessionExpireWatcher);
FutureUtils.ignore(bkLedgerManager.asyncClose()).proxyTo(closePromise);
return closePromise;
}
Aggregations