|
35 | 35 | import org.apache.lucene.index.VectorSimilarityFunction; |
36 | 36 | import org.apache.lucene.store.Directory; |
37 | 37 | import org.apache.lucene.store.FSDirectory; |
| 38 | +import org.apache.lucene.store.IOContext; |
38 | 39 | import org.apache.lucene.store.MMapDirectory; |
39 | 40 | import org.apache.lucene.store.NativeFSLockFactory; |
| 41 | +import org.apache.lucene.store.ReadAdvice; |
40 | 42 | import org.apache.lucene.util.PrintStreamInfoStream; |
41 | 43 | import org.elasticsearch.common.io.Channels; |
| 44 | +import org.elasticsearch.index.StandardIOBehaviorHint; |
42 | 45 | import org.elasticsearch.index.store.FsDirectoryFactory; |
43 | 46 |
|
44 | 47 | import java.io.IOException; |
|
51 | 54 | import java.util.ArrayList; |
52 | 55 | import java.util.List; |
53 | 56 | import java.util.Objects; |
| 57 | +import java.util.Optional; |
54 | 58 | import java.util.concurrent.ExecutionException; |
55 | 59 | import java.util.concurrent.ExecutorService; |
56 | 60 | import java.util.concurrent.Executors; |
57 | 61 | import java.util.concurrent.Future; |
58 | 62 | import java.util.concurrent.TimeUnit; |
59 | 63 | import java.util.concurrent.atomic.AtomicInteger; |
| 64 | +import java.util.function.BiFunction; |
60 | 65 |
|
61 | 66 | import static org.elasticsearch.test.knn.KnnIndexTester.logger; |
62 | 67 |
|
@@ -241,11 +246,21 @@ public boolean isEnabled(String component) { |
241 | 246 | static Directory getDirectory(Path indexPath) throws IOException { |
242 | 247 | Directory dir = FSDirectory.open(indexPath); |
243 | 248 | if (dir instanceof MMapDirectory mmapDir) { |
| 249 | + mmapDir.setReadAdvice(getReadAdviceFunc()); // enable madvise |
244 | 250 | return new FsDirectoryFactory.HybridDirectory(NativeFSLockFactory.INSTANCE, mmapDir, 64); |
245 | 251 | } |
246 | 252 | return dir; |
247 | 253 | } |
248 | 254 |
|
| 255 | + private static BiFunction<String, IOContext, Optional<ReadAdvice>> getReadAdviceFunc() { |
| 256 | + return (name, context) -> { |
| 257 | + if (context.hints().contains(StandardIOBehaviorHint.INSTANCE) || name.endsWith(".cfs")) { |
| 258 | + return Optional.of(ReadAdvice.NORMAL); |
| 259 | + } |
| 260 | + return MMapDirectory.ADVISE_BY_CONTEXT.apply(name, context); |
| 261 | + }; |
| 262 | + } |
| 263 | + |
249 | 264 | static class IndexerThread extends Thread { |
250 | 265 | private final IndexWriter iw; |
251 | 266 | private final AtomicInteger numDocsIndexed; |
|
0 commit comments