Search in sources :

Example 11 with RestHandlerException

use of org.apache.flink.runtime.rest.handler.RestHandlerException 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 12 with RestHandlerException

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

the class AbstractAsynchronousOperationHandlersTest method testUnknownTriggerId.

/**
 * Tests that an querying an unknown trigger id will return an exceptionally completed future.
 */
@Test
public void testUnknownTriggerId() throws Exception {
    try {
        testingStatusHandler.handleRequest(statusOperationRequest(new TriggerId()), DUMMY_GATEWAY).get();
        fail("This should have failed with a RestHandlerException.");
    } catch (ExecutionException ee) {
        final Optional<RestHandlerException> optionalRestHandlerException = ExceptionUtils.findThrowable(ee, RestHandlerException.class);
        assertThat(optionalRestHandlerException.isPresent(), is(true));
        final RestHandlerException restHandlerException = optionalRestHandlerException.get();
        assertThat(restHandlerException.getMessage(), containsString("Operation not found"));
        assertThat(restHandlerException.getHttpResponseStatus(), is(HttpResponseStatus.NOT_FOUND));
    }
}
Also used : Optional(java.util.Optional) TriggerId(org.apache.flink.runtime.rest.messages.TriggerId) ExecutionException(java.util.concurrent.ExecutionException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) Test(org.junit.Test)

Example 13 with RestHandlerException

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

the class JobExecutionResultHandlerTest method testPropagateFlinkJobNotFoundExceptionAsRestHandlerException.

@Test
public void testPropagateFlinkJobNotFoundExceptionAsRestHandlerException() throws Exception {
    final TestingRestfulGateway testingRestfulGateway = new TestingRestfulGateway.Builder().setRequestJobStatusFunction(jobId -> FutureUtils.completedExceptionally(new FlinkJobNotFoundException(jobId))).build();
    try {
        jobExecutionResultHandler.handleRequest(testRequest, testingRestfulGateway).get();
        fail("Expected exception not thrown");
    } catch (final ExecutionException e) {
        final Throwable cause = ExceptionUtils.stripCompletionException(e.getCause());
        assertThat(cause, instanceOf(RestHandlerException.class));
        assertThat(((RestHandlerException) cause).getHttpResponseStatus(), equalTo(HttpResponseStatus.NOT_FOUND));
    }
}
Also used : QueueStatus(org.apache.flink.runtime.rest.messages.queue.QueueStatus) Matchers.not(org.hamcrest.Matchers.not) ExceptionUtils(org.apache.flink.util.ExceptionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) JobStatus(org.apache.flink.api.common.JobStatus) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) Assert.assertThat(org.junit.Assert.assertThat) JobResult(org.apache.flink.runtime.jobmaster.JobResult) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Matchers.nullValue(org.hamcrest.Matchers.nullValue) TestLogger(org.apache.flink.util.TestLogger) HandlerRequest(org.apache.flink.runtime.rest.handler.HandlerRequest) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) ArchivedExecutionGraph(org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph) ArchivedExecutionGraphBuilder(org.apache.flink.runtime.rest.handler.legacy.utils.ArchivedExecutionGraphBuilder) Test(org.junit.Test) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) ExecutionException(java.util.concurrent.ExecutionException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) JobID(org.apache.flink.api.common.JobID) JobMessageParameters(org.apache.flink.runtime.rest.messages.JobMessageParameters) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Time(org.apache.flink.api.common.time.Time) JobExecutionResultResponseBody(org.apache.flink.runtime.rest.messages.job.JobExecutionResultResponseBody) ArchivedExecutionGraphBuilder(org.apache.flink.runtime.rest.handler.legacy.utils.ArchivedExecutionGraphBuilder) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) Test(org.junit.Test)

Example 14 with RestHandlerException

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

the class JarRunHandler method handleRequest.

@Override
@VisibleForTesting
public CompletableFuture<JarRunResponseBody> handleRequest(@Nonnull final HandlerRequest<JarRunRequestBody> request, @Nonnull final DispatcherGateway gateway) throws RestHandlerException {
    final Configuration effectiveConfiguration = new Configuration(configuration);
    effectiveConfiguration.set(DeploymentOptions.ATTACHED, false);
    effectiveConfiguration.set(DeploymentOptions.TARGET, EmbeddedExecutor.NAME);
    final JarHandlerContext context = JarHandlerContext.fromRequest(request, jarDir, log);
    context.applyToConfiguration(effectiveConfiguration);
    SavepointRestoreSettings.toConfiguration(getSavepointRestoreSettings(request), effectiveConfiguration);
    final PackagedProgram program = context.toPackagedProgram(effectiveConfiguration);
    return CompletableFuture.supplyAsync(() -> applicationRunner.run(gateway, program, effectiveConfiguration), executor).handle((jobIds, throwable) -> {
        program.close();
        if (throwable != null) {
            throw new CompletionException(new RestHandlerException("Could not execute application.", HttpResponseStatus.BAD_REQUEST, throwable));
        } else if (jobIds.isEmpty()) {
            throw new CompletionException(new RestHandlerException("No jobs included in application.", HttpResponseStatus.BAD_REQUEST));
        }
        return new JarRunResponseBody(jobIds.get(0));
    });
}
Also used : PackagedProgram(org.apache.flink.client.program.PackagedProgram) Configuration(org.apache.flink.configuration.Configuration) JarHandlerContext(org.apache.flink.runtime.webmonitor.handlers.utils.JarHandlerUtils.JarHandlerContext) CompletionException(java.util.concurrent.CompletionException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 15 with RestHandlerException

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

the class JarUploadHandlerTest method testFailedUpload.

@Test
public void testFailedUpload() throws Exception {
    final Path uploadedFile = jarDir.resolve("FooBazzleExample.jar");
    final HandlerRequest<EmptyRequestBody> request = createRequest(uploadedFile);
    try {
        jarUploadHandler.handleRequest(request, mockDispatcherGateway).get();
        fail("Expected exception not thrown.");
    } catch (final ExecutionException e) {
        final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause());
        assertThat(throwable, instanceOf(RestHandlerException.class));
        final RestHandlerException restHandlerException = (RestHandlerException) throwable;
        assertThat(restHandlerException.getMessage(), containsString("Could not move uploaded jar file"));
        assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.INTERNAL_SERVER_ERROR));
    }
}
Also used : Path(java.nio.file.Path) ExecutionException(java.util.concurrent.ExecutionException) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) Test(org.junit.Test)

Aggregations

RestHandlerException (org.apache.flink.runtime.rest.handler.RestHandlerException)39 Test (org.junit.Test)13 IOException (java.io.IOException)11 CompletionException (java.util.concurrent.CompletionException)11 EmptyRequestBody (org.apache.flink.runtime.rest.messages.EmptyRequestBody)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 HandlerRequest (org.apache.flink.runtime.rest.handler.HandlerRequest)8 File (java.io.File)7 ExecutionException (java.util.concurrent.ExecutionException)7 JobID (org.apache.flink.api.common.JobID)7 Time (org.apache.flink.api.common.time.Time)6 RestfulGateway (org.apache.flink.runtime.webmonitor.RestfulGateway)6 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 Map (java.util.Map)5 TestingRestfulGateway (org.apache.flink.runtime.webmonitor.TestingRestfulGateway)5 HttpResponseStatus (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus)5 ExceptionUtils (org.apache.flink.util.ExceptionUtils)5 FutureUtils (org.apache.flink.util.concurrent.FutureUtils)5