Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a more randomized passthrough indexing test. (#105344)
That also asserts routing aspects of indexing, searching and getting by id. Relates to #103567
- Loading branch information
Showing
3 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
194 changes: 194 additions & 0 deletions
194
...src/internalClusterTest/java/org/elasticsearch/datastreams/TSDBPassthroughIndexingIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
package org.elasticsearch.datastreams; | ||
|
||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; | ||
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | ||
import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; | ||
import org.elasticsearch.action.get.GetRequest; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | ||
import org.elasticsearch.cluster.metadata.Template; | ||
import org.elasticsearch.common.compress.CompressedXContent; | ||
import org.elasticsearch.common.network.InetAddresses; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.time.DateFormatter; | ||
import org.elasticsearch.common.time.FormatNames; | ||
import org.elasticsearch.index.query.TermQueryBuilder; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.test.ESSingleNodeTestCase; | ||
import org.elasticsearch.test.InternalSettingsPlugin; | ||
import org.elasticsearch.xcontent.ObjectPath; | ||
import org.elasticsearch.xcontent.XContentType; | ||
|
||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.test.MapMatcher.assertMap; | ||
import static org.elasticsearch.test.MapMatcher.matchesMap; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class TSDBPassthroughIndexingIT extends ESSingleNodeTestCase { | ||
|
||
public static final String MAPPING_TEMPLATE = """ | ||
{ | ||
"_doc":{ | ||
"properties": { | ||
"@timestamp" : { | ||
"type": "date" | ||
}, | ||
"attributes": { | ||
"type": "passthrough", | ||
"dynamic": true, | ||
"time_series_dimension": true | ||
}, | ||
"metrics": { | ||
"properties": { | ||
"network": { | ||
"properties": { | ||
"tx": { | ||
"type": "long", | ||
"time_series_metric": "counter" | ||
}, | ||
"rx": { | ||
"type": "long", | ||
"time_series_metric": "counter" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"""; | ||
|
||
private static final String DOC = """ | ||
{ | ||
"@timestamp": "$time", | ||
"attributes": { | ||
"metricset": "pod", | ||
"pod": { | ||
"name": "$name", | ||
"uid": "$uid", | ||
"ip": "$ip" | ||
} | ||
}, | ||
"metrics": { | ||
"network": { | ||
"tx": 1434595272, | ||
"rx": 530605511 | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> getPlugins() { | ||
return List.of(DataStreamsPlugin.class, InternalSettingsPlugin.class); | ||
} | ||
|
||
@Override | ||
protected Settings nodeSettings() { | ||
Settings.Builder newSettings = Settings.builder(); | ||
newSettings.put(super.nodeSettings()); | ||
// This essentially disables the automatic updates to end_time settings of a data stream's latest backing index. | ||
newSettings.put(DataStreamsPlugin.TIME_SERIES_POLL_INTERVAL.getKey(), "10m"); | ||
return newSettings.build(); | ||
} | ||
|
||
public void testIndexingGettingAndSearching() throws Exception { | ||
var templateSettings = Settings.builder() | ||
.put("index.mode", "time_series") | ||
.put("index.number_of_shards", randomIntBetween(2, 10)) | ||
.put("index.number_of_replicas", 0); | ||
|
||
var request = new TransportPutComposableIndexTemplateAction.Request("id"); | ||
request.indexTemplate( | ||
ComposableIndexTemplate.builder() | ||
.indexPatterns(List.of("k8s*")) | ||
.template(new Template(templateSettings.build(), new CompressedXContent(MAPPING_TEMPLATE), null)) | ||
.dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) | ||
.build() | ||
); | ||
client().execute(TransportPutComposableIndexTemplateAction.TYPE, request).actionGet(); | ||
|
||
String index = null; | ||
int indexingIters = randomIntBetween(16, 128); | ||
Instant time = Instant.now(); | ||
for (int i = 0; i < indexingIters; i++) { | ||
var indexRequest = new IndexRequest("k8s").opType(DocWriteRequest.OpType.CREATE); | ||
indexRequest.source( | ||
DOC.replace("$time", formatInstant(time)) | ||
.replace("$uid", randomUUID()) | ||
.replace("$name", randomAlphaOfLength(4)) | ||
.replace("$ip", InetAddresses.toAddrString(randomIp(randomBoolean()))), | ||
XContentType.JSON | ||
); | ||
var indexResponse = client().index(indexRequest).actionGet(); | ||
index = indexResponse.getIndex(); | ||
String id = indexResponse.getId(); | ||
|
||
var getResponse = client().get(new GetRequest(index, id)).actionGet(); | ||
assertThat(getResponse.isExists(), is(true)); | ||
|
||
client().admin().indices().refresh(new RefreshRequest(index)).actionGet(); | ||
var searchRequest = new SearchRequest(); | ||
searchRequest.source(new SearchSourceBuilder().query(new TermQueryBuilder("_id", id))); | ||
assertResponse(client().search(searchRequest), searchResponse -> { | ||
assertHitCount(searchResponse, 1); | ||
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo(id)); | ||
}); | ||
time = time.plusMillis(1); | ||
} | ||
|
||
// validate index: | ||
var getIndexResponse = client().admin().indices().getIndex(new GetIndexRequest().indices(index)).actionGet(); | ||
assertThat(getIndexResponse.getSettings().get(index).get("index.routing_path"), equalTo("[attributes.*]")); | ||
// validate mapping | ||
var mapping = getIndexResponse.mappings().get(index).getSourceAsMap(); | ||
assertMap( | ||
ObjectPath.eval("properties.attributes.properties.metricset", mapping), | ||
matchesMap().entry("type", "keyword").entry("time_series_dimension", true) | ||
); | ||
@SuppressWarnings("unchecked") | ||
var attributes = (Map<String, Map<?, ?>>) ObjectPath.eval("properties.attributes.properties", mapping); | ||
assertMap(attributes.get("pod.ip"), matchesMap().entry("type", "keyword").entry("time_series_dimension", true)); | ||
assertMap(attributes.get("pod.uid"), matchesMap().entry("type", "keyword").entry("time_series_dimension", true)); | ||
assertMap(attributes.get("pod.name"), matchesMap().entry("type", "keyword").entry("time_series_dimension", true)); | ||
// alias field mappers: | ||
assertMap( | ||
ObjectPath.eval("properties.metricset", mapping), | ||
matchesMap().entry("type", "alias").entry("path", "attributes.metricset") | ||
); | ||
assertMap( | ||
ObjectPath.eval("properties.pod.properties", mapping), | ||
matchesMap().extraOk().entry("name", matchesMap().entry("type", "alias").entry("path", "attributes.pod.name")) | ||
); | ||
assertMap( | ||
ObjectPath.eval("properties.pod.properties", mapping), | ||
matchesMap().extraOk().entry("uid", matchesMap().entry("type", "alias").entry("path", "attributes.pod.uid")) | ||
); | ||
assertMap( | ||
ObjectPath.eval("properties.pod.properties", mapping), | ||
matchesMap().extraOk().entry("ip", matchesMap().entry("type", "alias").entry("path", "attributes.pod.ip")) | ||
); | ||
} | ||
|
||
static String formatInstant(Instant instant) { | ||
return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters