Source:
private Optional<PingableLeader> getSuspectedLeaderOverNetwork(String uuid) { MultiplexingCompletionService<PingableLeader, Entry<String, PingableLeader>> pingService = MultiplexingCompletionService.create(leaderPingExecutors); List<Future<Entry<String, PingableLeader>>> allFutures = Lists.newArrayList(); for (final PingableLeader potentialLeader : otherPotentialLeadersToHosts.keySet()) { allFutures.add(pingService.submit(potentialLeader, () -> new AbstractMap.SimpleEntry<>(potentialLeader.getUUID(), potentialLeader))); } boolean interrupted = false; try { long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(leaderPingResponseWaitMs); for (;;) { try { Future<Entry<String, PingableLeader>> pingFuture = pingService.poll( deadline - System.nanoTime(), TimeUnit.NANOSECONDS); if (pingFuture == null) { break; } Entry<String, PingableLeader> cacheEntry = pingFuture.get(); PingableLeader service = uuidToServiceCache.putIfAbsent( cacheEntry.getKey(), cacheEntry.getValue()); throwIfInvalidSetup(service, cacheEntry.getValue(), cacheEntry.getKey()); if (uuid.equals(cacheEntry.getKey())) { return Optional.of(cacheEntry.getValue()); } } catch (InterruptedException e) { log.warn("uuid request interrupted", e); interrupted = true; break; } catch (ExecutionException e) { log.warn("unable to get uuid from server", e); } } Future<Entry<String, PingableLeader>> future; while ((future = pingService.poll()) != null) { try { Entry<String, PingableLeader> cacheEntry = future.get(); uuidToServiceCache.putIfAbsent(cacheEntry.getKey(), cacheEntry.getValue()); } catch (InterruptedException e) { log.warn("uuid request interrupted", e); interrupted = true; break; } catch (ExecutionException e) { log.warn("unable to get uuid from server", e); } } } finally { for (Future<Entry<String, PingableLeader>> future : allFutures) { future.cancel(false); } if (interrupted) { Thread.currentThread().interrupt(); } } return Optional.empty(); }

Target:
private Optional<PingableLeader> getSuspectedLeaderOverNetwork(String uuid) { MultiplexingCompletionService<PingableLeader, Entry<String, PingableLeader>> pingService = MultiplexingCompletionService.create(leaderPingExecutors); List<Future<Entry<String, PingableLeader>>> allFutures = Lists.newArrayList(); for (final PingableLeader potentialLeader : otherPotentialLeadersToHosts.keySet()) { allFutures.add(pingService.submit(potentialLeader, () -> Maps.immutableEntry(potentialLeader.getUUID(), potentialLeader))); } boolean interrupted = false; try { long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(leaderPingResponseWaitMs); for (;;) { try { Future<Entry<String, PingableLeader>> pingFuture = pingService.poll( deadline - System.nanoTime(), TimeUnit.NANOSECONDS); if (pingFuture == null) { break; } Entry<String, PingableLeader> cacheEntry = pingFuture.get(); PingableLeader service = uuidToServiceCache.putIfAbsent( cacheEntry.getKey(), cacheEntry.getValue()); throwIfInvalidSetup(service, cacheEntry.getValue(), cacheEntry.getKey()); if (uuid.equals(cacheEntry.getKey())) { return Optional.of(cacheEntry.getValue()); } } catch (InterruptedException e) { log.warn("uuid request interrupted", e); interrupted = true; break; } catch (ExecutionException e) { log.warn("unable to get uuid from server", e); } } Future<Entry<String, PingableLeader>> future; while ((future = pingService.poll()) != null) { try { Entry<String, PingableLeader> cacheEntry = future.get(); uuidToServiceCache.putIfAbsent(cacheEntry.getKey(), cacheEntry.getValue()); } catch (InterruptedException e) { log.warn("uuid request interrupted", e); interrupted = true; break; } catch (ExecutionException e) { log.warn("unable to get uuid from server", e); } } } finally { for (Future<Entry<String, PingableLeader>> future : allFutures) { future.cancel(false); } if (interrupted) { Thread.currentThread().interrupt(); } } return Optional.empty(); }