diff --git a/CHANGELOG.md b/CHANGELOG.md index a382ae18adc23..06f6323cc3da5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added ### Changed +- Refactor to move prepareIndex and prepareDelete methods to Engine class ([#19551](https://github.com/opensearch-project/OpenSearch/pull/19551)) ### Fixed diff --git a/server/src/main/java/org/opensearch/index/engine/Engine.java b/server/src/main/java/org/opensearch/index/engine/Engine.java index 82d8871b73fba..ebe7791aede8f 100644 --- a/server/src/main/java/org/opensearch/index/engine/Engine.java +++ b/server/src/main/java/org/opensearch/index/engine/Engine.java @@ -79,11 +79,14 @@ import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.core.index.shard.ShardId; import org.opensearch.index.VersionType; +import org.opensearch.index.mapper.DocumentMapperForType; import org.opensearch.index.mapper.IdFieldMapper; import org.opensearch.index.mapper.Mapping; import org.opensearch.index.mapper.ParseContext.Document; import org.opensearch.index.mapper.ParsedDocument; import org.opensearch.index.mapper.SeqNoFieldMapper; +import org.opensearch.index.mapper.SourceToParse; +import org.opensearch.index.mapper.Uid; import org.opensearch.index.merge.MergeStats; import org.opensearch.index.seqno.SeqNoStats; import org.opensearch.index.seqno.SequenceNumbers; @@ -1594,6 +1597,85 @@ public long startTime() { public abstract TYPE operationType(); } + /** + * Prepares an index operation by parsing the source document and creating an Engine.Index operation. + * + * @param docMapper the document mapper instance + * @param source the source to parse + * @param seqNo the sequence number + * @param primaryTerm the primary term + * @param version the version + * @param versionType the version type + * @param origin the operation origin + * @param autoGeneratedIdTimestamp the timestamp for auto-generated IDs + * @param isRetry whether this is a retry + * @param ifSeqNo the ifSeqNo + * @param ifPrimaryTerm the ifPrimaryTerm + * @return the prepared index operation + */ + public Engine.Index prepareIndex( + DocumentMapperForType docMapper, + SourceToParse source, + long seqNo, + long primaryTerm, + long version, + VersionType versionType, + Engine.Operation.Origin origin, + long autoGeneratedIdTimestamp, + boolean isRetry, + long ifSeqNo, + long ifPrimaryTerm + ) { + long startTime = System.nanoTime(); + ParsedDocument doc = docMapper.getDocumentMapper().parse(source); + if (docMapper.getMapping() != null) { + doc.addDynamicMappingsUpdate(docMapper.getMapping()); + } + Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(doc.id())); + return new Engine.Index( + uid, + doc, + seqNo, + primaryTerm, + version, + versionType, + origin, + startTime, + autoGeneratedIdTimestamp, + isRetry, + ifSeqNo, + ifPrimaryTerm + ); + } + + /** + * Prepares a delete operation by creating an Engine.Delete operation. + * + * @param id the document id + * @param seqNo the sequence number + * @param primaryTerm the primary term + * @param version the version + * @param versionType the version type + * @param origin the operation origin + * @param ifSeqNo the ifSeqNo + * @param ifPrimaryTerm the ifPrimaryTerm + * @return the prepared delete operation + */ + public Engine.Delete prepareDelete( + String id, + long seqNo, + long primaryTerm, + long version, + VersionType versionType, + Engine.Operation.Origin origin, + long ifSeqNo, + long ifPrimaryTerm + ) { + long startTime = System.nanoTime(); + final Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(id)); + return new Engine.Delete(id, uid, seqNo, primaryTerm, version, versionType, origin, startTime, ifSeqNo, ifPrimaryTerm); + } + /** * Index operation * diff --git a/server/src/main/java/org/opensearch/index/shard/IndexShard.java b/server/src/main/java/org/opensearch/index/shard/IndexShard.java index 609a6290d36ce..0a365e4d756d8 100644 --- a/server/src/main/java/org/opensearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/opensearch/index/shard/IndexShard.java @@ -1199,7 +1199,7 @@ private Engine.IndexResult applyIndexOperation( ensureWriteAllowed(origin); Engine.Index operation; try { - operation = prepareIndex( + operation = engine.prepareIndex( docMapper(), sourceToParse, seqNo, @@ -1228,6 +1228,12 @@ private Engine.IndexResult applyIndexOperation( return index(engine, operation); } + /** + * Prepares an index operation by parsing the source document and creating an Engine.Index operation. + * + * @deprecated This static method has been moved to {@link Engine#prepareIndex} as an instance method. + */ + @Deprecated(since = "3.4.0", forRemoval = true) public static Engine.Index prepareIndex( DocumentMapperForType docMapper, SourceToParse source, @@ -1418,10 +1424,16 @@ private Engine.DeleteResult applyDeleteOperation( + getOperationPrimaryTerm() + "]"; ensureWriteAllowed(origin); - final Engine.Delete delete = prepareDelete(id, seqNo, opPrimaryTerm, version, versionType, origin, ifSeqNo, ifPrimaryTerm); + final Engine.Delete delete = engine.prepareDelete(id, seqNo, opPrimaryTerm, version, versionType, origin, ifSeqNo, ifPrimaryTerm); return delete(engine, delete); } + /** + * Prepares a delete operation by creating an Engine.Delete operation. + * + * @deprecated This static method has been moved to {@link Engine#prepareDelete} as an instance method. + */ + @Deprecated(since = "3.4.0", forRemoval = true) public static Engine.Delete prepareDelete( String id, long seqNo, diff --git a/test/framework/src/main/java/org/opensearch/index/engine/TranslogHandler.java b/test/framework/src/main/java/org/opensearch/index/engine/TranslogHandler.java index 9e4e59d9a4d15..64ed92fc6c9fb 100644 --- a/test/framework/src/main/java/org/opensearch/index/engine/TranslogHandler.java +++ b/test/framework/src/main/java/org/opensearch/index/engine/TranslogHandler.java @@ -47,7 +47,6 @@ import org.opensearch.index.mapper.RootObjectMapper; import org.opensearch.index.mapper.SourceToParse; import org.opensearch.index.seqno.SequenceNumbers; -import org.opensearch.index.shard.IndexShard; import org.opensearch.index.similarity.SimilarityService; import org.opensearch.index.translog.Translog; import org.opensearch.index.translog.TranslogRecoveryRunner; @@ -135,7 +134,7 @@ public Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.O case INDEX: final Translog.Index index = (Translog.Index) operation; final String indexName = mapperService.index().getName(); - final Engine.Index engineIndex = IndexShard.prepareIndex( + final Engine.Index engineIndex = engine.prepareIndex( docMapper(MapperService.SINGLE_MAPPING_NAME), new SourceToParse( indexName, @@ -157,7 +156,7 @@ public Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.O return engineIndex; case DELETE: final Translog.Delete delete = (Translog.Delete) operation; - return IndexShard.prepareDelete( + return engine.prepareDelete( delete.id(), delete.seqNo(), delete.primaryTerm(),