Search in sources :

Example 31 with RestHandlerException

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

the class StaticFileServerHandler method respondAsLeader.

// ------------------------------------------------------------------------
// Responses to requests
// ------------------------------------------------------------------------
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, T gateway) throws Exception {
    final HttpRequest request = routedRequest.getRequest();
    final String requestPath;
    // make sure we request the "index.html" in case there is a directory request
    if (routedRequest.getPath().endsWith("/")) {
        requestPath = routedRequest.getPath() + "index.html";
    } else {
        requestPath = routedRequest.getPath();
    }
    try {
        respondToRequest(channelHandlerContext, request, requestPath);
    } catch (RestHandlerException rhe) {
        HandlerUtils.sendErrorResponse(channelHandlerContext, routedRequest.getRequest(), new ErrorResponseBody(rhe.getMessage()), rhe.getHttpResponseStatus(), responseHeaders);
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException)

Example 32 with RestHandlerException

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

the class StaticFileServerHandler method respondToRequest.

/**
 * Response when running with leading JobManager.
 */
private void respondToRequest(ChannelHandlerContext ctx, HttpRequest request, String requestPath) throws IOException, ParseException, URISyntaxException, RestHandlerException {
    // convert to absolute path
    final File file = new File(rootPath, requestPath);
    if (!file.exists()) {
        // file does not exist. Try to load it with the classloader
        ClassLoader cl = StaticFileServerHandler.class.getClassLoader();
        try (InputStream resourceStream = cl.getResourceAsStream("web" + requestPath)) {
            boolean success = false;
            try {
                if (resourceStream != null) {
                    URL root = cl.getResource("web");
                    URL requested = cl.getResource("web" + requestPath);
                    if (root != null && requested != null) {
                        URI rootURI = new URI(root.getPath()).normalize();
                        URI requestedURI = new URI(requested.getPath()).normalize();
                        // expected scope.
                        if (!rootURI.relativize(requestedURI).equals(requestedURI)) {
                            logger.debug("Loading missing file from classloader: {}", requestPath);
                            // ensure that directory to file exists.
                            file.getParentFile().mkdirs();
                            Files.copy(resourceStream, file.toPath());
                            success = true;
                        }
                    }
                }
            } catch (Throwable t) {
                logger.error("error while responding", t);
            } finally {
                if (!success) {
                    logger.debug("Unable to load requested file {} from classloader", requestPath);
                    throw new NotFoundException(String.format("Unable to load requested file %s.", requestPath));
                }
            }
        }
    }
    checkFileValidity(file, rootPath, logger);
    // cache validation
    final String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
    if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
        Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        long fileLastModifiedSeconds = file.lastModified() / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            if (logger.isDebugEnabled()) {
                logger.debug("Responding 'NOT MODIFIED' for file '" + file.getAbsolutePath() + '\'');
            }
            sendNotModified(ctx);
            return;
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Responding with file '" + file.getAbsolutePath() + '\'');
    }
    // Don't need to close this manually. Netty's DefaultFileRegion will take care of it.
    final RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Could not find file {}.", file.getAbsolutePath());
        }
        throw new NotFoundException("File not found.");
    }
    try {
        long fileLength = raf.length();
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        if (HttpHeaders.isKeepAlive(request)) {
            response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        HttpHeaders.setContentLength(response, fileLength);
        // write the initial line and the header.
        ctx.write(response);
        // write the content.
        ChannelFuture lastContentFuture;
        if (ctx.pipeline().get(SslHandler.class) == null) {
            ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
            lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        } else {
            lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());
        // HttpChunkedInput will write the end marker (LastHttpContent) for us.
        }
        // close the connection, if no keep-alive is needed
        if (!HttpHeaders.isKeepAlive(request)) {
            lastContentFuture.addListener(ChannelFutureListener.CLOSE);
        }
    } catch (Exception e) {
        raf.close();
        logger.error("Failed to serve file.", e);
        throw new RestHandlerException("Internal server error.", INTERNAL_SERVER_ERROR);
    }
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) InputStream(java.io.InputStream) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.apache.flink.runtime.rest.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) FullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFileRegion(org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion) URI(java.net.URI) URL(java.net.URL) Date(java.util.Date) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.apache.flink.runtime.rest.NotFoundException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) IOException(java.io.IOException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) HttpChunkedInput(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpChunkedInput) RandomAccessFile(java.io.RandomAccessFile) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) SimpleDateFormat(java.text.SimpleDateFormat)

Example 33 with RestHandlerException

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

the class AbstractAggregatingMetricsHandler method handleRequest.

