This is an automated email from the ASF dual-hosted git repository. jdyer pushed a commit to branch feature/SOLR-17516-c in repository https://gitbox.apache.org/repos/asf/solr.git
commit ec026e2fd9ffd5dc446928717a7e5de0e4f2f396 Author: jdyer1 <[email protected]> AuthorDate: Mon Oct 28 18:19:49 2024 -0500 - Randomly use either http client delegate in integration test - Fix JDK Client to throw the correct exception when IOException occurs --- .../solr/client/solrj/impl/HttpJdkSolrClient.java | 4 +- .../impl/LBHttp2SolrClientIntegrationTest.java | 95 ++++++++++++++-------- 2 files changed, 63 insertions(+), 36 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java index d3610f8d544..86a49f9fdec 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java @@ -173,8 +173,8 @@ public class HttpJdkSolrClient extends HttpSolrClientBase { "Timeout occurred while waiting response from server at: " + pReq.url, e); } catch (SolrException se) { throw se; - } catch (RuntimeException re) { - throw new SolrServerException(re); + } catch (IOException | RuntimeException e) { + throw new SolrServerException(e); } finally { if (pReq.contentWritingFuture != null) { pReq.contentWritingFuture.cancel(true); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java index 4347ba6f4f2..05a762669d5 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientIntegrationTest.java @@ -18,6 +18,7 @@ package org.apache.solr.client.solrj.impl; import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.lang.invoke.MethodHandles; import java.nio.file.Files; import java.nio.file.Path; @@ -57,7 +58,6 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); SolrInstance[] solr = new SolrInstance[3]; - Http2SolrClient httpClient; // TODO: fix this test to not require FSDirectory static String savedFactory; @@ -82,11 +82,6 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { @Override public void setUp() throws Exception { super.setUp(); - httpClient = - new Http2SolrClient.Builder() - .withConnectionTimeout(1000, TimeUnit.MILLISECONDS) - .withIdleTimeout(2000, TimeUnit.MILLISECONDS) - .build(); for (int i = 0; i < solr.length; i++) { solr[i] = @@ -96,7 +91,6 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { addDocs(solr[i]); } } - private void addDocs(SolrInstance solrInstance) throws IOException, SolrServerException { List<SolrInputDocument> docs = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -122,22 +116,41 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { aSolr.tearDown(); } } - httpClient.close(); super.tearDown(); } + private LBClientHolder client(LBSolrClient.Endpoint... baseSolrEndpoints) { + if(random().nextBoolean()) { + var delegateClient = new Http2SolrClient.Builder() + .withConnectionTimeout(1000, TimeUnit.MILLISECONDS) + .withIdleTimeout(2000, TimeUnit.MILLISECONDS) + .build(); + var lbClient = new LBHttp2SolrClient.Builder(delegateClient, baseSolrEndpoints) + .withDefaultCollection(solr[0].getDefaultCollection()) + .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) + .build(); + return new LBClientHolder(lbClient, delegateClient); + } else { + var delegateClient = new HttpJdkSolrClient.Builder() + .withConnectionTimeout(1000, TimeUnit.MILLISECONDS) + .withIdleTimeout(2000, TimeUnit.MILLISECONDS) + .build(); + var lbClient = new LBHttpJdkSolrClient.Builder(delegateClient, baseSolrEndpoints) + .withDefaultCollection(solr[0].getDefaultCollection()) + .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) + .build(); + return new LBClientHolder(lbClient, delegateClient); + } + } + public void testSimple() throws Exception { final var baseSolrEndpoints = bootstrapBaseSolrEndpoints(solr.length); - try (LBHttp2SolrClient client = - new LBHttp2SolrClient.Builder(httpClient, baseSolrEndpoints) - .withDefaultCollection(solr[0].getDefaultCollection()) - .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) - .build()) { + try (var h = client(baseSolrEndpoints)) { SolrQuery solrQuery = new SolrQuery("*:*"); Set<String> names = new HashSet<>(); QueryResponse resp = null; for (int i = 0; i < solr.length; i++) { - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); assertEquals(10, resp.getResults().getNumFound()); names.add(resp.getResults().get(0).getFieldValue("name").toString()); } @@ -148,7 +161,7 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { solr[1].jetty = null; names.clear(); for (int i = 0; i < solr.length; i++) { - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); assertEquals(10, resp.getResults().getNumFound()); names.add(resp.getResults().get(0).getFieldValue("name").toString()); } @@ -161,7 +174,7 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { Thread.sleep(1200); names.clear(); for (int i = 0; i < solr.length; i++) { - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); assertEquals(10, resp.getResults().getNumFound()); names.add(resp.getResults().get(0).getFieldValue("name").toString()); } @@ -171,19 +184,15 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { public void testTwoServers() throws Exception { final var baseSolrEndpoints = bootstrapBaseSolrEndpoints(2); - try (LBHttp2SolrClient client = - new LBHttp2SolrClient.Builder(httpClient, baseSolrEndpoints) - .withDefaultCollection(solr[0].getDefaultCollection()) - .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) - .build()) { + try (var h = client(baseSolrEndpoints)) { SolrQuery solrQuery = new SolrQuery("*:*"); QueryResponse resp = null; solr[0].jetty.stop(); solr[0].jetty = null; - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); String name = resp.getResults().get(0).getFieldValue("name").toString(); assertEquals("solr/collection11", name); - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); name = resp.getResults().get(0).getFieldValue("name").toString(); assertEquals("solr/collection11", name); solr[1].jetty.stop(); @@ -191,11 +200,11 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { solr[0].startJetty(); Thread.sleep(1200); try { - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); } catch (SolrServerException e) { // try again after a pause in case the error is lack of time to start server Thread.sleep(3000); - resp = client.query(solrQuery); + resp = h.lbClient.query(solrQuery); } name = resp.getResults().get(0).getFieldValue("name").toString(); assertEquals("solr/collection10", name); @@ -204,30 +213,27 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { public void testReliability() throws Exception { final var baseSolrEndpoints = bootstrapBaseSolrEndpoints(solr.length); - - try (LBHttp2SolrClient client = - new LBHttp2SolrClient.Builder(httpClient, baseSolrEndpoints) - .withDefaultCollection(solr[0].getDefaultCollection()) - .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) - .build()) { + try (var h = client(baseSolrEndpoints)) { // Kill a server and test again solr[1].jetty.stop(); solr[1].jetty = null; // query the servers - for (int i = 0; i < solr.length; i++) client.query(new SolrQuery("*:*")); + for (int i = 0; i < solr.length; i++) { + h.lbClient.query(new SolrQuery("*:*")); + } // Start the killed server once again solr[1].startJetty(); // Wait for the alive check to complete - waitForServer(30, client, 3, solr[1].name); + waitForServer(30, h.lbClient, 3, solr[1].name); } } // wait maximum ms for serverName to come back up private void waitForServer( - int maxSeconds, LBHttp2SolrClient client, int nServers, String serverName) throws Exception { + int maxSeconds, LBHttpSolrClientBase<?> client, int nServers, String serverName) throws Exception { final TimeOut timeout = new TimeOut(maxSeconds, TimeUnit.SECONDS, TimeSource.NANO_TIME); while (!timeout.hasTimedOut()) { QueryResponse resp; @@ -344,4 +350,25 @@ public class LBHttp2SolrClientIntegrationTest extends SolrTestCaseJ4 { // Thread.sleep(5000); } } + + private static class LBClientHolder implements AutoCloseable { + + final LBHttpSolrClientBase<?> lbClient; + final HttpSolrClientBase delegate; + + LBClientHolder(LBHttpSolrClientBase<?> lbClient, HttpSolrClientBase delegate) { + this.lbClient = lbClient; + this.delegate = delegate; + } + + @Override + public void close() { + lbClient.close(); + try { + delegate.close(); + } catch(IOException ioe) { + throw new UncheckedIOException(ioe); + } + } + } }
