Skip to content

Commit 7ed32c2

Browse files
authored
[8.17] Add source mode stats to MappingStats (elastic#117697)
* Add source mode stats to MappingStats (elastic#117463) * update bwc logic for 8.17
1 parent d6fdea4 commit 7ed32c2

File tree

9 files changed

+223
-12
lines changed

9 files changed

+223
-12
lines changed

docs/reference/cluster/stats.asciidoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,10 @@ The API returns the following response:
16441644
"total_deduplicated_mapping_size": "0b",
16451645
"total_deduplicated_mapping_size_in_bytes": 0,
16461646
"field_types": [],
1647-
"runtime_field_types": []
1647+
"runtime_field_types": [],
1648+
"source_modes" : {
1649+
"stored": 0
1650+
}
16481651
},
16491652
"analysis": {
16501653
"char_filter_types": [],
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
test source modes:
3+
- requires:
4+
cluster_features: ["cluster.stats.source_modes"]
5+
reason: requires source modes features
6+
7+
- do:
8+
indices.create:
9+
index: test-synthetic
10+
body:
11+
settings:
12+
index:
13+
mapping:
14+
source.mode: synthetic
15+
16+
- do:
17+
indices.create:
18+
index: test-stored
19+
20+
- do:
21+
indices.create:
22+
index: test-disabled
23+
body:
24+
settings:
25+
index:
26+
mapping:
27+
source.mode: disabled
28+
29+
- do:
30+
bulk:
31+
refresh: true
32+
body:
33+
- '{ "create": { "_index": "test-synthetic" } }'
34+
- '{ "name": "aaaa", "some_string": "AaAa", "some_int": 1000, "some_double": 123.456789, "some_bool": true }'
35+
- '{ "create": { "_index": "test-stored" } }'
36+
- '{ "name": "bbbb", "some_string": "BbBb", "some_int": 2000, "some_double": 321.987654, "some_bool": false }'
37+
- '{ "create": { "_index": "test-disabled" } }'
38+
- '{ "name": "cccc", "some_string": "CcCc", "some_int": 3000, "some_double": 421.484654, "some_bool": false }'
39+
40+
- do:
41+
search:
42+
index: test-*
43+
- match: { hits.total.value: 3 }
44+
45+
- do:
46+
cluster.stats: { }
47+
48+
- match: { indices.mappings.source_modes.disabled: 1 }
49+
- match: { indices.mappings.source_modes.stored: 1 }
50+
- match: { indices.mappings.source_modes.synthetic: 1 }

server/src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@
434434
org.elasticsearch.search.SearchFeatures,
435435
org.elasticsearch.script.ScriptFeatures,
436436
org.elasticsearch.search.retriever.RetrieversFeatures,
437-
org.elasticsearch.reservedstate.service.FileSettingsFeatures;
437+
org.elasticsearch.reservedstate.service.FileSettingsFeatures,
438+
org.elasticsearch.action.admin.cluster.stats.ClusterStatsFeatures;
438439

439440
uses org.elasticsearch.plugins.internal.SettingsExtension;
440441
uses RestExtension;

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ static TransportVersion def(int id) {
205205
public static final TransportVersion ESQL_ENRICH_RUNTIME_WARNINGS = def(8_796_00_0);
206206
public static final TransportVersion INGEST_PIPELINE_CONFIGURATION_AS_MAP = def(8_797_00_0);
207207
public static final TransportVersion LOGSDB_TELEMETRY_CUSTOM_CUTOFF_DATE_FIX_8_17 = def(8_797_00_1);
208+
public static final TransportVersion SOURCE_MODE_TELEMETRY_FIX_8_17 = def(8_797_00_2);
208209
/*
209210
* STOP! READ THIS FIRST! No, really,
210211
* ____ _____ ___ ____ _ ____ _____ _ ____ _____ _ _ ___ ____ _____ ___ ____ ____ _____ _
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.admin.cluster.stats;
11+
12+
import org.elasticsearch.features.FeatureSpecification;
13+
import org.elasticsearch.features.NodeFeature;
14+
15+
import java.util.Set;
16+
17+
/**
18+
* Spec for cluster stats features.
19+
*/
20+
public class ClusterStatsFeatures implements FeatureSpecification {
21+
22+
@Override
23+
public Set<NodeFeature> getFeatures() {
24+
return Set.of(MappingStats.SOURCE_MODES_FEATURE);
25+
}
26+
}

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/MappingStats.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.action.admin.cluster.stats;
1111

12+
import org.elasticsearch.TransportVersion;
1213
import org.elasticsearch.TransportVersions;
1314
import org.elasticsearch.cluster.metadata.IndexMetadata;
1415
import org.elasticsearch.cluster.metadata.MappingMetadata;
@@ -19,6 +20,8 @@
1920
import org.elasticsearch.common.io.stream.Writeable;
2021
import org.elasticsearch.common.unit.ByteSizeValue;
2122
import org.elasticsearch.core.Nullable;
23+
import org.elasticsearch.features.NodeFeature;
24+
import org.elasticsearch.index.mapper.SourceFieldMapper;
2225
import org.elasticsearch.xcontent.ToXContentFragment;
2326
import org.elasticsearch.xcontent.XContentBuilder;
2427

@@ -31,6 +34,7 @@
3134
import java.util.HashSet;
3235
import java.util.IdentityHashMap;
3336
import java.util.List;
37+
import java.util.Locale;
3438
import java.util.Map;
3539
import java.util.Objects;
3640
import java.util.OptionalLong;
@@ -44,6 +48,8 @@
4448
*/
4549
public final class MappingStats implements ToXContentFragment, Writeable {
4650

51+
static final NodeFeature SOURCE_MODES_FEATURE = new NodeFeature("cluster.stats.source_modes");
52+
4753
private static final Pattern DOC_PATTERN = Pattern.compile("doc[\\[.]");
4854
private static final Pattern SOURCE_PATTERN = Pattern.compile("params\\._source");
4955

@@ -53,6 +59,8 @@ public final class MappingStats implements ToXContentFragment, Writeable {
5359
public static MappingStats of(Metadata metadata, Runnable ensureNotCancelled) {
5460
Map<String, FieldStats> fieldTypes = new HashMap<>();
5561
Set<String> concreteFieldNames = new HashSet<>();
62+
// Account different source modes based on index.mapping.source.mode setting:
63+
Map<String, Integer> sourceModeUsageCount = new HashMap<>();
5664
Map<String, RuntimeFieldStats> runtimeFieldTypes = new HashMap<>();
5765
final Map<MappingMetadata, Integer> mappingCounts = new IdentityHashMap<>(metadata.getMappingsByHash().size());
5866
for (IndexMetadata indexMetadata : metadata) {
@@ -62,6 +70,9 @@ public static MappingStats of(Metadata metadata, Runnable ensureNotCancelled) {
6270
continue;
6371
}
6472
AnalysisStats.countMapping(mappingCounts, indexMetadata);
73+
74+
var sourceMode = SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexMetadata.getSettings());
75+
sourceModeUsageCount.merge(sourceMode.toString().toLowerCase(Locale.ENGLISH), 1, Integer::sum);
6576
}
6677
final AtomicLong totalFieldCount = new AtomicLong();
6778
final AtomicLong totalDeduplicatedFieldCount = new AtomicLong();
@@ -175,12 +186,14 @@ public static MappingStats of(Metadata metadata, Runnable ensureNotCancelled) {
175186
for (MappingMetadata mappingMetadata : metadata.getMappingsByHash().values()) {
176187
totalMappingSizeBytes += mappingMetadata.source().compressed().length;
177188
}
189+
178190
return new MappingStats(
179191
totalFieldCount.get(),
180192
totalDeduplicatedFieldCount.get(),
181193
totalMappingSizeBytes,
182194
fieldTypes.values(),
183-
runtimeFieldTypes.values()
195+
runtimeFieldTypes.values(),
196+
sourceModeUsageCount
184197
);
185198
}
186199

@@ -215,17 +228,20 @@ private static int countOccurrences(String script, Pattern pattern) {
215228

216229
private final List<FieldStats> fieldTypeStats;
217230
private final List<RuntimeFieldStats> runtimeFieldStats;
231+
private final Map<String, Integer> sourceModeUsageCount;
218232

219233
MappingStats(
220234
long totalFieldCount,
221235
long totalDeduplicatedFieldCount,
222236
long totalMappingSizeBytes,
223237
Collection<FieldStats> fieldTypeStats,
224-
Collection<RuntimeFieldStats> runtimeFieldStats
238+
Collection<RuntimeFieldStats> runtimeFieldStats,
239+
Map<String, Integer> sourceModeUsageCount
225240
) {
226241
this.totalFieldCount = totalFieldCount;
227242
this.totalDeduplicatedFieldCount = totalDeduplicatedFieldCount;
228243
this.totalMappingSizeBytes = totalMappingSizeBytes;
244+
this.sourceModeUsageCount = sourceModeUsageCount;
229245
List<FieldStats> stats = new ArrayList<>(fieldTypeStats);
230246
stats.sort(Comparator.comparing(IndexFeatureStats::getName));
231247
this.fieldTypeStats = Collections.unmodifiableList(stats);
@@ -246,6 +262,10 @@ private static int countOccurrences(String script, Pattern pattern) {
246262
}
247263
fieldTypeStats = in.readCollectionAsImmutableList(FieldStats::new);
248264
runtimeFieldStats = in.readCollectionAsImmutableList(RuntimeFieldStats::new);
265+
var transportVersion = in.getTransportVersion();
266+
sourceModeUsageCount = canReadOrWriteSourceModeTelemetry(transportVersion)
267+
? in.readImmutableMap(StreamInput::readString, StreamInput::readVInt)
268+
: Map.of();
249269
}
250270

251271
@Override
@@ -257,6 +277,14 @@ public void writeTo(StreamOutput out) throws IOException {
257277
}
258278
out.writeCollection(fieldTypeStats);
259279
out.writeCollection(runtimeFieldStats);
280+
var transportVersion = out.getTransportVersion();
281+
if (canReadOrWriteSourceModeTelemetry(transportVersion)) {
282+
out.writeMap(sourceModeUsageCount, StreamOutput::writeVInt);
283+
}
284+
}
285+
286+
private static boolean canReadOrWriteSourceModeTelemetry(TransportVersion version) {
287+
return version.onOrAfter(TransportVersions.SOURCE_MODE_TELEMETRY_FIX_8_17);
260288
}
261289

262290
private static OptionalLong ofNullable(Long l) {
@@ -300,6 +328,10 @@ public List<RuntimeFieldStats> getRuntimeFieldStats() {
300328
return runtimeFieldStats;
301329
}
302330

331+
public Map<String, Integer> getSourceModeUsageCount() {
332+
return sourceModeUsageCount;
333+
}
334+
303335
@Override
304336
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
305337
builder.startObject("mappings");
@@ -326,6 +358,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
326358
st.toXContent(builder, params);
327359
}
328360
builder.endArray();
361+
builder.startObject("source_modes");
362+
var entries = sourceModeUsageCount.entrySet().stream().sorted(Map.Entry.comparingByKey()).toList();
363+
for (var entry : entries) {
364+
builder.field(entry.getKey(), entry.getValue());
365+
}
366+
builder.endObject();
329367
builder.endObject();
330368
return builder;
331369
}
@@ -344,11 +382,19 @@ public boolean equals(Object o) {
344382
&& Objects.equals(totalDeduplicatedFieldCount, that.totalDeduplicatedFieldCount)
345383
&& Objects.equals(totalMappingSizeBytes, that.totalMappingSizeBytes)
346384
&& fieldTypeStats.equals(that.fieldTypeStats)
347-
&& runtimeFieldStats.equals(that.runtimeFieldStats);
385+
&& runtimeFieldStats.equals(that.runtimeFieldStats)
386+
&& sourceModeUsageCount.equals(that.sourceModeUsageCount);
348387
}
349388

350389
@Override
351390
public int hashCode() {
352-
return Objects.hash(totalFieldCount, totalDeduplicatedFieldCount, totalMappingSizeBytes, fieldTypeStats, runtimeFieldStats);
391+
return Objects.hash(
392+
totalFieldCount,
393+
totalDeduplicatedFieldCount,
394+
totalMappingSizeBytes,
395+
fieldTypeStats,
396+
runtimeFieldStats,
397+
sourceModeUsageCount
398+
);
353399
}
354400
}

server/src/main/resources/META-INF/services/org.elasticsearch.features.FeatureSpecification

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ org.elasticsearch.search.retriever.RetrieversFeatures
2323
org.elasticsearch.script.ScriptFeatures
2424
org.elasticsearch.reservedstate.service.FileSettingsFeatures
2525
org.elasticsearch.cluster.routing.RoutingFeatures
26+
org.elasticsearch.action.admin.cluster.stats.ClusterStatsFeatures

0 commit comments

Comments
 (0)