From bcb3fab6ac9ef52968f8670a4a55183476663590 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Fri, 2 Oct 2015 14:08:10 +0200 Subject: [PATCH] Engine: Remove Engine.Create The `_create` API is handy way to specify an index operation should only be done if the document doesn't exist. This is currently implemented in explicit code paths all the way down to the engine. However, conceptually this is no different than any other versioned operation - instead of requiring a document is on a specific version, we require it to be deleted (or non-existent). This PR removes Engine.Create in favor of a slight extension in the VersionType logic. There are however a couple of side effects: - DocumentAlreadyExistsException is removed and VersionConflictException is used instead (with an improved error message) - Update will reject version parameters if the upsert option is used (it doesn't compute anyway). - Translog.Create is also removed infavor of Translog.Index (that's OK because their binary format was the same, so we can just read Translog.Index of the translog file) Closes #13955 --- .../elasticsearch/ElasticsearchException.java | 4 +- .../action/bulk/TransportShardBulkAction.java | 18 +- .../action/index/IndexRequest.java | 22 +- .../action/index/TransportIndexAction.java | 13 +- .../TransportReplicationAction.java | 17 +- .../action/update/TransportUpdateAction.java | 11 +- .../action/index/MappingUpdatedAction.java | 2 +- .../common/lucene/uid/Versions.java | 16 +- .../org/elasticsearch/index/VersionType.java | 97 +++- .../engine/CreateFailedEngineException.java | 66 --- .../DocumentAlreadyExistsException.java | 44 -- .../elasticsearch/index/engine/Engine.java | 287 ++++-------- .../index/engine/EngineConfig.java | 6 +- .../index/engine/EngineException.java | 10 +- .../index/engine/InternalEngine.java | 219 +++------ .../index/engine/ShadowEngine.java | 5 - .../VersionConflictEngineException.java | 12 +- .../indexing/IndexingOperationListener.java | 35 +- .../index/indexing/IndexingSlowLog.java | 4 - .../index/indexing/ShardIndexingService.java | 54 +-- .../percolator/PercolatorQueriesRegistry.java | 25 +- .../elasticsearch/index/shard/IndexShard.java | 39 +- .../shard/TranslogRecoveryPerformer.java | 14 +- .../index/translog/Translog.java | 212 +-------- .../ExceptionSerializationTests.java | 53 +-- .../action/index/IndexRequestTests.java | 28 +- .../org/elasticsearch/get/GetActionIT.java | 23 +- .../elasticsearch/index/VersionTypeTests.java | 77 +-- .../index/engine/InternalEngineTests.java | 119 +++-- .../index/engine/ShadowEngineTests.java | 32 +- .../index/shard/IndexShardTests.java | 11 +- .../index/translog/TranslogTests.java | 93 ++-- .../index/translog/TranslogVersionTests.java | 6 +- .../versioning/SimpleVersioningIT.java | 438 ++++++++---------- docs/reference/migration/migrate_3_0.asciidoc | 5 + .../elasticsearch/messy/tests/BulkTests.java | 18 +- .../messy/tests/UpdateTests.java | 35 +- .../test/create/35_external_version.yaml | 33 -- .../test/create/36_external_gte_version.yaml | 33 -- .../test/create/37_force_version.yaml | 33 -- .../test/update/30_internal_version.yaml | 14 +- 41 files changed, 772 insertions(+), 1511 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/index/engine/CreateFailedEngineException.java delete mode 100644 core/src/main/java/org/elasticsearch/index/engine/DocumentAlreadyExistsException.java delete mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/create/35_external_version.yaml delete mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/create/36_external_gte_version.yaml delete mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/create/37_force_version.yaml diff --git a/core/src/main/java/org/elasticsearch/ElasticsearchException.java b/core/src/main/java/org/elasticsearch/ElasticsearchException.java index 87348f9dea5..4c82c280adc 100644 --- a/core/src/main/java/org/elasticsearch/ElasticsearchException.java +++ b/core/src/main/java/org/elasticsearch/ElasticsearchException.java @@ -482,7 +482,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte RESOURCE_NOT_FOUND_EXCEPTION(org.elasticsearch.ResourceNotFoundException.class, org.elasticsearch.ResourceNotFoundException::new, 19), ACTION_TRANSPORT_EXCEPTION(org.elasticsearch.transport.ActionTransportException.class, org.elasticsearch.transport.ActionTransportException::new, 20), ELASTICSEARCH_GENERATION_EXCEPTION(org.elasticsearch.ElasticsearchGenerationException.class, org.elasticsearch.ElasticsearchGenerationException::new, 21), - CREATE_FAILED_ENGINE_EXCEPTION(org.elasticsearch.index.engine.CreateFailedEngineException.class, org.elasticsearch.index.engine.CreateFailedEngineException::new, 22), + // 22 was CreateFailedEngineException INDEX_SHARD_STARTED_EXCEPTION(org.elasticsearch.index.shard.IndexShardStartedException.class, org.elasticsearch.index.shard.IndexShardStartedException::new, 23), SEARCH_CONTEXT_MISSING_EXCEPTION(org.elasticsearch.search.SearchContextMissingException.class, org.elasticsearch.search.SearchContextMissingException::new, 24), SCRIPT_EXCEPTION(org.elasticsearch.script.ScriptException.class, org.elasticsearch.script.ScriptException::new, 25), @@ -514,7 +514,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte INDEX_SHARD_ALREADY_EXISTS_EXCEPTION(org.elasticsearch.index.IndexShardAlreadyExistsException.class, org.elasticsearch.index.IndexShardAlreadyExistsException::new, 51), VERSION_CONFLICT_ENGINE_EXCEPTION(org.elasticsearch.index.engine.VersionConflictEngineException.class, org.elasticsearch.index.engine.VersionConflictEngineException::new, 52), ENGINE_EXCEPTION(org.elasticsearch.index.engine.EngineException.class, org.elasticsearch.index.engine.EngineException::new, 53), - DOCUMENT_ALREADY_EXISTS_EXCEPTION(org.elasticsearch.index.engine.DocumentAlreadyExistsException.class, org.elasticsearch.index.engine.DocumentAlreadyExistsException::new, 54), + // 54 was DocumentAlreadyExistsException, which is superseded by VersionConflictEngineException NO_SUCH_NODE_EXCEPTION(org.elasticsearch.action.NoSuchNodeException.class, org.elasticsearch.action.NoSuchNodeException::new, 55), SETTINGS_EXCEPTION(org.elasticsearch.common.settings.SettingsException.class, org.elasticsearch.common.settings.SettingsException::new, 56), INDEX_TEMPLATE_MISSING_EXCEPTION(org.elasticsearch.indices.IndexTemplateMissingException.class, org.elasticsearch.indices.IndexTemplateMissingException::new, 57), diff --git a/core/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java b/core/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java index c0718859bb3..0f00b87b12a 100644 --- a/core/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java +++ b/core/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java @@ -47,7 +47,6 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.VersionType; -import org.elasticsearch.index.engine.DocumentAlreadyExistsException; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.mapper.Mapping; @@ -97,6 +96,7 @@ public class TransportShardBulkAction extends TransportReplicationAction + * * The index requires the {@link #index()}, {@link #type(String)}, {@link #id(String)} and * {@link #source(byte[])} to be set. - *

+ * * The source (content to index) can be set in its bytes form using ({@link #source(byte[])}), * its string form ({@link #source(String)}) or using a {@link org.elasticsearch.common.xcontent.XContentBuilder} * ({@link #source(org.elasticsearch.common.xcontent.XContentBuilder)}). - *

+ * * If the {@link #id(String)} is not set, it will be automatically generated. * * @see IndexResponse @@ -114,7 +114,7 @@ public class IndexRequest extends ReplicationRequest implements Do public static OpType fromString(String sOpType) { String lowersOpType = sOpType.toLowerCase(Locale.ROOT); - switch(lowersOpType){ + switch (lowersOpType) { case "create": return OpType.CREATE; case "index": @@ -216,6 +216,14 @@ public class IndexRequest extends ReplicationRequest implements Do if (source == null) { validationException = addValidationError("source is missing", validationException); } + + if (opType() == OpType.CREATE) { + if (versionType != VersionType.INTERNAL || version != Versions.MATCH_DELETED) { + validationException = addValidationError("create operations do not support versioning. use index instead", validationException); + return validationException; + } + } + if (!versionType.validateVersionForWrites(version)) { validationException = addValidationError("illegal version value [" + version + "] for version type [" + versionType.name() + "]", validationException); } @@ -370,7 +378,7 @@ public class IndexRequest extends ReplicationRequest implements Do /** * Sets the document source to index. - *

+ * * Note, its preferable to either set it using {@link #source(org.elasticsearch.common.xcontent.XContentBuilder)} * or using the {@link #source(byte[])}. */ @@ -480,6 +488,10 @@ public class IndexRequest extends ReplicationRequest implements Do */ public IndexRequest opType(OpType opType) { this.opType = opType; + if (opType == OpType.CREATE) { + version(Versions.MATCH_DELETED); + versionType(VersionType.INTERNAL); + } return this; } diff --git a/core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java b/core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java index 3e98f1a32c2..63b82377d8a 100644 --- a/core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java +++ b/core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java @@ -54,7 +54,7 @@ import org.elasticsearch.transport.TransportService; /** * Performs the index operation. - *

+ * * Allows for the following settings: *