Search in sources :

Example 1 with PrestoSparkServiceWaitTimeMetrics

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);
        }
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) PrestoSparkServiceWaitTimeMetrics(com.facebook.presto.spark.PrestoSparkServiceWaitTimeMetrics) SparkException(org.apache.spark.SparkException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

PrestoSparkServiceWaitTimeMetrics (com.facebook.presto.spark.PrestoSparkServiceWaitTimeMetrics)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SparkException (org.apache.spark.SparkException)1