Skip to content

Commit 695bf75

Browse files
kkrik-eselasticsearchmachine
andauthored
[TEST] Cover custom sorting and routing in randomized testing (#120584)
* Cover custom sorting and routing in randomized testing * [CI] Auto commit changes from spotless * fix reindex tests * fix reindex tests * refactor classes * comment * more refactoring * more refactoring * restore tests with static mappings * reduce diff * reduce diff * Restore single-element array removal in synthetic source * Revert "Restore single-element array removal in synthetic source" This reverts commit e8e99e1. * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 2ca2bbf commit 695bf75

8 files changed

+279
-223
lines changed

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/qa/AbstractChallengeRestTest.java

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.common.Strings;
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.core.CheckedConsumer;
17-
import org.elasticsearch.core.Tuple;
1817
import org.elasticsearch.rest.RestStatus;
1918
import org.elasticsearch.search.builder.SearchSourceBuilder;
2019
import org.elasticsearch.test.cluster.ElasticsearchCluster;
@@ -221,44 +220,16 @@ private XContentBuilder createContenderMappings() throws IOException {
221220

222221
public abstract void contenderMappings(XContentBuilder builder) throws IOException;
223222

224-
public void baselineSettings(Settings.Builder builder) {}
223+
public abstract void baselineSettings(Settings.Builder builder);
225224

226-
public void contenderSettings(Settings.Builder builder) {}
225+
public abstract void contenderSettings(Settings.Builder builder);
227226

228227
public void commonSettings(Settings.Builder builder) {}
229228

230-
private Response indexDocuments(
231-
final String dataStreamName,
232-
final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier
233-
) throws IOException {
234-
final StringBuilder sb = new StringBuilder();
235-
int id = 0;
236-
for (var document : documentsSupplier.get()) {
237-
sb.append(Strings.format("{ \"create\": { \"_id\" : \"%d\" } }", id)).append("\n");
238-
sb.append(Strings.toString(document)).append("\n");
239-
id++;
240-
}
241-
var request = new Request("POST", "/" + dataStreamName + "/_bulk");
242-
request.setJsonEntity(sb.toString());
243-
request.addParameter("refresh", "true");
244-
return client.performRequest(request);
245-
}
246-
247-
public Response indexBaselineDocuments(final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier) throws IOException {
248-
return indexDocuments(getBaselineDataStreamName(), documentsSupplier);
249-
}
250-
251-
public Response indexContenderDocuments(final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier)
252-
throws IOException {
253-
return indexDocuments(getContenderDataStreamName(), documentsSupplier);
254-
}
255-
256-
public Tuple<Response, Response> indexDocuments(
257-
final CheckedSupplier<List<XContentBuilder>, IOException> baselineSupplier,
258-
final CheckedSupplier<List<XContentBuilder>, IOException> contenderSupplier
259-
) throws IOException {
260-
return new Tuple<>(indexBaselineDocuments(baselineSupplier), indexContenderDocuments(contenderSupplier));
261-
}
229+
public abstract void indexDocuments(
230+
CheckedSupplier<List<XContentBuilder>, IOException> baselineSupplier,
231+
CheckedSupplier<List<XContentBuilder>, IOException> contenderSupplier
232+
) throws IOException;
262233

263234
public Response queryBaseline(final SearchSourceBuilder search) throws IOException {
264235
return query(search, this::getBaselineDataStreamName);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.logsdb.qa;
9+
10+
import org.elasticsearch.common.CheckedSupplier;
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.xcontent.XContentBuilder;
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
/**
19+
* Challenge test that uses bulk indexing for both baseline and contender sides.
20+
* We index same documents into an index with standard index mode and an index with logsdb index mode.
21+
* Then we verify that results of common operations are the same modulo knows differences like synthetic source
22+
* modifications.
23+
*/
24+
public class BulkChallengeRestIT extends StandardVersusLogsIndexModeChallengeRestIT {
25+
26+
public BulkChallengeRestIT() {}
27+
28+
protected BulkChallengeRestIT(DataGenerationHelper dataGenerationHelper) {
29+
super(dataGenerationHelper);
30+
}
31+
32+
@Override
33+
public void indexDocuments(
34+
final CheckedSupplier<List<XContentBuilder>, IOException> baselineSupplier,
35+
final CheckedSupplier<List<XContentBuilder>, IOException> contenderSupplier
36+
) throws IOException {
37+
var contenderResponseEntity = indexContenderDocuments(contenderSupplier);
38+
indexBaselineDocuments(baselineSupplier, contenderResponseEntity);
39+
}
40+
41+
private Map<String, Object> indexContenderDocuments(final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier)
42+
throws IOException {
43+
final StringBuilder sb = new StringBuilder();
44+
int id = 0;
45+
for (var document : documentsSupplier.get()) {
46+
if (autoGenerateId()) {
47+
sb.append("{ \"create\": { } }\n");
48+
} else {
49+
sb.append(Strings.format("{ \"create\": { \"_id\" : \"%d\" } }\n", id));
50+
}
51+
sb.append(Strings.toString(document)).append("\n");
52+
id++;
53+
}
54+
return performBulkRequest(sb.toString(), false);
55+
}
56+
57+
@SuppressWarnings("unchecked")
58+
private void indexBaselineDocuments(
59+
final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier,
60+
final Map<String, Object> contenderResponseEntity
61+
) throws IOException {
62+
final StringBuilder sb = new StringBuilder();
63+
int id = 0;
64+
final List<Map<String, Object>> items = (List<Map<String, Object>>) contenderResponseEntity.get("items");
65+
for (var document : documentsSupplier.get()) {
66+
if (autoGenerateId()) {
67+
var contenderId = ((Map<String, Object>) items.get(id).get("create")).get("_id");
68+
sb.append(Strings.format("{ \"create\": { \"_id\" : \"%s\" } }\n", contenderId));
69+
} else {
70+
sb.append(Strings.format("{ \"create\": { \"_id\" : \"%d\" } }\n", id));
71+
}
72+
sb.append(Strings.toString(document)).append("\n");
73+
id++;
74+
}
75+
performBulkRequest(sb.toString(), true);
76+
}
77+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
import org.elasticsearch.common.settings.Settings;
1111

12-
public class StandardVersusLogsIndexModeRandomDataDynamicMappingChallengeRestIT extends
13-
StandardVersusLogsIndexModeRandomDataChallengeRestIT {
14-
public StandardVersusLogsIndexModeRandomDataDynamicMappingChallengeRestIT() {
12+
public class BulkDynamicMappingChallengeRestIT extends BulkChallengeRestIT {
13+
public BulkDynamicMappingChallengeRestIT() {
1514
super(new DataGenerationHelper(builder -> builder.withFullyDynamicMapping(true)));
1615
}
1716

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.logsdb.qa;
9+
10+
import org.elasticsearch.common.time.DateFormatter;
11+
import org.elasticsearch.common.time.FormatNames;
12+
import org.elasticsearch.xcontent.XContentBuilder;
13+
import org.elasticsearch.xcontent.XContentFactory;
14+
15+
import java.io.IOException;
16+
import java.time.Instant;
17+
18+
/**
19+
* This test uses simple mapping and document structure in order to allow easier debugging of the test itself.
20+
*/
21+
public class BulkStaticMappingChallengeRestIT extends BulkChallengeRestIT {
22+
public BulkStaticMappingChallengeRestIT() {}
23+
24+
@Override
25+
public void baselineMappings(XContentBuilder builder) throws IOException {
26+
if (fullyDynamicMapping == false) {
27+
builder.startObject()
28+
.startObject("properties")
29+
30+
.startObject("@timestamp")
31+
.field("type", "date")
32+
.endObject()
33+
34+
.startObject("host.name")
35+
.field("type", "keyword")
36+
.field("ignore_above", randomIntBetween(1000, 1200))
37+
.endObject()
38+
39+
.startObject("message")
40+
.field("type", "keyword")
41+
.field("ignore_above", randomIntBetween(1000, 1200))
42+
.endObject()
43+
44+
.startObject("method")
45+
.field("type", "keyword")
46+
.field("ignore_above", randomIntBetween(1000, 1200))
47+
.endObject()
48+
49+
.startObject("memory_usage_bytes")
50+
.field("type", "long")
51+
.field("ignore_malformed", randomBoolean())
52+
.endObject()
53+
54+
.endObject()
55+
56+
.endObject();
57+
} else {
58+
// We want dynamic mapping, but we need host.name to be a keyword instead of text to support aggregations.
59+
builder.startObject()
60+
.startObject("properties")
61+
62+
.startObject("host.name")
63+
.field("type", "keyword")
64+
.field("ignore_above", randomIntBetween(1000, 1200))
65+
.endObject()
66+
67+
.endObject()
68+
.endObject();
69+
}
70+
}
71+
72+
@Override
73+
public void contenderMappings(XContentBuilder builder) throws IOException {
74+
builder.startObject();
75+
builder.field("subobjects", false);
76+
77+
if (fullyDynamicMapping == false) {
78+
builder.startObject("properties")
79+
80+
.startObject("@timestamp")
81+
.field("type", "date")
82+
.endObject()
83+
84+
.startObject("host.name")
85+
.field("type", "keyword")
86+
.field("ignore_above", randomIntBetween(1000, 1200))
87+
.endObject()
88+
89+
.startObject("message")
90+
.field("type", "keyword")
91+
.field("ignore_above", randomIntBetween(1000, 1200))
92+
.endObject()
93+
94+
.startObject("method")
95+
.field("type", "keyword")
96+
.field("ignore_above", randomIntBetween(1000, 1200))
97+
.endObject()
98+
99+
.startObject("memory_usage_bytes")
100+
.field("type", "long")
101+
.field("ignore_malformed", randomBoolean())
102+
.endObject()
103+
104+
.endObject();
105+
}
106+
107+
builder.endObject();
108+
}
109+
110+
@Override
111+
protected XContentBuilder generateDocument(final Instant timestamp) throws IOException {
112+
return XContentFactory.jsonBuilder()
113+
.startObject()
114+
.field("@timestamp", DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(timestamp))
115+
.field("host.name", randomFrom("foo", "bar", "baz"))
116+
.field("message", randomFrom("a message", "another message", "still another message", "one more message"))
117+
.field("method", randomFrom("put", "post", "get"))
118+
.field("memory_usage_bytes", randomLongBetween(1000, 2000))
119+
.endObject();
120+
}
121+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* This test compares behavior of a standard mode data stream and a logsdb data stream using stored source.
1414
* There should be no differences between such two data streams.
1515
*/
16-
public class StandardVersusLogsStoredSourceChallengeRestIT extends StandardVersusLogsIndexModeRandomDataChallengeRestIT {
16+
public class BulkStoredSourceChallengeRestIT extends BulkChallengeRestIT {
1717
@Override
1818
public void contenderSettings(Settings.Builder builder) {
1919
super.contenderSettings(builder);

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/qa/ReindexChallengeRestIT.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
package org.elasticsearch.xpack.logsdb.qa;
99

1010
import org.elasticsearch.client.Request;
11-
import org.elasticsearch.client.Response;
1211
import org.elasticsearch.common.CheckedSupplier;
12+
import org.elasticsearch.common.Strings;
1313
import org.elasticsearch.xcontent.XContentBuilder;
1414

1515
import java.io.IOException;
@@ -18,9 +18,29 @@
1818

1919
import static org.hamcrest.Matchers.equalTo;
2020

21-
public abstract class ReindexChallengeRestIT extends StandardVersusLogsIndexModeRandomDataChallengeRestIT {
21+
public abstract class ReindexChallengeRestIT extends StandardVersusLogsIndexModeChallengeRestIT {
22+
2223
@Override
23-
public Response indexContenderDocuments(CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier) throws IOException {
24+
public void indexDocuments(
25+
final CheckedSupplier<List<XContentBuilder>, IOException> baselineSupplier,
26+
final CheckedSupplier<List<XContentBuilder>, IOException> contencontenderSupplierderSupplier
27+
) throws IOException {
28+
indexBaselineDocuments(baselineSupplier);
29+
indexContenderDocuments();
30+
}
31+
32+
private void indexBaselineDocuments(final CheckedSupplier<List<XContentBuilder>, IOException> documentsSupplier) throws IOException {
33+
final StringBuilder sb = new StringBuilder();
34+
int id = 0;
35+
for (var document : documentsSupplier.get()) {
36+
sb.append(Strings.format("{ \"create\": { \"_id\" : \"%d\" } }\n", id));
37+
sb.append(Strings.toString(document)).append("\n");
38+
id++;
39+
}
40+
performBulkRequest(sb.toString(), true);
41+
}
42+
43+
private void indexContenderDocuments() throws IOException {
2444
var reindexRequest = new Request("POST", "/_reindex?refresh=true");
2545
reindexRequest.setJsonEntity(String.format(Locale.ROOT, """
2646
{
@@ -38,7 +58,5 @@ public Response indexContenderDocuments(CheckedSupplier<List<XContentBuilder>, I
3858

3959
var body = entityAsMap(response);
4060
assertThat("encountered failures when performing reindex:\n " + body, body.get("failures"), equalTo(List.of()));
41-
42-
return response;
4361
}
4462
}

0 commit comments

Comments
 (0)