2222import org .apache .lucene .store .SimpleFSLockFactory ;
2323import org .elasticsearch .common .settings .Setting ;
2424import org .elasticsearch .common .settings .Setting .Property ;
25+ import org .elasticsearch .common .util .FeatureFlag ;
2526import org .elasticsearch .core .IOUtils ;
2627import org .elasticsearch .index .IndexModule ;
2728import org .elasticsearch .index .IndexSettings ;
3637
3738public class FsDirectoryFactory implements IndexStorePlugin .DirectoryFactory {
3839
40+ private static final FeatureFlag MADV_RANDOM_FEATURE_FLAG = new FeatureFlag ("madv_random" );
41+
3942 public static final Setting <LockFactory > INDEX_LOCK_FACTOR_SETTING = new Setting <>("index.store.fs.fs_lock" , "native" , (s ) -> {
4043 return switch (s ) {
4144 case "native" -> NativeFSLockFactory .INSTANCE ;
@@ -67,12 +70,20 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index
6770 // Use Lucene defaults
6871 final FSDirectory primaryDirectory = FSDirectory .open (location , lockFactory );
6972 if (primaryDirectory instanceof MMapDirectory mMapDirectory ) {
70- return new HybridDirectory (lockFactory , setPreload (mMapDirectory , lockFactory , preLoadExtensions ));
73+ Directory dir = new HybridDirectory (lockFactory , setPreload (mMapDirectory , lockFactory , preLoadExtensions ));
74+ if (MADV_RANDOM_FEATURE_FLAG .isEnabled () == false ) {
75+ dir = disableRandomAdvice (dir );
76+ }
77+ return dir ;
7178 } else {
7279 return primaryDirectory ;
7380 }
7481 case MMAPFS :
75- return setPreload (new MMapDirectory (location , lockFactory ), lockFactory , preLoadExtensions );
82+ Directory dir = setPreload (new MMapDirectory (location , lockFactory ), lockFactory , preLoadExtensions );
83+ if (MADV_RANDOM_FEATURE_FLAG .isEnabled () == false ) {
84+ dir = disableRandomAdvice (dir );
85+ }
86+ return dir ;
7687 case SIMPLEFS :
7788 case NIOFS :
7889 return new NIOFSDirectory (location , lockFactory );
@@ -94,6 +105,23 @@ public static MMapDirectory setPreload(MMapDirectory mMapDirectory, LockFactory
94105 return mMapDirectory ;
95106 }
96107
108+ /**
109+ * Return a {@link FilterDirectory} around the provided {@link Directory} that forcefully disables {@link IOContext#RANDOM random
110+ * access}.
111+ */
112+ static Directory disableRandomAdvice (Directory dir ) {
113+ return new FilterDirectory (dir ) {
114+ @ Override
115+ public IndexInput openInput (String name , IOContext context ) throws IOException {
116+ if (context .randomAccess ) {
117+ context = IOContext .READ ;
118+ }
119+ assert context .randomAccess == false ;
120+ return super .openInput (name , context );
121+ }
122+ };
123+ }
124+
97125 /**
98126 * Returns true iff the directory is a hybrid fs directory
99127 */
0 commit comments