From ffad9695545a4de346adc4b9185538ece42df143 Mon Sep 17 00:00:00 2001 From: carlosdelest Date: Tue, 23 Jan 2024 17:32:07 +0100 Subject: [PATCH] Allow choosing Direct IO using index setting --- .../common/settings/IndexScopedSettings.java | 1 + .../org/elasticsearch/index/IndexModule.java | 6 +++++ .../index/store/FsDirectoryFactory.java | 24 +++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java index c1b8d51c255db..58f4343a45a85 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java @@ -157,6 +157,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings { MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING, BitsetFilterCache.INDEX_LOAD_RANDOM_ACCESS_FILTERS_EAGERLY_SETTING, IndexModule.INDEX_STORE_TYPE_SETTING, + IndexModule.INDEX_STORE_DIRECT_IO_SETTING, IndexModule.INDEX_STORE_PRE_LOAD_SETTING, IndexModule.INDEX_RECOVERY_TYPE_SETTING, IndexModule.INDEX_QUERY_CACHE_ENABLED_SETTING, diff --git a/server/src/main/java/org/elasticsearch/index/IndexModule.java b/server/src/main/java/org/elasticsearch/index/IndexModule.java index e380a3859deea..c840a1f5220a6 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/server/src/main/java/org/elasticsearch/index/IndexModule.java @@ -112,6 +112,12 @@ public final class IndexModule { Property.NodeScope ); + public static final Setting INDEX_STORE_DIRECT_IO_SETTING = Setting.boolSetting( + "index.store.direct_io", + false, + Property.IndexScope + ); + public static final Setting INDEX_RECOVERY_TYPE_SETTING = new Setting<>( "index.recovery.type", "", diff --git a/server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java b/server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java index 05c3554b47602..14c5fbf2ee980 100644 --- a/server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java +++ b/server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java @@ -8,6 +8,7 @@ package org.elasticsearch.index.store; +import org.apache.lucene.misc.store.DirectIODirectory; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FileSwitchDirectory; @@ -61,23 +62,32 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index type = IndexModule.Type.fromSettingsKey(storeType); } Set preLoadExtensions = new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING)); + FSDirectory directory = null; switch (type) { case HYBRIDFS: // Use Lucene defaults - final FSDirectory primaryDirectory = FSDirectory.open(location, lockFactory); - if (primaryDirectory instanceof MMapDirectory mMapDirectory) { - return new HybridDirectory(lockFactory, setPreload(mMapDirectory, lockFactory, preLoadExtensions)); - } else { - return primaryDirectory; + directory = FSDirectory.open(location, lockFactory); + if (directory instanceof MMapDirectory mMapDirectory) { + directory = new HybridDirectory(lockFactory, setPreload(mMapDirectory, lockFactory, preLoadExtensions)); } + break; case MMAPFS: - return setPreload(new MMapDirectory(location, lockFactory), lockFactory, preLoadExtensions); + directory = setPreload(new MMapDirectory(location, lockFactory), lockFactory, preLoadExtensions); + break; case SIMPLEFS: case NIOFS: - return new NIOFSDirectory(location, lockFactory); + directory = new NIOFSDirectory(location, lockFactory); + break; default: throw new AssertionError("unexpected built-in store type [" + type + "]"); } + + final boolean useDirectIO = indexSettings.getValue(IndexModule.INDEX_STORE_DIRECT_IO_SETTING); + if (useDirectIO) { + return new DirectIODirectory(directory); + } + + return directory; } public static MMapDirectory setPreload(MMapDirectory mMapDirectory, LockFactory lockFactory, Set preLoadExtensions)