Apache Solr open-source search software
SOLR-13036 Fix retry logic in JettySolrRunner
Gus Heck59919b4ac06a
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d5240be7211..dcc62677ae4 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -113,6 +113,8 @@ Other Changes* SOLR-12775: LowerCaseTokenizer is deprecated, and should be replaced by LetterTokenizer andLowerCaseFilter (Alan Woodward)+* SOLR-13036: Fix retry logic in JettySolrRunner (Gus Heck)+================== 7.7.0 ==================Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.@@ -172,6 +174,9 @@ Other Changes* SOLR-12932: ant test (without badapples=false) should pass easily for developers. (Mark Miller)+* SOLR-13036: Fix retry logic in JettySolrRunner (Gus Heck)++================== 7.6.0 ==================Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.diff --git a/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java b/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.javaindex e9d84199d7c..24caec8f8a4 100644--- a/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java+++ b/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java@@ -509,12 +509,15 @@ public class JettySolrRunner {log.info("Trying to start Jetty on port {} try number {} ...", port, tryCnt++);server.start();break;- } catch (BindException e) {- log.info("Port is in use, will try again until timeout of " + timeout);- server.stop();- Thread.sleep(3000);- if (!timeout.hasTimedOut()) {- continue;+ } catch (IOException ioe) {+ Exception e = lookForBindException(ioe);+ if (e instanceof BindException) {+ log.info("Port is in use, will try again until timeout of " + timeout);+ server.stop();+ Thread.sleep(3000);+ if (!timeout.hasTimedOut()) {+ continue;+ }}throw e;@@ -522,6 +525,26 @@ public class JettySolrRunner {}}+ /**+ * Traverses the cause chain looking for a BindException. Returns either a bind exception+ * that was found in the chain or the original argument.+ *+ * @param ioe An IOException that might wrap a BindException+ * @return A bind exception if present otherwise ioe+ */+ Exception lookForBindException(IOException ioe) {+ Exception e = ioe;+ while(e.getCause() != null && !(e == e.getCause()) && ! (e instanceof BindException)) {+ if (e.getCause() instanceof Exception) {+ e = (Exception) e.getCause();+ if (e instanceof BindException) {+ return e;+ }+ }+ }+ return ioe;+ }+/*** Stop the Jetty server*diff --git a/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestJettySolrRunner.java b/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestJettySolrRunner.javaindex 6b21aa11929..ae8312e5f18 100644--- a/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestJettySolrRunner.java+++ b/solr/core/src/test/org/apache/solr/client/solrj/embedded/TestJettySolrRunner.java@@ -19,8 +19,12 @@ package org.apache.solr.client.solrj.embedded;import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.request.CoreAdminRequest;+import org.apache.solr.cloud.MiniSolrCloudCluster;import org.junit.Test;+import java.io.IOException;+import java.net.BindException;+import java.nio.charset.Charset;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;@@ -62,11 +66,56 @@ public class TestJettySolrRunner extends SolrTestCaseJ4 {assertTrue(Files.exists(coresDir.resolve("newcore").resolve("core.properties")));- }- finally {+ } finally {runner.stop();}}++ @SuppressWarnings("ThrowableNotThrown")+ @Test+ public void testLookForBindException() throws IOException {+ Path solrHome = createTempDir();+ Files.write(solrHome.resolve("solr.xml"), MiniSolrCloudCluster.DEFAULT_CLOUD_SOLR_XML.getBytes(Charset.defaultCharset()));++ JettyConfig config = JettyConfig.builder().build();++ JettySolrRunner jetty = new JettySolrRunner(solrHome.toString(), config);++ Exception result;+ BindException be = new BindException();+ IOException test = new IOException();++ result = jetty.lookForBindException(test);+ assertEquals(result, test);++ test = new IOException();+ result = jetty.lookForBindException(test);+ assertEquals(result, test);++ test = new IOException((Throwable) null);+ result = jetty.lookForBindException(test);+ assertEquals(result, test);++ test = new IOException() {+ @Override+ public synchronized Throwable getCause() {+ return this;+ }+ };+ result = jetty.lookForBindException(test);+ assertEquals(result, test);++ test = new IOException(new RuntimeException());+ result = jetty.lookForBindException(test);+ assertEquals(result, test);++ test = new IOException(new RuntimeException(be));+ result = jetty.lookForBindException(test);+ assertEquals(result, be);++ }++}