Skip to content

Commit

Permalink
Improve crash consistency on Linux (#8815)
Browse files Browse the repository at this point in the history
* Improve crash consistency

* `fsync` the correct parent directory

* More flexibility

* Add reference to man page

* fix formatting

---------

Co-authored-by: Tim Jacomb <[email protected]>
  • Loading branch information
basil and timja authored Jan 3, 2024
1 parent d75833e commit 444f2de
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/hudson/util/AtomicFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Functions;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.ref.Cleaner;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class AtomicFileWriter extends Writer {
private static /* final */ boolean DISABLE_FORCED_FLUSH = SystemProperties.getBoolean(
AtomicFileWriter.class.getName() + ".DISABLE_FORCED_FLUSH");

private static /* final */ boolean REQUIRES_DIR_FSYNC = SystemProperties.getBoolean(
AtomicFileWriter.class.getName() + ".REQUIRES_DIR_FSYNC", !Functions.isWindows());

Check warning on line 68 in core/src/main/java/hudson/util/AtomicFileWriter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 68 is only partially covered, one branch is missing

static {
if (DISABLE_FORCED_FLUSH) {
LOGGER.log(Level.WARNING, "DISABLE_FORCED_FLUSH flag used, this could result in dataloss if failures happen in your storage subsystem.");
Expand Down Expand Up @@ -234,6 +239,18 @@ public void commit() throws IOException {
throw replaceFailed;
}
}

/*
* From fsync(2) on Linux:
*
* Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also
* reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.
*/
if (!DISABLE_FORCED_FLUSH && REQUIRES_DIR_FSYNC) {

Check warning on line 249 in core/src/main/java/hudson/util/AtomicFileWriter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 249 is only partially covered, 2 branches are missing
try (FileChannel parentChannel = FileChannel.open(destPath.getParent())) {
parentChannel.force(true);
}
}
}

private static final class CleanupChecker implements Runnable {
Expand Down

0 comments on commit 444f2de

Please sign in to comment.