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));
}
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));
}
Aggregations