public boolean elapsed() { return elapsed(TimeUnit.NANOSECONDS) > spanNanos; }
public boolean sync(Span span) throws InterruptedException { synchronized (listener) { while (!done) { span.wait(listener); if (done || span.elapsed()) { return done; } } return done; } }
public long remaining(TimeUnit unit) { return unit.convert(Long.max(spanNanos - elapsed(TimeUnit.NANOSECONDS), 0L), TimeUnit.NANOSECONDS); }
protected synchronized T getInfo() throws IOException { if (info == null || span.elapsed()) { span = Span.start(TTL_NANOS, TimeUnit.NANOSECONDS); info = calculateInfo(); } return info; }
@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); } }
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); } }