Skip to content

Commit 378c475

Browse files
committed
Populate both index.dimensions and index.routing_path
1 parent ec975a9 commit 378c475

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

modules/data-streams/src/main/java/org/elasticsearch/datastreams/DataStreamIndexSettingsProvider.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ public void provideAdditionalMetadata(
131131
);
132132
if (dimensions.isEmpty() == false) {
133133
if (matchesAllDimensions) {
134+
// Only set index.dimensions if the paths in the dimensions list match all potential dimension fields.
135+
// This is not the case e.g. if a dynamic template matches by match_mapping_type instead of path_match
134136
additionalSettings.putList(INDEX_DIMENSIONS.getKey(), dimensions);
135-
} else {
136-
// Fall back to setting index.routing_path if the paths in the dimensions list don't match all potential
137-
// dimension fields (e.g. if a dynamic template matches by type instead of path).
138-
additionalSettings.putList(INDEX_ROUTING_PATH.getKey(), dimensions);
139137
}
138+
// always populate index.routing_path, so that routing works for older index versions
139+
// this applies to indices created during a rolling upgrade
140+
additionalSettings.putList(INDEX_ROUTING_PATH.getKey(), dimensions);
140141
}
141142
}
142143
}
@@ -171,8 +172,10 @@ public void onUpdateMappings(
171172
if (matchesAllDimensions) {
172173
additionalSettings.putList(INDEX_DIMENSIONS.getKey(), newIndexDimensions);
173174
} else {
174-
// If the new dimensions don't match all potential dimension fields, we need to set index.routing_path
175-
additionalSettings.putList(INDEX_ROUTING_PATH.getKey(), newIndexDimensions);
175+
// If the new dimensions don't match all potential dimension fields, we need to unset index.dimensions
176+
// so that index.routing_path is used instead.
177+
// This can happen if a new dynamic template is added to an existing index that matches by mapping type instead of path_match.
178+
additionalSettings.putList(INDEX_DIMENSIONS.getKey(), List.of());
176179
}
177180
}
178181

modules/data-streams/src/test/java/org/elasticsearch/datastreams/DataStreamIndexSettingsProviderTests.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ public void testGetAdditionalIndexSettings() throws Exception {
104104
// The index.time_series.end_time setting requires index.mode to be set to time_series adding it here so that we read this setting:
105105
// (in production the index.mode setting is usually provided in an index or component template)
106106
result = builder().put(result).put("index.mode", "time_series").build();
107-
assertThat(result.size(), equalTo(4));
107+
assertThat(result.size(), equalTo(5));
108108
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
109109
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
110110
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
111+
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), containsInAnyOrder("field3", "field4", "field5", "field6"));
111112
assertThat(IndexMetadata.INDEX_DIMENSIONS.get(result), containsInAnyOrder("field3", "field4", "field5", "field6"));
112113
}
113114

@@ -230,10 +231,11 @@ public void testGetAdditionalIndexSettingsMappingsMerging() throws Exception {
230231
// The index.time_series.end_time setting requires index.mode to be set to time_series adding it here so that we read this setting:
231232
// (in production the index.mode setting is usually provided in an index or component template)
232233
result = builder().put(result).put("index.mode", "time_series").build();
233-
assertThat(result.size(), equalTo(4));
234+
assertThat(result.size(), equalTo(5));
234235
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
235236
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
236237
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
238+
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), containsInAnyOrder("field1", "field3"));
237239
assertThat(IndexMetadata.INDEX_DIMENSIONS.get(result), containsInAnyOrder("field1", "field3"));
238240
}
239241

