Search in sources :

Example 1 with RoutedRequest

use of org.apache.flink.runtime.rest.handler.router.RoutedRequest in project flink by apache.

the class AbstractHandlerTest method testFileCleanup.

@Test
public void testFileCleanup() throws Exception {
    final Path dir = temporaryFolder.newFolder().toPath();
    final Path file = dir.resolve("file");
    Files.createFile(file);
    RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder().build();
    final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () -> CompletableFuture.completedFuture(mockRestfulGateway);
    CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
    TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);
    RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(), Unpooled.wrappedBuffer(new byte[0]));
    RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);
    Attribute<FileUploads> attribute = new SimpleAttribute();
    attribute.set(new FileUploads(dir));
    Channel channel = mock(Channel.class);
    when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    when(context.channel()).thenReturn(channel);
    handler.respondAsLeader(context, routerRequest, mockRestfulGateway);
    // the (asynchronous) request processing is not yet complete so the files should still exist
    Assert.assertTrue(Files.exists(file));
    requestProcessingCompleteFuture.complete(null);
    Assert.assertFalse(Files.exists(file));
}
Also used : Path(java.nio.file.Path) HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) AttributeKey(org.apache.flink.shaded.netty4.io.netty.util.AttributeKey) CompletableFuture(java.util.concurrent.CompletableFuture) RouteResult(org.apache.flink.runtime.rest.handler.router.RouteResult) RoutedRequest(org.apache.flink.runtime.rest.handler.router.RoutedRequest) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) Test(org.junit.Test)

Example 2 with RoutedRequest

use of org.apache.flink.runtime.rest.handler.router.RoutedRequest 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

RoutedRequest (org.apache.flink.runtime.rest.handler.router.RoutedRequest)2 RestfulGateway (org.apache.flink.runtime.webmonitor.RestfulGateway)2 ChannelHandlerContext (org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext)2 HttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest)2 Path (java.nio.file.Path)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Nonnull (javax.annotation.Nonnull)1 Time (org.apache.flink.api.common.time.Time)1 RouteResult (org.apache.flink.runtime.rest.handler.router.RouteResult)1 HandlerUtils (org.apache.flink.runtime.rest.handler.util.HandlerUtils)1 ErrorResponseBody (org.apache.flink.runtime.rest.messages.ErrorResponseBody)1 TestingRestfulGateway (org.apache.flink.runtime.webmonitor.TestingRestfulGateway)1 GatewayRetriever (org.apache.flink.runtime.webmonitor.retriever.GatewayRetriever)1 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)1 ChannelHandler (org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler)1 SimpleChannelInboundHandler (org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler)1 DefaultFullHttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest)1 HttpResponseStatus (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus)1 AttributeKey (org.apache.flink.shaded.netty4.io.netty.util.AttributeKey)1