PPS: wait for running callbacks to finish before publishing new result

This commit is contained in:
Jakub Valtar
2016-05-07 12:21:19 +02:00
parent 53f4f16f59
commit 87dd72e59b

View File

@@ -112,6 +112,7 @@ public class PreprocessingService {
private void mainLoop() {
running = true;
PreprocessedSketch prevResult = null;
CompletableFuture<?> runningCallbacks = null;
Messages.log("PPS: Hi!");
while (running) {
try {
@@ -126,8 +127,18 @@ public class PreprocessingService {
prevResult = preprocessSketch(prevResult);
// Wait until callbacks finish before firing new wave
// If new request arrives while waiting, break out and start preprocessing
while (requestQueue.isEmpty() && runningCallbacks != null) {
try {
runningCallbacks.get(10, TimeUnit.MILLISECONDS);
runningCallbacks = null;
} catch (TimeoutException e) { }
}
synchronized (requestLock) {
if (requestQueue.isEmpty()) {
runningCallbacks = lastCallback;
Messages.log("PPS: Done");
preprocessingTask.complete(prevResult);
}