@@ -529,10 +531,11 @@ public void testGenerateRoutingPathFromDynamicTemplate() throws Exception {
529531
""";
530532
ImmutableOpenMap.Builder<String, Map<String, String>> customMetadataBuilder = ImmutableOpenMap.builder();
531533
Settings result = generateTsdbSettings(mapping, now, customMetadataBuilder);
532-
assertThat(result.size(), equalTo(4));
534+
assertThat(result.size(), equalTo(5));
533535
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
534536
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
535537
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
538+
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), containsInAnyOrder("host.id", "prometheus.labels.*"));
536539
assertThat(IndexMetadata.INDEX_DIMENSIONS.get(result), containsInAnyOrder("host.id", "prometheus.labels.*"));
537540
}
538541

@@ -570,10 +573,14 @@ public void testGenerateRoutingPathFromDynamicTemplateWithMultiplePathMatchEntri
570573
""";
571574
ImmutableOpenMap.Builder<String, Map<String, String>> customMetadataBuilder = ImmutableOpenMap.builder();
572575
Settings result = generateTsdbSettings(mapping, now, customMetadataBuilder);
573-
assertThat(result.size(), equalTo(4));
576+
assertThat(result.size(), equalTo(5));
574577
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
575578
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
576579
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
580+
assertThat(
581+
IndexMetadata.INDEX_ROUTING_PATH.get(result),
582+
containsInAnyOrder("host.id", "xprometheus.labels.*", "yprometheus.labels.*")
583+
);
577584
assertThat(
578585
IndexMetadata.INDEX_DIMENSIONS.get(result),
579586
containsInAnyOrder("host.id", "xprometheus.labels.*", "yprometheus.labels.*")
@@ -619,14 +626,18 @@ public void testGenerateRoutingPathFromDynamicTemplateWithMultiplePathMatchEntri
619626
""";
620627
ImmutableOpenMap.Builder<String, Map<String, String>> customMetadataBuilder = ImmutableOpenMap.builder();
621628
Settings result = generateTsdbSettings(mapping, now, customMetadataBuilder);
622-
assertThat(result.size(), equalTo(4));
629+
assertThat(result.size(), equalTo(5));
623630
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
624631
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
625632
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
626633
assertThat(
627634
IndexMetadata.INDEX_DIMENSIONS.get(result),
628635
containsInAnyOrder("host.id", "xprometheus.labels.*", "yprometheus.labels.*")
629636
);
637+
assertThat(
638+
IndexMetadata.INDEX_ROUTING_PATH.get(result),
639+
containsInAnyOrder("host.id", "xprometheus.labels.*", "yprometheus.labels.*")
640+
);
630641
}
631642

632643
public void testGenerateRoutingPathFromDynamicTemplate_templateWithNoPathMatch() throws Exception {
@@ -672,10 +683,11 @@ public void testGenerateRoutingPathFromDynamicTemplate_templateWithNoPathMatch()
672683
""";
673684
ImmutableOpenMap.Builder<String, Map<String, String>> customMetadataBuilder = ImmutableOpenMap.builder();
674685
Settings result = generateTsdbSettings(mapping, now, customMetadataBuilder);
675-
assertThat(result.size(), equalTo(4));
686+
assertThat(result.size(), equalTo(5));
676687
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
677688
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
678689
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
690+
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), containsInAnyOrder("host.id", "prometheus.labels.*"));
679691
assertThat(IndexMetadata.INDEX_DIMENSIONS.get(result), containsInAnyOrder("host.id", "prometheus.labels.*"));
680692
}
681693

@@ -756,10 +768,11 @@ public void testGenerateRoutingPathFromPassThroughObject() throws Exception {
756768
""";
757769
ImmutableOpenMap.Builder<String, Map<String, String>> customMetadataBuilder = ImmutableOpenMap.builder();
758770
Settings result = generateTsdbSettings(mapping, now, customMetadataBuilder);
759-
assertThat(result.size(), equalTo(4));
771+
assertThat(result.size(), equalTo(5));
760772
assertThat(IndexSettings.MODE.get(result), equalTo(IndexMode.TIME_SERIES));
761773
assertThat(IndexSettings.TIME_SERIES_START_TIME.get(result), equalTo(now.minusMillis(DEFAULT_LOOK_BACK_TIME.getMillis())));
762774
assertThat(IndexSettings.TIME_SERIES_END_TIME.get(result), equalTo(now.plusMillis(DEFAULT_LOOK_AHEAD_TIME.getMillis())));
775+
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), containsInAnyOrder("labels.*"));
763776
assertThat(IndexMetadata.INDEX_DIMENSIONS.get(result), containsInAnyOrder("labels.*"));
764777
}
765778

server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ public static class ExtractFromSource extends IndexRouting {
337337
includePaths = metadata.getRoutingPaths();
338338
if (indexMode == IndexMode.TIME_SERIES) {
339339
if (metadata.getTimeSeriesDimensions().isEmpty() == false
340-
&& metadata.getRoutingPaths().isEmpty()
341340
&& metadata.getCreationVersion().onOrAfter(IndexVersions.TSID_CREATED_DURING_ROUTING)) {
342341
// This optimization is only available for new indices where
343342
// the dimensions index setting is automatically populated from the mappings.

0 commit comments

Comments
 (0)