public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(List<RemoteTask> existingTasks, int spaceThreshold) { if (existingTasks.isEmpty()) { return immediateFuture(null); } List<ListenableFuture<?>> stateChangeFutures = existingTasks.stream() .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); return whenAnyCompleteCancelOthers(stateChangeFutures); } }
public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(Set<Node> blockedNodes, List<RemoteTask> existingTasks, int spaceThreshold) { if (blockedNodes.isEmpty()) { return immediateFuture(null); } Map<String, RemoteTask> nodeToTaskMap = new HashMap<>(); for (RemoteTask task : existingTasks) { nodeToTaskMap.put(task.getNodeId(), task); } List<ListenableFuture<?>> blockedFutures = blockedNodes.stream() .map(Node::getNodeIdentifier) .map(nodeToTaskMap::get) .filter(Objects::nonNull) .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); if (blockedFutures.isEmpty()) { return immediateFuture(null); } return whenAnyCompleteCancelOthers(blockedFutures); }
public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(List<RemoteTask> existingTasks, int spaceThreshold) { if (existingTasks.isEmpty()) { return immediateFuture(null); } List<ListenableFuture<?>> stateChangeFutures = existingTasks.stream() .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); return whenAnyCompleteCancelOthers(stateChangeFutures); } }
public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(List<RemoteTask> existingTasks, int spaceThreshold) { if (existingTasks.isEmpty()) { return immediateFuture(null); } List<ListenableFuture<?>> stateChangeFutures = existingTasks.stream() .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); return whenAnyCompleteCancelOthers(stateChangeFutures); } }
public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(Set<Node> blockedNodes, List<RemoteTask> existingTasks, int spaceThreshold) { if (blockedNodes.isEmpty()) { return immediateFuture(null); } Map<String, RemoteTask> nodeToTaskMap = new HashMap<>(); for (RemoteTask task : existingTasks) { nodeToTaskMap.put(task.getNodeId(), task); } List<ListenableFuture<?>> blockedFutures = blockedNodes.stream() .map(Node::getNodeIdentifier) .map(nodeToTaskMap::get) .filter(Objects::nonNull) .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); if (blockedFutures.isEmpty()) { return immediateFuture(null); } return whenAnyCompleteCancelOthers(blockedFutures); }
public static ListenableFuture<?> toWhenHasSplitQueueSpaceFuture(Set<Node> blockedNodes, List<RemoteTask> existingTasks, int spaceThreshold) { if (blockedNodes.isEmpty()) { return immediateFuture(null); } Map<String, RemoteTask> nodeToTaskMap = new HashMap<>(); for (RemoteTask task : existingTasks) { nodeToTaskMap.put(task.getNodeId(), task); } List<ListenableFuture<?>> blockedFutures = blockedNodes.stream() .map(Node::getNodeIdentifier) .map(nodeToTaskMap::get) .filter(Objects::nonNull) .map(remoteTask -> remoteTask.whenSplitQueueHasSpace(spaceThreshold)) .collect(toImmutableList()); if (blockedFutures.isEmpty()) { return immediateFuture(null); } return whenAnyCompleteCancelOthers(blockedFutures); }
@Benchmark public void benchmark() throws Exception { Semaphore semaphore = new Semaphore(futureCount); ArrayList<SettableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < futureCount; i++) { SettableFuture<?> future = SettableFuture.create(); future.addListener(() -> semaphore.release(1), directExecutor()); futures.add(future); } ListenableFuture<?> anyComplete = whenAnyCompleteCancelOthers(futures); futures.get(futureCount / 2).set(null); semaphore.acquireUninterruptibly(futureCount); anyComplete.get(); }
@Benchmark public void benchmark() throws Exception { Semaphore semaphore = new Semaphore(futureCount); ArrayList<SettableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < futureCount; i++) { SettableFuture<?> future = SettableFuture.create(); future.addListener(() -> semaphore.release(1), directExecutor()); futures.add(future); } ListenableFuture<?> anyComplete = whenAnyCompleteCancelOthers(futures); futures.get(futureCount / 2).set(null); semaphore.acquireUninterruptibly(futureCount); anyComplete.get(); }
@Test public void testWhenAnyCompleteCancelOthers() throws Exception { assertGetUncheckedListenable(future -> { SettableFuture<Object> future1 = SettableFuture.create(); SettableFuture<Object> future3 = SettableFuture.create(); Object result = getFutureValue(whenAnyCompleteCancelOthers(ImmutableList.of(future1, future, future3))); assertTrue(future1.isCancelled()); assertTrue(future3.isCancelled()); return result; }); assertFailure(() -> whenAnyComplete(null), e -> assertInstanceOf(e, NullPointerException.class)); assertFailure(() -> whenAnyComplete(ImmutableList.of()), e -> assertInstanceOf(e, IllegalArgumentException.class)); assertEquals( tryGetFutureValue(whenAnyComplete(ImmutableList.of(SettableFuture.create(), SettableFuture.create())), 10, MILLISECONDS), Optional.empty()); }
@Test public void testWhenAnyCompleteCancelOthers() throws Exception { assertGetUncheckedListenable(future -> { SettableFuture<Object> future1 = SettableFuture.create(); SettableFuture<Object> future3 = SettableFuture.create(); Object result = getFutureValue(whenAnyCompleteCancelOthers(ImmutableList.of(future1, future, future3))); assertTrue(future1.isCancelled()); assertTrue(future3.isCancelled()); return result; }); assertFailure(() -> whenAnyComplete(null), e -> assertInstanceOf(e, NullPointerException.class)); assertFailure(() -> whenAnyComplete(ImmutableList.of()), e -> assertInstanceOf(e, IllegalArgumentException.class)); assertEquals( tryGetFutureValue(whenAnyComplete(ImmutableList.of(SettableFuture.create(), SettableFuture.create())), 10, MILLISECONDS), Optional.empty()); }