/** * Checks if a call to this method occurs later than {@code period} since the timer was initiated or reset. If that is the * case the method returns true, otherwise it returns false. Each time this method returns true, the counter is reset * (re-initiated) and a new cycle will start. * * @return true if the time elapsed since the last call returning true is greater than {@code period}. Returns false * otherwise. */ public boolean isExpiredResetOnTrue() { final boolean expired = Time.nanoTime() - start >= periodNanos; if (expired) { start = Time.nanoTime(); } return expired; } }
public static boolean isThreadWaiting(Thread t) { if (!SIMULATING.get()) { throw new IllegalStateException("Must be in simulation mode"); } AtomicLong time = THREAD_SLEEP_TIMES_NANOS.get(t); return !t.isAlive() || time != null && nanoTime() < time.longValue(); }
/** * Creates a class that mimics a single threaded timer that expires periodically. If a call to {@link * #isExpiredResetOnTrue()} occurs later than {@code period} since the timer was initiated or reset, this method returns * true. Each time the method returns true the counter is reset. The timer starts with the specified time delay. * * @param delay the initial delay before the timer starts * @param period the period between calls {@link #isExpiredResetOnTrue()} * @param timeUnit the time unit of delay and period */ public Timer(long delay, long period, TimeUnit timeUnit) { this.delay = delay; this.period = period; this.timeUnit = timeUnit; periodNanos = timeUnit.toNanos(period); start = Time.nanoTime() + timeUnit.toNanos(delay); }
public static void parkNanos(long nanos) throws InterruptedException { if (nanos > 0) { if (SIMULATING.get()) { simulatedSleepUntilNanos(nanoTime() + nanos); } else { LockSupport.parkNanos(nanos); } } }
@Override public int readyMessageCount() { int count = 0; final long currentTimeNanos = Time.nanoTime(); for (RetrySchedule retrySchedule : retrySchedules) { if (retrySchedule.retry(currentTimeNanos)) { ++count; } else { break; //Stop counting when past current time } } return count; }
@Override public Map<TopicPartition, Long> earliestRetriableOffsets() { final Map<TopicPartition, Long> tpToEarliestRetriableOffset = new HashMap<>(); final long currentTimeNanos = Time.nanoTime(); for (RetrySchedule retrySchedule : retrySchedules) { if (retrySchedule.retry(currentTimeNanos)) { final KafkaSpoutMessageId msgId = retrySchedule.msgId; final TopicPartition tpForMessage = new TopicPartition(msgId.topic(), msgId.partition()); tpToEarliestRetriableOffset.merge(tpForMessage, msgId.offset(), Math::min); } else { break; // Stop searching as soon as passed current time } } LOG.debug("Topic partitions with entries ready to be retried [{}] ", tpToEarliestRetriableOffset); return tpToEarliestRetriableOffset; }
private long nextTime(KafkaSpoutMessageId msgId) { Validate.isTrue(msgId.numFails() > 0, "nextTime assumes the message has failed at least once"); final long currentTimeNanos = Time.nanoTime(); final long nextTimeNanos = msgId.numFails() == 1 // numFails = 1, 2, 3, ... ? currentTimeNanos + initialDelay.lengthNanos : currentTimeNanos + delayPeriod.lengthNanos * (long)(Math.pow(2, msgId.numFails() - 1)); return Math.min(nextTimeNanos, currentTimeNanos + maxDelay.lengthNanos); }
@Override public boolean isReady(KafkaSpoutMessageId msgId) { boolean retry = false; if (isScheduled(msgId)) { final long currentTimeNanos = Time.nanoTime(); for (RetrySchedule retrySchedule : retrySchedules) { if (retrySchedule.retry(currentTimeNanos)) { if (retrySchedule.msgId.equals(msgId)) { retry = true; LOG.debug("Found entry to retry {}", retrySchedule); break; //Stop searching if the message is known to be ready for retry } } else { LOG.debug("Entry to retry not found {}", retrySchedule); break; // Stop searching as soon as passed current time } } } return retry; }
@Override public void execute(Tuple tuple, BasicOutputCollector collector) { sleep.simulateProcessAndExecTime(executorIndex, Time.nanoTime(), null , () -> { String sentence = tuple.getString(0); for (String word: sentence.split("\\s+")) { collector.emit(new Values(word, 1)); } }); }
Long currTime = Time.nanoTime(); Long startTime = schedulingStartTimeNs.get(); return TimeUnit.NANOSECONDS.toMillis(startTime == null ?
/** * Checks if a call to this method occurs later than {@code period} since the timer was initiated or reset. If that is the * case the method returns true, otherwise it returns false. Each time this method returns true, the counter is reset * (re-initiated) and a new cycle will start. * * @return true if the time elapsed since the last call returning true is greater than {@code period}. Returns false * otherwise. */ public boolean isExpiredResetOnTrue() { final boolean expired = Time.nanoTime() - start >= periodNanos; if (expired) { start = Time.nanoTime(); } return expired; } }
public static boolean isThreadWaiting(Thread t) { if(!simulating.get()) { throw new IllegalStateException("Must be in simulation mode"); } AtomicLong time; synchronized(sleepTimesLock) { time = threadSleepTimesNanos.get(t); } return !t.isAlive() || time!=null && nanoTime() < time.longValue(); } }
/** * Creates a class that mimics a single threaded timer that expires periodically. If a call to {@link * #isExpiredResetOnTrue()} occurs later than {@code period} since the timer was initiated or reset, this method returns * true. Each time the method returns true the counter is reset. The timer starts with the specified time delay. * * @param delay the initial delay before the timer starts * @param period the period between calls {@link #isExpiredResetOnTrue()} * @param timeUnit the time unit of delay and period */ public Timer(long delay, long period, TimeUnit timeUnit) { this.delay = delay; this.period = period; this.timeUnit = timeUnit; periodNanos = timeUnit.toNanos(period); start = Time.nanoTime() + timeUnit.toNanos(delay); }
public static void sleepNanos(long nanos) throws InterruptedException { sleepUntilNanos(nanoTime() + nanos); }
@Override public int readyMessageCount() { int count = 0; final long currentTimeNanos = Time.nanoTime(); for (RetrySchedule retrySchedule : retrySchedules) { if (retrySchedule.retry(currentTimeNanos)) { ++count; } else { break; //Stop counting when past current time } } return count; }
private long nextTime(KafkaSpoutMessageId msgId) { Validate.isTrue(msgId.numFails() > 0, "nextTime assumes the message has failed at least once"); final long currentTimeNanos = Time.nanoTime(); final long nextTimeNanos = msgId.numFails() == 1 // numFails = 1, 2, 3, ... ? currentTimeNanos + initialDelay.lengthNanos : currentTimeNanos + delayPeriod.lengthNanos * (long)(Math.pow(2, msgId.numFails()-1)); return Math.min(nextTimeNanos, currentTimeNanos + maxDelay.lengthNanos); }
@Override public Map<TopicPartition, Long> earliestRetriableOffsets() { final Map<TopicPartition, Long> tpToEarliestRetriableOffset = new HashMap<>(); final long currentTimeNanos = Time.nanoTime(); for (RetrySchedule retrySchedule : retrySchedules) { if (retrySchedule.retry(currentTimeNanos)) { final KafkaSpoutMessageId msgId = retrySchedule.msgId; final TopicPartition tpForMessage = new TopicPartition(msgId.topic(), msgId.partition()); final Long currentLowestOffset = tpToEarliestRetriableOffset.get(tpForMessage); if(currentLowestOffset != null) { tpToEarliestRetriableOffset.put(tpForMessage, Math.min(currentLowestOffset, msgId.offset())); } else { tpToEarliestRetriableOffset.put(tpForMessage, msgId.offset()); } } else { break; // Stop searching as soon as passed current time } } LOG.debug("Topic partitions with entries ready to be retried [{}] ", tpToEarliestRetriableOffset); return tpToEarliestRetriableOffset; }