rkhachatryan commented on a change in pull request #10151: [FLINK-14231] Handle
the processing-time timers before closing operator to properly support endInput
URL: https://github.com/apache/flink/pull/10151#discussion_r346734529
##########
File path:
flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/ProcessingTimeServiceImpl.java
##########
@@ -39,11 +63,110 @@ public long getCurrentProcessingTime() {
@Override
public ScheduledFuture<?> registerTimer(long timestamp,
ProcessingTimeCallback target) {
- return timerService.registerTimer(timestamp,
processingTimeCallbackWrapper.apply(target));
+ if (isQuiesced) {
+ return new NeverCompleteFuture(
+
ProcessingTimeServiceUtil.getProcessingTimeDelay(timestamp,
getCurrentProcessingTime()));
+ }
+
+ final TimerScheduledFuture<?> timer = new
TimerScheduledFuture<>();
+ timer.setTimer(timerService.registerTimer(timestamp,
invokeCallbackAndRemovePending(timer, target)));
+
+ pendingTimers.put(timer, true);
+ if (timer.isDone()) {
+ removePendingTimers(timer);
+ }
+ return timer;
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(ProcessingTimeCallback
callback, long initialDelay, long period) {
return
timerService.scheduleAtFixedRate(processingTimeCallbackWrapper.apply(callback),
initialDelay, period);
}
+
+ void quiesce() {
+ isQuiesced = true;
+ completeTimersDoneFutureIfQuiesced();
+ }
+
+ CompletableFuture<?> getTimersDoneFutureAfterQuiescing() {
+ return timersDoneFutureAfterQuiescing;
+ }
+
+ @VisibleForTesting
+ int getNumPendingTimers() {
+ return pendingTimers.size();
+ }
+
+ private ProcessingTimeCallback
invokeCallbackAndRemovePending(TimerScheduledFuture<?> timer,
ProcessingTimeCallback target) {
+ ProcessingTimeCallback originalCallback =
processingTimeCallbackWrapper.apply(target);
+
+ return timestamp -> {
+ try {
+ originalCallback.onProcessingTime(timestamp);
+ } finally {
+ removePendingTimers(timer);
+ }
+ };
+ }
+
+ private void removePendingTimers(TimerScheduledFuture<?> timer) {
+ pendingTimers.remove(timer);
+ completeTimersDoneFutureIfQuiesced();
+ }
+
+ private void completeTimersDoneFutureIfQuiesced() {
+ if (isQuiesced && pendingTimers.size() == 0) {
+ timersDoneFutureAfterQuiescing.complete(null);
+ }
+ }
+
+ private class TimerScheduledFuture<V> implements ScheduledFuture<V> {
+
+ private ScheduledFuture<?> timer;
Review comment:
If we stick with Map or set, then it's better to have this class final and
it's fields immutable.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services