Apache Lucene open-source search software
SOLR-6157: Refactor the ensureAllReplicasAreActive method into base class and ensure the ClusterState is updated to address intermittent test failures on Jenkins.
Timothy Potterf512330b1450
diff --git a/solr/core/src/test/org/apache/solr/cloud/HttpPartitionTest.java b/solr/core/src/test/org/apache/solr/cloud/HttpPartitionTest.javaindex 0f7ab61b011..5e28beaaf23 100644--- a/solr/core/src/test/org/apache/solr/cloud/HttpPartitionTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/HttpPartitionTest.java@@ -22,7 +22,6 @@ import java.net.ServerSocket;import java.net.URI;import java.net.URL;import java.util.ArrayList;-import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;@@ -158,7 +157,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {sendDoc(1);Replica notLeader =- ensureAllReplicasAreActive(testCollectionName, 2, maxWaitSecsToSeeAllActive).get(0);+ ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive).get(0);// ok, now introduce a network partition between the leader and the replicaSocketProxy proxy = getProxyForReplica(notLeader);@@ -180,7 +179,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {proxy.reopen();List<Replica> notLeaders =- ensureAllReplicasAreActive(testCollectionName, 2, maxWaitSecsToSeeAllActive);+ ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);sendDoc(3);@@ -212,7 +211,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {proxy.reopen();}- notLeaders = ensureAllReplicasAreActive(testCollectionName, 2, maxWaitSecsToSeeAllActive);+ notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);// verify all docs receivedassertDocsExistInAllReplicas(notLeaders, testCollectionName, 1, numDocs + 3);@@ -227,7 +226,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {sendDoc(1);List<Replica> notLeaders =- ensureAllReplicasAreActive(testCollectionName, 3, maxWaitSecsToSeeAllActive);+ ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 3, maxWaitSecsToSeeAllActive);assertTrue("Expected 2 replicas for collection " + testCollectionName+ " but found " + notLeaders.size() + "; clusterState: "+ printClusterStateInfo(),@@ -257,7 +256,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {proxy1.reopen();// sent 4 docs in so far, verify they are on the leader and replica- notLeaders = ensureAllReplicasAreActive(testCollectionName, 3, maxWaitSecsToSeeAllActive);+ notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 3, maxWaitSecsToSeeAllActive);sendDoc(4);@@ -275,7 +274,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {sendDoc(1);List<Replica> notLeaders =- ensureAllReplicasAreActive(testCollectionName, 3, maxWaitSecsToSeeAllActive);+ ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 3, maxWaitSecsToSeeAllActive);assertTrue("Expected 2 replicas for collection " + testCollectionName+ " but found " + notLeaders.size() + "; clusterState: "+ printClusterStateInfo(),@@ -306,7 +305,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {proxy1.reopen();// sent 4 docs in so far, verify they are on the leader and replica- notLeaders = ensureAllReplicasAreActive(testCollectionName, 3, maxWaitSecsToSeeAllActive);+ notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 3, maxWaitSecsToSeeAllActive);sendDoc(4);@@ -320,7 +319,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {JettySolrRunner leaderJetty = getJettyOnPort(getReplicaPort(leader));// since maxShardsPerNode is 1, we're safe to kill the leader- notLeaders = ensureAllReplicasAreActive(testCollectionName, 3, maxWaitSecsToSeeAllActive);+ notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 3, maxWaitSecsToSeeAllActive);proxy0 = getProxyForReplica(notLeaders.get(0));proxy0.close();@@ -373,12 +372,7 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {assertDocsExistInAllReplicas(activeReps, testCollectionName, 1, 6);}-- protected String printClusterStateInfo() throws Exception {- cloudClient.getZkStateReader().updateClusterState(true);- return String.valueOf(cloudClient.getZkStateReader().getClusterState());- }-+protected List<Replica> getActiveOrRecoveringReplicas(String testCollectionName, String shardId) throws Exception {Map<String,Replica> activeReplicas = new HashMap<String,Replica>();ZkStateReader zkr = cloudClient.getZkStateReader();@@ -457,66 +451,6 @@ public class HttpPartitionTest extends AbstractFullDistribZkTestBase {cloudClient.add(doc);}- protected List<Replica> ensureAllReplicasAreActive(String testCollectionName, int rf, int maxWaitSecs) throws Exception {- long startMs = System.currentTimeMillis();-- Map<String,Replica> notLeaders = new HashMap<String,Replica>();-- ZkStateReader zkr = cloudClient.getZkStateReader();- ClusterState cs = zkr.getClusterState();- Collection<Slice> slices = cs.getActiveSlices(testCollectionName);- assertTrue(slices.size() == 1); // shards == 1- boolean allReplicasUp = false;- long waitMs = 0L;- long maxWaitMs = maxWaitSecs * 1000L;- Replica leader = null;- while (waitMs < maxWaitMs && !allReplicasUp) {- cs = zkr.getClusterState();- assertNotNull(cs);- for (Slice shard : cs.getActiveSlices(testCollectionName)) {- allReplicasUp = true; // assume true- Collection<Replica> replicas = shard.getReplicas();- assertTrue(replicas.size() == rf);- leader = shard.getLeader();- assertNotNull(leader);-- // ensure all replicas are "active" and identify the non-leader replica- for (Replica replica : replicas) {- String replicaState = replica.getStr(ZkStateReader.STATE_PROP);- if (!ZkStateReader.ACTIVE.equals(replicaState)) {- log.info("Replica " + replica.getName() + " is currently " + replicaState);- allReplicasUp = false;- }-- if (!leader.equals(replica))- notLeaders.put(replica.getName(), replica);- }-- if (!allReplicasUp) {- try {- Thread.sleep(500L);- } catch (Exception ignoreMe) {}- waitMs += 500L;- }- }- } // end while-- if (!allReplicasUp)- fail("Didn't see all replicas come up within " + maxWaitMs +- " ms! ClusterState: " + printClusterStateInfo());-- if (notLeaders.isEmpty())- fail("Didn't isolate any replicas that are not the leader! ClusterState: " +- printClusterStateInfo());-- long diffMs = (System.currentTimeMillis() - startMs);- log.info("Took " + diffMs + " ms to see all replicas become active.");-- List<Replica> replicas = new ArrayList<Replica>();- replicas.addAll(notLeaders.values());- return replicas;- }-/*** Query the real-time get handler for a specific doc by ID to verify it* exists in the provided server.diff --git a/solr/core/src/test/org/apache/solr/cloud/ReplicationFactorTest.java b/solr/core/src/test/org/apache/solr/cloud/ReplicationFactorTest.javaindex 86ef1f3d0a8..4ad450632e1 100644--- a/solr/core/src/test/org/apache/solr/cloud/ReplicationFactorTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ReplicationFactorTest.java@@ -22,22 +22,18 @@ import java.net.ServerSocket;import java.net.URI;import java.net.URL;import java.util.ArrayList;-import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;-import org.apache.lucene.util.LuceneTestCase.AwaitsFix;import org.apache.lucene.util.LuceneTestCase.Slow;import org.apache.solr.SolrTestCaseJ4.SuppressSSL;import org.apache.solr.client.solrj.embedded.JettySolrRunner;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.common.SolrInputDocument;-import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.Replica;-import org.apache.solr.common.cloud.Slice;import org.apache.solr.common.cloud.ZkCoreNodeProps;import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.common.util.NamedList;@@ -49,7 +45,6 @@ import org.slf4j.LoggerFactory;/*** Tests a client application's ability to get replication factor* information back from the cluster after an add or update.- * @AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-6157")*/@Slow@SuppressSSL(bugUrl = "https://issues.apache.org/jira/browse/SOLR-5776")@@ -164,7 +159,7 @@ public class ReplicationFactorTest extends AbstractFullDistribZkTestBase {cloudClient.setDefaultCollection(testCollectionName);List<Replica> replicas =- ensureAllReplicasAreActive(testCollectionName, shardId, numShards, replicationFactor, 10);+ ensureAllReplicasAreActive(testCollectionName, shardId, numShards, replicationFactor, 30);assertTrue("Expected active 1 replicas for "+testCollectionName, replicas.size() == 1);List<SolrInputDocument> batch = new ArrayList<SolrInputDocument>(10);@@ -186,7 +181,7 @@ public class ReplicationFactorTest extends AbstractFullDistribZkTestBase {// so now kill the replica of shard2 and verify the achieved rf is only 1List<Replica> shard2Replicas =- ensureAllReplicasAreActive(testCollectionName, "shard2", numShards, replicationFactor, 10);+ ensureAllReplicasAreActive(testCollectionName, "shard2", numShards, replicationFactor, 30);assertTrue("Expected active 1 replicas for "+testCollectionName, replicas.size() == 1);getProxyForReplica(shard2Replicas.get(0)).close();@@ -234,7 +229,7 @@ public class ReplicationFactorTest extends AbstractFullDistribZkTestBase {cloudClient.setDefaultCollection(testCollectionName);List<Replica> replicas =- ensureAllReplicasAreActive(testCollectionName, shardId, numShards, replicationFactor, 10);+ ensureAllReplicasAreActive(testCollectionName, shardId, numShards, replicationFactor, 30);assertTrue("Expected 2 active replicas for "+testCollectionName, replicas.size() == 2);int rf = sendDoc(1, minRf);@@ -344,68 +339,6 @@ public class ReplicationFactorTest extends AbstractFullDistribZkTestBase {up.add(doc);return cloudClient.getMinAchievedReplicationFactor(cloudClient.getDefaultCollection(), cloudClient.request(up));}-- protected List<Replica> ensureAllReplicasAreActive(String testCollectionName, String shardId, int shards, int rf, int maxWaitSecs) throws Exception {- long startMs = System.currentTimeMillis();-- Map<String,Replica> notLeaders = new HashMap<String,Replica>();-- ZkStateReader zkr = cloudClient.getZkStateReader();- zkr.updateClusterState(true); // force the state to be fresh-- ClusterState cs = zkr.getClusterState();- Collection<Slice> slices = cs.getActiveSlices(testCollectionName);- assertTrue(slices.size() == shards);- boolean allReplicasUp = false;- long waitMs = 0L;- long maxWaitMs = maxWaitSecs * 1000L;- Replica leader = null;- while (waitMs < maxWaitMs && !allReplicasUp) {- cs = zkr.getClusterState();- assertNotNull(cs);- Slice shard = cs.getSlice(testCollectionName, shardId);- assertNotNull("No Slice for "+shardId, shard);- allReplicasUp = true; // assume true- Collection<Replica> replicas = shard.getReplicas();- assertTrue(replicas.size() == rf);- leader = shard.getLeader();- assertNotNull(leader);- log.info("Found "+replicas.size()+" replicas and leader on "+- leader.getNodeName()+" for "+shardId+" in "+testCollectionName);-- // ensure all replicas are "active" and identify the non-leader replica- for (Replica replica : replicas) {- String replicaState = replica.getStr(ZkStateReader.STATE_PROP);- if (!ZkStateReader.ACTIVE.equals(replicaState)) {- log.info("Replica " + replica.getName() + " is currently " + replicaState);- allReplicasUp = false;- }-- if (!leader.equals(replica))- notLeaders.put(replica.getName(), replica);- }-- if (!allReplicasUp) {- try {- Thread.sleep(500L);- } catch (Exception ignoreMe) {}- waitMs += 500L;- }- } // end while-- if (!allReplicasUp)- fail("Didn't see all replicas come up within " + maxWaitMs + " ms! ClusterState: " + printClusterStateInfo());-- if (notLeaders.isEmpty())- fail("Didn't isolate any replicas that are not the leader! ClusterState: " + printClusterStateInfo());-- long diffMs = (System.currentTimeMillis() - startMs);- log.info("Took " + diffMs + " ms to see all replicas become active.");-- List<Replica> replicas = new ArrayList<Replica>();- replicas.addAll(notLeaders.values());- return replicas;- }protected void assertRf(int expected, String explain, int actual) throws Exception {if (actual != expected) {@@ -413,10 +346,5 @@ public class ReplicationFactorTest extends AbstractFullDistribZkTestBase {String.format(Locale.ENGLISH, "Expected rf=%d because %s but got %d", expected, explain, actual);fail(assertionFailedMessage+"; clusterState: "+printClusterStateInfo());}- }-- protected String printClusterStateInfo() throws Exception {- cloudClient.getZkStateReader().updateClusterState(true);- return String.valueOf(cloudClient.getZkStateReader().getClusterState());}}diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.javaindex 69cc71138ea..24f1ee29b86 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java@@ -29,6 +29,7 @@ import java.io.IOException;import java.net.MalformedURLException;import java.net.URI;import java.util.ArrayList;+import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;@@ -1755,4 +1756,75 @@ public abstract class AbstractFullDistribZkTestBase extends AbstractDistribZkTescreateCollection(collectionInfos, collName, props, client);}+ protected List<Replica> ensureAllReplicasAreActive(String testCollectionName, String shardId, int shards, int rf, int maxWaitSecs) throws Exception {+ long startMs = System.currentTimeMillis();++ Map<String,Replica> notLeaders = new HashMap<String,Replica>();++ ZkStateReader zkr = cloudClient.getZkStateReader();+ zkr.updateClusterState(true); // force the state to be fresh++ ClusterState cs = zkr.getClusterState();+ Collection<Slice> slices = cs.getActiveSlices(testCollectionName);+ assertTrue(slices.size() == shards);+ boolean allReplicasUp = false;+ long waitMs = 0L;+ long maxWaitMs = maxWaitSecs * 1000L;+ Replica leader = null;+ while (waitMs < maxWaitMs && !allReplicasUp) {+ // refresh state every 2 secs+ if (waitMs % 2000 == 0)+ cloudClient.getZkStateReader().updateClusterState(true);++ cs = cloudClient.getZkStateReader().getClusterState();+ assertNotNull(cs);+ Slice shard = cs.getSlice(testCollectionName, shardId);+ assertNotNull("No Slice for "+shardId, shard);+ allReplicasUp = true; // assume true+ Collection<Replica> replicas = shard.getReplicas();+ assertTrue(replicas.size() == rf);+ leader = shard.getLeader();+ assertNotNull(leader);+ log.info("Found "+replicas.size()+" replicas and leader on "++ leader.getNodeName()+" for "+shardId+" in "+testCollectionName);++ // ensure all replicas are "active" and identify the non-leader replica+ for (Replica replica : replicas) {+ String replicaState = replica.getStr(ZkStateReader.STATE_PROP);+ if (!ZkStateReader.ACTIVE.equals(replicaState)) {+ log.info("Replica " + replica.getName() + " is currently " + replicaState);+ allReplicasUp = false;+ }++ if (!leader.equals(replica))+ notLeaders.put(replica.getName(), replica);+ }++ if (!allReplicasUp) {+ try {+ Thread.sleep(500L);+ } catch (Exception ignoreMe) {}+ waitMs += 500L;+ }+ } // end while++ if (!allReplicasUp)+ fail("Didn't see all replicas for shard "+shardId+" in "+testCollectionName++ " come up within " + maxWaitMs + " ms! ClusterState: " + printClusterStateInfo());++ if (notLeaders.isEmpty())+ fail("Didn't isolate any replicas that are not the leader! ClusterState: " + printClusterStateInfo());++ long diffMs = (System.currentTimeMillis() - startMs);+ log.info("Took " + diffMs + " ms to see all replicas become active.");++ List<Replica> replicas = new ArrayList<Replica>();+ replicas.addAll(notLeaders.values());+ return replicas;+ }++ protected String printClusterStateInfo() throws Exception {+ cloudClient.getZkStateReader().updateClusterState(true);+ return String.valueOf(cloudClient.getZkStateReader().getClusterState());+ }}