Skip to content

Commit 3aa6aa2

Browse files
authored
SOLR-18252: Fix flaky test by avoiding race conditions. (#4462)
Co-authored by: hoss.
1 parent dc05c0b commit 3aa6aa2

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

solr/cross-dc-manager/src/test/org/apache/solr/crossdc/manager/SolrAndKafkaIntegrationTest.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@
3838
import java.util.Map;
3939
import java.util.Properties;
4040
import java.util.Set;
41+
import java.util.concurrent.BlockingQueue;
4142
import java.util.concurrent.CountDownLatch;
4243
import java.util.concurrent.ExecutorService;
4344
import java.util.concurrent.Future;
45+
import java.util.concurrent.LinkedBlockingQueue;
46+
import java.util.concurrent.TimeUnit;
4447
import java.util.stream.IntStream;
4548
import org.apache.commons.io.IOUtils;
4649
import org.apache.kafka.clients.consumer.ConsumerRecord;
@@ -70,6 +73,7 @@
7073
import org.apache.solr.common.util.NamedList;
7174
import org.apache.solr.common.util.ObjectReleaseTracker;
7275
import org.apache.solr.common.util.SolrNamedThreadFactory;
76+
import org.apache.solr.common.util.TimeSource;
7377
import org.apache.solr.common.util.Utils;
7478
import org.apache.solr.crossdc.common.KafkaCrossDcConf;
7579
import org.apache.solr.crossdc.common.MirroredSolrRequest;
@@ -79,6 +83,7 @@
7983
import org.apache.solr.crossdc.manager.consumer.KafkaCrossDcConsumer;
8084
import org.apache.solr.crossdc.manager.consumer.PartitionManager;
8185
import org.apache.solr.util.SolrKafkaTestsIgnoredThreadsFilter;
86+
import org.apache.solr.util.TimeOut;
8287
import org.junit.After;
8388
import org.junit.Before;
8489
import org.junit.BeforeClass;
@@ -160,7 +165,7 @@ public String toString() {
160165

161166
protected volatile Consumer consumer;
162167

163-
private List<ConsumerBatch> consumerBatches;
168+
private BlockingQueue<ConsumerBatch> consumerBatches;
164169

165170
private static final String TOPIC = "topic1";
166171

@@ -180,7 +185,7 @@ public void beforeSolrAndKafkaIntegrationTest() throws Exception {
180185
(t, e) -> log.error("Uncaught exception in thread {}", t, e));
181186
System.setProperty("otel.metrics.exporter", "prometheus");
182187
System.setProperty(KafkaCrossDcConsumer.PROP_TOPIC_DEBUG, "true");
183-
consumerBatches = new ArrayList<>();
188+
consumerBatches = new LinkedBlockingQueue<>();
184189
consumer =
185190
new Consumer() {
186191
@Override
@@ -195,7 +200,7 @@ public void sendBatch(
195200
final MirroredSolrRequest.Type type,
196201
final ConsumerRecord<String, MirroredSolrRequest<?>> lastRecord,
197202
final PartitionManager.WorkUnit workUnit) {
198-
consumerBatches.add(new ConsumerBatch(type, solrReqBatch));
203+
consumerBatches.offer(new ConsumerBatch(type, solrReqBatch));
199204
super.sendBatch(solrReqBatch, type, lastRecord, workUnit);
200205
}
201206
};
@@ -357,10 +362,27 @@ public void testPartitioning() throws Exception {
357362
}
358363
client.commit(COLLECTION);
359364
client.commit(ALT_COLLECTION);
365+
366+
assertCluster2EventuallyHasDocs(COLLECTION, "*:*", NUM_DOCS);
367+
assertCluster2EventuallyHasDocs(ALT_COLLECTION, "*:*", NUM_DOCS);
368+
360369
// check that updates to different collections were always sent to the same partition
361370
Map<Integer, String> partitionsPerCol = new HashMap<>();
362371
Map<String, Set<String>> docsPerCol = new HashMap<>();
363-
for (ConsumerBatch batch : consumerBatches) {
372+
int batchCount = 0;
373+
TimeOut timeOut = new TimeOut(60, TimeUnit.SECONDS, TimeSource.CURRENT_TIME);
374+
while (!timeOut.hasTimedOut()) {
375+
final ConsumerBatch batch = consumerBatches.poll(2, TimeUnit.SECONDS);
376+
if (batch == null) {
377+
int totalDocsSeen = docsPerCol.values().stream().mapToInt(Set::size).sum();
378+
if (totalDocsSeen == NUM_DOCS * 2) {
379+
// we've collected all expected docs
380+
break;
381+
} else {
382+
continue; // keep waiting, it was just a longer pause
383+
}
384+
}
385+
batchCount++;
364386
String collection =
365387
partitionsPerCol.computeIfAbsent(batch.partitionId, k -> batch.collection);
366388
docsPerCol.computeIfAbsent(collection, col -> new HashSet<>()).addAll(batch.addIds);
@@ -376,8 +398,19 @@ public void testPartitioning() throws Exception {
376398
collection,
377399
batch.collection);
378400
}
379-
docsPerCol.forEach(
380-
(col, ids) -> assertEquals("incorrect count in collection " + col, NUM_DOCS, ids.size()));
401+
if (timeOut.hasTimedOut()) {
402+
fail("timed out waiting for batches");
403+
}
404+
assertTrue("No batches were received from consumer", batchCount > 0);
405+
assertEquals("Should have processed both collections", 2, docsPerCol.size());
406+
assertTrue("COLLECTION not found in results", docsPerCol.containsKey(COLLECTION));
407+
assertTrue("ALT_COLLECTION not found in results", docsPerCol.containsKey(ALT_COLLECTION));
408+
assertEquals(
409+
"incorrect count in collection " + COLLECTION, NUM_DOCS, docsPerCol.get(COLLECTION).size());
410+
assertEquals(
411+
"incorrect count in collection " + ALT_COLLECTION,
412+
NUM_DOCS,
413+
docsPerCol.get(ALT_COLLECTION).size());
381414
}
382415

383416
@Test

0 commit comments

Comments
 (0)