Skip to content

Commit 430c9fa

Browse files
authored
Optimize loading mappings in logsdb index settings provider
Optimize mapping service usages in when determining synthetic source usage and assessing whether host.name field can be used for index sorting. This is done by excluding the part of the mapping we're not interested in. This replaces #119935 and addresses the problem differently, by just loading the parts of the mapping LogsdbIndexModeSettingsProvider is interested in and by this lowering the overhead of parsing/merging mappings that has been reported via #119552.
1 parent 91b6a21 commit 430c9fa

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

docs/changelog/120055.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 120055
2+
summary: Optimize loading mappings when determining synthetic source usage and whether host.name can be sorted on.
3+
area: Logs
4+
type: enhancement
5+
issues: []

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.compress.CompressedXContent;
1717
import org.elasticsearch.common.regex.Regex;
1818
import org.elasticsearch.common.settings.Settings;
19+
import org.elasticsearch.common.xcontent.XContentHelper;
1920
import org.elasticsearch.core.CheckedFunction;
2021
import org.elasticsearch.core.Strings;
2122
import org.elasticsearch.index.IndexMode;
@@ -30,12 +31,14 @@
3031
import org.elasticsearch.index.mapper.NumberFieldMapper;
3132
import org.elasticsearch.index.mapper.ObjectMapper;
3233
import org.elasticsearch.index.mapper.SourceFieldMapper;
34+
import org.elasticsearch.xcontent.XContentType;
3335

3436
import java.io.IOException;
3537
import java.time.Instant;
3638
import java.util.ArrayList;
3739
import java.util.List;
3840
import java.util.Locale;
41+
import java.util.Set;
3942
import java.util.function.Supplier;
4043

