5252import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
5353import org .elasticsearch .common .util .concurrent .ConcurrentCollections ;
5454import org .elasticsearch .common .util .concurrent .ThreadContext ;
55- import org .elasticsearch .index .Index ;
5655import org .elasticsearch .index .IndexService ;
5756import org .elasticsearch .index .engine .VersionConflictEngineException ;
5857import org .elasticsearch .index .shard .IndexShard ;
@@ -359,32 +358,7 @@ public void onTimeout(TimeValue timeout) {
359358 }
360359 });
361360 } else {
362- try {
363- failReplicaIfNeeded (t );
364- } catch (Throwable unexpected ) {
365- logger .error ("{} unexpected error while failing replica" , unexpected , request .shardId ().id ());
366- } finally {
367361 responseWithFailure (t );
368- }
369- }
370- }
371-
372- private void failReplicaIfNeeded (Throwable t ) {
373- Index index = request .shardId ().getIndex ();
374- int shardId = request .shardId ().id ();
375- logger .trace ("failure on replica [{}][{}], action [{}], request [{}]" , t , index , shardId , actionName , request );
376- if (ignoreReplicaException (t ) == false ) {
377- IndexService indexService = indicesService .indexService (index );
378- if (indexService == null ) {
379- logger .debug ("ignoring failed replica {}[{}] because index was already removed." , index , shardId );
380- return ;
381- }
382- IndexShard indexShard = indexService .getShardOrNull (shardId );
383- if (indexShard == null ) {
384- logger .debug ("ignoring failed replica {}[{}] because index was already removed." , index , shardId );
385- return ;
386- }
387- indexShard .failShard (actionName + " failed on replica" , t );
388362 }
389363 }
390364
@@ -401,7 +375,7 @@ protected void responseWithFailure(Throwable t) {
401375 protected void doRun () throws Exception {
402376 setPhase (task , "replica" );
403377 assert request .shardId () != null : "request shardId must be set" ;
404- try (Releasable ignored = getIndexShardReferenceOnReplica (request .shardId ())) {
378+ try (Releasable ignored = getIndexShardReferenceOnReplica (request .shardId (), request . primaryTerm () )) {
405379 shardOperationOnReplica (request );
406380 if (logger .isTraceEnabled ()) {
407381 logger .trace ("action [{}] completed on shard [{}] for request [{}]" , transportReplicaAction , request .shardId (), request );
@@ -707,7 +681,6 @@ protected void doRun() throws Exception {
707681 indexShardReference = getIndexShardReferenceOnPrimary (shardId );
708682 if (indexShardReference .isRelocated () == false ) {
709683 executeLocally ();
710-
711684 } else {
712685 executeRemotely ();
713686 }
@@ -716,6 +689,7 @@ protected void doRun() throws Exception {
716689 private void executeLocally () throws Exception {
717690 // execute locally
718691 Tuple <Response , ReplicaRequest > primaryResponse = shardOperationOnPrimary (state .metaData (), request );
692+ primaryResponse .v2 ().primaryTerm (indexShardReference .opPrimaryTerm ());
719693 if (logger .isTraceEnabled ()) {
720694 logger .trace ("action [{}] completed on shard [{}] for request [{}] with cluster state version [{}]" , transportPrimaryAction , shardId , request , state .version ());
721695 }
@@ -825,17 +799,17 @@ void finishBecauseUnavailable(ShardId shardId, String message) {
825799 protected IndexShardReference getIndexShardReferenceOnPrimary (ShardId shardId ) {
826800 IndexService indexService = indicesService .indexServiceSafe (shardId .getIndex ());
827801 IndexShard indexShard = indexService .getShard (shardId .id ());
828- return new IndexShardReferenceImpl (indexShard , true );
802+ return IndexShardReferenceImpl . createOnPrimary (indexShard );
829803 }
830804
831805 /**
832806 * returns a new reference to {@link IndexShard} on a node that the request is replicated to. The reference is closed as soon as
833807 * replication is completed on the node.
834808 */
835- protected IndexShardReference getIndexShardReferenceOnReplica (ShardId shardId ) {
809+ protected IndexShardReference getIndexShardReferenceOnReplica (ShardId shardId , long primaryTerm ) {
836810 IndexService indexService = indicesService .indexServiceSafe (shardId .getIndex ());
837811 IndexShard indexShard = indexService .getShard (shardId .id ());
838- return new IndexShardReferenceImpl (indexShard , false );
812+ return IndexShardReferenceImpl . createOnReplica (indexShard , primaryTerm );
839813 }
840814
841815 /**
@@ -1098,9 +1072,13 @@ private void doFinish() {
10981072 totalShards ,
10991073 success .get (),
11001074 failuresArray
1101-
11021075 )
11031076 );
1077+ if (logger .isTraceEnabled ()) {
1078+ logger .trace ("finished replicating action [{}], request [{}], shardInfo [{}]" , actionName , replicaRequest ,
1079+ finalResponse .getShardInfo ());
1080+ }
1081+
11041082 try {
11051083 channel .sendResponse (finalResponse );
11061084 } catch (IOException responseException ) {
@@ -1125,22 +1103,33 @@ interface IndexShardReference extends Releasable {
11251103 boolean isRelocated ();
11261104 void failShard (String reason , @ Nullable Throwable e );
11271105 ShardRouting routingEntry ();
1106+
1107+ /** returns the primary term of the current operation */
1108+ long opPrimaryTerm ();
11281109 }
11291110
11301111 static final class IndexShardReferenceImpl implements IndexShardReference {
11311112
11321113 private final IndexShard indexShard ;
11331114 private final Releasable operationLock ;
11341115
1135- IndexShardReferenceImpl (IndexShard indexShard , boolean primaryAction ) {
1116+ private IndexShardReferenceImpl (IndexShard indexShard , long primaryTerm ) {
11361117 this .indexShard = indexShard ;
1137- if (primaryAction ) {
1118+ if (primaryTerm < 0 ) {
11381119 operationLock = indexShard .acquirePrimaryOperationLock ();
11391120 } else {
1140- operationLock = indexShard .acquireReplicaOperationLock ();
1121+ operationLock = indexShard .acquireReplicaOperationLock (primaryTerm );
11411122 }
11421123 }
11431124
1125+ static IndexShardReferenceImpl createOnPrimary (IndexShard indexShard ) {
1126+ return new IndexShardReferenceImpl (indexShard , -1 );
1127+ }
1128+
1129+ static IndexShardReferenceImpl createOnReplica (IndexShard indexShard , long primaryTerm ) {
1130+ return new IndexShardReferenceImpl (indexShard , primaryTerm );
1131+ }
1132+
11441133 @ Override
11451134 public void close () {
11461135 operationLock .close ();
@@ -1160,6 +1149,11 @@ public void failShard(String reason, @Nullable Throwable e) {
11601149 public ShardRouting routingEntry () {
11611150 return indexShard .routingEntry ();
11621151 }
1152+
1153+ @ Override
1154+ public long opPrimaryTerm () {
1155+ return indexShard .getPrimaryTerm ();
1156+ }
11631157 }
11641158
11651159 protected final void processAfterWrite (boolean refresh , IndexShard indexShard , Translog .Location location ) {
0 commit comments