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