gnodet-bot commented on code in PR #30863:
URL: https://github.com/apache/camel/pull/30863#discussion_r4707407089


##########
components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java:
##########
@@ -401,7 +450,12 @@ private void tryLock(long gen) {
                 if (isLeaderStale(latestClusterLeaderInfo, 
previousClusterLeaderInfo)
                         || canReclaimLeadership(latestClusterLeaderInfo)) {
                     if (previousClusterLeaderInfo != null && 
canReclaimLeadership(previousClusterLeaderInfo)) {
-                        // Backoff so the current cluster leader can notice 
leadership is relinquished
+                        // Backoff so the current cluster leader can notice 
leadership is relinquished. Only for this
+                        // check: the cluster data still names this member 
because no leader has written it since the
+                        // last attempt to take the lock failed, so the next 
check tries again. Otherwise a member
+                        // whose attempt failed (for example on a transient 
I/O error) would back off for good, and a
+                        // cluster with no other member would stay without a 
leader.
+                        
clusterLeaderInfoRef.compareAndSet(latestClusterLeaderInfo, null);

Review Comment:
   **[low / metadata]** This is the CAMEL-27566 fix, included inline to make 
this PR and #30862 merge-order independent — whichever merges first introduces 
it, the other becomes a no-op for this line. The approach is sound. Worth 
noting in the merge commit message for traceability.



##########
components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java:
##########
@@ -571,14 +634,117 @@ public Void get() throws Throwable {
     }
 
     RandomAccessFile createRandomAccessFile(Path path) throws 
ExecutionException, TimeoutException {
+        if (path.equals(leaderLockPath)) {
+            LockFileOpen open = new LockFileOpen();
+            try {
+                return 
open.end(clusterTaskExecutor.run(ThrowingHelper.wrapAsSupplier(open)));
+            } finally {
+                // closes what the attempts opened if the executor gave up (no 
effect if the call already ended)
+                open.end(null);
+            }
+        }
         return clusterTaskExecutor.run(ThrowingHelper.wrapAsSupplier(new 
ThrowingSupplier<RandomAccessFile, Throwable>() {
             @Override
             public RandomAccessFile get() throws Throwable {
-                return new RandomAccessFile(path.toFile(), "rw");
+                return openFile(path);
             }
         }));
     }
 
+    RandomAccessFile openFile(Path path) throws IOException {
+        return new RandomAccessFile(path.toFile(), "rw");
+    }
+
+    /**
+     * One open of the lock file by acquireLock. The cluster task executor 
runs it once per attempt, and an attempt that
+     * timed out keeps running. Every file an attempt opens is returned by 
{@link #end} or closed: by end if the attempt
+     * finished before the call ended, otherwise as soon as it is opened (see 
lateLockFileOpens).
+     */
+    private final class LockFileOpen implements 
ThrowingSupplier<RandomAccessFile, Throwable> {
+        // guarded by stateLock
+        private final List<RandomAccessFile> opened = new ArrayList<>();
+        private int running;
+        private boolean ended;
+
+        @Override
+        public RandomAccessFile get() throws Throwable {
+            stateLock.lock();
+            try {
+                if (ended) {
+                    // an attempt that only starts after the call ended
+                    return null;
+                }
+                running++;
+            } finally {
+                stateLock.unlock();
+            }
+            RandomAccessFile file = null;
+            try {
+                file = openFile(leaderLockPath);
+                return file;
+            } finally {
+                attemptEnded(file);
+            }
+        }
+
+        private void attemptEnded(RandomAccessFile file) {
+            boolean late;
+            stateLock.lock();
+            try {
+                running--;
+                late = ended;
+                if (!late && file != null) {
+                    opened.add(file);
+                }
+            } finally {
+                stateLock.unlock();
+            }
+            if (late) {
+                // this view has not tried the lock since the call ended, and 
no other view of the JVM has, as the key
+                // is still registered: closing the file releases no lock
+                closeFile(file);
+                lateLockFileOpenEnded();
+            }
+        }
+
+        /**
+         * Ends the call with the file the executor returned. Returns that 
file, or null if an attempt is still running:
+         * the lock must not be tried then, as closing the file of that 
attempt later would release it. Closes the other
+         * files the attempts opened.
+         */
+        RandomAccessFile end(RandomAccessFile result) {
+            List<RandomAccessFile> files;
+            boolean stillRunning;
+            stateLock.lock();
+            try {
+                if (ended) {
+                    return null;
+                }
+                ended = true;
+                stillRunning = running > 0;
+                lateLockFileOpens += running;
+                files = new ArrayList<>(opened);
+                opened.clear();
+            } finally {
+                stateLock.unlock();
+            }
+            RandomAccessFile file = stillRunning ? null : result;
+            for (RandomAccessFile f : files) {
+                if (f != file) {
+                    closeFile(f);
+                }
+            }
+            if (stillRunning) {
+                // logged once per wait (acquireLock does not open the lock 
file again until the wait ended), at WARN
+                // as the wait lasts until the JVM restarts if the open never 
returns
+                LOGGER.warn(

Review Comment:
   **[low]** This WARN message is ~50 words — unusually verbose for a single 
log line. It's accurate and operationally important (hung mount = no leader 
until JVM restart), but consider splitting into two sentences for readability, 
or moving the detail about the JVM-restart consequence to the upgrade guide 
only. No blocker.



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