4144
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_ROUTING_PATH;
@@ -44,6 +47,7 @@
4447
final class LogsdbIndexModeSettingsProvider implements IndexSettingProvider {
4548
private static final Logger LOGGER = LogManager.getLogger(LogsdbIndexModeSettingsProvider.class);
4649
private static final String LOGS_PATTERN = "logs-*-*";
50+
private static final Set<String> MAPPING_INCLUDES = Set.of("_doc._source.*", "_doc.properties.host**", "_doc.subobjects");
4751

4852
private final SyntheticSourceLicenseService syntheticSourceLicenseService;
4953
private final SetOnce<CheckedFunction<IndexMetadata, MapperService, IOException>> mapperServiceFactory = new SetOnce<>();
@@ -232,6 +236,19 @@ MappingHints getMappingHints(
232236
// combinedTemplateMappings can be empty when creating a normal index that doesn't match any template and without mapping.
233237
if (combinedTemplateMappings == null || combinedTemplateMappings.isEmpty()) {
234238
combinedTemplateMappings = List.of(new CompressedXContent("{}"));
239+
} else {
240+
// Filter the mapping to contain only the part this index settings provider is interested in.
241+
// This reduces the overhead of loading mappings, since mappings can be very large.
242+
// The _doc._source.mode is needed to determine synthetic source usage.
243+
// The _doc.properties.host* is needed to determine whether host.name field can be injected.
244+
// The _doc.subobjects is needed to determine whether subobjects is enabled.
245+
List<CompressedXContent> filteredMappings = new ArrayList<>(combinedTemplateMappings.size());
246+
for (CompressedXContent mappingSource : combinedTemplateMappings) {
247+
var ref = mappingSource.compressedReference();
248+
var map = XContentHelper.convertToMap(ref, true, XContentType.JSON, MAPPING_INCLUDES, Set.of()).v2();
249+
filteredMappings.add(new CompressedXContent(map));
250+
}
251+
combinedTemplateMappings = filteredMappings;
235252
}
236253
mapperService.merge(MapperService.SINGLE_MAPPING_NAME, combinedTemplateMappings, MapperService.MergeReason.INDEX_TEMPLATE);
237254
Mapper hostName = mapperService.mappingLookup().getMapper("host.name");

x-pack/plugin/logsdb/src/yamlRestTest/resources/rest-api-spec/test/30_logsdb_default_mapping.yml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,116 @@ create logsdb data stream with host.name as integer and timestamp as date:
9898
- do:
9999
indices.create_data_stream:
100100
name: "logsdb"
101+
- is_true: acknowledged
102+
103+
- do:
104+
indices.get_data_stream:
105+
name: "logsdb"
106+
expand_wildcards: hidden
107+
- length: { data_streams: 1 }
108+
- set: { data_streams.0.indices.0.index_name: backing_index }
109+
110+
- do:
111+
indices.get_settings:
112+
index: $backing_index
113+
- match: { .$backing_index.settings.index.mode: logsdb }
114+
- is_false: .$backing_index.settings.index.logsdb.add_host_name_field
115+
- match: { .$backing_index.settings.index.logsdb.sort_on_host_name: "true" }
116+
117+
---
118+
create logsdb data stream with no host.name and timestamp as date:
119+
- requires:
120+
test_runner_features: [ "allowed_warnings" ]
121+
122+
- do:
123+
cluster.put_component_template:
124+
name: "logsdb-mappings"
125+
body:
126+
template:
127+
settings:
128+
mode: "logsdb"
129+
mappings:
130+
properties:
131+
"@timestamp":
132+
type: "date"
133+
134+
- do:
135+
indices.put_index_template:
136+
name: "logsdb-index-template"
137+
body:
138+
index_patterns: ["logsdb"]
139+
data_stream: {}
140+
composed_of: ["logsdb-mappings"]
141+
allowed_warnings:
142+
- "index template [logsdb-index-template] has index patterns [logsdb] matching patterns from existing older templates [global] with patterns (global => [*]); this template [logsdb-index-template] will take precedence during new index creation"
101143

144+
- do:
145+
indices.create_data_stream:
146+
name: "logsdb"
102147
- is_true: acknowledged
103148

149+
- do:
150+
indices.get_data_stream:
151+
name: "logsdb"
152+
expand_wildcards: hidden
153+
- length: { data_streams: 1 }
154+
- set: { data_streams.0.indices.0.index_name: backing_index }
155+
156+
- do:
157+
indices.get_settings:
158+
index: $backing_index
159+
- match: { .$backing_index.settings.index.mode: logsdb }
160+
- match: { .$backing_index.settings.index.logsdb.add_host_name_field: "true" }
161+
- match: { .$backing_index.settings.index.logsdb.sort_on_host_name: "true" }
162+
163+
---
164+
create logsdb data stream with host as keyword and timestamp as date:
165+
- requires:
166+
test_runner_features: [ "allowed_warnings" ]
167+
168+
- do:
169+
cluster.put_component_template:
170+
name: "logsdb-mappings"
171+
body:
172+
template:
173+
settings:
174+
mode: "logsdb"
175+
mappings:
176+
properties:
177+
host:
178+
type: "keyword"
179+
"@timestamp":
180+
type: "date"
181+
182+
- do:
183+
indices.put_index_template:
184+
name: "logsdb-index-template"
185+
body:
186+
index_patterns: ["logsdb"]
187+
data_stream: {}
188+
composed_of: ["logsdb-mappings"]
189+
allowed_warnings:
190+
- "index template [logsdb-index-template] has index patterns [logsdb] matching patterns from existing older templates [global] with patterns (global => [*]); this template [logsdb-index-template] will take precedence during new index creation"
191+
192+
- do:
193+
indices.create_data_stream:
194+
name: "logsdb"
195+
- is_true: acknowledged
196+
197+
- do:
198+
indices.get_data_stream:
199+
name: "logsdb"
200+
expand_wildcards: hidden
201+
- length: { data_streams: 1 }
202+
- set: { data_streams.0.indices.0.index_name: backing_index }
203+
204+
- do:
205+
indices.get_settings:
206+
index: $backing_index
207+
- match: { .$backing_index.settings.index.mode: logsdb }
208+
- is_false: .$backing_index.settings.index.logsdb.add_host_name_field
209+
- is_false: .$backing_index.settings.index.logsdb.sort_on_host_name
210+
104211
---
105212
create logsdb data stream with host as keyword:
106213
- requires:

0 commit comments

Comments
 (0)