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

(fix) use atomic move to avoid file corruption #745

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void close(final LocalSnapshotWriter writer, final boolean keepDataOnError) thro
break;
}
LOG.info("Renaming {} to {}.", tempPath, newPath);
if (!new File(tempPath).renameTo(new File(newPath))) {
if (!Utils.atomicMoveFile(new File(tempPath), new File(newPath), true)) {
LOG.error("Renamed temp snapshot failed, from path {} to path {}.", tempPath, newPath);
ret = RaftError.EIO.getNumber();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.StorageOptionsFactory;
import com.alipay.sofa.jraft.util.SystemPropertyUtil;
import com.alipay.sofa.jraft.util.Utils;
import com.alipay.sofa.jraft.util.concurrent.AdjustableSemaphore;
import com.codahale.metrics.Timer;

Expand Down Expand Up @@ -1475,7 +1476,7 @@ void writeSnapshot(final String snapshotPath) {
checkpoint.createCheckpoint(tempPath);
final File snapshotFile = new File(snapshotPath);
FileUtils.deleteDirectory(snapshotFile);
if (!tempFile.renameTo(snapshotFile)) {
if (!Utils.atomicMoveFile(tempFile, snapshotFile, true)) {
throw new StorageException("Fail to rename [" + tempPath + "] to [" + snapshotPath + "].");
}
} catch (final StorageException e) {
Expand All @@ -1502,7 +1503,7 @@ void readSnapshot(final String snapshotPath) {
final String dbPath = this.opts.getDbPath();
final File dbFile = new File(dbPath);
FileUtils.deleteDirectory(dbFile);
if (!snapshotFile.renameTo(dbFile)) {
if (!Utils.atomicMoveFile(snapshotFile, dbFile, true)) {
throw new StorageException("Fail to rename [" + snapshotPath + "] to [" + dbPath + "].");
}
// reopen the db
Expand Down Expand Up @@ -1534,7 +1535,7 @@ CompletableFuture<Void> writeSstSnapshot(final String snapshotPath, final Region
try {
final File snapshotFile = new File(snapshotPath);
FileUtils.deleteDirectory(snapshotFile);
if (!tempFile.renameTo(snapshotFile)) {
if (!Utils.atomicMoveFile(tempFile, snapshotFile, true)) {
throw new StorageException("Fail to rename [" + tempPath + "] to [" + snapshotPath + "].");
}
snapshotFuture.complete(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alipay.sofa.jraft.util.ExecutorServiceHelper;
import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.ThreadPoolUtil;
import com.alipay.sofa.jraft.util.Utils;
import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
Expand Down Expand Up @@ -108,6 +109,7 @@ public void compress(final String rootDir, final String sourceDir, final String
archiveOutputStream.flush();
fos.getFD().sync();
}
Utils.fsync(zipFile);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.commons.io.output.NullOutputStream;

import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.Utils;

/**
*
Expand All @@ -51,6 +52,7 @@ public static void compress(final String rootDir, final String sourceDir, final
zos.flush();
fos.getFD().sync();
}
Utils.fsync(new File(outputFile));
}

private static void compressDirectoryToZipFile(final String rootDir, final String sourceDir,
Expand Down