You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "ankit-khare-2015 (via GitHub)" <gi...@apache.org> on 2026/04/22 04:05:41 UTC

[PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

ankit-khare-2015 opened a new pull request, #3158:
URL: https://github.com/apache/fluss/pull/3158

   ## Summary
   
   Fixes #3145 — `RemoteLogDownloaderTest.testPrefetchNum` was non-deterministically failing.
   
   **Root cause:** In `RemoteLogDownloader.fetchOnce()`, the prefetch semaphore was acquired *before* polling the work queue:
   
   ```java
   // old order
   prefetchSemaphore.acquire();                                   // holds permit
   RemoteLogDownloadRequest request = segmentsToFetch.poll(...); // may return null
   if (request == null) {
       prefetchSemaphore.release();  // held for up to pollTimeout (10 ms)
       return;
   }
   ```
   
   When the queue was empty the download thread held a permit for the full `pollTimeout` (10 ms in tests) before releasing it. The test assertion `availablePermits() == 2` ran during that 10 ms window and saw `1` instead of `2`, causing a spurious failure.
   
   **Fix:** Swap the order — poll the queue first, acquire the semaphore only when there is actual work to do. The semaphore is never borrowed for null-polls, so `availablePermits()` accurately reflects true available capacity at all times.
   
   ```java
   // new order
   RemoteLogDownloadRequest request = segmentsToFetch.poll(...); // check for work first
   if (request == null) {
       return;  // no permit touched
   }
   prefetchSemaphore.acquire();  // only acquired when there is something to download
   ```
   
   **Side effect on `testDownloadLogInParallelAndInPriority`:** With poll-first semantics the single download thread polls one extra item from the queue *before* blocking on the semaphore (4 downloading + 1 pending acquire). The hard `isEqualTo(totalSegments - 4)` queue-size assertion is relaxed to `isBetween(totalSegments - 5, totalSegments - 4)` to cover both cases.
   
   ## Test Plan
   
   - `./mvnw test -Dtest=RemoteLogDownloaderTest -pl fluss-client` — all 3 tests pass
   - `./mvnw spotless:check -pl fluss-client` — no formatting violations
   
   🤖 AI-assisted changes - reviewed by human developer


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3158:
URL: https://github.com/apache/fluss/pull/3158#discussion_r3139074832


##########
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/log/RemoteLogDownloader.java:
##########
@@ -142,16 +142,15 @@ void recycleRemoteLog(RemoteLogSegment segment) {
      * to fetch.
      */
     void fetchOnce() throws Exception {
-        // blocks until there is capacity (the fetched file is consumed)
-        prefetchSemaphore.acquire();
-
         // wait until there is a remote fetch request
         RemoteLogDownloadRequest request = segmentsToFetch.poll(pollTimeout, TimeUnit.MILLISECONDS);
         if (request == null) {
-            prefetchSemaphore.release();
             return;
         }
 
+        // blocks until there is capacity (the fetched file is consumed)
+        prefetchSemaphore.acquire();
+

Review Comment:
   fetchOnce() now polls from the PriorityBlockingQueue before acquiring the prefetchSemaphore. If no permits are available, the thread can block on acquire() while holding a dequeued request, which (1) can violate the intended priority ordering if higher-priority requests are added while blocked, and (2) can drop the request on shutdown/interrupt (InterruptedException from acquire() exits the thread without re-adding). Consider not removing an element from the priority queue until after a permit is obtained (e.g., poll to detect work, reinsert it, acquire permit, then take/poll again), and ensure InterruptedException preserves/requeues the request.
   ```suggestion
   
           // Put the request back before waiting for capacity so it is not lost on interruption
           // and higher-priority requests can still be selected while this thread is blocked.
           segmentsToFetch.add(request);
   
           // blocks until there is capacity (the fetched file is consumed)
           prefetchSemaphore.acquire();
   
           request = segmentsToFetch.poll();
           if (request == null) {
               prefetchSemaphore.release();
               return;
           }
   ```



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on PR #3158:
URL: https://github.com/apache/fluss/pull/3158#issuecomment-4322594964

   Hi guys, let me know if this will be merged or if there are any more open topics.


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on PR #3158:
URL: https://github.com/apache/fluss/pull/3158#issuecomment-4310950185

   Thanks, @zuston. Could you please merge these changes to resolve the issue? 


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on code in PR #3158:
URL: https://github.com/apache/fluss/pull/3158#discussion_r3140480979


##########
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/log/RemoteLogDownloader.java:
##########
@@ -142,16 +142,15 @@ void recycleRemoteLog(RemoteLogSegment segment) {
      * to fetch.
      */
     void fetchOnce() throws Exception {
-        // blocks until there is capacity (the fetched file is consumed)
-        prefetchSemaphore.acquire();
-
         // wait until there is a remote fetch request
         RemoteLogDownloadRequest request = segmentsToFetch.poll(pollTimeout, TimeUnit.MILLISECONDS);
         if (request == null) {
-            prefetchSemaphore.release();
             return;
         }
 
+        // blocks until there is capacity (the fetched file is consumed)
+        prefetchSemaphore.acquire();
+

Review Comment:
   I have added the suggested item. Please check.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "wuchong (via GitHub)" <gi...@apache.org>.
wuchong commented on PR #3158:
URL: https://github.com/apache/fluss/pull/3158#issuecomment-4314901900

   Hi @ankit-khare-2015, I think the root cause analysis provided by Claude Code is correct (pasted below). I suggest restricting the permits to 1 or 2 via an assertion and adding a comment before the assert line to explain the rationale.
   
   ```java
               // the 2 semaphores are released, it should be 2 permits available. But the next
               // fetchOnce() maybe called at this point of time, and acquire the permits,
               // so we just check the permits is between 1 and 2.
               assertThat(remoteLogDownloader.getPrefetchSemaphore().availablePermits())
                       .isBetween(1, 2);
   ```
   
   
   ----------------------------
   By AI Generated:
   
   ### Root Cause: Race Condition Between `recycleRemoteLog` and `fetchOnce`
   
   Here's the sequence of events that causes the flakiness:
   
   **The test at line 127-128:**
   ```java
   futures.get(1).getRecycleCallback().run();
   futures.get(2).getRecycleCallback().run();
   assertThat(remoteLogDownloader.getPrefetchSemaphore().availablePermits()).isEqualTo(2);
   ```
   
   Each `getRecycleCallback().run()` calls `recycleRemoteLog()` (line 133-136 of `RemoteLogDownloader`):
   
   ```java
   void recycleRemoteLog(RemoteLogSegment segment) {
       segmentsToRecycle.add(segment);
       prefetchSemaphore.release();  // increments permits
   }
   ```
   
   Meanwhile, the `DownloadRemoteLogThread` is continuously running `fetchOnce()` in a loop (via `doWork()`). The `fetchOnce()` method starts with:
   
   ```java
   void fetchOnce() throws Exception {
       prefetchSemaphore.acquire();  // decrements permits (blocks if 0)
       // ...
   }
   ```
   
   **The race condition:**
   
   1. `futures.get(1).getRecycleCallback().run()` → calls `prefetchSemaphore.release()` → permits becomes **1**
   2. The `DownloadRemoteLogThread` is spinning in its loop. It calls `prefetchSemaphore.acquire()` → permits drops back to **0**. But there's **nothing left in `segmentsToFetch`** (all 5 segments were already submitted and consumed), so `segmentsToFetch.poll(pollTimeout, ...)` returns `null`, and the thread calls `prefetchSemaphore.release()` → permits back to **1**
   3. `futures.get(2).getRecycleCallback().run()` → calls `prefetchSemaphore.release()` → permits becomes **2**
   4. Test asserts `availablePermits() == 2` ✅ **Happy path**
   
   **But in the failing scenario:**
   
   1. `futures.get(1).getRecycleCallback().run()` → permits becomes **1**
   2. `futures.get(2).getRecycleCallback().run()` → permits becomes **2**
   3. **Before the assertion**, the download thread wakes up and calls `prefetchSemaphore.acquire()` → permits drops to **1**
   4. Test asserts `availablePermits() == 2` ❌ **FAILS with expected 2 but was 1**
   
   The download thread acquired a permit between the two `release()` calls and the assertion check. Even though the thread will eventually release it back (because `segmentsToFetch` is empty and `poll` returns `null`), the assertion fires at the wrong moment.
   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on PR #3158:
URL: https://github.com/apache/fluss/pull/3158#issuecomment-4293776386

   tested the build in my fork https://github.com/ankit-khare-2015/fluss/actions/runs/24759894771/job/72441084138#logs
   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on code in PR #3158:
URL: https://github.com/apache/fluss/pull/3158#discussion_r3140480979


##########
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/log/RemoteLogDownloader.java:
##########
@@ -142,16 +142,15 @@ void recycleRemoteLog(RemoteLogSegment segment) {
      * to fetch.
      */
     void fetchOnce() throws Exception {
-        // blocks until there is capacity (the fetched file is consumed)
-        prefetchSemaphore.acquire();
-
         // wait until there is a remote fetch request
         RemoteLogDownloadRequest request = segmentsToFetch.poll(pollTimeout, TimeUnit.MILLISECONDS);
         if (request == null) {
-            prefetchSemaphore.release();
             return;
         }
 
+        // blocks until there is capacity (the fetched file is consumed)
+        prefetchSemaphore.acquire();
+

Review Comment:
   I have added the suggested item. Please check.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "ankit-khare-2015 (via GitHub)" <gi...@apache.org>.
ankit-khare-2015 commented on code in PR #3158:
URL: https://github.com/apache/fluss/pull/3158#discussion_r3140044230


##########
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/log/RemoteLogDownloader.java:
##########
@@ -142,16 +142,15 @@ void recycleRemoteLog(RemoteLogSegment segment) {
      * to fetch.
      */
     void fetchOnce() throws Exception {
-        // blocks until there is capacity (the fetched file is consumed)
-        prefetchSemaphore.acquire();
-
         // wait until there is a remote fetch request
         RemoteLogDownloadRequest request = segmentsToFetch.poll(pollTimeout, TimeUnit.MILLISECONDS);
         if (request == null) {
-            prefetchSemaphore.release();
             return;
         }
 
+        // blocks until there is capacity (the fetched file is consumed)
+        prefetchSemaphore.acquire();
+

Review Comment:
   Do you want me to revert the changes, assuming it was an intended behavior, and investigate further?



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "wuchong (via GitHub)" <gi...@apache.org>.
wuchong commented on code in PR #3158:
URL: https://github.com/apache/fluss/pull/3158#discussion_r3139097235


##########
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/log/RemoteLogDownloader.java:
##########
@@ -142,16 +142,15 @@ void recycleRemoteLog(RemoteLogSegment segment) {
      * to fetch.
      */
     void fetchOnce() throws Exception {
-        // blocks until there is capacity (the fetched file is consumed)
-        prefetchSemaphore.acquire();
-
         // wait until there is a remote fetch request
         RemoteLogDownloadRequest request = segmentsToFetch.poll(pollTimeout, TimeUnit.MILLISECONDS);
         if (request == null) {
-            prefetchSemaphore.release();
             return;
         }
 
+        // blocks until there is capacity (the fetched file is consumed)
+        prefetchSemaphore.acquire();
+

Review Comment:
   I agree with Copilot. The `acquire()` before `poll()` is intended to guarantees the consuming order of RemoteLogSegments. The consuming order is essential (see https://github.com/apache/fluss/issues/1421).



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [client] Fix race condition in RemoteLogDownloader causing flaky testPrefetchNum [fluss]

Posted by "github-actions[bot] (via GitHub) via issues" <is...@fluss.apache.org>.
github-actions[bot] commented on PR #3158:
URL: https://github.com/apache/fluss/pull/3158#issuecomment-6973419967

   This PR has been marked as stale because it has had no activity for 60 days. It will be closed in 10 days unless it receives a new comment, commit or other update.


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org