Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial metrics for synthetic source #106732

Merged
merged 22 commits into from
May 14, 2024
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 @@ -21,6 +21,7 @@
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.analysis.LowercaseNormalizer;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperRegistry;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ProvidedIdFieldMapper;
Expand Down Expand Up @@ -71,7 +72,8 @@ public static MapperService create(String mappings) {
public <T> T compile(Script script, ScriptContext<T> scriptContext) {
throw new UnsupportedOperationException();
}
}
},
MapperMetrics.NOOP
);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperRegistry;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParsedDocument;
Expand Down Expand Up @@ -154,7 +155,8 @@ protected SearchExecutionContext buildSearchExecutionContext() {
null,
() -> true,
null,
Collections.emptyMap()
Collections.emptyMap(),
MapperMetrics.NOOP
);
}

Expand Down Expand Up @@ -186,7 +188,8 @@ protected final MapperService createMapperService(String mappings) {
public <T> T compile(Script script, ScriptContext<T> scriptContext) {
throw new UnsupportedOperationException();
}
}
},
MapperMetrics.NOOP
);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.engine.NoOpEngine;
import org.elasticsearch.index.flush.FlushStats;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.seqno.RetentionLeaseSyncer;
import org.elasticsearch.index.seqno.SequenceNumbers;
Expand Down Expand Up @@ -629,7 +630,8 @@ public static final IndexShard newIndexShard(
cbs,
IndexModule.DEFAULT_SNAPSHOT_COMMIT_SUPPLIER,
System::nanoTime,
null
null,
MapperMetrics.NOOP
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.analysis.AnalyzerScope;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperRegistry;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.similarity.SimilarityService;
Expand Down Expand Up @@ -58,14 +59,16 @@ public class IndexMetadataVerifier {
private final MapperRegistry mapperRegistry;
private final IndexScopedSettings indexScopedSettings;
private final ScriptCompiler scriptService;
private final MapperMetrics mapperMetrics;

public IndexMetadataVerifier(
Settings settings,
ClusterService clusterService,
NamedXContentRegistry xContentRegistry,
MapperRegistry mapperRegistry,
IndexScopedSettings indexScopedSettings,
ScriptCompiler scriptCompiler
ScriptCompiler scriptCompiler,
MapperMetrics mapperMetrics
) {
this.settings = settings;
this.clusterService = clusterService;
Expand All @@ -74,6 +77,7 @@ public IndexMetadataVerifier(
this.mapperRegistry = mapperRegistry;
this.indexScopedSettings = indexScopedSettings;
this.scriptService = scriptCompiler;
this.mapperMetrics = mapperMetrics;
}

/**
Expand Down Expand Up @@ -182,7 +186,8 @@ protected TokenStreamComponents createComponents(String fieldName) {
mapperRegistry,
() -> null,
indexSettings.getMode().idFieldMapperWithoutFieldData(),
scriptService
scriptService,
mapperMetrics
)
) {
mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY);
Expand Down
12 changes: 9 additions & 3 deletions server/src/main/java/org/elasticsearch/index/IndexModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.engine.EngineFactory;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperRegistry;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.shard.IndexEventListener;
Expand Down Expand Up @@ -177,6 +178,7 @@ public interface DirectoryWrapper {
private final BooleanSupplier allowExpensiveQueries;
private final Map<String, IndexStorePlugin.RecoveryStateFactory> recoveryStateFactories;
private final SetOnce<Engine.IndexCommitListener> indexCommitListener = new SetOnce<>();
private final MapperMetrics mapperMetrics;

/**
* Construct the index module for the index with the specified index settings. The index module contains extension points for plugins
Expand All @@ -195,7 +197,8 @@ public IndexModule(
final BooleanSupplier allowExpensiveQueries,
final IndexNameExpressionResolver expressionResolver,
final Map<String, IndexStorePlugin.RecoveryStateFactory> recoveryStateFactories,
final SlowLogFieldProvider slowLogFieldProvider
final SlowLogFieldProvider slowLogFieldProvider,
final MapperMetrics mapperMetrics
) {
this.indexSettings = indexSettings;
this.analysisRegistry = analysisRegistry;
Expand All @@ -206,6 +209,7 @@ public IndexModule(
this.allowExpensiveQueries = allowExpensiveQueries;
this.expressionResolver = expressionResolver;
this.recoveryStateFactories = recoveryStateFactories;
this.mapperMetrics = mapperMetrics;
}

/**
Expand Down Expand Up @@ -536,7 +540,8 @@ public IndexService newIndexService(
recoveryStateFactory,
indexFoldersDeletionListener,
snapshotCommitSupplier,
indexCommitListener.get()
indexCommitListener.get(),
mapperMetrics
);
success = true;
return indexService;
Expand Down Expand Up @@ -646,7 +651,8 @@ public MapperService newIndexMapperService(
throw new UnsupportedOperationException("no index query shard context available");
},
indexSettings.getMode().idFieldMapperWithoutFieldData(),
scriptService
scriptService,
mapperMetrics
);
}

Expand Down
15 changes: 11 additions & 4 deletions server/src/main/java/org/elasticsearch/index/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.index.fielddata.ordinals.GlobalOrdinalsAccounting;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperRegistry;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MappingLookup;
Expand Down Expand Up @@ -158,6 +159,7 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust
private final IndexNameExpressionResolver expressionResolver;
private final Supplier<Sort> indexSortSupplier;
private final ValuesSourceRegistry valuesSourceRegistry;
private final MapperMetrics mapperMetrics;

@SuppressWarnings("this-escape")
public IndexService(
Expand Down Expand Up @@ -191,7 +193,8 @@ public IndexService(
IndexStorePlugin.RecoveryStateFactory recoveryStateFactory,
IndexStorePlugin.IndexFoldersDeletionListener indexFoldersDeletionListener,
IndexStorePlugin.SnapshotCommitSupplier snapshotCommitSupplier,
Engine.IndexCommitListener indexCommitListener
Engine.IndexCommitListener indexCommitListener,
MapperMetrics mapperMetrics
) {
super(indexSettings);
assert indexCreationContext != IndexCreationContext.RELOAD_ANALYZERS
Expand All @@ -218,7 +221,8 @@ public IndexService(
// we parse all percolator queries as they would be parsed on shard 0
() -> newSearchExecutionContext(0, 0, null, System::currentTimeMillis, null, emptyMap()),
idFieldMapper,
scriptService
scriptService,
mapperMetrics
);
this.indexFieldData = new IndexFieldDataService(indexSettings, indicesFieldDataCache, circuitBreakerService);
if (indexSettings.getIndexSortConfig().hasIndexSort()) {
Expand Down Expand Up @@ -263,6 +267,7 @@ public IndexService(
this.searchOperationListeners = Collections.unmodifiableList(searchOperationListeners);
this.indexingOperationListeners = Collections.unmodifiableList(indexingOperationListeners);
this.indexCommitListener = indexCommitListener;
this.mapperMetrics = mapperMetrics;
try (var ignored = threadPool.getThreadContext().clearTraceContext()) {
// kick off async ops for the first shard in this index
this.refreshTask = new AsyncRefreshTask(this);
Expand Down Expand Up @@ -541,7 +546,8 @@ public synchronized IndexShard createShard(
circuitBreakerService,
snapshotCommitSupplier,
System::nanoTime,
indexCommitListener
indexCommitListener,
mapperMetrics
);
eventListener.indexShardStateChanged(indexShard, null, indexShard.state(), "shard created");
eventListener.afterIndexShardCreated(indexShard);
Expand Down Expand Up @@ -685,7 +691,8 @@ public SearchExecutionContext newSearchExecutionContext(
allowExpensiveQueries,
valuesSourceRegistry,
runtimeMappings,
requestSize
requestSize,
mapperMetrics
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.index.mapper.IgnoredFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperMetrics;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MappingLookup;
import org.elasticsearch.index.mapper.RoutingFieldMapper;
Expand Down Expand Up @@ -59,11 +60,13 @@ public final class ShardGetService extends AbstractIndexShardComponent {
private final MeanMetric missingMetric = new MeanMetric();
private final CounterMetric currentMetric = new CounterMetric();
private final IndexShard indexShard;
private final MapperMetrics mapperMetrics;

public ShardGetService(IndexSettings indexSettings, IndexShard indexShard, MapperService mapperService) {
public ShardGetService(IndexSettings indexSettings, IndexShard indexShard, MapperService mapperService, MapperMetrics mapperMetrics) {
super(indexShard.shardId(), indexSettings);
this.mapperService = mapperService;
this.indexShard = indexShard;
this.mapperMetrics = mapperMetrics;
}

public GetStats stats() {
Expand Down Expand Up @@ -303,8 +306,8 @@ private GetResult innerGetFetch(
Map<String, DocumentField> metadataFields = null;
DocIdAndVersion docIdAndVersion = get.docIdAndVersion();
SourceLoader loader = forceSyntheticSource
? new SourceLoader.Synthetic(mappingLookup.getMapping())
: mappingLookup.newSourceLoader();
? new SourceLoader.Synthetic(mappingLookup.getMapping(), mapperMetrics.sourceFieldMetrics())
: mappingLookup.newSourceLoader(mapperMetrics.sourceFieldMetrics());
StoredFieldLoader storedFieldLoader = buildStoredFieldLoader(storedFields, fetchSourceContext, loader);
LeafStoredFieldLoader leafStoredFieldLoader = storedFieldLoader.getLoader(docIdAndVersion.reader.getContext(), null);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class DocumentMapper {
private final CompressedXContent mappingSource;
private final MappingLookup mappingLookup;
private final DocumentParser documentParser;
private final MapperMetrics mapperMetrics;

/**
* Create a new {@link DocumentMapper} that holds empty mappings.
Expand All @@ -32,14 +33,27 @@ public static DocumentMapper createEmpty(MapperService mapperService) {
);
MetadataFieldMapper[] metadata = mapperService.getMetadataMappers().values().toArray(new MetadataFieldMapper[0]);
Mapping mapping = new Mapping(root, metadata, null);
return new DocumentMapper(mapperService.documentParser(), mapping, mapping.toCompressedXContent(), IndexVersion.current());
return new DocumentMapper(
mapperService.documentParser(),
mapping,
mapping.toCompressedXContent(),
IndexVersion.current(),
mapperService.getMapperMetrics()
);
}

DocumentMapper(DocumentParser documentParser, Mapping mapping, CompressedXContent source, IndexVersion version) {
DocumentMapper(
DocumentParser documentParser,
Mapping mapping,
CompressedXContent source,
IndexVersion version,
MapperMetrics mapperMetrics
) {
this.documentParser = documentParser;
this.type = mapping.getRoot().name();
this.mappingLookup = MappingLookup.fromMapping(mapping);
this.mappingSource = source;
this.mapperMetrics = mapperMetrics;

assert mapping.toCompressedXContent().equals(source) || isSyntheticSourceMalformed(source, version)
: "provided source [" + source + "] differs from mapping [" + mapping.toCompressedXContent() + "]";
Expand Down Expand Up @@ -112,7 +126,7 @@ public void validate(IndexSettings settings, boolean checkLimits) {
* Build an empty source loader to validate that the mapping is compatible
* with the source loading strategy declared on the source field mapper.
*/
sourceMapper().newSourceLoader(mapping());
sourceMapper().newSourceLoader(mapping(), mapperMetrics.sourceFieldMetrics());
if (settings.getIndexSortConfig().hasIndexSort() && mappers().nestedLookup() != NestedLookup.EMPTY) {
throw new IllegalArgumentException("cannot have nested fields when index sort is activated");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.index.mapper;

/**
* Groups together all metrics used in mappers.
* Main purpose of this class is to avoid verbosity of passing individual metric instances around.
*/
public record MapperMetrics(SourceFieldMetrics sourceFieldMetrics) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should make this even more generic given that it is now in SearchExecutionContext.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but I'd use a container like SearchExecutionMetrics to the context and include MapperMetrics in it.

public static MapperMetrics NOOP = new MapperMetrics(SourceFieldMetrics.NOOP);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are this NOOP instance and the other one in SourceFieldMetrics only used in non production code? Otherwise maybe we can move it to test framework?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost - it is used in benchmarks too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, maybe then let's keep it here.

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public boolean isAutoUpdate() {
private final IndexVersion indexVersionCreated;
private final MapperRegistry mapperRegistry;
private final Supplier<MappingParserContext> mappingParserContextSupplier;
private final MapperMetrics mapperMetrics;

private volatile DocumentMapper mapper;
private volatile long mappingVersion;
Expand All @@ -165,7 +166,8 @@ public MapperService(
MapperRegistry mapperRegistry,
Supplier<SearchExecutionContext> searchExecutionContextSupplier,
IdFieldMapper idFieldMapper,
ScriptCompiler scriptCompiler
ScriptCompiler scriptCompiler,
MapperMetrics mapperMetrics
) {
this(
() -> clusterService.state().getMinTransportVersion(),
Expand All @@ -176,7 +178,8 @@ public MapperService(
mapperRegistry,
searchExecutionContextSupplier,
idFieldMapper,
scriptCompiler
scriptCompiler,
mapperMetrics
);
}

Expand All @@ -190,7 +193,8 @@ public MapperService(
MapperRegistry mapperRegistry,
Supplier<SearchExecutionContext> searchExecutionContextSupplier,
IdFieldMapper idFieldMapper,
ScriptCompiler scriptCompiler
ScriptCompiler scriptCompiler,
MapperMetrics mapperMetrics
) {
super(indexSettings);
this.indexVersionCreated = indexSettings.getIndexVersionCreated();
Expand Down Expand Up @@ -218,6 +222,7 @@ public MapperService(
this::getMetadataMappers,
this::resolveDocumentType
);
this.mapperMetrics = mapperMetrics;
}

public boolean hasNested() {
Expand Down Expand Up @@ -547,7 +552,7 @@ private synchronized DocumentMapper doMerge(String type, MergeReason reason, Map
}

private DocumentMapper newDocumentMapper(Mapping mapping, MergeReason reason, CompressedXContent mappingSource) {
DocumentMapper newMapper = new DocumentMapper(documentParser, mapping, mappingSource, indexVersionCreated);
DocumentMapper newMapper = new DocumentMapper(documentParser, mapping, mappingSource, indexVersionCreated, mapperMetrics);
newMapper.validate(indexSettings, reason != MergeReason.MAPPING_RECOVERY);
return newMapper;
}
Expand Down Expand Up @@ -780,4 +785,8 @@ public DynamicTemplate[] getAllDynamicTemplates() {
public MapperRegistry getMapperRegistry() {
return mapperRegistry;
}

public MapperMetrics getMapperMetrics() {
return mapperMetrics;
}
}
Loading