You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ds...@apache.org on 2024/02/04 00:26:20 UTC

(solr) 01/02: SOLR-17144: Do not keep an active thread per core (#2236)

This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit d17808347aa70ef9838e53a6f430220c598edf53
Author: Pierre Salagnac <82...@users.noreply.github.com>
AuthorDate: Sat Feb 3 22:23:46 2024 +0100

    SOLR-17144: Do not keep an active thread per core (#2236)
    
    
    
    Co-authored-by: Pierre Salagnac <ps...@salesforce.com>
---
 solr/CHANGES.txt                                          |  2 +-
 solr/core/src/java/org/apache/solr/core/SolrCore.java     |  4 +++-
 .../java/org/apache/solr/common/util/ExecutorUtil.java    | 15 ++++++++++++++-
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 5d807dc036f..a34cfdbcff2 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -19,7 +19,7 @@ Improvements
 
 Optimizations
 ---------------------
-(No changes)
+* SOLR-17144: Close searcherExecutor thread per core after 1 minute (Pierre Salagnac, Christine Poerschke)
 
 Bug Fixes
 ---------------------
diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java
index be6f7526b3d..c72d9debc55 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java
@@ -2056,8 +2056,10 @@ public class SolrCore implements SolrInfoBean, Closeable {
   private final ArrayDeque<RefCounted<SolrIndexSearcher>> _searchers = new ArrayDeque<>();
   private final ArrayDeque<RefCounted<SolrIndexSearcher>> _realtimeSearchers = new ArrayDeque<>();
 
+  // Single threaded pool, with a time-to-keep of 60 seconds
   final ExecutorService searcherExecutor =
-      ExecutorUtil.newMDCAwareSingleThreadExecutor(new SolrNamedThreadFactory("searcherExecutor"));
+      ExecutorUtil.newMDCAwareSingleLazyThreadExecutor(
+          new SolrNamedThreadFactory("searcherExecutor"), 60L, TimeUnit.SECONDS);
   private int onDeckSearchers; // number of searchers preparing
   // Lock ordering: one can acquire the openSearcherLock and then the searcherLock, but not
   // vice-versa.
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java b/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java
index cfa7b176d50..f0d1f8bc698 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java
@@ -150,12 +150,25 @@ public class ExecutorUtil {
         nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), threadFactory);
   }
 
-  /** See {@link java.util.concurrent.Executors#newSingleThreadExecutor(ThreadFactory)} */
+  /**
+   * See {@link java.util.concurrent.Executors#newSingleThreadExecutor(ThreadFactory)}. Note the
+   * thread is always active, even if no tasks are submitted to the executor.
+   */
   public static ExecutorService newMDCAwareSingleThreadExecutor(ThreadFactory threadFactory) {
     return new MDCAwareThreadPoolExecutor(
         1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), threadFactory);
   }
 
+  /**
+   * Similar to {@link #newMDCAwareSingleThreadExecutor(ThreadFactory)}, but the thread will not be
+   * kept active after the specified time if no task is submitted to the executor.
+   */
+  public static ExecutorService newMDCAwareSingleLazyThreadExecutor(
+      ThreadFactory threadFactory, long keepAliveTime, TimeUnit unit) {
+    return new MDCAwareThreadPoolExecutor(
+        0, 1, keepAliveTime, unit, new LinkedBlockingQueue<>(), threadFactory);
+  }
+
   /** Create a cached thread pool using a named thread factory */
   public static ExecutorService newMDCAwareCachedThreadPool(String name) {
     return newMDCAwareCachedThreadPool(new SolrNamedThreadFactory(name));