use of com.facebook.presto.spark.PrestoSparkServiceWaitTimeMetrics in project presto by prestodb.
the class PrestoSparkUtils method getActionResultWithTimeout.
public static <T> T getActionResultWithTimeout(JavaFutureAction<T> action, long timeout, TimeUnit timeUnit, Set<PrestoSparkServiceWaitTimeMetrics> waitTimeMetrics) throws SparkException, TimeoutException {
long deadline = System.currentTimeMillis() + timeUnit.toMillis(timeout);
try {
while (true) {
long totalWaitTime = waitTimeMetrics.stream().map(PrestoSparkServiceWaitTimeMetrics::getWaitTime).mapToLong(Duration::toMillis).sum();
long nextTimeoutInMillis = (deadline + totalWaitTime) - System.currentTimeMillis();
if (nextTimeoutInMillis <= 0) {
throw new TimeoutException();
}
try {
return action.get(nextTimeoutInMillis, MILLISECONDS);
} catch (TimeoutException e) {
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
propagateIfPossible(e.getCause(), SparkException.class);
propagateIfPossible(e.getCause(), RuntimeException.class);
// this should never happen
throw new UncheckedExecutionException(e.getCause());
} finally {
if (!action.isDone()) {
action.cancel(true);
}
}
}
Aggregations