Package org.apache.lucene.index
Class ConcurrentMergeScheduler
java.lang.Object
org.apache.lucene.index.MergeScheduler
org.apache.lucene.index.ConcurrentMergeScheduler
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Cloneable
A
MergeScheduler
that runs each merge using a
separate thread.
Specify the max number of threads that may run at
once, and the maximum number of simultaneous merges
with setMaxMergesAndThreads(int, int)
.
If the number of merges exceeds the max number of threads then the largest merges are paused until one of the smaller merges completes.
If more than getMaxMergeCount()
merges are
requested then this class will forcefully throttle the
incoming threads by pausing until one more more merges
complete.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
Runs a merge thread, which may run one or more merges in sequence. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Comparator<ConcurrentMergeScheduler.MergeThread>
SortsConcurrentMergeScheduler.MergeThread
s; larger merges come first.static final int
DefaultmaxMergeCount
.static final int
DefaultmaxThreadCount
.protected Directory
Directory
that holds the index.protected int
How manyConcurrentMergeScheduler.MergeThread
s have kicked off (this is use to name them).protected List<ConcurrentMergeScheduler.MergeThread>
List of currently activeConcurrentMergeScheduler.MergeThread
s.protected IndexWriter
IndexWriter
that owns this instance. -
Constructor Summary
ConstructorsConstructorDescriptionSole constructor, with all settings set to default values. -
Method Summary
Modifier and TypeMethodDescriptionclone()
void
close()
Close this MergeScheduler.protected void
doMerge
(MergePolicy.OneMerge merge) Does the actual merge, by callingIndexWriter.merge(org.apache.lucene.index.MergePolicy.OneMerge)
int
int
ReturnsmaxThreadCount
.protected ConcurrentMergeScheduler.MergeThread
getMergeThread
(IndexWriter writer, MergePolicy.OneMerge merge) Create and return a new MergeThreadint
Return the priority that merge threads run at.protected void
Called when an exception is hit in a background merge threadvoid
merge
(IndexWriter writer) Run the merges provided byIndexWriter.getNextMerge()
.protected int
Returns the number of merge threads that are alive.protected void
Outputs the given message - this method assumesverbose()
was called and returned true.void
setMaxMergesAndThreads
(int maxMergeCount, int maxThreadCount) Sets the maximum number of merge threads and simultaneous merges allowed.void
setMergeThreadPriority
(int pri) Set the base priority that merge threads run at.void
sync()
Wait for any running merge threads to finish.toString()
protected void
Called whenever the running merges have changed, to pause & unpause threads.protected boolean
verbose()
Returns true if verbosing is enabled.
-
Field Details
-
mergeThreads
List of currently activeConcurrentMergeScheduler.MergeThread
s. -
DEFAULT_MAX_THREAD_COUNT
public static final int DEFAULT_MAX_THREAD_COUNTDefaultmaxThreadCount
. We default to 1: tests on spinning-magnet drives showed slower indexing performance if more than one merge thread runs at once (though on an SSD it was faster)- See Also:
-
DEFAULT_MAX_MERGE_COUNT
public static final int DEFAULT_MAX_MERGE_COUNTDefaultmaxMergeCount
.- See Also:
-
dir
Directory
that holds the index. -
writer
IndexWriter
that owns this instance. -
mergeThreadCount
protected int mergeThreadCountHow manyConcurrentMergeScheduler.MergeThread
s have kicked off (this is use to name them). -
compareByMergeDocCount
SortsConcurrentMergeScheduler.MergeThread
s; larger merges come first.
-
-
Constructor Details
-
ConcurrentMergeScheduler
public ConcurrentMergeScheduler()Sole constructor, with all settings set to default values.
-
-
Method Details
-
setMaxMergesAndThreads
public void setMaxMergesAndThreads(int maxMergeCount, int maxThreadCount) Sets the maximum number of merge threads and simultaneous merges allowed.- Parameters:
maxMergeCount
- the max # simultaneous merges that are allowed. If a merge is necessary yet we already have this many threads running, the incoming thread (that is calling add/updateDocument) will block until a merge thread has completed. Note that we will only run the smallestmaxThreadCount
merges at a time.maxThreadCount
- the max # simultaneous merge threads that should be running at once. This must be <=maxMergeCount
-
getMaxThreadCount
public int getMaxThreadCount()ReturnsmaxThreadCount
.- See Also:
-
getMaxMergeCount
public int getMaxMergeCount() -
getMergeThreadPriority
public int getMergeThreadPriority()Return the priority that merge threads run at. By default the priority is 1 plus the priority of (ie, slightly higher priority than) the first thread that calls merge. -
setMergeThreadPriority
public void setMergeThreadPriority(int pri) Set the base priority that merge threads run at. Note that CMS may increase priority of some merge threads beyond this base priority. It's best not to set this any higher than Thread.MAX_PRIORITY-maxThreadCount, so that CMS has room to set relative priority among threads. -
updateMergeThreads
protected void updateMergeThreads()Called whenever the running merges have changed, to pause & unpause threads. This method sorts the merge threads by their merge size in descending order and then pauses/unpauses threads from first to last -- that way, smaller merges are guaranteed to run before larger ones. -
verbose
protected boolean verbose()Returns true if verbosing is enabled. This method is usually used in conjunction withmessage(String)
, like that:if (verbose()) { message("your message"); }
-
message
Outputs the given message - this method assumesverbose()
was called and returned true. -
close
public void close()Description copied from class:MergeScheduler
Close this MergeScheduler.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classMergeScheduler
-
sync
public void sync()Wait for any running merge threads to finish. This call is not interruptible as used byclose()
. -
mergeThreadCount
protected int mergeThreadCount()Returns the number of merge threads that are alive. Note that this number is ≤mergeThreads
size. -
merge
Description copied from class:MergeScheduler
Run the merges provided byIndexWriter.getNextMerge()
.- Specified by:
merge
in classMergeScheduler
- Throws:
IOException
-
doMerge
Does the actual merge, by callingIndexWriter.merge(org.apache.lucene.index.MergePolicy.OneMerge)
- Throws:
IOException
-
getMergeThread
protected ConcurrentMergeScheduler.MergeThread getMergeThread(IndexWriter writer, MergePolicy.OneMerge merge) throws IOException Create and return a new MergeThread- Throws:
IOException
-
handleMergeException
Called when an exception is hit in a background merge thread -
toString
-
clone
- Overrides:
clone
in classMergeScheduler
-