Skip to content

Commit 439a474

Browse files
committed
ESIntegTestCase.indexRandom should not introduce types. (#24202)
Since we plan on removing types, `indexRandom` should not introduce new types. This commit refactors `indexRandom` to reuse existing types.
1 parent a7e76dc commit 439a474

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,6 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, IndexReque
13271327
indexRandom(forceRefresh, dummyDocuments, Arrays.asList(builders));
13281328
}
13291329

1330-
1331-
private static final String RANDOM_BOGUS_TYPE = "RANDOM_BOGUS_TYPE______";
1332-
13331330
/**
13341331
* Indexes the given {@link IndexRequestBuilder} instances randomly. It shuffles the given builders and either
13351332
* indexes them in a blocking or async fashion. This is very useful to catch problems that relate to internal document
@@ -1377,31 +1374,33 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, List<Index
13771374
* @param builders the documents to index.
13781375
*/
13791376
public void indexRandom(boolean forceRefresh, boolean dummyDocuments, boolean maybeFlush, List<IndexRequestBuilder> builders) throws InterruptedException, ExecutionException {
1380-
13811377
Random random = random();
1382-
Set<String> indicesSet = new HashSet<>();
1378+
Map<String, Set<String>> indicesAndTypes = new HashMap<>();
13831379
for (IndexRequestBuilder builder : builders) {
1384-
indicesSet.add(builder.request().index());
1380+
final Set<String> types = indicesAndTypes.computeIfAbsent(builder.request().index(), index -> new HashSet<>());
1381+
types.add(builder.request().type());
13851382
}
1386-
Set<Tuple<String, String>> bogusIds = new HashSet<>();
1383+
Set<List<String>> bogusIds = new HashSet<>(); // (index, type, id)
13871384
if (random.nextBoolean() && !builders.isEmpty() && dummyDocuments) {
13881385
builders = new ArrayList<>(builders);
1389-
final String[] indices = indicesSet.toArray(new String[indicesSet.size()]);
13901386
// inject some bogus docs
13911387
final int numBogusDocs = scaledRandomIntBetween(1, builders.size() * 2);
13921388
final int unicodeLen = between(1, 10);
13931389
for (int i = 0; i < numBogusDocs; i++) {
1394-
String id = randomRealisticUnicodeOfLength(unicodeLen) + Integer.toString(dummmyDocIdGenerator.incrementAndGet());
1395-
String index = RandomPicks.randomFrom(random, indices);
1396-
bogusIds.add(new Tuple<>(index, id));
1397-
builders.add(client().prepareIndex(index, RANDOM_BOGUS_TYPE, id).setSource("{}", XContentType.JSON));
1390+
String id = "bogus_doc_" + randomRealisticUnicodeOfLength(unicodeLen) + Integer.toString(dummmyDocIdGenerator.incrementAndGet());
1391+
Map.Entry<String, Set<String>> indexAndTypes = RandomPicks.randomFrom(random, indicesAndTypes.entrySet());
1392+
String index = indexAndTypes.getKey();
1393+
String type = RandomPicks.randomFrom(random, indexAndTypes.getValue());
1394+
bogusIds.add(Arrays.asList(index, type, id));
1395+
// We configure a routing key in case the mapping requires it
1396+
builders.add(client().prepareIndex(index, type, id).setSource("{}", XContentType.JSON).setRouting(id));
13981397
}
13991398
}
1400-
final String[] indices = indicesSet.toArray(new String[indicesSet.size()]);
14011399
Collections.shuffle(builders, random());
14021400
final CopyOnWriteArrayList<Tuple<IndexRequestBuilder, Exception>> errors = new CopyOnWriteArrayList<>();
14031401
List<CountDownLatch> inFlightAsyncOperations = new ArrayList<>();
14041402
// If you are indexing just a few documents then frequently do it one at a time. If many then frequently in bulk.
1403+
final String[] indices = indicesAndTypes.keySet().toArray(new String[0]);
14051404
if (builders.size() < FREQUENT_BULK_THRESHOLD ? frequently() : builders.size() < ALWAYS_BULK_THRESHOLD ? rarely() : false) {
14061405
if (frequently()) {
14071406
logger.info("Index [{}] docs async: [{}] bulk: [{}]", builders.size(), true, false);
@@ -1443,10 +1442,10 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, boolean ma
14431442
assertThat(actualErrors, emptyIterable());
14441443
if (!bogusIds.isEmpty()) {
14451444
// delete the bogus types again - it might trigger merges or at least holes in the segments and enforces deleted docs!
1446-
for (Tuple<String, String> doc : bogusIds) {
1447-
assertEquals("failed to delete a dummy doc [" + doc.v1() + "][" + doc.v2() + "]",
1445+
for (List<String> doc : bogusIds) {
1446+
assertEquals("failed to delete a dummy doc [" + doc.get(0) + "][" + doc.get(2) + "]",
14481447
DocWriteResponse.Result.DELETED,
1449-
client().prepareDelete(doc.v1(), RANDOM_BOGUS_TYPE, doc.v2()).get().getResult());
1448+
client().prepareDelete(doc.get(0), doc.get(1), doc.get(2)).setRouting(doc.get(2)).get().getResult());
14501449
}
14511450
}
14521451
if (forceRefresh) {

0 commit comments

Comments
 (0)