Search in sources :

Example 1 with RestfulGateway

use of org.apache.flink.runtime.webmonitor.RestfulGateway in project flink by apache.

the class JobManagerLogListHandler method handleRequest.

@Override
protected CompletableFuture<LogListInfo> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
    if (logDir == null) {
        return CompletableFuture.completedFuture(new LogListInfo(Collections.emptyList()));
    }
    final File[] logFiles = logDir.listFiles();
    if (logFiles == null) {
        return FutureUtils.completedExceptionally(new IOException("Could not list files in " + logDir));
    }
    final List<LogInfo> logs = Arrays.stream(logFiles).filter(File::isFile).map(logFile -> new LogInfo(logFile.getName(), logFile.length(), logFile.lastModified())).collect(Collectors.toList());
    return CompletableFuture.completedFuture(new LogListInfo(logs));
}
Also used : LogListInfo(org.apache.flink.runtime.rest.messages.LogListInfo) Arrays(java.util.Arrays) GatewayRetriever(org.apache.flink.runtime.webmonitor.retriever.GatewayRetriever) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) LogInfo(org.apache.flink.runtime.rest.messages.LogInfo) MessageHeaders(org.apache.flink.runtime.rest.messages.MessageHeaders) Collectors(java.util.stream.Collectors) File(java.io.File) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) EmptyMessageParameters(org.apache.flink.runtime.rest.messages.EmptyMessageParameters) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) List(java.util.List) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Map(java.util.Map) LogListInfo(org.apache.flink.runtime.rest.messages.LogListInfo) HandlerRequest(org.apache.flink.runtime.rest.handler.HandlerRequest) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) Time(org.apache.flink.api.common.time.Time) AbstractRestHandler(org.apache.flink.runtime.rest.handler.AbstractRestHandler) Nullable(javax.annotation.Nullable) LogInfo(org.apache.flink.runtime.rest.messages.LogInfo) IOException(java.io.IOException) File(java.io.File)

Example 2 with RestfulGateway

use of org.apache.flink.runtime.webmonitor.RestfulGateway in project flink by apache.

the class RestServerEndpointITCase method setup.

@Before
public void setup() throws Exception {
    config.setString(WebOptions.UPLOAD_DIR, temporaryFolder.newFolder().getCanonicalPath());
    defaultSSLContext = SSLContext.getDefault();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    final SSLContext sslClientContext = SSLUtils.createRestSSLContext(config, true);
    if (sslClientContext != null) {
        SSLContext.setDefault(sslClientContext);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslClientContext.getSocketFactory());
    }
    RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder().build();
    final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () -> CompletableFuture.completedFuture(mockRestfulGateway);
    testHandler = new TestHandler(mockGatewayRetriever, RpcUtils.INF_TIMEOUT);
    TestVersionHandler testVersionHandler = new TestVersionHandler(mockGatewayRetriever, RpcUtils.INF_TIMEOUT);
    TestRestHandler<RestfulGateway, EmptyRequestBody, EmptyResponseBody, EmptyMessageParameters> testVersionSelectionHandler1 = new TestRestHandler<>(mockGatewayRetriever, TestVersionSelectionHeaders1.INSTANCE, FutureUtils.completedExceptionally(new RestHandlerException("test failure 1", HttpResponseStatus.OK)));
    TestRestHandler<RestfulGateway, EmptyRequestBody, EmptyResponseBody, EmptyMessageParameters> testVersionSelectionHandler2 = new TestRestHandler<>(mockGatewayRetriever, TestVersionSelectionHeaders2.INSTANCE, FutureUtils.completedExceptionally(new RestHandlerException("test failure 2", HttpResponseStatus.ACCEPTED)));
    testUploadHandler = new TestUploadHandler(mockGatewayRetriever, RpcUtils.INF_TIMEOUT);
    final StaticFileServerHandler<RestfulGateway> staticFileServerHandler = new StaticFileServerHandler<>(mockGatewayRetriever, RpcUtils.INF_TIMEOUT, temporaryFolder.getRoot());
    serverEndpoint = TestRestServerEndpoint.builder(config).withHandler(new TestHeaders(), testHandler).withHandler(TestUploadHeaders.INSTANCE, testUploadHandler).withHandler(testVersionHandler).withHandler(testVersionSelectionHandler1).withHandler(testVersionSelectionHandler2).withHandler(WebContentHandlerSpecification.getInstance(), staticFileServerHandler).buildAndStart();
    restClient = new TestRestClient(config);
    serverAddress = serverEndpoint.getServerAddress();
}
Also used : SSLContext(javax.net.ssl.SSLContext) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) StaticFileServerHandler(org.apache.flink.runtime.rest.handler.legacy.files.StaticFileServerHandler) TestRestHandler(org.apache.flink.runtime.rest.util.TestRestHandler) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) EmptyMessageParameters(org.apache.flink.runtime.rest.messages.EmptyMessageParameters) Before(org.junit.Before)

