Search in sources :

Example 96 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException 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)

Example 97 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project samza by apache.

the class TestHdfsSystemConsumer method testEmptyStagingDirectory.

/*
   * Ensure that empty staging directory will not break system admin,
   * but should fail system consumer
   */
@Test
public void testEmptyStagingDirectory() throws Exception {
    Map<String, String> configMap = new HashMap<>();
    configMap.put(String.format(HdfsConfig.CONSUMER_PARTITIONER_WHITELIST(), SYSTEM_NAME), ".*avro");
    Config config = new MapConfig(configMap);
    HdfsSystemFactory systemFactory = new HdfsSystemFactory();
    // create admin and do partitioning
    HdfsSystemAdmin systemAdmin = systemFactory.getAdmin(SYSTEM_NAME, config);
    String stream = WORKING_DIRECTORY;
    Set<String> streamNames = new HashSet<>();
    streamNames.add(stream);
    generateAvroDataFiles();
    Map<String, SystemStreamMetadata> streamMetadataMap = systemAdmin.getSystemStreamMetadata(streamNames);
    SystemStreamMetadata systemStreamMetadata = streamMetadataMap.get(stream);
    Assert.assertEquals(NUM_FILES, systemStreamMetadata.getSystemStreamPartitionMetadata().size());
    // create consumer and read from files
    HdfsSystemConsumer systemConsumer = systemFactory.getConsumer(SYSTEM_NAME, config, new NoOpMetricsRegistry());
    Partition partition = new Partition(0);
    SystemStreamPartition ssp = new SystemStreamPartition(SYSTEM_NAME, stream, partition);
    try {
        systemConsumer.register(ssp, "0");
        Assert.fail("Empty staging directory should fail system consumer");
    } catch (UncheckedExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof SamzaException);
    }
}
Also used : Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) HashMap(java.util.HashMap) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) SamzaException(org.apache.samza.SamzaException) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) MapConfig(org.apache.samza.config.MapConfig) HashSet(java.util.HashSet) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 98 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project gerrit by GerritCodeReview.

the class MergeabilityCacheImpl method get.

