-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BEAM-8543] Dataflow streaming timers are not strictly time ordered when set earlier mid-bundle #11924
[BEAM-8543] Dataflow streaming timers are not strictly time ordered when set earlier mid-bundle #11924
Changes from all commits
9c1dd6a
b73c770
fba4c3d
032bde5
abd93ec
dc7b614
10487f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,12 @@ | |
import com.google.api.services.dataflow.model.SideInputInfo; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.PriorityQueue; | ||
import java.util.Set; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
@@ -519,7 +521,7 @@ public void start( | |
synchronizedProcessingTime); | ||
|
||
this.cachedFiredTimers = null; | ||
this.cachedFiredUserTimers = null; | ||
this.toBeFiredTimersOrdered = null; | ||
} | ||
|
||
public void flushState() { | ||
|
@@ -559,28 +561,67 @@ public <W extends BoundedWindow> TimerData getNextFiredTimer(Coder<W> windowCode | |
return nextTimer; | ||
} | ||
|
||
// Lazily initialized | ||
private Iterator<TimerData> cachedFiredUserTimers = null; | ||
private PriorityQueue<TimerData> toBeFiredTimersOrdered = null; | ||
|
||
// to track if timer is reset earlier mid-bundle. | ||
// Map of timer's id to timer's firing time to check | ||
// the actual firing time of a timer. | ||
private Map<String, Instant> firedTimer = new HashMap<>(); | ||
|
||
public <W extends BoundedWindow> TimerData getNextFiredUserTimer(Coder<W> windowCoder) { | ||
if (cachedFiredUserTimers == null) { | ||
cachedFiredUserTimers = | ||
FluentIterable.<Timer>from(StreamingModeExecutionContext.this.getFiredTimers()) | ||
.filter( | ||
timer -> | ||
WindmillTimerInternals.isUserTimer(timer) | ||
&& timer.getStateFamily().equals(stateFamily)) | ||
.transform( | ||
timer -> | ||
WindmillTimerInternals.windmillTimerToTimerData( | ||
WindmillNamespacePrefix.USER_NAMESPACE_PREFIX, timer, windowCoder)) | ||
.iterator(); | ||
if (toBeFiredTimersOrdered == null) { | ||
|
||
toBeFiredTimersOrdered = new PriorityQueue<>(Comparator.comparing(TimerData::getTimestamp)); | ||
FluentIterable.from(StreamingModeExecutionContext.this.getFiredTimers()) | ||
.filter( | ||
timer -> | ||
WindmillTimerInternals.isUserTimer(timer) | ||
&& timer.getStateFamily().equals(stateFamily)) | ||
.transform( | ||
timer -> | ||
WindmillTimerInternals.windmillTimerToTimerData( | ||
WindmillNamespacePrefix.USER_NAMESPACE_PREFIX, timer, windowCoder)) | ||
.iterator() | ||
.forEachRemaining( | ||
timerData -> { | ||
firedTimer.put( | ||
timerData.getTimerId() + '+' + timerData.getTimerFamilyId(), | ||
timerData.getTimestamp()); | ||
toBeFiredTimersOrdered.add(timerData); | ||
}); | ||
} | ||
|
||
if (!cachedFiredUserTimers.hasNext()) { | ||
Instant currentInputWatermark = userTimerInternals.currentInputWatermarkTime(); | ||
|
||
if (userTimerInternals.hasTimerBefore(currentInputWatermark)) { | ||
List<TimerData> currentTimers = userTimerInternals.getCurrentTimers(); | ||
|
||
for (TimerData timerData : currentTimers) { | ||
firedTimer.put( | ||
timerData.getTimerId() + '+' + timerData.getTimerFamilyId(), | ||
timerData.getTimestamp()); | ||
toBeFiredTimersOrdered.add(timerData); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kennknowles for comment. This doesn't look right to me, as I don't think we should be modifying the WindmillTimerInternals here. I think we just want to merge the timer modifications from processing the workitem into this priority queue; note that if timers are deleted, we need to detect that as well and remove from the priority queue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea I don't actually understand what this block is for. FWIW to do timer deletion/reset cheaply without building a bespoke data structure just keep a map from id to firing time or tombstone. This way, whenever a timer comes up in the prio queue you pull out the actual time for it from the map. If it is actually set for another time, don't fire it. If it is obsolete, don't fire it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @reuvenlax done |
||
|
||
TimerData nextTimer = null; | ||
|
||
// fire timer only if its timestamp matched. Else it is either reset or obsolete. | ||
while (!toBeFiredTimersOrdered.isEmpty()) { | ||
nextTimer = toBeFiredTimersOrdered.poll(); | ||
String timerUniqueId = nextTimer.getTimerId() + '+' + nextTimer.getTimerFamilyId(); | ||
if (firedTimer.containsKey(timerUniqueId) | ||
&& firedTimer.get(timerUniqueId).isEqual(nextTimer.getTimestamp())) { | ||
break; | ||
} else { | ||
nextTimer = null; | ||
} | ||
} | ||
|
||
if (nextTimer == null) { | ||
return null; | ||
} | ||
TimerData nextTimer = cachedFiredUserTimers.next(); | ||
|
||
// User timers must be explicitly deleted when delivered, to release the implied hold | ||
userTimerInternals.deleteTimer(nextTimer); | ||
return nextTimer; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment about what are the keys and values of this map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added