sonatype-lift[bot] commented on code in PR #1126:
URL: https://github.com/apache/solr/pull/1126#discussion_r1108778401


##########
solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java:
##########
@@ -493,4 +404,133 @@ public interface CoreAdminOp {
      */
     void execute(CallInfo it) throws Exception;
   }
+
+  public static class CoreAdminAsyncTracker {
+    private static final int MAX_TRACKED_REQUESTS = 100;
+    public static final String RUNNING = "running";
+    public static final String COMPLETED = "completed";
+    public static final String FAILED = "failed";
+    public final Map<String, Map<String, TaskObject>> requestStatusMap;
+
+    private ExecutorService parallelExecutor =
+        ExecutorUtil.newMDCAwareFixedThreadPool(
+            50, new 
SolrNamedThreadFactory("parallelCoreAdminAPIBaseExecutor"));
+
+    public CoreAdminAsyncTracker() {
+      HashMap<String, Map<String, TaskObject>> map = new HashMap<>(3, 1.0f);
+      map.put(RUNNING, Collections.synchronizedMap(new LinkedHashMap<>()));
+      map.put(COMPLETED, Collections.synchronizedMap(new LinkedHashMap<>()));
+      map.put(FAILED, Collections.synchronizedMap(new LinkedHashMap<>()));
+      requestStatusMap = Collections.unmodifiableMap(map);
+    }
+
+    public void shutdown() {
+      ExecutorUtil.shutdownAndAwaitTermination(parallelExecutor);
+    }
+
+    public Map<String, TaskObject> getRequestStatusMap(String key) {
+      return requestStatusMap.get(key);
+    }
+
+    public void submitAsyncTask(
+        TaskObject taskObject, String action, Callable<SolrQueryResponse> task)
+        throws SolrException {
+      ensureTaskIdNotInUse(taskObject.taskId);
+      addTask(RUNNING, taskObject);
+
+      try {
+        MDC.put("CoreAdminHandler.asyncId", taskObject.taskId);
+        MDC.put("CoreAdminHandler.action", action);
+        parallelExecutor.execute(
+            () -> {
+              boolean exceptionCaught = false;
+              try {
+                final SolrQueryResponse response = task.call();
+                taskObject.setRspObject(response);
+                taskObject.setOperationRspObject(response);
+              } catch (Exception e) {
+                exceptionCaught = true;
+                taskObject.setRspObjectFromException(e);
+              } finally {
+                finishTask(taskObject, !exceptionCaught);
+              }
+            });
+      } finally {
+        MDC.remove("CoreAdminHandler.asyncId");
+        MDC.remove("CoreAdminHandler.action");
+      }
+    }
+
+    /** Helper method to add a task to a tracking type. */
+    private void addTask(String type, TaskObject o, boolean limit) {
+      synchronized (getRequestStatusMap(type)) {
+        if (limit && getRequestStatusMap(type).size() == MAX_TRACKED_REQUESTS) 
{
+          String key = 
getRequestStatusMap(type).entrySet().iterator().next().getKey();
+          getRequestStatusMap(type).remove(key);
+        }
+        addTask(type, o);
+      }
+    }
+
+    private void addTask(String type, TaskObject o) {
+      synchronized (getRequestStatusMap(type)) {
+        getRequestStatusMap(type).put(o.taskId, o);
+      }
+    }
+
+    /** Helper method to remove a task from a tracking map. */
+    private void removeTask(String map, String taskId) {
+      synchronized (getRequestStatusMap(map)) {
+        getRequestStatusMap(map).remove(taskId);
+      }
+    }
+
+    private void ensureTaskIdNotInUse(String taskId) throws SolrException {
+      if (getRequestStatusMap(RUNNING).containsKey(taskId)

Review Comment:
   <picture><img alt="5% of developers fix this issue" 
src="https://lift.sonatype.com/api/commentimage/fixrate/5/display.svg";></picture>
   
   <b>*NULLPTR_DEREFERENCE:</b>*  `Map 
CoreAdminHandler$CoreAdminAsyncTracker.getRequestStatusMap(String)` could be 
null (from the call to 
`CoreAdminHandler$CoreAdminAsyncTracker.getRequestStatusMap(...)` on line 489) 
and is dereferenced.
   
   ---
   
   <details><summary>ℹī¸ Expand to see all <b>@sonatype-lift</b> 
commands</summary>
   
   You can reply with the following commands. For example, reply with 
***@sonatype-lift ignoreall*** to leave out all findings.
   | **Command** | **Usage** |
   | ------------- | ------------- |
   | `@sonatype-lift ignore` | Leave out the above finding from this PR |
   | `@sonatype-lift ignoreall` | Leave out all the existing findings from this 
PR |
   | `@sonatype-lift exclude <file\|issue\|path\|tool>` | Exclude specified 
`file\|issue\|path\|tool` from Lift findings by updating your config.toml file |
   
   **Note:** When talking to LiftBot, you need to **refresh** the page to see 
its response.
   <sub>[Click here](https://github.com/apps/sonatype-lift/installations/new) 
to add LiftBot to another repo.</sub></details>
   
   
   
   ---
   
   <b>Help us improve LIFT! (<i>Sonatype LiftBot external survey</i>)</b>
   
   Was this a good recommendation for you? <sub><small>Answering this survey 
will not impact your Lift settings.</small></sub>
   
   [ [🙁 Not 
relevant](https://www.sonatype.com/lift-comment-rating?comment=394165237&lift_comment_rating=1)
 ] - [ [😕 Won't 
fix](https://www.sonatype.com/lift-comment-rating?comment=394165237&lift_comment_rating=2)
 ] - [ [😑 Not critical, will 
fix](https://www.sonatype.com/lift-comment-rating?comment=394165237&lift_comment_rating=3)
 ] - [ [🙂 Critical, will 
fix](https://www.sonatype.com/lift-comment-rating?comment=394165237&lift_comment_rating=4)
 ] - [ [😊 Critical, fixing 
now](https://www.sonatype.com/lift-comment-rating?comment=394165237&lift_comment_rating=5)
 ]



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to