Skip to content

Apache Lucene open-source search software

Open files twice for sequential reads only when it helps (#16883)

Jim Ferencziea57360912ed
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
index 99c17c736b0..544006fcc0f 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
@@ -53,12 +53,14 @@ import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.store.ChecksumIndexInput;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.FileDataHint;
import org.apache.lucene.store.FileTypeHint;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.IOSupplier;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.VectorUtil;
@@ -254,6 +256,18 @@ public class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader
@Override
public BinarizedVectorValues getFloatVectorValues(String field) throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field));
+ }
+
+ @Override
+ public BinarizedVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field, access));
+ }
+
+ /** The raw vectors of {@code field} paired with its quantized vectors. */
+ private BinarizedVectorValues withQuantized(String field, IOSupplier<FloatVectorValues> rawValues)
+ throws IOException {
FieldEntry fi = fields.get(field);
if (fi == null) {
return null;
@@ -280,7 +294,7 @@ public class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader
fi.vectorDataOffset,
fi.vectorDataLength,
quantizedVectorData);
- return new BinarizedVectorValues(rawVectorsReader.getFloatVectorValues(field), bvv);
+ return new BinarizedVectorValues(rawValues.get(), bvv);
}
@Override
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
index aacce895b4d..87d10403722 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
@@ -44,10 +44,12 @@ import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.internal.hppc.IntObjectHashMap;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.store.ChecksumIndexInput;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.FileDataHint;
import org.apache.lucene.store.FileTypeHint;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.util.IOSupplier;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier;
@@ -217,8 +219,20 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
@Override
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field));
+ }
+
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field, access));
+ }
+
+ /** The raw vectors of {@code field} paired with its quantized vectors. */
+ private FloatVectorValues withQuantized(String field, IOSupplier<FloatVectorValues> rawValues)
+ throws IOException {
final FieldEntry fieldEntry = getFieldEntry(field);
- final FloatVectorValues rawVectorValues = rawVectorsReader.getFloatVectorValues(field);
+ final FloatVectorValues rawVectorValues = rawValues.get();
OffHeapQuantizedByteVectorValues quantizedByteVectorValues =
OffHeapQuantizedByteVectorValues.load(
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene102/TestLucene102HnswBinaryQuantizedVectorsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene102/TestLucene102HnswBinaryQuantizedVectorsFormat.java
index 81e2b0e0add..4ecdca30e50 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene102/TestLucene102HnswBinaryQuantizedVectorsFormat.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene102/TestLucene102HnswBinaryQuantizedVectorsFormat.java
@@ -49,11 +49,13 @@ import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.SameThreadExecutorService;
@@ -199,8 +201,8 @@ public class TestLucene102HnswBinaryQuantizedVectorsFormat extends BaseKnnVector
}
/**
- * A merge instance reaches the raw vectors reader, which opens the raw vectors again for the
- * merge, and finishing the merge closes them.
+ * Merges and sequential values reach the raw vectors reader, which opens the raw vectors again
+ * for sequential reads and closes them with the reader.
*/
public void testMergeInstanceOpensTheRawVectors() throws IOException {
List<AtomicBoolean> mergeOpens = new CopyOnWriteArrayList<>();
@@ -210,16 +212,29 @@ public class TestLucene102HnswBinaryQuantizedVectorsFormat extends BaseKnnVector
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
IndexInput in = super.openInput(name, context);
- if (name.endsWith(".vec") == false || context.context() != IOContext.Context.MERGE) {
+ if (name.endsWith(".vec") == false) {
return in;
}
+ boolean sequential = context.hints().contains(DataAccessHint.SEQUENTIAL);
AtomicBoolean closed = new AtomicBoolean();
- mergeOpens.add(closed);
+ if (sequential) {
+ mergeOpens.add(closed);
+ }
return new FilterIndexInput(name, in) {
+ // reads like a memory-mapped input using ADVISE_BY_CONTEXT
+ @Override
+ public boolean isEfficientFor(IOContext other) {
+ return MMapDirectory.ADVISE_BY_CONTEXT
+ .apply(name, context)
+ .equals(MMapDirectory.ADVISE_BY_CONTEXT.apply(name, other));
+ }
+
@Override
public IndexInput slice(String sliceDescription, long offset, long length)
throws IOException {
- mergeSlices.incrementAndGet(); // the vectors are read through slices
+ if (sequential) {
+ mergeSlices.incrementAndGet(); // the vectors are read through slices
+ }
return super.slice(sliceDescription, offset, length);
}
@@ -258,9 +273,15 @@ public class TestLucene102HnswBinaryQuantizedVectorsFormat extends BaseKnnVector
assertArrayEquals(expected.vectorValue(ord), actual.vectorValue(ord), 0f);
}
+ int slices = mergeSlices.get();
+ vectors.getFloatVectorValues("f", DataAccessHint.SEQUENTIAL);
+ assertTrue("sequential values read the same input", mergeSlices.get() > slices);
+ assertEquals(1, mergeOpens.size());
+
merging.finishMerge();
- assertTrue("finishing the merge did not close them", mergeOpens.get(0).get());
+ assertFalse("kept while the reader is open", mergeOpens.get(0).get());
}
+ assertTrue("closed with the reader", mergeOpens.get(0).get());
}
}
}
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99HnswScalarQuantizedVectorsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99HnswScalarQuantizedVectorsFormat.java
index 27142a71873..a5544e63c79 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99HnswScalarQuantizedVectorsFormat.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99HnswScalarQuantizedVectorsFormat.java
@@ -37,11 +37,13 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase;
import org.apache.lucene.tests.util.TestUtil;
@@ -88,8 +90,8 @@ public class TestLucene99HnswScalarQuantizedVectorsFormat extends BaseKnnVectors
}
/**
- * A merge instance reaches the raw vectors reader, which opens the raw vectors again for the
- * merge, and finishing the merge closes them.
+ * Merges and sequential values reach the raw vectors reader, which opens the raw vectors again
+ * for sequential reads and closes them with the reader.
*/
public void testMergeInstanceOpensTheRawVectors() throws IOException {
List<AtomicBoolean> mergeOpens = new CopyOnWriteArrayList<>();
@@ -99,16 +101,29 @@ public class TestLucene99HnswScalarQuantizedVectorsFormat extends BaseKnnVectors
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
IndexInput in = super.openInput(name, context);
- if (name.endsWith(".vec") == false || context.context() != IOContext.Context.MERGE) {
+ if (name.endsWith(".vec") == false) {
return in;
}
+ boolean sequential = context.hints().contains(DataAccessHint.SEQUENTIAL);
AtomicBoolean closed = new AtomicBoolean();
- mergeOpens.add(closed);
+ if (sequential) {
+ mergeOpens.add(closed);
+ }
return new FilterIndexInput(name, in) {
+ // reads like a memory-mapped input using ADVISE_BY_CONTEXT
+ @Override
+ public boolean isEfficientFor(IOContext other) {
+ return MMapDirectory.ADVISE_BY_CONTEXT
+ .apply(name, context)
+ .equals(MMapDirectory.ADVISE_BY_CONTEXT.apply(name, other));
+ }
+
@Override
public IndexInput slice(String sliceDescription, long offset, long length)
throws IOException {
- mergeSlices.incrementAndGet(); // the vectors are read through slices
+ if (sequential) {
+ mergeSlices.incrementAndGet(); // the vectors are read through slices
+ }
return super.slice(sliceDescription, offset, length);
}
@@ -147,9 +162,15 @@ public class TestLucene99HnswScalarQuantizedVectorsFormat extends BaseKnnVectors
assertArrayEquals(expected.vectorValue(ord), actual.vectorValue(ord), 0f);
}
+ int slices = mergeSlices.get();
+ vectors.getFloatVectorValues("f", DataAccessHint.SEQUENTIAL);
+ assertTrue("sequential values read the same input", mergeSlices.get() > slices);
+ assertEquals(1, mergeOpens.size());
+
merging.finishMerge();
- assertTrue("finishing the merge did not close them", mergeOpens.get(0).get());
+ assertFalse("kept while the reader is open", mergeOpens.get(0).get());
}
+ assertTrue("closed with the reader", mergeOpens.get(0).get());
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader.java
index 1fec137f17f..8a0887987a9 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader.java
@@ -32,6 +32,7 @@ import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TotalHits;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Bits;
@@ -67,6 +68,17 @@ public abstract class KnnVectorsReader implements Closeable {
*/
public abstract FloatVectorValues getFloatVectorValues(String field) throws IOException;
+ /**
+ * Returns the {@link FloatVectorValues} for the given {@code field}, to be read with the given
+ * access pattern, such as {@link DataAccessHint#SEQUENTIAL} to read all vectors in order.
+ *
+ * <p>The default implementation ignores {@code access}.
+ */
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return getFloatVectorValues(field);
+ }
+
/**
* Returns the {@link ByteVectorValues} for the given {@code field}. The behavior is undefined if
* the given field doesn't have KNN vectors enabled on its {@link FieldInfo}. The return value is
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsReader.java
index b5406cfc0a6..447f88716b0 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsReader.java
@@ -49,6 +49,7 @@ import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.store.ChecksumIndexInput;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FileDataHint;
import org.apache.lucene.store.FileTypeHint;
@@ -56,6 +57,7 @@ import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.IOSupplier;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier;
@@ -243,6 +245,18 @@ public class Lucene104ScalarQuantizedVectorsReader extends FlatVectorsReader
@Override
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field));
+ }
+
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return withQuantized(field, () -> rawVectorsReader.getFloatVectorValues(field, access));
+ }
+
+ /** The raw vectors of {@code field} paired with its quantized vectors. */
+ private FloatVectorValues withQuantized(String field, IOSupplier<FloatVectorValues> rawValues)
+ throws IOException {
FieldEntry fi = fields.get(field);
if (fi == null) {
return null;
@@ -257,7 +271,7 @@ public class Lucene104ScalarQuantizedVectorsReader extends FlatVectorsReader
+ VectorEncoding.FLOAT32);
}
- FloatVectorValues rawFloatVectorValues = rawVectorsReader.getFloatVectorValues(field);
+ FloatVectorValues rawFloatVectorValues = rawValues.get();
OffHeapScalarQuantizedVectorValues sqvv = loadQuantizedVectorValues(fi);
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
index b33d6231caa..8d6aca49d8f 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
@@ -19,8 +19,10 @@ package org.apache.lucene.codecs.lucene90;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.codecs.CodecUtil;
@@ -54,11 +56,14 @@ final class Lucene90CompoundReader extends CompoundDirectory {
private final String dataFileName;
private final Map<String, FileEntry> entries;
private final IndexInput handle;
- // the mapping merges read through, opened by the first merge open and closed with this reader
- private IndexInput mergeHandle;
+ // copies of the data file, starting with handle, guarded by this
+ private final List<Copy> copies = new ArrayList<>();
private boolean closed;
private int version;
+ /** A copy of the data file and the context each inner file was first opened with on it. */
+ private record Copy(IndexInput data, Map<String, IOContext> contexts) {}
+
/** Create a new CompoundFileDirectory. */
// TODO: we should just pre-strip "entries" and append segment name up-front like simpletext?
// this need not be a "general purpose" directory anymore (it only writes index files)
@@ -80,6 +85,7 @@ final class Lucene90CompoundReader extends CompoundDirectory {
+ CodecUtil.footerLength();
handle = directory.openInput(dataFileName, IOContext.DEFAULT);
+ copies.add(new Copy(handle, new HashMap<>()));
try {
CodecUtil.checkIndexHeader(
handle, Lucene90CompoundFormat.DATA_CODEC, version, version, si.getId(), "");
@@ -148,13 +154,15 @@ final class Lucene90CompoundReader extends CompoundDirectory {
@Override
public void close() throws IOException {
- IndexInput merge;
+ List<IndexInput> toClose = new ArrayList<>();
synchronized (this) {
closed = true;
- merge = mergeHandle == handle ? null : mergeHandle;
- mergeHandle = null;
+ for (Copy copy : copies) {
+ toClose.add(copy.data());
+ }
+ copies.clear();
}
- IOUtils.close(handle, merge);
+ IOUtils.close(toClose);
}
@Override
@@ -176,28 +184,31 @@ final class Lucene90CompoundReader extends CompoundDirectory {
+ entries.keySet()
+ ")");
}
- IndexInput data = context.context() == IOContext.Context.MERGE ? mergeHandle() : handle;
- return data.slice(name, entry.offset, entry.length, context);
- }
-
- /**
- * A merge reads through a mapping of its own, so the access it advises does not replace the
- * advice of searches reading the same inner files. Merges of this segment share it. Like the
- * search mapping, it is opened without advice: each inner file is advised on its own region.
- */
- private synchronized IndexInput mergeHandle() throws IOException {
- if (closed) {
- throw new AlreadyClosedException("this Directory is closed");
- }
- if (mergeHandle == null) {
+ synchronized (this) {
+ if (closed) {
+ throw new AlreadyClosedException("this Directory is closed");
+ }
+ for (Copy copy : copies) {
+ // an inner file keeps the advice it was first opened with on a copy
+ IOContext first = copy.contexts().putIfAbsent(id, context);
+ IndexInput slice =
+ copy.data().slice(name, entry.offset, entry.length, first == null ? context : first);
+ if (first == null || slice.isEfficientFor(context)) {
+ return slice;
+ }
+ }
+ final IndexInput data;
try {
- mergeHandle = directory.openInput(dataFileName, IOContext.DEFAULT);
+ // without advice, like handle, so inner files advise their own pages
+ data = directory.openInput(dataFileName, IOContext.DEFAULT);
} catch (@SuppressWarnings("unused") FileNotFoundException | NoSuchFileException e) {
- // an open reader outlives its files, so fall back to the mapping it already holds
- mergeHandle = handle;
+ // an open reader outlives its files, so read through the copy it already holds
+ IOContext first = copies.getFirst().contexts().get(id);
+ return handle.slice(name, entry.offset, entry.length, first);
}
+ copies.add(new Copy(data, new HashMap<>(Map.of(id, context))));
+ return data.slice(name, entry.offset, entry.length, context);
}
- return mergeHandle;
}
/** Returns an array of strings, one for each file in the directory. */
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
index d2fb330ead2..8c4d218abd2 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
@@ -43,6 +43,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
+import java.util.stream.Stream;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.codecs.StoredFieldsReader;
import org.apache.lucene.codecs.compressing.CompressionMode;
@@ -102,17 +103,14 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
private final long[] prefetchedBlockIDCache;
private int prefetchedBlockIDCacheIndex;
private volatile boolean closed;
- // what a merge needs to map the data file for itself
+ // to open the data file again for sequential reads
private final Directory directory;
private final String fieldsStreamFN;
- private final IOContext context;
- // the reader this one was cloned from, which owns the mapping merges read
+ private final IOContext dataContext;
+ // the reader this one was cloned from
private final Lucene90CompressingStoredFieldsReader original;
- private IndexInput mergeFieldsStream;
- // merge instances handed out and not yet finished
- private int mergeInstances;
- // set once this merge instance has released the mapping
- private boolean mergeFinished;
+ // the input for sequential reads, opened lazily and closed with this reader
+ private IndexInput sequentialFieldsStream;
// used by clone
private Lucene90CompressingStoredFieldsReader(
@@ -122,7 +120,7 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
this.fieldsStream = fieldsStream;
this.directory = reader.directory;
this.fieldsStreamFN = reader.fieldsStreamFN;
- this.context = reader.context;
+ this.dataContext = reader.dataContext;
this.original = reader.original;
this.indexReader = reader.indexReader.clone();
this.maxPointer = reader.maxPointer;
@@ -155,17 +153,15 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
fieldInfos = fn;
numDocs = si.maxDoc();
this.directory = d;
- this.context = context;
+ this.dataContext =
+ context.withHints(FileTypeHint.DATA, DataAccessHint.RANDOM, NoReuseHint.INSTANCE);
this.original = this;
this.fieldsStreamFN = IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_EXTENSION);
ChecksumIndexInput metaIn = null;
try {
// Open the data file
- fieldsStream =
- d.openInput(
- fieldsStreamFN,
- context.withHints(FileTypeHint.DATA, DataAccessHint.RANDOM, NoReuseHint.INSTANCE));
+ fieldsStream = d.openInput(fieldsStreamFN, dataContext);
version =
CodecUtil.checkIndexHeader(
fieldsStream, formatName, VERSION_START, VERSION_CURRENT, si.getId(), segmentSuffix);
@@ -273,15 +269,19 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
/** Close the underlying {@link IndexInput}s. */
@Override
public void close() throws IOException {
- if (!closed) {
- IOUtils.close(indexReader, fieldsStream, mergeFieldsStreamToClose());
+ IndexInput sequential;
+ synchronized (this) {
+ if (closed) {
+ return;
+ }
closed = true;
+ sequential =
+ original == this && sequentialFieldsStream != fieldsStream
+ ? sequentialFieldsStream
+ : null;
+ sequentialFieldsStream = null;
}
- }
-
- /** The mapping a merge opened, read under the lock that guards it, closed outside it. */
- private synchronized IndexInput mergeFieldsStreamToClose() {
- return original == this && mergeFieldsStream != fieldsStream ? mergeFieldsStream : null;
+ IOUtils.close(indexReader, fieldsStream, sequential);
}
static void readField(
@@ -728,68 +728,38 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
@Override
public StoredFieldsReader getMergeInstance() throws IOException {
ensureOpen();
- IndexInput stream = original.mergeFieldsStream();
- boolean success = false;
- try {
- StoredFieldsReader mergeInstance =
- new Lucene90CompressingStoredFieldsReader(this, true, stream.clone());
- success = true;
- return mergeInstance;
- } finally {
- if (success == false) {
- original.releaseMergeFieldsStream();
- }
- }
+ return new Lucene90CompressingStoredFieldsReader(
+ this, true, original.sequentialFieldsStream().clone());
}
- /**
- * The data file as a merge reads it, front to back. Advice applies to a whole mapping, so a merge
- * maps the file again. Mapped on the first merge, released by {@link #finishMerge()}.
- */
- private synchronized IndexInput mergeFieldsStream() throws IOException {
+ /** Returns the input for sequential reads, opening the data file again if needed. */
+ private synchronized IndexInput sequentialFieldsStream() throws IOException {
assert original == this;
ensureOpen();
- if (mergeFieldsStream == null) {
- if (context.context() == IOContext.Context.MERGE) {
- // opened by a merge to begin with, so it already advises sequential reads
- mergeFieldsStream = fieldsStream;
+ if (sequentialFieldsStream == null) {
+ IOContext sequential = sequentialContext();
+ if (fieldsStream.isEfficientFor(sequential)) {
+ sequentialFieldsStream = fieldsStream;
} else {
try {
- mergeFieldsStream =
- directory.openInput(
- fieldsStreamFN,
- IOContext.merge()
- .withHints(
- FileTypeHint.DATA, DataAccessHint.SEQUENTIAL, NoReuseHint.INSTANCE));
+ sequentialFieldsStream = directory.openInput(fieldsStreamFN, sequential);
} catch (FileNotFoundException | NoSuchFileException _) {
- // an open reader outlives its files, so fall back to the mapping it already holds
- mergeFieldsStream = fieldsStream;
+ // an open reader outlives its files, so read through the input it already holds
+ sequentialFieldsStream = fieldsStream;
}
}
}
- mergeInstances++;
- return mergeFieldsStream;
- }
-
- /** Releases the mapping once no other merge instance holds it. A later merge maps it again. */
- @Override
- public void finishMerge() throws IOException {
- if (merging == false || mergeFinished) {
- return;
- }
- mergeFinished = true;
- original.releaseMergeFieldsStream();
+ return sequentialFieldsStream;
}
- private synchronized void releaseMergeFieldsStream() throws IOException {
- assert original == this;
- if (closed || mergeInstances == 0 || --mergeInstances > 0) {
- return;
- }
- if (mergeFieldsStream != null && mergeFieldsStream != fieldsStream) {
- mergeFieldsStream.close();
- }
- mergeFieldsStream = null;
+ /** Returns the context of the data file with sequential access. */
+ private IOContext sequentialContext() {
+ return dataContext.withHints(
+ Stream.concat(
+ dataContext.hints().stream()
+ .filter(hint -> hint instanceof DataAccessHint == false),
+ Stream.of(DataAccessHint.SEQUENTIAL))
+ .toArray(IOContext.FileOpenHint[]::new));
}
int getVersion() {
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
index 91dcb47a6e1..7689cd5d63b 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
@@ -44,6 +44,7 @@ import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.internal.hppc.IntObjectHashMap;
+import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
@@ -72,13 +73,12 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
private final IOContext dataContext;
private final Directory directory;
private final String vectorDataFN;
- // the reader that owns the mapping merges read
+ // the reader this one was created from
private final Lucene99FlatVectorsReader original;
- private IndexInput mergeVectorData;
- // merge instances handed out and not yet finished
- private int mergeInstances;
- // on a merge instance: whether it gave the mapping back, guarded by the original's lock
- private boolean finished;
+ // the input for sequential reads, opened lazily and closed with this reader
+ private IndexInput sequentialVectorData;
+ // guarded by this
+ private boolean closed;
public Lucene99FlatVectorsReader(SegmentReadState state, FlatVectorsScorer scorer)
throws IOException {
@@ -109,7 +109,7 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
}
}
- /** Reads the same fields as {@code reader}, through the mapping a merge opened for itself. */
+ /** Reads the same fields as {@code reader}, through {@code vectorData}. */
private Lucene99FlatVectorsReader(Lucene99FlatVectorsReader reader, IndexInput vectorData) {
this.fields = reader.fields;
this.vectorScorer = reader.vectorScorer;
@@ -212,59 +212,40 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
@Override
public FlatVectorsReader getMergeInstance() throws IOException {
- if (mergeNeedsItsOwnMapping() == false) {
- return this;
- }
- IndexInput data = original.mergeVectorData();
- boolean success = false;
- try {
- FlatVectorsReader mergeInstance = new Lucene99FlatVectorsReader(this, data.clone());
- success = true;
- return mergeInstance;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(original.release());
- }
- }
+ IndexInput data = original.sequentialVectorData();
+ return data == vectorData ? this : new Lucene99FlatVectorsReader(this, data.clone());
}
- /**
- * Whether a merge has to map the file again: only when searches read it at random. Otherwise the
- * mapping searches use carries no advice a merge reading front to back would suffer from, and a
- * reader a merge opened already reads the file the way a merge does.
- */
- private boolean mergeNeedsItsOwnMapping() {
- return dataContext.context() != IOContext.Context.MERGE
- && dataContext.hints().contains(DataAccessHint.RANDOM);
- }
-
- /**
- * The vectors as a merge reads them, front to back. Advice belongs to a mapping, so a merge maps
- * the file again. Mapped on the first merge, released by {@link #finishMerge()}.
- */
- private synchronized IndexInput mergeVectorData() throws IOException {
+ /** Returns the input for sequential reads, opening the data file again if needed. */
+ private synchronized IndexInput sequentialVectorData() throws IOException {
assert original == this;
- if (mergeVectorData == null) {
- try {
- mergeVectorData = directory.openInput(vectorDataFN, mergeContext());
- } catch (FileNotFoundException | NoSuchFileException _) {
- // an open reader outlives its files, so fall back to the mapping it already holds
- mergeVectorData = vectorData;
+ if (closed) {
+ throw new AlreadyClosedException("this reader is closed");
+ }
+ if (sequentialVectorData == null) {
+ IOContext sequential = sequentialContext();
+ if (vectorData.isEfficientFor(sequential)) {
+ sequentialVectorData = vectorData;
+ } else {
+ try {
+ sequentialVectorData = directory.openInput(vectorDataFN, sequential);
+ } catch (FileNotFoundException | NoSuchFileException _) {
+ // an open reader outlives its files, so read through the input it already holds
+ sequentialVectorData = vectorData;
+ }
}
}
- mergeInstances++;
- return mergeVectorData;
+ return sequentialVectorData;
}
- /** The caller's context as a merge reading the file front to back: only the access changes. */
- private IOContext mergeContext() {
- return IOContext.merge()
- .withHints(
- Stream.concat(
- dataContext.hints().stream()
- .filter(hint -> hint instanceof DataAccessHint == false),
- Stream.of(DataAccessHint.SEQUENTIAL))
- .toArray(IOContext.FileOpenHint[]::new));
+ /** Returns the context of the data file with sequential access. */
+ private IOContext sequentialContext() {
+ return dataContext.withHints(
+ Stream.concat(
+ dataContext.hints().stream()
+ .filter(hint -> hint instanceof DataAccessHint == false),
+ Stream.of(DataAccessHint.SEQUENTIAL))
+ .toArray(IOContext.FileOpenHint[]::new));
}
private FieldEntry getFieldEntryOrThrow(String field) {
@@ -292,6 +273,19 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
@Override
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
+ return loadFloatVectorValues(field, vectorData);
+ }
+
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ IndexInput data =
+ access == DataAccessHint.SEQUENTIAL ? original.sequentialVectorData() : vectorData;
+ return loadFloatVectorValues(field, data);
+ }
+
+ private FloatVectorValues loadFloatVectorValues(String field, IndexInput data)
+ throws IOException {
final FieldEntry fieldEntry = getFieldEntry(field, VectorEncoding.FLOAT32);
return OffHeapFloatVectorValues.load(
fieldEntry.similarityFunction,
@@ -301,7 +295,7 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
fieldEntry.dimension,
fieldEntry.vectorDataOffset,
fieldEntry.vectorDataLength,
- vectorData);
+ data);
}
@Override
@@ -389,58 +383,19 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
target);
}
- /**
- * Closes the mapping a merge used, once no merge instance holds it. A later merge maps the file
- * again. Only a merge instance holds the mapping, and it gives it back once: finishing the reader
- * it came from, or finishing it again, releases nothing.
- */
- @Override
- public void finishMerge() throws IOException {
- if (original != this) {
- IOUtils.close(original.releaseMergeVectorData(this));
- }
- }
-
- /** Gives back the hold of {@code mergeInstance}, once; returns the mapping to close, if any. */
- private synchronized IndexInput releaseMergeVectorData(Lucene99FlatVectorsReader mergeInstance) {
- assert original == this && mergeInstance.original == this;
- if (mergeInstance.finished) {
- return null;
- }
- mergeInstance.finished = true;
- return release();
- }
-
- /**
- * Gives back one hold on the mapping. Once none is left, returns it for the caller to close
- * outside the lock.
- */
- private synchronized IndexInput release() {
- assert original == this && mergeInstances > 0;
- if (--mergeInstances > 0) {
- return null;
- }
- IndexInput toClose = mergeVectorData == vectorData ? null : mergeVectorData;
- mergeVectorData = null;
- return toClose;
- }
-
@Override
public void close() throws IOException {
- IOUtils.close(vectorData, takeMergeVectorData());
- }
-
- /**
- * The mapping a merge opened, taken under the lock that guards it so that a merge finishing later
- * does not close it again, and closed outside it.
- */
- private synchronized IndexInput takeMergeVectorData() {
- if (original != this || mergeVectorData == vectorData) {
- return null;
+ IndexInput sequential;
+ synchronized (this) {
+ if (closed) {
+ return;
+ }
+ closed = true;
+ sequential =
+ original == this && sequentialVectorData != vectorData ? sequentialVectorData : null;
+ sequentialVectorData = null;
}
- IndexInput toClose = mergeVectorData;
- mergeVectorData = null;
- return toClose;
+ IOUtils.close(vectorData, sequential);
}
private record FieldEntry(
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
index 8538b16edb7..1109e9dcf71 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
@@ -278,6 +278,12 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
return flatVectorsReader.getFloatVectorValues(field);
}
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return flatVectorsReader.getFloatVectorValues(field, access);
+ }
+
@Override
public ByteVectorValues getByteVectorValues(String field) throws IOException {
return flatVectorsReader.getByteVectorValues(field);
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
index f6ffdfca8a1..59daed0d8bd 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
@@ -43,6 +43,7 @@ import org.apache.lucene.internal.hppc.IntObjectHashMap;
import org.apache.lucene.internal.hppc.ObjectCursor;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.KnnCollector;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.util.IORunnable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.hnsw.HnswGraph;
@@ -285,6 +286,17 @@ public abstract class PerFieldKnnVectorsFormat extends KnnVectorsFormat {
return reader.getFloatVectorValues(field);
}
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ final FieldInfo info = fieldInfos.fieldInfo(field);
+ final KnnVectorsReader reader;
+ if (info == null || (reader = fields.get(info.number)) == null) {
+ return null;
+ }
+ return reader.getFloatVectorValues(field, access);
+ }
+
@Override
public ByteVectorValues getByteVectorValues(String field) throws IOException {
final FieldInfo info = fieldInfos.fieldInfo(field);
diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
index 5c99098fdc2..ff490566298 100644
--- a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
@@ -238,6 +238,17 @@ public abstract class IndexInput extends DataInput implements Closeable {
*/
public void updateIOContext(IOContext context) throws IOException {}
+ /**
+ * Returns false if callers reading with the given {@code IOContext} should open the file again
+ * rather than read through this input. This is a hint based on the advice applied when this input
+ * was opened or sliced.
+ *
+ * <p>The default implementation returns {@code true}, so callers never open the file again.
+ */
+ public boolean isEfficientFor(IOContext context) {
+ return true;
+ }
+
/**
* Returns a hint whether all the contents of this input are resident in physical memory. It's a
* hint because the operating system may have paged out some of the data by the time this method
diff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
index 99e099eacfc..9bfc26ae84f 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
@@ -322,26 +322,27 @@ public class MMapDirectory extends FSDirectory {
final String resourceDescription = "MemorySegmentIndexInput(path=\"" + path.toString() + "\")";
final boolean confined = context.hints().contains(ReadOnceHint.INSTANCE);
+ // the function this input is opened with, even if setReadAdvice is called later
+ final var readAdviceFunction = readAdvice;
Function<IOContext, ReadAdvice> toReadAdvice =
- c -> readAdvice.apply(name, c).orElse(Constants.DEFAULT_READADVICE);
+ c -> readAdviceFunction.apply(name, c).orElse(Constants.DEFAULT_READADVICE);
final Arena arena = confined ? Arena.ofConfined() : getSharedArena(name, arenas);
try (var fc = FileChannel.open(path, StandardOpenOption.READ)) {
final long fileSize = fc.size();
+ final ReadAdvice advice = toReadAdvice.apply(context);
+ final boolean preloaded = preload.test(name, context);
+ // preloaded or within a page, any advice reads the same
+ final boolean withinAPage =
+ NATIVE_ACCESS.filter(na -> fileSize < na.getPageSize()).isPresent();
return MemorySegmentIndexInput.newInstance(
resourceDescription,
arena,
- map(
- arena,
- resourceDescription,
- fc,
- toReadAdvice.apply(context),
- chunkSizePower,
- preload.test(name, context),
- fileSize),
+ map(arena, resourceDescription, fc, advice, chunkSizePower, preloaded, fileSize),
fileSize,
chunkSizePower,
confined,
- toReadAdvice);
+ toReadAdvice,
+ preloaded || withinAPage ? null : advice);
} catch (Throwable t) {
arena.close();
throw t;
diff --git a/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
index 6c6dc5b67b9..42a20a81685 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MemorySegmentIndexInput.java
@@ -60,6 +60,8 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
final MemorySegment[] segments;
final Function<IOContext, ReadAdvice> toReadAdvice;
final AtomicInteger sharedPrefetchCounter;
+ // the advice applied when this input was opened or sliced, or null if any advice reads the same
+ final ReadAdvice readAdvice;
int curSegmentIndex = -1;
MemorySegment
@@ -73,7 +75,8 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
long length,
int chunkSizePower,
boolean confined,
- Function<IOContext, ReadAdvice> toReadAdvice) {
+ Function<IOContext, ReadAdvice> toReadAdvice,
+ ReadAdvice readAdvice) {
assert Arrays.stream(segments).map(MemorySegment::scope).allMatch(arena.scope()::equals);
AtomicInteger sharedPrefetchCounter = new AtomicInteger();
if (segments.length == 1) {
@@ -85,6 +88,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
} else {
return new MultiSegmentImpl(
@@ -96,6 +100,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
}
}
@@ -108,6 +113,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
int chunkSizePower,
boolean confined,
Function<IOContext, ReadAdvice> toReadAdvice,
+ ReadAdvice readAdvice,
AtomicInteger sharedPrefetchCounter) {
super(resourceDescription);
this.arena = arena;
@@ -118,6 +124,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
this.chunkSizeMask = (1L << chunkSizePower) - 1L;
this.curSegment = segments[0];
this.toReadAdvice = toReadAdvice;
+ this.readAdvice = readAdvice;
this.sharedPrefetchCounter = sharedPrefetchCounter;
}
@@ -371,27 +378,17 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
}
@Override
- public void updateIOContext(IOContext context) throws IOException {
- updateReadAdvice(toReadAdvice.apply(context));
- }
-
- private void updateReadAdvice(ReadAdvice readAdvice) throws IOException {
- if (NATIVE_ACCESS.isEmpty()) {
- return;
- }
- final NativeAccess nativeAccess = NATIVE_ACCESS.get();
-
- long offset = 0;
- for (MemorySegment seg : segments) {
- advise(
- offset,
- seg.byteSize(),
- segment -> {
- nativeAccess.madvise(segment, readAdvice);
- return true;
- });
- offset += seg.byteSize();
+ public boolean isEfficientFor(IOContext context) {
+ if (NATIVE_ACCESS.isEmpty() || readAdvice == null) {
+ return true;
}
+ // only random and sequential reads get inputs of their own
+ ReadAdvice advice = toReadAdvice.apply(context);
+ return switch (readAdvice) {
+ case RANDOM -> advice != ReadAdvice.SEQUENTIAL;
+ case SEQUENTIAL -> advice != ReadAdvice.RANDOM;
+ case NORMAL -> true;
+ };
}
boolean advise(long offset, long length, IOFunction<MemorySegment, Boolean> advice)
@@ -570,6 +567,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
} else {
clone =
@@ -582,6 +580,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
}
try {
@@ -599,6 +598,11 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
*/
@Override
public final MemorySegmentIndexInput slice(String sliceDescription, long offset, long length) {
+ return sliceWithAdvice(sliceDescription, offset, length, readAdvice);
+ }
+
+ private MemorySegmentIndexInput sliceWithAdvice(
+ String sliceDescription, long offset, long length, ReadAdvice readAdvice) {
if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
"slice() "
@@ -613,38 +617,41 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
+ this);
}
- return buildSlice(sliceDescription, offset, length);
+ return buildSlice(sliceDescription, offset, length, readAdvice);
}
@Override
public final MemorySegmentIndexInput slice(
String sliceDescription, long offset, long length, IOContext context) throws IOException {
- MemorySegmentIndexInput slice = slice(sliceDescription, offset, length);
ReadAdvice advice = toReadAdvice.apply(context);
- if (NATIVE_ACCESS.isPresent() && advice != ReadAdvice.NORMAL) {
+ if (NATIVE_ACCESS.isEmpty() || advice == ReadAdvice.NORMAL) {
// No need to madvise with a normal advice, since it's the OS' default.
- final NativeAccess nativeAccess = NATIVE_ACCESS.get();
- if (length >= nativeAccess.getPageSize()) {
- // Only set the read advice if the inner file is large enough. Otherwise the cons are likely
- // outweighing the pros as we're:
- // - potentially overriding the advice of other files that share the same pages,
- // - paying the cost of a madvise system call for little value.
- // We could align inner files with the page size to avoid the first issue, but again the
- // pros don't clearly overweigh the cons.
- slice.advise(
- 0,
- slice.length,
- segment -> {
- nativeAccess.madvise(segment, advice);
- return true;
- });
- }
+ return slice(sliceDescription, offset, length);
}
+ final NativeAccess nativeAccess = NATIVE_ACCESS.get();
+ if (length < nativeAccess.getPageSize()) {
+ // Only set the read advice if the inner file is large enough. Otherwise the cons are likely
+ // outweighing the pros as we're:
+ // - potentially overriding the advice of other files that share the same pages,
+ // - paying the cost of a madvise system call for little value.
+ // We could align inner files with the page size to avoid the first issue, but again the
+ // pros don't clearly overweigh the cons.
+ return sliceWithAdvice(sliceDescription, offset, length, null);
+ }
+ MemorySegmentIndexInput slice = sliceWithAdvice(sliceDescription, offset, length, advice);
+ slice.advise(
+ 0,
+ slice.length,
+ segment -> {
+ nativeAccess.madvise(segment, advice);
+ return true;
+ });
return slice;
}
/** Builds the actual sliced IndexInput (may apply extra offset in subclasses). * */
- MemorySegmentIndexInput buildSlice(String sliceDescription, long offset, long length) {
+ MemorySegmentIndexInput buildSlice(
+ String sliceDescription, long offset, long length, ReadAdvice readAdvice) {
ensureOpen();
ensureAccessible();
final MemorySegment[] slices;
@@ -675,6 +682,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
} else {
return new MultiSegmentImpl(
@@ -686,6 +694,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
}
}
@@ -731,6 +740,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
int chunkSizePower,
boolean confined,
Function<IOContext, ReadAdvice> toReadAdvice,
+ ReadAdvice readAdvice,
AtomicInteger sharedPrefetchCounter) {
super(
resourceDescription,
@@ -740,6 +750,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
this.curSegmentIndex = 0;
}
@@ -845,6 +856,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
int chunkSizePower,
boolean confined,
Function<IOContext, ReadAdvice> toReadAdvice,
+ ReadAdvice readAdvice,
AtomicInteger sharedPrefetchCounter) {
super(
resourceDescription,
@@ -854,6 +866,7 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
chunkSizePower,
confined,
toReadAdvice,
+ readAdvice,
sharedPrefetchCounter);
this.offset = offset;
try {
@@ -921,8 +934,9 @@ abstract class MemorySegmentIndexInput extends IndexInput implements MemorySegme
}
@Override
- MemorySegmentIndexInput buildSlice(String sliceDescription, long ofs, long length) {
- return super.buildSlice(sliceDescription, this.offset + ofs, length);
+ MemorySegmentIndexInput buildSlice(
+ String sliceDescription, long ofs, long length, ReadAdvice readAdvice) {
+ return super.buildSlice(sliceDescription, this.offset + ofs, length, readAdvice);
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90CompoundFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90CompoundFormat.java
index 11ae5a8c198..7bf7a2e15c7 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90CompoundFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90CompoundFormat.java
@@ -20,7 +20,9 @@ import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.CountDownLatch;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.CodecUtil;
@@ -30,14 +32,17 @@ import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSLockFactory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.store.MemorySegmentAccessInput;
+import org.apache.lucene.store.ReadAdvice;
import org.apache.lucene.tests.index.BaseCompoundFormatTestCase;
import org.apache.lucene.tests.util.TestUtil;
+import org.apache.lucene.util.IOUtils;
public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
private final Codec codec = TestUtil.getDefaultCodec();
@@ -103,55 +108,78 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
dir.close();
}
- public void testMergesReadThroughAMappingOfTheirOwn() throws IOException {
+ /** Random and sequential reads of an inner file use mappings of their own, in either order. */
+ public void testRandomAndSequentialReadsUseTheirOwnMapping() throws IOException {
+ IOContext normal = IOContext.DEFAULT;
+ for (List<IOContext> order :
+ List.of(
+ List.of(RANDOM, normal, SEQUENTIAL),
+ List.of(SEQUENTIAL, normal, RANDOM),
+ List.of(normal, RANDOM, SEQUENTIAL))) {
+ try (Directory base = newDirectory()) {
+ SegmentInfo si = writeCompound(base);
+ DataOpens dir = new DataOpens(base);
+ int mappings = order.getFirst() == normal ? 1 : 2;
+ try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si)) {
+ for (String file : List.of("_123.0", "_123.1")) {
+ List<IndexInput> inputs = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ for (IOContext context : order) {
+ inputs.add(cfs.openInput(file, context));
+ }
+ }
+ for (IndexInput input : inputs) {
+ try (IndexInput expected = base.openInput(file, IOContext.DEFAULT)) {
+ assertSameStreams(file, expected, input);
+ }
+ }
+ IOUtils.close(inputs);
+ }
+ assertEquals(order.toString(), Collections.nCopies(mappings, normal), dir.opens);
+ Set<IOContext> advised = mappings == 1 ? Set.of(normal) : Set.of(RANDOM, SEQUENTIAL);
+ assertEquals("each copy keeps the advice of its first slice", advised, dir.advised);
+ assertEquals(0, dir.closes);
+ }
+ assertEquals("closed with the reader", mappings, dir.closes);
+ }
+ }
+ }
+
+ /** No second mapping when inputs serve every context. */
+ public void testNoSecondMappingWhenNotNeeded() throws IOException {
try (Directory base = newDirectory()) {
SegmentInfo si = writeCompound(base);
- DataOpens dir = new DataOpens(base);
- Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
- try {
+ DataOpens dir = new DataOpens(base, false);
+ try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
+ IndexInput search = cfs.openInput("_123.0", RANDOM);
+ IndexInput sequential = cfs.openInput("_123.0", SEQUENTIAL)) {
assertEquals(1, dir.opens.size());
- String first = "_123.0";
- String second = "_123.1";
- try (IndexInput search = cfs.openInput(first, IOContext.DEFAULT)) {
- assertEquals("searches read the mapping opened with the reader", 1, dir.opens.size());
-
- IndexInput merge = cfs.openInput(first, IOContext.merge());
- IndexInput otherMerge = cfs.openInput(second, IOContext.merge());
- assertEquals("merges share one mapping of their own", 2, dir.opens.size());
- assertEquals(
- "opened without advice, like the search mapping",
- IOContext.DEFAULT,
- dir.opens.get(1));
- try (IndexInput expected = base.openInput(first, IOContext.DEFAULT)) {
- assertSameStreams(first, expected, merge);
- }
- try (IndexInput expected = base.openInput(first, IOContext.DEFAULT)) {
- assertSameStreams(first, expected, search);
- }
- try (IndexInput expected = base.openInput(second, IOContext.DEFAULT)) {
- assertSameStreams(second, expected, otherMerge);
- }
- merge.close();
- otherMerge.close();
- assertEquals("the merge mapping stays open with the reader", 0, dir.closes);
+ try (IndexInput expected = base.openInput("_123.0", IOContext.DEFAULT)) {
+ assertSameStreams("_123.0", expected, sequential);
+ }
+ try (IndexInput expected = base.openInput("_123.0", IOContext.DEFAULT)) {
+ assertSameStreams("_123.0", expected, search);
}
- } finally {
- cfs.close();
}
- assertEquals("closing the reader closes both mappings", 2, dir.closes);
+ assertEquals(1, dir.closes);
}
}
- public void testMergesFallBackToTheReaderMappingWhenTheFileIsGone() throws IOException {
+ public void testFallBackToTheReaderMappingWhenTheFileIsGone() throws IOException {
try (Directory base = newDirectory()) {
SegmentInfo si = writeCompound(base);
DataOpens dir = new DataOpens(base);
try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si)) {
- dir.gone = true;
String file = "_123.0";
- try (IndexInput merge = cfs.openInput(file, IOContext.merge());
- IndexInput expected = base.openInput(file, IOContext.DEFAULT)) {
- assertSameStreams(file, expected, merge);
+ try (IndexInput search = cfs.openInput(file, RANDOM)) {
+ dir.gone = true;
+ try (IndexInput sequential = cfs.openInput(file, SEQUENTIAL);
+ IndexInput expected = base.openInput(file, IOContext.DEFAULT)) {
+ assertSameStreams(file, expected, sequential);
+ }
+ try (IndexInput expected = base.openInput(file, IOContext.DEFAULT)) {
+ assertSameStreams(file, expected, search);
+ }
}
assertEquals(1, dir.opens.size());
}
@@ -159,21 +187,20 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
}
}
- /** Searches share the mapping opened with the reader; a merge reads one of its own. */
- public void testAMergeDoesNotReadTheMappingSearchesRead() throws IOException {
- try (MMapDirectory dir = new MMapDirectory(createTempDir("cfsMerge"))) {
+ /** On MMapDirectory, random and sequential reads of an inner file use different mappings. */
+ public void testEachAdviceReadsItsOwnMapping() throws IOException {
+ try (MMapDirectory dir = new MMapDirectory(createTempDir("cfsAdvice"))) {
dir.setReadAdvice(MMapDirectory.ADVISE_BY_CONTEXT);
- SegmentInfo si = writeCompound(dir);
+ // larger than a page, so that inner files get advised
+ SegmentInfo si = writeCompound(dir, 1 << 17);
try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
- IndexInput search = cfs.openInput("_123.0", IOContext.DEFAULT);
- IndexInput randomSearch =
- cfs.openInput("_123.0", IOContext.DEFAULT.withHints(DataAccessHint.RANDOM));
- IndexInput merge =
- cfs.openInput("_123.0", IOContext.merge().withHints(DataAccessHint.SEQUENTIAL))) {
- assertEquals(address(search), address(randomSearch));
- assertNotEquals(address(search), address(merge));
+ IndexInput search = cfs.openInput("_123.0", RANDOM);
+ IndexInput otherSearch = cfs.openInput("_123.0", RANDOM);
+ IndexInput sequential = cfs.openInput("_123.0", SEQUENTIAL)) {
+ assertEquals(address(search), address(otherSearch));
+ assertEquals(MMapDirectory.supportsMadvise(), address(search) != address(sequential));
try (IndexInput expected = dir.openInput("_123.0", IOContext.DEFAULT)) {
- assertSameStreams("_123.0", expected, merge);
+ assertSameStreams("_123.0", expected, sequential);
}
}
}
@@ -183,41 +210,47 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
return ((MemorySegmentAccessInput) in).segmentSliceOrNull(0, 1).address();
}
- public void testNoMergeMappingOnceClosed() throws IOException {
+ public void testNoOpenOnceClosed() throws IOException {
try (Directory base = newDirectory()) {
SegmentInfo si = writeCompound(base);
DataOpens dir = new DataOpens(base);
Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
cfs.close();
- expectThrows(AlreadyClosedException.class, () -> cfs.openInput("_123.0", IOContext.merge()));
+ expectThrows(AlreadyClosedException.class, () -> cfs.openInput("_123.0", SEQUENTIAL));
assertEquals(1, dir.opens.size());
assertEquals(1, dir.closes);
}
}
- public void testAFailedMergeOpenKeepsNothingOpen() throws IOException {
+ public void testAFailedOpenKeepsNothingOpen() throws IOException {
try (Directory base = newDirectory()) {
SegmentInfo si = writeCompound(base);
DataOpens dir = new DataOpens(base);
- try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si)) {
+ try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
+ IndexInput search = cfs.openInput("_123.0", RANDOM)) {
dir.failNextOpen = true;
- expectThrows(IOException.class, () -> cfs.openInput("_123.0", IOContext.merge()));
+ expectThrows(IOException.class, () -> cfs.openInput("_123.0", SEQUENTIAL));
assertEquals(1, dir.opens.size());
- try (IndexInput merge = cfs.openInput("_123.0", IOContext.merge());
+ try (IndexInput sequential = cfs.openInput("_123.0", SEQUENTIAL);
IndexInput expected = base.openInput("_123.0", IOContext.DEFAULT)) {
- assertSameStreams("_123.0", expected, merge);
+ assertSameStreams("_123.0", expected, sequential);
+ }
+ assertEquals("the next open maps the file", 2, dir.opens.size());
+ try (IndexInput expected = base.openInput("_123.0", IOContext.DEFAULT)) {
+ assertSameStreams("_123.0", expected, search);
}
- assertEquals("the next merge maps the file", 2, dir.opens.size());
}
assertEquals(2, dir.closes);
}
}
- public void testConcurrentMergeOpensMapOnce() throws Exception {
+ public void testConcurrentOpensMapOnce() throws Exception {
try (Directory base = newDirectory()) {
SegmentInfo si = writeCompound(base);
DataOpens dir = new DataOpens(base);
- try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si)) {
+ try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
+ IndexInput first = cfs.openInput("_123.0", RANDOM);
+ IndexInput second = cfs.openInput("_123.1", RANDOM)) {
int threadCount = 2 + random().nextInt(4);
CountDownLatch start = new CountDownLatch(1);
List<Thread> threads = new ArrayList<>();
@@ -229,7 +262,7 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
() -> {
try {
start.await();
- try (IndexInput in = cfs.openInput(file, IOContext.merge())) {
+ try (IndexInput in = cfs.openInput(file, SEQUENTIAL)) {
in.seek(in.length());
}
} catch (Throwable e) {
@@ -245,17 +278,28 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
}
assertEquals(List.of(), failures);
assertEquals(2, dir.opens.size());
+ for (IndexInput search : List.of(first, second)) {
+ search.seek(search.length());
+ }
}
assertEquals(2, dir.closes);
}
}
+ private static final IOContext RANDOM = IOContext.DEFAULT.withHints(DataAccessHint.RANDOM);
+ private static final IOContext SEQUENTIAL =
+ IOContext.DEFAULT.withHints(DataAccessHint.SEQUENTIAL);
+
private static SegmentInfo writeCompound(Directory dir) throws IOException {
+ return writeCompound(dir, random().nextInt(1, 4096));
+ }
+
+ private static SegmentInfo writeCompound(Directory dir, int length) throws IOException {
SegmentInfo si = newSegmentInfo(dir, "_123");
List<String> files = new ArrayList<>();
for (int i = 0; i < 2; i++) {
String name = "_123." + i;
- createRandomFile(dir, name, random().nextInt(1, 4096), si.getId());
+ createRandomFile(dir, name, length, si.getId());
files.add(name);
}
si.setFiles(files);
@@ -263,15 +307,26 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
return si;
}
- /** Records the contexts the compound data file is opened with, and counts closes. */
+ /**
+ * Records the contexts the compound data file is opened with and counts closes. Its slices are
+ * efficient for a context like memory-mapped inputs using {@link
+ * MMapDirectory#ADVISE_BY_CONTEXT}, or for every context.
+ */
private static class DataOpens extends FilterDirectory {
final List<IOContext> opens = Collections.synchronizedList(new ArrayList<>());
+ final boolean advises;
volatile int closes;
+ final Set<IOContext> advised = Collections.synchronizedSet(new HashSet<>());
volatile boolean gone;
volatile boolean failNextOpen;
DataOpens(Directory in) {
+ this(in, true);
+ }
+
+ DataOpens(Directory in, boolean advises) {
super(in);
+ this.advises = advises;
}
@Override
@@ -305,7 +360,65 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
public IndexInput slice(String description, long offset, long length) throws IOException {
return in.slice(description, offset, length);
}
+
+ @Override
+ public IndexInput slice(String description, long offset, long length, IOContext context)
+ throws IOException {
+ advised.add(context);
+ return new Advised(in.slice(description, offset, length, context), context, advises);
+ }
};
}
}
+
+ private static final class Advised extends FilterIndexInput {
+ private final IOContext context;
+ private final boolean advises;
+
+ Advised(IndexInput in, IOContext context, boolean advises) {
+ super("Advised(" + in + ")", in);
+ this.context = context;
+ this.advises = advises;
+ }
+
+ @Override
+ public boolean isEfficientFor(IOContext other) {
+ if (advises == false) {
+ return true;
+ }
+ ReadAdvice advice = MMapDirectory.ADVISE_BY_CONTEXT.apply("", context).orElseThrow();
+ ReadAdvice otherAdvice = MMapDirectory.ADVISE_BY_CONTEXT.apply("", other).orElseThrow();
+ return advice == ReadAdvice.NORMAL
+ || otherAdvice == ReadAdvice.NORMAL
+ || advice == otherAdvice;
+ }
+
+ @Override
+ public IndexInput clone() {
+ return new Advised(in.clone(), context, advises);
+ }
+
+ @Override
+ public IndexInput slice(String description, long offset, long length) throws IOException {
+ return new Advised(in.slice(description, offset, length), context, advises);
+ }
+ }
+
+ /** Closing an input of an inner file does not close the other inputs of that file. */
+ public void testClosingAnInputKeepsTheOthers() throws IOException {
+ try (Directory dir = new MMapDirectory(createTempDir(), FSLockFactory.getDefault(), 1 << 10)) {
+ SegmentInfo si = newSegmentInfo(dir, "_123");
+ createSequenceFile(dir, "_123.d1", (byte) 0, 10_000, si.getId(), "suffix");
+ si.setFiles(List.of("_123.d1"));
+ si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT);
+ try (Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
+ IndexInput expected = dir.openInput("_123.d1", IOContext.DEFAULT)) {
+ IOContext context = IOContext.DEFAULT.withHints(DataAccessHint.RANDOM);
+ cfs.openInput("_123.d1", context).close();
+ try (IndexInput actual = cfs.openInput("_123.d1", context)) {
+ assertSameStreams("_123.d1", expected, actual);
+ }
+ }
+ }
+ }
}
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestVectorsMergeReadAdvice.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestVectorsMergeReadAdvice.java
index 5e1b737d1d2..138f2751e02 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestVectorsMergeReadAdvice.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestVectorsMergeReadAdvice.java
@@ -17,9 +17,9 @@
package org.apache.lucene.codecs.lucene99;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.KnnVectorsReader;
@@ -36,489 +36,264 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentReader;
import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.FileDataHint;
-import org.apache.lucene.store.FileTypeHint;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.store.NoReuseHint;
+import org.apache.lucene.store.ReadAdvice;
import org.apache.lucene.store.ReadOnceHint;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
/**
- * A merge reads the raw vectors front to back while a graph search reads them at random, and read
- * advice applies to a whole mapping, so a merge opens the data file for itself.
+ * Raw vectors read at random get a second input for sequential reads, shared by merges and
+ * sequential values and closed with the reader.
*/
public class TestVectorsMergeReadAdvice extends LuceneTestCase {
private static final int DIM = 8;
- public void testMergeOpensItsOwnVectors() throws Exception {
- assertMergeOpensItsOwnVectors(new Lucene99HnswVectorsFormat(), false);
+ private static List<KnnVectorsFormat> formats() {
+ return List.of(
+ new Lucene99HnswVectorsFormat(), new Lucene104HnswScalarQuantizedVectorsFormat());
}
- public void testQuantizedMergeOpensItsOwnVectors() throws Exception {
- assertMergeOpensItsOwnVectors(new Lucene104HnswScalarQuantizedVectorsFormat(), true);
- }
-
- /**
- * @param noReuse whether the format opens its raw vectors with {@link NoReuseHint}
- */
- private static void assertMergeOpensItsOwnVectors(KnnVectorsFormat format, boolean noReuse)
- throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(format));
- iwc.setUseCompoundFile(false); // so the directory sees the data file by name
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < 2; segment++) {
- for (int i = 0; i < 64; i++) {
- Document doc = new Document();
- doc.add(
- new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
-
- // searches open the segments first, so the merge finds readers that are already open
+ public void testMergeOpensTheVectorsAgain() throws Exception {
+ for (KnnVectorsFormat format : formats()) {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ List<Open> sequential;
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ IndexWriter w = writer(dir, format, false)) {
+ addSegments(w, 2);
+ // open the segments first, so the merge reuses readers that read at random
try (DirectoryReader reader = DirectoryReader.open(w)) {
assertEquals(2, reader.leaves().size());
opens.clear();
w.forceMerge(1);
-
- // the merge is over, so what it opened is gone even though the segments are still open
- List<Open> sequential = sequentialOpens(opens);
- assertFalse(
- "the merge never opened the vectors for itself: " + opens, sequential.isEmpty());
+ sequential = sequentialOpens(opens);
+ assertEquals(opens.toString(), 2, sequential.size());
for (Open open : sequential) {
- assertTrue("the merge did not read the vectors it opened: " + open, open.read());
- assertTrue("the merge kept its vectors open: " + open, open.closed());
+ assertTrue(open.toString(), open.read);
assertEquals(
- "the merge mapping keeps what the format said: " + open,
- noReuse,
- open.hints().contains(NoReuseHint.INSTANCE));
+ "keeps the format's hints: " + open,
+ format instanceof Lucene104HnswScalarQuantizedVectorsFormat,
+ open.context.hints().contains(NoReuseHint.INSTANCE));
}
}
}
-
- assertEquals(
- "the merge re-advised the vectors searches are reading: " + opens,
- List.of(),
- opens.advised());
+ for (Open open : sequential) {
+ assertTrue("closed with the reader: " + open, open.closed);
+ }
}
}
- /**
- * In a compound segment, a merge reads the vectors through a mapping of the compound file of its
- * own, opened without advice and closed with the segment's reader.
- */
- public void testACompoundMergeOpensItsOwnMapping() throws Exception {
- KnnVectorsFormat format =
- random().nextBoolean()
- ? new Lucene99HnswVectorsFormat()
- : new Lucene104HnswScalarQuantizedVectorsFormat();
- Opens opens = new Opens();
- List<Open> mergeOpens;
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(format));
- iwc.setUseCompoundFile(true);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < 2; segment++) {
- for (int i = 0; i < 64; i++) {
- Document doc = new Document();
- doc.add(
- new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
-
- try (DirectoryReader reader = DirectoryReader.open(w)) {
- List<String> compoundFiles = new ArrayList<>();
- for (var leaf : reader.leaves()) {
- SegmentReader segmentReader = (SegmentReader) leaf.reader();
- assertTrue(segmentReader.getSegmentInfo().info.getUseCompoundFile());
- compoundFiles.add(segmentReader.getSegmentName() + ".cfs");
- }
- compoundFiles.sort(null);
- opens.clear();
- w.forceMerge(1);
-
- // the segments were open before the merge, so opening their compound files again is the
- // merge
- mergeOpens = opens.all().stream().filter(o -> compoundFiles.contains(o.name())).toList();
- assertEquals(
- "the merge mapped each compound file once: " + opens,
- compoundFiles,
- mergeOpens.stream().map(Open::name).sorted().toList());
- for (Open open : mergeOpens) {
- assertTrue("the merge did not read its mapping: " + open, open.read());
- assertEquals("opened without advice: " + open, Set.of(), open.hints());
- }
+ /** In a compound segment, a merge opens the compound file again, without advice. */
+ public void testCompoundMergeOpensTheCompoundFileAgain() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ List<Open> cfsOpens;
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ IndexWriter w = writer(dir, new Lucene99HnswVectorsFormat(), true)) {
+ addSegments(w, 2);
+ try (DirectoryReader reader = DirectoryReader.open(w)) {
+ assertEquals(2, reader.leaves().size());
+ opens.clear();
+ w.forceMerge(1);
+ cfsOpens = opens.stream().filter(o -> o.name.endsWith(".cfs")).toList();
+ assertEquals(opens.toString(), 2, cfsOpens.size());
+ for (Open open : cfsOpens) {
+ assertTrue(open.toString(), open.read);
+ assertEquals(open.toString(), Set.of(), open.context.hints());
}
}
}
- for (Open open : mergeOpens) {
- assertTrue("closed with the segment's reader: " + open, open.closed());
+ for (Open open : cfsOpens) {
+ assertTrue("closed with the reader: " + open, open.closed);
}
}
- /** A reader a merge opened already reads the vectors the way a merge does. */
- public void testAMergeReadsThroughTheReaderItOpened() throws Exception {
- Opens opens = new Opens();
+ /** A reader opened by a merge already reads sequentially. */
+ public void testMergeReadsThroughTheReaderItOpened() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < 2; segment++) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(
- new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- // no reader is open, so the merge opens the segments itself, with a merge context
+ try (IndexWriter w = writer(dir, new Lucene99HnswVectorsFormat(), false)) {
+ addSegments(w, 2);
opens.clear();
w.forceMerge(1);
}
- assertEquals(
- "the merge mapped vectors its own readers already map: " + opens,
- List.of(),
- sequentialOpens(opens));
- assertEquals("the merge re-advised the vectors: " + opens, List.of(), opens.advised());
+ assertEquals(opens.toString(), List.of(), sequentialOpens(opens));
}
}
- /** Searches that do not read the vectors at random leave nothing for a merge to undo. */
- public void testNoMappingWhenSearchesDoNotReadAtRandom() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- SegmentReader segment = (SegmentReader) getOnlyLeafReader(reader);
- try (FlatVectorsReader flat = flatReader(dir, segment, IOContext.DEFAULT)) {
- opens.clear();
- assertSame(flat, flat.getMergeInstance());
- flat.finishMerge();
- assertEquals("nothing to map again: " + opens, List.of(), opens.all());
- }
- }
- }
+ /** Nothing is opened again when the input already serves sequential reads. */
+ public void testNoSecondInputWhenNotNeeded() throws Exception {
+ assertNoSecondInput(true, IOContext.DEFAULT.withHints(DataAccessHint.SEQUENTIAL));
+ assertNoSecondInput(
+ false, IOContext.DEFAULT.withHints(DataAccessHint.RANDOM, NoReuseHint.INSTANCE));
}
- /** The merge mapping changes how the file is read and keeps everything else the caller said. */
- public void testTheMergeMappingKeepsWhatTheCallerSaid() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- SegmentReader segment = (SegmentReader) getOnlyLeafReader(reader);
- IOContext searchContext =
- IOContext.DEFAULT.withHints(
- DataAccessHint.RANDOM, NoReuseHint.INSTANCE, CallerHint.INSTANCE);
- try (FlatVectorsReader flat = flatReader(dir, segment, searchContext)) {
- opens.clear();
- FlatVectorsReader mergeInstance = flat.getMergeInstance();
- List<Open> mapped = sequentialOpens(opens);
- assertEquals("one mapping for the merge: " + opens, 1, mapped.size());
- Set<IOContext.FileOpenHint> hints = mapped.get(0).hints();
- assertTrue("the caller's hint is kept: " + hints, hints.contains(CallerHint.INSTANCE));
- assertTrue(hints.contains(NoReuseHint.INSTANCE));
- assertTrue(hints.contains(FileTypeHint.DATA));
- assertTrue(hints.contains(FileDataHint.KNN_VECTORS));
- assertFalse(
- "searches read at random, not the merge: " + hints,
- hints.contains(DataAccessHint.RANDOM));
- mergeInstance.finishMerge();
- }
- }
+ private static void assertNoSecondInput(boolean advises, IOContext context) throws Exception {
+ RecordingDirectory dir =
+ new RecordingDirectory(newDirectory(), new CopyOnWriteArrayList<>(), advises);
+ try (dir;
+ DirectoryReader reader = openReader(dir, new Lucene99HnswVectorsFormat());
+ FlatVectorsReader flat = flatReader(dir, reader, context)) {
+ dir.opens.clear();
+ assertSame(flat, flat.getMergeInstance());
+ assertEquals(dir.opens.toString(), List.of(), dir.opens);
}
}
- /** A hint only the caller knows about. */
- private enum CallerHint implements IOContext.FileOpenHint {
- INSTANCE
- }
-
- /**
- * A flat vectors reader over the only vectors file of {@code segment}, opened with {@code
- * context}.
- */
- private static FlatVectorsReader flatReader(
- Directory dir, SegmentReader segment, IOContext context) throws IOException {
- String vectorData = null;
- for (String file : segment.getSegmentInfo().files()) {
- if (file.endsWith("." + Lucene99FlatVectorsFormat.VECTOR_DATA_EXTENSION)) {
- vectorData = file;
- }
+ /** The sequential input keeps the caller's hints and only changes the access. */
+ public void testSequentialInputKeepsTheCallerHints() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ IOContext context =
+ IOContext.DEFAULT.withHints(
+ DataAccessHint.RANDOM, NoReuseHint.INSTANCE, CallerHint.INSTANCE);
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ DirectoryReader reader = openReader(dir, new Lucene99HnswVectorsFormat());
+ FlatVectorsReader flat = flatReader(dir, reader, context)) {
+ opens.clear();
+ assertNotSame(flat, flat.getMergeInstance());
+ List<Open> sequential = sequentialOpens(opens);
+ assertEquals(opens.toString(), 1, sequential.size());
+ Set<IOContext.FileOpenHint> hints = sequential.get(0).context.hints();
+ assertTrue(hints.toString(), hints.contains(CallerHint.INSTANCE));
+ assertTrue(hints.toString(), hints.contains(NoReuseHint.INSTANCE));
+ assertFalse(hints.toString(), hints.contains(DataAccessHint.RANDOM));
}
- assertNotNull(vectorData);
- String segmentName = segment.getSegmentInfo().info.name;
- // the per-field suffix, between the segment name and the extension
- String suffix = vectorData.substring(segmentName.length() + 1, vectorData.lastIndexOf('.'));
- SegmentReadState state =
- new SegmentReadState(
- dir, segment.getSegmentInfo().info, segment.getFieldInfos(), context, suffix);
- return new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer())
- .fieldsReader(state);
}
- public void testTheMappingIsSharedThenReleasedByTheLastMergeInstance() throws Exception {
- assertMappingIsSharedThenReleasedByTheLastMergeInstance(new Lucene99HnswVectorsFormat());
- }
-
- public void testQuantizedMappingIsSharedThenReleasedByTheLastMergeInstance() throws Exception {
- assertMappingIsSharedThenReleasedByTheLastMergeInstance(
- new Lucene104HnswScalarQuantizedVectorsFormat());
+ private enum CallerHint implements IOContext.FileOpenHint {
+ INSTANCE
}
- private static void assertMappingIsSharedThenReleasedByTheLastMergeInstance(
- KnnVectorsFormat format) throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(format));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- KnnVectorsReader vectors =
- ((CodecReader) getOnlyLeafReader(reader))
- .getVectorReader()
- .unwrapReaderForField("field");
+ /** Merges and sequential values share one input, kept until the reader closes. */
+ public void testOneSequentialInputUntilTheReaderCloses() throws Exception {
+ for (KnnVectorsFormat format : formats()) {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ List<Open> sequential;
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ DirectoryReader reader = openReader(dir, format)) {
+ KnnVectorsReader vectors = ((CodecReader) getOnlyLeafReader(reader)).getVectorReader();
opens.clear();
- KnnVectorsReader first = vectors.getMergeInstance();
- List<Open> mapped = sequentialOpens(opens);
- assertEquals("one mapping for the first merge instance: " + opens, 1, mapped.size());
-
- KnnVectorsReader second = vectors.getMergeInstance();
- assertEquals(
- "a second merge instance shares it: " + opens, 1, sequentialOpens(opens).size());
+ FloatVectorValues random = vectors.getFloatVectorValues("field", DataAccessHint.RANDOM);
+ assertEquals(opens.toString(), List.of(), sequentialOpens(opens));
- first.finishMerge();
- assertFalse("still held by the second instance: " + opens, mapped.get(0).closed());
-
- second.finishMerge();
- assertTrue("released by the last instance: " + opens, mapped.get(0).closed());
-
- KnnVectorsReader third = vectors.getMergeInstance();
- assertEquals("a later merge maps it again: " + opens, 2, sequentialOpens(opens).size());
- third.finishMerge();
- }
- }
- }
-
- /**
- * Fields sharing a reader each ask it for a merge instance, so one segment has several merge
- * instances at once. They share one mapping, each through a clone of its own.
- */
- public void testFieldsSharingAReaderShareOneMapping() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- int segments = 3;
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < segments; segment++) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("a", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- doc.add(new KnnFloatVectorField("b", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- doc.add(new KnnFloatVectorField("c", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
+ FloatVectorValues values = vectors.getFloatVectorValues("field", DataAccessHint.SEQUENTIAL);
+ sequential = sequentialOpens(opens);
+ assertEquals(opens.toString(), 1, sequential.size());
+ assertEquals(random.size(), values.size());
+ for (int ord = 0; ord < random.size(); ord++) {
+ assertArrayEquals(random.vectorValue(ord), values.vectorValue(ord), 0f);
}
- try (DirectoryReader reader = DirectoryReader.open(w)) {
- assertEquals(segments, reader.leaves().size());
- opens.clear();
- w.forceMerge(1);
- List<Open> sequential = sequentialOpens(opens);
- assertEquals("one mapping per merged segment: " + opens, segments, sequential.size());
- for (Open open : sequential) {
- assertTrue("released once every field was done: " + open, open.closed());
- }
- }
+ KnnVectorsReader field = vectors.unwrapReaderForField("field");
+ field.getMergeInstance().finishMerge();
+ assertEquals(DIM, field.getMergeInstance().getFloatVectorValues("field").dimension());
+ assertEquals(opens.toString(), 1, sequentialOpens(opens).size());
+ assertFalse("kept while the reader is open", sequential.get(0).closed);
}
+ assertTrue("closed with the reader", sequential.get(0).closed);
}
}
- /**
- * A merge instance gives the mapping back once. Finishing it again, or finishing the reader it
- * came from, must not release the mapping another merge instance still reads.
- */
- public void testOnlyAMergeInstanceReleasesTheMappingAndOnlyOnce() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- List<Open> mapped;
- KnnVectorsReader third;
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- KnnVectorsReader vectors =
- ((CodecReader) getOnlyLeafReader(reader))
- .getVectorReader()
- .unwrapReaderForField("field");
- opens.clear();
-
- KnnVectorsReader first = vectors.getMergeInstance();
- KnnVectorsReader second = vectors.getMergeInstance();
- mapped = sequentialOpens(opens);
- assertEquals("one mapping for both merge instances: " + opens, 1, mapped.size());
-
- first.finishMerge();
- first.finishMerge();
- assertFalse("finishing twice released another's hold: " + opens, mapped.get(0).closed());
-
- vectors.finishMerge();
- assertFalse("the reader itself holds nothing to release: " + opens, mapped.get(0).closed());
-
- // the second instance still reads through the mapping
- FloatVectorValues values = second.getFloatVectorValues("field");
- assertEquals(DIM, values.vectorValue(0).length);
-
- second.finishMerge();
- assertTrue("released by the last instance: " + opens, mapped.get(0).closed());
-
- // a merge instance left unfinished when the reader closes
- third = vectors.getMergeInstance();
- mapped = sequentialOpens(opens);
- assertEquals(2, mapped.size());
- }
- assertTrue("closed with the reader: " + opens, mapped.get(1).closed());
- // and finishing it afterwards does not close it a second time
- third.finishMerge();
- }
- }
-
- /** The mapping goes with the reader, even if no merge ever says it is finished. */
- public void testTheMappingIsClosedWithTheReader() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- List<Open> mapped;
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- KnnVectorsReader vectors =
- ((CodecReader) getOnlyLeafReader(reader))
- .getVectorReader()
- .unwrapReaderForField("field");
- opens.clear();
-
- // a merge instance nobody finishes, as an abandoned merge leaves behind
- assertNotNull(vectors.getMergeInstance());
- mapped = sequentialOpens(opens);
- assertEquals("one mapping for the merge instance: " + opens, 1, mapped.size());
- assertFalse("still held by the merge instance: " + opens, mapped.get(0).closed());
- }
- assertTrue("closed with the reader: " + mapped.get(0), mapped.get(0).closed());
+ public void testNoSecondInputOnceClosed() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ DirectoryReader reader = openReader(dir, new Lucene99HnswVectorsFormat())) {
+ FlatVectorsReader flat =
+ flatReader(
+ dir,
+ reader,
+ IOContext.DEFAULT.withHints(DataAccessHint.RANDOM, NoReuseHint.INSTANCE));
+ flat.close();
+ opens.clear();
+ expectThrows(AlreadyClosedException.class, flat::getMergeInstance);
+ assertEquals(opens.toString(), List.of(), sequentialOpens(opens));
}
}
- /** A merge open that fails leaves nothing behind, so a later merge still releases the mapping. */
- public void testAFailedMergeOpenIsNotCountedAsAMergeInstance() throws Exception {
- Opens opens = new Opens();
- AtomicBoolean failMergeOpens = new AtomicBoolean();
- try (Directory dir =
+ public void testFailedOpenIsRetried() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ AtomicBoolean fail = new AtomicBoolean();
+ Directory dir =
new RecordingDirectory(newDirectory(), opens) {
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
- if (failMergeOpens.get()
+ if (fail.get()
&& name.endsWith(".vec")
- && context.context() == IOContext.Context.MERGE) {
+ && context.hints().contains(DataAccessHint.SEQUENTIAL)) {
throw new IOException("injected");
}
return super.openInput(name, context);
}
- }) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(new Lucene99HnswVectorsFormat()));
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int i = 0; i < 16; i++) {
- Document doc = new Document();
- doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
- w.addDocument(doc);
- }
- w.commit();
- }
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- KnnVectorsReader vectors =
- ((CodecReader) getOnlyLeafReader(reader))
- .getVectorReader()
- .unwrapReaderForField("field");
- opens.clear();
-
- failMergeOpens.set(true);
- expectThrows(IOException.class, vectors::getMergeInstance);
- assertEquals("a failed open maps nothing: " + opens, List.of(), sequentialOpens(opens));
+ };
+ try (dir;
+ DirectoryReader reader = openReader(dir, new Lucene99HnswVectorsFormat())) {
+ KnnVectorsReader vectors =
+ ((CodecReader) getOnlyLeafReader(reader)).getVectorReader().unwrapReaderForField("field");
+ opens.clear();
+ fail.set(true);
+ expectThrows(IOException.class, vectors::getMergeInstance);
+ assertEquals(opens.toString(), List.of(), sequentialOpens(opens));
+ fail.set(false);
+ vectors.getMergeInstance();
+ assertEquals(opens.toString(), 1, sequentialOpens(opens).size());
+ }
+ assertTrue("closed with the reader", opens.getLast().closed);
+ }
- failMergeOpens.set(false);
- KnnVectorsReader merging = vectors.getMergeInstance();
- List<Open> mapped = sequentialOpens(opens);
- assertEquals("one mapping for the one merge instance: " + opens, 1, mapped.size());
+ private static IndexWriter writer(Directory dir, KnnVectorsFormat format, boolean compound)
+ throws IOException {
+ IndexWriterConfig iwc = new IndexWriterConfig();
+ iwc.setCodec(TestUtil.alwaysKnnVectorsFormat(format));
+ iwc.setUseCompoundFile(compound);
+ return new IndexWriter(dir, iwc);
+ }
- merging.finishMerge();
- assertTrue("released by the only merge instance: " + opens, mapped.get(0).closed());
+ private static void addSegments(IndexWriter w, int segments) throws IOException {
+ for (int segment = 0; segment < segments; segment++) {
+ for (int i = 0; i < 16; i++) {
+ Document doc = new Document();
+ doc.add(new KnnFloatVectorField("field", vector(), VectorSimilarityFunction.DOT_PRODUCT));
+ w.addDocument(doc);
}
+ w.commit();
+ }
+ }
+
+ /** Opens a reader over a single segment of vectors. */
+ private static DirectoryReader openReader(Directory dir, KnnVectorsFormat format)
+ throws IOException {
+ try (IndexWriter w = writer(dir, format, false)) {
+ addSegments(w, 1);
}
+ return DirectoryReader.open(dir);
+ }
+
+ /** A flat vectors reader over the vectors of the only segment, opened with {@code context}. */
+ private static FlatVectorsReader flatReader(
+ Directory dir, DirectoryReader reader, IOContext context) throws IOException {
+ SegmentReader segment = (SegmentReader) getOnlyLeafReader(reader);
+ String segmentName = segment.getSegmentInfo().info.name;
+ String vectorData =
+ segment.getSegmentInfo().files().stream()
+ .filter(f -> f.endsWith("." + Lucene99FlatVectorsFormat.VECTOR_DATA_EXTENSION))
+ .findFirst()
+ .orElseThrow();
+ // the per-field suffix, between the segment name and the extension
+ String suffix = vectorData.substring(segmentName.length() + 1, vectorData.lastIndexOf('.'));
+ SegmentReadState state =
+ new SegmentReadState(
+ dir, segment.getSegmentInfo().info, segment.getFieldInfos(), context, suffix);
+ return new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer())
+ .fieldsReader(state);
}
private static float[] vector() {
@@ -529,132 +304,84 @@ public class TestVectorsMergeReadAdvice extends LuceneTestCase {
return v;
}
- /** The opens a merge made for itself: the vector data, read front to back. */
- private static List<Open> sequentialOpens(Opens opens) {
- List<Open> sequential = new ArrayList<>();
- for (Open open : opens.all()) {
- // integrity checks read a file once, front to back, and say so with READONCE
- if (open.name().endsWith(".vec")
- && open.hint() == DataAccessHint.SEQUENTIAL
- && open.hints().contains(ReadOnceHint.INSTANCE) == false) {
- assertSame(
- "the open says a merge is reading, so a directory can route it: " + open,
- IOContext.Context.MERGE,
- open.context());
- sequential.add(open);
- }
- }
- return sequential;
+ /** The opens of the vector data for sequential reads, other than integrity checks. */
+ private static List<Open> sequentialOpens(List<Open> opens) {
+ return opens.stream()
+ .filter(o -> o.name.endsWith(".vec"))
+ .filter(o -> o.context.hints().contains(DataAccessHint.SEQUENTIAL))
+ .filter(o -> o.context.hints().contains(ReadOnceHint.INSTANCE) == false)
+ .toList();
}
/** One {@link Directory#openInput} call. */
private static final class Open {
- private final String name;
- private final IOContext context;
- private volatile boolean closed;
- private volatile boolean read;
+ final String name;
+ final IOContext context;
+ volatile boolean closed;
+ // whether the input was sliced, which is how vectors are read
+ volatile boolean read;
Open(String name, IOContext context) {
this.name = name;
this.context = context;
}
- String name() {
- return name;
- }
-
- Set<IOContext.FileOpenHint> hints() {
- return context.hints();
- }
-
- DataAccessHint hint() {
- return context.hints(DataAccessHint.class).findFirst().orElse(null);
- }
-
- IOContext.Context context() {
- return context.context();
- }
-
- boolean closed() {
- return closed;
- }
-
- /** Whether the input was sliced, which is how the vectors are read. */
- boolean read() {
- return read;
- }
-
@Override
public String toString() {
- return name + " [hint=" + hint() + " closed=" + closed + " read=" + read + "]";
+ return name + context.hints() + (closed ? " closed" : "") + (read ? " read" : "");
}
}
- private static final class Opens {
- private final List<Open> opens = new ArrayList<>();
- private final List<String> advised = new ArrayList<>();
-
- synchronized Open record(String name, IOContext context) {
- Open open = new Open(name, context);
- opens.add(open);
- return open;
- }
-
- /** Files whose advice was changed after they were opened. */
- synchronized void recordAdvice(String name) {
- advised.add(name);
- }
-
- synchronized List<Open> all() {
- return List.copyOf(opens);
- }
-
- synchronized List<String> advised() {
- return List.copyOf(advised);
- }
-
- synchronized void clear() {
- opens.clear();
- advised.clear();
- }
+ /**
+ * Records opens. Its inputs are efficient for a context like memory-mapped inputs using {@link
+ * MMapDirectory#ADVISE_BY_CONTEXT}, or for every context.
+ */
+ private static class RecordingDirectory extends FilterDirectory {
+ final List<Open> opens;
+ private final boolean advises;
- @Override
- public synchronized String toString() {
- return "opens: " + opens + ", advised: " + advised;
+ RecordingDirectory(Directory in, List<Open> opens) {
+ this(in, opens, true);
}
- }
- private static class RecordingDirectory extends FilterDirectory {
- private final Opens opens;
-
- RecordingDirectory(Directory in, Opens opens) {
+ RecordingDirectory(Directory in, List<Open> opens, boolean advises) {
super(in);
this.opens = opens;
+ this.advises = advises;
}
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
- Open open = opens.record(name, context);
- return new RecordingIndexInput(super.openInput(name, context), name, open, opens);
+ Open open = new Open(name, context);
+ opens.add(open);
+ return new RecordingIndexInput(super.openInput(name, context), open, context, advises);
}
}
private static final class RecordingIndexInput extends FilterIndexInput {
- private final String name;
private final Open open;
- private final Opens opens;
+ // the context the pages of this input were advised with
+ private final IOContext context;
+ private final boolean advises;
- RecordingIndexInput(IndexInput in, String name, Open open, Opens opens) {
- super("Recording(" + name + ")", in);
- this.name = name;
+ RecordingIndexInput(IndexInput in, Open open, IOContext context, boolean advises) {
+ super("Recording(" + open.name + ")", in);
this.open = open;
- this.opens = opens;
+ this.context = context;
+ this.advises = advises;
}
@Override
- public void updateIOContext(IOContext context) throws IOException {
- opens.recordAdvice(name);
- in.updateIOContext(context);
+ public boolean isEfficientFor(IOContext other) {
+ if (advises == false) {
+ return true;
+ }
+ ReadAdvice advice = MMapDirectory.ADVISE_BY_CONTEXT.apply(open.name, context).orElseThrow();
+ ReadAdvice otherAdvice =
+ MMapDirectory.ADVISE_BY_CONTEXT.apply(open.name, other).orElseThrow();
+ return advice == ReadAdvice.NORMAL
+ || otherAdvice == ReadAdvice.NORMAL
+ || advice == otherAdvice;
}
@Override
@@ -665,13 +392,22 @@ public class TestVectorsMergeReadAdvice extends LuceneTestCase {
@Override
public IndexInput clone() {
- return new RecordingIndexInput(in.clone(), name, open, opens);
+ return new RecordingIndexInput(in.clone(), open, context, advises);
}
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
open.read = true;
- return new RecordingIndexInput(in.slice(sliceDescription, offset, length), name, open, opens);
+ return new RecordingIndexInput(
+ in.slice(sliceDescription, offset, length), open, context, advises);
+ }
+
+ @Override
+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)
+ throws IOException {
+ open.read = true;
+ return new RecordingIndexInput(
+ in.slice(sliceDescription, offset, length, context), open, context, advises);
}
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsMergeReadAdvice.java b/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsMergeReadAdvice.java
index 2b673f0e30e..13342e5abd6 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsMergeReadAdvice.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsMergeReadAdvice.java
@@ -19,7 +19,7 @@ package org.apache.lucene.index;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.lucene.codecs.StoredFieldsReader;
import org.apache.lucene.document.Document;
@@ -31,82 +31,54 @@ import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.store.NoReuseHint;
+import org.apache.lucene.store.ReadAdvice;
import org.apache.lucene.store.ReadOnceHint;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
/**
- * A merge reads stored fields front to back, while searches read them at random, and read advice
- * applies to a whole mapping. So a merge opens the data file for itself instead of re-advising the
- * one searches are reading.
+ * Stored fields read at random get a second input for sequential reads, shared by merges and closed
+ * with the reader.
*/
public class TestStoredFieldsMergeReadAdvice extends LuceneTestCase {
- public void testMergeOpensItsOwnStoredFields() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.getDefaultCodec()); // the merge advice is the default format's
- iwc.setUseCompoundFile(false); // so the directory sees the data file by name
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < 2; segment++) {
- for (int i = 0; i < 64; i++) {
- Document doc = new Document();
- doc.add(new StoredField("field", "value " + i));
- w.addDocument(doc);
- }
- w.commit();
- }
-
- // searches open the segments first, so the merge finds readers advised for random access
- try (DirectoryReader reader = DirectoryReader.open(w)) {
- assertEquals(2, reader.leaves().size());
- opens.clear();
- w.forceMerge(1);
-
- // the merge is over, so what it opened is gone even though the segments are still open
- List<Open> sequential = sequentialOpens(opens);
- assertFalse(
- "the merge never opened stored fields for itself: " + opens, sequential.isEmpty());
- for (Open open : sequential) {
- assertTrue("the merge kept its stored fields open: " + open, open.closed());
- }
- }
+ public void testMergeOpensTheStoredFieldsAgain() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ List<Open> sequential;
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ IndexWriter w = writer(dir)) {
+ addSegments(w, 2);
+ // open the segments first, so the merge reuses readers that read at random
+ try (DirectoryReader reader = DirectoryReader.open(w)) {
+ assertEquals(2, reader.leaves().size());
+ opens.clear();
+ w.forceMerge(1);
+ sequential = sequentialOpens(opens);
+ assertEquals(opens.toString(), 2, sequential.size());
}
-
- assertEquals(
- "the merge re-advised the stored fields searches are reading: " + opens,
- List.of(),
- opens.advised());
+ }
+ for (Open open : sequential) {
+ assertTrue("closed with the reader: " + open, open.closed);
}
}
- /** The data file is written front to back and, as it is read, not reused. */
public void testDataIsWrittenSequentiallyAndNotReused() throws Exception {
List<IOContext> dataWrites = new ArrayList<>();
try (Directory dir =
- new FilterDirectory(newDirectory()) {
- @Override
- public IndexOutput createOutput(String name, IOContext context) throws IOException {
- if (name.endsWith(".fdt")) {
- dataWrites.add(context);
- }
- return super.createOutput(name, context);
- }
- }) {
- IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.getDefaultCodec());
- iwc.setUseCompoundFile(false);
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
- for (int segment = 0; segment < 2; segment++) {
- Document doc = new Document();
- doc.add(new StoredField("field", "value " + segment));
- w.addDocument(doc);
- w.commit();
- }
- w.forceMerge(1);
- }
+ new FilterDirectory(newDirectory()) {
+ @Override
+ public IndexOutput createOutput(String name, IOContext context) throws IOException {
+ if (name.endsWith(".fdt")) {
+ dataWrites.add(context);
+ }
+ return super.createOutput(name, context);
+ }
+ };
+ IndexWriter w = writer(dir)) {
+ addSegments(w, 2);
+ w.forceMerge(1);
}
assertEquals("two flushes and a merge: " + dataWrites, 3, dataWrites.size());
for (IOContext context : dataWrites) {
@@ -115,93 +87,73 @@ public class TestStoredFieldsMergeReadAdvice extends LuceneTestCase {
}
}
- /** Nothing depends on the hook: finishing twice, or never, still leaves the mapping sound. */
- public void testTheHookIsAHint() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- writeDocuments(dir);
- List<Open> mapped;
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
- opens.clear();
-
- StoredFieldsReader first = fields.getMergeInstance();
- StoredFieldsReader second = fields.getMergeInstance();
- mapped = sequentialOpens(opens);
- assertEquals("one mapping for both merge instances: " + opens, 1, mapped.size());
-
- // twice from one instance, and once from a reader that is not a merge instance
- first.finishMerge();
- first.finishMerge();
- fields.finishMerge();
- assertFalse("still held by the second instance: " + opens, mapped.get(0).closed());
-
- second.finishMerge();
- assertTrue("released by the last instance: " + opens, mapped.get(0).closed());
- }
+ /** Merges share one input, kept until the reader closes. */
+ public void testOneSequentialInputUntilTheReaderCloses() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ List<Open> sequential;
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens);
+ DirectoryReader reader = openReader(dir)) {
+ StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
+ opens.clear();
+ fields.getMergeInstance().finishMerge();
+ fields.getMergeInstance();
+ sequential = sequentialOpens(opens);
+ assertEquals(opens.toString(), 1, sequential.size());
+ assertFalse("kept while the reader is open", sequential.get(0).closed);
}
+ assertTrue("closed with the reader", sequential.get(0).closed);
}
- /** A merge instance nobody finishes, the way an abandoned merge leaves one behind. */
- public void testTheMappingIsClosedWithTheReader() throws Exception {
- Opens opens = new Opens();
- try (Directory dir = new RecordingDirectory(newDirectory(), opens)) {
- writeDocuments(dir);
- List<Open> mapped;
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
- opens.clear();
-
- assertNotNull(fields.getMergeInstance());
- mapped = sequentialOpens(opens);
- assertEquals("one mapping for the merge instance: " + opens, 1, mapped.size());
- assertFalse("still held by the merge instance: " + opens, mapped.get(0).closed());
- }
- assertTrue("closed with the reader: " + mapped.get(0), mapped.get(0).closed());
+ /** Nothing is opened again when the input already serves sequential reads. */
+ public void testNoSecondInputWhenNotNeeded() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ try (Directory dir = new RecordingDirectory(newDirectory(), opens, false);
+ DirectoryReader reader = openReader(dir)) {
+ StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
+ opens.clear();
+ fields.getMergeInstance();
+ assertEquals(opens.toString(), List.of(), opens);
}
}
- /** A merge open that fails leaves nothing behind, so a later merge still releases the mapping. */
- public void testAFailedMergeOpenIsNotCountedAsAMergeInstance() throws Exception {
- Opens opens = new Opens();
- AtomicBoolean failMergeOpens = new AtomicBoolean();
- try (Directory dir =
+ public void testFailedOpenIsRetried() throws Exception {
+ List<Open> opens = new CopyOnWriteArrayList<>();
+ AtomicBoolean fail = new AtomicBoolean();
+ Directory dir =
new RecordingDirectory(newDirectory(), opens) {
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
- if (failMergeOpens.get()
+ if (fail.get()
&& name.endsWith(".fdt")
- && context.context() == IOContext.Context.MERGE) {
+ && context.hints().contains(DataAccessHint.SEQUENTIAL)) {
throw new IOException("injected");
}
return super.openInput(name, context);
}
- }) {
- writeDocuments(dir);
- try (DirectoryReader reader = DirectoryReader.open(dir)) {
- StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
- opens.clear();
-
- failMergeOpens.set(true);
- expectThrows(IOException.class, fields::getMergeInstance);
- assertEquals("a failed open maps nothing: " + opens, List.of(), sequentialOpens(opens));
-
- failMergeOpens.set(false);
- StoredFieldsReader merging = fields.getMergeInstance();
- List<Open> mapped = sequentialOpens(opens);
- assertEquals("one mapping for the one merge instance: " + opens, 1, mapped.size());
-
- merging.finishMerge();
- assertTrue("released by the only merge instance: " + opens, mapped.get(0).closed());
- }
+ };
+ try (dir;
+ DirectoryReader reader = openReader(dir)) {
+ StoredFieldsReader fields = ((CodecReader) getOnlyLeafReader(reader)).getFieldsReader();
+ opens.clear();
+ fail.set(true);
+ expectThrows(IOException.class, fields::getMergeInstance);
+ assertEquals(opens.toString(), List.of(), sequentialOpens(opens));
+ fail.set(false);
+ fields.getMergeInstance();
+ assertEquals(opens.toString(), 1, sequentialOpens(opens).size());
}
+ assertTrue("closed with the reader", opens.getLast().closed);
}
- private static void writeDocuments(Directory dir) throws IOException {
+ private static IndexWriter writer(Directory dir) throws IOException {
IndexWriterConfig iwc = new IndexWriterConfig();
- iwc.setCodec(TestUtil.getDefaultCodec()); // the merge advice is the default format's
- iwc.setUseCompoundFile(false); // so the directory sees the data file by name
- try (IndexWriter w = new IndexWriter(dir, iwc)) {
+ iwc.setCodec(TestUtil.getDefaultCodec());
+ iwc.setUseCompoundFile(false);
+ return new IndexWriter(dir, iwc);
+ }
+
+ private static void addSegments(IndexWriter w, int segments) throws IOException {
+ for (int segment = 0; segment < segments; segment++) {
for (int i = 0; i < 64; i++) {
Document doc = new Document();
doc.add(new StoredField("field", "value " + i));
@@ -211,129 +163,95 @@ public class TestStoredFieldsMergeReadAdvice extends LuceneTestCase {
}
}
- /** The opens a merge made for itself: the data file, asked for front to back. */
- private static List<Open> sequentialOpens(Opens opens) {
- List<Open> sequential = new ArrayList<>();
- for (Open open : opens.all()) {
- // integrity checks read a file once, front to back, and say so with READONCE
- if (open.name().endsWith(".fdt")
- && open.hint() == DataAccessHint.SEQUENTIAL
- && open.hints().contains(ReadOnceHint.INSTANCE) == false) {
- assertTrue(
- "a merge reads the data file once and does not come back: " + open,
- open.hints().contains(NoReuseHint.INSTANCE));
- assertSame(
- "the open says a merge is reading, so a directory can route it: " + open,
- IOContext.Context.MERGE,
- open.context());
- sequential.add(open);
- }
+ /** Opens a reader over a single segment of stored fields. */
+ private static DirectoryReader openReader(Directory dir) throws IOException {
+ try (IndexWriter w = writer(dir)) {
+ addSegments(w, 1);
+ }
+ return DirectoryReader.open(dir);
+ }
+
+ /** The opens of the data file for sequential reads, other than integrity checks. */
+ private static List<Open> sequentialOpens(List<Open> opens) {
+ List<Open> sequential =
+ opens.stream()
+ .filter(o -> o.name.endsWith(".fdt"))
+ .filter(o -> o.context.hints().contains(DataAccessHint.SEQUENTIAL))
+ .filter(o -> o.context.hints().contains(ReadOnceHint.INSTANCE) == false)
+ .toList();
+ for (Open open : sequential) {
+ assertTrue(open.toString(), open.context.hints().contains(NoReuseHint.INSTANCE));
}
return sequential;
}
/** One {@link Directory#openInput} call. */
private static final class Open {
- private final String name;
- private final IOContext context;
- private volatile boolean closed;
+ final String name;
+ final IOContext context;
+ volatile boolean closed;
Open(String name, IOContext context) {
this.name = name;
this.context = context;
}
- String name() {
- return name;
- }
-
- Set<IOContext.FileOpenHint> hints() {
- return context.hints();
- }
-
- DataAccessHint hint() {
- return context.hints(DataAccessHint.class).findFirst().orElse(null);
- }
-
- IOContext.Context context() {
- return context.context();
- }
-
- boolean closed() {
- return closed;
- }
-
@Override
public String toString() {
- return name + " [hint=" + hint() + " closed=" + closed + "]";
+ return name + context.hints() + (closed ? " closed" : "");
}
}
- private static final class Opens {
- private final List<Open> opens = new ArrayList<>();
- private final List<String> advised = new ArrayList<>();
-
- synchronized Open record(String name, IOContext context) {
- Open open = new Open(name, context);
- opens.add(open);
- return open;
- }
-
- /** Files whose advice was changed after they were opened. */
- synchronized void recordAdvice(String name) {
- advised.add(name);
- }
-
- synchronized List<Open> all() {
- return List.copyOf(opens);
- }
-
- synchronized List<String> advised() {
- return List.copyOf(advised);
- }
-
- synchronized void clear() {
- opens.clear();
- advised.clear();
- }
+ /**
+ * Records opens. Its inputs are efficient for a context like memory-mapped inputs using {@link
+ * MMapDirectory#ADVISE_BY_CONTEXT}, or for every context.
+ */
+ private static class RecordingDirectory extends FilterDirectory {
+ private final List<Open> opens;
+ private final boolean advises;
- @Override
- public synchronized String toString() {
- return "opens: " + opens + ", advised: " + advised;
+ RecordingDirectory(Directory in, List<Open> opens) {
+ this(in, opens, true);
}
- }
- private static class RecordingDirectory extends FilterDirectory {
- private final Opens opens;
-
- RecordingDirectory(Directory in, Opens opens) {
+ RecordingDirectory(Directory in, List<Open> opens, boolean advises) {
super(in);
this.opens = opens;
+ this.advises = advises;
}
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
- Open open = opens.record(name, context);
- return new RecordingIndexInput(super.openInput(name, context), name, open, opens);
+ Open open = new Open(name, context);
+ opens.add(open);
+ return new RecordingIndexInput(super.openInput(name, context), open, context, advises);
}
}
private static final class RecordingIndexInput extends FilterIndexInput {
- private final String name;
private final Open open;
- private final Opens opens;
+ // the context the pages of this input were advised with
+ private final IOContext context;
+ private final boolean advises;
- RecordingIndexInput(IndexInput in, String name, Open open, Opens opens) {
- super("Recording(" + name + ")", in);
- this.name = name;
+ RecordingIndexInput(IndexInput in, Open open, IOContext context, boolean advises) {
+ super("Recording(" + open.name + ")", in);
this.open = open;
- this.opens = opens;
+ this.context = context;
+ this.advises = advises;
}
@Override
- public void updateIOContext(IOContext context) throws IOException {
- opens.recordAdvice(name);
- in.updateIOContext(context);
+ public boolean isEfficientFor(IOContext other) {
+ if (advises == false) {
+ return true;
+ }
+ ReadAdvice advice = MMapDirectory.ADVISE_BY_CONTEXT.apply(open.name, context).orElseThrow();
+ ReadAdvice otherAdvice =
+ MMapDirectory.ADVISE_BY_CONTEXT.apply(open.name, other).orElseThrow();
+ return advice == ReadAdvice.NORMAL
+ || otherAdvice == ReadAdvice.NORMAL
+ || advice == otherAdvice;
}
@Override
@@ -344,12 +262,13 @@ public class TestStoredFieldsMergeReadAdvice extends LuceneTestCase {
@Override
public IndexInput clone() {
- return new RecordingIndexInput(in.clone(), name, open, opens);
+ return new RecordingIndexInput(in.clone(), open, context, advises);
}
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- return new RecordingIndexInput(in.slice(sliceDescription, offset, length), name, open, opens);
+ return new RecordingIndexInput(
+ in.slice(sliceDescription, offset, length), open, context, advises);
}
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
index 538c73a1c64..f465331d39b 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestMMapDirectory.java
@@ -181,6 +181,62 @@ public class TestMMapDirectory extends BaseDirectoryTestCase {
}
}
+ /** Only random and sequential reads get inputs of their own. */
+ public void testIsEfficientFor() throws IOException {
+ var random = new DefaultIOContext(DataAccessHint.RANDOM);
+ var sequential = new DefaultIOContext(DataAccessHint.SEQUENTIAL);
+ // larger than a page, so that slices get advised
+ final int size = 1 << 17;
+ try (MMapDirectory dir = new MMapDirectory(createTempDir("testIsEfficientFor"))) {
+ try (IndexOutput out = dir.createOutput("test", IOContext.DEFAULT)) {
+ out.writeBytes(new byte[size], 0, size);
+ }
+ try (IndexInput in = dir.openInput("test", random)) {
+ assertTrue("one advice for every context", in.isEfficientFor(sequential));
+ }
+
+ dir.setReadAdvice(MMapDirectory.ADVISE_BY_CONTEXT);
+ boolean advises = MMapDirectory.supportsMadvise();
+ try (IndexInput in = dir.openInput("test", random)) {
+ assertTrue(in.isEfficientFor(random));
+ assertTrue("normal reads share a random input", in.isEfficientFor(IOContext.DEFAULT));
+ assertEquals(advises == false, in.isEfficientFor(sequential));
+ assertEquals(
+ "a clone reads the same pages",
+ advises == false,
+ in.clone().isEfficientFor(sequential));
+ assertEquals(
+ "a slice reads the same pages",
+ advises == false,
+ in.slice("slice", 0, size).isEfficientFor(sequential));
+ dir.setReadAdvice((_, _) -> Optional.of(ReadAdvice.NORMAL));
+ assertEquals(
+ "an input keeps the function it was opened with",
+ advises == false,
+ in.isEfficientFor(sequential));
+ dir.setReadAdvice(MMapDirectory.ADVISE_BY_CONTEXT);
+
+ IndexInput advised = in.slice("advised", 0, size, sequential);
+ assertTrue(advised.isEfficientFor(sequential));
+ assertEquals(advises == false, advised.isEfficientFor(random));
+ assertTrue(
+ "too small to be advised", in.slice("small", 0, 1, sequential).isEfficientFor(random));
+ }
+
+ try (IndexOutput out = dir.createOutput("small", IOContext.DEFAULT)) {
+ out.writeByte((byte) 0);
+ }
+ try (IndexInput in = dir.openInput("small", random)) {
+ assertTrue("a file within a page is not opened again", in.isEfficientFor(sequential));
+ }
+
+ dir.setPreload((_, _) -> true);
+ try (IndexInput in = dir.openInput("test", random)) {
+ assertTrue("preloaded pages are resident", in.isEfficientFor(sequential));
+ }
+ }
+ }
+
public void testPreload() throws Exception {
assumeTrue("madvise for preloading only works on linux/mac", MMapDirectory.supportsMadvise());
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsReader.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsReader.java
index 1c83b45e6b1..295a4c15e5c 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsReader.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsReader.java
@@ -172,6 +172,12 @@ final class FaissKnnVectorsReader extends KnnVectorsReader {
return rawVectorsReader.getFloatVectorValues(field);
}
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return rawVectorsReader.getFloatVectorValues(field, access);
+ }
+
@Override
public ByteVectorValues getByteVectorValues(String field) {
// TODO: Support using SQ8 quantization, see:
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingKnnVectorsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingKnnVectorsFormat.java
index 6a55353ffc9..2db24023e25 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingKnnVectorsFormat.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingKnnVectorsFormat.java
@@ -38,9 +38,11 @@ import org.apache.lucene.index.Sorter;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.KnnCollector;
+import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.tests.search.AssertingAcceptDocs;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.IORunnable;
+import org.apache.lucene.util.IOSupplier;
import org.apache.lucene.util.hnsw.HnswGraph;
/** Wraps the default KnnVectorsFormat and provides additional assertions. */
@@ -134,11 +136,22 @@ public class AssertingKnnVectorsFormat extends KnnVectorsFormat {
@Override
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
+ return checkFloatVectorValues(field, () -> delegate.getFloatVectorValues(field));
+ }
+
+ @Override
+ public FloatVectorValues getFloatVectorValues(String field, DataAccessHint access)
+ throws IOException {
+ return checkFloatVectorValues(field, () -> delegate.getFloatVectorValues(field, access));
+ }
+
+ private FloatVectorValues checkFloatVectorValues(
+ String field, IOSupplier<FloatVectorValues> values) throws IOException {
FieldInfo fi = fis.fieldInfo(field);
assert fi != null
&& fi.getVectorDimension() > 0
&& fi.getVectorEncoding() == VectorEncoding.FLOAT32;
- FloatVectorValues floatValues = delegate.getFloatVectorValues(field);
+ FloatVectorValues floatValues = values.get();
assert floatValues != null;
assert floatValues.iterator().docID() == -1;
assert floatValues.size() >= 0;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java
index 55344ad063d..7e1792f5d55 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java
@@ -387,8 +387,8 @@ public abstract class BaseCompoundFormatTestCase extends BaseIndexFileFormatTest
dir.close();
}
- // Make sure we don't somehow use more than 1 descriptor when reading a CFS with many subs, plus
- // 1 for the mapping merges may read through:
+ // Make sure we don't somehow use more than 1 descriptor
+ // when reading a CFS with many subs:
public void testManySubFiles() throws IOException {
final MockDirectoryWrapper dir = newMockFSDirectory(createTempDir("CFSManySubFiles"));
@@ -413,24 +413,18 @@ public abstract class BaseCompoundFormatTestCase extends BaseIndexFileFormatTest
Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si);
final IndexInput[] ins = new IndexInput[FILE_COUNT];
- int maxHandles = 1;
for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
- IOContext context = newIOContext(random());
- if (context.context() == IOContext.Context.MERGE) {
- maxHandles = 2;
- }
- ins[fileIdx] = cfs.openInput("_123." + fileIdx, context);
+ ins[fileIdx] = cfs.openInput("_123." + fileIdx, newIOContext(random()));
CodecUtil.checkIndexHeader(ins[fileIdx], "Foo", 0, 0, si.getId(), "suffix");
}
- long handles = dir.getFileHandleCount();
- assertTrue("handles: " + handles, handles >= 1 && handles <= maxHandles);
+ assertEquals(1, dir.getFileHandleCount());
for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
assertEquals((byte) fileIdx, ins[fileIdx].readByte());
}
- assertEquals(handles, dir.getFileHandleCount());
+ assertEquals(1, dir.getFileHandleCount());
for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
ins[fileIdx].close();
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
index 9e54cb83ac6..02f38cd124a 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
@@ -191,6 +191,13 @@ public class MockIndexInputWrapper extends FilterIndexInput {
in.updateIOContext(context);
}
+ @Override
+ public boolean isEfficientFor(IOContext context) {
+ ensureOpen();
+ ensureAccessible();
+ return in.isEfficientFor(context);
+ }
+
@Override
public long length() {
ensureOpen();