LUCENE-6505: NRT readers now reflect prior commit metadata

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1682296 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2015-05-28 18:11:48 +00:00
parent 40f3361338
commit 9a22532467
5 changed files with 116 additions and 2 deletions

View File

@ -218,6 +218,9 @@ Bug Fixes
documents with no payloads and now returns an empty BytesRef instead
(Marius Grama via Michael McCandless)
* LUCENE-6505: NRT readers now reflect segments_N filename and commit
user data from previous commits (Mike McCandless)
API Changes
* LUCENE-6377: SearcherFactory#newSearcher now accepts the previous reader

View File

@ -2880,10 +2880,17 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {
// we committed, if anything goes wrong after this, we are screwed and it's a tragedy:
commitCompleted = true;
if (infoStream.isEnabled("IW")) {
infoStream.message("IW", "commit: done writing segments file \"" + committedSegmentsFileName + "\"");
}
// NOTE: don't use this.checkpoint() here, because
// we do not want to increment changeCount:
deleter.checkpoint(pendingCommit, true);
// Carry over generation to our master SegmentInfos:
segmentInfos.updateGeneration(pendingCommit);
lastCommitChangeCount = pendingCommitChangeCount;
rollbackSegments = pendingCommit.createBackupSegmentInfos();
@ -2922,7 +2929,6 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {
}
if (infoStream.isEnabled("IW")) {
infoStream.message("IW", "commit: wrote segments file \"" + committedSegmentsFileName + "\"");
infoStream.message("IW", String.format(Locale.ROOT, "commit: took %.1f msec", (System.nanoTime()-startCommitTime)/1000000.0));
infoStream.message("IW", "commit: done");
}
@ -4297,6 +4303,10 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {
// (this method unwinds everything it did on
// an exception)
toSync.prepareCommit(directory);
if (infoStream.isEnabled("IW")) {
infoStream.message("IW", "startCommit: wrote pending segments file \"" + IndexFileNames.fileNameFromGeneration(IndexFileNames.PENDING_SEGMENTS, "", toSync.getGeneration()) + "\"");
}
//System.out.println("DONE prepareCommit");
pendingCommitSet = true;

View File

@ -757,6 +757,8 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
} else {
userData = data;
}
changed();
}
/** Replaces all segments in this instance, but keeps

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
@ -2721,5 +2722,103 @@ public class TestIndexWriter extends LuceneTestCase {
r.close();
dir.close();
}
// LUCENE-6505
public void testNRTSegmentsFile() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
// creates segments_1
w.commit();
// newly opened NRT reader should see gen=1 segments file
DirectoryReader r = DirectoryReader.open(w, true);
assertEquals(1, r.getIndexCommit().getGeneration());
assertEquals("segments_1", r.getIndexCommit().getSegmentsFileName());
// newly opened non-NRT reader should see gen=1 segments file
DirectoryReader r2 = DirectoryReader.open(dir);
assertEquals(1, r2.getIndexCommit().getGeneration());
assertEquals("segments_1", r2.getIndexCommit().getSegmentsFileName());
r2.close();
// make a change and another commit
w.addDocument(new Document());
w.commit();
DirectoryReader r3 = DirectoryReader.openIfChanged(r);
r.close();
assertNotNull(r3);
// reopened NRT reader should see gen=2 segments file
assertEquals(2, r3.getIndexCommit().getGeneration());
assertEquals("segments_2", r3.getIndexCommit().getSegmentsFileName());
r3.close();
// newly opened non-NRT reader should see gen=2 segments file
DirectoryReader r4 = DirectoryReader.open(dir);
assertEquals(2, r4.getIndexCommit().getGeneration());
assertEquals("segments_2", r4.getIndexCommit().getSegmentsFileName());
r4.close();
w.close();
dir.close();
}
// LUCENE-6505
public void testNRTAfterCommit() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
w.commit();
w.addDocument(new Document());
DirectoryReader r = DirectoryReader.open(w, true);
w.commit();
// commit even with no other changes counts as a "change" that NRT reader reopen will see:
DirectoryReader r2 = DirectoryReader.open(dir);
assertNotNull(r2);
assertEquals(2, r2.getIndexCommit().getGeneration());
assertEquals("segments_2", r2.getIndexCommit().getSegmentsFileName());
IOUtils.close(r, r2, w, dir);
}
// LUCENE-6505
public void testNRTAfterSetUserDataWithoutCommit() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
w.commit();
DirectoryReader r = DirectoryReader.open(w, true);
Map<String,String> m = new HashMap<>();
m.put("foo", "bar");
w.setCommitData(m);
// setCommitData with no other changes should count as an NRT change:
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
IOUtils.close(r2, r, w, dir);
}
// LUCENE-6505
public void testNRTAfterSetUserDataWithCommit() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
w.commit();
DirectoryReader r = DirectoryReader.open(w, true);
Map<String,String> m = new HashMap<>();
m.put("foo", "bar");
w.setCommitData(m);
w.commit();
// setCommitData and also commit, with no other changes, should count as an NRT change:
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
IOUtils.close(r, r2, w, dir);
}
}

View File

@ -117,7 +117,7 @@ public class TestSearcherReuse extends SolrTestCaseJ4 {
assertSearcherHasNotChanged(expectedSearcher);
assertU(delI("0")); // no doc has this id, yet
assertU(commit());
assertU(commit("softCommit","true"));
assertSearcherHasNotChanged(expectedSearcher);
} finally {