Example 3 with RestfulGateway

use of org.apache.flink.runtime.webmonitor.RestfulGateway in project flink by apache.

the class RestServerSSLAuthITCase method testConnectFailure.

@Test
public void testConnectFailure() throws Exception {
    RestClient restClient = null;
    RestServerEndpoint serverEndpoint = null;
    try {
        RestfulGateway restfulGateway = new TestingRestfulGateway.Builder().build();
        RestServerEndpointITCase.TestVersionHandler testVersionHandler = new RestServerEndpointITCase.TestVersionHandler(() -> CompletableFuture.completedFuture(restfulGateway), RpcUtils.INF_TIMEOUT);
        serverEndpoint = TestRestServerEndpoint.builder(serverConfig).withHandler(testVersionHandler.getMessageHeaders(), testVersionHandler).buildAndStart();
        restClient = new RestServerEndpointITCase.TestRestClient(clientConfig);
        CompletableFuture<EmptyResponseBody> response = restClient.sendRequest(serverEndpoint.getServerAddress().getHostName(), serverEndpoint.getServerAddress().getPort(), RestServerEndpointITCase.TestVersionHeaders.INSTANCE, EmptyMessageParameters.getInstance(), EmptyRequestBody.getInstance(), Collections.emptyList());
        response.get(60, TimeUnit.SECONDS);
        fail("should never complete normally");
    } catch (ExecutionException exception) {
        // that is what we want
        assertTrue(ExceptionUtils.findThrowable(exception, SSLException.class).isPresent());
    } finally {
        if (restClient != null) {
            restClient.shutdown(timeout);
        }
        if (serverEndpoint != null) {
            serverEndpoint.close();
        }
    }
}
Also used : TestRestServerEndpoint(org.apache.flink.runtime.rest.util.TestRestServerEndpoint) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) SSLException(javax.net.ssl.SSLException) ExecutionException(java.util.concurrent.ExecutionException) 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) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 4 with RestfulGateway

use of org.apache.flink.runtime.webmonitor.RestfulGateway 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 5 with RestfulGateway

use of org.apache.flink.runtime.webmonitor.RestfulGateway in project flink by apache.

the class JobCancellationHandlerTest method testResponse.

private static void testResponse(Function<JobID, CompletableFuture<Acknowledge>> cancelJobFunction, ThrowingConsumer<CompletableFuture<EmptyResponseBody>, Exception> assertion) throws Exception {
    final RestfulGateway gateway = createGateway(cancelJobFunction);
    final JobCancellationHandler jobCancellationHandler = createHandler(gateway);
    final JobCancellationMessageParameters messageParameters = jobCancellationHandler.getMessageHeaders().getUnresolvedMessageParameters().resolveJobId(new JobID());
    final CompletableFuture<EmptyResponseBody> cancellationFuture = jobCancellationHandler.handleRequest(HandlerRequest.create(EmptyRequestBody.getInstance(), messageParameters), gateway);
    assertion.accept(cancellationFuture);
}
Also used : JobCancellationMessageParameters(org.apache.flink.runtime.rest.messages.JobCancellationMessageParameters) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) JobID(org.apache.flink.api.common.JobID)

Aggregations

RestfulGateway (org.apache.flink.runtime.webmonitor.RestfulGateway)13 TestingRestfulGateway (org.apache.flink.runtime.webmonitor.TestingRestfulGateway)8 Test (org.junit.Test)7 CompletableFuture (java.util.concurrent.CompletableFuture)5 JobID (org.apache.flink.api.common.JobID)5 Time (org.apache.flink.api.common.time.Time)5 EmptyRequestBody (org.apache.flink.runtime.rest.messages.EmptyRequestBody)5 RestHandlerException (org.apache.flink.runtime.rest.handler.RestHandlerException)4 EmptyResponseBody (org.apache.flink.runtime.rest.messages.EmptyResponseBody)4 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Nonnull (javax.annotation.Nonnull)3 FlinkJobNotFoundException (org.apache.flink.runtime.messages.FlinkJobNotFoundException)3 AbstractRestHandler (org.apache.flink.runtime.rest.handler.AbstractRestHandler)3 HandlerRequest (org.apache.flink.runtime.rest.handler.HandlerRequest)3 EmptyMessageParameters (org.apache.flink.runtime.rest.messages.EmptyMessageParameters)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2