Apache Lucene open-source search software
Stop buffering doc-values updates in heap during merges (#16570)
Jim Ferenczi9092bda09d61
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 67a4e7abd7a..62204c11d4b 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -4041,10 +4041,6 @@ public class IndexWritermerge.initMergeReaders(sci -> {final ReadersAndUpdates rld = getPooledInstance(sci, true);- // calling setIsMerging is important since it causes the RaU to record all DV- // updates- // in a separate map in order to be applied to the merged segment after it's done- rld.setIsMerging();return rld.getReaderForMerge(context, mr -> deleter.incRef(mr.reader.getSegmentInfo().files()));});@@ -4465,10 +4461,6 @@ public class IndexWriterfinal ReadersAndUpdates mergedDeletesAndUpdates = getPooledInstance(merge.info, true);int numDeletesBefore = mergedDeletesAndUpdates.getDelCount();// field -> delGen -> dv field updates- Map<String, LongObjectHashMap<DocValuesFieldUpdates>> mappedDVUpdates = new HashMap<>();-- boolean anyDVUpdates = false;-assert sourceSegments.size() == docMaps.length;for (int i = 0; i < sourceSegments.size(); i++) {SegmentCommitInfo info = sourceSegments.get(i);@@ -4487,96 +4479,231 @@ public class IndexWritermerge.getMergeReader().get(i).hardLiveDocs,rld.getHardLiveDocs(),segDocMap);+ }- // Now carry over all doc values updates that were resolved while we were merging, remapping- // the docIDs to the newly merged docIDs.- // We only carry over packets that finished resolving; if any are still running (concurrently)- // they will detect that our merge completed- // and re-resolve against the newly merged segment:- Map<String, List<DocValuesFieldUpdates>> mergingDVUpdates = rld.getMergingDVUpdates();- for (Map.Entry<String, List<DocValuesFieldUpdates>> ent : mergingDVUpdates.entrySet()) {-- String field = ent.getKey();+ // Carry over the doc-values updates that resolved while merging, remapping to the merged+ // docIDs.+ // Updates still resolving concurrently are skipped; they re-resolve against the merged segment+ // once they complete.+ Map<String, LongObjectHashMap<DocValuesFieldUpdates>> mappedDVUpdates =+ buildMappedDVUpdatesFromDisk(merge, docMaps, minGen);- LongObjectHashMap<DocValuesFieldUpdates> mappedField = mappedDVUpdates.get(field);- if (mappedField == null) {- mappedField = new LongObjectHashMap<>();- mappedDVUpdates.put(field, mappedField);+ boolean anyDVUpdates = false;+ if (mappedDVUpdates.isEmpty() == false) {+ // Persist the merged DV updates onto the RAU for the merged segment (already finished):+ for (LongObjectHashMap<DocValuesFieldUpdates> d : mappedDVUpdates.values()) {+ for (ObjectCursor<DocValuesFieldUpdates> updates : d.values()) {+ mergedDeletesAndUpdates.addDVUpdate(updates.value);+ anyDVUpdates = true;}+ }+ }++ if (infoStream.isEnabled("IW")) {+ String msg = mergedDeletesAndUpdates.getDelCount() - numDeletesBefore + " new deletes";+ if (anyDVUpdates) {+ msg += " and " + mergedDeletesAndUpdates.getNumDVUpdates() + " new field updates";+ msg += " (" + mergedDeletesAndUpdates.ramBytesUsed.get() + ") bytes";+ }+ msg += " since merge started";+ infoStream.message("IW", msg);+ }- for (DocValuesFieldUpdates updates : ent.getValue()) {+ merge.info.setBufferedDeletesGen(minGen);- if (bufferedUpdatesStream.stillRunning(updates.delGen)) {- continue;- }+ return mergedDeletesAndUpdates;+ }- // sanity check:- assert field.equals(updates.field);+ /** Sentinel for "no doc-values value" (a reset, or a doc that simply has no value). */+ private static final Object DV_NO_VALUE = new Object();- DocValuesFieldUpdates mappedUpdates = mappedField.get(updates.delGen);- if (mappedUpdates == null) {- switch (updates.type) {- case NUMERIC:- mappedUpdates =- new NumericDocValuesFieldUpdates(- updates.delGen, updates.field, merge.info.info.maxDoc());- break;- case BINARY:- mappedUpdates =- new BinaryDocValuesFieldUpdates(- updates.delGen, updates.field, merge.info.info.maxDoc());- break;- case NONE:- case SORTED:- case SORTED_SET:- case SORTED_NUMERIC:- default:- throw new AssertionError();+ /**+ * Builds the doc-values-update merge carry-over from the source segments' on-disk state plus+ * their residual (resolved but not-yet-written) pending updates, remapping to merged docIDs.+ * Returns {@code field -> delGen -> updates} for the merged {@link ReadersAndUpdates} to apply.+ *+ * <p>Per source segment and updated field, the written changes are read back from disk+ * (current-vs-baseline diff) and collapsed into one packet, while the residual updates keep their+ * real delGens. Flushing advances {@code completedDelGen} as a prefix, so every residual delGen+ * is greater than any written one; the collapsed packet therefore uses {@code minResidualDelGen -+ * 1} (or {@code completedDelGen} when there is no residual), a gen below all residual and+ * still-running ones so newest-wins ordering holds on the merged segment.+ */+ private Map<String, LongObjectHashMap<DocValuesFieldUpdates>> buildMappedDVUpdatesFromDisk(+ MergePolicy.OneMerge merge, MergeState.DocMap[] docMaps, long minGen) throws IOException {+ final int mergedMaxDoc = merge.info.info.maxDoc();+ Map<String, LongObjectHashMap<DocValuesFieldUpdates>> mapped = new HashMap<>();+ for (int i = 0; i < merge.segments.size(); i++) {+ SegmentCommitInfo info = merge.segments.get(i);+ final ReadersAndUpdates rld = getPooledInstance(info, false);+ final MergeState.DocMap segDocMap = docMaps[i];+ final CodecReader baseline = merge.getMergeReader().get(i).codecReader;+ final SegmentReader current = rld.getReader(IOContext.DEFAULT);+ try {+ // Residual finished-but-unflushed updates for this segment, per field, skipping any packet+ // still globally running (those re-resolve against the merged segment).+ Map<String, List<DocValuesFieldUpdates>> residualByField = new HashMap<>();+ for (Map.Entry<String, List<DocValuesFieldUpdates>> e :+ rld.getPendingDVUpdatesSnapshot().entrySet()) {+ List<DocValuesFieldUpdates> keep = new ArrayList<>();+ for (DocValuesFieldUpdates u : e.getValue()) {+ if (bufferedUpdatesStream.stillRunning(u.delGen) == false) {+ keep.add(u);}- mappedField.put(updates.delGen, mappedUpdates);}+ if (keep.isEmpty() == false) {+ residualByField.put(e.getKey(), keep);+ }+ }+ // Updated fields = those whose on-disk DV generation advanced vs the merge-reader baseline+ // (gen advances for both classic and overlay writes), plus any field with residual updates.+ Set<String> updatedFields = new HashSet<>(residualByField.keySet());+ FieldInfos baseFieldInfos = baseline.getFieldInfos();+ for (FieldInfo fi : current.getFieldInfos()) {+ DocValuesType t = fi.getDocValuesType();+ if (t != DocValuesType.NUMERIC && t != DocValuesType.BINARY) {+ continue;+ }+ FieldInfo baseFi = baseFieldInfos.fieldInfo(fi.name);+ if (baseFi == null || baseFi.getDocValuesGen() != fi.getDocValuesGen()) {+ updatedFields.add(fi.name);+ }+ }- DocValuesFieldUpdates.Iterator it = updates.iterator();- int doc;- while ((doc = it.nextDoc()) != NO_MORE_DOCS) {- int mappedDoc = segDocMap.get(doc);- if (mappedDoc != -1) {+ for (String field : updatedFields) {+ List<DocValuesFieldUpdates> residual = residualByField.getOrDefault(field, List.of());+ long minResidual = Long.MAX_VALUE;+ for (DocValuesFieldUpdates u : residual) {+ minResidual = Math.min(minResidual, u.delGen);+ }+ final long diskDelGen =+ minResidual != Long.MAX_VALUE+ ? minResidual - 1+ : bufferedUpdatesStream.getCompletedDelGen();+ LongObjectHashMap<DocValuesFieldUpdates> byGen =+ mapped.computeIfAbsent(field, _ -> new LongObjectHashMap<>());++ // (A) flushed changes since baseline, collapsed at diskDelGen. Only when the field has+ // on-disk doc values; a field whose only updates are still pending is absent from the+ // reader (e.g. a soft-deletes field on its first, not-yet-written update).+ FieldInfo fi = current.getFieldInfos().fieldInfo(field);+ if (fi != null) {+ addDiskDiffToPacket(fi, baseline, current, segDocMap, diskDelGen, mergedMaxDoc, byGen);+ assert byGen.containsKey(diskDelGen) == false || diskDelGen > minGen+ : "diskDelGen " + diskDelGen + " <= minGen " + minGen;+ }++ // (B) residual updates at their real delGens, typed from the packet itself since the+ // field+ // need not exist on disk yet.+ for (DocValuesFieldUpdates u : residual) {+ DocValuesFieldUpdates.Iterator it = u.iterator();+ int doc;+ while ((doc = it.nextDoc()) != NO_MORE_DOCS) {+ int md = segDocMap.get(doc);+ if (md == -1) {+ continue;+ }+ DocValuesFieldUpdates p = byGen.get(u.delGen);+ if (p == null) {+ p =+ u.type == DocValuesType.BINARY+ ? new BinaryDocValuesFieldUpdates(u.delGen, u.field, mergedMaxDoc)+ : new NumericDocValuesFieldUpdates(u.delGen, u.field, mergedMaxDoc);+ byGen.put(u.delGen, p);+ }if (it.hasValue()) {- // not deleted- mappedUpdates.add(mappedDoc, it);+ p.add(md, it);} else {- mappedUpdates.reset(mappedDoc);+ p.reset(md);}- anyDVUpdates = true;}}}+ } finally {+ current.decRef();}}-- if (anyDVUpdates) {- // Persist the merged DV updates onto the RAU for the merged segment:- for (LongObjectHashMap<DocValuesFieldUpdates> d : mappedDVUpdates.values()) {- for (ObjectCursor<DocValuesFieldUpdates> updates : d.values()) {- updates.value.finish();- mergedDeletesAndUpdates.addDVUpdate(updates.value);- }+ for (LongObjectHashMap<DocValuesFieldUpdates> byGen : mapped.values()) {+ for (ObjectCursor<DocValuesFieldUpdates> c : byGen.values()) {+ c.value.finish();}}+ return mapped;+ }- if (infoStream.isEnabled("IW")) {- String msg = mergedDeletesAndUpdates.getDelCount() - numDeletesBefore + " new deletes";- if (anyDVUpdates) {- msg += " and " + mergedDeletesAndUpdates.getNumDVUpdates() + " new field updates";- msg += " (" + mergedDeletesAndUpdates.ramBytesUsed.get() + ") bytes";- }- msg += " since merge started";- infoStream.message("IW", msg);+ private static DocValuesFieldUpdates getOrCreatePacket(+ LongObjectHashMap<DocValuesFieldUpdates> byGen, long delGen, FieldInfo fi, int mergedMaxDoc) {+ DocValuesFieldUpdates p = byGen.get(delGen);+ if (p == null) {+ p =+ fi.getDocValuesType() == DocValuesType.BINARY+ ? new BinaryDocValuesFieldUpdates(delGen, fi.name, mergedMaxDoc)+ : new NumericDocValuesFieldUpdates(delGen, fi.name, mergedMaxDoc);+ byGen.put(delGen, p);}+ return p;+ }- merge.info.setBufferedDeletesGen(minGen);+ /**+ * Emits, into a single {@code diskDelGen} packet, each merged doc of {@code fi} whose current+ * on-disk value differs from the merge-reader baseline (i.e. an update flushed during the merge),+ * remapping source docIDs to merged docIDs.+ */+ private static void addDiskDiffToPacket(+ FieldInfo fi,+ CodecReader baseline,+ CodecReader current,+ MergeState.DocMap segDocMap,+ long diskDelGen,+ int mergedMaxDoc,+ LongObjectHashMap<DocValuesFieldUpdates> byGen)+ throws IOException {+ final int maxDoc = current.maxDoc();+ final boolean binary = fi.getDocValuesType() == DocValuesType.BINARY;+ BinaryDocValues curB = binary ? current.getBinaryDocValues(fi.name) : null;+ BinaryDocValues baseB = binary ? baseline.getBinaryDocValues(fi.name) : null;+ NumericDocValues curN = binary ? null : current.getNumericDocValues(fi.name);+ NumericDocValues baseN = binary ? null : baseline.getNumericDocValues(fi.name);+ for (int doc = 0; doc < maxDoc; doc++) {+ int md = segDocMap.get(doc);+ if (md == -1) {+ continue;+ }+ Object curVal;+ Object baseVal;+ if (binary) {+ // Live BytesRefs suffice to compare; only a changed value is copied, below, when it is+ // stored.+ curVal = (curB != null && curB.advanceExact(doc)) ? curB.binaryValue() : DV_NO_VALUE;+ baseVal = (baseB != null && baseB.advanceExact(doc)) ? baseB.binaryValue() : DV_NO_VALUE;+ } else {+ curVal =+ (curN != null && curN.advanceExact(doc)) ? Long.valueOf(curN.longValue()) : DV_NO_VALUE;+ baseVal =+ (baseN != null && baseN.advanceExact(doc))+ ? Long.valueOf(baseN.longValue())+ : DV_NO_VALUE;+ }+ if (dvEquals(curVal, baseVal)) {+ continue;+ }+ DocValuesFieldUpdates p = getOrCreatePacket(byGen, diskDelGen, fi, mergedMaxDoc);+ if (curVal == DV_NO_VALUE) {+ p.reset(md);+ } else if (binary) {+ ((BinaryDocValuesFieldUpdates) p).add(md, BytesRef.deepCopyOf((BytesRef) curVal));+ } else {+ ((NumericDocValuesFieldUpdates) p).add(md, (Long) curVal);+ }+ }+ }- return mergedDeletesAndUpdates;+ private static boolean dvEquals(Object a, Object b) {+ if (a == DV_NO_VALUE || b == DV_NO_VALUE) {+ return a == b;+ }+ return a.equals(b);}/**@@ -5160,8 +5287,6 @@ public class IndexWriterassert rld != null;if (drop) {rld.dropChanges();- } else {- rld.dropMergingUpdates();}rld.release(sr);release(rld);@@ -5238,7 +5363,6 @@ public class IndexWritermerge.initMergeReaders(sci -> {final ReadersAndUpdates rld = getPooledInstance(sci, true);- rld.setIsMerging();synchronized (this) {return rld.getReaderForMerge(context, mr -> deleter.incRef(mr.reader.getSegmentInfo().files()));diff --git a/lucene/core/src/java/org/apache/lucene/index/ReaderPool.java b/lucene/core/src/java/org/apache/lucene/index/ReaderPool.javaindex d66b03021c3..dddb582ebd1 100644--- a/lucene/core/src/java/org/apache/lucene/index/ReaderPool.java+++ b/lucene/core/src/java/org/apache/lucene/index/ReaderPool.java@@ -270,7 +270,6 @@ final class ReaderPool implements Closeable {any |=rld.writeFieldUpdates(directory, fieldNumbers, completedDelGenSupplier.getAsLong(), infoStream);- rld.setIsMerging();}}return any;diff --git a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.javaindex 4593447a4ff..80c062bbd42 100644--- a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java+++ b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java@@ -78,23 +78,10 @@ final class ReadersAndUpdates {private final LiveIndexWriterConfig config;- // Indicates whether this segment is currently being merged. While a segment- // is merging, all field updates are also registered in the- // mergingDVUpdates map. Also, calls to writeFieldUpdates merge the- // updates with mergingDVUpdates.- // That way, when the segment is done merging, IndexWriter can apply the- // updates on the merged segment too.- private boolean isMerging = false;-// Holds resolved (to docIDs) doc values updates that have not yet been// written to the indexprivate final Map<String, List<DocValuesFieldUpdates>> pendingDVUpdates = new HashMap<>();- // Holds resolved (to docIDs) doc values updates that were resolved while- // this segment was being merged; at the end of the merge we carry over- // these updates (remapping their docIDs) to the newly merged segment- private final Map<String, List<DocValuesFieldUpdates>> mergingDVUpdates = new HashMap<>();-// Only set if there are doc values updates against this segment, and the index is sorted:Sorter.DocMap sortMap;@@ -174,15 +161,6 @@ final class ReadersAndUpdates {ramBytesUsed.addAndGet(update.ramBytesUsed());fieldUpdates.add(update);-- if (isMerging) {- fieldUpdates = mergingDVUpdates.get(update.field);- if (fieldUpdates == null) {- fieldUpdates = new ArrayList<>();- mergingDVUpdates.put(update.field, fieldUpdates);- }- fieldUpdates.add(update);- }}public synchronized long getNumDVUpdates() {@@ -193,6 +171,19 @@ final class ReadersAndUpdates {return count;}+ /**+ * Copies of the currently-pending (resolved but not yet written) doc-values updates, keyed by+ * field. Returning copies lets the merge carry-over iterate them, and do its disk I/O, without+ * holding this monitor; IndexWriter still coordinates the carry-over for correctness.+ */+ synchronized Map<String, List<DocValuesFieldUpdates>> getPendingDVUpdatesSnapshot() {+ Map<String, List<DocValuesFieldUpdates>> copy = new HashMap<>();+ for (Map.Entry<String, List<DocValuesFieldUpdates>> ent : pendingDVUpdates.entrySet()) {+ copy.put(ent.getKey(), new ArrayList<>(ent.getValue()));+ }+ return copy;+ }+/** Returns a {@link SegmentReader}. */public synchronized SegmentReader getReader(IOContext context) throws IOException {if (reader == null) {@@ -291,7 +282,6 @@ final class ReadersAndUpdates {// deletes onto the newly merged segment, so we can// discard them on the sub-readers:pendingDeletes.dropChanges();- dropMergingUpdates();}// Commit live docs (writes new _X_N.del files) and field updates (writes new@@ -1000,36 +990,9 @@ final class ReadersAndUpdates {reader = createNewReaderWithLatestLiveDocs(reader);}- synchronized void setIsMerging() {- // This ensures any newly resolved doc value updates while we are merging are- // saved for re-applying after this segment is done merging:- if (isMerging == false) {- isMerging = true;- assert mergingDVUpdates.isEmpty();- }- }-- synchronized boolean isMerging() {- return isMerging;- }-/** Returns a reader for merge, with the latest doc values updates and deletions. */synchronized MergePolicy.MergeReader getReaderForMerge(IOContext context, IOConsumer<MergePolicy.MergeReader> readerConsumer) throws IOException {-- // We must carry over any still-pending DV updates because they were not- // successfully written, e.g. because there was a hole in the delGens,- // or they arrived after we wrote all DVs for merge but before we set- // isMerging here:- for (Map.Entry<String, List<DocValuesFieldUpdates>> ent : pendingDVUpdates.entrySet()) {- List<DocValuesFieldUpdates> mergingUpdates = mergingDVUpdates.get(ent.getKey());- if (mergingUpdates == null) {- mergingUpdates = new ArrayList<>();- mergingDVUpdates.put(ent.getKey(), mergingUpdates);- }- mergingUpdates.addAll(ent.getValue());- }-SegmentReader reader = getReader(context);if (pendingDeletes.needsRefresh(reader)|| reader.getSegmentInfo().getDelGen() != pendingDeletes.info.getDelGen()) {@@ -1044,22 +1007,6 @@ final class ReadersAndUpdates {return mergeReader;}- /**- * Drops all merging updates. Called from IndexWriter after this segment finished merging (whether- * successfully or not).- */- public synchronized void dropMergingUpdates() {- mergingDVUpdates.clear();- isMerging = false;- }-- public synchronized Map<String, List<DocValuesFieldUpdates>> getMergingDVUpdates() {- // We must atomically (in single sync'd block) clear isMerging when we return the DV updates- // otherwise we can lose updates:- isMerging = false;- return mergingDVUpdates;- }-@Overridepublic String toString() {StringBuilder sb = new StringBuilder();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestMergeCarryOverFromDisk.java b/lucene/core/src/test/org/apache/lucene/index/TestMergeCarryOverFromDisk.javanew file mode 100644index 00000000000..0cf456aa53e--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestMergeCarryOverFromDisk.java@@ -0,0 +1,290 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import java.io.IOException;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.atomic.AtomicReference;+import org.apache.lucene.document.BinaryDocValuesField;+import org.apache.lucene.document.Document;+import org.apache.lucene.document.NumericDocValuesField;+import org.apache.lucene.document.StringField;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.FilterDirectory;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.tests.analysis.MockAnalyzer;+import org.apache.lucene.tests.util.LuceneTestCase;+import org.apache.lucene.util.Bits;+import org.apache.lucene.util.BytesRef;++/**+ * Deterministically exercises the doc-values-update merge carry-over path: an update that resolves+ * onto a segment <em>while it is being merged</em> must appear on the merged segment. That+ * carry-over is reconstructed from the source segments at merge commit; the test blocks a merge+ * mid-flight (only its own output, via {@link IOContext#mergeInfo}, so the update's flush is free),+ * applies + resolves updates, then releases the merge.+ */+public class TestMergeCarryOverFromDisk extends LuceneTestCase {++ /** Wraps a directory, pausing the first merge output write until the test releases it. */+ private static class MergePausingDirectory extends FilterDirectory {+ final CountDownLatch mergeStarted = new CountDownLatch(1);+ final CountDownLatch resumeMerge = new CountDownLatch(1);+ final AtomicReference<Throwable> failure = new AtomicReference<>();++ MergePausingDirectory(Directory in) {+ super(in);+ }++ @Override+ public IndexOutput createOutput(String name, IOContext context) throws IOException {+ if (context != null && context.mergeInfo() != null && mergeStarted.getCount() > 0) {+ mergeStarted.countDown();+ try {+ resumeMerge.await();+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ throw new IOException(e);+ }+ }+ return super.createOutput(name, context);+ }+ }++ private static Document doc(int id, long ndv, String bdv) {+ Document d = new Document();+ d.add(new StringField("id", Integer.toString(id), StringField.Store.NO));+ d.add(new NumericDocValuesField("ndv", ndv));+ d.add(new BinaryDocValuesField("bdv", new BytesRef(bdv)));+ return d;+ }++ public void testUpdatesResolvedDuringMergeAreCarriedOver() throws Exception {+ MergePausingDirectory dir = new MergePausingDirectory(newDirectory());+ IndexWriterConfig conf =+ newIndexWriterConfig(new MockAnalyzer(random()))+ .setMergeScheduler(new ConcurrentMergeScheduler());+ IndexWriter writer = new IndexWriter(dir, conf);++ // Two segments; the default TieredMergePolicy won't auto-merge two small segments.+ for (int i = 0; i < 4; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();+ for (int i = 4; i < 8; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();++ // Force the merge on a background thread so we can inject updates while it is paused+ // mid-flight.+ Thread merger =+ new Thread(+ () -> {+ try {+ writer.forceMerge(1);+ } catch (Throwable t) {+ dir.failure.compareAndSet(null, t);+ }+ },+ "forceMerge");+ merger.start();++ // Wait until the merge has opened its readers (its on-disk baseline is fixed) and begun writing+ // output.+ dir.mergeStarted.await();++ // Update numeric + binary values on docs in the now-merging segments, then resolve them to the+ // segments (and disk) by reopening a reader from the writer.+ writer.updateNumericDocValue(new Term("id", "1"), "ndv", 101L);+ writer.updateNumericDocValue(new Term("id", "5"), "ndv", 105L);+ writer.updateBinaryDocValue(new Term("id", "2"), "bdv", new BytesRef("updated-2"));+ writer.updateBinaryDocValue(new Term("id", "6"), "bdv", new BytesRef("updated-6"));+ // Force resolution onto the merging segments (writes DV gens to disk).+ try (DirectoryReader r = DirectoryReader.open(writer)) {+ assertNotNull(r);+ }++ // Let the merge finish; the disk carry-over in commitMergedDeletesAndUpdates runs here.+ dir.resumeMerge.countDown();+ merger.join();+ assertNull("forceMerge failed: " + dir.failure.get(), dir.failure.get());++ // Verify the merged segment reflects the numeric + binary updates that resolved during the+ // merge.+ try (DirectoryReader reader = DirectoryReader.open(writer)) {+ assertEquals(1, reader.leaves().size()); // single merged segment+ LeafReader leaf = reader.leaves().get(0).reader();+ NumericDocValues ndv = leaf.getNumericDocValues("ndv");+ BinaryDocValues bdv = leaf.getBinaryDocValues("bdv");+ java.util.Set<Long> ndvValues = new java.util.HashSet<>();+ java.util.Set<String> bdvValues = new java.util.HashSet<>();+ for (int d = 0; d < leaf.maxDoc(); d++) {+ assertTrue(ndv.advanceExact(d));+ ndvValues.add(ndv.longValue());+ assertTrue(bdv.advanceExact(d));+ bdvValues.add(bdv.binaryValue().utf8ToString());+ }+ assertTrue("carried numeric update 101 missing", ndvValues.contains(101L));+ assertTrue("carried numeric update 105 missing", ndvValues.contains(105L));+ assertTrue("carried binary update updated-2 missing", bdvValues.contains("updated-2"));+ assertTrue("carried binary update updated-6 missing", bdvValues.contains("updated-6"));+ }++ writer.close();+ dir.close();+ }++ /**+ * Applies more than {@code maxDocValuesOverlays} updates to one field of a merging segment while+ * the merge is paused, forcing the sparse overlays to fold (and possibly fold to a dense column)+ * before the carry-over reads them back. The merged segment must reflect the final value.+ */+ public void testFoldedOverlaysCarriedOverDuringMerge() throws Exception {+ MergePausingDirectory dir = new MergePausingDirectory(newDirectory());+ IndexWriterConfig conf =+ newIndexWriterConfig(new MockAnalyzer(random()))+ .setMergeScheduler(new ConcurrentMergeScheduler())+ // small overlay budget so a handful of updates already triggers a fold+ .setMaxDocValuesOverlays(4);+ IndexWriter writer = new IndexWriter(dir, conf);+ for (int i = 0; i < 4; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();+ for (int i = 4; i < 8; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();++ Thread merger =+ new Thread(+ () -> {+ try {+ writer.forceMerge(1);+ } catch (Throwable t) {+ dir.failure.compareAndSet(null, t);+ }+ },+ "forceMerge");+ merger.start();+ dir.mergeStarted.await();++ // Many updates to doc 3 (in the first, merging segment), each resolved separately so it becomes+ // a+ // new overlay generation; well past maxDocValuesOverlays, so folds happen mid-merge.+ long finalValue = -1;+ for (int v = 0; v < 20; v++) {+ finalValue = 1000L + v;+ writer.updateNumericDocValue(new Term("id", "3"), "ndv", finalValue);+ try (DirectoryReader r = DirectoryReader.open(writer)) {+ assertNotNull(r);+ }+ }++ dir.resumeMerge.countDown();+ merger.join();+ assertNull("forceMerge failed: " + dir.failure.get(), dir.failure.get());++ try (DirectoryReader reader = DirectoryReader.open(writer)) {+ assertEquals(1, reader.leaves().size());+ NumericDocValues ndv = reader.leaves().get(0).reader().getNumericDocValues("ndv");+ java.util.Set<Long> values = new java.util.HashSet<>();+ for (int d = 0; d < reader.leaves().get(0).reader().maxDoc(); d++) {+ assertTrue(ndv.advanceExact(d));+ values.add(ndv.longValue());+ }+ assertTrue("folded final value " + finalValue + " missing", values.contains(finalValue));+ }++ writer.close();+ dir.close();+ }++ /**+ * A soft-delete that resolves onto a segment while it is being merged must be carried over, so+ * the old document is not live on the merged segment. The soft-deletes field is a doc-values+ * update that need not exist on disk yet, exercising the carry-over of pending updates to a field+ * absent from the source reader.+ */+ public void testSoftDeleteResolvedDuringMergeIsCarriedOver() throws Exception {+ MergePausingDirectory dir = new MergePausingDirectory(newDirectory());+ IndexWriterConfig conf =+ newIndexWriterConfig(new MockAnalyzer(random()))+ .setMergeScheduler(new ConcurrentMergeScheduler())+ .setSoftDeletesField("__soft");+ IndexWriter writer = new IndexWriter(dir, conf);+ for (int i = 0; i < 4; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();+ for (int i = 4; i < 8; i++) {+ writer.addDocument(doc(i, i, "v" + i));+ }+ writer.commit();++ Thread merger =+ new Thread(+ () -> {+ try {+ writer.forceMerge(1);+ } catch (Throwable t) {+ dir.failure.compareAndSet(null, t);+ }+ },+ "forceMerge");+ merger.start();+ dir.mergeStarted.await();++ // Replace doc id=1 (in the merging segment): adds a new doc and soft-deletes the old one via a+ // doc-values update to the soft-deletes field. Resolve it onto the merging segment.+ Document replacement = doc(1, 101, "updated-1");+ writer.softUpdateDocument(+ new Term("id", "1"), replacement, new NumericDocValuesField("__soft", 1));+ try (DirectoryReader r = DirectoryReader.open(writer)) {+ assertNotNull(r);+ }++ dir.resumeMerge.countDown();+ merger.join();+ assertNull("forceMerge failed: " + dir.failure.get(), dir.failure.get());++ // Exactly one doc with id=1 must be live (the replacement); the old one must be soft-deleted.+ try (DirectoryReader reader = DirectoryReader.open(writer)) {+ int live = 0;+ for (LeafReaderContext ctx : reader.leaves()) {+ LeafReader leaf = ctx.reader();+ NumericDocValues ndv = leaf.getNumericDocValues("ndv");+ Bits liveDocs = leaf.getLiveDocs();+ for (int d = 0; d < leaf.maxDoc(); d++) {+ if (liveDocs != null && liveDocs.get(d) == false) {+ continue;+ }+ assertTrue(ndv.advanceExact(d));+ if (ndv.longValue() == 101) {+ live++;+ }+ }+ }+ assertEquals("exactly one live replacement doc expected", 1, live);+ }++ writer.close();+ dir.close();+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestReaderPool.java b/lucene/core/src/test/org/apache/lucene/index/TestReaderPool.javaindex 2635be05bb9..048d6375372 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestReaderPool.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestReaderPool.java@@ -156,22 +156,17 @@ public class TestReaderPool extends LuceneTestCase {if (pool.isReaderPoolingEnabled()) {if (random().nextBoolean()) {writtenToDisk = pool.writeAllDocValuesUpdates();- assertFalse(readersAndUpdates.isMerging());} else if (random().nextBoolean()) {writtenToDisk = pool.commit(segmentInfos);- assertFalse(readersAndUpdates.isMerging());} else {writtenToDisk = pool.writeDocValuesUpdatesForMerge(Collections.singletonList(commitInfo));- assertTrue(readersAndUpdates.isMerging());}assertFalse(pool.release(readersAndUpdates, random().nextBoolean()));} else {if (random().nextBoolean()) {writtenToDisk = pool.release(readersAndUpdates, random().nextBoolean());- assertFalse(readersAndUpdates.isMerging());} else {writtenToDisk = pool.writeDocValuesUpdatesForMerge(Collections.singletonList(commitInfo));- assertTrue(readersAndUpdates.isMerging());assertFalse(pool.release(readersAndUpdates, random().nextBoolean()));}}