Sho SHIMIZU
Committed by Gerrit Code Review

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

Change-Id: I728fbcfe8fc4a81da2b08e6dddc1fa63add694a3
...@@ -317,13 +317,7 @@ public class IntentManager ...@@ -317,13 +317,7 @@ 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 - /*
322 - 1. wrap each intentdata in a runnable and submit
323 - 2. wait for completion of all the work
324 - 3. accumulate results and submit batch write of IntentData to store
325 - (we can also try to update these individually)
326 - */
327 List<CompletableFuture<IntentData>> futures = operations.stream() 321 List<CompletableFuture<IntentData>> futures = operations.stream()
328 .map(x -> CompletableFuture.completedFuture(x) 322 .map(x -> CompletableFuture.completedFuture(x)
329 .thenApply(IntentManager.this::createInitialPhase) 323 .thenApply(IntentManager.this::createInitialPhase)
...@@ -333,12 +327,13 @@ public class IntentManager ...@@ -333,12 +327,13 @@ public class IntentManager
333 //FIXME 327 //FIXME
334 log.warn("Future failed: {}", e); 328 log.warn("Future failed: {}", e);
335 return null; 329 return null;
336 - })) 330 + })).collect(Collectors.toList());
337 - .collect(Collectors.toList()); 331 +
332 + // write multiple data to store in order
338 store.batchWrite(Tools.allOf(futures).join().stream() 333 store.batchWrite(Tools.allOf(futures).join().stream()
339 .filter(Objects::nonNull) 334 .filter(Objects::nonNull)
340 .collect(Collectors.toList())); 335 .collect(Collectors.toList()));
341 - } catch (Exception e) { 336 + }, batchExecutor).exceptionally(e -> {
342 log.error("Error submitting batches:", e); 337 log.error("Error submitting batches:", e);
343 // FIXME incomplete Intents should be cleaned up 338 // FIXME incomplete Intents should be cleaned up
344 // (transition to FAILED, etc.) 339 // (transition to FAILED, etc.)
...@@ -348,9 +343,9 @@ public class IntentManager ...@@ -348,9 +343,9 @@ public class IntentManager
348 log.error("Walk the plank, matey..."); 343 log.error("Walk the plank, matey...");
349 //FIXME 344 //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
......