[STORE] Cut over MetaDataStateFormat to NIO Path API

This class already uses Path most of the time since it
uses ATOMIC_MOVE. This commit makes it a bit more consistent.
This commit is contained in:
Simon Willnauer 2014-10-28 15:49:29 +01:00
parent faff0f86f0
commit f6b37a31c7
1 changed files with 18 additions and 15 deletions

View File

@ -32,9 +32,13 @@ import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.lucene.store.InputStreamIndexInput; import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
import org.elasticsearch.common.xcontent.*; 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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -89,13 +93,12 @@ public abstract class MetaDataStateFormat<T> {
Preconditions.checkArgument(locations != null, "Locations must not be null"); Preconditions.checkArgument(locations != null, "Locations must not be null");
Preconditions.checkArgument(locations.length > 0, "One or more locations required"); Preconditions.checkArgument(locations.length > 0, "One or more locations required");
String fileName = prefix + version + STATE_FILE_EXTENSION; String fileName = prefix + version + STATE_FILE_EXTENSION;
File stateLocation = new File(locations[0], STATE_DIR_NAME); Path stateLocation = Paths.get(locations[0].getPath(), STATE_DIR_NAME);
FileSystemUtils.mkdirs(stateLocation); Files.createDirectories(stateLocation);
final File tmpStateFile = new File(stateLocation, fileName + ".tmp"); final Path tmpStatePath = stateLocation.resolve(fileName + ".tmp");
final Path tmpStatePath = tmpStateFile.toPath(); final Path finalStatePath = stateLocation.resolve(fileName);
final Path finalStatePath = new File(stateLocation, fileName).toPath();
try { 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); CodecUtil.writeHeader(out, STATE_FILE_CODEC, STATE_FILE_VERSION);
out.writeInt(format.index()); out.writeInt(format.index());
out.writeLong(version); out.writeLong(version);
@ -114,24 +117,24 @@ public abstract class MetaDataStateFormat<T> {
} }
CodecUtil.writeFooter(out); 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); Files.move(tmpStatePath, finalStatePath, StandardCopyOption.ATOMIC_MOVE);
IOUtils.fsync(stateLocation, true); IOUtils.fsync(stateLocation.toFile(), true);
for (int i = 1; i < locations.length; i++) { for (int i = 1; i < locations.length; i++) {
stateLocation = new File(locations[i], STATE_DIR_NAME); stateLocation = Paths.get(locations[i].getPath(), STATE_DIR_NAME);
FileSystemUtils.mkdirs(stateLocation); Files.createDirectories(stateLocation);
Path tmpPath = new File(stateLocation, fileName + ".tmp").toPath(); Path tmpPath = stateLocation.resolve(fileName + ".tmp");
Path finalPath = new File(stateLocation, fileName).toPath(); Path finalPath = stateLocation.resolve(fileName);
try { try {
Files.copy(finalStatePath, tmpPath); 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 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 { } finally {
Files.deleteIfExists(tmpPath); Files.deleteIfExists(tmpPath);
} }
} }
} finally { } finally {
Files.deleteIfExists(tmpStateFile.toPath()); Files.deleteIfExists(tmpStatePath);
} }
if (deleteOldFiles) { if (deleteOldFiles) {
cleanupOldFiles(prefix, fileName, locations); cleanupOldFiles(prefix, fileName, locations);