Skip to content

Commit fe5cdd3

Browse files
committed
Set created flag in index operation
Now document created flag is set in the index operation instead of being returned from engine operation. This change makes the engine index and delete operations have the same signature.
1 parent d4dec26 commit fe5cdd3

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static WriteResult<IndexResponse> executeIndexRequestOnPrimary(IndexReque
188188
"Dynamic mappings are not available on the node that holds the primary yet");
189189
}
190190
}
191-
final boolean created = indexShard.index(operation);
191+
indexShard.index(operation);
192192

193193
// update the version on request so it will happen on the replicas
194194
final long version = operation.version();
@@ -197,7 +197,7 @@ public static WriteResult<IndexResponse> executeIndexRequestOnPrimary(IndexReque
197197

198198
assert request.versionType().validateVersionForWrites(request.version());
199199

200-
IndexResponse response = new IndexResponse(shardId, request.type(), request.id(), request.version(), created);
200+
IndexResponse response = new IndexResponse(shardId, request.type(), request.id(), request.version(), operation.isCreated());
201201
return new WriteResult<>(response, operation.getTranslogLocation());
202202
}
203203
}

core/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public Condition newCondition() {
277277
}
278278
}
279279

280-
public abstract boolean index(Index operation) throws EngineException;
280+
public abstract void index(Index operation) throws EngineException;
281281

282282
public abstract void delete(Delete delete) throws EngineException;
283283

