Don't lookup version for auto generated id and create

When a create document is executed, and its an auto generated id (based on UUID), we know that the document will not exists in the index, so there is no need to try and lookup the version from the index.
For many cases, like logging, where ids are auto generated, this can improve the indexing performance, specifically for lightweight documents where analysis is not a big part of the execution.
This commit is contained in:
Shay Banon 2014-04-12 12:10:56 +02:00
parent f8537183b9
commit dc73498454
6 changed files with 45 additions and 12 deletions

View File

@ -424,7 +424,8 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation
op = index; op = index;
created = index.created(); created = index.created();
} else { } else {
Engine.Create create = indexShard.prepareCreate(sourceToParse).version(indexRequest.version()).versionType(indexRequest.versionType()).origin(Engine.Operation.Origin.PRIMARY); Engine.Create create = indexShard.prepareCreate(sourceToParse).version(indexRequest.version()).versionType(indexRequest.versionType()).origin(Engine.Operation.Origin.PRIMARY)
.autoGeneratedId(indexRequest.autoGeneratedId());
if (create.parsedDoc().mappingsModified()) { if (create.parsedDoc().mappingsModified()) {
mappingsToUpdate = Tuple.tuple(indexRequest.index(), indexRequest.type()); mappingsToUpdate = Tuple.tuple(indexRequest.index(), indexRequest.type());
} }
@ -573,6 +574,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation
} else { } else {
Engine.Create create = indexShard.prepareCreate(sourceToParse) Engine.Create create = indexShard.prepareCreate(sourceToParse)
.version(indexRequest.version()).versionType(indexRequest.versionType()) .version(indexRequest.version()).versionType(indexRequest.versionType())
.autoGeneratedId(indexRequest.autoGeneratedId())
.origin(Engine.Operation.Origin.REPLICA); .origin(Engine.Operation.Origin.REPLICA);
indexShard.create(create); indexShard.create(create);
} }

View File

@ -20,10 +20,7 @@
package org.elasticsearch.action.index; package org.elasticsearch.action.index;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.*;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.RoutingMissingException; import org.elasticsearch.action.RoutingMissingException;
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest; import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
@ -128,6 +125,7 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
private boolean sourceUnsafe; private boolean sourceUnsafe;
private OpType opType = OpType.INDEX; private OpType opType = OpType.INDEX;
private boolean autoGeneratedId = false;
private boolean refresh = false; private boolean refresh = false;
private long version = Versions.MATCH_ANY; private long version = Versions.MATCH_ANY;
@ -541,6 +539,13 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
return this.versionType; return this.versionType;
} }
/**
* Has the id been auto generated?
*/
public boolean autoGeneratedId() {
return this.autoGeneratedId;
}
public void process(MetaData metaData, String aliasOrIndex, @Nullable MappingMetaData mappingMd, boolean allowIdGeneration) throws ElasticsearchException { public void process(MetaData metaData, String aliasOrIndex, @Nullable MappingMetaData mappingMd, boolean allowIdGeneration) throws ElasticsearchException {
// resolve the routing if needed // resolve the routing if needed
routing(metaData.resolveIndexRouting(routing, aliasOrIndex)); routing(metaData.resolveIndexRouting(routing, aliasOrIndex));
@ -597,6 +602,7 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
id(Strings.randomBase64UUID()); id(Strings.randomBase64UUID());
// since we generate the id, change it to CREATE // since we generate the id, change it to CREATE
opType(IndexRequest.OpType.CREATE); opType(IndexRequest.OpType.CREATE);
autoGeneratedId = true;
} }
} }
@ -622,6 +628,9 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
refresh = in.readBoolean(); refresh = in.readBoolean();
version = in.readLong(); version = in.readLong();
versionType = VersionType.fromValue(in.readByte()); versionType = VersionType.fromValue(in.readByte());
if (in.getVersion().onOrAfter(Version.V_1_2_0)) {
autoGeneratedId = in.readBoolean();
}
} }
@Override @Override
@ -638,6 +647,9 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
out.writeBoolean(refresh); out.writeBoolean(refresh);
out.writeLong(version); out.writeLong(version);
out.writeByte(versionType.getValue()); out.writeByte(versionType.getValue());
if (out.getVersion().onOrAfter(Version.V_1_2_0)) {
out.writeBoolean(autoGeneratedId);
}
} }
@Override @Override

View File

