Skip to content

Apache Lucene open-source search software

Fix segmentInfos replace doesn't set userData (#12626)

Shibi Balamurugan6db09131d6dd
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a66d86a5ae5..8341d772d77 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -323,6 +323,8 @@ Bug Fixes
* GITHUB#12640: Ensure #finish is called on all drill-sideways collectors even if one throws a
CollectionTerminatedException (Greg Miller)
+* GITHUB#12626: Fix segmentInfos replace to set userData (Shibi Balamurugan, Uwe Schindler, Marcus Eagan, Michael Froh)
+
Build
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
index d8e3d622b50..7caf2dbda8c 100644
--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -1010,6 +1010,7 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
void replace(SegmentInfos other) {
rollbackSegmentInfos(other.asList());
lastGeneration = other.lastGeneration;
+ userData = other.userData;
}
/** Returns sum of all segment's maxDocs. Note that this does not include deletions */
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
index 5c0303d9f9e..364f1cc3720 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -118,7 +118,6 @@ import org.apache.lucene.util.automaton.Automata;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
import org.junit.Ignore;
-import org.junit.Test;
public class TestIndexWriter extends LuceneTestCase {
@@ -2042,7 +2041,6 @@ public class TestIndexWriter extends LuceneTestCase {
return data;
}
- @Test
public void testGetCommitData() throws Exception {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(null));
@@ -2063,6 +2061,47 @@ public class TestIndexWriter extends LuceneTestCase {
dir.close();
}
+ public void testGetCommitDataFromOldSnapshot() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter writer = new IndexWriter(dir, newSnapshotIndexWriterConfig(null));
+ writer.setLiveCommitData(
+ new HashMap<String, String>() {
+ {
+ put("key", "value");
+ }
+ }.entrySet());
+ assertEquals("value", getLiveCommitData(writer).get("key"));
+ writer.commit();
+ // Snapshot this commit to open later
+ IndexCommit indexCommit =
+ ((SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy()).snapshot();
+ writer.close();
+
+ // Modify the commit data and commit on close so the most recent commit data is different
+ writer = new IndexWriter(dir, newSnapshotIndexWriterConfig(null));
+ writer.setLiveCommitData(
+ new HashMap<String, String>() {
+ {
+ put("key", "value2");
+ }
+ }.entrySet());
+ assertEquals("value2", getLiveCommitData(writer).get("key"));
+ writer.close();
+
+ // validate that when opening writer from older snapshotted index commit, the old commit data is
+ // visible
+ writer =
+ new IndexWriter(
+ dir,
+ newSnapshotIndexWriterConfig(null)
+ .setOpenMode(OpenMode.APPEND)
+ .setIndexCommit(indexCommit));
+ assertEquals("value", getLiveCommitData(writer).get("key"));
+ writer.close();
+
+ dir.close();
+ }
+
public void testNullAnalyzer() throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwConf = newIndexWriterConfig(null);
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
index 5b114ff9a49..7b6dd064660 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
@@ -133,6 +133,7 @@ import org.apache.lucene.index.MergeScheduler;
import org.apache.lucene.index.MultiBits;
import org.apache.lucene.index.MultiDocValues;
import org.apache.lucene.index.MultiTerms;
+import org.apache.lucene.index.NoDeletionPolicy;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.ParallelCompositeReader;
import org.apache.lucene.index.ParallelLeafReader;
@@ -141,6 +142,7 @@ import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.SerialMergeScheduler;
import org.apache.lucene.index.SimpleMergedSegmentWarmer;
+import org.apache.lucene.index.SnapshotDeletionPolicy;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.SortedSetDocValues;
@@ -930,6 +932,13 @@ public abstract class LuceneTestCase extends Assert {
dumpIterator(label, iter, stream);
}
+ /** create a new index writer config with a snapshot deletion policy */
+ public static IndexWriterConfig newSnapshotIndexWriterConfig(Analyzer analyzer) {
+ IndexWriterConfig c = newIndexWriterConfig(analyzer);
+ c.setIndexDeletionPolicy(new SnapshotDeletionPolicy(NoDeletionPolicy.INSTANCE));
+ return c;
+ }
+
/** create a new index writer config with random defaults */
public static IndexWriterConfig newIndexWriterConfig() {
return newIndexWriterConfig(new MockAnalyzer(random()));