Skip to content

Commit db02d9b

Browse files
dnhatnnicktindall
andauthored
Avoid throw exception in SyntheticSourceIndexSettingsProvider (#114662)
Backport of #114479: - Move buildIndexMetadata inside a try/catch block to prevent errors, as SyntheticSourceIndexSettingsProvider can use a incomplete settings to build an IndexMetadata. - Update documentation for index_routing_shards. Co-authored-by: Nick Tindall <[email protected]>
1 parent 25ffc15 commit db02d9b

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

docs/reference/index-modules.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ preview:[]
122122

123123
The number of shards a custom <<mapping-routing-field,routing>> value can go to.
124124
Defaults to 1 and can only be set at index creation time. This value must be less
125-
than the `index.number_of_shards` unless the `index.number_of_shards` value is also 1.
125+
than the `index.number_of_routing_shards` unless the `index.number_of_routing_shards` value is also 1.
126126
See <<routing-index-partition>> for more details about how this setting is used.
127127

128128
[[ccr-index-soft-deletes]]

server/src/internalClusterTest/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ public void testPartitionedTemplate() throws Exception {
881881
);
882882
assertThat(
883883
eBadSettings.getMessage(),
884-
containsString("partition size [6] should be a positive number less than the number of shards [5]")
884+
containsString("partition size [6] should be a positive number less than the number of routing shards [5]")
885885
);
886886

887887
// provide an invalid mapping for a partitioned index
@@ -913,7 +913,7 @@ public void testPartitionedTemplate() throws Exception {
913913

914914
assertThat(
915915
eBadIndex.getMessage(),
916-
containsString("partition size [6] should be a positive number less than the number of shards [5]")
916+
containsString("partition size [6] should be a positive number less than the number of routing shards [5]")
917917
);
918918

919919
// finally, create a valid index

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ IndexMetadata build(boolean repair) {
22682268
"routing partition size ["
22692269
+ routingPartitionSize
22702270
+ "] should be a positive number"
2271-
+ " less than the number of shards ["
2271+
+ " less than the number of routing shards ["
22722272
+ getRoutingNumShards()
22732273
+ "] for ["
22742274
+ index

x-pack/plugin/logsdb/src/main/java/org/elasticsearch/xpack/logsdb/SyntheticSourceIndexSettingsProvider.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ boolean newIndexHasSyntheticSourceUsage(
7979
return false;
8080
}
8181

82-
var tmpIndexMetadata = buildIndexMetadataForMapperService(indexName, isTimeSeries, indexTemplateAndCreateRequestSettings);
83-
try (var mapperService = mapperServiceFactory.apply(tmpIndexMetadata)) {
84-
// combinedTemplateMappings can be null when creating system indices
85-
// combinedTemplateMappings can be empty when creating a normal index that doesn't match any template and without mapping.
86-
if (combinedTemplateMappings == null || combinedTemplateMappings.isEmpty()) {
87-
combinedTemplateMappings = List.of(new CompressedXContent("{}"));
82+
try {
83+
var tmpIndexMetadata = buildIndexMetadataForMapperService(indexName, isTimeSeries, indexTemplateAndCreateRequestSettings);
84+
try (var mapperService = mapperServiceFactory.apply(tmpIndexMetadata)) {
85+
// combinedTemplateMappings can be null when creating system indices
86+
// combinedTemplateMappings can be empty when creating a normal index that doesn't match any template and without mapping.
87+
if (combinedTemplateMappings == null || combinedTemplateMappings.isEmpty()) {
88+
combinedTemplateMappings = List.of(new CompressedXContent("{}"));
89+
}
90+
mapperService.merge(MapperService.SINGLE_MAPPING_NAME, combinedTemplateMappings, MapperService.MergeReason.INDEX_TEMPLATE);
91+
return mapperService.documentMapper().sourceMapper().isSynthetic();
8892
}
89-
mapperService.merge(MapperService.SINGLE_MAPPING_NAME, combinedTemplateMappings, MapperService.MergeReason.INDEX_TEMPLATE);
90-
return mapperService.documentMapper().sourceMapper().isSynthetic();
9193
} catch (AssertionError | Exception e) {
9294
// In case invalid mappings or setting are provided, then mapper service creation can fail.
9395
// In that case it is ok to return false here. The index creation will fail anyway later, so need to fallback to stored source.

0 commit comments

Comments
 (0)