@@ -789,6 +789,10 @@ protected long doGenerateSeqNoForOperation(final Operation operation) {
789789 return localCheckpointTracker .generateSeqNo ();
790790 }
791791
792+ private long getPrimaryTerm () {
793+ return engineConfig .getPrimaryTermSupplier ().getAsLong ();
794+ }
795+
792796 @ Override
793797 public IndexResult index (Index index ) throws IOException {
794798 assert Objects .equals (index .uid ().field (), uidField ) : index .uid ().field ();
@@ -842,7 +846,7 @@ public IndexResult index(Index index) throws IOException {
842846 indexResult = indexIntoLucene (index , plan );
843847 } else {
844848 indexResult = new IndexResult (
845- plan .versionForIndexing , plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
849+ plan .versionForIndexing , getPrimaryTerm (), plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
846850 }
847851 if (index .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
848852 final Translog .Location location ;
@@ -963,7 +967,7 @@ private IndexingStrategy planIndexingAsPrimary(Index index) throws IOException {
963967 currentVersion , index .version (), currentNotFoundOrDeleted )) {
964968 final VersionConflictEngineException e =
965969 new VersionConflictEngineException (shardId , index , currentVersion , currentNotFoundOrDeleted );
966- plan = IndexingStrategy .skipDueToVersionConflict (e , currentNotFoundOrDeleted , currentVersion );
970+ plan = IndexingStrategy .skipDueToVersionConflict (e , currentNotFoundOrDeleted , currentVersion , getPrimaryTerm () );
967971 } else {
968972 plan = IndexingStrategy .processNormally (currentNotFoundOrDeleted ,
969973 generateSeqNoForOperation (index ),
@@ -993,7 +997,7 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan)
993997 assert assertDocDoesNotExist (index , canOptimizeAddDocument (index ) == false );
994998 addDocs (index .docs (), indexWriter );
995999 }
996- return new IndexResult (plan .versionForIndexing , plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
1000+ return new IndexResult (plan .versionForIndexing , getPrimaryTerm (), plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
9971001 } catch (Exception ex ) {
9981002 if (indexWriter .getTragicException () == null ) {
9991003 /* There is no tragic event recorded so this must be a document failure.
@@ -1009,7 +1013,7 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan)
10091013 * we return a `MATCH_ANY` version to indicate no document was index. The value is
10101014 * not used anyway
10111015 */
1012- return new IndexResult (ex , Versions .MATCH_ANY , plan .seqNoForIndexing );
1016+ return new IndexResult (ex , Versions .MATCH_ANY , getPrimaryTerm (), plan .seqNoForIndexing );
10131017 } else {
10141018 throw ex ;
10151019 }
@@ -1082,8 +1086,8 @@ static IndexingStrategy optimizedAppendOnly(long seqNoForIndexing) {
10821086 }
10831087
10841088 static IndexingStrategy skipDueToVersionConflict (
1085- VersionConflictEngineException e , boolean currentNotFoundOrDeleted , long currentVersion ) {
1086- final IndexResult result = new IndexResult (e , currentVersion );
1089+ VersionConflictEngineException e , boolean currentNotFoundOrDeleted , long currentVersion , long term ) {
1090+ final IndexResult result = new IndexResult (e , currentVersion , term );
10871091 return new IndexingStrategy (
10881092 currentNotFoundOrDeleted , false , false , SequenceNumbers .UNASSIGNED_SEQ_NO , Versions .NOT_FOUND , result );
10891093 }
@@ -1161,7 +1165,7 @@ public DeleteResult delete(Delete delete) throws IOException {
11611165 deleteResult = deleteInLucene (delete , plan );
11621166 } else {
11631167 deleteResult = new DeleteResult (
1164- plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1168+ plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
11651169 }
11661170 if (delete .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
11671171 final Translog .Location location ;
@@ -1250,7 +1254,7 @@ private DeletionStrategy planDeletionAsPrimary(Delete delete) throws IOException
12501254 final DeletionStrategy plan ;
12511255 if (delete .versionType ().isVersionConflictForWrites (currentVersion , delete .version (), currentlyDeleted )) {
12521256 final VersionConflictEngineException e = new VersionConflictEngineException (shardId , delete , currentVersion , currentlyDeleted );
1253- plan = DeletionStrategy .skipDueToVersionConflict (e , currentVersion , currentlyDeleted );
1257+ plan = DeletionStrategy .skipDueToVersionConflict (e , currentVersion , getPrimaryTerm (), currentlyDeleted );
12541258 } else {
12551259 plan = DeletionStrategy .processNormally (
12561260 currentlyDeleted ,
@@ -1273,12 +1277,12 @@ private DeleteResult deleteInLucene(Delete delete, DeletionStrategy plan)
12731277 new DeleteVersionValue (plan .versionOfDeletion , plan .seqNoOfDeletion , delete .primaryTerm (),
12741278 engineConfig .getThreadPool ().relativeTimeInMillis ()));
12751279 return new DeleteResult (
1276- plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1280+ plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
12771281 } catch (Exception ex ) {
12781282 if (indexWriter .getTragicException () == null ) {
12791283 // there is no tragic event and such it must be a document level failure
12801284 return new DeleteResult (
1281- ex , plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1285+ ex , plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
12821286 } else {
12831287 throw ex ;
12841288 }
@@ -1309,9 +1313,9 @@ private DeletionStrategy(boolean deleteFromLucene, boolean currentlyDeleted,
13091313 }
13101314
13111315 static DeletionStrategy skipDueToVersionConflict (
1312- VersionConflictEngineException e , long currentVersion , boolean currentlyDeleted ) {
1316+ VersionConflictEngineException e , long currentVersion , long term , boolean currentlyDeleted ) {
13131317 final long unassignedSeqNo = SequenceNumbers .UNASSIGNED_SEQ_NO ;
1314- final DeleteResult deleteResult = new DeleteResult (e , currentVersion , unassignedSeqNo , currentlyDeleted == false );
1318+ final DeleteResult deleteResult = new DeleteResult (e , currentVersion , term , unassignedSeqNo , currentlyDeleted == false );
13151319 return new DeletionStrategy (false , currentlyDeleted , unassignedSeqNo , Versions .NOT_FOUND , deleteResult );
13161320 }
13171321
@@ -1340,7 +1344,7 @@ public NoOpResult noOp(final NoOp noOp) {
13401344 try (ReleasableLock ignored = readLock .acquire ()) {
13411345 noOpResult = innerNoOp (noOp );
13421346 } catch (final Exception e ) {
1343- noOpResult = new NoOpResult (noOp .seqNo (), e );
1347+ noOpResult = new NoOpResult (getPrimaryTerm (), noOp .seqNo (), e );
13441348 }
13451349 return noOpResult ;
13461350 }
@@ -1350,7 +1354,7 @@ private NoOpResult innerNoOp(final NoOp noOp) throws IOException {
13501354 assert noOp .seqNo () > SequenceNumbers .NO_OPS_PERFORMED ;
13511355 final long seqNo = noOp .seqNo ();
13521356 try {
1353- final NoOpResult noOpResult = new NoOpResult (noOp .seqNo ());
1357+ final NoOpResult noOpResult = new NoOpResult (getPrimaryTerm (), noOp .seqNo ());
13541358 if (noOp .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
13551359 final Translog .Location location = translog .add (new Translog .NoOp (noOp .seqNo (), noOp .primaryTerm (), noOp .reason ()));
13561360 noOpResult .setTranslogLocation (location );
0 commit comments