Search in sources :

Example 1 with OptionalConsumer

use of org.apache.flink.util.OptionalConsumer in project flink by apache.

the class LeaderRetrievalHandler method channelRead0.

@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));
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) SimpleChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) Logger(org.slf4j.Logger) RoutedRequest(org.apache.flink.runtime.rest.handler.router.RoutedRequest) GatewayRetriever(org.apache.flink.runtime.webmonitor.retriever.GatewayRetriever) LoggerFactory(org.slf4j.LoggerFactory) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) Preconditions(org.apache.flink.util.Preconditions) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) OptionalConsumer(org.apache.flink.util.OptionalConsumer) Map(java.util.Map) HandlerUtils(org.apache.flink.runtime.rest.handler.util.HandlerUtils) HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) Nonnull(javax.annotation.Nonnull) Time(org.apache.flink.api.common.time.Time) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody)

Aggregations

Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 Time (org.apache.flink.api.common.time.Time)1 RoutedRequest (org.apache.flink.runtime.rest.handler.router.RoutedRequest)1 HandlerUtils (org.apache.flink.runtime.rest.handler.util.HandlerUtils)1 ErrorResponseBody (org.apache.flink.runtime.rest.messages.ErrorResponseBody)1 RestfulGateway (org.apache.flink.runtime.webmonitor.RestfulGateway)1 GatewayRetriever (org.apache.flink.runtime.webmonitor.retriever.GatewayRetriever)1 ChannelHandler (org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler)1 ChannelHandlerContext (org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler)1 HttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest)1 HttpResponseStatus (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus)1 OptionalConsumer (org.apache.flink.util.OptionalConsumer)1 Preconditions (org.apache.flink.util.Preconditions)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1