@ -215,6 +215,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
Engine.Create create = indexShard.prepareCreate(sourceToParse) Engine.Create create = indexShard.prepareCreate(sourceToParse)
.version(request.version()) .version(request.version())
.versionType(request.versionType()) .versionType(request.versionType())
.autoGeneratedId(request.autoGeneratedId())
.origin(Engine.Operation.Origin.PRIMARY); .origin(Engine.Operation.Origin.PRIMARY);
if (create.parsedDoc().mappingsModified()) { if (create.parsedDoc().mappingsModified()) {
updateMappingOnMaster(request, indexMetaData); updateMappingOnMaster(request, indexMetaData);
@ -256,6 +257,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
} else { } else {
Engine.Create create = indexShard.prepareCreate(sourceToParse) Engine.Create create = indexShard.prepareCreate(sourceToParse)
.version(request.version()).versionType(request.versionType()) .version(request.version()).versionType(request.versionType())
.autoGeneratedId(request.autoGeneratedId())
.origin(Engine.Operation.Origin.REPLICA); .origin(Engine.Operation.Origin.REPLICA);
indexShard.create(create); indexShard.create(create);
} }

View File

@ -388,6 +388,7 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
private final Term uid; private final Term uid;
private final ParsedDocument doc; private final ParsedDocument doc;
private long version = Versions.MATCH_ANY; private long version = Versions.MATCH_ANY;
private boolean autoGeneratedId = false;
private VersionType versionType = VersionType.INTERNAL; private VersionType versionType = VersionType.INTERNAL;
private Origin origin = Origin.PRIMARY; private Origin origin = Origin.PRIMARY;
@ -468,6 +469,15 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
return this; return this;
} }
public Create autoGeneratedId(boolean autoGeneratedId) {
this.autoGeneratedId = autoGeneratedId;
return this;
}
public boolean autoGeneratedId() {
return this.autoGeneratedId;
}
public String parent() { public String parent() {
return this.doc.parent(); return this.doc.parent();
} }

View File

@ -406,7 +406,12 @@ public class InternalEngine extends AbstractIndexShardComponent implements Engin
synchronized (dirtyLock(create.uid())) { synchronized (dirtyLock(create.uid())) {
HashedBytesRef versionKey = versionKey(create.uid()); HashedBytesRef versionKey = versionKey(create.uid());
final long currentVersion; final long currentVersion;
VersionValue versionValue = versionMap.get(versionKey); final VersionValue versionValue;
if (create.autoGeneratedId()) {
currentVersion = Versions.NOT_FOUND;
versionValue = null;
} else {
versionValue = versionMap.get(versionKey);
if (versionValue == null) { if (versionValue == null) {
currentVersion = loadCurrentVersionFromIndex(create.uid()); currentVersion = loadCurrentVersionFromIndex(create.uid());
} else { } else {
@ -416,6 +421,7 @@ public class InternalEngine extends AbstractIndexShardComponent implements Engin
currentVersion = versionValue.version(); currentVersion = versionValue.version();
} }
} }
}
// same logic as index // same logic as index
long updatedVersion; long updatedVersion;

View File

@ -50,6 +50,7 @@ public class SingleThreadBulkStress {
int shardsCount = Integer.parseInt(System.getProperty("es.shards", "1")); int shardsCount = Integer.parseInt(System.getProperty("es.shards", "1"));
int replicaCount = Integer.parseInt(System.getProperty("es.replica", "1")); int replicaCount = Integer.parseInt(System.getProperty("es.replica", "1"));
boolean autoGenerateId = true;
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("index.refresh_interval", "1s") .put("index.refresh_interval", "1s")
@ -94,7 +95,7 @@ public class SingleThreadBulkStress {
BulkRequestBuilder request = client1.prepareBulk(); BulkRequestBuilder request = client1.prepareBulk();
for (int j = 0; j < BATCH; j++) { for (int j = 0; j < BATCH; j++) {
counter++; counter++;
request.add(Requests.indexRequest("test").type("type1").id(Integer.toString(counter)).source(source(Integer.toString(counter), "test" + counter))); request.add(Requests.indexRequest("test").type("type1").id(autoGenerateId ? null : Integer.toString(counter)).source(source(Integer.toString(counter), "test" + counter)));
} }
BulkResponse response = request.execute().actionGet(); BulkResponse response = request.execute().actionGet();
if (response.hasFailures()) { if (response.hasFailures()) {