22
22
import org .apache .lucene .store .SimpleFSLockFactory ;
23
23
import org .elasticsearch .common .settings .Setting ;
24
24
import org .elasticsearch .common .settings .Setting .Property ;
25
+ import org .elasticsearch .common .util .FeatureFlag ;
25
26
import org .elasticsearch .core .IOUtils ;
26
27
import org .elasticsearch .index .IndexModule ;
27
28
import org .elasticsearch .index .IndexSettings ;
36
37
37
38
public class FsDirectoryFactory implements IndexStorePlugin .DirectoryFactory {
38
39
40
+ private static final FeatureFlag MADV_RANDOM_FEATURE_FLAG = new FeatureFlag ("madv_random" );
41
+
39
42
public static final Setting <LockFactory > INDEX_LOCK_FACTOR_SETTING = new Setting <>("index.store.fs.fs_lock" , "native" , (s ) -> {
40
43
return switch (s ) {
41
44
case "native" -> NativeFSLockFactory .INSTANCE ;
@@ -67,12 +70,20 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index
67
70
// Use Lucene defaults
68
71
final FSDirectory primaryDirectory = FSDirectory .open (location , lockFactory );
69
72
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 ;
71
78
} else {
72
79
return primaryDirectory ;
73
80
}
74
81
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 ;
76
87
case SIMPLEFS :
77
88
case NIOFS :
78
89
return new NIOFSDirectory (location , lockFactory );
@@ -94,6 +105,23 @@ public static MMapDirectory setPreload(MMapDirectory mMapDirectory, LockFactory
94
105
return mMapDirectory ;
95
106
}
96
107
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
+
97
125
/**
98
126
* Returns true iff the directory is a hybrid fs directory
99
127
*/
0 commit comments