Apache Solr open-source search software
LUCENE-6849: expose IndexWriter.flush to move in-memory segments to disk
Michael McCandless84ede2378cfd
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 331c65e9f65..7bfe534233a 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -154,6 +154,10 @@ API Changes* LUCENE-6884: Analyzer.tokenStream() and Tokenizer.setReader() are no longerdeclared as throwing IOException. (Alan Woodward)+* LUCENE-6849: Expose IndexWriter.flush() method, to move all+ in-memory segments to disk without opening a near-real-time reader+ nor calling fsync (Robert Muir, Simon Willnauer, Mike McCandless)+Optimizations* LUCENE-6708: TopFieldCollector does not compute the score several times on thediff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 09a67aff954..523ac1821f3 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -3045,6 +3045,12 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {return Thread.holdsLock(fullFlushLock);}+ /** Moves all in-memory segments to the {@link Directory}, but does not commit+ * (fsync) them (call {@link #commit} for that). */+ public final void flush() throws IOException {+ flush(true, true);+ }+/*** Flush all in-memory buffered updates (adds and deletes)* to the Directory.@@ -3052,8 +3058,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {* deletes or docs were flushed) if necessary* @param applyAllDeletes whether pending deletes should also*/- // why protected- protected final void flush(boolean triggerMerge, boolean applyAllDeletes) throws IOException {+ final void flush(boolean triggerMerge, boolean applyAllDeletes) throws IOException {// NOTE: this method cannot be sync'd because// maybeMerge() in turn calls mergeScheduler.merge whichdiff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 8d121d70996..39eada26dae 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -522,8 +522,9 @@ public class TestIndexWriter extends LuceneTestCase {customType.setStoreTermVectorPositions(true);customType.setStoreTermVectorOffsets(true);doc.add(newField("field", "aaa", customType));- for(int i=0;i<19;i++)+ for(int i=0;i<19;i++) {writer.addDocument(doc);+ }writer.flush(false, true);writer.close();SegmentInfos sis = SegmentInfos.readLatestCommit(dir);@@ -533,6 +534,25 @@ public class TestIndexWriter extends LuceneTestCase {dir.close();}+ public void testFlushWithNoCommit() throws IOException {+ Directory dir = newDirectory();+ IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));+ IndexWriter writer = new IndexWriter(dir, iwc);+ Document doc = new Document();+ writer.addDocument(doc);+ writer.commit();++ writer.addDocument(doc);+ writer.flush();+ DirectoryReader r = DirectoryReader.open(dir);+ assertEquals(1, r.maxDoc());+ writer.commit();+ DirectoryReader r2 = DirectoryReader.openIfChanged(r);+ assertNotNull(r2);+ assertEquals(2, r2.maxDoc());+ IOUtils.close(r2, r, writer, dir);+ }+// Make sure we can flush segment w/ norms, then add// empty doc (no norms) and flushpublic void testEmptyDocAfterFlushingRealDoc() throws IOException {diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java b/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.javaindex 4be5aa6cf8c..ba51b4c9a03 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/RandomIndexWriter.java@@ -24,7 +24,6 @@ import java.util.Random;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.MockAnalyzer;-import org.apache.lucene.codecs.Codec;import org.apache.lucene.document.Field;import org.apache.lucene.search.Query;import org.apache.lucene.store.Directory;@@ -49,7 +48,6 @@ public class RandomIndexWriter implements Closeable {int flushAt;private double flushAtFactor = 1.0;private boolean getReaderCalled;- private final Codec codec; // sugarprivate final Analyzer analyzer; // only if WE created it (then we close it)/** Returns an indexwriter that randomly mixes up thread scheduling (by yielding at test points) */@@ -120,7 +118,6 @@ public class RandomIndexWriter implements Closeable {} else {analyzer = null;}- codec = w.getConfig().getCodec();if (LuceneTestCase.VERBOSE) {System.out.println("RIW dir=" + dir);}@@ -173,16 +170,23 @@ public class RandomIndexWriter implements Closeable {w.addDocument(doc);}- maybeCommit();+ maybeFlushOrCommit();}- private void maybeCommit() throws IOException {+ private void maybeFlushOrCommit() throws IOException {LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, w.getConfig());if (docCount++ == flushAt) {- if (LuceneTestCase.VERBOSE) {- System.out.println("RIW.add/updateDocument: now doing a commit at docCount=" + docCount);+ if (r.nextBoolean()) {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("RIW.add/updateDocument: now doing a flush at docCount=" + docCount);+ }+ w.flush();+ } else {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("RIW.add/updateDocument: now doing a commit at docCount=" + docCount);+ }+ w.commit();}- w.commit();flushAt += TestUtil.nextInt(r, (int) (flushAtFactor * 10), (int) (flushAtFactor * 1000));if (flushAtFactor < 2e6) {// gradually but exponentially increase time b/w flushes@@ -194,13 +198,13 @@ public class RandomIndexWriter implements Closeable {public void addDocuments(Iterable<? extends IndexDocument> docs) throws IOException {LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, w.getConfig());w.addDocuments(docs);- maybeCommit();+ maybeFlushOrCommit();}public void updateDocuments(Term delTerm, Iterable<? extends IndexDocument> docs) throws IOException {LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, w.getConfig());w.updateDocuments(delTerm, docs);- maybeCommit();+ maybeFlushOrCommit();}/**@@ -241,7 +245,7 @@ public class RandomIndexWriter implements Closeable {} else {w.updateDocument(t, doc);}- maybeCommit();+ maybeFlushOrCommit();}public void addIndexes(Directory... dirs) throws IOException {@@ -447,6 +451,11 @@ public class RandomIndexWriter implements Closeable {}}+ /** Writes all in-memory segments to the {@link Directory}. */+ public final void flush() throws IOException {+ w.flush();+ }+/*** Simple interface that is executed for each <tt>TP</tt> {@link InfoStream} component* message. See also {@link RandomIndexWriter#mockIndexWriter(Random, Directory, IndexWriterConfig, TestPoint)}