This is an automated email from the ASF dual-hosted git repository.

psalagnac pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 7db165f8cd0 Make TestCpuTimeSearch cloud flavored, and re-enable 
distributed test (#3755)
7db165f8cd0 is described below

commit 7db165f8cd01471bf7717f793903a88e40511dfb
Author: Pierre Salagnac <[email protected]>
AuthorDate: Sat Oct 11 09:05:15 2025 +0200

    Make TestCpuTimeSearch cloud flavored, and re-enable distributed test 
(#3755)
    
    This is a rework of TestCpuTimeSearch SOLR-16986 so it now runs in cloud 
mode. For some reasons, the distributed test (with multiple shard) was flaky, 
depending on the seed. It is much easier to maintain now it is aligned with 
most of multi-shard tests.
---
 .../test/org/apache/solr/TestCpuTimeSearch.java    | 116 ++++++++++++---------
 1 file changed, 66 insertions(+), 50 deletions(-)

diff --git a/solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java 
b/solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java
index e2bb71f291e..2c1e8d1da2e 100644
--- a/solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java
+++ b/solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java
@@ -17,59 +17,70 @@
 package org.apache.solr;
 
 import java.io.IOException;
-import java.nio.file.Path;
 import java.util.List;
-import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
 import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.common.SolrDocumentList;
 import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.common.params.ShardParams;
+import org.apache.solr.common.cloud.CompositeIdRouter;
+import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.DocRouter;
+import org.apache.solr.common.cloud.Replica;
 import org.apache.solr.common.util.NamedList;
-import org.apache.solr.util.SolrJettyTestRule;
 import org.apache.solr.util.ThreadCpuTimer;
 import org.junit.BeforeClass;
-import org.junit.ClassRule;
-import org.junit.Ignore;
 
-// TODO @Seed("861F5769E4B6F696")
-public class TestCpuTimeSearch extends SolrTestCaseJ4 {
+public class TestCpuTimeSearch extends SolrCloudTestCase {
 
-  @ClassRule public static final SolrJettyTestRule solrRule = new 
SolrJettyTestRule();
-
-  private static String shard1;
-  private static String shard2;
+  private static final String COLLECTION = "cpu-col";
 
   @BeforeClass
   public static void setupSolr() throws Exception {
     System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
 
-    Path configSet = createTempDir("configSet");
-    copyMinConf(configSet);
-    solrRule.startSolr(LuceneTestCase.createTempDir());
-
-    
solrRule.newCollection("core1").withConfigSet(configSet.toString()).create();
-    
solrRule.newCollection("core2").withConfigSet(configSet.toString()).create();
-    var clientCore1 = solrRule.getSolrClient("core1");
-    var clientCore2 = solrRule.getSolrClient("core2");
-
-    String urlCore1 = solrRule.getBaseUrl() + "/" + "core1";
-    String urlCore2 = solrRule.getBaseUrl() + "/" + "core2";
-    shard1 = urlCore1.replaceAll("https?://", "");
-    shard2 = urlCore2.replaceAll("https?://", "");
-
-    SolrInputDocument doc = new SolrInputDocument();
-    doc.setField("id", "1");
-    doc.setField("subject", "batman");
-    doc.setField("title", "foo bar");
-    clientCore1.add(doc);
-    clientCore1.commit();
-
-    doc.setField("id", "2");
-    doc.setField("subject", "superman");
-    clientCore2.add(doc);
-    clientCore2.commit();
+    configureCluster(1)
+        .addConfig(
+            "conf", 
TEST_PATH().resolve("configsets").resolve("cloud-minimal").resolve("conf"))
+        .configure();
+
+    CollectionAdminRequest.createCollection(COLLECTION, "conf", 2, 1)
+        .setRouterName(CompositeIdRouter.NAME)
+        .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
+
+    waitForState("New collection", COLLECTION, clusterShape(2, 2));
+    DocCollection coll = getCollectionState(COLLECTION);
+
+    DocRouter router = DocRouter.getDocRouter(CompositeIdRouter.NAME);
+
+    UpdateRequest req = new UpdateRequest();
+
+    SolrInputDocument doc1 = new SolrInputDocument();
+    String id1 = "1";
+    doc1.setField("id", id1);
+    doc1.setField("subject", "batman");
+    doc1.setField("title", "foo bar");
+    req.add(doc1);
+
+    // For second record, generate an ID until we are sure it will be indexed
+    // in a different shard from first record
+    String slice1 = router.getTargetSlice(id1, doc1, null, null, 
coll).getName();
+    String slice2;
+    String id2;
+    do {
+      id2 = Integer.toString(random().nextInt(Integer.MAX_VALUE));
+      slice2 = router.getTargetSlice(id2, doc1, null, null, coll).getName();
+    } while (slice1.equals(slice2));
+
+    SolrInputDocument doc2 = new SolrInputDocument();
+    doc2.setField("id", "2");
+    doc2.setField("subject", "superman");
+    req.add(doc2);
+    req.commit(cluster.getSolrClient(), COLLECTION);
   }
 
   public void testWithoutDistrib() throws SolrServerException, IOException {
@@ -77,8 +88,21 @@ public class TestCpuTimeSearch extends SolrTestCaseJ4 {
     query.setQuery("subject:batman OR subject:superman");
     query.addField("id");
     query.addField("subject");
-
-    QueryResponse response = solrRule.getSolrClient("core1").query(query);
+    query.setDistrib(false);
+
+    QueryResponse response;
+    DocCollection coll = getCollectionState(COLLECTION);
+
+    // Send the query to a random replica of the collection.
+    // Distributing the query is disabled, so we should get a single result 
record.
+    Replica randomReplica =
+        pickRandom(
+            coll.getSlices().stream()
+                .flatMap(s -> s.getReplicas().stream())
+                .toArray(Replica[]::new));
+    try (SolrClient client = getHttpSolrClient(randomReplica.getCoreUrl())) {
+      response = client.query(query);
+    }
 
     SolrDocumentList results = response.getResults();
     int size = results.size();
@@ -88,25 +112,17 @@ public class TestCpuTimeSearch extends SolrTestCaseJ4 {
     List<Object> cpuTimes = header.getAll(ThreadCpuTimer.CPU_TIME);
     assertEquals("localCpuTime should not have values", 0, 
localCpuTimes.size());
     assertEquals("cpuTime should only have one value", 1, cpuTimes.size());
-    long cpuTime = (long) cpuTimes.iterator().next();
+    long cpuTime = (long) cpuTimes.getFirst();
     assertTrue("cpuTime (" + cpuTime + ") should be positive", cpuTime >= 0);
   }
 
-  @Ignore
   public void testWithDistrib() throws SolrServerException, IOException {
     SolrQuery query = new SolrQuery();
     query.setQuery("subject:batman OR subject:superman");
     query.addField("id");
     query.addField("subject");
-    query.set("distrib", "true");
-    query.set("shards", shard1 + "," + shard2);
-    query.set(ShardParams.SHARDS_INFO, "true");
-    query.set("debug", "true");
-    query.set("stats", "true");
-    query.set("stats.field", "id");
-    query.set(ShardParams.SHARDS_TOLERANT, "true");
 
-    QueryResponse response = solrRule.getSolrClient("core1").query(query);
+    QueryResponse response = cluster.getSolrClient(COLLECTION).query(query);
 
     SolrDocumentList results = response.getResults();
     int size = results.size();
@@ -116,7 +132,7 @@ public class TestCpuTimeSearch extends SolrTestCaseJ4 {
     List<Object> cpuTimes = header.getAll(ThreadCpuTimer.CPU_TIME);
     assertEquals("localCpuTime should not have values", 0, 
localCpuTimes.size());
     assertEquals("cpuTime should only have one value", 1, cpuTimes.size());
-    long cpuTime = (long) cpuTimes.iterator().next();
+    long cpuTime = (long) cpuTimes.getFirst();
     assertTrue("cpuTime (" + cpuTime + ") should be positive", cpuTime >= 0);
   }
 }

Reply via email to