Skip to content

Commit 4766166

Browse files
committed
Fix flaky DerivedSourceLeafReaderTests
testWithRandomDocuments was flaky due to two issues: 1. NoMergePolicy blocked forceMerge(1), so randomized IndexWriterConfig settings that caused multiple flushes resulted in multiple segments instead of the expected one. 2. The test stored _source in the index, but DerivedSourceStoredFields injects _source via the source provider then delegates to the underlying stored fields. This caused the visitor to receive _source twice, with the second call using Lucene's internal buffer which could differ from the original bytes. Fix by removing NoMergePolicy so forceMerge(1) works, and not storing _source in the index, which matches the production use case where derived source synthesizes _source from other data. Signed-off-by: Andrew Ross <andrross@amazon.com>
1 parent cfebc67 commit 4766166

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

server/src/test/java/org/opensearch/common/lucene/index/DerivedSourceLeafReaderTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.apache.lucene.index.IndexWriter;
1919
import org.apache.lucene.index.IndexWriterConfig;
2020
import org.apache.lucene.index.LeafReader;
21-
import org.apache.lucene.index.NoMergePolicy;
2221
import org.apache.lucene.index.StoredFieldVisitor;
2322
import org.apache.lucene.index.StoredFields;
2423
import org.apache.lucene.store.Directory;
@@ -146,8 +145,7 @@ public void testGetCoreAndReaderCacheHelper() {
146145

147146
public void testWithRandomDocuments() throws IOException {
148147
Directory randomDir = newDirectory();
149-
IndexWriterConfig config = newIndexWriterConfig(random(), null).setCodec(new RandomCodec(random()))
150-
.setMergePolicy(NoMergePolicy.INSTANCE); // Prevent automatic merges
148+
IndexWriterConfig config = newIndexWriterConfig(random(), null).setCodec(new RandomCodec(random()));
151149

152150
IndexWriter randomWriter = new IndexWriter(randomDir, config);
153151

@@ -158,7 +156,7 @@ public void testWithRandomDocuments() throws IOException {
158156
byte[] source = randomByteArrayOfLength(randomIntBetween(10, 50));
159157
docIdToSource.put(i, source);
160158
Document doc = new Document();
161-
doc.add(new StoredField("_source", source));
159+
doc.add(new StoredField("field", i));
162160
randomWriter.addDocument(doc);
163161
}
164162

0 commit comments

Comments
 (0)