Skip to content

Commit

Permalink
use atomic move to avoid file corruption (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang0423 authored Dec 21, 2021
1 parent af17c01 commit 1a9df32
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
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

0 comments on commit 1a9df32

Please sign in to comment.