Search in sources :

Example 1 with ReadCancelledException

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReadCancelledException(com.twitter.distributedlog.exceptions.ReadCancelledException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ReadCancelledException

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;
}
Also used : Promise(com.twitter.util.Promise) ReadCancelledException(com.twitter.distributedlog.exceptions.ReadCancelledException) ReadCancelledException(com.twitter.distributedlog.exceptions.ReadCancelledException) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) IdleReaderException(com.twitter.distributedlog.exceptions.IdleReaderException) DLIllegalStateException(com.twitter.distributedlog.exceptions.DLIllegalStateException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) IOException(java.io.IOException)

Aggregations

ReadCancelledException (com.twitter.distributedlog.exceptions.ReadCancelledException)2 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)1 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)1 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)1 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 Promise (com.twitter.util.Promise)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1