Skip to content

Commit 7db165f

Browse files
authored
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.
1 parent bad307e commit 7db165f

1 file changed

Lines changed: 66 additions & 50 deletions

File tree

solr/core/src/test/org/apache/solr/TestCpuTimeSearch.java

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,92 @@
1717
package org.apache.solr;
1818

1919
import java.io.IOException;
20-
import java.nio.file.Path;
2120
import java.util.List;
22-
import org.apache.lucene.tests.util.LuceneTestCase;
21+
import org.apache.solr.client.solrj.SolrClient;
2322
import org.apache.solr.client.solrj.SolrQuery;
2423
import org.apache.solr.client.solrj.SolrServerException;
24+
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
25+
import org.apache.solr.client.solrj.request.UpdateRequest;
2526
import org.apache.solr.client.solrj.response.QueryResponse;
27+
import org.apache.solr.cloud.SolrCloudTestCase;
2628
import org.apache.solr.common.SolrDocumentList;
2729
import org.apache.solr.common.SolrInputDocument;
28-
import org.apache.solr.common.params.ShardParams;
30+
import org.apache.solr.common.cloud.CompositeIdRouter;
31+
import org.apache.solr.common.cloud.DocCollection;
32+
import org.apache.solr.common.cloud.DocRouter;
33+
import org.apache.solr.common.cloud.Replica;
2934
import org.apache.solr.common.util.NamedList;
30-
import org.apache.solr.util.SolrJettyTestRule;
3135
import org.apache.solr.util.ThreadCpuTimer;
3236
import org.junit.BeforeClass;
33-
import org.junit.ClassRule;
34-
import org.junit.Ignore;
3537

