org.apache.flink.util.OptionalConsumer Java Examples

The following examples show how to use org.apache.flink.util.OptionalConsumer. 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: SlotManagerImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void fulfillPendingSlotRequestWithPendingTaskManagerSlot(PendingSlotRequest pendingSlotRequest) throws ResourceManagerException {
	ResourceProfile resourceProfile = pendingSlotRequest.getResourceProfile();
	Optional<PendingTaskManagerSlot> pendingTaskManagerSlotOptional = findFreeMatchingPendingTaskManagerSlot(resourceProfile);

	if (!pendingTaskManagerSlotOptional.isPresent()) {
		pendingTaskManagerSlotOptional = allocateResource(resourceProfile);
	}

	OptionalConsumer.of(pendingTaskManagerSlotOptional)
		.ifPresent(pendingTaskManagerSlot -> assignPendingTaskManagerSlot(pendingSlotRequest, pendingTaskManagerSlot))
		.ifNotPresent(() -> {
			// request can not be fulfilled by any free slot or pending slot that can be allocated,
			// check whether it can be fulfilled by allocated slots
			if (failUnfulfillableRequest && !isFulfillableByRegisteredOrPendingSlots(pendingSlotRequest.getResourceProfile())) {
				throw new UnfulfillableSlotRequestException(pendingSlotRequest.getAllocationId(), pendingSlotRequest.getResourceProfile());
			}
		});
}
 
Example #2
Source File: LeaderRetrievalHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(
	ChannelHandlerContext channelHandlerContext,
	RoutedRequest routedRequest) {

	HttpRequest request = routedRequest.getRequest();

	OptionalConsumer<? extends T> optLeaderConsumer = OptionalConsumer.of(leaderRetriever.getNow());

	optLeaderConsumer.ifPresent(
		gateway -> {
			try {
				respondAsLeader(channelHandlerContext, routedRequest, gateway);
			} catch (Exception e) {
				logger.error("Error while responding to the http request.", e);
				HandlerUtils.sendErrorResponse(
					channelHandlerContext,
					request,
					new ErrorResponseBody("Error while responding to the http request."),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					responseHeaders);
			}
		}
	).ifNotPresent(
		() ->
			HandlerUtils.sendErrorResponse(
				channelHandlerContext,
				request,
				new ErrorResponseBody("Service temporarily unavailable due to an ongoing leader election. Please refresh."),
				HttpResponseStatus.SERVICE_UNAVAILABLE,
				responseHeaders));
}
 
Example #3
Source File: LeaderRetrievalHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(
	ChannelHandlerContext channelHandlerContext,
	RoutedRequest routedRequest) {

	HttpRequest request = routedRequest.getRequest();

	OptionalConsumer<? extends T> optLeaderConsumer = OptionalConsumer.of(leaderRetriever.getNow());

	optLeaderConsumer.ifPresent(
		gateway -> {
			try {
				respondAsLeader(channelHandlerContext, routedRequest, gateway);
			} catch (Exception e) {
				logger.error("Error while responding to the http request.", e);
				HandlerUtils.sendErrorResponse(
					channelHandlerContext,
					request,
					new ErrorResponseBody("Error while responding to the http request."),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					responseHeaders);
			}
		}
	).ifNotPresent(
		() ->
			HandlerUtils.sendErrorResponse(
				channelHandlerContext,
				request,
				new ErrorResponseBody("Service temporarily unavailable due to an ongoing leader election. Please refresh."),
				HttpResponseStatus.SERVICE_UNAVAILABLE,
				responseHeaders));
}
 
Example #4
Source File: TaskExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void reportPayload(ResourceID resourceID, AllocatedSlotReport allocatedSlotReport) {
	validateRunsInMainThread();
	OptionalConsumer.of(jobTable.getConnection(allocatedSlotReport.getJobId()))
		.ifPresent(
			jobManagerConnection -> {
				syncSlotsWithSnapshotFromJobMaster(jobManagerConnection.getJobManagerGateway(), allocatedSlotReport);
			})
		.ifNotPresent(() -> log.debug("Ignoring allocated slot report from job {} because there is no active leader.", allocatedSlotReport.getJobId()));

}
 
Example #5
Source File: LeaderRetrievalHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(
	ChannelHandlerContext channelHandlerContext,
	RoutedRequest routedRequest) {

	HttpRequest request = routedRequest.getRequest();

	OptionalConsumer<? extends T> optLeaderConsumer = OptionalConsumer.of(leaderRetriever.getNow());

	optLeaderConsumer.ifPresent(
		gateway -> {
			try {
				respondAsLeader(channelHandlerContext, routedRequest, gateway);
			} catch (Exception e) {
				logger.error("Error while responding to the http request.", e);
				HandlerUtils.sendErrorResponse(
					channelHandlerContext,
					request,
					new ErrorResponseBody("Error while responding to the http request."),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					responseHeaders);
			}
		}
	).ifNotPresent(
		() ->
			HandlerUtils.sendErrorResponse(
				channelHandlerContext,
				request,
				new ErrorResponseBody("Service temporarily unavailable due to an ongoing leader election. Please refresh."),
				HttpResponseStatus.SERVICE_UNAVAILABLE,
				responseHeaders));
}
 
Example #6
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Tries to allocate a slot for the given slot request. If there is no slot available, the
 * resource manager is informed to allocate more resources and a timeout for the request is
 * registered.
 *
 * @param pendingSlotRequest to allocate a slot for
 * @throws ResourceManagerException if the slot request failed or is unfulfillable
 */
private void internalRequestSlot(PendingSlotRequest pendingSlotRequest) throws ResourceManagerException {
	final ResourceProfile resourceProfile = pendingSlotRequest.getResourceProfile();

	OptionalConsumer.of(findMatchingSlot(resourceProfile))
		.ifPresent(taskManagerSlot -> allocateSlot(taskManagerSlot, pendingSlotRequest))
		.ifNotPresent(() -> fulfillPendingSlotRequestWithPendingTaskManagerSlot(pendingSlotRequest));
}