Java Code Examples for org.apache.flink.runtime.concurrent.FutureUtils#forward()

The following examples show how to use org.apache.flink.runtime.concurrent.FutureUtils#forward() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDispatcherRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	synchronized (lock) {
		if (!running) {
			return terminationFuture;
		} else {
			running = false;
		}
	}

	stopDispatcherLeaderProcess();

	FutureUtils.forward(
		previousDispatcherLeaderProcessTerminationFuture,
		terminationFuture);

	return terminationFuture;
}
 
Example 2
Source File: SlotSharingManager.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new root slot with the given {@link SlotRequestId}, {@link SlotContext} future and
 * the {@link SlotRequestId} of the allocated slot.
 *
 * @param slotRequestId of the root slot
 * @param slotContextFuture with which we create the root slot
 * @param allocatedSlotRequestId slot request id of the underlying allocated slot which can be used
 *                               to cancel the pending slot request or release the allocated slot
 * @return New root slot
 */
@Nonnull
MultiTaskSlot createRootSlot(
		SlotRequestId slotRequestId,
		CompletableFuture<? extends SlotContext> slotContextFuture,
		SlotRequestId allocatedSlotRequestId) {
	LOG.debug("Create multi task slot [{}] in slot [{}].", slotRequestId, allocatedSlotRequestId);

	final CompletableFuture<SlotContext> slotContextFutureAfterRootSlotResolution = new CompletableFuture<>();
	final MultiTaskSlot rootMultiTaskSlot = createAndRegisterRootSlot(
		slotRequestId,
		allocatedSlotRequestId,
		slotContextFutureAfterRootSlotResolution);

	FutureUtils.forward(
		slotContextFuture.thenApply(
			(SlotContext slotContext) -> {
				// add the root node to the set of resolved root nodes once the SlotContext future has
				// been completed and we know the slot's TaskManagerLocation
				tryMarkSlotAsResolved(slotRequestId, slotContext);
				return slotContext;
			}),
		slotContextFutureAfterRootSlotResolution);

	return rootMultiTaskSlot;
}
 
Example 3
Source File: TaskSlotTableImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
	if (state == State.CREATED) {
		state = State.CLOSED;
		closingFuture.complete(null);
	} else if (state == State.RUNNING) {
		state = State.CLOSING;
		final FlinkException cause = new FlinkException("Closing task slot table");
		CompletableFuture<Void> cleanupFuture = FutureUtils
			.waitForAll(
				new ArrayList<>(allocatedSlots.values())
					.stream()
					.map(slot -> freeSlotInternal(slot, cause))
					.collect(Collectors.toList()))
			.thenRunAsync(
				() -> {
					state = State.CLOSED;
					timerService.stop();
				},
				mainThreadExecutor);
		FutureUtils.forward(cleanupFuture, closingFuture);
	}
	return closingFuture;
}
 
Example 4
Source File: TaskSlot.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Close the task slot asynchronously.
 *
 * <p>Slot is moved to {@link TaskSlotState#RELEASING} state and only once.
 * If there are active tasks running in the slot then they are failed.
 * The future of all tasks terminated and slot cleaned up is initiated only once and always returned
 * in case of multiple attempts to close the slot.
 *
 * @param cause cause of closing
 * @return future of all running task if any being done and slot cleaned up.
 */
CompletableFuture<Void> closeAsync(Throwable cause) {
	if (!isReleasing()) {
		state = TaskSlotState.RELEASING;
		if (!isEmpty()) {
			// we couldn't free the task slot because it still contains task, fail the tasks
			// and set the slot state to releasing so that it gets eventually freed
			tasks.values().forEach(task -> task.failExternally(cause));
		}
		final CompletableFuture<Void> cleanupFuture = FutureUtils
			.waitForAll(tasks.values().stream().map(TaskSlotPayload::getTerminationFuture).collect(Collectors.toList()))
			.thenRun(() -> {
				verifyMemoryFreed();
				this.memoryManager.shutdown();
			});

		FutureUtils.forward(cleanupFuture, closingFuture);
	}
	return closingFuture;
}
 
Example 5
Source File: AbstractDispatcherLeaderProcess.java    From flink with Apache License 2.0 5 votes vote down vote up
private void closeInternal() {
	log.info("Stopping {}.", getClass().getSimpleName());

	final CompletableFuture<Void> dispatcherServiceTerminationFuture = closeDispatcherService();

	final CompletableFuture<Void> onCloseTerminationFuture = FutureUtils.composeAfterwards(
		dispatcherServiceTerminationFuture,
		this::onClose);

	FutureUtils.forward(
		onCloseTerminationFuture,
		this.terminationFuture);

	state = State.STOPPED;
}
 
Example 6
Source File: OperatorEventValve.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Opens the value, releasing all buffered events.
 */
public void openValveAndUnmarkCheckpoint() {
	final ArrayList<FuturePair> futures;

	// send all events under lock, so that no new event can sneak between
	synchronized (lock) {
		currentCheckpointId = NO_CHECKPOINT;

		if (!shut) {
			return;
		}

		futures = new ArrayList<>(blockedEvents.size());

		for (List<BlockedEvent> eventsForTask : blockedEvents.values()) {
			for (BlockedEvent blockedEvent : eventsForTask) {
				final CompletableFuture<Acknowledge> ackFuture = eventSender.apply(blockedEvent.event, blockedEvent.subtask);
				futures.add(new FuturePair(blockedEvent.future, ackFuture));
			}
		}
		blockedEvents.clear();
		shut = false;
	}

	// apply the logic on the future outside the lock, to be safe
	for (FuturePair pair : futures) {
		FutureUtils.forward(pair.ackFuture, pair.originalFuture);
	}
}
 
Example 7
Source File: AbstractDispatcherLeaderProcess.java    From flink with Apache License 2.0 4 votes vote down vote up
private void completeDispatcherSetupInternal(DispatcherGatewayService createdDispatcherService) {
	Preconditions.checkState(dispatcherService == null, "The DispatcherGatewayService can only be set once.");
	dispatcherService = createdDispatcherService;
	dispatcherGatewayFuture.complete(createdDispatcherService.getGateway());
	FutureUtils.forward(createdDispatcherService.getShutDownFuture(), shutDownFuture);
}
 
Example 8
Source File: DispatcherResourceManagerComponent.java    From flink with Apache License 2.0 4 votes vote down vote up
private void registerShutDownFuture() {
	FutureUtils.forward(dispatcherRunner.getShutDownFuture(), shutDownFuture);
}