public static <T extends Serializable> SerializableOptional<T> of(@Nonnull T value) { return new SerializableOptional<>(value); }
/** * Fail the specified allocation and release the corresponding slot if we have one. * This may triggered by JobManager when some slot allocation failed with rpcTimeout. * Or this could be triggered by TaskManager, when it finds out something went wrong with the slot, * and decided to take it back. * * @param allocationID Represents the allocation which should be failed * @param cause The cause of the failure * @return Optional task executor if it has no more slots registered */ @Override public CompletableFuture<SerializableOptional<ResourceID>> failAllocation(final AllocationID allocationID, final Exception cause) { final PendingRequest pendingRequest = pendingRequests.removeKeyB(allocationID); if (pendingRequest != null) { // request was still pending failPendingRequest(pendingRequest, cause); return CompletableFuture.completedFuture(SerializableOptional.empty()); } else { return tryFailingAllocatedSlot(allocationID, cause); } // TODO: add some unit tests when the previous two are ready, the allocation may failed at any phase }
private void internalFailAllocation(AllocationID allocationId, Exception cause) { final CompletableFuture<SerializableOptional<ResourceID>> emptyTaskExecutorFuture = slotPoolGateway.failAllocation(allocationId, cause); emptyTaskExecutorFuture.thenAcceptAsync( resourceIdOptional -> resourceIdOptional.ifPresent(this::releaseEmptyTaskManager), getMainThreadExecutor()); }
@Override public CompletableFuture<SerializableOptional<String>> requestMetricQueryServiceAddress(Time timeout) { return CompletableFuture.completedFuture(SerializableOptional.ofNullable(metricQueryServicePath)); }
@Override public CompletableFuture<Collection<Tuple2<ResourceID, String>>> requestTaskManagerMetricQueryServicePaths(Time timeout) { final ArrayList<CompletableFuture<Optional<Tuple2<ResourceID, String>>>> metricQueryServicePathFutures = new ArrayList<>(taskExecutors.size()); for (Map.Entry<ResourceID, WorkerRegistration<WorkerType>> workerRegistrationEntry : taskExecutors.entrySet()) { final ResourceID tmResourceId = workerRegistrationEntry.getKey(); final WorkerRegistration<WorkerType> workerRegistration = workerRegistrationEntry.getValue(); final TaskExecutorGateway taskExecutorGateway = workerRegistration.getTaskExecutorGateway(); final CompletableFuture<Optional<Tuple2<ResourceID, String>>> metricQueryServicePathFuture = taskExecutorGateway .requestMetricQueryServiceAddress(timeout) .thenApply(optional -> optional.map(path -> Tuple2.of(tmResourceId, path))); metricQueryServicePathFutures.add(metricQueryServicePathFuture); } return FutureUtils.combineAll(metricQueryServicePathFutures).thenApply( collection -> collection .stream() .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList())); }
/** * Fail the specified allocation and release the corresponding slot if we have one. * This may triggered by JobManager when some slot allocation failed with rpcTimeout. * Or this could be triggered by TaskManager, when it finds out something went wrong with the slot, * and decided to take it back. * * @param allocationID Represents the allocation which should be failed * @param cause The cause of the failure * @return Optional task executor if it has no more slots registered */ @Override public CompletableFuture<SerializableOptional<ResourceID>> failAllocation(final AllocationID allocationID, final Exception cause) { final PendingRequest pendingRequest = pendingRequests.removeKeyB(allocationID); if (pendingRequest != null) { // request was still pending failPendingRequest(pendingRequest, cause); return CompletableFuture.completedFuture(SerializableOptional.empty()); } else { return tryFailingAllocatedSlot(allocationID, cause); } // TODO: add some unit tests when the previous two are ready, the allocation may failed at any phase }
private void internalFailAllocation(AllocationID allocationId, Exception cause) { final CompletableFuture<SerializableOptional<ResourceID>> emptyTaskExecutorFuture = slotPoolGateway.failAllocation(allocationId, cause); emptyTaskExecutorFuture.thenAcceptAsync( resourceIdOptional -> resourceIdOptional.ifPresent(this::releaseEmptyTaskManager), getMainThreadExecutor()); }
@Override public CompletableFuture<SerializableOptional<String>> requestMetricQueryServiceAddress(Time timeout) { return CompletableFuture.completedFuture(SerializableOptional.ofNullable(metricQueryServicePath)); }
@Override public CompletableFuture<Collection<Tuple2<ResourceID, String>>> requestTaskManagerMetricQueryServicePaths(Time timeout) { final ArrayList<CompletableFuture<Optional<Tuple2<ResourceID, String>>>> metricQueryServicePathFutures = new ArrayList<>(taskExecutors.size()); for (Map.Entry<ResourceID, WorkerRegistration<WorkerType>> workerRegistrationEntry : taskExecutors.entrySet()) { final ResourceID tmResourceId = workerRegistrationEntry.getKey(); final WorkerRegistration<WorkerType> workerRegistration = workerRegistrationEntry.getValue(); final TaskExecutorGateway taskExecutorGateway = workerRegistration.getTaskExecutorGateway(); final CompletableFuture<Optional<Tuple2<ResourceID, String>>> metricQueryServicePathFuture = taskExecutorGateway .requestMetricQueryServiceAddress(timeout) .thenApply(optional -> optional.map(path -> Tuple2.of(tmResourceId, path))); metricQueryServicePathFutures.add(metricQueryServicePathFuture); } return FutureUtils.combineAll(metricQueryServicePathFutures).thenApply( collection -> collection .stream() .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList())); }
public static <T extends Serializable> SerializableOptional<T> of(@Nonnull T value) { return new SerializableOptional<>(value); }
private CompletableFuture<SerializableOptional<ResourceID>> tryFailingAllocatedSlot(AllocationID allocationID, Exception cause) { AllocatedSlot allocatedSlot = availableSlots.tryRemove(allocationID); if (allocatedSlot == null) { allocatedSlot = allocatedSlots.remove(allocationID); } if (allocatedSlot != null) { log.debug("Failed allocated slot [{}]: {}", allocationID, cause.getMessage()); // notify TaskExecutor about the failure allocatedSlot.getTaskManagerGateway().freeSlot(allocationID, cause, rpcTimeout); // release the slot. // since it is not in 'allocatedSlots' any more, it will be dropped o return' allocatedSlot.releasePayload(cause); final ResourceID taskManagerId = allocatedSlot.getTaskManagerId(); if (!availableSlots.containsTaskManager(taskManagerId) && !allocatedSlots.containResource(taskManagerId)) { return CompletableFuture.completedFuture(SerializableOptional.of(taskManagerId)); } } return CompletableFuture.completedFuture(SerializableOptional.empty()); }
private CompletableFuture<SerializableOptional<ResourceID>> tryFailingAllocatedSlot(AllocationID allocationID, Exception cause) { AllocatedSlot allocatedSlot = availableSlots.tryRemove(allocationID); if (allocatedSlot == null) { allocatedSlot = allocatedSlots.remove(allocationID); } if (allocatedSlot != null) { log.debug("Failed allocated slot [{}]: {}", allocationID, cause.getMessage()); // notify TaskExecutor about the failure allocatedSlot.getTaskManagerGateway().freeSlot(allocationID, cause, rpcTimeout); // release the slot. // since it is not in 'allocatedSlots' any more, it will be dropped o return' allocatedSlot.releasePayload(cause); final ResourceID taskManagerId = allocatedSlot.getTaskManagerId(); if (!availableSlots.containsTaskManager(taskManagerId) && !allocatedSlots.containResource(taskManagerId)) { return CompletableFuture.completedFuture(SerializableOptional.of(taskManagerId)); } } return CompletableFuture.completedFuture(SerializableOptional.empty()); }