Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve crash consistency on Linux #8815

Merged
merged 8 commits into from
Jan 3, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 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());

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,19 @@ 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) {
try (FileChannel parentChannel = FileChannel.open(destPath.getParent())) {
parentChannel.force(true);
}
}
}

private static final class CleanupChecker implements Runnable {
Expand Down