Sho SHIMIZU
Committed by Gerrit Code Review

Refactor: Remove try-catch by using CompletableFuture#exceptionally()

Change-Id: I728fbcfe8fc4a81da2b08e6dddc1fa63add694a3
...@@ -317,40 +317,35 @@ public class IntentManager ...@@ -317,40 +317,35 @@ public class IntentManager
317 317
318 // batchExecutor is single-threaded, so only one batch is in flight at a time 318 // batchExecutor is single-threaded, so only one batch is in flight at a time
319 CompletableFuture.runAsync(() -> { 319 CompletableFuture.runAsync(() -> {
320 - try { 320 + // process intent until the phase reaches one of the final phases
321 - /* 321 + List<CompletableFuture<IntentData>> futures = operations.stream()
322 - 1. wrap each intentdata in a runnable and submit 322 + .map(x -> CompletableFuture.completedFuture(x)
323 - 2. wait for completion of all the work 323 + .thenApply(IntentManager.this::createInitialPhase)
324 - 3. accumulate results and submit batch write of IntentData to store 324 + .thenApplyAsync(IntentManager.this::process, workerExecutor)
325 - (we can also try to update these individually) 325 + .thenApply(FinalIntentProcessPhase::data)
326 - */ 326 + .exceptionally(e -> {
327 - List<CompletableFuture<IntentData>> futures = operations.stream() 327 + //FIXME
328 - .map(x -> CompletableFuture.completedFuture(x) 328 + log.warn("Future failed: {}", e);
329 - .thenApply(IntentManager.this::createInitialPhase) 329 + return null;
330 - .thenApplyAsync(IntentManager.this::process, workerExecutor) 330 + })).collect(Collectors.toList());
331 - .thenApply(FinalIntentProcessPhase::data) 331 +
332 - .exceptionally(e -> { 332 + // write multiple data to store in order
333 - //FIXME 333 + store.batchWrite(Tools.allOf(futures).join().stream()
334 - log.warn("Future failed: {}", e); 334 + .filter(Objects::nonNull)
335 - return null; 335 + .collect(Collectors.toList()));
336 - })) 336 + }, batchExecutor).exceptionally(e -> {
337 - .collect(Collectors.toList()); 337 + log.error("Error submitting batches:", e);
338 - store.batchWrite(Tools.allOf(futures).join().stream() 338 + // FIXME incomplete Intents should be cleaned up
339 - .filter(Objects::nonNull) 339 + // (transition to FAILED, etc.)
340 - .collect(Collectors.toList())); 340 +
341 - } catch (Exception e) { 341 + // the batch has failed
342 - log.error("Error submitting batches:", e); 342 + // TODO: maybe we should do more?
343 - // FIXME incomplete Intents should be cleaned up 343 + log.error("Walk the plank, matey...");
344 - // (transition to FAILED, etc.) 344 + //FIXME
345 -
346 - // the batch has failed
347 - // TODO: maybe we should do more?
348 - log.error("Walk the plank, matey...");
349 - //FIXME
350 // batchService.removeIntentOperations(data); 345 // batchService.removeIntentOperations(data);
351 - } 346 + return null;
352 - accumulator.ready(); 347 + }).thenRun(accumulator::ready);
353 - }, batchExecutor); 348 +
354 } 349 }
355 } 350 }
356 351
......