diff --git a/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index 64a5b256cb880..b98e9636d23fb 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -42,7 +42,6 @@ import org.elasticsearch.node.InternalSettingsPreparer; import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeValidationException; -import org.elasticsearch.snapshots.SnapshotsService; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -172,18 +171,6 @@ private void setup(boolean addShutdownHook, Environment environment) throws Boot BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(settings), BootstrapSettings.CTRLHANDLER_SETTING.get(settings)); - final long cacheSize = SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.get(settings).getBytes(); - final long regionSize = SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.get(settings).getBytes(); - final int numRegions = Math.toIntExact(cacheSize / regionSize); - final long fileSize = numRegions * regionSize; - if (fileSize > 0) { - try { - Natives.tryCreateCacheFile(environment, fileSize); - } catch (Exception e) { - throw new BootstrapException(e); - } - } - // initialize probes before the security manager is installed initializeProbes(); diff --git a/server/src/main/java/org/elasticsearch/bootstrap/JNAFalloc.java b/server/src/main/java/org/elasticsearch/bootstrap/JNAFalloc.java deleted file mode 100644 index 32a1fa238b554..0000000000000 --- a/server/src/main/java/org/elasticsearch/bootstrap/JNAFalloc.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -package org.elasticsearch.bootstrap; - -import com.sun.jna.Native; -import com.sun.jna.Platform; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.lucene.util.Constants; -import org.elasticsearch.common.Nullable; - -/** - * System specific wrappers of the fallocate system call via JNA for Linux. - * TODO: look into adding native implementations for falloc equivalents for other platforms (Windows) to improve performance. - */ -abstract class JNAFalloc { - - private static final Logger logger = LogManager.getLogger(JNAFalloc.class); - - public abstract int fallocate(int fd, long offset, long length); - - @Nullable - public static JNAFalloc falloc() { - try { - if (Constants.LINUX) { - return Linux.INSTANCE; - } - } catch (Throwable t) { - logger.warn("unable to link C library. native (falloc) will be disabled.", t); - } - return null; - } - - private static class Linux extends JNAFalloc { - - static final Linux INSTANCE = new Linux(); - - static { - Native.register(Platform.C_LIBRARY_NAME); - } - - @Override - public int fallocate(int fd, long offset, long length) { - final int res = fallocate(fd, 0, offset, length); - return res == 0 ? 0 : Native.getLastError(); - } - - private static native int fallocate(int fd, int mode, long offset, long length); - } - -} diff --git a/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java b/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java index b62f35a0f07ae..de9fee12f3113 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java @@ -11,20 +11,11 @@ import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.WString; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.util.Constants; -import org.elasticsearch.common.SuppressForbidden; -import org.elasticsearch.env.Environment; import org.elasticsearch.monitor.jvm.JvmInfo; -import org.elasticsearch.snapshots.SnapshotUtils; -import java.io.FileOutputStream; -import java.io.IOException; -import java.lang.reflect.Field; -import java.nio.file.Files; import java.nio.file.Path; import static org.elasticsearch.bootstrap.JNAKernel32Library.SizeT; @@ -269,39 +260,4 @@ static void tryInstallSystemCallFilter(Path tmpFile) { } } - @SuppressForbidden(reason = "need access to fd on FileOutputStream") - static void fallocateSnapshotCacheFile(Environment environment, long fileSize) throws IOException { - final JNAFalloc falloc = JNAFalloc.falloc(); - if (falloc == null) { - logger.debug("not trying to create a shared cache file using fallocate because native fallocate library could not be loaded."); - return; - } - - Path cacheFile = SnapshotUtils.findCacheSnapshotCacheFilePath(environment, fileSize); - if (cacheFile == null) { - throw new IOException("could not find a directory with adequate free space for cache file"); - } - boolean success = false; - try (FileOutputStream fileChannel = new FileOutputStream(cacheFile.toFile())) { - long currentSize = fileChannel.getChannel().size(); - if (currentSize < fileSize) { - final Field field = fileChannel.getFD().getClass().getDeclaredField("fd"); - field.setAccessible(true); - final int result = falloc.fallocate((int) field.get(fileChannel.getFD()), currentSize, fileSize - currentSize); - if (result == 0) { - success = true; - logger.info("allocated cache file [{}] using fallocate", cacheFile); - } else { - logger.warn("failed to initialize cache file [{}] using fallocate errno [{}]", cacheFile, result); - } - } - } catch (Exception e) { - logger.warn(new ParameterizedMessage("failed to initialize cache file [{}] using fallocate", cacheFile), e); - } finally { - if (success == false) { - // if anything goes wrong, delete the potentially created file to not waste disk space - Files.deleteIfExists(cacheFile); - } - } - } } diff --git a/server/src/main/java/org/elasticsearch/bootstrap/Natives.java b/server/src/main/java/org/elasticsearch/bootstrap/Natives.java index e91116a5508ec..f5e94b74234c4 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/Natives.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/Natives.java @@ -10,9 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.env.Environment; -import java.io.IOException; import java.nio.file.Path; /** @@ -135,19 +133,4 @@ static boolean isSystemCallFilterInstalled() { return JNANatives.LOCAL_SYSTEM_CALL_FILTER; } - /** - * On Linux, this method tries to create the searchable snapshot frozen cache file using fallocate if JNA is available. This enables - * a much faster creation of the file than the fallback mechanism in the searchable snapshots plugin that will pre-allocate the cache - * file by writing zeros to the file. - * - * @throws IOException on failure to determine free disk space for a data path - */ - public static void tryCreateCacheFile(Environment environment, long fileSize) throws IOException { - if (JNA_AVAILABLE == false) { - logger.warn("cannot use fallocate to create cache file because JNA is not available"); - return; - } - JNANatives.fallocateSnapshotCacheFile(environment, fileSize); - } - } diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotUtils.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotUtils.java index b6948eef80ffa..74386cdee37a0 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotUtils.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotUtils.java @@ -9,15 +9,10 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.regex.Regex; -import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexNotFoundException; import java.util.ArrayList; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -115,28 +110,4 @@ public static List filterIndices(List availableIndices, String[] return Collections.unmodifiableList(new ArrayList<>(result)); } - /** - * Tries to find a suitable path to a searchable snapshots shared cache file in the data paths founds in the environment. - * - * @return path for the cache file or {@code null} if none could be found - */ - @Nullable - public static Path findCacheSnapshotCacheFilePath(Environment environment, long fileSize) throws IOException { - Path cacheFile = null; - for (Path path : environment.dataFiles()) { - Files.createDirectories(path); - // TODO: be resilient to this check failing and try next path? - long usableSpace = Environment.getUsableSpace(path); - Path p = path.resolve(SnapshotsService.CACHE_FILE_NAME); - if (Files.exists(p)) { - usableSpace += Files.size(p); - } - // TODO: leave some margin for error here - if (usableSpace > fileSize) { - cacheFile = p; - break; - } - } - return cacheFile; - } } diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java index 8ed546ceaeaf2..adebb415584a3 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java @@ -37,9 +37,6 @@ import org.elasticsearch.cluster.RestoreInProgress; import org.elasticsearch.cluster.SnapshotDeletionsInProgress; import org.elasticsearch.cluster.SnapshotsInProgress; -import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.repositories.RepositoryShardId; import org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus; import org.elasticsearch.cluster.SnapshotsInProgress.ShardState; import org.elasticsearch.cluster.SnapshotsInProgress.State; @@ -68,6 +65,7 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.AbstractRunnable; +import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.SystemIndices; @@ -77,6 +75,7 @@ import org.elasticsearch.repositories.RepositoryData; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.RepositoryMissingException; +import org.elasticsearch.repositories.RepositoryShardId; import org.elasticsearch.repositories.ShardGenerations; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -143,26 +142,6 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus public static final String UPDATE_SNAPSHOT_STATUS_ACTION_NAME = "internal:cluster/snapshot/update_snapshot_status"; - public static final String SHARED_CACHE_SETTINGS_PREFIX = "xpack.searchable.snapshot.shared_cache."; - - public static final Setting SHARED_CACHE_RANGE_SIZE_SETTING = Setting.byteSizeSetting( - SHARED_CACHE_SETTINGS_PREFIX + "range_size", - ByteSizeValue.ofMb(16), // default - Setting.Property.NodeScope - ); - public static final Setting SNAPSHOT_CACHE_REGION_SIZE_SETTING = Setting.byteSizeSetting( - SHARED_CACHE_SETTINGS_PREFIX + "region_size", - SHARED_CACHE_RANGE_SIZE_SETTING, - Setting.Property.NodeScope - ); - public static final Setting SNAPSHOT_CACHE_SIZE_SETTING = Setting.byteSizeSetting( - SHARED_CACHE_SETTINGS_PREFIX + "size", - ByteSizeValue.ZERO, - Setting.Property.NodeScope - ); - - public static final String CACHE_FILE_NAME = "shared_snapshot_cache"; - public static final String NO_FEATURE_STATES_VALUE = "none"; private final ClusterService clusterService; diff --git a/x-pack/plugin/searchable-snapshots/build.gradle b/x-pack/plugin/searchable-snapshots/build.gradle index 490744738b4d9..da23d1fb514db 100644 --- a/x-pack/plugin/searchable-snapshots/build.gradle +++ b/x-pack/plugin/searchable-snapshots/build.gradle @@ -12,7 +12,8 @@ archivesBaseName = 'x-pack-searchable-snapshots' dependencies { compileOnly project(path: xpackModule('core')) + implementation project(path: 'preallocate') internalClusterTestImplementation(testArtifact(project(xpackModule('core')))) } -addQaCheckDependencies() \ No newline at end of file +addQaCheckDependencies() diff --git a/x-pack/plugin/searchable-snapshots/preallocate/build.gradle b/x-pack/plugin/searchable-snapshots/preallocate/build.gradle new file mode 100644 index 0000000000000..2794fc7e6ecd4 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/build.gradle @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +apply plugin: 'elasticsearch.build' + +archivesBaseName = 'preallocate' + +dependencies { + compileOnly project(":server") +} + +tasks.named("testingConventions").configure { enabled = false } diff --git a/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/LinuxPreallocator.java b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/LinuxPreallocator.java new file mode 100644 index 0000000000000..02eb5d0add036 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/LinuxPreallocator.java @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.searchablesnapshots.preallocate; + +import com.sun.jna.Native; +import com.sun.jna.Platform; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +final class LinuxPreallocator implements Preallocator { + + @Override + public boolean available() { + return Natives.NATIVES_AVAILABLE; + } + + @Override + public int preallocate(final int fd, final long currentSize, final long fileSize) { + final int rc = Natives.fallocate(fd, 0, currentSize, fileSize - currentSize); + return rc == 0 ? 0 : Native.getLastError(); + } + + @Override + public String error(int errno) { + return Natives.strerror(errno); + } + + private static class Natives { + + public static final boolean NATIVES_AVAILABLE; + + static { + NATIVES_AVAILABLE = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + Native.register(Natives.class, Platform.C_LIBRARY_NAME); + } catch (final UnsatisfiedLinkError e) { + return false; + } + return true; + }); + } + + static native int fallocate(int fd, int mode, long offset, long length); + + static native String strerror(int errno); + + } + +} diff --git a/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/MacOsPreallocator.java b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/MacOsPreallocator.java new file mode 100644 index 0000000000000..61e7b3eafebf4 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/MacOsPreallocator.java @@ -0,0 +1,107 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.searchablesnapshots.preallocate; + +import com.sun.jna.Native; +import com.sun.jna.NativeLong; +import com.sun.jna.Platform; +import com.sun.jna.Structure; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Arrays; +import java.util.List; + +final class MacOsPreallocator implements Preallocator { + + @Override + public boolean available() { + return Natives.NATIVES_AVAILABLE; + } + + @Override + public int preallocate(final int fd, final long currentSize /* unused */ , final long fileSize) { + // the Structure.ByReference constructor requires access to declared members + final Natives.Fcntl.FStore fst = AccessController.doPrivileged((PrivilegedAction) Natives.Fcntl.FStore::new); + fst.fst_flags = Natives.Fcntl.F_ALLOCATECONTIG; + fst.fst_posmode = Natives.Fcntl.F_PEOFPOSMODE; + fst.fst_offset = new NativeLong(0); + fst.fst_length = new NativeLong(fileSize); + // first, try allocating contiguously + if (Natives.fcntl(fd, Natives.Fcntl.F_PREALLOCATE, fst) != 0) { + // that failed, so let us try allocating non-contiguously + fst.fst_flags = Natives.Fcntl.F_ALLOCATEALL; + if (Natives.fcntl(fd, Natives.Fcntl.F_PREALLOCATE, fst) != 0) { + // i'm afraid captain dale had to bail + return Native.getLastError(); + } + } + if (Natives.ftruncate(fd, new NativeLong(fileSize)) != 0) { + return Native.getLastError(); + } + return 0; + } + + @Override + public String error(final int errno) { + return Natives.strerror(errno); + } + + private static class Natives { + + static boolean NATIVES_AVAILABLE; + + static { + NATIVES_AVAILABLE = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + Native.register(Natives.class, Platform.C_LIBRARY_NAME); + } catch (final UnsatisfiedLinkError e) { + return false; + } + return true; + }); + } + + static class Fcntl { + private static final int F_PREALLOCATE = 42; + + // allocate flags; these might be unused, but are here for reference + @SuppressWarnings("unused") + private static final int F_ALLOCATECONTIG = 0x00000002; // allocate contiguous space + private static final int F_ALLOCATEALL = 0x00000004; // allocate all the requested space or no space at all + + // position modes; these might be unused, but are here for reference + private static final int F_PEOFPOSMODE = 3; // allocate from the physical end of the file + @SuppressWarnings("unused") + private static final int F_VOLPOSMODE = 4; // allocate from the volume offset + + public static final class FStore extends Structure implements Structure.ByReference { + public int fst_flags = 0; + public int fst_posmode = 0; + public NativeLong fst_offset = new NativeLong(0); + public NativeLong fst_length = new NativeLong(0); + @SuppressWarnings("unused") + public NativeLong fst_bytesalloc = new NativeLong(0); + + @Override + protected List getFieldOrder() { + return Arrays.asList("fst_flags", "fst_posmode", "fst_offset", "fst_length", "fst_bytesalloc"); + } + + } + } + + static native int fcntl(int fd, int cmd, Fcntl.FStore fst); + + static native int ftruncate(int fd, NativeLong length); + + static native String strerror(int errno); + + } + +} diff --git a/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocate.java b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocate.java new file mode 100644 index 0000000000000..3f9ed1ac7e60e --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocate.java @@ -0,0 +1,90 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.searchablesnapshots.preallocate; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.apache.lucene.util.Constants; +import org.elasticsearch.common.SuppressForbidden; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; + +public class Preallocate { + + private static final Logger logger = LogManager.getLogger(Preallocate.class); + + public static void preallocate(final Path cacheFile, final long fileSize) throws IOException { + if (Constants.LINUX) { + preallocate(cacheFile, fileSize, new LinuxPreallocator()); + } else if (Constants.MAC_OS_X) { + preallocate(cacheFile, fileSize, new MacOsPreallocator()); + } else { + preallocate(cacheFile, fileSize, new UnsupportedPreallocator()); + } + } + + @SuppressForbidden(reason = "need access to fd on FileOutputStream") + private static void preallocate(final Path cacheFile, final long fileSize, final Preallocator prealloactor) throws IOException { + if (prealloactor.available() == false) { + logger.warn("failed to pre-allocate cache file [{}] as native methods are not available", cacheFile); + } + boolean success = false; + try (FileOutputStream fileChannel = new FileOutputStream(cacheFile.toFile())) { + long currentSize = fileChannel.getChannel().size(); + if (currentSize < fileSize) { + final Field field = AccessController.doPrivileged(new FileDescriptorFieldAction(fileChannel)); + final int errno = prealloactor.preallocate((int) field.get(fileChannel.getFD()), currentSize, fileSize - currentSize); + if (errno == 0) { + success = true; + logger.debug("pre-allocated cache file [{}] using native methods", cacheFile); + } else { + logger.warn( + "failed to pre-allocate cache file [{}] using native methods, errno: [{}], error: [{}]", + cacheFile, + errno, + prealloactor.error(errno) + ); + } + } + } catch (final Exception e) { + logger.warn(new ParameterizedMessage("failed to pre-allocate cache file [{}] using native methods", cacheFile), e); + } finally { + if (success == false) { + // if anything goes wrong, delete the potentially created file to not waste disk space + Files.deleteIfExists(cacheFile); + } + } + } + + @SuppressForbidden(reason = "need access to fd on FileOutputStream") + private static class FileDescriptorFieldAction implements PrivilegedExceptionAction { + + private final FileOutputStream fileOutputStream; + + private FileDescriptorFieldAction(FileOutputStream fileOutputStream) { + this.fileOutputStream = fileOutputStream; + } + + @Override + public Field run() throws IOException, NoSuchFieldException { + // accessDeclaredMembers + final Field f = fileOutputStream.getFD().getClass().getDeclaredField("fd"); + // suppressAccessChecks + f.setAccessible(true); + return f; + } + } + +} diff --git a/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocator.java b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocator.java new file mode 100644 index 0000000000000..05b181ac122f1 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/Preallocator.java @@ -0,0 +1,40 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.searchablesnapshots.preallocate; + +/** + * Represents platform native methods for pre-allocating files. + */ +interface Preallocator { + + /** + * Returns if native methods for pre-allocating files are available. + * + * @return true if native methods are available, otherwise false + */ + boolean available(); + + /** + * Pre-allocate a file of given current size to the specified size using the given file descriptor. + * + * @param fd the file descriptor + * @param currentSize the current size of the file + * @param fileSize the size to pre-allocate + * @return 0 upon success + */ + int preallocate(int fd, long currentSize, long fileSize); + + /** + * Provide a string representation of the given error number. + * + * @param errno the error number + * @return the error message + */ + String error(int errno); + +} diff --git a/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/UnsupportedPreallocator.java b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/UnsupportedPreallocator.java new file mode 100644 index 0000000000000..6859824e508f9 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/preallocate/src/main/java/org/elasticsearch/xpack/searchablesnapshots/preallocate/UnsupportedPreallocator.java @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.searchablesnapshots.preallocate; + +final class UnsupportedPreallocator implements Preallocator { + + @Override + public boolean available() { + return false; + } + + @Override + public int preallocate(final int fd, final long currentSize, final long fileSize) { + throw new UnsupportedOperationException(); + } + + @Override + public String error(final int errno) { + throw new UnsupportedOperationException(); + } + +} diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/blobstore/cache/SearchableSnapshotsBlobStoreCacheIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/blobstore/cache/SearchableSnapshotsBlobStoreCacheIntegTests.java index 638454390fb41..a69c7653106dc 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/blobstore/cache/SearchableSnapshotsBlobStoreCacheIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/blobstore/cache/SearchableSnapshotsBlobStoreCacheIntegTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.plugins.ClusterPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.snapshots.SnapshotId; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider; import org.elasticsearch.xpack.core.ClientHelper; @@ -84,10 +83,10 @@ public static void setUpCacheSettings() { builder.put(CacheService.SNAPSHOT_CACHE_RECOVERY_RANGE_SIZE_SETTING.getKey(), blobCacheMaxLength); // Frozen (shared cache) cache should be large enough to not cause direct reads - builder.put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), ByteSizeValue.ofMb(128)); + builder.put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), ByteSizeValue.ofMb(128)); // Align ranges to match the blob cache max length - builder.put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), blobCacheMaxLength); - builder.put(SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), blobCacheMaxLength); + builder.put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), blobCacheMaxLength); + builder.put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), blobCacheMaxLength); builder.put(FrozenCacheService.FROZEN_CACHE_RECOVERY_RANGE_SIZE_SETTING.getKey(), blobCacheMaxLength); cacheSettings = builder.build(); } diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/BaseSearchableSnapshotsIntegTestCase.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/BaseSearchableSnapshotsIntegTestCase.java index 93675b2de2f63..8b30aa6a0041b 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/BaseSearchableSnapshotsIntegTestCase.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/BaseSearchableSnapshotsIntegTestCase.java @@ -24,7 +24,6 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest.Storage; @@ -81,7 +80,7 @@ protected Settings nodeSettings(int nodeOrdinal) { ); } builder.put( - SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), + FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), rarely() ? randomBoolean() ? new ByteSizeValue(randomIntBetween(0, 10), ByteSizeUnit.KB) @@ -89,14 +88,14 @@ protected Settings nodeSettings(int nodeOrdinal) { : new ByteSizeValue(randomIntBetween(1, 10), ByteSizeUnit.MB) ); builder.put( - SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), + FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), rarely() ? new ByteSizeValue(randomIntBetween(4, 1024), ByteSizeUnit.KB) : new ByteSizeValue(randomIntBetween(1, 10), ByteSizeUnit.MB) ); if (randomBoolean()) { builder.put( - SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), + FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), rarely() ? new ByteSizeValue(randomIntBetween(4, 1024), ByteSizeUnit.KB) : new ByteSizeValue(randomIntBetween(1, 10), ByteSizeUnit.MB) diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/PartiallyCachedShardAllocationIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/PartiallyCachedShardAllocationIntegTests.java index c2b92475fe66b..63531b70c89b3 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/PartiallyCachedShardAllocationIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/PartiallyCachedShardAllocationIntegTests.java @@ -48,8 +48,8 @@ import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING; import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; -import static org.elasticsearch.snapshots.SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java index a0197e1d5acb9..f937bf4032a82 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java @@ -30,13 +30,13 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.snapshots.SnapshotId; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.snapshots.mockstore.MockRepository; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; import org.elasticsearch.xpack.searchablesnapshots.cache.CacheService; +import org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService; import java.io.IOException; import java.time.Instant; @@ -70,7 +70,7 @@ protected Settings nodeSettings(int nodeOrdinal) { // Use an unbound cache so we can recover the searchable snapshot completely all the times .put(CacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES)) // Have a shared cache of reasonable size available on each node because tests randomize over frozen and cold allocation - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), ByteSizeValue.ofMb(randomLongBetween(1, 10))) + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), ByteSizeValue.ofMb(randomLongBetween(1, 10))) .build(); } diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/HasFrozenCacheAllocationDecider.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/HasFrozenCacheAllocationDecider.java index fbdf446eabe6c..50dae8203a8e1 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/HasFrozenCacheAllocationDecider.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/HasFrozenCacheAllocationDecider.java @@ -17,8 +17,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheInfoService; -import static org.elasticsearch.snapshots.SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING; import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING; +import static org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING; public class HasFrozenCacheAllocationDecider extends AllocationDecider { diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshots.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshots.java index 8fbf149b8491a..258a685248e9d 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshots.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshots.java @@ -62,7 +62,6 @@ import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.script.ScriptService; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.snapshots.SourceOnlySnapshotRepository; import org.elasticsearch.threadpool.ExecutorBuilder; import org.elasticsearch.threadpool.ScalingExecutorBuilder; @@ -301,9 +300,9 @@ public List> getSettings() { CacheService.SNAPSHOT_CACHE_MAX_FILES_TO_SYNC_AT_ONCE_SETTING, CacheService.SNAPSHOT_CACHE_SYNC_SHUTDOWN_TIMEOUT, SearchableSnapshotEnableAllocationDecider.SEARCHABLE_SNAPSHOTS_ALLOCATE_ON_ROLLING_RESTART, - SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING, - SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING, - SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING, + FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING, + FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING, + FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING, FrozenCacheService.FROZEN_CACHE_RECOVERY_RANGE_SIZE_SETTING, FrozenCacheService.SNAPSHOT_CACHE_MAX_FREQ_SETTING, FrozenCacheService.SNAPSHOT_CACHE_DECAY_INTERVAL_SETTING, diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/cache/FrozenCacheInfoNodeAction.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/cache/FrozenCacheInfoNodeAction.java index 890f45a6cf142..e7437200a18ec 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/cache/FrozenCacheInfoNodeAction.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/cache/FrozenCacheInfoNodeAction.java @@ -21,7 +21,7 @@ import java.io.IOException; -import static org.elasticsearch.snapshots.SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING; +import static org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING; public class FrozenCacheInfoNodeAction extends ActionType { diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheService.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheService.java index f6e2003cc1db3..25a12d4954734 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheService.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheService.java @@ -46,10 +46,6 @@ import java.util.function.LongSupplier; import java.util.function.Predicate; -import static org.elasticsearch.snapshots.SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING; -import static org.elasticsearch.snapshots.SnapshotsService.SHARED_CACHE_SETTINGS_PREFIX; -import static org.elasticsearch.snapshots.SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING; -import static org.elasticsearch.snapshots.SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING; import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsUtils.toIntBytes; public class FrozenCacheService implements Releasable { @@ -57,6 +53,26 @@ public class FrozenCacheService implements Releasable { public static final ByteSizeValue MIN_SNAPSHOT_CACHE_RANGE_SIZE = new ByteSizeValue(4, ByteSizeUnit.KB); public static final ByteSizeValue MAX_SNAPSHOT_CACHE_RANGE_SIZE = new ByteSizeValue(Integer.MAX_VALUE, ByteSizeUnit.BYTES); + private static final String SHARED_CACHE_SETTINGS_PREFIX = "xpack.searchable.snapshot.shared_cache."; + + public static final Setting SHARED_CACHE_RANGE_SIZE_SETTING = Setting.byteSizeSetting( + SHARED_CACHE_SETTINGS_PREFIX + "range_size", + ByteSizeValue.ofMb(16), // default + Setting.Property.NodeScope + ); + + public static final Setting SNAPSHOT_CACHE_REGION_SIZE_SETTING = Setting.byteSizeSetting( + SHARED_CACHE_SETTINGS_PREFIX + "region_size", + SHARED_CACHE_RANGE_SIZE_SETTING, + Setting.Property.NodeScope + ); + + public static final Setting SNAPSHOT_CACHE_SIZE_SETTING = Setting.byteSizeSetting( + SHARED_CACHE_SETTINGS_PREFIX + "size", + ByteSizeValue.ZERO, + Setting.Property.NodeScope + ); + public static final Setting FROZEN_CACHE_RECOVERY_RANGE_SIZE_SETTING = Setting.byteSizeSetting( SHARED_CACHE_SETTINGS_PREFIX + "recovery_range_size", new ByteSizeValue(128, ByteSizeUnit.KB), // default diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/SharedBytes.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/SharedBytes.java index 4dfd9ba195b85..2f234ed3b82ce 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/SharedBytes.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/SharedBytes.java @@ -9,14 +9,14 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.io.Channels; import org.elasticsearch.common.util.concurrent.AbstractRefCounted; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; -import org.elasticsearch.snapshots.SnapshotUtils; -import org.elasticsearch.snapshots.SnapshotsService; +import org.elasticsearch.xpack.searchablesnapshots.preallocate.Preallocate; import java.io.IOException; import java.nio.ByteBuffer; @@ -30,6 +30,8 @@ public class SharedBytes extends AbstractRefCounted { private static final Logger logger = LogManager.getLogger(SharedBytes.class); + private static final String CACHE_FILE_NAME = "shared_snapshot_cache"; + private static final StandardOpenOption[] OPEN_OPTIONS = new StandardOpenOption[] { StandardOpenOption.READ, StandardOpenOption.WRITE, @@ -51,10 +53,11 @@ public class SharedBytes extends AbstractRefCounted { final long fileSize = numRegions * regionSize; Path cacheFile = null; if (fileSize > 0) { - cacheFile = SnapshotUtils.findCacheSnapshotCacheFilePath(environment, fileSize); + cacheFile = findCacheSnapshotCacheFilePath(environment, fileSize); if (cacheFile == null) { throw new IOException("Could not find a directory with adequate free space for cache file"); } + Preallocate.preallocate(cacheFile, fileSize); // TODO: maybe make this faster by allocating a larger direct buffer if this is too slow for very large files // We fill either the full file or the bytes between its current size and the desired size once with zeros to fully allocate // the file up front @@ -79,12 +82,37 @@ public class SharedBytes extends AbstractRefCounted { } else { this.fileChannel = null; for (Path path : environment.dataFiles()) { - Files.deleteIfExists(path.resolve(SnapshotsService.CACHE_FILE_NAME)); + Files.deleteIfExists(path.resolve(CACHE_FILE_NAME)); } } this.path = cacheFile; } + /** + * Tries to find a suitable path to a searchable snapshots shared cache file in the data paths founds in the environment. + * + * @return path for the cache file or {@code null} if none could be found + */ + @Nullable + public static Path findCacheSnapshotCacheFilePath(Environment environment, long fileSize) throws IOException { + Path cacheFile = null; + for (Path path : environment.dataFiles()) { + Files.createDirectories(path); + // TODO: be resilient to this check failing and try next path? + long usableSpace = Environment.getUsableSpace(path); + Path p = path.resolve(CACHE_FILE_NAME); + if (Files.exists(p)) { + usableSpace += Files.size(p); + } + // TODO: leave some margin for error here + if (usableSpace > fileSize) { + cacheFile = p; + break; + } + } + return cacheFile; + } + @Override protected void closeInternal() { try { diff --git a/x-pack/plugin/searchable-snapshots/src/main/plugin-metadata/plugin-security.policy b/x-pack/plugin/searchable-snapshots/src/main/plugin-metadata/plugin-security.policy new file mode 100644 index 0000000000000..7860d049e3a56 --- /dev/null +++ b/x-pack/plugin/searchable-snapshots/src/main/plugin-metadata/plugin-security.policy @@ -0,0 +1,6 @@ +grant codeBase "${codebase.preallocate}" { + // for registering native methods + permission java.lang.RuntimePermission "accessDeclaredMembers"; + // for accessing the file descriptor field in FileChannel + permission java.lang.reflect.ReflectPermission "suppressAccessChecks"; +}; diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/FrozenIndexInputTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/FrozenIndexInputTests.java index c8030a94f22f2..04c80ee6058ef 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/FrozenIndexInputTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/FrozenIndexInputTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.index.store.StoreFileMetadata; import org.elasticsearch.repositories.IndexId; import org.elasticsearch.snapshots.SnapshotId; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.xpack.searchablesnapshots.AbstractSearchableSnapshotsTestCase; import org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots; import org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants; @@ -59,7 +58,7 @@ public void testRandomReads() throws IOException { final ByteSizeValue rangeSize; if (rarely()) { - rangeSize = SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.get(Settings.EMPTY); + rangeSize = FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.get(Settings.EMPTY); } else if (randomBoolean()) { rangeSize = new ByteSizeValue( randomLongBetween(CacheService.MIN_SNAPSHOT_CACHE_RANGE_SIZE.getBytes(), ByteSizeValue.ofKb(8L).getBytes()) @@ -72,7 +71,7 @@ public void testRandomReads() throws IOException { final ByteSizeValue regionSize; if (rarely()) { - regionSize = SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.get(Settings.EMPTY); + regionSize = FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.get(Settings.EMPTY); } else if (randomBoolean()) { regionSize = new ByteSizeValue(randomLongBetween(ByteSizeValue.ofKb(1L).getBytes(), ByteSizeValue.ofKb(8L).getBytes())); } else { @@ -87,9 +86,9 @@ public void testRandomReads() throws IOException { } final Settings settings = Settings.builder() - .put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), regionSize) - .put(SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), rangeSize) - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize) + .put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), regionSize) + .put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), rangeSize) + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize) .put("path.home", createTempDir()) .build(); final Environment environment = TestEnvironment.newEnvironment(settings); diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsTestCase.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsTestCase.java index fbbade0891071..bd06ccba910b6 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsTestCase.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsTestCase.java @@ -43,7 +43,6 @@ import org.elasticsearch.repositories.IndexId; import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotId; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.test.ClusterServiceUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; @@ -153,13 +152,13 @@ protected FrozenCacheService defaultFrozenCacheService() { protected FrozenCacheService randomFrozenCacheService() { final Settings.Builder cacheSettings = Settings.builder(); if (randomBoolean()) { - cacheSettings.put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), randomFrozenCacheSize()); + cacheSettings.put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), randomFrozenCacheSize()); } if (randomBoolean()) { - cacheSettings.put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), randomFrozenCacheSize()); + cacheSettings.put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), randomFrozenCacheSize()); } if (randomBoolean()) { - cacheSettings.put(SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), randomCacheRangeSize()); + cacheSettings.put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), randomCacheRangeSize()); } if (randomBoolean()) { cacheSettings.put(FrozenCacheService.FROZEN_CACHE_RECOVERY_RANGE_SIZE_SETTING.getKey(), randomCacheRangeSize()); @@ -186,8 +185,8 @@ protected FrozenCacheService createFrozenCacheService(final ByteSizeValue cacheS return new FrozenCacheService( newEnvironment( Settings.builder() - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize) - .put(SnapshotsService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), cacheRangeSize) + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize) + .put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), cacheRangeSize) .build() ), threadPool diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheServiceTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheServiceTests.java index bda1258f3bee2..769b924a24238 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheServiceTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/FrozenCacheServiceTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.store.cache.CacheKey; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService.CacheFileRegion; @@ -29,8 +28,8 @@ public class FrozenCacheServiceTests extends ESTestCase { public void testBasicEviction() throws IOException { Settings settings = Settings.builder() .put(NODE_NAME_SETTING.getKey(), "node") - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") - .put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") + .put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") .put("path.home", createTempDir()) .build(); final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random()); @@ -75,8 +74,8 @@ public void testBasicEviction() throws IOException { public void testAutoEviction() throws IOException { Settings settings = Settings.builder() .put(NODE_NAME_SETTING.getKey(), "node") - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "200b") - .put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "200b") + .put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") .put("path.home", createTempDir()) .build(); final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random()); @@ -112,8 +111,8 @@ public void testAutoEviction() throws IOException { public void testForceEviction() throws IOException { Settings settings = Settings.builder() .put(NODE_NAME_SETTING.getKey(), "node") - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") - .put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") + .put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") .put("path.home", createTempDir()) .build(); final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random()); @@ -141,8 +140,8 @@ public void testForceEviction() throws IOException { public void testDecay() throws IOException { Settings settings = Settings.builder() .put(NODE_NAME_SETTING.getKey(), "node") - .put(SnapshotsService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") - .put(SnapshotsService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") + .put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), "500b") + .put(FrozenCacheService.SNAPSHOT_CACHE_REGION_SIZE_SETTING.getKey(), "100b") .put("path.home", createTempDir()) .build(); final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());