1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.Closeable;
21 import java.io.IOException;
23 import org.apache.lucene.util.ThreadInterruptedException;
26 * Utility class that runs a reopen thread to periodically
27 * reopen the NRT searchers in the provided {@link
30 * <p> Typical usage looks like this:
33 * ... open your own writer ...
35 * NRTManager manager = new NRTManager(writer);
37 * // Refreshes searcher every 5 seconds when nobody is waiting, and up to 100 msec delay
38 * // when somebody is waiting:
39 * NRTManagerReopenThread reopenThread = new NRTManagerReopenThread(manager, 5.0, 0.1);
40 * reopenThread.setName("NRT Reopen Thread");
41 * reopenThread.setPriority(Math.min(Thread.currentThread().getPriority()+2, Thread.MAX_PRIORITY));
42 * reopenThread.setDaemon(true);
43 * reopenThread.start();
46 * Then, for each incoming query, do this:
49 * // For each incoming query:
50 * IndexSearcher searcher = manager.get();
52 * // Use searcher to search...
54 * manager.release(searcher);
58 * You should make changes using the <code>NRTManager</code>; if you later need to obtain
59 * a searcher reflecting those changes:
62 * // ... or updateDocument, deleteDocuments, etc:
63 * long gen = manager.addDocument(...);
65 * // Returned searcher is guaranteed to reflect the just added document
66 * IndexSearcher searcher = manager.get(gen);
68 * // Use searcher to search...
70 * manager.release(searcher);
75 * When you are done be sure to close both the manager and the reopen thrad:
77 * reopenThread.close();
81 * @lucene.experimental
84 public class NRTManagerReopenThread extends Thread implements NRTManager.WaitingListener, Closeable {
86 private final NRTManager manager;
87 private final long targetMaxStaleNS;
88 private final long targetMinStaleNS;
89 private boolean finish;
90 private long waitingGen;
91 private boolean waitingNeedsDeletes;
94 * Create NRTManagerReopenThread, to periodically reopen the NRT searcher.
96 * @param targetMaxStaleSec Maximum time until a new
97 * reader must be opened; this sets the upper bound
98 * on how slowly reopens may occur
100 * @param targetMinStaleSec Mininum time until a new
101 * reader can be opened; this sets the lower bound
102 * on how quickly reopens may occur, when a caller
103 * is waiting for a specific indexing change to
107 public NRTManagerReopenThread(NRTManager manager, double targetMaxStaleSec, double targetMinStaleSec) {
108 if (targetMaxStaleSec < targetMinStaleSec) {
109 throw new IllegalArgumentException("targetMaxScaleSec (= " + targetMaxStaleSec + ") < targetMinStaleSec (=" + targetMinStaleSec + ")");
111 this.manager = manager;
112 this.targetMaxStaleNS = (long) (1000000000*targetMaxStaleSec);
113 this.targetMinStaleNS = (long) (1000000000*targetMinStaleSec);
114 manager.addWaitingListener(this);
117 public synchronized void close() {
118 //System.out.println("NRT: set finish");
119 manager.removeWaitingListener(this);
124 } catch (InterruptedException ie) {
125 throw new ThreadInterruptedException(ie);
129 public synchronized void waiting(boolean needsDeletes, long targetGen) {
130 waitingNeedsDeletes |= needsDeletes;
131 waitingGen = Math.max(waitingGen, targetGen);
133 //System.out.println(Thread.currentThread().getName() + ": force wakeup waitingGen=" + waitingGen + " applyDeletes=" + applyDeletes + " waitingNeedsDeletes=" + waitingNeedsDeletes);
138 // TODO: maybe use private thread ticktock timer, in
139 // case clock shift messes up nanoTime?
140 long lastReopenStartNS = System.nanoTime();
142 //System.out.println("reopen: start");
146 boolean hasWaiting = false;
149 // TODO: try to guestimate how long reopen might
150 // take based on past data?
153 //System.out.println("reopen: cycle");
155 // True if we have someone waiting for reopen'd searcher:
156 hasWaiting = waitingGen > manager.getCurrentSearchingGen(waitingNeedsDeletes);
157 final long nextReopenStartNS = lastReopenStartNS + (hasWaiting ? targetMinStaleNS : targetMaxStaleNS);
159 final long sleepNS = nextReopenStartNS - System.nanoTime();
162 //System.out.println("reopen: sleep " + (sleepNS/1000000.0) + " ms (hasWaiting=" + hasWaiting + ")");
164 wait(sleepNS/1000000, (int) (sleepNS%1000000));
165 } catch (InterruptedException ie) {
166 Thread.currentThread().interrupt();
167 //System.out.println("NRT: set finish on interrupt");
177 //System.out.println("reopen: finish");
180 //System.out.println("reopen: start hasWaiting=" + hasWaiting);
183 lastReopenStartNS = System.nanoTime();
185 //final long t0 = System.nanoTime();
186 manager.maybeReopen(waitingNeedsDeletes);
187 //System.out.println("reopen took " + ((System.nanoTime()-t0)/1000000.0) + " msec");
188 } catch (IOException ioe) {
189 //System.out.println(Thread.currentThread().getName() + ": IOE");
190 //ioe.printStackTrace();
191 throw new RuntimeException(ioe);
194 } catch (Throwable t) {
195 //System.out.println("REOPEN EXC");
196 //t.printStackTrace(System.out);
197 throw new RuntimeException(t);