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);
}
}
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);
}
}
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);
}
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));
}
}
});
}
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);
}
Aggregations