This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new c3debb7e4 RATIS-2581. Route file operations through NIO.2 (#1499)
c3debb7e4 is described below
commit c3debb7e4c1ac9ab316f2b0a624ad52397b04dca
Author: Haonan <[email protected]>
AuthorDate: Fri Jul 3 23:49:31 2026 +0800
RATIS-2581. Route file operations through NIO.2 (#1499)
---
.../main/java/org/apache/ratis/util/AtomicFileOutputStream.java | 6 ++++--
.../ratis/server/raftlog/segmented/BufferedWriteChannel.java | 7 ++++---
.../org/apache/ratis/server/raftlog/segmented/LogSegmentPath.java | 5 +++--
3 files changed, 11 insertions(+), 7 deletions(-)
diff --git
a/ratis-common/src/main/java/org/apache/ratis/util/AtomicFileOutputStream.java
b/ratis-common/src/main/java/org/apache/ratis/util/AtomicFileOutputStream.java
index ec0eda94f..100c111c0 100644
---
a/ratis-common/src/main/java/org/apache/ratis/util/AtomicFileOutputStream.java
+++
b/ratis-common/src/main/java/org/apache/ratis/util/AtomicFileOutputStream.java
@@ -109,8 +109,10 @@ public final class AtomicFileOutputStream extends
FilterOutputStream {
} catch (IOException ioe) {
LOG.warn("Unable to abort file " + tmpFile, ioe);
} finally {
- if (!tmpFile.delete()) {
- LOG.warn("Unable to delete tmp file during abort " + tmpFile);
+ try {
+ FileUtils.deleteIfExists(tmpFile);
+ } catch (IOException ioe) {
+ LOG.warn("Unable to delete tmp file during abort " + tmpFile, ioe);
}
}
}
diff --git
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/BufferedWriteChannel.java
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/BufferedWriteChannel.java
index 634fca4ce..efb89e5e2 100644
---
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/BufferedWriteChannel.java
+++
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/BufferedWriteChannel.java
@@ -17,6 +17,7 @@
*/
package org.apache.ratis.server.raftlog.segmented;
+import org.apache.ratis.util.FileUtils;
import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.function.CheckedBiFunction;
import org.apache.ratis.util.function.CheckedConsumer;
@@ -26,9 +27,9 @@ import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
@@ -44,8 +45,8 @@ class BufferedWriteChannel implements Closeable {
@SuppressWarnings("java:S2095") // return Closable
static BufferedWriteChannel open(File file, boolean append, ByteBuffer
buffer) throws IOException {
- final RandomAccessFile raf = new RandomAccessFile(file, "rw");
- final FileChannel fc = raf.getChannel();
+ final FileChannel fc = FileUtils.newFileChannel(file,
+ StandardOpenOption.CREATE, StandardOpenOption.READ,
StandardOpenOption.WRITE);
final long size = file.length(); // 0L if the file does not exist.
if (append) {
fc.position(size);
diff --git
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegmentPath.java
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegmentPath.java
index 45dc9a335..f2022e118 100644
---
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegmentPath.java
+++
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegmentPath.java
@@ -18,6 +18,7 @@
package org.apache.ratis.server.raftlog.segmented;
import org.apache.ratis.server.storage.RaftStorage;
+import org.apache.ratis.util.FileUtils;
import org.apache.ratis.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -117,7 +118,7 @@ public final class LogSegmentPath {
LOG.info("Found zero size open segment file " + path);
try {
- Files.delete(path);
+ FileUtils.delete(path);
LOG.info("Deleted zero size open segment file " + path);
} catch (IOException e) {
LOG.warn("Failed to delete zero size open segment file " + path + ": "
+ e);
@@ -131,4 +132,4 @@ public final class LogSegmentPath {
final Long endIndex =
Optional.ofNullable(endIndexString).map(Long::parseLong).orElse(null);
return new LogSegmentPath(path, startIndex, endIndex);
}
-}
\ No newline at end of file
+}