public static Span start(long span, TimeUnit unit) { return new Span(span, unit); }
public boolean elapsed() { return elapsed(TimeUnit.NANOSECONDS) > spanNanos; }
private void cancelJob(Throwable th) { cancelJobSafely(metadataProvider, th); final WaitForStateSubscriber cancelSubscriber = new WaitForStateSubscriber(this, EnumSet.of(ActivityState.STOPPED)); final Span span = Span.start(2, TimeUnit.MINUTES); InvokeUtil.doUninterruptibly(() -> { if (!cancelSubscriber.sync(span)) { ExitUtil.halt(ExitUtil.EC_FAILED_TO_CANCEL_ACTIVE_START_STOP); } }); }
/** * Sleep for the minimum of the duration or the remaining of this span * * @param sleep * the amount to sleep * @param unit * the unit of the amount * @throws InterruptedException */ public void sleep(long sleep, TimeUnit unit) throws InterruptedException { TimeUnit.NANOSECONDS.sleep(Math.min(remaining(TimeUnit.NANOSECONDS), unit.toNanos(sleep))); }
public void loopUntilExhausted(ThrowingAction action) throws Exception { loopUntilExhausted(action, 0, TimeUnit.NANOSECONDS); }
public long remaining(TimeUnit unit) { return unit.convert(Long.max(spanNanos - elapsed(TimeUnit.NANOSECONDS), 0L), TimeUnit.NANOSECONDS); }
public void wait(Object monitor) throws InterruptedException { TimeUnit.NANOSECONDS.timedWait(monitor, remaining(TimeUnit.NANOSECONDS)); }
public static <T> T retryUntilSuccessOrExhausted(Span span, ComputingAction<T> action, IRetryPolicy policy, IDelay delay) throws HyracksDataException { Throwable failure; int attempt = 0; do { attempt++; try { return action.compute(); } catch (Throwable th) { failure = th; if (!policy.retry(th)) { break; } try { LOGGER.log(Level.WARN, "Failure executing action {} for the {} time", action, attempt, failure); span.sleep(delay.calculate(attempt), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw HyracksDataException.create(e); } } } while (!span.elapsed()); LOGGER.log(Level.WARN, "Final Failure executing action {} after {} attempts", action, attempt, failure); throw HyracksDataException.create(failure); } }
@Override public void run() { try { LOGGER.info("Ensuring all tasks of CC {} have completed", ccId); final Span maxWaitTime = Span.start(2, TimeUnit.MINUTES); while (!maxWaitTime.elapsed()) { removeCompleted(); if (runningTasks.isEmpty()) { break; } LOGGER.info("{} tasks are still running", runningTasks.size()); TimeUnit.SECONDS.sleep(1); // Check once a second } if (runningTasks.isEmpty()) { LOGGER.info("All tasks of CC {} have completed", ccId); ncs.notifyTasksCompleted(ccId); } else { LOGGER.error("{} tasks associated with CC {} failed to complete after {}ms. Giving up", runningTasks.size(), ccId, TIMEOUT); logPendingTasks(); ExitUtil.halt(ExitUtil.EC_NC_FAILED_TO_ABORT_ALL_PREVIOUS_TASKS); } } catch (Throwable th) { LOGGER.error("Failed to abort all previous tasks associated with CC {}", ccId, th); ExitUtil.halt(ExitUtil.EC_NC_FAILED_TO_ABORT_ALL_PREVIOUS_TASKS); } }