Skip to content

Commit

Permalink
Core: remove per-ID locking when ID was auto-generated
Browse files Browse the repository at this point in the history
When we know the ID for the document we are about to index was
auto-generated, we don't need to acquire/release the per-ID lock,
which might provide small speedups during highly concurrent indexing.

Closes #6584
  • Loading branch information
mikemccand committed Jul 7, 2014
1 parent 909cc4f commit 00c7a2f
Showing 1 changed file with 41 additions and 37 deletions.
Expand Up @@ -402,13 +402,13 @@ private void maybeFailEngine(Throwable t) {
}

private void innerCreate(Create create, IndexWriter writer) throws IOException {
synchronized (dirtyLock(create.uid())) {
final long currentVersion;
final VersionValue versionValue;
if (optimizeAutoGenerateId && create.autoGeneratedId() && !create.canHaveDuplicates()) {
currentVersion = Versions.NOT_FOUND;
versionValue = null;
} else {
if (optimizeAutoGenerateId && create.autoGeneratedId() && !create.canHaveDuplicates()) {
// We don't need to lock because this ID cannot be concurrently updated:
innerCreateNoLock(create, writer, Versions.NOT_FOUND, null);
} else {
synchronized (dirtyLock(create.uid())) {
final long currentVersion;
final VersionValue versionValue;
versionValue = versionMap.getUnderLock(create.uid().bytes());
if (versionValue == null) {
currentVersion = loadCurrentVersionFromIndex(create.uid());
Expand All @@ -419,51 +419,55 @@ private void innerCreate(Create create, IndexWriter writer) throws IOException {
currentVersion = versionValue.version();
}
}
innerCreateNoLock(create, writer, currentVersion, versionValue);
}
}
}

// same logic as index
long updatedVersion;
long expectedVersion = create.version();
if (create.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
if (create.origin() == Operation.Origin.RECOVERY) {
return;
} else {
throw new VersionConflictEngineException(shardId, create.type(), create.id(), currentVersion, expectedVersion);
}
private void innerCreateNoLock(Create create, IndexWriter writer, long currentVersion, VersionValue versionValue) throws IOException {

// same logic as index
long updatedVersion;
long expectedVersion = create.version();
if (create.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
if (create.origin() == Operation.Origin.RECOVERY) {
return;
} else {
throw new VersionConflictEngineException(shardId, create.type(), create.id(), currentVersion, expectedVersion);
}
updatedVersion = create.versionType().updateVersion(currentVersion, expectedVersion);
}
updatedVersion = create.versionType().updateVersion(currentVersion, expectedVersion);

// if the doc does not exist or it exists but is not deleted
if (versionValue != null) {
if (!versionValue.delete()) {
if (create.origin() == Operation.Origin.RECOVERY) {
return;
} else {
throw new DocumentAlreadyExistsException(shardId, create.type(), create.id());
}
}
} else if (currentVersion != Versions.NOT_FOUND) {
// its not deleted, its already there
// if the doc does not exist or it exists but is not deleted
if (versionValue != null) {
if (!versionValue.delete()) {
if (create.origin() == Operation.Origin.RECOVERY) {
return;
} else {
throw new DocumentAlreadyExistsException(shardId, create.type(), create.id());
}
}

create.updateVersion(updatedVersion);

if (create.docs().size() > 1) {
writer.addDocuments(create.docs(), create.analyzer());
} else if (currentVersion != Versions.NOT_FOUND) {
// its not deleted, its already there
if (create.origin() == Operation.Origin.RECOVERY) {
return;
} else {
writer.addDocument(create.docs().get(0), create.analyzer());
throw new DocumentAlreadyExistsException(shardId, create.type(), create.id());
}
Translog.Location translogLocation = translog.add(new Translog.Create(create));
}

versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, translogLocation));
create.updateVersion(updatedVersion);

indexingService.postCreateUnderLock(create);
if (create.docs().size() > 1) {
writer.addDocuments(create.docs(), create.analyzer());
} else {
writer.addDocument(create.docs().get(0), create.analyzer());
}
Translog.Location translogLocation = translog.add(new Translog.Create(create));

versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, translogLocation));

indexingService.postCreateUnderLock(create);
}

@Override
Expand Down

0 comments on commit 00c7a2f

Please sign in to comment.