Skip to content

Apache Lucene open-source search software

SOLR-11469: Sanity check assertions about coreNodeNames are identical in 2 collections

Cao Manh Dat81a4f7cc9ceb
diff --git a/solr/core/src/test/org/apache/solr/cloud/LeaderElectionContextKeyTest.java b/solr/core/src/test/org/apache/solr/cloud/LeaderElectionContextKeyTest.java
index a98cf507249..dd265aa85aa 100644
--- a/solr/core/src/test/org/apache/solr/cloud/LeaderElectionContextKeyTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/LeaderElectionContextKeyTest.java
@@ -26,7 +26,7 @@ import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
-import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkStateReader;
@@ -72,14 +72,26 @@ public class LeaderElectionContextKeyTest extends SolrCloudTestCase {
public void test() throws KeeperException, InterruptedException, IOException, SolrServerException {
ZkStateReader stateReader = cluster.getSolrClient().getZkStateReader();
stateReader.forceUpdateCollection(TEST_COLLECTION_1);
- List<Replica> replicasOfCollection1 = stateReader.getClusterState().getCollection(TEST_COLLECTION_1).getReplicas();
- List<Replica> replicasOfCollection2 = stateReader.getClusterState().getCollection(TEST_COLLECTION_2).getReplicas();
- Replica replica = findLeaderReplicaWithDuplicatedName(replicasOfCollection1, replicasOfCollection2);
- String shard = getShard(stateReader.getClusterState().getCollection(TEST_COLLECTION_1), replica);
+ ClusterState clusterState = stateReader.getClusterState();
+ // The test assume that TEST_COLLECTION_1 and TEST_COLLECTION_2 will have identical layout
+ // ( same replica's name on every shard )
+ for (int i = 1; i <= 2; i++) {
+ String coll1ShardiLeader = clusterState.getCollection(TEST_COLLECTION_1).getLeader("shard"+i).getName();
+ String coll2ShardiLeader = clusterState.getCollection(TEST_COLLECTION_2).getLeader("shard"+i).getName();
+ String assertMss = String.format("Expect %s and %s each have a replica with same name on shard %s",
+ coll1ShardiLeader, coll2ShardiLeader, "shard"+i);
+ assertEquals(
+ assertMss,
+ coll1ShardiLeader,
+ coll2ShardiLeader
+ );
+ }
+
+ String shard = "shard" + String.valueOf(random().nextInt(2) + 1);
+ Replica replica = clusterState.getCollection(TEST_COLLECTION_1).getLeader(shard);
assertNotNull(replica);
- SolrClient shardLeaderClient = new HttpSolrClient.Builder(replica.get("base_url").toString()).build();
- try {
+ try (SolrClient shardLeaderClient = new HttpSolrClient.Builder(replica.get("base_url").toString()).build()) {
assertEquals(1L, getElectionNodes(TEST_COLLECTION_1, shard, stateReader.getZkClient()).size());
List<String> collection2Shard1Nodes = getElectionNodes(TEST_COLLECTION_2, "shard1", stateReader.getZkClient());
List<String> collection2Shard2Nodes = getElectionNodes(TEST_COLLECTION_2, "shard2", stateReader.getZkClient());
@@ -99,27 +111,7 @@ public class LeaderElectionContextKeyTest extends SolrCloudTestCase {
// There are no leader election was kicked off on testCollection2
assertThat(collection2Shard1Nodes, CoreMatchers.is(getElectionNodes(TEST_COLLECTION_2, "shard1", stateReader.getZkClient())));
assertThat(collection2Shard2Nodes, CoreMatchers.is(getElectionNodes(TEST_COLLECTION_2, "shard2", stateReader.getZkClient())));
- } finally {
- shardLeaderClient.close();
- }
- }
-
- private String getShard(DocCollection docCollection, Replica replica) {
- return docCollection.getShardId(replica.getNodeName(), replica.getCoreName());
- }
-
- private Replica findLeaderReplicaWithDuplicatedName(List<Replica> replicas1, List<Replica> replicas2) {
- for (Replica replica1 : replicas1) {
- if (!replica1.containsKey("leader")) continue;
- for (Replica replica2 : replicas2) {
- if (replica1.getName().equals(replica2.getName())
- && replica1.get("base_url").equals(replica2.get("base_url"))
- && replica2.containsKey("leader")) {
- return replica1;
- }
- }
}
- return null;
}
private List<String> getElectionNodes(String collection, String shard, SolrZkClient client) throws KeeperException, InterruptedException {