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;
}
Aggregations