36-
// TODO @Seed("861F5769E4B6F696")
37-
public class TestCpuTimeSearch extends SolrTestCaseJ4 {
38+
public class TestCpuTimeSearch extends SolrCloudTestCase {
3839

39-
@ClassRule public static final SolrJettyTestRule solrRule = new SolrJettyTestRule();
40-
41-
private static String shard1;
42-
private static String shard2;
40+
private static final String COLLECTION = "cpu-col";
4341

4442
@BeforeClass
4543
public static void setupSolr() throws Exception {
4644
System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true");
4745

48-
Path configSet = createTempDir("configSet");
49-
copyMinConf(configSet);
50-
solrRule.startSolr(LuceneTestCase.createTempDir());
51-
52-
solrRule.newCollection("core1").withConfigSet(configSet.toString()).create();
53-
solrRule.newCollection("core2").withConfigSet(configSet.toString()).create();
54-
var clientCore1 = solrRule.getSolrClient("core1");
55-
var clientCore2 = solrRule.getSolrClient("core2");
56-
57-
String urlCore1 = solrRule.getBaseUrl() + "/" + "core1";
58-
String urlCore2 = solrRule.getBaseUrl() + "/" + "core2";
59-
shard1 = urlCore1.replaceAll("https?://", "");
60-
shard2 = urlCore2.replaceAll("https?://", "");
61-
62-
SolrInputDocument doc = new SolrInputDocument();
63-
doc.setField("id", "1");
64-
doc.setField("subject", "batman");
65-
doc.setField("title", "foo bar");
66-
clientCore1.add(doc);
67-
clientCore1.commit();
68-
69-
doc.setField("id", "2");
70-
doc.setField("subject", "superman");
71-
clientCore2.add(doc);
72-
clientCore2.commit();
46+
configureCluster(1)
47+
.addConfig(
48+
"conf", TEST_PATH().resolve("configsets").resolve("cloud-minimal").resolve("conf"))
49+
.configure();
50+
51+
CollectionAdminRequest.createCollection(COLLECTION, "conf", 2, 1)
52+
.setRouterName(CompositeIdRouter.NAME)
53+
.processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
54+
55+
waitForState("New collection", COLLECTION, clusterShape(2, 2));
56+
DocCollection coll = getCollectionState(COLLECTION);
57+
58+
DocRouter router = DocRouter.getDocRouter(CompositeIdRouter.NAME);
59+
60+
UpdateRequest req = new UpdateRequest();
61+
62+
SolrInputDocument doc1 = new SolrInputDocument();
63+
String id1 = "1";
64+
doc1.setField("id", id1);
65+
doc1.setField("subject", "batman");
66+
doc1.setField("title", "foo bar");
67+
req.add(doc1);
68+
69+
// For second record, generate an ID until we are sure it will be indexed
70+
// in a different shard from first record
71+
String slice1 = router.getTargetSlice(id1, doc1, null, null, coll).getName();
72+
String slice2;
73+
String id2;
74+
do {
75+
id2 = Integer.toString(random().nextInt(Integer.MAX_VALUE));
76+
slice2 = router.getTargetSlice(id2, doc1, null, null, coll).getName();
77+
} while (slice1.equals(slice2));
78+
79+
SolrInputDocument doc2 = new SolrInputDocument();
80+
doc2.setField("id", "2");
81+
doc2.setField("subject", "superman");
82+
req.add(doc2);
83+
req.commit(cluster.getSolrClient(), COLLECTION);
7384
}
7485

7586
public void testWithoutDistrib() throws SolrServerException, IOException {
7687
SolrQuery query = new SolrQuery();
7788
query.setQuery("subject:batman OR subject:superman");
7889
query.addField("id");
7990
query.addField("subject");
80-
81-
QueryResponse response = solrRule.getSolrClient("core1").query(query);
91+
query.setDistrib(false);
92+
93+
QueryResponse response;
94+
DocCollection coll = getCollectionState(COLLECTION);
95+
96+
// Send the query to a random replica of the collection.
97+
// Distributing the query is disabled, so we should get a single result record.
98+
Replica randomReplica =
99+
pickRandom(
100+
coll.getSlices().stream()
101+
.flatMap(s -> s.getReplicas().stream())
102+
.toArray(Replica[]::new));
103+
try (SolrClient client = getHttpSolrClient(randomReplica.getCoreUrl())) {
104+
response = client.query(query);
105+
}
82106

83107
SolrDocumentList results = response.getResults();
84108
int size = results.size();
@@ -88,25 +112,17 @@ public void testWithoutDistrib() throws SolrServerException, IOException {
88112
List<Object> cpuTimes = header.getAll(ThreadCpuTimer.CPU_TIME);
89113
assertEquals("localCpuTime should not have values", 0, localCpuTimes.size());
90114
assertEquals("cpuTime should only have one value", 1, cpuTimes.size());
91-
long cpuTime = (long) cpuTimes.iterator().next();
115+
long cpuTime = (long) cpuTimes.getFirst();
92116
assertTrue("cpuTime (" + cpuTime + ") should be positive", cpuTime >= 0);
93117
}
94118

95-
@Ignore
96119
public void testWithDistrib() throws SolrServerException, IOException {
97120
SolrQuery query = new SolrQuery();
98121
query.setQuery("subject:batman OR subject:superman");
99122
query.addField("id");
100123
query.addField("subject");
101-
query.set("distrib", "true");
102-
query.set("shards", shard1 + "," + shard2);
103-
query.set(ShardParams.SHARDS_INFO, "true");
104-
query.set("debug", "true");
105-
query.set("stats", "true");
106-
query.set("stats.field", "id");
107-
query.set(ShardParams.SHARDS_TOLERANT, "true");
108124

109-
QueryResponse response = solrRule.getSolrClient("core1").query(query);
125+
QueryResponse response = cluster.getSolrClient(COLLECTION).query(query);
110126

111127
SolrDocumentList results = response.getResults();
112128
int size = results.size();
@@ -116,7 +132,7 @@ public void testWithDistrib() throws SolrServerException, IOException {
116132
List<Object> cpuTimes = header.getAll(ThreadCpuTimer.CPU_TIME);
117133
assertEquals("localCpuTime should not have values", 0, localCpuTimes.size());
118134
assertEquals("cpuTime should only have one value", 1, cpuTimes.size());
119-
long cpuTime = (long) cpuTimes.iterator().next();
135+
long cpuTime = (long) cpuTimes.getFirst();
120136
assertTrue("cpuTime (" + cpuTime + ") should be positive", cpuTime >= 0);
121137
}
122138
}

0 commit comments

Comments
 (0)