Skip to content

Commit 43aa301

Browse files
authored
[8.x] Add more tsdb and logsdb rolling upgrade indexing tests. (#115760)
Backport #115639 to 8.x branch. The main difference between other rolling upgrade tests is that these tests index more data while performing the rolling upgrade and no rollover is performed during rolling upgrade. For example this makes it more likely for merging to happen, which could uncover bwc bugs. Note that currently both test suites start trial license so that synthetic source gets used.
1 parent c24bce0 commit 43aa301

File tree

5 files changed

+467
-4
lines changed

5 files changed

+467
-4
lines changed

build-tools-internal/src/main/resources/checkstyle_suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<!-- Intentionally have multi line string for a bulk request, otherwise this needs to fallback to string concatenation -->
3636
<suppress files="modules[/\\]data-streams[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]datastreams[/\\]TsdbDataStreamRestIT.java" checks="LineLength" />
3737
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIT.java" checks="LineLength" />
38+
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIndexingRollingUpgradeIT.java" checks="LineLength" />
39+
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]LogsdbIndexingRollingUpgradeIT.java" checks="LineLength" />
3840

3941
<!-- Gradle requires inputs to be seriablizable -->
4042
<suppress files="build-tools-internal[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]gradle[/\\]internal[/\\]precommit[/\\]TestingConventionRule.java" checks="RegexpSinglelineJava" />

qa/rolling-upgrade/src/javaRestTest/java/org/elasticsearch/upgrades/LogsIndexModeRollingUpgradeIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void testLogsIndexing() throws IOException {
171171
}
172172
}
173173

