Skip to content

Commit

Permalink
Feat: write snapshot files to temp files first
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Moog committed Jan 3, 2019
1 parent bad59e9 commit 47c4f5e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
Expand Down Expand Up @@ -118,15 +119,38 @@ public Snapshot getLatestSnapshot() {
}

/**
* {@inheritDoc}
* {@inheritDoc}<br />
* <br />
* It first writes two temporary files and then renames them to avoid corruption of the written files in case of IRI
* crashes.<br />
*/
@Override
public void writeSnapshotToDisk(Snapshot snapshot, String basePath) throws SnapshotException {
snapshot.lockRead();

File tmpStateFile = null;
File tmpMetaFile = null;
try {
writeSnapshotStateToDisk(snapshot, basePath + ".snapshot.state");
writeSnapshotMetaDataToDisk(snapshot, basePath + ".snapshot.meta");
tmpStateFile = File.createTempFile("iri.", ".snapshot.state");
tmpMetaFile = File.createTempFile("iri.", ".snapshot.meta");

writeSnapshotStateToDisk(snapshot, tmpStateFile.getAbsolutePath());
writeSnapshotMetaDataToDisk(snapshot, tmpMetaFile.getAbsolutePath());

Files.move(Paths.get(tmpStateFile.getAbsolutePath()), Paths.get(basePath + ".snapshot.state"),
StandardCopyOption.ATOMIC_MOVE);
Files.move(Paths.get(tmpMetaFile.getAbsolutePath()), Paths.get(basePath + ".snapshot.meta"),
StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
// issue a delete for both temp files - just to be safe and not pollute the temp folder
if (tmpStateFile != null) {
tmpStateFile.delete();
}
if (tmpMetaFile != null) {
tmpMetaFile.delete();
}

throw new SnapshotException("failed to write snapshot files", e);
} finally {
snapshot.unlockRead();
}
Expand Down
Empty file added testnet.snapshot.spentaddresses
Empty file.

0 comments on commit 47c4f5e

Please sign in to comment.