Skip to content

Commit

Permalink
[STORE] Cut over MetaDataStateFormat to NIO Path API
Browse files Browse the repository at this point in the history
This class already uses Path most of the time since it
uses ATOMIC_MOVE. This commit makes it a bit more consistent.
  • Loading branch information
s1monw committed Oct 30, 2014
1 parent faff0f8 commit f6b37a3
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
import org.elasticsearch.common.xcontent.*;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -89,13 +93,12 @@ public final void write(final T state, final String prefix, final long version,
Preconditions.checkArgument(locations != null, "Locations must not be null");
Preconditions.checkArgument(locations.length > 0, "One or more locations required");
String fileName = prefix + version + STATE_FILE_EXTENSION;
File stateLocation = new File(locations[0], STATE_DIR_NAME);
FileSystemUtils.mkdirs(stateLocation);
final File tmpStateFile = new File(stateLocation, fileName + ".tmp");
final Path tmpStatePath = tmpStateFile.toPath();
final Path finalStatePath = new File(stateLocation, fileName).toPath();
Path stateLocation = Paths.get(locations[0].getPath(), STATE_DIR_NAME);
Files.createDirectories(stateLocation);
final Path tmpStatePath = stateLocation.resolve(fileName + ".tmp");
final Path finalStatePath = stateLocation.resolve(fileName);
try {
try (OutputStreamIndexOutput out = new OutputStreamIndexOutput(new FileOutputStream(tmpStateFile), BUFFER_SIZE)) {
try (OutputStreamIndexOutput out = new OutputStreamIndexOutput(Files.newOutputStream(tmpStatePath), BUFFER_SIZE)) {
CodecUtil.writeHeader(out, STATE_FILE_CODEC, STATE_FILE_VERSION);
out.writeInt(format.index());
out.writeLong(version);
Expand All @@ -114,24 +117,24 @@ public void close() throws IOException {
}
CodecUtil.writeFooter(out);
}
IOUtils.fsync(tmpStateFile, false); // fsync the state file
IOUtils.fsync(tmpStatePath.toFile(), false); // fsync the state file
Files.move(tmpStatePath, finalStatePath, StandardCopyOption.ATOMIC_MOVE);
IOUtils.fsync(stateLocation, true);
IOUtils.fsync(stateLocation.toFile(), true);
for (int i = 1; i < locations.length; i++) {
stateLocation = new File(locations[i], STATE_DIR_NAME);
FileSystemUtils.mkdirs(stateLocation);
Path tmpPath = new File(stateLocation, fileName + ".tmp").toPath();
Path finalPath = new File(stateLocation, fileName).toPath();
stateLocation = Paths.get(locations[i].getPath(), STATE_DIR_NAME);
Files.createDirectories(stateLocation);
Path tmpPath = stateLocation.resolve(fileName + ".tmp");
Path finalPath = stateLocation.resolve(fileName);
try {
Files.copy(finalStatePath, tmpPath);
Files.move(tmpPath, finalPath, StandardCopyOption.ATOMIC_MOVE); // we are on the same FileSystem / Partition here we can do an atomic move
IOUtils.fsync(stateLocation, true); // we just fsync the dir here..
IOUtils.fsync(stateLocation.toFile(), true); // we just fsync the dir here..
} finally {
Files.deleteIfExists(tmpPath);
}
}
} finally {
Files.deleteIfExists(tmpStateFile.toPath());
Files.deleteIfExists(tmpStatePath);
}
if (deleteOldFiles) {
cleanupOldFiles(prefix, fileName, locations);
Expand Down

0 comments on commit f6b37a3

Please sign in to comment.