Search in sources :

Example 1 with ReadCancelledException

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException)

Example 2 with ReadCancelledException

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());
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) Entry(org.apache.distributedlog.Entry) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) List(java.util.List) Test(org.junit.Test)

Example 3 with ReadCancelledException

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

Example 4 with ReadCancelledException

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle)

Aggregations

ReadCancelledException (org.apache.distributedlog.exceptions.ReadCancelledException)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)2 Test (org.junit.Test)2 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 Entry (org.apache.distributedlog.Entry)1 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)1 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1