public void addTimer(Slice keyBytes, TimerData timer) { Set<Slice> timersForKey = activeTimers.get(keyBytes); if (timersForKey == null) { timersForKey = new HashSet<>(); } try { Slice timerBytes = new Slice(CoderUtils.encodeToByteArray(timerDataCoder, timer)); timersForKey.add(timerBytes); } catch (CoderException e) { throw new RuntimeException(e); } activeTimers.put(keyBytes, timersForKey); this.minTimestamp = Math.min(minTimestamp, timer.getTimestamp().getMillis()); }
TimerData timerData = CoderUtils.decodeFromByteArray(timers.timerDataCoder, timerIt.next().buffer); if (timerData.getTimestamp().isBefore(currentTime)) { toFire.put(keyWithTimers.getKey(), timerData); timerIt.remove();
/** * Returns when the next timer in the given time domain will fire, or {@code null} if there are no * timers scheduled in that time domain. */ @Nullable public Instant getNextTimer(TimeDomain domain) { try { switch (domain) { case EVENT_TIME: return watermarkTimers.first().getTimestamp(); case PROCESSING_TIME: return processingTimers.first().getTimestamp(); case SYNCHRONIZED_PROCESSING_TIME: return synchronizedProcessingTimers.first().getTimestamp(); default: throw new IllegalArgumentException("Unexpected time domain: " + domain); } } catch (NoSuchElementException exc) { return null; } }
/** @deprecated use {@link #setTimer(StateNamespace, String, Instant, TimeDomain)}. */ @Deprecated @Override public void setTimer(TimerData timerData) { WindowTracing.trace("{}.setTimer: {}", getClass().getSimpleName(), timerData); @Nullable TimerData existing = existingTimers.get(timerData.getNamespace(), timerData.getTimerId()); if (existing == null) { existingTimers.put(timerData.getNamespace(), timerData.getTimerId(), timerData); timersForDomain(timerData.getDomain()).add(timerData); } else { checkArgument( timerData.getDomain().equals(existing.getDomain()), "Attempt to set %s for time domain %s, but it is already set for time domain %s", timerData.getTimerId(), timerData.getDomain(), existing.getDomain()); if (!timerData.getTimestamp().equals(existing.getTimestamp())) { NavigableSet<TimerData> timers = timersForDomain(timerData.getDomain()); timers.remove(existing); timers.add(timerData); existingTimers.put(timerData.getNamespace(), timerData.getTimerId(), timerData); } } }
/** * {@inheritDoc}. * * <p>Used for sorting {@link TimerData} by timestamp. Furthermore, we compare timers by all the * other fields so that {@code compareTo()} only returns 0 when {@code equals()} returns 0. This * ensures consistent sort order. */ @Override public int compareTo(TimerData that) { if (this.equals(that)) { return 0; } ComparisonChain chain = ComparisonChain.start() .compare(this.getTimestamp(), that.getTimestamp()) .compare(this.getDomain(), that.getDomain()) .compare(this.getTimerId(), that.getTimerId()); if (chain.result() == 0 && !this.getNamespace().equals(that.getNamespace())) { // Obtaining the stringKey may be expensive; only do so if required chain = chain.compare(getNamespace().stringKey(), that.getNamespace().stringKey()); } return chain.result(); } }
@Override public void fireTimer(Object key, Collection<TimerData> timerDataSet) { pushbackDoFnRunner.startBundle(); @SuppressWarnings("unchecked") Coder<Object> keyCoder = (Coder) currentKeyStateInternals.getKeyCoder(); ((StateInternalsProxy) currentKeyStateInternals).setKey(key); currentKeyTimerInternals.setContext( key, keyCoder, new Instant(this.currentInputWatermark), new Instant(this.currentOutputWatermark)); for (TimerData timerData : timerDataSet) { StateNamespace namespace = timerData.getNamespace(); checkArgument(namespace instanceof WindowNamespace); BoundedWindow window = ((WindowNamespace<?>) namespace).getWindow(); pushbackDoFnRunner.onTimer( timerData.getTimerId(), window, timerData.getTimestamp(), timerData.getDomain()); } pushbackDoFnRunner.finishBundle(); }
private void fireTimer( TimerInternals.TimerData timer, BiConsumer<String, WindowedValue> timerConsumer) { StateNamespace namespace = timer.getNamespace(); Preconditions.checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); Instant timestamp = timer.getTimestamp(); WindowedValue<KV<Object, Timer>> timerValue = WindowedValue.of( KV.of(currentTimerKey, Timer.of(timestamp, new byte[0])), timestamp, Collections.singleton(window), PaneInfo.NO_FIRING); timerConsumer.accept(timer.getTimerId(), timerValue); }
private void fireTimer( TimerInternals.TimerData timer, BiConsumer<String, WindowedValue> timerConsumer) { StateNamespace namespace = timer.getNamespace(); Preconditions.checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); Instant timestamp = timer.getTimestamp(); WindowedValue<KV<Object, Timer>> timerValue = WindowedValue.of( KV.of(currentTimerKey, Timer.of(timestamp, new byte[0])), timestamp, Collections.singleton(window), PaneInfo.NO_FIRING); timerConsumer.accept(timer.getTimerId(), timerValue); }
public void fireTimer(InternalTimer<?, TimerData> timer) { TimerInternals.TimerData timerData = timer.getNamespace(); StateNamespace namespace = timerData.getNamespace(); // This is a user timer, so namespace must be WindowNamespace checkArgument(namespace instanceof WindowNamespace); BoundedWindow window = ((WindowNamespace) namespace).getWindow(); pushbackDoFnRunner.onTimer(timerData.getTimerId(), window, timerData.getTimestamp(), timerData.getDomain()); }
@Override public void encode(TimerData timer, OutputStream outStream) throws CoderException, IOException { STRING_CODER.encode(timer.getTimerId(), outStream); STRING_CODER.encode(timer.getNamespace().stringKey(), outStream); INSTANT_CODER.encode(timer.getTimestamp(), outStream); STRING_CODER.encode(timer.getDomain().name(), outStream); }
@Nullable private TimerData removeNextTimer(Instant currentTime, TimeDomain domain) { NavigableSet<TimerData> timers = timersForDomain(domain); if (!timers.isEmpty() && currentTime.isAfter(timers.first().getTimestamp())) { TimerData timer = timers.pollFirst(); existingTimers.remove(timer.getNamespace(), timer.getTimerId()); return timer; } else { return null; } } }
private void fireTimer(TimerInternals.TimerData timer, DoFnRunner<KV<K, V>, OutputT> doFnRunner) { StateNamespace namespace = timer.getNamespace(); checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); doFnRunner.onTimer(timer.getTimerId(), window, timer.getTimestamp(), timer.getDomain()); }
private void fireTimer(TimerInternals.TimerData timer, DoFnRunner<KV<K, V>, OutputT> doFnRunner) { StateNamespace namespace = timer.getNamespace(); checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); doFnRunner.onTimer(timer.getTimerId(), window, timer.getTimestamp(), timer.getDomain()); }
private static void advanceInputWatermark( InMemoryTimerInternals timerInternals, Instant newInputWatermark, DoFnRunner<?, ?> toTrigger) throws Exception { timerInternals.advanceInputWatermark(newInputWatermark); TimerInternals.TimerData timer; while ((timer = timerInternals.removeNextEventTimer()) != null) { StateNamespace namespace = timer.getNamespace(); checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); toTrigger.onTimer(timer.getTimerId(), window, timer.getTimestamp(), timer.getDomain()); } }
public void fireTimer(InternalTimer<?, TimerData> timer) { TimerInternals.TimerData timerData = timer.getNamespace(); StateNamespace namespace = timerData.getNamespace(); // This is a user timer, so namespace must be WindowNamespace checkArgument(namespace instanceof WindowNamespace); BoundedWindow window = ((WindowNamespace) namespace).getWindow(); timerInternals.cleanupPendingTimer(timerData); pushbackDoFnRunner.onTimer( timerData.getTimerId(), window, timerData.getTimestamp(), timerData.getDomain()); }
public void fireTimer(InternalTimer<?, TimerData> timer) { TimerInternals.TimerData timerData = timer.getNamespace(); StateNamespace namespace = timerData.getNamespace(); // This is a user timer, so namespace must be WindowNamespace checkArgument(namespace instanceof WindowNamespace); BoundedWindow window = ((WindowNamespace) namespace).getWindow(); timerInternals.cleanupPendingTimer(timer.getNamespace()); pushbackDoFnRunner.onTimer( timerData.getTimerId(), window, timerData.getTimestamp(), timerData.getDomain()); }
private void fireTimer( TimerInternals.TimerData timer, DoFnRunner<KV<K, V>, OutputT> doFnRunner) { StateNamespace namespace = timer.getNamespace(); checkArgument(namespace instanceof StateNamespaces.WindowNamespace); BoundedWindow window = ((StateNamespaces.WindowNamespace) namespace).getWindow(); doFnRunner.onTimer(timer.getTimerId(), window, timer.getTimestamp(), timer.getDomain()); }