[reindex] Extract runnable to inner class

Makes it more readable
This commit is contained in:
Nik Everett 2016-06-08 14:00:15 -04:00
parent 2437313e4e
commit bd1af34506
1 changed files with 27 additions and 17 deletions

View File

@ -408,23 +408,8 @@ public class BulkByScrollTask extends CancellableTask {
// Synchronize so we are less likely to schedule the same request twice.
synchronized (delayedPrepareBulkRequestReference) {
TimeValue delay = throttleWaitTime(lastBatchStartTime, lastBatchSize);
AbstractRunnable oneTime = new AbstractRunnable() {
private final AtomicBoolean hasRun = new AtomicBoolean(false);
@Override
protected void doRun() throws Exception {
// Paranoia to prevent furiously rethrottling from running the command multiple times. Without this we totally can.
if (hasRun.compareAndSet(false, true)) {
prepareBulkRequestRunnable.run();
}
}
@Override
public void onFailure(Throwable t) {
prepareBulkRequestRunnable.onFailure(t);
}
};
delayedPrepareBulkRequestReference.set(new DelayedPrepareBulkRequest(threadPool, getRequestsPerSecond(), delay, oneTime));
delayedPrepareBulkRequestReference.set(new DelayedPrepareBulkRequest(threadPool, getRequestsPerSecond(),
delay, new RunOnce(prepareBulkRequestRunnable)));
}
}
@ -540,4 +525,29 @@ public class BulkByScrollTask extends CancellableTask {
return timeValueNanos(round(remainingDelay * requestsPerSecond / newRequestsPerSecond));
}
}
/**
* Runnable that can only be run one time. This is paranoia to prevent furiously rethrottling from running the command multiple times.
* Without it the command would be run multiple times.
*/
private static class RunOnce extends AbstractRunnable {
private final AtomicBoolean hasRun = new AtomicBoolean(false);
private final AbstractRunnable delegate;
public RunOnce(AbstractRunnable delegate) {
this.delegate = delegate;
}
@Override
protected void doRun() throws Exception {
if (hasRun.compareAndSet(false, true)) {
delegate.run();
}
}
@Override
public void onFailure(Throwable t) {
delegate.onFailure(t);
}
}
}