1 package org.apache.lucene.index;
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();
82 public class NRTManagerReopenThread extends Thread implements NRTManager.WaitingListener, Closeable {
83 private final NRTManager manager;
84 private final long targetMaxStaleNS;
85 private final long targetMinStaleNS;
86 private boolean finish;
87 private boolean waitingNeedsDeletes;
88 private long waitingGen;
91 * Create NRTManagerReopenThread, to periodically reopen the NRT searcher.
93 * @param targetMaxStaleSec Maximum time until a new
94 * reader must be opened; this sets the upper bound
95 * on how slowly reopens may occur
97 * @param targetMinStaleSec Mininum time until a new
98 * reader can be opened; this sets the lower bound
99 * on how quickly reopens may occur, when a caller
100 * is waiting for a specific indexing change to
104 public NRTManagerReopenThread(NRTManager manager, double targetMaxStaleSec, double targetMinStaleSec) {
105 if (targetMaxStaleSec < targetMinStaleSec) {
106 throw new IllegalArgumentException("targetMaxScaleSec (= " + targetMaxStaleSec + ") < targetMinStaleSec (=" + targetMinStaleSec + ")");
108 this.manager = manager;
109 this.targetMaxStaleNS = (long) (1000000000*targetMaxStaleSec);
110 this.targetMinStaleNS = (long) (1000000000*targetMinStaleSec);
111 manager.addWaitingListener(this);
114 public synchronized void close() {
115 //System.out.println("NRT: set finish");
116 manager.removeWaitingListener(this);
121 } catch (InterruptedException ie) {
122 throw new ThreadInterruptedException(ie);
126 public synchronized void waiting(boolean needsDeletes, long targetGen) {
127 waitingNeedsDeletes |= needsDeletes;
128 waitingGen = Math.max(waitingGen, targetGen);
130 //System.out.println(Thread.currentThread().getName() + ": force wakeup waitingGen=" + waitingGen + " applyDeletes=" + applyDeletes + " waitingNeedsDeletes=" + waitingNeedsDeletes);
135 // TODO: maybe use private thread ticktock timer, in
136 // case clock shift messes up nanoTime?
137 long lastReopenStartNS = System.nanoTime();
139 //System.out.println("reopen: start");
143 final boolean doApplyDeletes;
145 boolean hasWaiting = false;
148 // TODO: try to guestimate how long reopen might
149 // take based on past data?
152 //System.out.println("reopen: cycle");
154 // True if we have someone waiting for reopen'd searcher:
155 hasWaiting = waitingGen > manager.getCurrentSearchingGen(waitingNeedsDeletes);
156 final long nextReopenStartNS = lastReopenStartNS + (hasWaiting ? targetMinStaleNS : targetMaxStaleNS);
158 final long sleepNS = nextReopenStartNS - System.nanoTime();
161 //System.out.println("reopen: sleep " + (sleepNS/1000000.0) + " ms (hasWaiting=" + hasWaiting + ")");
163 wait(sleepNS/1000000, (int) (sleepNS%1000000));
164 } catch (InterruptedException ie) {
165 Thread.currentThread().interrupt();
166 //System.out.println("NRT: set finish on interrupt");
176 //System.out.println("reopen: finish");
180 doApplyDeletes = hasWaiting ? waitingNeedsDeletes : true;
181 waitingNeedsDeletes = false;
182 //System.out.println("reopen: start hasWaiting=" + hasWaiting);
185 lastReopenStartNS = System.nanoTime();
187 //final long t0 = System.nanoTime();
188 manager.reopen(doApplyDeletes);
189 //System.out.println("reopen took " + ((System.nanoTime()-t0)/1000000.0) + " msec");
190 } catch (IOException ioe) {
191 //System.out.println(Thread.currentThread().getName() + ": IOE");
192 //ioe.printStackTrace();
193 throw new RuntimeException(ioe);
196 } catch (Throwable t) {
197 //System.out.println("REOPEN EXC");
198 //t.printStackTrace(System.out);
199 throw new RuntimeException(t);