174-
private static void enableLogsdbByDefault() throws IOException {
174+
static void enableLogsdbByDefault() throws IOException {
175175
var request = new Request("PUT", "/_cluster/settings");
176176
request.setJsonEntity("""
177177
{
@@ -214,7 +214,7 @@ private static Request rolloverDataStream(final RestClient client, final String
214214
}
215215

216216
@SuppressWarnings("unchecked")
217-
private static String getWriteBackingIndex(final RestClient client, final String dataStreamName, int backingIndex) throws IOException {
217+
static String getWriteBackingIndex(final RestClient client, final String dataStreamName, int backingIndex) throws IOException {
218218
final Request request = new Request("GET", "_data_stream/" + dataStreamName);
219219
final List<Object> dataStreams = (List<Object>) entityAsMap(client.performRequest(request)).get("data_streams");
220220
final Map<String, Object> dataStream = (Map<String, Object>) dataStreams.get(0);
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.upgrades;
11+
12+
import com.carrotsearch.randomizedtesting.annotations.Name;
13+
14+
import org.elasticsearch.client.Request;
15+
import org.elasticsearch.client.Response;
16+
import org.elasticsearch.client.ResponseException;
17+
import org.elasticsearch.common.network.NetworkAddress;
18+
import org.elasticsearch.common.xcontent.XContentHelper;
19+
import org.elasticsearch.test.rest.ObjectPath;
20+
import org.elasticsearch.xcontent.XContentType;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.time.Instant;
25+
import java.util.Locale;
26+
import java.util.Map;
27+
28+
import static org.elasticsearch.upgrades.LogsIndexModeRollingUpgradeIT.enableLogsdbByDefault;
29+
import static org.elasticsearch.upgrades.LogsIndexModeRollingUpgradeIT.getWriteBackingIndex;
30+
import static org.elasticsearch.upgrades.TsdbIT.formatInstant;
31+
import static org.hamcrest.Matchers.containsString;
32+
import static org.hamcrest.Matchers.equalTo;
33+
import static org.hamcrest.Matchers.greaterThan;
34+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
35+
import static org.hamcrest.Matchers.notNullValue;
36+
37+
public class LogsdbIndexingRollingUpgradeIT extends AbstractRollingUpgradeTestCase {
38+
39+
static String BULK_ITEM_TEMPLATE =
40+
"""
41+
{"@timestamp": "$now", "host.name": "$host", "method": "$method", "ip": "$ip", "message": "$message", "length": $length, "factor": $factor}
42+
""";
43+
44+
private static final String TEMPLATE = """
45+
{
46+
"mappings": {
47+
"properties": {
48+
"@timestamp" : {
49+
"type": "date"
50+
},
51+
"method": {
52+
"type": "keyword"
53+
},
54+
"message": {
55+
"type": "text"
56+
},
57+
"ip": {
58+
"type": "ip"
59+
},
60+
"length": {
61+
"type": "long"
62+
},
63+
"factor": {
64+
"type": "double"
65+
}
66+
}
67+
}
68+
}""";
69+
70+
public LogsdbIndexingRollingUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
71+
super(upgradedNodes);
72+
}
73+
74+
public void testIndexing() throws Exception {
75+
assumeTrue("test relies on index.mapping.source.mode setting", getOldClusterTestVersion().onOrAfter("8.16.0"));
76+
String dataStreamName = "logs-bwc-test";
77+
if (isOldCluster()) {
78+
startTrial();
79+
enableLogsdbByDefault();
80+
createTemplate(dataStreamName, getClass().getSimpleName().toLowerCase(Locale.ROOT), TEMPLATE);
81+
82+
Instant startTime = Instant.now().minusSeconds(60 * 60);
83+
bulkIndex(dataStreamName, 4, 1024, startTime);
84+
85+
String firstBackingIndex = getWriteBackingIndex(client(), dataStreamName, 0);
86+
var settings = (Map<?, ?>) getIndexSettingsWithDefaults(firstBackingIndex).get(firstBackingIndex);
87+
assertThat(((Map<?, ?>) settings.get("settings")).get("index.mode"), equalTo("logsdb"));
88+
assertThat(((Map<?, ?>) settings.get("defaults")).get("index.mapping.source.mode"), equalTo("SYNTHETIC"));
89+
90+
ensureGreen(dataStreamName);
91+
search(dataStreamName);
92+
query(dataStreamName);
93+
} else if (isMixedCluster()) {
94+
Instant startTime = Instant.now().minusSeconds(60 * 30);
95+
bulkIndex(dataStreamName, 4, 1024, startTime);
96+
97+
ensureGreen(dataStreamName);
98+
search(dataStreamName);
99+
query(dataStreamName);
100+
} else if (isUpgradedCluster()) {
101+
ensureGreen(dataStreamName);
102+
Instant startTime = Instant.now();
103+
bulkIndex(dataStreamName, 4, 1024, startTime);
104+
search(dataStreamName);
105+
query(dataStreamName);
106+
107+
var forceMergeRequest = new Request("POST", "/" + dataStreamName + "/_forcemerge");
108+
forceMergeRequest.addParameter("max_num_segments", "1");
109+
assertOK(client().performRequest(forceMergeRequest));
110+
111+
ensureGreen(dataStreamName);
112+
search(dataStreamName);
113+
query(dataStreamName);
114+
}
115+
}
116+
117+
static void createTemplate(String dataStreamName, String id, String template) throws IOException {
118+
final String INDEX_TEMPLATE = """
119+
{
120+
"index_patterns": ["$DATASTREAM"],
121+
"template": $TEMPLATE,
122+
"data_stream": {
123+
}
124+
}""";
125+
var putIndexTemplateRequest = new Request("POST", "/_index_template/" + id);
126+
putIndexTemplateRequest.setJsonEntity(INDEX_TEMPLATE.replace("$TEMPLATE", template).replace("$DATASTREAM", dataStreamName));
127+
assertOK(client().performRequest(putIndexTemplateRequest));
128+
}
129+
130+
static void bulkIndex(String dataStreamName, int numRequest, int numDocs, Instant startTime) throws Exception {
131+
for (int i = 0; i < numRequest; i++) {
132+
var bulkRequest = new Request("POST", "/" + dataStreamName + "/_bulk");
133+
StringBuilder requestBody = new StringBuilder();
134+
for (int j = 0; j < numDocs; j++) {
135+
String hostName = "host" + j % 50; // Not realistic, but makes asserting search / query response easier.
136+
String methodName = "method" + j % 5;
137+
String ip = NetworkAddress.format(randomIp(true));
138+
String message = randomAlphaOfLength(128);
139+
long length = randomLong();
140+
double factor = randomDouble();
141+
142+
requestBody.append("{\"create\": {}}");
143+
requestBody.append('\n');
144+
requestBody.append(
145+
BULK_ITEM_TEMPLATE.replace("$now", formatInstant(startTime))
146+
.replace("$host", hostName)
147+
.replace("$method", methodName)
148+
.replace("$ip", ip)
149+
.replace("$message", message)
150+
.replace("$length", Long.toString(length))
151+
.replace("$factor", Double.toString(factor))
152+
);
153+
requestBody.append('\n');
154+
155+
startTime = startTime.plusMillis(1);
156+
}
157+
bulkRequest.setJsonEntity(requestBody.toString());
158+
bulkRequest.addParameter("refresh", "true");
159+
var response = client().performRequest(bulkRequest);
160+
assertOK(response);
161+
var responseBody = entityAsMap(response);
162+
assertThat("errors in response:\n " + responseBody, responseBody.get("errors"), equalTo(false));
163+
}
164+
}
165+
166+
void search(String dataStreamName) throws Exception {
167+
var searchRequest = new Request("POST", "/" + dataStreamName + "/_search");
168+
searchRequest.addParameter("pretty", "true");
169+
searchRequest.setJsonEntity("""
170+
{
171+
"size": 0,
172+
"aggs": {
173+
"host_name": {
174+
"terms": {
175+
"field": "host.name",
176+
"order": { "_key": "asc" }
177+
},
178+
"aggs": {
179+
"max_length": {
180+
"max": {
181+
"field": "length"
182+
}
183+
},
184+
"max_factor": {
185+
"max": {
186+
"field": "factor"
187+
}
188+
}
189+
}
190+
}
191+
}
192+
}
193+
""");
194+
var response = client().performRequest(searchRequest);
195+
assertOK(response);
196+
var responseBody = entityAsMap(response);
197+
198+
Integer totalCount = ObjectPath.evaluate(responseBody, "hits.total.value");
199+
assertThat(totalCount, greaterThanOrEqualTo(4096));
200+
String key = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.key");
201+
assertThat(key, equalTo("host0"));
202+
Integer docCount = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.doc_count");
203+
assertThat(docCount, greaterThan(0));
204+
Double maxTx = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.max_length.value");
205+
assertThat(maxTx, notNullValue());
206+
Double maxRx = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.max_factor.value");
207+
assertThat(maxRx, notNullValue());
208+
}
209+
210+
void query(String dataStreamName) throws Exception {
211+
var queryRequest = new Request("POST", "/_query");
212+
queryRequest.addParameter("pretty", "true");
213+
queryRequest.setJsonEntity("""
214+
{
215+
"query": "FROM $ds | STATS max(length), max(factor) BY host.name | SORT host.name | LIMIT 5"
216+
}
217+
""".replace("$ds", dataStreamName));
218+
var response = client().performRequest(queryRequest);
219+
assertOK(response);
220+
var responseBody = entityAsMap(response);
221+
222+
String column1 = ObjectPath.evaluate(responseBody, "columns.0.name");
223+
String column2 = ObjectPath.evaluate(responseBody, "columns.1.name");
224+
String column3 = ObjectPath.evaluate(responseBody, "columns.2.name");
225+
assertThat(column1, equalTo("max(length)"));
226+
assertThat(column2, equalTo("max(factor)"));
227+
assertThat(column3, equalTo("host.name"));
228+
229+
String key = ObjectPath.evaluate(responseBody, "values.0.2");
230+
assertThat(key, equalTo("host0"));
231+
Long maxRx = ObjectPath.evaluate(responseBody, "values.0.0");
232+
assertThat(maxRx, notNullValue());
233+
Double maxTx = ObjectPath.evaluate(responseBody, "values.0.1");
234+
assertThat(maxTx, notNullValue());
235+
}
236+
237+
protected static void startTrial() throws IOException {
238+
Request startTrial = new Request("POST", "/_license/start_trial");
239+
startTrial.addParameter("acknowledge", "true");
240+
try {
241+
assertOK(client().performRequest(startTrial));
242+
} catch (ResponseException e) {
243+
var responseBody = entityAsMap(e.getResponse());
244+
String error = ObjectPath.evaluate(responseBody, "error_message");
245+
assertThat(error, containsString("Trial was already activated."));
246+
}
247+
}
248+
249+
static Map<String, Object> getIndexSettingsWithDefaults(String index) throws IOException {
250+
Request request = new Request("GET", "/" + index + "/_settings");
251+
request.addParameter("flat_settings", "true");
252+
request.addParameter("include_defaults", "true");
253+
Response response = client().performRequest(request);
254+
try (InputStream is = response.getEntity().getContent()) {
255+
return XContentHelper.convertToMap(
256+
XContentType.fromMediaType(response.getEntity().getContentType().getValue()).xContent(),
257+
is,
258+
true
259+
);
260+
}
261+
}
262+
263+
}

qa/rolling-upgrade/src/javaRestTest/java/org/elasticsearch/upgrades/TsdbIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public TsdbIT(@Name("upgradedNodes") int upgradedNodes) {
3333
super(upgradedNodes);
3434
}
3535

36-
private static final String TEMPLATE = """
36+
static final String TEMPLATE = """
3737
{
3838
"settings":{
3939
"index": {
@@ -289,7 +289,7 @@ private static void assertSearch(String dataStreamName, int expectedHitCount) th
289289
assertThat(ObjectPath.evaluate(responseBody, "hits.total.value"), equalTo(expectedHitCount));
290290
}
291291

292-
private static String formatInstant(Instant instant) {
292+
static String formatInstant(Instant instant) {
293293
return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant);
294294
}
295295

0 commit comments

Comments
 (0)