@Override
protected CompletableFuture<AggregatedMetricsResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
    return CompletableFuture.supplyAsync(() -> {
        try {
            fetcher.update();
            List<String> requestedMetrics = request.getQueryParameter(MetricsFilterParameter.class);
            List<MetricsAggregationParameter.AggregationMode> requestedAggregations = request.getQueryParameter(MetricsAggregationParameter.class);
            MetricStore store = fetcher.getMetricStore();
            Collection<? extends MetricStore.ComponentMetricStore> stores = getStores(store, request);
            if (requestedMetrics.isEmpty()) {
                Collection<String> list = getAvailableMetrics(stores);
                return new AggregatedMetricsResponseBody(list.stream().map(AggregatedMetric::new).collect(Collectors.toList()));
            }
            DoubleAccumulator.DoubleMinimumFactory minimumFactory = null;
            DoubleAccumulator.DoubleMaximumFactory maximumFactory = null;
            DoubleAccumulator.DoubleAverageFactory averageFactory = null;
            DoubleAccumulator.DoubleSumFactory sumFactory = null;
            // by default we return all aggregations
            if (requestedAggregations.isEmpty()) {
                minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
                maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
                averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
                sumFactory = DoubleAccumulator.DoubleSumFactory.get();
            } else {
                for (MetricsAggregationParameter.AggregationMode aggregation : requestedAggregations) {
                    switch(aggregation) {
                        case MIN:
                            minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
                            break;
                        case MAX:
                            maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
                            break;
                        case AVG:
                            averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
                            break;
                        case SUM:
                            sumFactory = DoubleAccumulator.DoubleSumFactory.get();
                            break;
                        default:
                            log.warn("Unsupported aggregation specified: {}", aggregation);
                    }
                }
            }
            MetricAccumulatorFactory metricAccumulatorFactory = new MetricAccumulatorFactory(minimumFactory, maximumFactory, averageFactory, sumFactory);
            return getAggregatedMetricValues(stores, requestedMetrics, metricAccumulatorFactory);
        } catch (Exception e) {
            log.warn("Could not retrieve metrics.", e);
            throw new CompletionException(new RestHandlerException("Could not retrieve metrics.", HttpResponseStatus.INTERNAL_SERVER_ERROR));
        }
    }, executor);
}
Also used : MetricStore(org.apache.flink.runtime.rest.handler.legacy.metrics.MetricStore) CompletionException(java.util.concurrent.CompletionException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) AggregatedMetricsResponseBody(org.apache.flink.runtime.rest.messages.job.metrics.AggregatedMetricsResponseBody) AggregatedMetric(org.apache.flink.runtime.rest.messages.job.metrics.AggregatedMetric) CompletionException(java.util.concurrent.CompletionException) MetricsAggregationParameter(org.apache.flink.runtime.rest.messages.job.metrics.MetricsAggregationParameter)

Example 34 with RestHandlerException

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

the class AbstractTaskManagerFileHandler method respondToRequest.

@Override
protected CompletableFuture<Void> respondToRequest(ChannelHandlerContext ctx, HttpRequest httpRequest, HandlerRequest<EmptyRequestBody> handlerRequest, RestfulGateway gateway) throws RestHandlerException {
    final ResourceID taskManagerId = handlerRequest.getPathParameter(TaskManagerIdPathParameter.class);
    String filename = getFileName(handlerRequest);
    final Tuple2<ResourceID, String> taskManagerIdAndFileName = new Tuple2<>(taskManagerId, filename);
    final CompletableFuture<TransientBlobKey> blobKeyFuture;
    try {
        blobKeyFuture = fileBlobKeys.get(taskManagerIdAndFileName);
    } catch (ExecutionException e) {
        final Throwable cause = ExceptionUtils.stripExecutionException(e);
        throw new RestHandlerException("Could not retrieve file blob key future.", HttpResponseStatus.INTERNAL_SERVER_ERROR, cause);
    }
    final CompletableFuture<Void> resultFuture = blobKeyFuture.thenAcceptAsync((TransientBlobKey blobKey) -> {
        final File file;
        try {
            file = transientBlobService.getFile(blobKey);
        } catch (IOException e) {
            throw new CompletionException(new FlinkException("Could not retrieve file from transient blob store.", e));
        }
        try {
            HandlerUtils.transferFile(ctx, file, httpRequest);
        } catch (FlinkException e) {
            throw new CompletionException(new FlinkException("Could not transfer file to client.", e));
        }
    }, ctx.executor());
    return resultFuture.whenComplete((Void ignored, Throwable throwable) -> {
        if (throwable != null) {
            log.error("Failed to transfer file from TaskExecutor {}.", taskManagerId, throwable);
            fileBlobKeys.invalidate(taskManagerId);
            final Throwable strippedThrowable = ExceptionUtils.stripCompletionException(throwable);
            if (strippedThrowable instanceof UnknownTaskExecutorException) {
                throw new CompletionException(new NotFoundException(String.format("Failed to transfer file from TaskExecutor %s because it was unknown.", taskManagerId), strippedThrowable));
            } else {
                throw new CompletionException(new FlinkException(String.format("Failed to transfer file from TaskExecutor %s.", taskManagerId), strippedThrowable));
            }
        }
    });
}
Also used : TransientBlobKey(org.apache.flink.runtime.blob.TransientBlobKey) UnknownTaskExecutorException(org.apache.flink.runtime.resourcemanager.exceptions.UnknownTaskExecutorException) NotFoundException(org.apache.flink.runtime.rest.NotFoundException) IOException(java.io.IOException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) FlinkException(org.apache.flink.util.FlinkException) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) Tuple2(org.apache.flink.api.java.tuple.Tuple2) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 35 with RestHandlerException

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

the class AbstractTaskManagerFileHandler method loadTaskManagerFile.

private CompletableFuture<TransientBlobKey> loadTaskManagerFile(Tuple2<ResourceID, String> taskManagerIdAndFileName) throws RestHandlerException {
    log.debug("Load file from TaskManager {}.", taskManagerIdAndFileName.f0);
    final ResourceManagerGateway resourceManagerGateway = resourceManagerGatewayRetriever.getNow().orElseThrow(() -> {
        log.debug("Could not connect to ResourceManager right now.");
        return new RestHandlerException("Cannot connect to ResourceManager right now. Please try to refresh.", HttpResponseStatus.NOT_FOUND);
    });
    return requestFileUpload(resourceManagerGateway, taskManagerIdAndFileName);
}
Also used : RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) ResourceManagerGateway(org.apache.flink.runtime.resourcemanager.ResourceManagerGateway)

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