Search in sources :

Example 6 with IdleReaderException

use of org.apache.distributedlog.exceptions.IdleReaderException in project bookkeeper by apache.

the class TestAsyncReaderWriter method testAsyncReadIdleErrorInternal.

private void testAsyncReadIdleErrorInternal(String name, final int idleReaderErrorThreshold, final boolean heartBeatUsingControlRecs, final boolean simulateReaderStall) throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    confLocal.setReadAheadBatchSize(1);
    confLocal.setReadAheadMaxRecords(1);
    confLocal.setReaderIdleWarnThresholdMillis(0);
    confLocal.setReaderIdleErrorThresholdMillis(idleReaderErrorThreshold);
    final DistributedLogManager dlm = createNewDLM(confLocal, name);
    final Thread currentThread = Thread.currentThread();
    final int segmentSize = 3;
    final int numSegments = 3;
    final CountDownLatch latch = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            try {
                int txid = 1;
                for (long i = 0; i < numSegments; i++) {
                    long start = txid;
                    BKSyncLogWriter writer = (BKSyncLogWriter) dlm.startLogSegmentNonPartitioned();
                    for (long j = 1; j <= segmentSize; j++) {
                        writer.write(DLMTestUtil.getLargeLogRecordInstance(txid++));
                        if ((i == 0) && (j == 1)) {
                            latch.countDown();
                        }
                    }
                    if (heartBeatUsingControlRecs) {
                        // There should be a control record such that
                        // wait time + commit time (BK) < Idle Reader Threshold
                        int threadSleepTime = idleReaderErrorThreshold - // BK commitTime
                        200 - // safety margin
                        100;
                        for (int iter = 1; iter <= (2 * idleReaderErrorThreshold / threadSleepTime); iter++) {
                            Thread.sleep(threadSleepTime);
                            writer.write(DLMTestUtil.getLargeLogRecordInstance(txid, true));
                            writer.flush();
                        }
                        Thread.sleep(threadSleepTime);
                    }
                    writer.closeAndComplete();
                    if (!heartBeatUsingControlRecs) {
                        Thread.sleep(2 * idleReaderErrorThreshold);
                    }
                }
            } catch (Exception exc) {
                if (!executor.isShutdown()) {
                    currentThread.interrupt();
                }
            }
        }
    }, 0, TimeUnit.MILLISECONDS);
    latch.await();
    BKAsyncLogReader reader = (BKAsyncLogReader) dlm.getAsyncLogReader(DLSN.InitialDLSN);
    if (simulateReaderStall) {
        reader.disableProcessingReadRequests();
    }
    boolean exceptionEncountered = false;
    int recordCount = 0;
    try {
        while (true) {
            CompletableFuture<LogRecordWithDLSN> record = reader.readNext();
            Utils.ioResult(record);
            recordCount++;
            if (recordCount >= segmentSize * numSegments) {
                break;
            }
        }
    } catch (IdleReaderException exc) {
        exceptionEncountered = true;
    }
    if (simulateReaderStall) {
        assertTrue(exceptionEncountered);
    } else if (heartBeatUsingControlRecs) {
        assertFalse(exceptionEncountered);
        Assert.assertEquals(segmentSize * numSegments, recordCount);
    } else {
        assertTrue(exceptionEncountered);
        Assert.assertEquals(segmentSize, recordCount);
    }
    assertFalse(currentThread.isInterrupted());
    Utils.close(reader);
    executor.shutdown();
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException) OverCapacityException(org.apache.distributedlog.exceptions.OverCapacityException) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException) DLIllegalStateException(org.apache.distributedlog.exceptions.DLIllegalStateException) WriteException(org.apache.distributedlog.exceptions.WriteException) LockingException(org.apache.distributedlog.exceptions.LockingException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) IOException(java.io.IOException) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager)

Example 7 with IdleReaderException

use of org.apache.distributedlog.exceptions.IdleReaderException in project bookkeeper by apache.

the class BKSyncLogReader method markReaderAsIdle.

private void markReaderAsIdle() throws IdleReaderException {
    idleReaderError.inc();
    IdleReaderException ire = new IdleReaderException("Sync reader on stream " + readHandler.getFullyQualifiedName() + " is idle for more than " + idleErrorThresholdMillis + " ms");
    readerException.compareAndSet(null, ire);
    throw ire;
}
Also used : IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException)

Aggregations

IdleReaderException (org.apache.distributedlog.exceptions.IdleReaderException)7 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)5 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)4 Test (org.junit.Test)4 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 BKTransmitException (org.apache.distributedlog.exceptions.BKTransmitException)2 DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)2 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)2 LockingException (org.apache.distributedlog.exceptions.LockingException)2 LogRecordTooLongException (org.apache.distributedlog.exceptions.LogRecordTooLongException)2 OverCapacityException (org.apache.distributedlog.exceptions.OverCapacityException)2 ReadCancelledException (org.apache.distributedlog.exceptions.ReadCancelledException)2 WriteException (org.apache.distributedlog.exceptions.WriteException)2 URI (java.net.URI)1 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1