@@ -736,6 +736,10 @@ protected long doGenerateSeqNoForOperation(final Operation operation) {
736736 return localCheckpointTracker .generateSeqNo ();
737737 }
738738
739+ private long getPrimaryTerm () {
740+ return engineConfig .getPrimaryTermSupplier ().getAsLong ();
741+ }
742+
739743 @ Override
740744 public IndexResult index (Index index ) throws IOException {
741745 assert Objects .equals (index .uid ().field (), IdFieldMapper .NAME ) : index .uid ().field ();
@@ -788,7 +792,7 @@ public IndexResult index(Index index) throws IOException {
788792 indexResult = indexIntoLucene (index , plan );
789793 } else {
790794 indexResult = new IndexResult (
791- plan .versionForIndexing , plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
795+ plan .versionForIndexing , getPrimaryTerm (), plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
792796 }
793797 if (index .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
794798 final Translog .Location location ;
@@ -900,7 +904,7 @@ private IndexingStrategy planIndexingAsPrimary(Index index) throws IOException {
900904 currentVersion , index .version (), currentNotFoundOrDeleted )) {
901905 final VersionConflictEngineException e =
902906 new VersionConflictEngineException (shardId , index , currentVersion , currentNotFoundOrDeleted );
903- plan = IndexingStrategy .skipDueToVersionConflict (e , currentNotFoundOrDeleted , currentVersion );
907+ plan = IndexingStrategy .skipDueToVersionConflict (e , currentNotFoundOrDeleted , currentVersion , getPrimaryTerm () );
904908 } else {
905909 plan = IndexingStrategy .processNormally (currentNotFoundOrDeleted ,
906910 generateSeqNoForOperation (index ),
@@ -930,7 +934,7 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan)
930934 assert assertDocDoesNotExist (index , canOptimizeAddDocument (index ) == false );
931935 addDocs (index .docs (), indexWriter );
932936 }
933- return new IndexResult (plan .versionForIndexing , plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
937+ return new IndexResult (plan .versionForIndexing , getPrimaryTerm (), plan .seqNoForIndexing , plan .currentNotFoundOrDeleted );
934938 } catch (Exception ex ) {
935939 if (indexWriter .getTragicException () == null ) {
936940 /* There is no tragic event recorded so this must be a document failure.
@@ -946,7 +950,7 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan)
946950 * we return a `MATCH_ANY` version to indicate no document was index. The value is
947951 * not used anyway
948952 */
949- return new IndexResult (ex , Versions .MATCH_ANY , plan .seqNoForIndexing );
953+ return new IndexResult (ex , Versions .MATCH_ANY , getPrimaryTerm (), plan .seqNoForIndexing );
950954 } else {
951955 throw ex ;
952956 }
@@ -1019,8 +1023,8 @@ static IndexingStrategy optimizedAppendOnly(long seqNoForIndexing) {
10191023 }
10201024
10211025 static IndexingStrategy skipDueToVersionConflict (
1022- VersionConflictEngineException e , boolean currentNotFoundOrDeleted , long currentVersion ) {
1023- final IndexResult result = new IndexResult (e , currentVersion );
1026+ VersionConflictEngineException e , boolean currentNotFoundOrDeleted , long currentVersion , long term ) {
1027+ final IndexResult result = new IndexResult (e , currentVersion , term );
10241028 return new IndexingStrategy (
10251029 currentNotFoundOrDeleted , false , false , SequenceNumbers .UNASSIGNED_SEQ_NO , Versions .NOT_FOUND , result );
10261030 }
@@ -1097,7 +1101,7 @@ public DeleteResult delete(Delete delete) throws IOException {
10971101 deleteResult = deleteInLucene (delete , plan );
10981102 } else {
10991103 deleteResult = new DeleteResult (
1100- plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1104+ plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
11011105 }
11021106 if (delete .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
11031107 final Translog .Location location ;
@@ -1178,7 +1182,7 @@ private DeletionStrategy planDeletionAsPrimary(Delete delete) throws IOException
11781182 final DeletionStrategy plan ;
11791183 if (delete .versionType ().isVersionConflictForWrites (currentVersion , delete .version (), currentlyDeleted )) {
11801184 final VersionConflictEngineException e = new VersionConflictEngineException (shardId , delete , currentVersion , currentlyDeleted );
1181- plan = DeletionStrategy .skipDueToVersionConflict (e , currentVersion , currentlyDeleted );
1185+ plan = DeletionStrategy .skipDueToVersionConflict (e , currentVersion , getPrimaryTerm (), currentlyDeleted );
11821186 } else {
11831187 plan = DeletionStrategy .processNormally (
11841188 currentlyDeleted ,
@@ -1201,12 +1205,12 @@ private DeleteResult deleteInLucene(Delete delete, DeletionStrategy plan)
12011205 new DeleteVersionValue (plan .versionOfDeletion , plan .seqNoOfDeletion , delete .primaryTerm (),
12021206 engineConfig .getThreadPool ().relativeTimeInMillis ()));
12031207 return new DeleteResult (
1204- plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1208+ plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
12051209 } catch (Exception ex ) {
12061210 if (indexWriter .getTragicException () == null ) {
12071211 // there is no tragic event and such it must be a document level failure
12081212 return new DeleteResult (
1209- ex , plan .versionOfDeletion , plan .seqNoOfDeletion , plan .currentlyDeleted == false );
1213+ ex , plan .versionOfDeletion , getPrimaryTerm (), plan .seqNoOfDeletion , plan .currentlyDeleted == false );
12101214 } else {
12111215 throw ex ;
12121216 }
@@ -1237,9 +1241,9 @@ private DeletionStrategy(boolean deleteFromLucene, boolean currentlyDeleted,
12371241 }
12381242
12391243 static DeletionStrategy skipDueToVersionConflict (
1240- VersionConflictEngineException e , long currentVersion , boolean currentlyDeleted ) {
1244+ VersionConflictEngineException e , long currentVersion , long term , boolean currentlyDeleted ) {
12411245 final long unassignedSeqNo = SequenceNumbers .UNASSIGNED_SEQ_NO ;
1242- final DeleteResult deleteResult = new DeleteResult (e , currentVersion , unassignedSeqNo , currentlyDeleted == false );
1246+ final DeleteResult deleteResult = new DeleteResult (e , currentVersion , term , unassignedSeqNo , currentlyDeleted == false );
12431247 return new DeletionStrategy (false , currentlyDeleted , unassignedSeqNo , Versions .NOT_FOUND , deleteResult );
12441248 }
12451249
@@ -1268,7 +1272,7 @@ public NoOpResult noOp(final NoOp noOp) {
12681272 try (ReleasableLock ignored = readLock .acquire ()) {
12691273 noOpResult = innerNoOp (noOp );
12701274 } catch (final Exception e ) {
1271- noOpResult = new NoOpResult (noOp .seqNo (), e );
1275+ noOpResult = new NoOpResult (getPrimaryTerm (), noOp .seqNo (), e );
12721276 }
12731277 return noOpResult ;
12741278 }
@@ -1278,7 +1282,7 @@ private NoOpResult innerNoOp(final NoOp noOp) throws IOException {
12781282 assert noOp .seqNo () > SequenceNumbers .NO_OPS_PERFORMED ;
12791283 final long seqNo = noOp .seqNo ();
12801284 try {
1281- final NoOpResult noOpResult = new NoOpResult (noOp .seqNo ());
1285+ final NoOpResult noOpResult = new NoOpResult (getPrimaryTerm (), noOp .seqNo ());
12821286 if (noOp .origin () != Operation .Origin .LOCAL_TRANSLOG_RECOVERY ) {
12831287 final Translog .Location location = translog .add (new Translog .NoOp (noOp .seqNo (), noOp .primaryTerm (), noOp .reason ()));
12841288 noOpResult .setTranslogLocation (location );
0 commit comments