ferenc-csaky commented on code in PR #29038:
URL: https://github.com/apache/flink/pull/29038#discussion_r4709837401


##########
flink-runtime/src/main/java/org/apache/flink/runtime/source/coordinator/SourceCoordinator.java:
##########
@@ -726,17 +733,18 @@ private void handleReportedWatermark(int subtask, 
Watermark watermark) throws Fl
 
         combinedWatermark
                 .aggregate(subtask, watermark)
-                .ifPresent(
-                        newCombinedWatermark ->
-                                coordinatorStore.computeIfPresent(
-                                        
watermarkAlignmentParams.getWatermarkGroup(),
-                                        (key, oldValue) -> {
-                                            WatermarkAggregator<String> 
watermarkAggregator =
-                                                    
(WatermarkAggregator<String>) oldValue;
-                                            watermarkAggregator.aggregate(
-                                                    operatorName, 
newCombinedWatermark);
-                                            return watermarkAggregator;
-                                        }));
+                .ifPresent(this::updateAggregatedWatermarkOfGroup);
+    }
+
+    private void updateAggregatedWatermarkOfGroup(Watermark 
newCombinedWatermark) {
+        coordinatorStore.computeIfPresent(
+                watermarkAlignmentParams.getWatermarkGroup(),
+                (key, oldValue) -> {
+                    WatermarkAggregator<String> watermarkAggregator =
+                            (WatermarkAggregator<String>) oldValue;
+                    watermarkAggregator.aggregate(operatorName, 
newCombinedWatermark);
+                    return watermarkAggregator;
+                });
     }

Review Comment:
   You're right. Now I did some further testing with the updated code and in a 
group with only this source, that lost entry can stall the job. After the late 
removal the group's aggregator is empty, so the newer coordinator announces 
`Long.MIN_VALUE + drift` and every subtask pauses. Paused readers keep 
reporting the same watermark, so `aggregate()` sees no change and doesn't put 
the entry back.
   
   It's narrow, since the timeout already fails the job through 
`cleanAndFailJob`: it only lasts if the old closing thread survives 
`abortThread`'s interrupts and finishes after the coordinator of that next 
failover has reported. In that case `shutdownExecutorForcefully` can also 
return early on the interrupt, so the comment above `removeFromGroup()` doesn't 
always hold.
   
   It can be closed by removing the entry only while it still holds the 
`Watermark` this coordinator wrote. Each report creates a new instance, so a 
newer coordinator's entry never matches:
   
   ```java
   // SourceCoordinator
   private volatile Watermark watermarkInGroup;
   
   // updateAggregatedWatermarkOfGroup, after aggregate(...)
   watermarkInGroup = newCombinedWatermark;
   
   // removeFromGroup
   ((WatermarkAggregator<String>) oldValue).remove(operatorName, 
watermarkInGroup);
   
   // WatermarkAggregator
   public boolean remove(T key) {
       WatermarkElement watermarkElement = watermarks.get(key);
       return watermarkElement != null && remove(key, 
watermarkElement.watermark);
   }
   
   public boolean remove(T key, Watermark expected) {
       WatermarkElement watermarkElement = watermarks.get(key);
       if (watermarkElement == null || watermarkElement.watermark != expected) {
           return false;
       }
       watermarks.remove(key);
       orderedWatermarks.remove(watermarkElement);
       return true;
   }
   ```
   
   This test fails on the current head and passes with the change; the rest of 
`SourceCoordinatorAlignmentTest` and `SourceCoordinatorTest` pass too:
   
   ```
   expected: WatermarkAlignmentEvent{maxWatermark=1200}
    but was: WatermarkAlignmentEvent{maxWatermark=-9223372036854774808}
   ```
   
   ```java
   @Test
   void testLateCloseOfPreviousCoordinatorKeepsEntryOfCurrentOne() throws 
Exception {
       try (AutoCloseableRegistry closeableRegistry = new 
AutoCloseableRegistry()) {
           CoordinatorStoreImpl store = new CoordinatorStoreImpl();
           SourceCoordinator<?, ?> previous =
                   getAndStartNewSourceCoordinator("sourceA", store, 
closeableRegistry);
           reportWatermarkEvent(previous, 0, 100);
   
           // the previous close() outlived the closing timeout, so the next 
coordinator of
           // sourceA has already reported when the previous one removes its 
entry
           SourceCoordinator<?, ?> current =
                   getAndStartNewSourceCoordinator("sourceA", store, 
closeableRegistry);
           reportWatermarkEvent(current, 0, 200);
           assertLatestWatermarkAlignmentEvent(0, 1200);
   
           previous.close();
           current.announceCombinedWatermark();
   
           assertLatestWatermarkAlignmentEvent(0, 1200);
       }
   }
   ```
   
   The odds of hitting this are low, but the fix is small, so I think it's 
worth to do. WDYT?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to