@@ -847,6 +847,7 @@ public long endTime() {
847847
public static class Index extends Operation {
848848

849849
private final ParsedDocument doc;
850+
private boolean created;
850851

851852
public Index(Term uid, ParsedDocument doc, long version, VersionType versionType, Origin origin, long startTime) {
852853
super(uid, version, versionType, origin, startTime);
@@ -905,6 +906,14 @@ public BytesReference source() {
905906
return this.doc.source();
906907
}
907908

909+
public boolean isCreated() {
910+
return created;
911+
}
912+
913+
public void setCreated(boolean created) {
914+
this.created = created;
915+
}
916+
908917
@Override
909918
protected int estimatedSizeInBytes() {
910919
return (id().length() + type().length()) * 2 + source().length() + 12;

core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,15 @@ private <T extends Engine.Operation> void maybeAddToTranslog(
394394
}
395395

396396
@Override
397-
public boolean index(Index index) {
398-
final boolean created;
397+
public void index(Index index) {
399398
try (ReleasableLock lock = readLock.acquire()) {
400399
ensureOpen();
401400
if (index.origin().isRecovery()) {
402401
// Don't throttle recovery operations
403-
created = innerIndex(index);
402+
innerIndex(index);
404403
} else {
405404
try (Releasable r = throttle.acquireThrottle()) {
406-
created = innerIndex(index);
405+
innerIndex(index);
407406
}
408407
}
409408
} catch (IllegalStateException | IOException e) {
@@ -414,10 +413,9 @@ public boolean index(Index index) {
414413
}
415414
throw new IndexFailedEngineException(shardId, index.type(), index.id(), e);
416415
}
417-
return created;
418416
}
419417

420-
private boolean innerIndex(Index index) throws IOException {
418+
private void innerIndex(Index index) throws IOException {
421419
try (Releasable ignored = acquireLock(index.uid())) {
422420
lastWriteNanos = index.startTime();
423421
final long currentVersion;
@@ -432,15 +430,16 @@ private boolean innerIndex(Index index) throws IOException {
432430
}
433431

434432
final long expectedVersion = index.version();
435-
if (checkVersionConflict(index, currentVersion, expectedVersion, deleted)) return false;
433+
if (checkVersionConflict(index, currentVersion, expectedVersion, deleted)) {
434+
index.setCreated(false);
435+
return;
436+
}
436437

437438
final long updatedVersion = updateVersion(index, currentVersion, expectedVersion);
438439

439-
final boolean created = indexOrUpdate(index, currentVersion, versionValue);
440+
indexOrUpdate(index, currentVersion, versionValue);
440441

441442
maybeAddToTranslog(index, updatedVersion, Translog.Index::new, NEW_VERSION_VALUE);
442-
443-
return created;
444443
}
445444
}
446445

@@ -450,16 +449,14 @@ private long updateVersion(Engine.Operation op, long currentVersion, long expect
450449
return updatedVersion;
451450
}
452451

453-
private boolean indexOrUpdate(final Index index, final long currentVersion, final VersionValue versionValue) throws IOException {
454-
final boolean created;
452+
private void indexOrUpdate(final Index index, final long currentVersion, final VersionValue versionValue) throws IOException {
455453
if (currentVersion == Versions.NOT_FOUND) {
456454
// document does not exists, we can optimize for create
457-
created = true;
455+
index.setCreated(true);
458456
index(index, indexWriter);
459457
} else {
460-
created = update(index, versionValue, indexWriter);
458+
update(index, versionValue, indexWriter);
461459
}
462-
return created;
463460
}
464461

465462
private static void index(final Index index, final IndexWriter indexWriter) throws IOException {
@@ -470,19 +467,17 @@ private static void index(final Index index, final IndexWriter indexWriter) thro
470467
}
471468
}
472469

473-
private static boolean update(final Index index, final VersionValue versionValue, final IndexWriter indexWriter) throws IOException {
474-
final boolean created;
470+
private static void update(final Index index, final VersionValue versionValue, final IndexWriter indexWriter) throws IOException {
475471
if (versionValue != null) {
476-
created = versionValue.delete(); // we have a delete which is not GC'ed...
472+
index.setCreated(versionValue.delete()); // we have a delete which is not GC'ed...
477473
} else {
478-
created = false;
474+
index.setCreated(false);
479475
}
480476
if (index.docs().size() > 1) {
481477
indexWriter.updateDocuments(index.uid(), index.docs());
482478
} else {
483479
indexWriter.updateDocument(index.uid(), index.docs().get(0));
484480
}
485-
return created;
486481
}
487482

488483
@Override

core/src/main/java/org/elasticsearch/index/engine/ShadowEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public ShadowEngine(EngineConfig engineConfig) {
106106

107107

108108
@Override
109-
public boolean index(Index index) throws EngineException {
109+
public void index(Index index) throws EngineException {
110110
throw new UnsupportedOperationException(shardId + " index operation not allowed on shadow engine");
111111
}
112112

core/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -518,34 +518,26 @@ static Engine.Index prepareIndex(DocumentMapperForType docMapper, SourceToParse
518518
return new Engine.Index(uid, doc, version, versionType, origin, startTime);
519519
}
520520

521-
/**
522-
* Index a document and return whether it was created, as opposed to just
523-
* updated.
524-
*/
525-
public boolean index(Engine.Index index) {
521+
public void index(Engine.Index index) {
526522
ensureWriteAllowed(index);
527523
Engine engine = getEngine();
528-
return index(engine, index);
524+
index(engine, index);
529525
}
530526

531-
private boolean index(Engine engine, Engine.Index index) {
527+
private void index(Engine engine, Engine.Index index) {
532528
active.set(true);
533529
index = indexingOperationListeners.preIndex(index);
534-
final boolean created;
535530
try {
536531
if (logger.isTraceEnabled()) {
537532
logger.trace("index [{}][{}]{}", index.type(), index.id(), index.docs());
538533
}
539-
created = engine.index(index);
534+
engine.index(index);
540535
index.endTime(System.nanoTime());
541536
} catch (Exception e) {
542537
indexingOperationListeners.postIndex(index, e);
543538
throw e;
544539
}
545-
546-
indexingOperationListeners.postIndex(index, created);
547-
548-
return created;
540+
indexingOperationListeners.postIndex(index, index.isCreated());
549541
}
550542

551543
public Engine.Delete prepareDeleteOnPrimary(String type, String id, long version, VersionType versionType) {

core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,28 +1480,33 @@ public void testVersioningReplicaConflict2() {
14801480
public void testBasicCreatedFlag() {
14811481
ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocument(), B_1, null);
14821482
Engine.Index index = new Engine.Index(newUid("1"), doc);
1483-
assertTrue(engine.index(index));
1483+
engine.index(index);
1484+
assertTrue(index.isCreated());
14841485

14851486
index = new Engine.Index(newUid("1"), doc);
1486-
assertFalse(engine.index(index));
1487+
engine.index(index);
1488+
assertFalse(index.isCreated());
14871489

14881490
engine.delete(new Engine.Delete(null, "1", newUid("1")));
14891491

14901492
index = new Engine.Index(newUid("1"), doc);
1491-
assertTrue(engine.index(index));
1493+
engine.index(index);
1494+
assertTrue(index.isCreated());
14921495
}
14931496

14941497
public void testCreatedFlagAfterFlush() {
14951498
ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocument(), B_1, null);
14961499
Engine.Index index = new Engine.Index(newUid("1"), doc);
1497-
assertTrue(engine.index(index));
1500+
engine.index(index);
1501+
assertTrue(index.isCreated());
14981502

14991503
engine.delete(new Engine.Delete(null, "1", newUid("1")));
15001504

15011505
engine.flush();
15021506

15031507
index = new Engine.Index(newUid("1"), doc);
1504-
assertTrue(engine.index(index));
1508+
engine.index(index);
1509+
assertTrue(index.isCreated());
15051510
}
15061511

15071512
private static class MockAppender extends AppenderSkeleton {

0 commit comments

Comments
 (0)