Search in sources :

Example 1 with EmptyResponseBody

use of org.apache.flink.runtime.rest.messages.EmptyResponseBody 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 2 with EmptyResponseBody

use of org.apache.flink.runtime.rest.messages.EmptyResponseBody 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 3 with EmptyResponseBody

use of org.apache.flink.runtime.rest.messages.EmptyResponseBody in project flink by apache.

the class RestClientTest method testConnectionClosedHandling.

/**
 * Tests that we fail the operation if the remote connection closes.
 */
@Test
public void testConnectionClosedHandling() throws Exception {
    final Configuration config = new Configuration();
    config.setLong(RestOptions.IDLENESS_TIMEOUT, 5000L);
    try (final ServerSocket serverSocket = new ServerSocket(0);
        final RestClient restClient = new RestClient(config, TestingUtils.defaultExecutor())) {
        final String targetAddress = "localhost";
        final int targetPort = serverSocket.getLocalPort();
        // start server
        final CompletableFuture<Socket> socketCompletableFuture = CompletableFuture.supplyAsync(CheckedSupplier.unchecked(() -> NetUtils.acceptWithoutTimeout(serverSocket)));
        final CompletableFuture<EmptyResponseBody> responseFuture = restClient.sendRequest(targetAddress, targetPort, new TestMessageHeaders(), EmptyMessageParameters.getInstance(), EmptyRequestBody.getInstance(), Collections.emptyList());
        Socket connectionSocket = null;
        try {
            connectionSocket = socketCompletableFuture.get(TIMEOUT, TimeUnit.SECONDS);
        } catch (TimeoutException ignored) {
            // could not establish a server connection --> see that the response failed
            socketCompletableFuture.cancel(true);
        }
        if (connectionSocket != null) {
            // close connection
            connectionSocket.close();
        }
        try {
            responseFuture.get();
        } catch (ExecutionException ee) {
            if (!ExceptionUtils.findThrowable(ee, IOException.class).isPresent()) {
                throw ee;
            }
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ServerSocket(java.net.ServerSocket) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.flink.shaded.netty4.io.netty.channel.ConnectTimeoutException) Test(org.junit.Test)

Example 4 with EmptyResponseBody

use of org.apache.flink.runtime.rest.messages.EmptyResponseBody in project flink by apache.

the class RestClientTest method testInvalidVersionRejection.

@Test
public void testInvalidVersionRejection() throws Exception {
    try (final RestClient restClient = new RestClient(new Configuration(), Executors.directExecutor())) {
        CompletableFuture<EmptyResponseBody> invalidVersionResponse = restClient.sendRequest(unroutableIp, 80, new TestMessageHeaders(), EmptyMessageParameters.getInstance(), EmptyRequestBody.getInstance(), Collections.emptyList(), RestAPIVersion.V0);
        Assert.fail("The request should have been rejected due to a version mismatch.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) Test(org.junit.Test)

Example 5 with EmptyResponseBody

use of org.apache.flink.runtime.rest.messages.EmptyResponseBody 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

EmptyResponseBody (org.apache.flink.runtime.rest.messages.EmptyResponseBody)8 Test (org.junit.Test)5 ExecutionException (java.util.concurrent.ExecutionException)4 RestfulGateway (org.apache.flink.runtime.webmonitor.RestfulGateway)4 Configuration (org.apache.flink.configuration.Configuration)3 TestingRestfulGateway (org.apache.flink.runtime.webmonitor.TestingRestfulGateway)3 IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 TimeoutException (java.util.concurrent.TimeoutException)2 EmptyMessageParameters (org.apache.flink.runtime.rest.messages.EmptyMessageParameters)2 EmptyRequestBody (org.apache.flink.runtime.rest.messages.EmptyRequestBody)2 JobCancellationMessageParameters (org.apache.flink.runtime.rest.messages.JobCancellationMessageParameters)2 TestRestHandler (org.apache.flink.runtime.rest.util.TestRestHandler)2 TestRestServerEndpoint (org.apache.flink.runtime.rest.util.TestRestServerEndpoint)2 ConnectTimeoutException (org.apache.flink.shaded.netty4.io.netty.channel.ConnectTimeoutException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 SSLContext (javax.net.ssl.SSLContext)1 SSLException (javax.net.ssl.SSLException)1 JobID (org.apache.flink.api.common.JobID)1