Github user synhershko commented on a diff in the pull request:
https://github.com/apache/lucenenet/pull/171#discussion_r63999838
--- Diff: src/Lucene.Net.Core/Index/TaskMergeScheduler.cs ---
@@ -0,0 +1,664 @@
+/*
+ * 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.
+ */
+using Lucene.Net.Support;
+using Lucene.Net.Util;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Directory = Lucene.Net.Store.Directory;
+
+namespace Lucene.Net.Index
+{
+ /// <summary>
+ /// A <seealso cref="MergeScheduler"/> that runs each merge using
+ /// Tasks on the default TaskScheduler.
+ ///
+ /// <p>If more than <seealso cref="#GetMaxMergeCount"/> merges are
+ /// requested then this class will forcefully throttle the
+ /// incoming threads by pausing until one more more merges
+ /// complete.</p>
+ ///
+ /// LUCENENET specific
+ /// </summary>
+ public class TaskMergeScheduler : MergeScheduler,
IConcurrentMergeScheduler
+ {
+ public const string COMPONENT_NAME = "CMS";
+
+ private readonly TaskScheduler _taskScheduler =
TaskScheduler.Default;
+ private readonly ReaderWriterLockSlim _lock = new
ReaderWriterLockSlim();
+ private readonly ManualResetEventSlim _manualResetEvent = new
ManualResetEventSlim();
+ /// <summary>
+ /// List of currently active <seealso
cref="MergeThread"/>s.</summary>
+ private readonly IList<MergeThread> _mergeThreads = new
List<MergeThread>();
+
+ /// <summary>
+ /// How many <seealso cref="MergeThread"/>s have kicked off (this
is use
+ /// to name them).
+ /// </summary>
+ private int _mergeThreadCount;
+
+ /// <summary>
+ /// <seealso cref="Directory"/> that holds the index. </summary>
+ private Directory _directory;
+
+ /// <summary>
+ /// <seealso cref="IndexWriter"/> that owns this instance.
+ /// </summary>
+ private IndexWriter _writer;
+
+ /// <summary>
+ /// Sole constructor, with all settings set to default
+ /// values.
+ /// </summary>
+ public TaskMergeScheduler() : base()
+ {
+ MaxThreadCount = _taskScheduler.MaximumConcurrencyLevel;
+ MaxMergeCount = _taskScheduler.MaximumConcurrencyLevel;
+ }
+
+ /// <summary>
+ /// Sets the maximum number of merge threads and simultaneous
merges allowed.
+ /// </summary>
+ /// <param name="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
+ /// smallest <code>maxThreadCount</code> merges at a time.
</param>
+ /// <param name="maxThreadCount"> the max # simultaneous merge
threads that should
+ /// be running at once. this must be <=
<code>maxMergeCount</code> </param>
+ public void SetMaxMergesAndThreads(int maxMergeCount, int
maxThreadCount)
+ {
+ // This is handled by
TaskScheduler.Default.MaximumConcurrencyLevel
+ }
+
+ /// <summary>
+ /// Max number of merge threads allowed to be running at
+ /// once. When there are more merges then this, we
+ /// forcefully pause the larger ones, letting the smaller
+ /// ones run, up until maxMergeCount merges at which point
+ /// we forcefully pause incoming threads (that presumably
+ /// are the ones causing so much merging).
+ /// </summary>
+ /// <seealso cref= #setMaxMergesAndThreads(int, int) </seealso>
+ public int MaxThreadCount { get; private set; }
+
+ /// <summary>
+ /// Max number of merges we accept before forcefully
+ /// throttling the incoming threads
+ /// </summary>
+ public int MaxMergeCount { get; private set; }
+
+ /// <summary>
+ /// Return the priority that merge threads run at. This is always
the same.
+ /// </summary>
+ public int MergeThreadPriority
+ {
+ get
+ {
+ return (int)ThreadPriority.Normal;
+ }
+ set
+ {
+ }
+ }
+
+ /// <summary>
+ /// 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.
+ /// </summary>
+ private void UpdateMergeThreads()
+ {
+ foreach (var merge in _mergeThreads.ToArray())
+ {
+ // Prune any dead threads
+ if (!merge.IsAlive)
+ {
+ _mergeThreads.Remove(merge);
+ merge.Dispose();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Returns true if verbosing is enabled. this method is usually
used in
+ /// conjunction with <seealso cref="#message(String)"/>, like that:
+ ///
+ /// <pre class="prettyprint">
+ /// if (verbose()) {
+ /// message("your message");
+ /// }
+ /// </pre>
+ /// </summary>
+ protected internal bool Verbose()
+ {
+ return _writer != null &&
_writer.infoStream.IsEnabled(COMPONENT_NAME);
--- End diff --
cache return value (or calculate on init / assignment of `_writer`)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---