Apache Solr open-source search software
SOLR-18025 Test LeaderTragicEventTest flaky (#3939)
Jan Høydahl5d8220c599fa
diff --git a/solr/core/src/test/org/apache/solr/cloud/LeaderTragicEventTest.java b/solr/core/src/test/org/apache/solr/cloud/LeaderTragicEventTest.javaindex c1520025fe8..01d84378474 100644--- a/solr/core/src/test/org/apache/solr/cloud/LeaderTragicEventTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/LeaderTragicEventTest.java@@ -26,7 +26,7 @@ import org.apache.lucene.store.AlreadyClosedException;import org.apache.solr.client.solrj.RemoteSolrException;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrServerException;-import org.apache.solr.client.solrj.apache.HttpSolrClient;+import org.apache.solr.client.solrj.jetty.HttpJettySolrClient;import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.client.solrj.request.QueryRequest;import org.apache.solr.client.solrj.request.SolrQuery;@@ -113,7 +113,7 @@ public class LeaderTragicEventTest extends SolrCloudTestCase {updateResponse = new UpdateRequest().add("id", "2").commit(cluster.getSolrClient(), collection);assertEquals(0, updateResponse.getStatus());try (SolrClient followerClient =- new HttpSolrClient.Builder(oldLeader.getBaseUrl())+ new HttpJettySolrClient.Builder(oldLeader.getBaseUrl()).withDefaultCollection(oldLeader.getCoreName()).build()) {QueryResponse queryResponse = new QueryRequest(new SolrQuery("*:*")).process(followerClient);@@ -131,15 +131,45 @@ public class LeaderTragicEventTest extends SolrCloudTestCase {log.info("Will crash leader : {}", oldLeader);final Replica leaderReplica = dc.getLeader("shard1");- try (SolrClient solrClient = new HttpSolrClient.Builder(leaderReplica.getBaseUrl()).build()) {- new UpdateRequest().add("id", "99").commit(solrClient, leaderReplica.getCoreName());- fail("Should have injected tragedy");- } catch (RemoteSolrException e) {- // solrClient.add would throw RemoteSolrException with code 500- // or 404 if the bad replica has already been deleted- assertThat(e.code(), anyOf(is(500), is(404)));- } catch (AlreadyClosedException e) {- // If giving up leadership, might be already closed/closing++ // Retry up to 3 times to ensure the tragic event is properly triggered+ // This works around LUCENE-8692 where getTragicException() may not reflect all exceptions+ int attempts = 0;+ int maxAttempts = 3;+ boolean tragedyTriggered = false;++ while (attempts < maxAttempts && !tragedyTriggered) {+ attempts++;+ try (SolrClient solrClient =+ new HttpJettySolrClient.Builder(leaderReplica.getBaseUrl()).build()) {+ new UpdateRequest()+ .add("id", "99_attempt_" + attempts)+ .commit(solrClient, leaderReplica.getCoreName());+ } catch (RemoteSolrException e) {+ // solrClient.add would throw RemoteSolrException with code 500+ // or 404 if the bad replica has already been deleted+ assertThat(e.code(), anyOf(is(500), is(404)));+ log.info("Tragic event triggered on attempt {}", attempts);+ tragedyTriggered = true;+ } catch (AlreadyClosedException e) {+ // If giving up leadership, might be already closed/closing+ log.info("Core already closed on attempt {} (leadership likely given up)", attempts);+ tragedyTriggered = true;+ }++ // Brief pause between attempts to let the system stabilize+ if (!tragedyTriggered && attempts < maxAttempts) {+ try {+ Thread.sleep(100);+ } catch (InterruptedException ie) {+ Thread.currentThread().interrupt();+ throw new RuntimeException(ie);+ }+ }+ }++ if (!tragedyTriggered) {+ fail("Failed to trigger tragic event after " + maxAttempts + " attempts");}return oldLeader;