Skip to content

Commit 99734ec

Browse files
authored
Merge pull request #20034 from areek/cleanup/index_operation
Set created flag in index operation
2 parents 9c3f6d5 + fe5cdd3 commit 99734ec

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
@@ -386,16 +386,15 @@ private <T extends Engine.Operation> void maybeAddToTranslog(
386386
}
387387

388388
@Override
389-
public boolean index(Index index) {
390-
final boolean created;
389+
public void index(Index index) {
391390
try (ReleasableLock lock = readLock.acquire()) {
392391
ensureOpen();
393392
if (index.origin().isRecovery()) {
394393
// Don't throttle recovery operations
395-
created = innerIndex(index);
394+
innerIndex(index);
396395
} else {
397396
try (Releasable r = throttle.acquireThrottle()) {
398-
created = innerIndex(index);
397+
innerIndex(index);
399398
}
400399
}
401400
} catch (IllegalStateException | IOException e) {
@@ -406,10 +405,9 @@ public boolean index(Index index) {
406405
}
407406
throw new IndexFailedEngineException(shardId, index.type(), index.id(), e);
408407
}
409-
return created;
410408
}
411409

412-
private boolean innerIndex(Index index) throws IOException {
410+
private void innerIndex(Index index) throws IOException {
413411
try (Releasable ignored = acquireLock(index.uid())) {
414412
lastWriteNanos = index.startTime();
415413
final long currentVersion;
@@ -424,15 +422,16 @@ private boolean innerIndex(Index index) throws IOException {
424422
}
425423

426424
final long expectedVersion = index.version();
427-
if (checkVersionConflict(index, currentVersion, expectedVersion, deleted)) return false;
425+
if (checkVersionConflict(index, currentVersion, expectedVersion, deleted)) {
426+
index.setCreated(false);
427+
return;
428+
}
428429

429430
final long updatedVersion = updateVersion(index, currentVersion, expectedVersion);
430431

431-
final boolean created = indexOrUpdate(index, currentVersion, versionValue);
432+
indexOrUpdate(index, currentVersion, versionValue);
432433

433434
maybeAddToTranslog(index, updatedVersion, Translog.Index::new, NEW_VERSION_VALUE);
434-
435-
return created;
436435
}
437436
}
438437

@@ -442,16 +441,14 @@ private long updateVersion(Engine.Operation op, long currentVersion, long expect
442441
return updatedVersion;
443442
}
444443

445-
private boolean indexOrUpdate(final Index index, final long currentVersion, final VersionValue versionValue) throws IOException {
446-
final boolean created;
444+
private void indexOrUpdate(final Index index, final long currentVersion, final VersionValue versionValue) throws IOException {
447445
if (currentVersion == Versions.NOT_FOUND) {
448446
// document does not exists, we can optimize for create
449-
created = true;
447+
index.setCreated(true);
450448
index(index, indexWriter);
451449
} else {
452-
created = update(index, versionValue, indexWriter);
450+
update(index, versionValue, indexWriter);
453451
}
454-
return created;
455452
}
456453

457454
private static void index(final Index index, final IndexWriter indexWriter) throws IOException {
@@ -462,19 +459,17 @@ private static void index(final Index index, final IndexWriter indexWriter) thro
462459
}
463460
}
464461

465-
private static boolean update(final Index index, final VersionValue versionValue, final IndexWriter indexWriter) throws IOException {
466-
final boolean created;
462+
private static void update(final Index index, final VersionValue versionValue, final IndexWriter indexWriter) throws IOException {
467463
if (versionValue != null) {
468-
created = versionValue.delete(); // we have a delete which is not GC'ed...
464+
index.setCreated(versionValue.delete()); // we have a delete which is not GC'ed...
469465
} else {
470-
created = false;
466+
index.setCreated(false);
471467
}
472468
if (index.docs().size() > 1) {
473469
indexWriter.updateDocuments(index.uid(), index.docs());
474470
} else {
475471
indexWriter.updateDocument(index.uid(), index.docs().get(0));
476472
}
477-
return created;
478473
}
479474

480475
@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
@@ -523,34 +523,26 @@ static Engine.Index prepareIndex(DocumentMapperForType docMapper, SourceToParse
523523
return new Engine.Index(uid, doc, version, versionType, origin, startTime);
524524
}
525525

526-
/**
527-
* Index a document and return whether it was created, as opposed to just
528-
* updated.
529-
*/
530-
public boolean index(Engine.Index index) {
526+
public void index(Engine.Index index) {
531527
ensureWriteAllowed(index);
532528
Engine engine = getEngine();
533-
return index(engine, index);
529+
index(engine, index);
534530
}
535531

536-
private boolean index(Engine engine, Engine.Index index) {
532+
private void index(Engine engine, Engine.Index index) {
537533
active.set(true);
538534
index = indexingOperationListeners.preIndex(index);
539-
final boolean created;
540535
try {
541536
if (logger.isTraceEnabled()) {
542537
logger.trace("index [{}][{}]{}", index.type(), index.id(), index.docs());
543538
}
544-
created = engine.index(index);
539+
engine.index(index);
545540
index.endTime(System.nanoTime());
546541
} catch (Exception e) {
547542
indexingOperationListeners.postIndex(index, e);
548543
throw e;
549544
}
550-
551-
indexingOperationListeners.postIndex(index, created);
552-
553-
return created;
545+
indexingOperationListeners.postIndex(index, index.isCreated());
554546
}
555547

556548
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
@@ -1475,28 +1475,33 @@ public void testVersioningReplicaConflict2() {
14751475
public void testBasicCreatedFlag() {
14761476
ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocument(), B_1, null);
14771477
Engine.Index index = new Engine.Index(newUid("1"), doc);
1478-
assertTrue(engine.index(index));
1478+
engine.index(index);
1479+
assertTrue(index.isCreated());
14791480

14801481
index = new Engine.Index(newUid("1"), doc);
1481-
assertFalse(engine.index(index));
1482+
engine.index(index);
1483+
assertFalse(index.isCreated());
14821484

14831485
engine.delete(new Engine.Delete(null, "1", newUid("1")));
14841486

14851487
index = new Engine.Index(newUid("1"), doc);
1486-
assertTrue(engine.index(index));
1488+
engine.index(index);
1489+
assertTrue(index.isCreated());
14871490
}
14881491

14891492
public void testCreatedFlagAfterFlush() {
14901493
ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocument(), B_1, null);
14911494
Engine.Index index = new Engine.Index(newUid("1"), doc);
1492-
assertTrue(engine.index(index));
1495+
engine.index(index);
1496+
assertTrue(index.isCreated());
14931497

14941498
engine.delete(new Engine.Delete(null, "1", newUid("1")));
14951499

14961500
engine.flush();
14971501

14981502
index = new Engine.Index(newUid("1"), doc);
1499-
assertTrue(engine.index(index));
1503+
engine.index(index);
1504+
assertTrue(index.isCreated());
15001505
}
15011506

15021507
private static class MockAppender extends AppenderSkeleton {

0 commit comments

Comments
 (0)