You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2013/05/02 18:44:37 UTC
svn commit: r1478438 - in /lucene/dev/trunk/lucene: ./
core/src/java/org/apache/lucene/index/
core/src/java/org/apache/lucene/search/
core/src/test/org/apache/lucene/search/
Author: mikemccand
Date: Thu May 2 16:44:36 2013
New Revision: 1478438
URL: http://svn.apache.org/r1478438
Log:
LUCENE-4967: move NRTManager entirely into a reopen thread so it can interact with any ReferenceManager
Added:
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/TrackingIndexWriter.java (with props)
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/ControlledRealTimeReopenThread.java (with props)
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java
- copied, changed from r1476786, lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestNRTManager.java
Removed:
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/NRTManager.java
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/NRTManagerReopenThread.java
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestNRTManager.java
Modified:
lucene/dev/trunk/lucene/CHANGES.txt
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherFactory.java
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestLiveFieldValues.java
Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1478438&r1=1478437&r2=1478438&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Thu May 2 16:44:36 2013
@@ -47,6 +47,11 @@ Changes in backwards compatibility polic
(a, ab, b, bc, c) instead of (a, b, c, ab, bc) and doesn't trim trailing
whitespaces. (Adrien Grand)
+* LUCENE-4967: NRTManager is replaced by
+ ControlledRealTimeReopenThread, for controlling which requests must
+ see which indexing changes, so that it can work with any
+ ReferenceManager (Mike McCandless)
+
Bug Fixes
* LUCENE-4935: CustomScoreQuery wrongly applied its query boost twice
Added: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/TrackingIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/TrackingIndexWriter.java?rev=1478438&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/TrackingIndexWriter.java (added)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/index/TrackingIndexWriter.java Thu May 2 16:44:36 2013
@@ -0,0 +1,204 @@
+package org.apache.lucene.index;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.search.ControlledRealTimeReopenThread; // javadocs
+import org.apache.lucene.search.Query;
+import org.apache.lucene.store.Directory;
+
+/** Class that tracks changes to a delegated
+ * IndexWriter, used by {@link
+ * ControlledRealTimeReopenThread} to ensure specific
+ * changes are visible. Create this class (passing your
+ * IndexWriter), and then pass this class to {@link
+ * ControlledRealTimeReopenThread}.
+ * Be sure to make all changes via the
+ * TrackingIndexWriter, otherwise {@link
+ * ControlledRealTimeReopenThread} won't know about the changes.
+ *
+ * @lucene.experimental */
+
+public class TrackingIndexWriter {
+ private final IndexWriter writer;
+ private final AtomicLong indexingGen = new AtomicLong(1);
+
+ /** Create a {@code TrackingIndexWriter} wrapping the
+ * provided {@link IndexWriter}. */
+ public TrackingIndexWriter(IndexWriter writer) {
+ this.writer = writer;
+ }
+
+ /** Calls {@link
+ * IndexWriter#updateDocument(Term,IndexDocument,Analyzer)}
+ * and returns the generation that reflects this change. */
+ public long updateDocument(Term t, IndexDocument d, Analyzer a) throws IOException {
+ writer.updateDocument(t, d, a);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link
+ * IndexWriter#updateDocument(Term,IndexDocument)} and
+ * returns the generation that reflects this change. */
+ public long updateDocument(Term t, IndexDocument d) throws IOException {
+ writer.updateDocument(t, d);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link
+ * IndexWriter#updateDocuments(Term,Iterable,Analyzer)}
+ * and returns the generation that reflects this change. */
+ public long updateDocuments(Term t, Iterable<? extends IndexDocument> docs, Analyzer a) throws IOException {
+ writer.updateDocuments(t, docs, a);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link
+ * IndexWriter#updateDocuments(Term,Iterable)} and returns
+ * the generation that reflects this change. */
+ public long updateDocuments(Term t, Iterable<? extends IndexDocument> docs) throws IOException {
+ writer.updateDocuments(t, docs);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#deleteDocuments(Term)} and
+ * returns the generation that reflects this change. */
+ public long deleteDocuments(Term t) throws IOException {
+ writer.deleteDocuments(t);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#deleteDocuments(Term...)} and
+ * returns the generation that reflects this change. */
+ public long deleteDocuments(Term... terms) throws IOException {
+ writer.deleteDocuments(terms);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#deleteDocuments(Query)} and
+ * returns the generation that reflects this change. */
+ public long deleteDocuments(Query q) throws IOException {
+ writer.deleteDocuments(q);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#deleteDocuments(Query...)}
+ * and returns the generation that reflects this change. */
+ public long deleteDocuments(Query... queries) throws IOException {
+ writer.deleteDocuments(queries);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#deleteAll} and returns the
+ * generation that reflects this change. */
+ public long deleteAll() throws IOException {
+ writer.deleteAll();
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link
+ * IndexWriter#addDocument(IndexDocument,Analyzer)} and
+ * returns the generation that reflects this change. */
+ public long addDocument(IndexDocument d, Analyzer a) throws IOException {
+ writer.addDocument(d, a);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link
+ * IndexWriter#addDocuments(Iterable,Analyzer)} and
+ * returns the generation that reflects this change. */
+ public long addDocuments(Iterable<? extends IndexDocument> docs, Analyzer a) throws IOException {
+ writer.addDocuments(docs, a);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#addDocument(IndexDocument)}
+ * and returns the generation that reflects this change. */
+ public long addDocument(IndexDocument d) throws IOException {
+ writer.addDocument(d);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#addDocuments(Iterable)} and
+ * returns the generation that reflects this change. */
+ public long addDocuments(Iterable<? extends IndexDocument> docs) throws IOException {
+ writer.addDocuments(docs);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#addIndexes(Directory...)} and
+ * returns the generation that reflects this change. */
+ public long addIndexes(Directory... dirs) throws IOException {
+ writer.addIndexes(dirs);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Calls {@link IndexWriter#addIndexes(IndexReader...)}
+ * and returns the generation that reflects this change. */
+ public long addIndexes(IndexReader... readers) throws IOException {
+ writer.addIndexes(readers);
+ // Return gen as of when indexing finished:
+ return indexingGen.get();
+ }
+
+ /** Return the current generation being indexed. */
+ public long getGeneration() {
+ return indexingGen.get();
+ }
+
+ /** Return the wrapped {@link IndexWriter}. */
+ public IndexWriter getIndexWriter() {
+ return writer;
+ }
+
+ /** Return and increment current gen.
+ *
+ * @lucene.internal */
+ public long getAndIncrementGeneration() {
+ return indexingGen.getAndIncrement();
+ }
+
+ /** Cals {@link
+ * IndexWriter#tryDeleteDocument(IndexReader,int)} and
+ * returns the generation that reflects this change. */
+ public long tryDeleteDocument(IndexReader reader, int docID) throws IOException {
+ if (writer.tryDeleteDocument(reader, docID)) {
+ return indexingGen.get();
+ } else {
+ return -1;
+ }
+ }
+}
+
Added: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/ControlledRealTimeReopenThread.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/ControlledRealTimeReopenThread.java?rev=1478438&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/ControlledRealTimeReopenThread.java (added)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/ControlledRealTimeReopenThread.java Thu May 2 16:44:36 2013
@@ -0,0 +1,247 @@
+package org.apache.lucene.search;
+
+/*
+ * 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.
+ */
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.TrackingIndexWriter;
+import org.apache.lucene.util.ThreadInterruptedException;
+
+/** Utility class that runs a thread to manage periodicc
+ * reopens of a {@link ReferenceManager}, with methods to wait for a specific
+ * index changes to become visible. To use this class you
+ * must first wrap your {@link IndexWriter} with a {@link
+ * TrackingIndexWriter} and always use it to make changes
+ * to the index, saving the returned generation. Then,
+ * when a given search request needs to see a specific
+ * index change, call the {#waitForGeneration} to wait for
+ * that change to be visible. Note that this will only
+ * scale well if most searches do not need to wait for a
+ * specific index generation.
+ *
+ * @lucene.experimental */
+
+public class ControlledRealTimeReopenThread<T> extends Thread implements Closeable {
+ private final ReferenceManager<T> manager;
+ private final long targetMaxStaleNS;
+ private final long targetMinStaleNS;
+ private final TrackingIndexWriter writer;
+ private volatile boolean finish;
+ private volatile long waitingGen;
+ private volatile long searchingGen;
+ private long refreshStartGen;
+
+ private final ReentrantLock reopenLock = new ReentrantLock();
+ private final Condition reopenCond = reopenLock.newCondition();
+
+ /**
+ * Create ControlledRealTimeReopenThread, to periodically
+ * reopen the a {@link ReferenceManager}.
+ *
+ * @param targetMaxStaleSec Maximum time until a new
+ * reader must be opened; this sets the upper bound
+ * on how slowly reopens may occur, when no
+ * caller is waiting for a specific generation to
+ * become visible.
+ *
+ * @param targetMinStaleSec Mininum time until a new
+ * reader can be opened; this sets the lower bound
+ * on how quickly reopens may occur, when a caller
+ * is waiting for a specific generation to
+ * become visible.
+ */
+ public ControlledRealTimeReopenThread(TrackingIndexWriter writer, ReferenceManager<T> manager, double targetMaxStaleSec, double targetMinStaleSec) {
+ if (targetMaxStaleSec < targetMinStaleSec) {
+ throw new IllegalArgumentException("targetMaxScaleSec (= " + targetMaxStaleSec + ") < targetMinStaleSec (=" + targetMinStaleSec + ")");
+ }
+ this.writer = writer;
+ this.manager = manager;
+ this.targetMaxStaleNS = (long) (1000000000*targetMaxStaleSec);
+ this.targetMinStaleNS = (long) (1000000000*targetMinStaleSec);
+ manager.addListener(new HandleRefresh());
+ }
+
+ private class HandleRefresh implements ReferenceManager.RefreshListener {
+ @Override
+ public void beforeRefresh() {
+ }
+
+ @Override
+ public void afterRefresh(boolean didRefresh) {
+ refreshDone(didRefresh);
+ }
+ }
+
+ private synchronized void refreshDone(boolean didRefresh) {
+ searchingGen = refreshStartGen;
+ notifyAll();
+ }
+
+ @Override
+ public synchronized void close() {
+ //System.out.println("NRT: set finish");
+
+ finish = true;
+
+ // So thread wakes up and notices it should finish:
+ reopenLock.lock();
+ try {
+ reopenCond.signal();
+ } finally {
+ reopenLock.unlock();
+ }
+
+ try {
+ join();
+ } catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
+ }
+
+ // Max it out so any waiting search threads will return:
+ searchingGen = Long.MAX_VALUE;
+ notifyAll();
+ }
+
+ /**
+ * Waits for the target generation to become visible in
+ * the searcher.
+ * If the current searcher is older than the
+ * target generation, this method will block
+ * until the searcher is reopened, by another via
+ * {@link ReferenceManager#maybeRefresh} or until the {@link ReferenceManager} is closed.
+ *
+ * @param targetGen the generation to wait for
+ */
+ public void waitForGeneration(long targetGen) throws InterruptedException {
+ waitForGeneration(targetGen, -1);
+ }
+
+ /**
+ * Waits for the target generation to become visible in
+ * the searcher, up to a maximum specified milli-seconds.
+ * If the current searcher is older than the target
+ * generation, this method will block until the
+ * searcher has been reopened by another thread via
+ * {@link ReferenceManager#maybeRefresh}, the given waiting time has elapsed, or until
+ * the {@link ReferenceManager} is closed.
+ * <p>
+ * NOTE: if the waiting time elapses before the requested target generation is
+ * available the current {@link SearcherManager} is returned instead.
+ *
+ * @param targetGen
+ * the generation to wait for
+ * @param maxMS
+ * maximum milliseconds to wait, or -1 to wait indefinitely
+ * @return true if the targetGeneration is now available,
+ * or false if maxMS wait time was exceeded
+ */
+ public synchronized boolean waitForGeneration(long targetGen, int maxMS) throws InterruptedException {
+ final long curGen = writer.getGeneration();
+ if (targetGen > curGen) {
+ throw new IllegalArgumentException("targetGen=" + targetGen + " was never returned by the ReferenceManager instance (current gen=" + curGen + ")");
+ }
+ if (targetGen > searchingGen) {
+ waitingGen = Math.max(waitingGen, targetGen);
+
+ // Notify the reopen thread that the waitingGen has
+ // changed, so it may wake up and realize it should
+ // not sleep for much or any longer before reopening:
+ reopenLock.lock();
+ try {
+ reopenCond.signal();
+ } finally {
+ reopenLock.unlock();
+ }
+
+ long startMS = System.nanoTime()/1000000;
+
+ while (targetGen > searchingGen) {
+ if (maxMS < 0) {
+ wait();
+ } else {
+ long msLeft = ((startMS + maxMS) - (System.nanoTime())/1000000);
+ if (msLeft <= 0) {
+ return false;
+ } else {
+ wait(msLeft);
+ }
+ }
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void run() {
+ // TODO: maybe use private thread ticktock timer, in
+ // case clock shift messes up nanoTime?
+ long lastReopenStartNS = System.nanoTime();
+
+ //System.out.println("reopen: start");
+ while (!finish) {
+
+ // TODO: try to guestimate how long reopen might
+ // take based on past data?
+
+ // Loop until we've waiting long enough before the
+ // next reopen:
+ while (!finish) {
+
+ // True if we have someone waiting for reopened searcher:
+ boolean hasWaiting = waitingGen > searchingGen;
+ final long nextReopenStartNS = lastReopenStartNS + (hasWaiting ? targetMinStaleNS : targetMaxStaleNS);
+
+ final long sleepNS = nextReopenStartNS - System.nanoTime();
+
+ if (sleepNS > 0) {
+ reopenLock.lock();
+ try {
+ reopenCond.awaitNanos(sleepNS);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ return;
+ } finally {
+ reopenLock.unlock();
+ }
+ } else {
+ break;
+ }
+ }
+
+ if (finish) {
+ break;
+ }
+
+ lastReopenStartNS = System.nanoTime();
+ // Save the gen as of when we started the reopen; the
+ // listener (HandleRefresh above) copies this to
+ // searchingGen once the reopen completes:
+ refreshStartGen = writer.getAndIncrementGeneration();
+ try {
+ manager.maybeRefreshBlocking();
+ } catch (IOException ioe) {
+ throw new RuntimeException(ioe);
+ }
+ }
+ }
+}
Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherFactory.java?rev=1478438&r1=1478437&r2=1478438&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherFactory.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherFactory.java Thu May 2 16:44:36 2013
@@ -26,7 +26,7 @@ import org.apache.lucene.index.IndexWrit
import org.apache.lucene.search.similarities.Similarity; // javadocs
/**
- * Factory class used by {@link SearcherManager} and {@link NRTManager} to
+ * Factory class used by {@link SearcherManager} to
* create new IndexSearchers. The default implementation just creates
* an IndexSearcher with no custom behavior:
*
Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java?rev=1478438&r1=1478437&r2=1478438&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java Thu May 2 16:44:36 2013
@@ -24,7 +24,6 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
-import org.apache.lucene.search.NRTManager; // javadocs
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.IOUtils;
@@ -41,8 +40,8 @@ import org.apache.lucene.util.IOUtils;
*
* Per search-request, if it's a "new" search request, then
* obtain the latest searcher you have (for example, by
- * using {@link SearcherManager} or {@link NRTManager}), and
- * then record this searcher:
+ * using {@link SearcherManager}), and then record this
+ * searcher:
*
* <pre class="prettyprint">
* // Record the current searcher, and save the returend
@@ -143,8 +142,7 @@ public class SearcherLifetimeManager imp
/** Records that you are now using this IndexSearcher.
* Always call this when you've obtained a possibly new
- * {@link IndexSearcher}, for example from one of the
- * <code>get</code> methods in {@link NRTManager} or {@link
+ * {@link IndexSearcher}, for example from {@link
* SearcherManager}. It's fine if you already passed the
* same searcher to this method before.
*
Copied: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java (from r1476786, lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestNRTManager.java)
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java?p2=lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java&p1=lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestNRTManager.java&r1=1476786&r2=1478438&rev=1478438&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestNRTManager.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestControlledRealTimeReopenThread.java Thu May 2 16:44:36 2013
@@ -36,21 +36,33 @@ import org.apache.lucene.index.NoMergePo
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.ThreadedIndexingAndSearchingTestCase;
+import org.apache.lucene.index.TrackingIndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NRTCachingDirectory;
import org.apache.lucene.util.IOUtils;
-import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
+import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.ThreadInterruptedException;
@SuppressCodecs({ "SimpleText", "Memory", "Direct" })
-public class TestNRTManager extends ThreadedIndexingAndSearchingTestCase {
+public class TestControlledRealTimeReopenThread extends ThreadedIndexingAndSearchingTestCase {
+
+ // Not guaranteed to reflect deletes:
+ private SearcherManager nrtNoDeletes;
+
+ // Is guaranteed to reflect deletes:
+ private SearcherManager nrtDeletes;
+
+ private TrackingIndexWriter genWriter;
+
+ private ControlledRealTimeReopenThread<IndexSearcher> nrtDeletesThread;
+ private ControlledRealTimeReopenThread<IndexSearcher> nrtNoDeletesThread;
private final ThreadLocal<Long> lastGens = new ThreadLocal<Long>();
private boolean warmCalled;
- public void testNRTManager() throws Exception {
- runTest("TestNRTManager");
+ public void testControlledRealTimeReopenThread() throws Exception {
+ runTest("TestControlledRealTimeReopenThread");
}
@Override
@@ -58,7 +70,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println("TEST: finalSearcher maxGen=" + maxGen);
}
- nrtDeletes.waitForGeneration(maxGen);
+ nrtDeletesThread.waitForGeneration(maxGen);
return nrtDeletes.acquire();
}
@@ -85,7 +97,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: verify " + id);
}
- nrtDeletes.waitForGeneration(gen);
+ nrtDeletesThread.waitForGeneration(gen);
final IndexSearcher s = nrtDeletes.acquire();
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: got searcher=" + s);
@@ -108,7 +120,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: verify " + id);
}
- nrtNoDeletes.waitForGeneration(gen);
+ nrtNoDeletesThread.waitForGeneration(gen);
final IndexSearcher s = nrtNoDeletes.acquire();
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: got searcher=" + s);
@@ -131,7 +143,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: verify " + id);
}
- nrtNoDeletes.waitForGeneration(gen);
+ nrtNoDeletesThread.waitForGeneration(gen);
final IndexSearcher s = nrtNoDeletes.acquire();
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: got searcher=" + s);
@@ -153,7 +165,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: verify " + id);
}
- nrtDeletes.waitForGeneration(gen);
+ nrtDeletesThread.waitForGeneration(gen);
final IndexSearcher s = nrtDeletes.acquire();
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: got searcher=" + s);
@@ -175,7 +187,7 @@ public class TestNRTManager extends Thre
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: verify del " + id);
}
- nrtDeletes.waitForGeneration(gen);
+ nrtDeletesThread.waitForGeneration(gen);
final IndexSearcher s = nrtDeletes.acquire();
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": nrt: got searcher=" + s);
@@ -189,48 +201,37 @@ public class TestNRTManager extends Thre
lastGens.set(gen);
}
- // Not guaranteed to reflect deletes:
- private NRTManager nrtNoDeletes;
-
- // Is guaranteed to reflect deletes:
- private NRTManager nrtDeletes;
-
- private NRTManager.TrackingIndexWriter genWriter;
-
- private NRTManagerReopenThread nrtDeletesThread;
- private NRTManagerReopenThread nrtNoDeletesThread;
-
@Override
protected void doAfterWriter(final ExecutorService es) throws Exception {
final double minReopenSec = 0.01 + 0.05 * random().nextDouble();
final double maxReopenSec = minReopenSec * (1.0 + 10 * random().nextDouble());
if (VERBOSE) {
- System.out.println("TEST: make NRTManager maxReopenSec=" + maxReopenSec + " minReopenSec=" + minReopenSec);
+ System.out.println("TEST: make SearcherManager maxReopenSec=" + maxReopenSec + " minReopenSec=" + minReopenSec);
}
- genWriter = new NRTManager.TrackingIndexWriter(writer);
+ genWriter = new TrackingIndexWriter(writer);
final SearcherFactory sf = new SearcherFactory() {
@Override
public IndexSearcher newSearcher(IndexReader r) throws IOException {
- TestNRTManager.this.warmCalled = true;
+ TestControlledRealTimeReopenThread.this.warmCalled = true;
IndexSearcher s = new IndexSearcher(r, es);
s.search(new TermQuery(new Term("body", "united")), 10);
return s;
}
};
- nrtNoDeletes = new NRTManager(genWriter, sf, false);
- nrtDeletes = new NRTManager(genWriter, sf, true);
+ nrtNoDeletes = new SearcherManager(writer, false, sf);
+ nrtDeletes = new SearcherManager(writer, true, sf);
- nrtDeletesThread = new NRTManagerReopenThread(nrtDeletes, maxReopenSec, minReopenSec);
+ nrtDeletesThread = new ControlledRealTimeReopenThread<IndexSearcher>(genWriter, nrtDeletes, maxReopenSec, minReopenSec);
nrtDeletesThread.setName("NRTDeletes Reopen Thread");
nrtDeletesThread.setPriority(Math.min(Thread.currentThread().getPriority()+2, Thread.MAX_PRIORITY));
nrtDeletesThread.setDaemon(true);
nrtDeletesThread.start();
- nrtNoDeletesThread = new NRTManagerReopenThread(nrtNoDeletes, maxReopenSec, minReopenSec);
+ nrtNoDeletesThread = new ControlledRealTimeReopenThread<IndexSearcher>(genWriter, nrtNoDeletes, maxReopenSec, minReopenSec);
nrtNoDeletesThread.setName("NRTNoDeletes Reopen Thread");
nrtNoDeletesThread.setPriority(Math.min(Thread.currentThread().getPriority()+2, Thread.MAX_PRIORITY));
nrtNoDeletesThread.setDaemon(true);
@@ -260,7 +261,7 @@ public class TestNRTManager extends Thre
protected IndexSearcher getCurrentSearcher() throws Exception {
// Test doesn't assert deletions until the end, so we
// can randomize whether dels must be applied
- final NRTManager nrt;
+ final SearcherManager nrt;
if (random().nextBoolean()) {
nrt = nrtDeletes;
} else {
@@ -273,7 +274,7 @@ public class TestNRTManager extends Thre
@Override
protected void releaseSearcher(IndexSearcher s) throws Exception {
// NOTE: a bit iffy... technically you should release
- // against the same NRT mgr you acquired from... but
+ // against the same SearcherManager you acquired from... but
// both impls just decRef the underlying reader so we
// can get away w/ cheating:
nrtNoDeletes.release(s);
@@ -283,7 +284,7 @@ public class TestNRTManager extends Thre
protected void doClose() throws Exception {
assertTrue(warmCalled);
if (VERBOSE) {
- System.out.println("TEST: now close NRTManager");
+ System.out.println("TEST: now close SearcherManagers");
}
nrtDeletesThread.close();
nrtDeletes.close();
@@ -302,13 +303,12 @@ public class TestNRTManager extends Thre
final CountDownLatch signal = new CountDownLatch(1);
LatchedIndexWriter _writer = new LatchedIndexWriter(d, conf, latch, signal);
- final NRTManager.TrackingIndexWriter writer = new NRTManager.TrackingIndexWriter(_writer);
- final NRTManager manager = new NRTManager(writer, null, false);
+ final TrackingIndexWriter writer = new TrackingIndexWriter(_writer);
+ final SearcherManager manager = new SearcherManager(_writer, false, null);
Document doc = new Document();
doc.add(newTextField("test", "test", Field.Store.YES));
- long gen = writer.addDocument(doc);
+ writer.addDocument(doc);
manager.maybeRefresh();
- assertFalse(gen < manager.getCurrentSearchingGen());
Thread t = new Thread() {
@Override
public void run() {
@@ -336,7 +336,7 @@ public class TestNRTManager extends Thre
} finally {
manager.release(searcher);
}
- NRTManagerReopenThread thread = new NRTManagerReopenThread(manager, 0.01, 0.01);
+ final ControlledRealTimeReopenThread<IndexSearcher> thread = new ControlledRealTimeReopenThread<IndexSearcher>(writer, manager, 0.01, 0.01);
thread.start(); // start reopening
if (VERBOSE) {
System.out.println("waiting now for generation " + lastGen);
@@ -346,7 +346,12 @@ public class TestNRTManager extends Thre
Thread waiter = new Thread() {
@Override
public void run() {
- manager.waitForGeneration(lastGen);
+ try {
+ thread.waitForGeneration(lastGen);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(ie);
+ }
finished.set(true);
}
};
@@ -408,7 +413,7 @@ public class TestNRTManager extends Thre
};
try {
- new NRTManager(new NRTManager.TrackingIndexWriter(w.w), theEvilOne);
+ new SearcherManager(w.w, false, theEvilOne);
} catch (IllegalStateException ise) {
// expected
}
@@ -421,7 +426,7 @@ public class TestNRTManager extends Thre
Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
final AtomicBoolean afterRefreshCalled = new AtomicBoolean(false);
- NRTManager sm = new NRTManager(new NRTManager.TrackingIndexWriter(iw),new SearcherFactory());
+ SearcherManager sm = new SearcherManager(iw, true, new SearcherFactory());
sm.addListener(new ReferenceManager.RefreshListener() {
@Override
public void beforeRefresh() {
Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestLiveFieldValues.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestLiveFieldValues.java?rev=1478438&r1=1478437&r2=1478438&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestLiveFieldValues.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestLiveFieldValues.java Thu May 2 16:44:36 2013
@@ -25,7 +25,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import org.apache.lucene.analysis.MockAnalyzer;
@@ -36,10 +35,8 @@ import org.apache.lucene.document.String
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
-import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.StoredDocument;
import org.apache.lucene.index.Term;
-import org.apache.lucene.search.NRTManager.TrackingIndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
@@ -50,10 +47,9 @@ public class TestLiveFieldValues extends
Directory dir = newFSDirectory(_TestUtil.getTempDir("livefieldupdates"));
IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()));
- final IndexWriter _w = new IndexWriter(dir, iwc);
- final TrackingIndexWriter w = new TrackingIndexWriter(_w);
+ final IndexWriter w = new IndexWriter(dir, iwc);
- final NRTManager mgr = new NRTManager(w, new SearcherFactory() {
+ final SearcherManager mgr = new SearcherManager(w, true, new SearcherFactory() {
@Override
public IndexSearcher newSearcher(IndexReader r) {
return new IndexSearcher(r);
@@ -174,7 +170,7 @@ public class TestLiveFieldValues extends
rt.close();
mgr.close();
- _w.close();
+ w.close();
dir.close();
}
}