Copilot commented on code in PR #5368:
URL: https://github.com/apache/solr/pull/5368#discussion_r4641178649


##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -154,6 +157,15 @@ private Optional<Integer> 
parsePortFromProcess(ProcessHandle ph) {
     return portStr.isPresent() ? portStr.map(Integer::parseInt) : 
Optional.empty();
   }
 
+  private String parseAdvertisedHostFromProcess(ProcessHandle ph) {
+    return arguments(ph).stream()
+        .filter(a -> a.startsWith("-Dsolr.host.advertise="))
+        .map(a -> a.substring(a.indexOf('=') + 1))
+        .filter(h -> !h.isBlank())
+        .findFirst()
+        .orElse("localhost");
+  }

Review Comment:
   The `substring(a.indexOf('=') + 1)` assumes an '=' is present. If an 
argument ever matches the `startsWith` filter unexpectedly (or the prefix 
changes), `indexOf('=')` could be `-1`, producing the full string as the 'host' 
(e.g., `-Dsolr.host.advertise`), which would later generate invalid URLs. Safer 
parsing would remove the known prefix via its length (or explicitly validate 
`indexOf('=') >= 0` / `contains('=')`) before substring extraction.



##########
solr/core/src/java/org/apache/solr/cli/StatusTool.java:
##########
@@ -307,21 +313,61 @@ public void waitForSolrUpAndPrintStatus(String pidUrl) 
throws Exception {
 
   /**
    * Wait for Solr to come online and return true if it does, false otherwise. 
A node that rejects
-   * the request as unauthenticated or unauthorized counts as online.
+   * the request as unauthenticated or unauthorized counts as online. So does 
a node whose port
+   * accepts connections but which this host cannot talk to, such as one 
requiring a client
+   * certificate this host's name does not match; it is given {@link 
#UNREACHABLE_GRACE_MS} to
+   * finish starting.
    *
    * @return true if Solr comes online, false otherwise
    */
   public boolean waitForSolrUp(String pidUrl) throws Exception {
-    try {
-      waitToSeeSolrUp(pidUrl, credentials, maxWaitSecs, TimeUnit.SECONDS);
-      return true;
-    } catch (TimeoutException timeout) {
-      return false;
-    } catch (Exception e) {
-      if (CLIUtils.exceptionIsAuthRelated(e)) {
+    return waitForSolrUp(pidUrl, maxWaitSecs);
+  }
+
+  @SuppressWarnings("BusyWait")
+  boolean waitForSolrUp(String pidUrl, long maxWaitSecs) throws Exception {
+    long timeout = System.nanoTime() + TimeUnit.SECONDS.toNanos(maxWaitSecs);
+    int unreachable = 0;
+    while (System.nanoTime() < timeout) {
+      try {
+        getStatus(pidUrl, credentials);
         return true;
+      } catch (Exception e) {
+        if (CLIUtils.exceptionIsAuthRelated(e)) {
+          return true;
+        }
+        Throwable rootCause = SolrException.getRootCause(e);
+        boolean noHttpResponse =
+            rootCause instanceof IOException || rootCause instanceof 
SolrServerException;
+        if (noHttpResponse && isListening(pidUrl)) {

Review Comment:
   This broad `catch (Exception e)` causes *all* non-auth exceptions to be 
retried until timeout, which can mask non-transient errors (e.g., malformed 
URL/URI parsing errors, misconfiguration) and turn them into a silent `false` 
return. Consider rethrowing immediately for clearly non-retryable exceptions 
(or only retrying for network/IO-category failures) so genuine 
configuration/usage errors fail fast instead of timing out.



##########
solr/core/src/java/org/apache/solr/cli/StatusTool.java:
##########
@@ -307,21 +313,61 @@ public void waitForSolrUpAndPrintStatus(String pidUrl) 
throws Exception {
 
   /**
    * Wait for Solr to come online and return true if it does, false otherwise. 
A node that rejects
-   * the request as unauthenticated or unauthorized counts as online.
+   * the request as unauthenticated or unauthorized counts as online. So does 
a node whose port
+   * accepts connections but which this host cannot talk to, such as one 
requiring a client
+   * certificate this host's name does not match; it is given {@link 
#UNREACHABLE_GRACE_MS} to
+   * finish starting.
    *
    * @return true if Solr comes online, false otherwise
    */
   public boolean waitForSolrUp(String pidUrl) throws Exception {
-    try {
-      waitToSeeSolrUp(pidUrl, credentials, maxWaitSecs, TimeUnit.SECONDS);
-      return true;
-    } catch (TimeoutException timeout) {
-      return false;
-    } catch (Exception e) {
-      if (CLIUtils.exceptionIsAuthRelated(e)) {
+    return waitForSolrUp(pidUrl, maxWaitSecs);
+  }
+
+  @SuppressWarnings("BusyWait")
+  boolean waitForSolrUp(String pidUrl, long maxWaitSecs) throws Exception {
+    long timeout = System.nanoTime() + TimeUnit.SECONDS.toNanos(maxWaitSecs);
+    int unreachable = 0;
+    while (System.nanoTime() < timeout) {
+      try {
+        getStatus(pidUrl, credentials);
         return true;
+      } catch (Exception e) {
+        if (CLIUtils.exceptionIsAuthRelated(e)) {
+          return true;
+        }
+        Throwable rootCause = SolrException.getRootCause(e);
+        boolean noHttpResponse =
+            rootCause instanceof IOException || rootCause instanceof 
SolrServerException;
+        if (noHttpResponse && isListening(pidUrl)) {
+          if (++unreachable >= 2) {
+            CLIO.err(
+                "WARNING: Solr is listening at "
+                    + pidUrl
+                    + " but this host cannot connect to it ("
+                    + rootCause
+                    + "); assuming it has started.");
+            Thread.sleep(UNREACHABLE_GRACE_MS);
+            return true;
+          }
+        } else {
+          unreachable = 0;
+        }
       }
-      throw e;
+      Thread.sleep(2000L);
+    }
+    return false;
+  }

Review Comment:
   `waitForSolrUp(pidUrl, maxWaitSecs)` can exceed `maxWaitSecs` and still 
return `true` due to unconditional sleeps (2s polling sleep and 3s 
`UNREACHABLE_GRACE_MS`). With small `maxWaitSecs` values this violates the 
method’s implied contract (wait up to max, then return false if not up). 
Consider computing remaining time each loop and bounding sleeps to the 
remaining deadline; also ensure the 'unreachable => assume started' path only 
triggers if there is enough remaining time (or return based on the deadline 
rather than a fixed sleep).



-- 
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: [email protected]

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