@Override
public boolean get(ObjectId commit, Ref intoRef, SubmitType submitType, String mergeStrategy, BranchNameKey dest, Repository repo) {
    ObjectId into = intoRef != null ? intoRef.getObjectId() : ObjectId.zeroId();
    EntryKey key = new EntryKey(commit, into, submitType, mergeStrategy);
    try {
        return cache.get(key, () -> {
            if (key.into.equals(ObjectId.zeroId())) {
                // Assume yes on new branch.
                return true;
            }
            try (CodeReviewRevWalk rw = CodeReviewCommit.newRevWalk(repo)) {
                Set<RevCommit> accepted = SubmitDryRun.getAlreadyAccepted(repo, rw);
                accepted.add(rw.parseCommit(key.into));
                accepted.addAll(Arrays.asList(rw.parseCommit(key.commit).getParents()));
                return submitDryRun.run(null, key.submitType, repo, rw, dest, key.into, key.commit, accepted);
            }
        });
    } catch (ExecutionException | UncheckedExecutionException e) {
        logger.atSevere().withCause(e.getCause()).log("Error checking mergeability of %s into %s (%s)", key.commit.name(), key.into.name(), key.submitType.name());
        return false;
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ObjectId(org.eclipse.jgit.lib.ObjectId) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 99 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project gerrit by GerritCodeReview.

the class MultiProgressMonitor method waitForNonFinalTask.

/**
 * Wait for a task managed by a {@link Future}. This call does not expect the worker thread to
 * call {@link #end()}. It is intended to be used to track a non-final task.
 *
 * @param workerFuture a future that returns when worker threads are finished.
 * @param taskTimeoutTime overall timeout for the task; the future is forcefully cancelled if the
 *     task exceeds the timeout. Non-positive values indicate no timeout.
 * @param taskTimeoutUnit unit for overall task timeout.
 * @param cancellationTimeoutTime timeout for the task to react to the cancellation signal; if the
 *     task doesn't terminate within this time it is forcefully cancelled; non-positive values
 *     indicate no timeout.
 * @param cancellationTimeoutUnit unit for the cancellation timeout.
 * @throws TimeoutException if this thread or a worker thread was interrupted, the worker was
 *     cancelled, or timed out waiting for a worker to call {@link #end()}.
 */
public <T> T waitForNonFinalTask(Future<T> workerFuture, long taskTimeoutTime, TimeUnit taskTimeoutUnit, long cancellationTimeoutTime, TimeUnit cancellationTimeoutUnit) throws TimeoutException {
    long overallStart = ticker.read();
    long cancellationNanos = cancellationTimeoutTime > 0 ? NANOSECONDS.convert(cancellationTimeoutTime, cancellationTimeoutUnit) : 0;
    long deadline;
    if (taskTimeoutTime > 0) {
        timeout = Optional.of(NANOSECONDS.convert(taskTimeoutTime, taskTimeoutUnit));
        deadline = overallStart + timeout.get();
    } else {
        deadline = 0;
    }
    synchronized (this) {
        long left = maxIntervalNanos;
        while (!workerFuture.isDone() && !done) {
            long start = ticker.read();
            try {
                // 500ms.
                if (deadlineExceeded || deadline == 0) {
                    // We want to set deadlineExceeded flag as earliest as possible. If it is already
                    // set - there is no reason to wait less than maxIntervalNanos
                    NANOSECONDS.timedWait(this, maxIntervalNanos);
                } else if (start <= deadline) {
                    // if deadlineExceeded is not set, then we should wait until deadline, but no longer
                    // than maxIntervalNanos (because we want to report a progress every maxIntervalNanos).
                    NANOSECONDS.timedWait(this, Math.min(deadline - start + 1, maxIntervalNanos));
                }
            } catch (InterruptedException e) {
                throw new UncheckedExecutionException(e);
            }
            // Send an update on every wakeup (manual or spurious), but only move
            // the spinner every maxInterval.
            long now = ticker.read();
            if (deadline > 0 && now > deadline) {
                if (!deadlineExceeded) {
                    logger.atFine().log("deadline exceeded after %sms, signaling cancellation (timeout=%sms, task=%s(%s))", MILLISECONDS.convert(now - overallStart, NANOSECONDS), MILLISECONDS.convert(now - deadline, NANOSECONDS), taskKind, taskName);
                }
                deadlineExceeded = true;
                // cancellation and return gracefully.
                if (now > deadline + cancellationNanos) {
                    // The worker didn't react to the cancellation, cancel it forcefully by an interrupt.
                    workerFuture.cancel(true);
                    forcefulTermination = true;
                    if (workerFuture.isCancelled()) {
                        logger.atWarning().log("MultiProgressMonitor worker killed after %sms, cancelled (timeout=%sms, task=%s(%s))", MILLISECONDS.convert(now - overallStart, NANOSECONDS), MILLISECONDS.convert(now - deadline, NANOSECONDS), taskKind, taskName);
                        if (taskKind == TaskKind.RECEIVE_COMMITS) {
                            cancellationMetrics.countForcefulReceiveTimeout();
                        }
                    }
                    break;
                }
            }
            left -= now - start;
            if (left <= 0) {
                moveSpinner();
                left = maxIntervalNanos;
            }
            sendUpdate();
        }
        if (deadlineExceeded && !forcefulTermination && taskKind == TaskKind.RECEIVE_COMMITS) {
            cancellationMetrics.countGracefulReceiveTimeout();
        }
        wakeUp();
    }
    // 2 x maxIntervalNanos to finish up and return.
    try {
        return workerFuture.get(2 * maxIntervalNanos, NANOSECONDS);
    } catch (InterruptedException | CancellationException e) {
        logger.atWarning().withCause(e).log("unable to finish processing (task=%s(%s))", taskKind, taskName);
        throw new UncheckedExecutionException(e);
    } catch (TimeoutException e) {
        workerFuture.cancel(true);
        throw e;
    } catch (ExecutionException e) {
        throw new UncheckedExecutionException(e);
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) CancellationException(java.util.concurrent.CancellationException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 100 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project spf4j by zolyfarkas.

the class RetryAspect method retriedMethod.

@Around(value = "execution(@org.spf4j.annotations.Retry * *(..)) && @annotation(org.spf4j.annotations.Retry annot)", argNames = "pjp,annot")
@SuppressFBWarnings("FII_USE_METHOD_REFERENCE")
public Object retriedMethod(final ProceedingJoinPoint pjp, final Retry annot) throws Throwable {
    try (ExecutionContext ctx = ExecutionContexts.start(pjp.toShortString(), annot.timeout(), annot.units())) {
        Callable c = () -> {
            try {
                return pjp.proceed();
            } catch (Exception | Error e) {
                throw e;
            } catch (Throwable ex) {
                throw new UncheckedExecutionException(ex);
            }
        };
        String retryPolicyName = annot.retryPolicyName();
        if ("".equals(retryPolicyName)) {
            return RetryPolicy.defaultPolicy().call(c, Exception.class, ctx.getDeadlineNanos());
        } else {
            return POLICIES.get(retryPolicyName).call(c, Exception.class, ctx.getDeadlineNanos());
        }
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Callable(java.util.concurrent.Callable) Around(org.aspectj.lang.annotation.Around) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)101 ExecutionException (java.util.concurrent.ExecutionException)60 IOException (java.io.IOException)31 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Test (org.junit.Test)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 TimeoutException (java.util.concurrent.TimeoutException)6 Stopwatch (com.google.common.base.Stopwatch)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 HashMap (java.util.HashMap)5 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 InputStream (java.io.InputStream)4 Callable (java.util.concurrent.Callable)4 SirixIOException (org.sirix.exception.SirixIOException)4 TypeSignature (com.facebook.presto.common.type.TypeSignature)3