Search in sources :

Example 1 with SafeRunnable

use of org.apache.bookkeeper.common.util.SafeRunnable in project bookkeeper by apache.

the class BKAsyncLogReader method scheduleIdleReaderTaskIfNecessary.

private ScheduledFuture<?> scheduleIdleReaderTaskIfNecessary() {
    if (idleErrorThresholdMillis < Integer.MAX_VALUE) {
        // Dont run the task more than once every seconds (for sanity)
        long period = Math.max(idleErrorThresholdMillis / 10, 1000);
        // Except when idle reader threshold is less than a second (tests?)
        period = Math.min(period, idleErrorThresholdMillis / 5);
        return scheduler.scheduleAtFixedRateOrdered(streamName, new SafeRunnable() {

            @Override
            public void safeRun() {
                PendingReadRequest nextRequest = pendingRequests.peek();
                idleReaderCheckCount.inc();
                if (null == nextRequest) {
                    return;
                }
                idleReaderCheckIdleReadRequestCount.inc();
                if (nextRequest.elapsedSinceEnqueue(TimeUnit.MILLISECONDS) < idleErrorThresholdMillis) {
                    return;
                }
                ReadAheadEntryReader readAheadReader = getReadAheadReader();
                // read request has been idle
                // - cache has records but read request are idle,
                // that means notification was missed between readahead and reader.
                // - cache is empty and readahead is idle (no records added for a long time)
                idleReaderCheckIdleReadAheadCount.inc();
                try {
                    if (null == readAheadReader || (!hasMoreRecords() && readAheadReader.isReaderIdle(idleErrorThresholdMillis, TimeUnit.MILLISECONDS))) {
                        markReaderAsIdle();
                        return;
                    } else if (lastProcessTime.elapsed(TimeUnit.MILLISECONDS) > idleErrorThresholdMillis) {
                        markReaderAsIdle();
                    }
                } catch (IOException e) {
                    setLastException(e);
                    return;
                }
            }
        }, period, period, TimeUnit.MILLISECONDS);
    }
    return null;
}
Also used : SafeRunnable(org.apache.bookkeeper.common.util.SafeRunnable) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 SafeRunnable (org.apache.bookkeeper.common.util.SafeRunnable)1