From b92b9d1c94df208f3fbb707a75b6f7085bf97de1 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Mon, 3 May 2021 09:42:37 +0200 Subject: [PATCH] Replace some DocumentMapper usages with MappingLookup (#72400) We recently replaced some usages of DocumentMapper with MappingLookup in the search layer, as document mapper is mutable which can cause issues. In order to do that, MappingLookup grew and became quite similar to DocumentMapper in what it does and holds. In many cases it makes sense to use MappingLookup instead of DocumentMapper, and we may even be able to remove DocumentMapper entirely in favour of MappingLookup in the long run. This commit replaces some of its straight-forward usages. --- .../action/PainlessExecuteAction.java | 4 +- .../TransportGetFieldMappingsIndexAction.java | 13 ++-- .../MetadataCreateDataStreamService.java | 10 +-- .../metadata/MetadataCreateIndexService.java | 2 +- .../MetadataIndexTemplateService.java | 3 +- .../elasticsearch/index/engine/Engine.java | 4 +- .../index/engine/InternalEngine.java | 10 +-- .../index/engine/ReadOnlyEngine.java | 4 +- .../engine/SingleDocDirectoryReader.java | 14 ++-- .../index/get/ShardGetService.java | 16 ++--- .../elasticsearch/index/mapper/Mapping.java | 6 +- .../index/mapper/MappingLookup.java | 6 +- .../elasticsearch/index/shard/IndexShard.java | 8 +-- .../MetadataRolloverServiceTests.java | 54 ++++++-------- .../index/engine/InternalEngineTests.java | 71 ++++++++++--------- .../index/engine/ReadOnlyEngineTests.java | 2 +- .../index/shard/RefreshListenersTests.java | 5 +- .../index/engine/EngineTestCase.java | 5 +- .../MetadataCreateDataStreamServiceTests.java | 24 +++---- 19 files changed, 125 insertions(+), 136 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java index 8666034992e09..97e7950b0beff 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java @@ -647,7 +647,9 @@ private static Response prepareRamIndex(Request request, BytesReference document = request.contextSetup.document; XContentType xContentType = request.contextSetup.xContentType; SourceToParse sourceToParse = new SourceToParse(index, "_id", document, xContentType); - ParsedDocument parsedDocument = indexService.mapperService().documentMapper().parse(sourceToParse); + //TODO this throws NPE when called against an empty index with no provided mappings: DocumentMapper is null + // and the corresponding empty MappingLookup does not have a DocumentParser set + ParsedDocument parsedDocument = indexService.mapperService().mappingLookup().parseDocument(sourceToParse); indexWriter.addDocuments(parsedDocument.docs()); try (IndexReader indexReader = DirectoryReader.open(indexWriter)) { final IndexSearcher searcher = new IndexSearcher(indexReader); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java index acaca5419e60e..2e893d32d853a 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java @@ -27,7 +27,6 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.shard.ShardId; @@ -84,8 +83,8 @@ protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexReq Predicate metadataFieldPredicate = (f) -> indexService.mapperService().isMetadataField(f); Predicate fieldPredicate = metadataFieldPredicate.or(indicesService.getFieldFilter().apply(shardId.getIndexName())); - DocumentMapper documentMapper = indexService.mapperService().documentMapper(); - Map fieldMapping = findFieldMappings(fieldPredicate, documentMapper, request); + MappingLookup mappingLookup = indexService.mapperService().mappingLookup(); + Map fieldMapping = findFieldMappings(fieldPredicate, mappingLookup, request); return new GetFieldMappingsResponse(singletonMap(shardId.getIndexName(), fieldMapping)); } @@ -137,15 +136,11 @@ public Boolean paramAsBoolean(String key, Boolean defaultValue) { }; private static Map findFieldMappings(Predicate fieldPredicate, - DocumentMapper documentMapper, - GetFieldMappingsIndexRequest request) { - if (documentMapper == null) { - return Collections.emptyMap(); - } + MappingLookup mappingLookup, + GetFieldMappingsIndexRequest request) { //TODO the logic here needs to be reworked to also include runtime fields. Though matching is against mappers rather // than field types, and runtime fields are mixed with ordinary fields in FieldTypeLookup Map fieldMappings = new HashMap<>(); - final MappingLookup mappingLookup = documentMapper.mappers(); for (String field : request.fields()) { if (Regex.isMatchAllPattern(field)) { for (Mapper fieldMapper : mappingLookup.fieldMappers()) { diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateDataStreamService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateDataStreamService.java index e138937fc26fe..85cf15f67cb7d 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateDataStreamService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateDataStreamService.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.ObjectPath; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.MetadataFieldMapper; import org.elasticsearch.indices.SystemDataStreamDescriptor; import org.elasticsearch.rest.RestStatus; @@ -237,13 +238,12 @@ public static ComposableIndexTemplate lookupTemplateForDataStream(String dataStr return composableIndexTemplate; } - public static void validateTimestampFieldMapping(String timestampFieldName, MapperService mapperService) throws IOException { - MetadataFieldMapper fieldMapper = - (MetadataFieldMapper) mapperService.documentMapper().mappers().getMapper("_data_stream_timestamp"); + public static void validateTimestampFieldMapping(MappingLookup mappingLookup) throws IOException { + MetadataFieldMapper fieldMapper = (MetadataFieldMapper) mappingLookup.getMapper("_data_stream_timestamp"); assert fieldMapper != null : "[_data_stream_timestamp] meta field mapper must exist"; Map parsedTemplateMapping = - MapperService.parseMapping(NamedXContentRegistry.EMPTY, mapperService.documentMapper().mappingSource().string()); + MapperService.parseMapping(NamedXContentRegistry.EMPTY, mappingLookup.getMapping().toCompressedXContent().string()); Boolean enabled = ObjectPath.eval("_doc._data_stream_timestamp.enabled", parsedTemplateMapping); // Sanity check: if this fails then somehow the mapping for _data_stream_timestamp has been overwritten and // that would be a bug. @@ -252,7 +252,7 @@ public static void validateTimestampFieldMapping(String timestampFieldName, Mapp } // Sanity check (this validation logic should already have been executed when merging mappings): - fieldMapper.validate(mapperService.documentMapper().mappers()); + fieldMapper.validate(mappingLookup); } } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java index b0c6ab0ebfbe5..7135c65e0d183 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -992,7 +992,7 @@ private static void updateIndexMappingsAndBuildSortOrder(IndexService indexServi indexService.getIndexSortSupplier().get(); } if (request.dataStreamName() != null) { - validateTimestampFieldMapping("@timestamp", mapperService); + validateTimestampFieldMapping(mapperService.mappingLookup()); } } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java index 9aaab63164c8d..f4f2556fd1035 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -1233,8 +1233,7 @@ private static void validateCompositeTemplate(final ClusterState state, } if (template.getDataStreamTemplate() != null) { - String tsFieldName = template.getDataStreamTemplate().getTimestampField(); - validateTimestampFieldMapping(tsFieldName, mapperService); + validateTimestampFieldMapping(mapperService.mappingLookup()); } } catch (Exception e) { throw new IllegalArgumentException("invalid composite mappings for [" + templateName + "]", e); diff --git a/server/src/main/java/org/elasticsearch/index/engine/Engine.java b/server/src/main/java/org/elasticsearch/index/engine/Engine.java index be5c57de7fcda..ec25b4530816d 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/Engine.java +++ b/server/src/main/java/org/elasticsearch/index/engine/Engine.java @@ -46,9 +46,9 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ReleasableLock; import org.elasticsearch.index.VersionType; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.Mapping; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.Uid; @@ -581,7 +581,7 @@ protected final GetResult getFromSearcher(Get get, Engine.Searcher searcher) thr } } - public abstract GetResult get(Get get, DocumentMapper mapper, Function searcherWrapper); + public abstract GetResult get(Get get, MappingLookup mappingLookup, Function searcherWrapper); /** * Acquires a point-in-time reader that can be used to create {@link Engine.Searcher}s on demand. diff --git a/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java b/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java index ba6bc1820f434..7fa5ea768edea 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java +++ b/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java @@ -61,8 +61,8 @@ import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.VersionType; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.IdFieldMapper; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SeqNoFieldMapper; @@ -606,10 +606,10 @@ private ExternalReaderManager createReaderManager(RefreshWarmerListener external } } - private GetResult getFromTranslog(Get get, Translog.Index index, DocumentMapper mapper, + private GetResult getFromTranslog(Get get, Translog.Index index, MappingLookup mappingLookup, Function searcherWrapper) throws IOException { assert get.isReadFromTranslog(); - final SingleDocDirectoryReader inMemoryReader = new SingleDocDirectoryReader(shardId, index, mapper, config().getAnalyzer()); + final SingleDocDirectoryReader inMemoryReader = new SingleDocDirectoryReader(shardId, index, mappingLookup, config().getAnalyzer()); final Engine.Searcher searcher = new Engine.Searcher("realtime_get", ElasticsearchDirectoryReader.wrap(inMemoryReader, shardId), config().getSimilarity(), config().getQueryCache(), config().getQueryCachingPolicy(), inMemoryReader); final Searcher wrappedSearcher = searcherWrapper.apply(searcher); @@ -628,7 +628,7 @@ private GetResult getFromTranslog(Get get, Translog.Index index, DocumentMapper } @Override - public GetResult get(Get get, DocumentMapper mapper, Function searcherWrapper) { + public GetResult get(Get get, MappingLookup mappingLookup, Function searcherWrapper) { assert Objects.equals(get.uid().field(), IdFieldMapper.NAME) : get.uid().field(); try (ReleasableLock ignored = readLock.acquire()) { ensureOpen(); @@ -659,7 +659,7 @@ public GetResult get(Get get, DocumentMapper mapper, Function searcherWrapper) { + public GetResult get(Get get, MappingLookup mappingLookup, Function searcherWrapper) { return getFromSearcher(get, acquireSearcher("get", SearcherScope.EXTERNAL, searcherWrapper)); } diff --git a/server/src/main/java/org/elasticsearch/index/engine/SingleDocDirectoryReader.java b/server/src/main/java/org/elasticsearch/index/engine/SingleDocDirectoryReader.java index 02ef823d79dc7..413fb606ba2bd 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/SingleDocDirectoryReader.java +++ b/server/src/main/java/org/elasticsearch/index/engine/SingleDocDirectoryReader.java @@ -30,7 +30,7 @@ import org.apache.lucene.util.Bits; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.internal.io.IOUtils; -import org.elasticsearch.index.mapper.DocumentMapper; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.shard.ShardId; @@ -47,8 +47,8 @@ final class SingleDocDirectoryReader extends DirectoryReader { private final SingleDocLeafReader leafReader; - SingleDocDirectoryReader(ShardId shardId, Translog.Index operation, DocumentMapper mapper, Analyzer analyzer) throws IOException { - this(new SingleDocLeafReader(shardId, operation, mapper, analyzer)); + SingleDocDirectoryReader(ShardId shardId, Translog.Index operation, MappingLookup mappingLookup, Analyzer analyzer) throws IOException { + this(new SingleDocLeafReader(shardId, operation, mappingLookup, analyzer)); } private SingleDocDirectoryReader(SingleDocLeafReader leafReader) throws IOException { @@ -109,15 +109,15 @@ private static class SingleDocLeafReader extends LeafReader { private final ShardId shardId; private final Translog.Index operation; - private final DocumentMapper mapper; + private final MappingLookup mappingLookup; private final Analyzer analyzer; private final Directory directory; private final AtomicReference delegate = new AtomicReference<>(); - SingleDocLeafReader(ShardId shardId, Translog.Index operation, DocumentMapper mapper, Analyzer analyzer) { + SingleDocLeafReader(ShardId shardId, Translog.Index operation, MappingLookup mappingLookup, Analyzer analyzer) { this.shardId = shardId; this.operation = operation; - this.mapper = mapper; + this.mappingLookup = mappingLookup; this.analyzer = analyzer; this.directory = new ByteBuffersDirectory(); } @@ -140,7 +140,7 @@ private LeafReader getDelegate() { private LeafReader createInMemoryLeafReader() { assert Thread.holdsLock(this); - final ParsedDocument parsedDocs = mapper.parse(new SourceToParse(shardId.getIndexName(), operation.id(), + final ParsedDocument parsedDocs = mappingLookup.parseDocument(new SourceToParse(shardId.getIndexName(), operation.id(), operation.source(), XContentHelper.xContentType(operation.source()), operation.routing(), Map.of())); parsedDocs.updateSeqID(operation.seqNo(), operation.primaryTerm()); diff --git a/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java index c5b61cd0b8b6d..2320809122ad5 100644 --- a/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -34,9 +34,9 @@ import org.elasticsearch.index.engine.TranslogLeafReader; import org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor; import org.elasticsearch.index.fieldvisitor.FieldsVisitor; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.RoutingFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; @@ -122,7 +122,7 @@ public GetResult get(Engine.GetResult engineGetResult, String id, try { long now = System.nanoTime(); fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, fields); - GetResult getResult = innerGetLoadFromStoredFields(id, fields, fetchSourceContext, engineGetResult, mapperService); + GetResult getResult = innerGetLoadFromStoredFields(id, fields, fetchSourceContext, engineGetResult); if (getResult.isExists()) { existsMetric.inc(System.nanoTime() - now); } else { @@ -169,23 +169,23 @@ private GetResult innerGet(String id, String[] gFields, boolean realtime, long v try { // break between having loaded it from translog (so we only have _source), and having a document to load - return innerGetLoadFromStoredFields(id, gFields, fetchSourceContext, get, mapperService); + return innerGetLoadFromStoredFields(id, gFields, fetchSourceContext, get); } finally { get.close(); } } private GetResult innerGetLoadFromStoredFields(String id, String[] storedFields, FetchSourceContext fetchSourceContext, - Engine.GetResult get, MapperService mapperService) { + Engine.GetResult get) { assert get.exists() : "method should only be called if document could be retrieved"; // check first if stored fields to be loaded don't contain an object field - DocumentMapper docMapper = mapperService.documentMapper(); + MappingLookup mappingLookup = mapperService.mappingLookup(); if (storedFields != null) { for (String field : storedFields) { - Mapper fieldMapper = docMapper.mappers().getMapper(field); + Mapper fieldMapper = mappingLookup.getMapper(field); if (fieldMapper == null) { - if (docMapper.mappers().objectMappers().get(field) != null) { + if (mappingLookup.objectMappers().get(field) != null) { // Only fail if we know it is a object field, missing paths / fields shouldn't fail. throw new IllegalArgumentException("field [" + field + "] isn't a leaf field"); } @@ -225,7 +225,7 @@ private GetResult innerGetLoadFromStoredFields(String id, String[] storedFields, assert source != null : "original source in translog must exist"; SourceToParse sourceToParse = new SourceToParse(shardId.getIndexName(), id, source, XContentHelper.xContentType(source), fieldVisitor.routing(), Map.of()); - ParsedDocument doc = indexShard.mapperService().documentMapper().parse(sourceToParse); + ParsedDocument doc = indexShard.mapperService().mappingLookup().parseDocument(sourceToParse); assert doc.dynamicMappingsUpdate() == null : "mapping updates should not be required on already-indexed doc"; // update special fields doc.updateSeqID(docIdAndVersion.seqNo, docIdAndVersion.primaryTerm); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/Mapping.java b/server/src/main/java/org/elasticsearch/index/mapper/Mapping.java index 950dd7ed93838..7e3644bfff723 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/Mapping.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/Mapping.java @@ -69,7 +69,11 @@ public int compare(Mapper o1, Mapper o2) { } - CompressedXContent toCompressedXContent() { + /** + * Outputs this mapping instance and returns it in {@link CompressedXContent} format + * @return the {@link CompressedXContent} representation of this mapping instance + */ + public CompressedXContent toCompressedXContent() { try { return new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS); } catch (Exception e) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java b/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java index 7ba115c19afbc..6ad8798306d18 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java @@ -374,7 +374,11 @@ public CacheKey cacheKey() { return cacheKey; } - Mapping getMapping() { + /** + * Returns the mapping source that this lookup originated from + * @return the mapping source + */ + public Mapping getMapping() { return mapping; } diff --git a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 448d6adea3a47..e7b50db3694ac 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -100,12 +100,12 @@ import org.elasticsearch.index.get.GetStats; import org.elasticsearch.index.get.ShardGetService; import org.elasticsearch.index.mapper.DateFieldMapper; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperForType; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Mapping; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.mapper.Uid; @@ -973,11 +973,11 @@ private Engine.DeleteResult delete(Engine engine, Engine.Delete delete) throws I public Engine.GetResult get(Engine.Get get) { readAllowed(); - DocumentMapper mapper = mapperService.documentMapper(); - if (mapper == null) { + MappingLookup mappingLookup = mapperService.mappingLookup(); + if (mappingLookup.hasMappings() == false) { return GetResult.NOT_EXISTS; } - return getEngine().get(get, mapper, this::wrapSearcher); + return getEngine().get(get, mappingLookup, this::wrapSearcher); } /** diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java index dc877496bd9e9..502eb06714efc 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java @@ -29,13 +29,13 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.MetadataCreateIndexService; import org.elasticsearch.cluster.metadata.MetadataIndexAliasesService; +import org.elasticsearch.cluster.metadata.MetadataIndexTemplateServiceTests; import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.cluster.routing.allocation.AllocationService; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -45,13 +45,12 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.ContentPath; import org.elasticsearch.index.mapper.DateFieldMapper; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Mapping; import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.MetadataFieldMapper; -import org.elasticsearch.index.mapper.RoutingFieldMapper; +import org.elasticsearch.index.mapper.RootObjectMapper; import org.elasticsearch.index.shard.IndexEventListener; import org.elasticsearch.indices.EmptySystemIndices; import org.elasticsearch.indices.IndicesService; @@ -72,7 +71,6 @@ import java.util.Locale; import java.util.Map; -import static org.elasticsearch.cluster.metadata.DataStreamTestHelper.generateMapping; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; @@ -574,26 +572,22 @@ protected String contentType() { return null; } }; - MappingLookup mappingLookup = MappingLookup.fromMappers( - Mapping.EMPTY, - List.of(mockedTimestampField, dateFieldMapper), - List.of(), - List.of()); ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool); Environment env = mock(Environment.class); when(env.sharedDataFile()).thenReturn(null); AllocationService allocationService = mock(AllocationService.class); when(allocationService.reroute(any(ClusterState.class), any(String.class))).then(i -> i.getArguments()[0]); - DocumentMapper documentMapper = mock(DocumentMapper.class); - when(documentMapper.mappers()).thenReturn(mappingLookup); - when(documentMapper.type()).thenReturn("_doc"); - CompressedXContent mapping = - new CompressedXContent("{\"_doc\":" + generateMapping(dataStream.getTimeStampField().getName(), "date") + "}"); - when(documentMapper.mappingSource()).thenReturn(mapping); - RoutingFieldMapper routingFieldMapper = mock(RoutingFieldMapper.class); - when(routingFieldMapper.required()).thenReturn(false); - when(documentMapper.routingFieldMapper()).thenReturn(routingFieldMapper); - IndicesService indicesService = mockIndicesServices(documentMapper); + MetadataFieldMapper[] metadataFieldMappers = {new MetadataIndexTemplateServiceTests.MetadataTimestampFieldMapper(true)}; + RootObjectMapper.Builder root = new RootObjectMapper.Builder("_doc", Version.CURRENT); + root.add(new DateFieldMapper.Builder(dataStream.getTimeStampField().getName(), DateFieldMapper.Resolution.MILLISECONDS, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, ScriptCompiler.NONE, true, Version.CURRENT)); + Mapping mapping = new Mapping(root.build(new ContentPath("")), metadataFieldMappers, Collections.emptyMap()); + MappingLookup mappingLookup = MappingLookup.fromMappers( + mapping, + List.of(mockedTimestampField, dateFieldMapper), + List.of(), + List.of()); + IndicesService indicesService = mockIndicesServices(mappingLookup); IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class); when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg()); @@ -673,8 +667,13 @@ protected String contentType() { return null; } }; + MetadataFieldMapper[] metadataFieldMappers = {new MetadataIndexTemplateServiceTests.MetadataTimestampFieldMapper(true)}; + RootObjectMapper.Builder root = new RootObjectMapper.Builder("_doc", Version.CURRENT); + root.add(new DateFieldMapper.Builder(dataStream.getTimeStampField().getName(), DateFieldMapper.Resolution.MILLISECONDS, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, ScriptCompiler.NONE, true, Version.CURRENT)); + Mapping mapping = new Mapping(root.build(new ContentPath("")), metadataFieldMappers, Collections.emptyMap()); MappingLookup mappingLookup = MappingLookup.fromMappers( - Mapping.EMPTY, + mapping, List.of(mockedTimestampField, dateFieldMapper), List.of(), List.of()); @@ -683,16 +682,7 @@ protected String contentType() { when(env.sharedDataFile()).thenReturn(null); AllocationService allocationService = mock(AllocationService.class); when(allocationService.reroute(any(ClusterState.class), any(String.class))).then(i -> i.getArguments()[0]); - DocumentMapper documentMapper = mock(DocumentMapper.class); - when(documentMapper.mappers()).thenReturn(mappingLookup); - when(documentMapper.type()).thenReturn("_doc"); - CompressedXContent mapping = - new CompressedXContent("{\"_doc\":" + generateMapping(dataStream.getTimeStampField().getName(), "date") + "}"); - when(documentMapper.mappingSource()).thenReturn(mapping); - RoutingFieldMapper routingFieldMapper = mock(RoutingFieldMapper.class); - when(routingFieldMapper.required()).thenReturn(false); - when(documentMapper.routingFieldMapper()).thenReturn(routingFieldMapper); - IndicesService indicesService = mockIndicesServices(documentMapper); + IndicesService indicesService = mockIndicesServices(mappingLookup); IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class); when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg()); @@ -823,7 +813,7 @@ private IndicesService mockIndicesServices() throws Exception { return mockIndicesServices(null); } - private IndicesService mockIndicesServices(DocumentMapper documentMapper) throws Exception { + private IndicesService mockIndicesServices(MappingLookup mappingLookup) throws Exception { /* * Throws Exception because Eclipse uses the lower bound for * CheckedFunction's exception type so it thinks the "when" call @@ -838,7 +828,7 @@ private IndicesService mockIndicesServices(DocumentMapper documentMapper) throws when(indexService.index()).thenReturn(indexMetadata.getIndex()); MapperService mapperService = mock(MapperService.class); when(indexService.mapperService()).thenReturn(mapperService); - when(mapperService.documentMapper()).thenReturn(documentMapper); + when(mapperService.mappingLookup()).thenReturn(mappingLookup); when(indexService.getIndexEventListener()).thenReturn(new IndexEventListener() {}); when(indexService.getIndexSortSupplier()).thenReturn(() -> null); //noinspection unchecked diff --git a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index f3d4a809e835f..382add29211ce 100644 --- a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -100,8 +100,8 @@ import org.elasticsearch.index.VersionType; import org.elasticsearch.index.codec.CodecService; import org.elasticsearch.index.fieldvisitor.FieldsVisitor; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.IdFieldMapper; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParsedDocument; @@ -697,7 +697,7 @@ public void testConcurrentGetAndFlush() throws Exception { engine.index(indexForDoc(doc)); final AtomicReference latestGetResult = new AtomicReference<>(); - latestGetResult.set(engine.get(newGet(true, doc), docMapper(), randomSearcherWrapper())); + latestGetResult.set(engine.get(newGet(true, doc), mappingLookup(), randomSearcherWrapper())); final AtomicBoolean flushFinished = new AtomicBoolean(false); final CyclicBarrier barrier = new CyclicBarrier(2); Thread getThread = new Thread(() -> { @@ -711,7 +711,7 @@ public void testConcurrentGetAndFlush() throws Exception { if (previousGetResult != null) { previousGetResult.close(); } - latestGetResult.set(engine.get(newGet(true, doc), docMapper(), randomSearcherWrapper())); + latestGetResult.set(engine.get(newGet(true, doc), mappingLookup(), randomSearcherWrapper())); if (latestGetResult.get().exists() == false) { break; } @@ -727,7 +727,7 @@ public void testConcurrentGetAndFlush() throws Exception { } public void testSimpleOperations() throws Exception { - final DocumentMapper mapper = docMapper(); + final MappingLookup mappingLookup = mappingLookup(); engine.refresh("warm_up"); Engine.Searcher searchResult = engine.acquireSearcher("test"); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); @@ -749,18 +749,18 @@ public void testSimpleOperations() throws Exception { searchResult.close(); // but, not there non realtime - try (Engine.GetResult getResult = engine.get(newGet(false, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(false, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(false)); } // but, we can still get it (in realtime) - try (Engine.GetResult getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.docIdAndVersion(), notNullValue()); } // but not real time is not yet visible - try (Engine.GetResult getResult = engine.get(newGet(false, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(false, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(false)); } @@ -775,7 +775,7 @@ public void testSimpleOperations() throws Exception { searchResult.close(); // also in non realtime - try (Engine.GetResult getResult = engine.get(newGet(false, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(false, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.docIdAndVersion(), notNullValue()); } @@ -797,7 +797,7 @@ public void testSimpleOperations() throws Exception { searchResult.close(); // but, we can still get it (in realtime) - try (Engine.GetResult getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.docIdAndVersion(), notNullValue()); } @@ -826,7 +826,7 @@ public void testSimpleOperations() throws Exception { searchResult.close(); // but, get should not see it (in realtime) - try (Engine.GetResult getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(false)); } @@ -872,7 +872,7 @@ public void testSimpleOperations() throws Exception { engine.flush(); // and, verify get (in real time) - try (Engine.GetResult getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.docIdAndVersion(), notNullValue()); } @@ -909,7 +909,7 @@ public void testGetWithSearcherWrapper() throws Exception { engine.refresh("warm_up"); engine.index(indexForDoc(createParsedDoc("1", null))); assertThat(engine.lastRefreshedCheckpoint(), equalTo(NO_OPS_PERFORMED)); - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), randomSearcherWrapper())) { // we do not track the translog location yet assertTrue(get.exists()); assertFalse(get.isFromTranslog()); @@ -918,7 +918,7 @@ public void testGetWithSearcherWrapper() throws Exception { assertThat(engine.lastRefreshedCheckpoint(), equalTo(0L)); engine.index(indexForDoc(createParsedDoc("1", null))); - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), searcher -> searcher)) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), searcher -> searcher)) { assertTrue(get.exists()); assertTrue(get.isFromTranslog()); } @@ -926,25 +926,25 @@ public void testGetWithSearcherWrapper() throws Exception { if (randomBoolean()) { engine.index(indexForDoc(createParsedDoc("1", null))); } - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), searcher -> SearcherHelper.wrapSearcher(searcher, reader -> new MatchingDirectoryReader(reader, new MatchAllDocsQuery())))) { assertTrue(get.exists()); assertFalse(get.isFromTranslog()); } - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), searcher -> SearcherHelper.wrapSearcher(searcher, reader -> new MatchingDirectoryReader(reader, new MatchNoDocsQuery())))) { assertFalse(get.exists()); assertFalse(get.isFromTranslog()); } - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), searcher -> SearcherHelper.wrapSearcher(searcher, reader -> new MatchingDirectoryReader(reader, new TermQuery(newUid("1")))))) { assertTrue(get.exists()); assertFalse(get.isFromTranslog()); } - try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), docMapper(), + try (Engine.GetResult get = engine.get(new Engine.Get(true, true, "1"), mappingLookup(), searcher -> SearcherHelper.wrapSearcher(searcher, reader -> new MatchingDirectoryReader(reader, new TermQuery(newUid("2")))))) { assertFalse(get.exists()); assertFalse(get.isFromTranslog()); @@ -1205,7 +1205,7 @@ public void testVersionedUpdate() throws IOException { Engine.Index create = new Engine.Index(newUid(doc), primaryTerm.get(), doc, Versions.MATCH_DELETED); Engine.IndexResult indexResult = engine.index(create); assertThat(indexResult.getVersion(), equalTo(1L)); - try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), mappingLookup(), randomSearcherWrapper())) { assertEquals(1, get.version()); } @@ -1213,7 +1213,7 @@ public void testVersionedUpdate() throws IOException { Engine.IndexResult update_1_result = engine.index(update_1); assertThat(update_1_result.getVersion(), equalTo(2L)); - try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), mappingLookup(), randomSearcherWrapper())) { assertEquals(2, get.version()); } @@ -1221,7 +1221,7 @@ public void testVersionedUpdate() throws IOException { Engine.IndexResult update_2_result = engine.index(update_2); assertThat(update_2_result.getVersion(), equalTo(3L)); - try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), mappingLookup(), randomSearcherWrapper())) { assertEquals(3, get.version()); } @@ -1241,22 +1241,22 @@ public void testGetIfSeqNoIfPrimaryTerm() throws IOException { try (Engine.GetResult get = engine.get( new Engine.Get(true, true, doc.id()) .setIfSeqNo(indexResult.getSeqNo()).setIfPrimaryTerm(primaryTerm.get()), - docMapper(), randomSearcherWrapper())) { + mappingLookup(), randomSearcherWrapper())) { assertEquals(indexResult.getSeqNo(), get.docIdAndVersion().seqNo); } expectThrows(VersionConflictEngineException.class, () -> engine.get(new Engine.Get(true, false, doc.id()) .setIfSeqNo(indexResult.getSeqNo() + 1).setIfPrimaryTerm(primaryTerm.get()), - docMapper(), randomSearcherWrapper())); + mappingLookup(), randomSearcherWrapper())); expectThrows(VersionConflictEngineException.class, () -> engine.get(new Engine.Get(true, false, doc.id()) .setIfSeqNo(indexResult.getSeqNo()).setIfPrimaryTerm(primaryTerm.get() + 1), - docMapper(), randomSearcherWrapper())); + mappingLookup(), randomSearcherWrapper())); final VersionConflictEngineException versionConflictEngineException = expectThrows(VersionConflictEngineException.class, () -> engine.get(new Engine.Get(true, false, doc.id()) .setIfSeqNo(indexResult.getSeqNo() + 1).setIfPrimaryTerm(primaryTerm.get() + 1), - docMapper(), randomSearcherWrapper())); + mappingLookup(), randomSearcherWrapper())); assertThat(versionConflictEngineException.getStackTrace(), emptyArray()); } @@ -2008,7 +2008,8 @@ class OpAndVersion { throw new AssertionError(e); } for (int op = 0; op < opsPerThread; op++) { - try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), randomSearcherWrapper())) { + Engine.Get engineGet = new Engine.Get(true, false, doc.id()); + try (Engine.GetResult get = engine.get(engineGet, mappingLookup(), randomSearcherWrapper())) { FieldsVisitor visitor = new FieldsVisitor(true); get.docIdAndVersion().reader.document(get.docIdAndVersion().docId, visitor); List values = new ArrayList<>(Strings.commaDelimitedListToSet(visitor.source().utf8ToString())); @@ -2050,7 +2051,7 @@ class OpAndVersion { assertTrue(op.added + " should not exist", exists); } - try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult get = engine.get(new Engine.Get(true, false, doc.id()), mappingLookup(), randomSearcherWrapper())) { FieldsVisitor visitor = new FieldsVisitor(true); get.docIdAndVersion().reader.document(get.docIdAndVersion().docId, visitor); List values = Arrays.asList(Strings.commaDelimitedListToStringArray(visitor.source().utf8ToString())); @@ -2446,7 +2447,7 @@ public void testEnableGcDeletes() throws Exception { Engine engine = createEngine(config(defaultSettings, store, createTempDir(), newMergePolicy(), null))) { engine.config().setEnableGcDeletes(false); - final DocumentMapper mapper = docMapper(); + final MappingLookup mappingLookup = mappingLookup(); // Add document Document document = testDocument(); @@ -2462,7 +2463,7 @@ public void testEnableGcDeletes() throws Exception { 10, VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime(), UNASSIGNED_SEQ_NO, 0)); // Get should not find the document - Engine.GetResult getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper()); + Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper()); assertThat(getResult.exists(), equalTo(false)); // Give the gc pruning logic a chance to kick in @@ -2477,7 +2478,7 @@ public void testEnableGcDeletes() throws Exception { 10, VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime(), UNASSIGNED_SEQ_NO, 0)); // Get should not find the document (we never indexed uid=2): - getResult = engine.get(new Engine.Get(true, false, "2"), mapper, randomSearcherWrapper()); + getResult = engine.get(new Engine.Get(true, false, "2"), mappingLookup, randomSearcherWrapper()); assertThat(getResult.exists(), equalTo(false)); // Try to index uid=1 with a too-old version, should fail: @@ -2488,7 +2489,7 @@ public void testEnableGcDeletes() throws Exception { assertThat(indexResult.getFailure(), instanceOf(VersionConflictEngineException.class)); // Get should still not find the document - getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper()); + getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper()); assertThat(getResult.exists(), equalTo(false)); // Try to index uid=2 with a too-old version, should fail: @@ -2499,7 +2500,7 @@ public void testEnableGcDeletes() throws Exception { assertThat(indexResult.getFailure(), instanceOf(VersionConflictEngineException.class)); // Get should not find the document - getResult = engine.get(newGet(true, doc), mapper, randomSearcherWrapper()); + getResult = engine.get(newGet(true, doc), mappingLookup, randomSearcherWrapper()); assertThat(getResult.exists(), equalTo(false)); } } @@ -4013,7 +4014,7 @@ public void testOutOfOrderSequenceNumbersWithVersionConflict() throws IOExceptio } assertThat(engine.getProcessedLocalCheckpoint(), equalTo(expectedLocalCheckpoint)); - try (Engine.GetResult result = engine.get(new Engine.Get(true, false, "1"), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult result = engine.get(new Engine.Get(true, false, "1"), mappingLookup(), randomSearcherWrapper())) { assertThat(result.exists(), equalTo(exists)); } } @@ -4945,13 +4946,13 @@ public void testStressUpdateSameDocWhileGettingIt() throws IOException, Interrup Thread thread = new Thread(() -> { awaitStarted.countDown(); try (Engine.GetResult getResult = engine.get( - new Engine.Get(true, false, doc3.id()), docMapper(), searcher -> searcher)) { + new Engine.Get(true, false, doc3.id()), mappingLookup(), searcher -> searcher)) { assertTrue(getResult.exists()); } }); thread.start(); awaitStarted.await(); - try (Engine.GetResult getResult = engine.get(new Engine.Get(true, false, doc.id()), docMapper(), + try (Engine.GetResult getResult = engine.get(new Engine.Get(true, false, doc.id()), mappingLookup(), searcher -> SearcherHelper.wrapSearcher(searcher, r -> new MatchingDirectoryReader(r, new MatchAllDocsQuery())))) { assertFalse(getResult.exists()); } @@ -5899,7 +5900,7 @@ public void afterRefresh(boolean didRefresh) { int iters = randomIntBetween(1, 10); for (int i = 0; i < iters; i++) { ParsedDocument doc = createParsedDoc(randomFrom(ids), null); - try (Engine.GetResult getResult = engine.get(newGet(true, doc), docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(newGet(true, doc), mappingLookup(), randomSearcherWrapper())) { assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.docIdAndVersion(), notNullValue()); } diff --git a/server/src/test/java/org/elasticsearch/index/engine/ReadOnlyEngineTests.java b/server/src/test/java/org/elasticsearch/index/engine/ReadOnlyEngineTests.java index 38e3f30b05404..29bbe7499f7af 100644 --- a/server/src/test/java/org/elasticsearch/index/engine/ReadOnlyEngineTests.java +++ b/server/src/test/java/org/elasticsearch/index/engine/ReadOnlyEngineTests.java @@ -98,7 +98,7 @@ public void testReadOnlyEngine() throws Exception { assertThat(readOnlyEngine.getPersistedLocalCheckpoint(), equalTo(lastSeqNoStats.getLocalCheckpoint())); assertThat(readOnlyEngine.getSeqNoStats(globalCheckpoint.get()).getMaxSeqNo(), equalTo(lastSeqNoStats.getMaxSeqNo())); assertThat(getDocIds(readOnlyEngine, false), equalTo(lastDocIds)); - try (Engine.GetResult getResult = readOnlyEngine.get(get, docMapper(), randomSearcherWrapper())) { + try (Engine.GetResult getResult = readOnlyEngine.get(get, mappingLookup(), randomSearcherWrapper())) { assertTrue(getResult.exists()); } } diff --git a/server/src/test/java/org/elasticsearch/index/shard/RefreshListenersTests.java b/server/src/test/java/org/elasticsearch/index/shard/RefreshListenersTests.java index 1773ada48b0f8..756747b22a81d 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/RefreshListenersTests.java +++ b/server/src/test/java/org/elasticsearch/index/shard/RefreshListenersTests.java @@ -36,7 +36,6 @@ import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.index.engine.EngineTestCase; import org.elasticsearch.index.engine.InternalEngine; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParsedDocument; @@ -335,8 +334,8 @@ public void testLotsOfThreads() throws Exception { listener.assertNoError(); Engine.Get get = new Engine.Get(false, false, threadId); - final DocumentMapper mapper = EngineTestCase.docMapper(); - try (Engine.GetResult getResult = engine.get(get, mapper, EngineTestCase.randomSearcherWrapper())) { + try (Engine.GetResult getResult = engine.get(get, EngineTestCase.mappingLookup(), + EngineTestCase.randomSearcherWrapper())) { assertTrue("document not found", getResult.exists()); assertEquals(iteration, getResult.version()); org.apache.lucene.document.Document document = diff --git a/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java index b24781140d80b..b519a362c8ece 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java @@ -82,6 +82,7 @@ import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Mapping; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SeqNoFieldMapper; @@ -1166,9 +1167,9 @@ public static MapperService createMapperService() throws IOException { return mapperService; } - public static DocumentMapper docMapper() { + public static MappingLookup mappingLookup() { try { - return createMapperService().documentMapper(); + return createMapperService().mappingLookup(); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java index b817e6eff8f1f..f1d2a0bdaa4f5 100644 --- a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java +++ b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.MapperTestUtils; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.datastreams.DataStreamsPlugin; @@ -27,16 +28,13 @@ public class MetadataCreateDataStreamServiceTests extends ESTestCase { public void testValidateTimestampFieldMapping() throws Exception { String mapping = generateMapping("@timestamp", "date"); - validateTimestampFieldMapping("@timestamp", createMapperService(mapping)); + validateTimestampFieldMapping(createMappingLookup(mapping)); mapping = generateMapping("@timestamp", "date_nanos"); - validateTimestampFieldMapping("@timestamp", createMapperService(mapping)); + validateTimestampFieldMapping(createMappingLookup(mapping)); } public void testValidateTimestampFieldMappingNoFieldMapping() { - Exception e = expectThrows( - IllegalStateException.class, - () -> validateTimestampFieldMapping("@timestamp", createMapperService("{}")) - ); + Exception e = expectThrows(IllegalStateException.class, () -> validateTimestampFieldMapping(createMappingLookup("{}"))); assertThat(e.getMessage(), equalTo("[_data_stream_timestamp] meta field has been disabled")); String mapping1 = "{\n" + " \"_data_stream_timestamp\": {\n" @@ -48,27 +46,24 @@ public void testValidateTimestampFieldMappingNoFieldMapping() { + " }\n" + " }\n" + " }"; - e = expectThrows(IllegalStateException.class, () -> validateTimestampFieldMapping("@timestamp", createMapperService(mapping1))); + e = expectThrows(IllegalStateException.class, () -> validateTimestampFieldMapping(createMappingLookup(mapping1))); assertThat(e.getMessage(), equalTo("[_data_stream_timestamp] meta field has been disabled")); String mapping2 = generateMapping("@timestamp2", "date"); - e = expectThrows(IllegalArgumentException.class, () -> validateTimestampFieldMapping("@timestamp", createMapperService(mapping2))); + e = expectThrows(IllegalArgumentException.class, () -> validateTimestampFieldMapping(createMappingLookup(mapping2))); assertThat(e.getMessage(), equalTo("data stream timestamp field [@timestamp] does not exist")); } public void testValidateTimestampFieldMappingInvalidFieldType() { String mapping = generateMapping("@timestamp", "keyword"); - Exception e = expectThrows( - IllegalArgumentException.class, - () -> validateTimestampFieldMapping("@timestamp", createMapperService(mapping)) - ); + Exception e = expectThrows(IllegalArgumentException.class, () -> validateTimestampFieldMapping(createMappingLookup(mapping))); assertThat( e.getMessage(), equalTo("data stream timestamp field [@timestamp] is of type [keyword], " + "but [date,date_nanos] is expected") ); } - MapperService createMapperService(String mapping) throws IOException { + MappingLookup createMappingLookup(String mapping) throws IOException { String indexName = "test"; IndexMetadata indexMetadata = IndexMetadata.builder(indexName) .settings( @@ -88,7 +83,6 @@ MapperService createMapperService(String mapping) throws IOException { indexName ); mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_UPDATE); - return mapperService; + return mapperService.mappingLookup(); } - }