Skip to content

Commit

Permalink
Add more tsdb and logsdb rolling upgrade indexing tests. (elastic#115639
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
martijnvg authored Oct 28, 2024
1 parent 5fb1e23 commit 98cd34f
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<!-- Intentionally have multi line string for a bulk request, otherwise this needs to fallback to string concatenation -->
<suppress files="modules[/\\]data-streams[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]datastreams[/\\]TsdbDataStreamRestIT.java" checks="LineLength" />
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIT.java" checks="LineLength" />
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]TsdbIndexingRollingUpgradeIT.java" checks="LineLength" />
<suppress files="qa[/\\]rolling-upgrade[/\\]src[/\\]javaRestTest[/\\]java[/\\]org[/\\]elasticsearch[/\\]upgrades[/\\]LogsdbIndexingRollingUpgradeIT.java" checks="LineLength" />

<!-- Gradle requires inputs to be seriablizable -->
<suppress files="build-tools-internal[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]gradle[/\\]internal[/\\]precommit[/\\]TestingConventionRule.java" checks="RegexpSinglelineJava" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void testLogsIndexing() throws IOException {
}
}

private static void enableLogsdbByDefault() throws IOException {
static void enableLogsdbByDefault() throws IOException {
var request = new Request("PUT", "/_cluster/settings");
request.setJsonEntity("""
{
Expand Down Expand Up @@ -214,7 +214,7 @@ private static Request rolloverDataStream(final RestClient client, final String
}

@SuppressWarnings("unchecked")
private static String getWriteBackingIndex(final RestClient client, final String dataStreamName, int backingIndex) throws IOException {
static String getWriteBackingIndex(final RestClient client, final String dataStreamName, int backingIndex) throws IOException {
final Request request = new Request("GET", "_data_stream/" + dataStreamName);
final List<Object> dataStreams = (List<Object>) entityAsMap(client.performRequest(request)).get("data_streams");
final Map<String, Object> dataStream = (Map<String, Object>) dataStreams.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.upgrades;

import com.carrotsearch.randomizedtesting.annotations.Name;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.Map;

import static org.elasticsearch.upgrades.LogsIndexModeRollingUpgradeIT.enableLogsdbByDefault;
import static org.elasticsearch.upgrades.LogsIndexModeRollingUpgradeIT.getWriteBackingIndex;
import static org.elasticsearch.upgrades.TsdbIT.formatInstant;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;

public class LogsdbIndexingRollingUpgradeIT extends AbstractRollingUpgradeTestCase {

static String BULK_ITEM_TEMPLATE =
"""
{"@timestamp": "$now", "host.name": "$host", "method": "$method", "ip": "$ip", "message": "$message", "length": $length, "factor": $factor}
""";

private static final String TEMPLATE = """
{
"mappings": {
"properties": {
"@timestamp" : {
"type": "date"
},
"method": {
"type": "keyword"
},
"message": {
"type": "text"
},
"ip": {
"type": "ip"
},
"length": {
"type": "long"
},
"factor": {
"type": "double"
}
}
}
}""";

public LogsdbIndexingRollingUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}

public void testIndexing() throws Exception {
String dataStreamName = "logs-bwc-test";
if (isOldCluster()) {
startTrial();
enableLogsdbByDefault();
createTemplate(dataStreamName, "3", TEMPLATE);

Instant startTime = Instant.now().minusSeconds(60 * 60);
bulkIndex(dataStreamName, 4, 1024, startTime);

String firstBackingIndex = getWriteBackingIndex(client(), dataStreamName, 0);
var settings = (Map<?, ?>) getIndexSettingsWithDefaults(firstBackingIndex).get(firstBackingIndex);
assertThat(((Map<?, ?>) settings.get("settings")).get("index.mode"), equalTo("logsdb"));
assertThat(((Map<?, ?>) settings.get("defaults")).get("index.mapping.source.mode"), equalTo("SYNTHETIC"));

ensureGreen(dataStreamName);
search(dataStreamName);
query(dataStreamName);
} else if (isMixedCluster()) {
Instant startTime = Instant.now().minusSeconds(60 * 30);
bulkIndex(dataStreamName, 4, 1024, startTime);

ensureGreen(dataStreamName);
search(dataStreamName);
query(dataStreamName);
} else if (isUpgradedCluster()) {
ensureGreen(dataStreamName);
Instant startTime = Instant.now();
bulkIndex(dataStreamName, 4, 1024, startTime);
search(dataStreamName);
query(dataStreamName);

var forceMergeRequest = new Request("POST", "/" + dataStreamName + "/_forcemerge");
forceMergeRequest.addParameter("max_num_segments", "1");
assertOK(client().performRequest(forceMergeRequest));

ensureGreen(dataStreamName);
search(dataStreamName);
query(dataStreamName);
}
}

static void createTemplate(String dataStreamName, String id, String template) throws IOException {
final String INDEX_TEMPLATE = """
{
"index_patterns": ["$DATASTREAM"],
"template": $TEMPLATE,
"data_stream": {
}
}""";
var putIndexTemplateRequest = new Request("POST", "/_index_template/" + id);
putIndexTemplateRequest.setJsonEntity(INDEX_TEMPLATE.replace("$TEMPLATE", template).replace("$DATASTREAM", dataStreamName));
assertOK(client().performRequest(putIndexTemplateRequest));
}

static void bulkIndex(String dataStreamName, int numRequest, int numDocs, Instant startTime) throws Exception {
for (int i = 0; i < numRequest; i++) {
var bulkRequest = new Request("POST", "/" + dataStreamName + "/_bulk");
StringBuilder requestBody = new StringBuilder();
for (int j = 0; j < numDocs; j++) {
String hostName = "host" + j % 50; // Not realistic, but makes asserting search / query response easier.
String methodName = "method" + j % 5;
String ip = NetworkAddress.format(randomIp(true));
String message = randomAlphaOfLength(128);
long length = randomLong();
double factor = randomDouble();

requestBody.append("{\"create\": {}}");
requestBody.append('\n');
requestBody.append(
BULK_ITEM_TEMPLATE.replace("$now", formatInstant(startTime))
.replace("$host", hostName)
.replace("$method", methodName)
.replace("$ip", ip)
.replace("$message", message)
.replace("$length", Long.toString(length))
.replace("$factor", Double.toString(factor))
);
requestBody.append('\n');

startTime = startTime.plusMillis(1);
}
bulkRequest.setJsonEntity(requestBody.toString());
bulkRequest.addParameter("refresh", "true");
var response = client().performRequest(bulkRequest);
assertOK(response);
var responseBody = entityAsMap(response);
assertThat("errors in response:\n " + responseBody, responseBody.get("errors"), equalTo(false));
}
}

void search(String dataStreamName) throws Exception {
var searchRequest = new Request("POST", "/" + dataStreamName + "/_search");
searchRequest.addParameter("pretty", "true");
searchRequest.setJsonEntity("""
{
"size": 0,
"aggs": {
"host_name": {
"terms": {
"field": "host.name",
"order": { "_key": "asc" }
},
"aggs": {
"max_length": {
"max": {
"field": "length"
}
},
"max_factor": {
"max": {
"field": "factor"
}
}
}
}
}
}
""");
var response = client().performRequest(searchRequest);
assertOK(response);
var responseBody = entityAsMap(response);

Integer totalCount = ObjectPath.evaluate(responseBody, "hits.total.value");
assertThat(totalCount, greaterThanOrEqualTo(4096));
String key = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.key");
assertThat(key, equalTo("host0"));
Integer docCount = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.doc_count");
assertThat(docCount, greaterThan(0));
Double maxTx = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.max_length.value");
assertThat(maxTx, notNullValue());
Double maxRx = ObjectPath.evaluate(responseBody, "aggregations.host_name.buckets.0.max_factor.value");
assertThat(maxRx, notNullValue());
}

void query(String dataStreamName) throws Exception {
var queryRequest = new Request("POST", "/_query");
queryRequest.addParameter("pretty", "true");
queryRequest.setJsonEntity("""
{
"query": "FROM $ds | STATS max(length), max(factor) BY host.name | SORT host.name | LIMIT 5"
}
""".replace("$ds", dataStreamName));
var response = client().performRequest(queryRequest);
assertOK(response);
var responseBody = entityAsMap(response);

String column1 = ObjectPath.evaluate(responseBody, "columns.0.name");
String column2 = ObjectPath.evaluate(responseBody, "columns.1.name");
String column3 = ObjectPath.evaluate(responseBody, "columns.2.name");
assertThat(column1, equalTo("max(length)"));
assertThat(column2, equalTo("max(factor)"));
assertThat(column3, equalTo("host.name"));

String key = ObjectPath.evaluate(responseBody, "values.0.2");
assertThat(key, equalTo("host0"));
Long maxRx = ObjectPath.evaluate(responseBody, "values.0.0");
assertThat(maxRx, notNullValue());
Double maxTx = ObjectPath.evaluate(responseBody, "values.0.1");
assertThat(maxTx, notNullValue());
}

protected static void startTrial() throws IOException {
Request startTrial = new Request("POST", "/_license/start_trial");
startTrial.addParameter("acknowledge", "true");
assertOK(client().performRequest(startTrial));
}

static Map<String, Object> getIndexSettingsWithDefaults(String index) throws IOException {
Request request = new Request("GET", "/" + index + "/_settings");
request.addParameter("flat_settings", "true");
request.addParameter("include_defaults", "true");
Response response = client().performRequest(request);
try (InputStream is = response.getEntity().getContent()) {
return XContentHelper.convertToMap(
XContentType.fromMediaType(response.getEntity().getContentType().getValue()).xContent(),
is,
true
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TsdbIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}

private static final String TEMPLATE = """
static final String TEMPLATE = """
{
"settings":{
"index": {
Expand Down Expand Up @@ -289,7 +289,7 @@ private static void assertSearch(String dataStreamName, int expectedHitCount) th
assertThat(ObjectPath.evaluate(responseBody, "hits.total.value"), equalTo(expectedHitCount));
}

private static String formatInstant(Instant instant) {
static String formatInstant(Instant instant) {
return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant);
}

Expand Down
Loading

0 comments on commit 98cd34f

Please sign in to comment.