52
52
import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
53
53
import org .elasticsearch .common .util .concurrent .ConcurrentCollections ;
54
54
import org .elasticsearch .common .util .concurrent .ThreadContext ;
55
- import org .elasticsearch .index .Index ;
56
55
import org .elasticsearch .index .IndexService ;
57
56
import org .elasticsearch .index .engine .VersionConflictEngineException ;
58
57
import org .elasticsearch .index .shard .IndexShard ;
@@ -359,32 +358,7 @@ public void onTimeout(TimeValue timeout) {
359
358
}
360
359
});
361
360
} else {
362
- try {
363
- failReplicaIfNeeded (t );
364
- } catch (Throwable unexpected ) {
365
- logger .error ("{} unexpected error while failing replica" , unexpected , request .shardId ().id ());
366
- } finally {
367
361
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 );
388
362
}
389
363
}
390
364
@@ -401,7 +375,7 @@ protected void responseWithFailure(Throwable t) {
401
375
protected void doRun () throws Exception {
402
376
setPhase (task , "replica" );
403
377
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 () )) {
405
379
shardOperationOnReplica (request );
406
380
if (logger .isTraceEnabled ()) {
407
381
logger .trace ("action [{}] completed on shard [{}] for request [{}]" , transportReplicaAction , request .shardId (), request );
@@ -707,7 +681,6 @@ protected void doRun() throws Exception {
707
681
indexShardReference = getIndexShardReferenceOnPrimary (shardId );
708
682
if (indexShardReference .isRelocated () == false ) {
709
683
executeLocally ();
710
-
711
684
} else {
712
685
executeRemotely ();
713
686
}
@@ -716,6 +689,7 @@ protected void doRun() throws Exception {
716
689
private void executeLocally () throws Exception {
717
690
// execute locally
718
691
Tuple <Response , ReplicaRequest > primaryResponse = shardOperationOnPrimary (state .metaData (), request );
692
+ primaryResponse .v2 ().primaryTerm (indexShardReference .opPrimaryTerm ());
719
693
if (logger .isTraceEnabled ()) {
720
694
logger .trace ("action [{}] completed on shard [{}] for request [{}] with cluster state version [{}]" , transportPrimaryAction , shardId , request , state .version ());
721
695
}
@@ -825,17 +799,17 @@ void finishBecauseUnavailable(ShardId shardId, String message) {
825
799
protected IndexShardReference getIndexShardReferenceOnPrimary (ShardId shardId ) {
826
800
IndexService indexService = indicesService .indexServiceSafe (shardId .getIndex ());
827
801
IndexShard indexShard = indexService .getShard (shardId .id ());
828
- return new IndexShardReferenceImpl (indexShard , true );
802
+ return IndexShardReferenceImpl . createOnPrimary (indexShard );
829
803
}
830
804
831
805
/**
832
806
* returns a new reference to {@link IndexShard} on a node that the request is replicated to. The reference is closed as soon as
833
807
* replication is completed on the node.
834
808
*/
835
- protected IndexShardReference getIndexShardReferenceOnReplica (ShardId shardId ) {
809
+ protected IndexShardReference getIndexShardReferenceOnReplica (ShardId shardId , long primaryTerm ) {
836
810
IndexService indexService = indicesService .indexServiceSafe (shardId .getIndex ());
837
811
IndexShard indexShard = indexService .getShard (shardId .id ());
838
- return new IndexShardReferenceImpl (indexShard , false );
812
+ return IndexShardReferenceImpl . createOnReplica (indexShard , primaryTerm );
839
813
}
840
814
841
815
/**
@@ -1098,9 +1072,13 @@ private void doFinish() {
1098
1072
totalShards ,
1099
1073
success .get (),
1100
1074
failuresArray
1101
-
1102
1075
)
1103
1076
);
1077
+ if (logger .isTraceEnabled ()) {
1078
+ logger .trace ("finished replicating action [{}], request [{}], shardInfo [{}]" , actionName , replicaRequest ,
1079
+ finalResponse .getShardInfo ());
1080
+ }
1081
+
1104
1082
try {
1105
1083
channel .sendResponse (finalResponse );
1106
1084
} catch (IOException responseException ) {
@@ -1125,22 +1103,33 @@ interface IndexShardReference extends Releasable {
1125
1103
boolean isRelocated ();
1126
1104
void failShard (String reason , @ Nullable Throwable e );
1127
1105
ShardRouting routingEntry ();
1106
+
1107
+ /** returns the primary term of the current operation */
1108
+ long opPrimaryTerm ();
1128
1109
}
1129
1110
1130
1111
static final class IndexShardReferenceImpl implements IndexShardReference {
1131
1112
1132
1113
private final IndexShard indexShard ;
1133
1114
private final Releasable operationLock ;
1134
1115
1135
- IndexShardReferenceImpl (IndexShard indexShard , boolean primaryAction ) {
1116
+ private IndexShardReferenceImpl (IndexShard indexShard , long primaryTerm ) {
1136
1117
this .indexShard = indexShard ;
1137
- if (primaryAction ) {
1118
+ if (primaryTerm < 0 ) {
1138
1119
operationLock = indexShard .acquirePrimaryOperationLock ();
1139
1120
} else {
1140
- operationLock = indexShard .acquireReplicaOperationLock ();
1121
+ operationLock = indexShard .acquireReplicaOperationLock (primaryTerm );
1141
1122
}
1142
1123
}
1143
1124
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
+
1144
1133
@ Override
1145
1134
public void close () {
1146
1135
operationLock .close ();
@@ -1160,6 +1149,11 @@ public void failShard(String reason, @Nullable Throwable e) {
1160
1149
public ShardRouting routingEntry () {
1161
1150
return indexShard .routingEntry ();
1162
1151
}
1152
+
1153
+ @ Override
1154
+ public long opPrimaryTerm () {
1155
+ return indexShard .getPrimaryTerm ();
1156
+ }
1163
1157
}
1164
1158
1165
1159
protected final void processAfterWrite (boolean refresh , IndexShard indexShard , Translog .Location location ) {
0 commit comments