Copilot commented on code in PR #30068:
URL: https://github.com/apache/flink/pull/30068#discussion_r4722878226


##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/join/RetryableAsyncLookupFunctionDelegatorTest.java:
##########
@@ -129,6 +141,126 @@ void testLookupWithCustomRetry() throws Exception {
         delegator.close();
     }
 
+    @Test
+    void testRetryBackoffDoesNotBlockLookupCompletionExecutor() throws 
Exception {
+        try (LookupTestContext test = new LookupTestContext()) {
+            long backoff = MINUTES.toMillis(1);
+            RowData key = GenericRowData.of(1);
+            AtomicInteger submissions = new AtomicInteger();
+            CompletableFuture<Collection<RowData>> firstAttempt = 
test.newLookupFuture();
+            RetryableAsyncLookupFunctionDelegator delegator =
+                    test.open(
+                            backoff,
+                            ignored ->
+                                    submissions.getAndIncrement() == 0
+                                            ? firstAttempt
+                                            : 
CompletableFuture.completedFuture(data.get(key)));
+            CompletableFuture<Collection<RowData>> result = 
delegator.asyncLookup(key);
+
+            firstAttempt.complete(Collections.emptyList());
+            test.awaitCompletionExecutor();
+            assertThat(submissions).hasValue(1);
+            assertThat(result).isNotDone();
+
+            test.processingTimeService.advance(backoff);
+            assertThat(result.get(10, SECONDS)).isEqualTo(data.get(key));
+        }
+    }
+
+    @Test
+    void testRetryDoesNotOverlapNextLookupSubmission() throws Exception {
+        try (LookupTestContext test = new LookupTestContext()) {
+            RowData firstKey = GenericRowData.of(1);
+            RowData secondKey = GenericRowData.of(3);
+            CompletableFuture<Collection<RowData>> firstAttempt = 
test.newLookupFuture();
+            CompletableFuture<Collection<RowData>> secondAttempt = new 
CompletableFuture<>();
+            AtomicBoolean retryStarted = new AtomicBoolean();
+            RetryableAsyncLookupFunctionDelegator delegator =
+                    test.open(
+                            1,
+                            key -> {
+                                if (key.equals(secondKey)) {
+                                    // Process the miss while the next call is 
still submitting.
+                                    
firstAttempt.complete(Collections.emptyList());
+                                    test.awaitCompletionExecutor();
+                                    assertThat(retryStarted)
+                                            .as("retry started during the next 
lookup submission")
+                                            .isFalse();
+                                    return secondAttempt;
+                                }
+                                if (!firstAttempt.isDone()) {
+                                    return firstAttempt;
+                                }
+                                retryStarted.set(true);
+                                return 
CompletableFuture.completedFuture(data.get(key));
+                            });
+            CompletableFuture<Collection<RowData>> firstResult = 
delegator.asyncLookup(firstKey);
+            CompletableFuture<Collection<RowData>> secondResult = 
delegator.asyncLookup(secondKey);
+
+            test.processingTimeService.advance(1);

Review Comment:
   This test makes the retry timer due only after the second submission returns 
and bypasses the runner's mailbox wiring. It would still pass if retries ran on 
a timer thread and overlapped connector submissions. Please add a 
mailbox-backed runner/operator test for direct and cached delegates that makes 
the timer due during the second submission and verifies that the retry runs 
afterward on the task thread.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/lookup/RetryableAsyncLookupFunctionDelegator.java:
##########
@@ -78,24 +100,58 @@ private void lookupWithRetry(
                 (result, throwable) -> {
                     if (retryResultPredicate.test(result)
                             && retryStrategy.canRetry(currentAttempts)) {
-                        long backoff = 
retryStrategy.getBackoffTimeMillis(currentAttempts);
-                        try {
-                            Thread.sleep(backoff);
-                        } catch (InterruptedException e) {
-                            // Do not raise an error when interrupted, just 
complete with last
-                            // result intermediately.
-                            resultFuture.complete(result);
-                            return;
-                        }
-                        lookupWithRetry(resultFuture, currentAttempts + 1, 
keyRow);
+                        scheduleRetry(resultFuture, currentAttempts, keyRow);
                     } else {
                         resultFuture.complete(result);
                     }
                 });
     }
 
+    private synchronized void scheduleRetry(
+            CompletableFuture<Collection<RowData>> resultFuture,
+            int currentAttempts,
+            RowData keyRow) {
+        if (retryTimers == null) {
+            return;
+        }
+        long retryTimestamp =
+                LongMath.saturatedAdd(
+                        processingTimeService.getCurrentProcessingTime(),
+                        retryStrategy.getBackoffTimeMillis(currentAttempts));
+        // The operator's processing time service dispatches timer callbacks 
to the task mailbox.
+        ScheduledFuture<?> timer =
+                processingTimeService.registerTimer(
+                        retryTimestamp,
+                        timestamp -> retryLookup(resultFuture, currentAttempts 
+ 1, keyRow));
+        retryTimers.put(resultFuture, timer);
+    }
+
+    private void retryLookup(
+            CompletableFuture<Collection<RowData>> resultFuture,
+            int currentAttempts,
+            RowData keyRow) {
+        synchronized (this) {
+            // A timer callback can already be queued in the mailbox when 
retries are stopped.
+            if (retryTimers == null || retryTimers.remove(resultFuture) == 
null) {
+                return;
+            }
+        }
+        try {
+            lookupWithRetry(resultFuture, currentAttempts, keyRow);
+        } catch (Throwable ignored) {
+            // The previous whenCompleteAsync callback captured synchronous 
retry failures in its
+            // unused completion stage. Leave the result pending for the 
operator timeout.
+        }
+    }
+
     @Override
     public void close() throws Exception {
+        synchronized (this) {
+            if (retryTimers != null) {
+                retryTimers.values().forEach(timer -> timer.cancel(false));
+                retryTimers = null;

Review Comment:
   The new timer shutdown behavior has no regression coverage: both new tests 
finish their lookups before closing, and the existing tests do not assert timer 
cancellation or post-close submissions. Please add deterministic cases for 
closing with a pending timer, draining a timer callback already queued before 
close, and receiving a lookup miss after close. Assert that pending timers are 
cancelled and none of these paths submits another lookup; these exercise both 
the cancellation here and the stopped-state guards.



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