Search in sources :

Example 16 with RestHandlerException

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

the class JarListHandler method handleRequest.

@Override
protected CompletableFuture<JarListInfo> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
    final String localAddress;
    Preconditions.checkState(localAddressFuture.isDone());
    try {
        localAddress = localAddressFuture.get();
    } catch (Exception e) {
        return FutureUtils.completedExceptionally(e);
    }
    return CompletableFuture.supplyAsync(() -> {
        try {
            final File[] list = getJarFiles();
            final List<JarListInfo.JarFileInfo> jarFileList = new ArrayList<>(list.length);
            for (File f : list) {
                // separate the uuid and the name parts.
                String id = f.getName();
                int startIndex = id.indexOf("_");
                if (startIndex < 0) {
                    continue;
                }
                String name = id.substring(startIndex + 1);
                if (name.length() < 5 || !name.endsWith(".jar")) {
                    continue;
                }
                List<JarListInfo.JarEntryInfo> jarEntryList = new ArrayList<>();
                String[] classes = new String[0];
                try (JarFile jar = new JarFile(f)) {
                    Manifest manifest = jar.getManifest();
                    String assemblerClass = null;
                    if (manifest != null) {
                        assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS);
                        if (assemblerClass == null) {
                            assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS);
                        }
                    }
                    if (assemblerClass != null) {
                        classes = assemblerClass.split(",");
                    }
                } catch (IOException ignored) {
                // we simply show no entries here
                }
                // show every entry class that can be loaded later on.
                for (String clazz : classes) {
                    clazz = clazz.trim();
                    try (PackagedProgram program = PackagedProgram.newBuilder().setJarFile(f).setEntryPointClassName(clazz).setConfiguration(configuration).build()) {
                        JarListInfo.JarEntryInfo jarEntryInfo = new JarListInfo.JarEntryInfo(clazz, program.getDescription());
                        jarEntryList.add(jarEntryInfo);
                    } catch (Exception ignored) {
                    // ignore jar files which throw an error upon creating a
                    // PackagedProgram
                    }
                }
                jarFileList.add(new JarListInfo.JarFileInfo(id, name, f.lastModified(), jarEntryList));
            }
            return new JarListInfo(localAddress, jarFileList);
        } catch (Exception e) {
            throw new CompletionException(new FlinkException("Failed to fetch jar list.", e));
        }
    }, executor);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) FlinkException(org.apache.flink.util.FlinkException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) FlinkException(org.apache.flink.util.FlinkException) PackagedProgram(org.apache.flink.client.program.PackagedProgram) CompletionException(java.util.concurrent.CompletionException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 17 with RestHandlerException

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

the class RestClusterClientTest method testSendIsNotRetriableIfHttpNotFound.

/**
 * Tests that the send operation is not being retried when receiving a NOT_FOUND return code.
 */
@Test
public void testSendIsNotRetriableIfHttpNotFound() throws Exception {
    final String exceptionMessage = "test exception";
    final PingRestHandler pingRestHandler = new PingRestHandler(FutureUtils.completedExceptionally(new RestHandlerException(exceptionMessage, HttpResponseStatus.NOT_FOUND)));
    try (final TestRestServerEndpoint restServerEndpoint = createRestServerEndpoint(pingRestHandler)) {
        RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort());
        try {
            restClusterClient.sendRequest(PingRestHandlerHeaders.INSTANCE).get();
            fail("The rest request should have failed.");
        } catch (Exception e) {
            assertThat(ExceptionUtils.findThrowableWithMessage(e, exceptionMessage).isPresent(), is(true));
        } finally {
            restClusterClient.close();
        }
    }
}
Also used : TestRestServerEndpoint(org.apache.flink.runtime.rest.util.TestRestServerEndpoint) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) FlinkException(org.apache.flink.util.FlinkException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RestClientException(org.apache.flink.runtime.rest.util.RestClientException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) ConfigurationException(org.apache.flink.util.ConfigurationException) Test(org.junit.Test)

Example 18 with RestHandlerException

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

the class JobSubmitHandlerTest method testRejectionOnCountMismatch.

@Test
public void testRejectionOnCountMismatch() throws Exception {
    final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();
    try (ObjectOutputStream objectOut = new ObjectOutputStream(Files.newOutputStream(jobGraphFile))) {
        objectOut.writeObject(JobGraphTestUtils.emptyJobGraph());
    }
    final Path countExceedingFile = TEMPORARY_FOLDER.newFile().toPath();
    TestingDispatcherGateway.Builder builder = TestingDispatcherGateway.newBuilder();
    builder.setBlobServerPort(blobServer.getPort()).setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get())).setHostname("localhost");
    DispatcherGateway mockGateway = builder.build();
    JobSubmitHandler handler = new JobSubmitHandler(() -> CompletableFuture.completedFuture(mockGateway), RpcUtils.INF_TIMEOUT, Collections.emptyMap(), TestingUtils.defaultExecutor(), configuration);
    JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.getFileName().toString(), Collections.emptyList(), Collections.emptyList());
    try {
        handler.handleRequest(HandlerRequest.create(request, EmptyMessageParameters.getInstance(), Arrays.asList(jobGraphFile.toFile(), countExceedingFile.toFile())), mockGateway).get();
    } catch (Exception e) {
        ExceptionUtils.findThrowable(e, candidate -> candidate instanceof RestHandlerException && candidate.getMessage().contains("count"));
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) BlobServer(org.apache.flink.runtime.blob.BlobServer) Tuple2(org.apache.flink.api.java.tuple.Tuple2) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) RunWith(org.junit.runner.RunWith) ExceptionUtils(org.apache.flink.util.ExceptionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) DispatcherGateway(org.apache.flink.runtime.dispatcher.DispatcherGateway) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) EmptyMessageParameters(org.apache.flink.runtime.rest.messages.EmptyMessageParameters) ArrayList(java.util.ArrayList) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) After(org.junit.After) JobGraphTestUtils(org.apache.flink.runtime.jobgraph.JobGraphTestUtils) TestLogger(org.apache.flink.util.TestLogger) HandlerRequest(org.apache.flink.runtime.rest.handler.HandlerRequest) ObjectOutputStream(java.io.ObjectOutputStream) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) JobSubmitRequestBody(org.apache.flink.runtime.rest.messages.job.JobSubmitRequestBody) Before(org.junit.Before) VoidBlobStore(org.apache.flink.runtime.blob.VoidBlobStore) Files(java.nio.file.Files) Configuration(org.apache.flink.configuration.Configuration) Test(org.junit.Test) IOException(java.io.IOException) RpcUtils(org.apache.flink.runtime.rpc.RpcUtils) DistributedCache(org.apache.flink.api.common.cache.DistributedCache) Acknowledge(org.apache.flink.runtime.messages.Acknowledge) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest) TestingUtils(org.apache.flink.testutils.TestingUtils) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) JobSubmitRequestBody(org.apache.flink.runtime.rest.messages.job.JobSubmitRequestBody) ObjectOutputStream(java.io.ObjectOutputStream) DispatcherGateway(org.apache.flink.runtime.dispatcher.DispatcherGateway) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) IOException(java.io.IOException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 19 with RestHandlerException

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

the class JobSubmitHandlerTest method testSerializationFailureHandling.

@Test
public void testSerializationFailureHandling() throws Exception {
    final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();
    DispatcherGateway mockGateway = TestingDispatcherGateway.newBuilder().setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get())).build();
    JobSubmitHandler handler = new JobSubmitHandler(() -> CompletableFuture.completedFuture(mockGateway), RpcUtils.INF_TIMEOUT, Collections.emptyMap(), TestingUtils.defaultExecutor(), configuration);
    JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.toString(), Collections.emptyList(), Collections.emptyList());
    try {
        handler.handleRequest(HandlerRequest.create(request, EmptyMessageParameters.getInstance()), mockGateway);
        Assert.fail();
    } catch (RestHandlerException rhe) {
        Assert.assertEquals(HttpResponseStatus.BAD_REQUEST, rhe.getHttpResponseStatus());
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) BlobServer(org.apache.flink.runtime.blob.BlobServer) Tuple2(org.apache.flink.api.java.tuple.Tuple2) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) RunWith(org.junit.runner.RunWith) ExceptionUtils(org.apache.flink.util.ExceptionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) DispatcherGateway(org.apache.flink.runtime.dispatcher.DispatcherGateway) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) EmptyMessageParameters(org.apache.flink.runtime.rest.messages.EmptyMessageParameters) ArrayList(java.util.ArrayList) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) After(org.junit.After) JobGraphTestUtils(org.apache.flink.runtime.jobgraph.JobGraphTestUtils) TestLogger(org.apache.flink.util.TestLogger) HandlerRequest(org.apache.flink.runtime.rest.handler.HandlerRequest) ObjectOutputStream(java.io.ObjectOutputStream) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) JobSubmitRequestBody(org.apache.flink.runtime.rest.messages.job.JobSubmitRequestBody) Before(org.junit.Before) VoidBlobStore(org.apache.flink.runtime.blob.VoidBlobStore) Files(java.nio.file.Files) Configuration(org.apache.flink.configuration.Configuration) Test(org.junit.Test) IOException(java.io.IOException) RpcUtils(org.apache.flink.runtime.rpc.RpcUtils) DistributedCache(org.apache.flink.api.common.cache.DistributedCache) Acknowledge(org.apache.flink.runtime.messages.Acknowledge) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest) TestingUtils(org.apache.flink.testutils.TestingUtils) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) JobSubmitRequestBody(org.apache.flink.runtime.rest.messages.job.JobSubmitRequestBody) DispatcherGateway(org.apache.flink.runtime.dispatcher.DispatcherGateway) TestingDispatcherGateway(org.apache.flink.runtime.webmonitor.TestingDispatcherGateway) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 20 with RestHandlerException

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

the class StopWithSavepointHandlersTest method testTriggerSavepointNoDirectory.

@Test
public void testTriggerSavepointNoDirectory() throws Exception {
    TestingRestfulGateway testingRestfulGateway = new TestingRestfulGateway.Builder().setStopWithSavepointFunction((AsynchronousJobOperationKey operationKey, String directory, SavepointFormatType formatType) -> CompletableFuture.completedFuture(Acknowledge.get())).build();
    try {
        savepointTriggerHandler.handleRequest(triggerSavepointRequestWithDefaultDirectory(), testingRestfulGateway).get();
        fail("Expected exception not thrown.");
    } catch (RestHandlerException rhe) {
        assertThat(rhe.getMessage(), equalTo("Config key [state.savepoints.dir] is not set. " + "Property [targetDirectory] must be provided."));
        assertThat(rhe.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST));
    }
}
Also used : AsynchronousJobOperationKey(org.apache.flink.runtime.rest.handler.job.AsynchronousJobOperationKey) SavepointFormatType(org.apache.flink.core.execution.SavepointFormatType) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) 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