dsmiley commented on a change in pull request #1527:
URL: https://github.com/apache/lucene-solr/pull/1527#discussion_r435405488



##########
File path: solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java
##########
@@ -52,35 +56,60 @@
   private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   public static SolrRequestInfo getRequestInfo() {
-    return threadLocal.get();
+    Deque<SolrRequestInfo> stack = threadLocal.get();
+    if (stack.isEmpty()) return null;
+    return stack.peek();
   }
 
+  /** Adds the SolrRequestInfo onto the stack provided that the stack is not 
reached MAX_STACK_SIZE */
   public static void setRequestInfo(SolrRequestInfo info) {
-    // TODO: temporary sanity check... this can be changed to just an assert 
in the future
-    SolrRequestInfo prev = threadLocal.get();
-    if (prev != null) {
-      log.error("Previous SolrRequestInfo was not closed!  req={}", 
prev.req.getOriginalParams());
-      log.error("prev == info : {}", prev.req == info.req, new 
RuntimeException());
+    Deque<SolrRequestInfo> stack = threadLocal.get();
+    if (info == null) {
+      throw new IllegalArgumentException("SolrRequestInfo is null");
+    } else {
+      if (stack.size() <= MAX_STACK_SIZE) {
+        stack.push(info);
+      } else {
+        assert true : "SolrRequestInfo Stack is full";
+        log.error("SolrRequestInfo Stack is full");
+      }
     }
-    assert prev == null;
-
-    threadLocal.set(info);
   }
 
+  /** Removes the most recent SolrRequestInfo from the stack */
   public static void clearRequestInfo() {
-    try {
-      SolrRequestInfo info = threadLocal.get();
-      if (info != null && info.closeHooks != null) {
-        for (Closeable hook : info.closeHooks) {
-          try {
-            hook.close();
-          } catch (Exception e) {
-            SolrException.log(log, "Exception during close hook", e);
-          }
+    Deque<SolrRequestInfo> stack = threadLocal.get();
+    if (stack.isEmpty()) {
+      log.error("clearRequestInfo called too many times");
+    } else {
+      SolrRequestInfo info = stack.pop();
+      closeHooks(info);
+    }
+  }
+
+  /**
+   * This reset method is more of a protection mechanism as
+   * we expect it to be empty by now because all "set" calls need to be 
balanced with a "clear".
+   */
+  public static void reset() {
+    Deque<SolrRequestInfo> stack = threadLocal.get();
+    boolean isEmpty = stack.isEmpty();
+    while (!stack.isEmpty()) {
+      SolrRequestInfo info = stack.pop();
+      closeHooks(info);
+    }
+    assert isEmpty : "SolrRequestInfo Stack should have been cleared.";
+  }
+
+  private static void closeHooks(SolrRequestInfo info) {
+    if (info != null && info.closeHooks != null) {

Review comment:
       The callers of this method will not pass null though; right?

##########
File path: 
solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java
##########
@@ -271,7 +273,9 @@ public void writeResults(ResultContext ctx, JavaBinCodec 
codec) throws IOExcepti
       throw new SolrServerException(ex);
     } finally {
       if (req != null) req.close();
-      SolrRequestInfo.clearRequestInfo();
+      if (mustClearSolrRequestInfo) {

Review comment:
       Hmm; might it simply be enough to call clearRequestInfo if req != null, 
thus no new boolean?

##########
File path: 
solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java
##########
@@ -71,6 +71,7 @@
   protected final String coreName;
   private final SolrRequestParsers _parser;
   private final RequestWriterSupplier supplier;
+  private boolean mustClearSolrRequestInfo = false;

Review comment:
       This change is highly suspicious.  Why is this boolean now a field of 
EmbeddedSolrServer?  It doesn't seem like *state* of this object and so I don't 
think it belongs here.  It seems like a transient status in the course of work 
within the method that sets/clears the requestInfo.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to