From 6125ae1fdfb1ef869ca391e1d3a0eca6bd0fc523 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sun, 22 Feb 2015 17:30:24 -0500 Subject: [PATCH] Remove redundant fs metadata ops. Files.exists(f) && Files.isDirectory(f) -> Files.exists(f) if (Files.exists(f)) Files.delete(f) -> Files.deleteIfExists(f) if (!Files.exists(f)) Files.createDirectories(f) -> Files.createDirectories(f) In a few places where successive i/o ops are done against the same file, convert to Files.readAttributes(). Closes #9807. --- .../metadata/MetaDataCreateIndexService.java | 2 +- .../common/blobstore/fs/FsBlobContainer.java | 4 +--- .../elasticsearch/common/io/FileSystemUtils.java | 4 ++-- .../org/elasticsearch/env/NodeEnvironment.java | 12 +++++------- .../gateway/MetaDataStateFormat.java | 2 +- .../java/org/elasticsearch/http/HttpServer.java | 8 ++++++-- .../org/elasticsearch/plugins/PluginManager.java | 4 ++-- .../org/elasticsearch/plugins/PluginsService.java | 4 ++-- .../org/elasticsearch/watcher/FileWatcher.java | 15 +++++++++------ 9 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index 1f1a5197c97..38dfa56ea95 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -527,7 +527,7 @@ public class MetaDataCreateIndexService extends AbstractComponent { // see if we have templates defined under config final Path templatesDir = environment.configFile().resolve("templates"); - if (Files.exists(templatesDir) && Files.isDirectory(templatesDir)) { + if (Files.isDirectory(templatesDir)) { try (DirectoryStream stream = Files.newDirectoryStream(templatesDir)) { for (Path templatesFile : stream) { if (Files.isRegularFile(templatesFile)) { diff --git a/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java b/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java index 852c3399845..dcfbbb8fc0a 100644 --- a/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java +++ b/src/main/java/org/elasticsearch/common/blobstore/fs/FsBlobContainer.java @@ -66,9 +66,7 @@ public class FsBlobContainer extends AbstractBlobContainer { @Override public void deleteBlob(String blobName) throws IOException { Path blobPath = path.resolve(blobName); - if (Files.exists(blobPath)) { - Files.delete(blobPath); - } + Files.deleteIfExists(blobPath); } @Override diff --git a/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java b/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java index d06b3a1e54a..a170ba525bd 100644 --- a/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java +++ b/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java @@ -309,8 +309,8 @@ public final class FileSystemUtils { Path newFile = target.resolve(source.relativize(file)); try { Files.copy(file, newFile); - if ((delete) && (Files.exists(newFile))) { - Files.delete(file); + if (delete) { + Files.deleteIfExists(file); } } catch (IOException x) { // We ignore this diff --git a/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/src/main/java/org/elasticsearch/env/NodeEnvironment.java index 38f723d7999..45f95adc130 100644 --- a/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -97,9 +97,7 @@ public class NodeEnvironment extends AbstractComponent implements Closeable{ for (int possibleLockId = 0; possibleLockId < maxLocalStorageNodes; possibleLockId++) { for (int dirIndex = 0; dirIndex < environment.dataWithClusterFiles().length; dirIndex++) { Path dir = environment.dataWithClusterFiles()[dirIndex].resolve(Paths.get(NODES_FOLDER, Integer.toString(possibleLockId))); - if (Files.exists(dir) == false) { - Files.createDirectories(dir); - } + Files.createDirectories(dir); try (Directory luceneDir = FSDirectory.open(dir, NativeFSLockFactory.INSTANCE)) { logger.trace("obtaining node lock on {} ...", dir.toAbsolutePath()); @@ -473,7 +471,7 @@ public class NodeEnvironment extends AbstractComponent implements Closeable{ Set indices = Sets.newHashSet(); for (Path indicesLocation : nodeIndicesPaths) { - if (Files.exists(indicesLocation) && Files.isDirectory(indicesLocation)) { + if (Files.isDirectory(indicesLocation)) { try (DirectoryStream stream = Files.newDirectoryStream(indicesLocation)) { for (Path index : stream) { if (Files.isDirectory(index)) { @@ -505,7 +503,7 @@ public class NodeEnvironment extends AbstractComponent implements Closeable{ private static Set findAllShardIds(@Nullable final String index, Path... locations) throws IOException { final Set shardIds = Sets.newHashSet(); for (final Path location : locations) { - if (Files.exists(location) && Files.isDirectory(location)) { + if (Files.isDirectory(location)) { try (DirectoryStream indexStream = Files.newDirectoryStream(location)) { for (Path indexPath : indexStream) { if (index == null || index.equals(indexPath.getFileName().toString())) { @@ -520,11 +518,11 @@ public class NodeEnvironment extends AbstractComponent implements Closeable{ private static Set findAllShardsForIndex(Path indexPath) throws IOException { Set shardIds = new HashSet<>(); - if (Files.exists(indexPath) && Files.isDirectory(indexPath)) { + if (Files.isDirectory(indexPath)) { try (DirectoryStream stream = Files.newDirectoryStream(indexPath)) { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { - if (Files.exists(shardPath) && Files.isDirectory(shardPath)) { + if (Files.isDirectory(shardPath)) { Integer shardId = Ints.tryParse(shardPath.getFileName().toString()); if (shardId != null) { ShardId id = new ShardId(currentIndex, shardId); diff --git a/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java b/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java index 03cae56c9d2..a66fe047923 100644 --- a/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java +++ b/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java @@ -232,7 +232,7 @@ public abstract class MetaDataStateFormat { if (dataLocations != null) { // select all eligable files first for (Path dataLocation : dataLocations) { final Path stateDir = dataLocation.resolve(STATE_DIR_NAME); - if (!Files.exists(stateDir) || !Files.isDirectory(stateDir)) { + if (!Files.isDirectory(stateDir)) { continue; } // now, iterate over the current versions, and find latest one diff --git a/src/main/java/org/elasticsearch/http/HttpServer.java b/src/main/java/org/elasticsearch/http/HttpServer.java index cf9d3f567a0..77b5abb5c68 100644 --- a/src/main/java/org/elasticsearch/http/HttpServer.java +++ b/src/main/java/org/elasticsearch/http/HttpServer.java @@ -20,6 +20,7 @@ package org.elasticsearch.http; import com.google.common.collect.ImmutableMap; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.inject.Inject; @@ -33,6 +34,7 @@ import org.elasticsearch.rest.*; import java.io.File; import java.io.IOException; import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -180,9 +182,11 @@ public class HttpServer extends AbstractLifecycleComponent { channel.sendResponse(new BytesRestResponse(NOT_FOUND)); return; } - if (!Files.isRegularFile(file)) { + + BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class); + if (!attributes.isRegularFile()) { // If it's not a dir, we send a 403 - if (!Files.isDirectory(file)) { + if (!attributes.isDirectory()) { channel.sendResponse(new BytesRestResponse(FORBIDDEN)); return; } diff --git a/src/main/java/org/elasticsearch/plugins/PluginManager.java b/src/main/java/org/elasticsearch/plugins/PluginManager.java index 99b89235e6d..7007ae6592f 100644 --- a/src/main/java/org/elasticsearch/plugins/PluginManager.java +++ b/src/main/java/org/elasticsearch/plugins/PluginManager.java @@ -237,7 +237,7 @@ public class PluginManager { // It could potentially be a non explicit _site plugin boolean potentialSitePlugin = true; Path binFile = extractLocation.resolve("bin"); - if (Files.exists(binFile) && Files.isDirectory(binFile)) { + if (Files.isDirectory(binFile)) { Path toLocation = pluginHandle.binDir(environment); debug("Found bin, moving to " + toLocation.toAbsolutePath()); if (Files.exists(toLocation)) { @@ -270,7 +270,7 @@ public class PluginManager { } Path configFile = extractLocation.resolve("config"); - if (Files.exists(configFile) && Files.isDirectory(configFile)) { + if (Files.isDirectory(configFile)) { Path configDestLocation = pluginHandle.configDir(environment); debug("Found config, moving to " + configDestLocation.toAbsolutePath()); moveFilesWithoutOverwriting(configFile, configDestLocation, ".new"); diff --git a/src/main/java/org/elasticsearch/plugins/PluginsService.java b/src/main/java/org/elasticsearch/plugins/PluginsService.java index ce44fbb1029..29149494e3d 100644 --- a/src/main/java/org/elasticsearch/plugins/PluginsService.java +++ b/src/main/java/org/elasticsearch/plugins/PluginsService.java @@ -387,7 +387,7 @@ public class PluginsService extends AbstractComponent { List libFiles = Lists.newArrayList(); libFiles.addAll(Arrays.asList(files(plugin))); Path libLocation = plugin.resolve("lib"); - if (Files.exists(libLocation) && Files.isDirectory(libLocation)) { + if (Files.isDirectory(libLocation)) { libFiles.addAll(Arrays.asList(files(libLocation))); } @@ -519,7 +519,7 @@ public class PluginsService extends AbstractComponent { // Let's try to find all _site plugins we did not already found Path pluginsFile = environment.pluginsFile(); - if (!Files.exists(pluginsFile) || !Files.isDirectory(pluginsFile)) { + if (!Files.isDirectory(pluginsFile)) { return false; } diff --git a/src/main/java/org/elasticsearch/watcher/FileWatcher.java b/src/main/java/org/elasticsearch/watcher/FileWatcher.java index 383fedc580c..2b498fed130 100644 --- a/src/main/java/org/elasticsearch/watcher/FileWatcher.java +++ b/src/main/java/org/elasticsearch/watcher/FileWatcher.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.logging.Loggers; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; /** @@ -92,13 +93,14 @@ public class FileWatcher extends AbstractResourceWatcher { exists = Files.exists(file); // TODO we might use the new NIO2 API to get real notification? if (exists) { - isDirectory = Files.isDirectory(file); + BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class); + isDirectory = attributes.isDirectory(); if (isDirectory) { length = 0; lastModified = 0; } else { - length = Files.size(file); - lastModified = Files.getLastModifiedTime(file).toMillis(); + length = attributes.size(); + lastModified = attributes.lastModifiedTime().toMillis(); } } else { isDirectory = false; @@ -154,12 +156,13 @@ public class FileWatcher extends AbstractResourceWatcher { private void init(boolean initial) throws IOException { exists = Files.exists(file); if (exists) { - isDirectory =Files.isDirectory(file); + BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class); + isDirectory = attributes.isDirectory(); if (isDirectory) { onDirectoryCreated(initial); } else { - length = Files.size(file); - lastModified = Files.getLastModifiedTime(file).toMillis(); + length = attributes.size(); + lastModified = attributes.lastModifiedTime().toMillis(); onFileCreated(initial); } }