This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new cc0e35f CAMEL-17068: Fix file cluster view exception handling. (#6243)
cc0e35f is described below
commit cc0e35fae5cde093a3e267e672964ebf7fa30b79
Author: iliya-gr <[email protected]>
AuthorDate: Tue Oct 12 07:18:24 2021 +0300
CAMEL-17068: Fix file cluster view exception handling. (#6243)
Handle and log all exceptions in tryLock.
Close lock file on failure and do not keep reference to channel.
---
.../file/cluster/FileLockClusterView.java | 41 +++++++++++++---------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java
index 700996f..e9069aa 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/cluster/FileLockClusterView.java
@@ -16,8 +16,8 @@
*/
package org.apache.camel.component.file.cluster;
+import java.io.IOException;
import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.Files;
@@ -30,10 +30,8 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import org.apache.camel.RuntimeCamelException;
import org.apache.camel.cluster.CamelClusterMember;
import org.apache.camel.support.cluster.AbstractCamelClusterView;
-import org.apache.camel.util.IOHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,8 +40,7 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
private final ClusterMember localMember;
private final Path path;
- private RandomAccessFile file;
- private FileChannel channel;
+ private RandomAccessFile lockFile;
private FileLock lock;
private ScheduledFuture<?> task;
@@ -76,7 +73,7 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
@Override
protected void doStart() throws Exception {
- if (file != null) {
+ if (lockFile != null) {
closeInternal();
fireLeadershipChangedEvent(Optional.empty());
@@ -86,9 +83,6 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
Files.createDirectories(path.getParent());
}
- file = new RandomAccessFile(path.toFile(), "rw");
- channel = file.getChannel();
-
FileLockClusterService service =
getClusterService().unwrap(FileLockClusterService.class);
ScheduledExecutorService executor = service.getExecutor();
@@ -116,17 +110,24 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
lock.release();
}
- if (file != null) {
- IOHelper.close(channel);
- IOHelper.close(file);
+ closeLockFile();
+ }
- channel = null;
- file = null;
+ private void closeLockFile() {
+ if (lockFile != null) {
+ try {
+ lockFile.close();
+ } catch (Throwable ignore) {
+ // Ignore
+ }
+ lockFile = null;
}
}
private void tryLock() {
if (isStarting() || isStarted()) {
+ Exception reason = null;
+
try {
if (localMember.isLeader()) {
LOGGER.trace("Holding the lock on file {} (lock={})",
path, lock);
@@ -140,9 +141,10 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
}
LOGGER.debug("Try to acquire a lock on {}", path);
+ lockFile = new RandomAccessFile(path.toFile(), "rw");
lock = null;
- lock = channel.tryLock();
+ lock = lockFile.getChannel().tryLock(0, Math.max(1,
lockFile.getChannel().size()), false);
if (lock != null) {
LOGGER.info("Lock on file {} acquired (lock={})",
path, lock);
@@ -152,9 +154,14 @@ public class FileLockClusterView extends
AbstractCamelClusterView {
}
}
} catch (OverlappingFileLockException e) {
- LOGGER.debug("Lock on file {} not acquired ", path);
+ reason = new IOException(e);
} catch (Exception e) {
- throw new RuntimeCamelException(e);
+ reason = e;
+ }
+
+ if (lock == null) {
+ LOGGER.debug("Lock on file {} not acquired ", path, reason);
+ closeLockFile();
}
}
}