Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.elasticsearch.xpack.logsdb;

import org.elasticsearch.client.Request;
import org.elasticsearch.index.IndexFeatures;
import org.elasticsearch.test.rest.ObjectPath;

import java.io.IOException;
Expand All @@ -22,11 +23,13 @@

public class TsdbIT extends AbstractLogsdbRollingUpgradeTestCase {

static final String TEMPLATE = """
private static final String SYNTHETIC_ID_PLACEHOLDER = "$SYNTHETIC_ID_SETTING";
// Do not access directly, use getTemplate(Boolean useSyntheticId)
private static final String TEMPLATE = """
{
"settings":{
"index": {
"mode": "time_series"
"mode": "time_series"%s
}
},
"mappings":{
Expand Down Expand Up @@ -80,7 +83,17 @@ public class TsdbIT extends AbstractLogsdbRollingUpgradeTestCase {
}
}
}
""";
""".formatted(SYNTHETIC_ID_PLACEHOLDER);

/**
* Returns the template with optional synthetic_id setting. When {@code useSyntheticId} is null the setting is omitted;
* when true/false, adds {@code "mapping": { "synthetic_id": true/false }} to index settings.
*/
static String getTemplate(Boolean useSyntheticId) {
String replacement = useSyntheticId == null ? "" : ", \"mapping\": { \"synthetic_id\": " + useSyntheticId + " }";
return TEMPLATE.replace(SYNTHETIC_ID_PLACEHOLDER, replacement);
}

private static final String BULK =
"""
{"create": {}}
Expand Down Expand Up @@ -120,6 +133,9 @@ public class TsdbIT extends AbstractLogsdbRollingUpgradeTestCase {
""";

public void testTsdbDataStream() throws Exception {
boolean hasSupport = oldClusterHasFeature(IndexFeatures.TIME_SERIES_SYNTHETIC_ID);
Boolean useSyntheticId = hasSupport ? randomBoolean() : null;

String dataStreamName = "k8s";
final String INDEX_TEMPLATE = """
{
Expand All @@ -131,7 +147,9 @@ public void testTsdbDataStream() throws Exception {
// Add composable index template
String templateName = "1";
var putIndexTemplateRequest = new Request("POST", "/_index_template/" + templateName);
putIndexTemplateRequest.setJsonEntity(INDEX_TEMPLATE.replace("$TEMPLATE", TEMPLATE).replace("$PATTERN", dataStreamName));
putIndexTemplateRequest.setJsonEntity(
INDEX_TEMPLATE.replace("$TEMPLATE", getTemplate(useSyntheticId)).replace("$PATTERN", dataStreamName)
);
assertOK(client().performRequest(putIndexTemplateRequest));

performOldClustertOperations(templateName, dataStreamName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.IndexFeatures;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.xcontent.XContentType;

Expand All @@ -25,8 +29,8 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import static org.elasticsearch.xpack.logsdb.TsdbIT.TEMPLATE;
import static org.elasticsearch.xpack.logsdb.TsdbIT.formatInstant;
import static org.elasticsearch.xpack.logsdb.TsdbIT.getTemplate;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand All @@ -40,16 +44,27 @@ public class TsdbIndexingRollingUpgradeIT extends AbstractLogsdbRollingUpgradeTe
""";

public void testIndexing() throws Exception {
// If cluster has support for synthetic id, randomly set index.mapping.synthetic_id to true/false
// If cluster doesn't have support, don't set index.mapping.synthetic_id at all (null),
// indicated by oldClusterHasFeature(IndexFeatures.TIME_SERIES_SYNTHETIC_ID)==false
Boolean useSyntheticId = oldClusterHasFeature(IndexFeatures.TIME_SERIES_SYNTHETIC_ID) ? randomBoolean() : null;

String dataStreamName = "k9s";
createTemplate(dataStreamName, getClass().getSimpleName().toLowerCase(Locale.ROOT), TEMPLATE);
createTemplate(dataStreamName, getClass().getSimpleName().toLowerCase(Locale.ROOT), getTemplate(useSyntheticId));

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

String firstBackingIndex = getDataStreamBackingIndexNames(dataStreamName).getFirst();
var settings = (Map<?, ?>) getIndexSettings(firstBackingIndex, true).get(firstBackingIndex);
assertThat(((Map<?, ?>) settings.get("settings")).get("index.mode"), equalTo("time_series"));
assertThat(((Map<?, ?>) settings.get("defaults")).get("index.mapping.source.mode"), equalTo("SYNTHETIC"));
assertThat(((Map<?, ?>) settings.get("settings")).get(IndexSettings.MODE.getKey()), equalTo(IndexMode.TIME_SERIES.getName()));
assertThat(
((Map<?, ?>) settings.get("defaults")).get(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey()),
equalTo(SourceFieldMapper.Mode.SYNTHETIC.toString())
);
if (useSyntheticId != null) {
assertThat(((Map<?, ?>) settings.get("settings")).get(IndexSettings.SYNTHETIC_ID.getKey()), equalTo(useSyntheticId.toString()));
}

var mapping = getIndexMappingAsMap(firstBackingIndex);
assertThat(
Expand Down