2222import org .elasticsearch .ElasticsearchException ;
2323import org .elasticsearch .ElasticsearchIllegalStateException ;
2424import org .elasticsearch .ExceptionsHelper ;
25- import org .elasticsearch .action .*;
25+ import org .elasticsearch .action .ActionListener ;
26+ import org .elasticsearch .action .ActionResponse ;
27+ import org .elasticsearch .action .UnavailableShardsException ;
28+ import org .elasticsearch .action .WriteConsistencyLevel ;
2629import org .elasticsearch .action .support .TransportAction ;
2730import org .elasticsearch .action .support .TransportActions ;
2831import org .elasticsearch .cluster .ClusterChangedEvent ;
4447import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
4548import org .elasticsearch .index .engine .DocumentAlreadyExistsException ;
4649import org .elasticsearch .index .engine .VersionConflictEngineException ;
50+ import org .elasticsearch .index .service .IndexService ;
51+ import org .elasticsearch .index .shard .service .IndexShard ;
4752import org .elasticsearch .indices .IndicesService ;
4853import org .elasticsearch .node .NodeClosedException ;
4954import org .elasticsearch .rest .RestStatus ;
5459import java .util .concurrent .atomic .AtomicBoolean ;
5560import java .util .concurrent .atomic .AtomicInteger ;
5661
57- import static org .elasticsearch .ExceptionsHelper .detailedMessage ;
58-
5962/**
6063 */
61- public abstract class TransportShardReplicationOperationAction <Request extends ShardReplicationOperationRequest , ReplicaRequest extends ActionRequest , Response extends ActionResponse > extends TransportAction <Request , Response > {
64+ public abstract class TransportShardReplicationOperationAction <Request extends ShardReplicationOperationRequest , ReplicaRequest extends ShardReplicationOperationRequest , Response extends ActionResponse > extends TransportAction <Request , Response > {
6265
6366 protected final TransportService transportService ;
6467 protected final ClusterService clusterService ;
@@ -242,7 +245,12 @@ public boolean isForceExecution() {
242245
243246 @ Override
244247 public void messageReceived (final ReplicaOperationRequest request , final TransportChannel channel ) throws Exception {
245- shardOperationOnReplica (request );
248+ try {
249+ shardOperationOnReplica (request );
250+ } catch (Throwable t ) {
251+ failReplicaIfNeeded (request .request .index (), request .shardId , t );
252+ throw t ;
253+ }
246254 channel .sendResponse (TransportResponse .Empty .INSTANCE );
247255 }
248256 }
@@ -700,7 +708,7 @@ void performOnReplica(final PrimaryResponse<Response, ReplicaRequest> response,
700708
701709 final ReplicaOperationRequest shardRequest = new ReplicaOperationRequest (shardIt .shardId ().id (), response .replicaRequest ());
702710 if (!nodeId .equals (clusterState .nodes ().localNodeId ())) {
703- DiscoveryNode node = clusterState .nodes ().get (nodeId );
711+ final DiscoveryNode node = clusterState .nodes ().get (nodeId );
704712 transportService .sendRequest (node , transportReplicaAction , shardRequest , transportOptions , new EmptyTransportResponseHandler (ThreadPool .Names .SAME ) {
705713 @ Override
706714 public void handleResponse (TransportResponse .Empty vResponse ) {
@@ -710,9 +718,9 @@ public void handleResponse(TransportResponse.Empty vResponse) {
710718 @ Override
711719 public void handleException (TransportException exp ) {
712720 if (!ignoreReplicaException (exp .unwrapCause ())) {
713- logger .warn ("Failed to perform " + transportAction + " on replica " + shardIt .shardId (), exp );
721+ logger .warn ("Failed to perform " + transportAction + " on remote replica " + node + shardIt .shardId (), exp );
714722 shardStateAction .shardFailed (shard , indexMetaData .getUUID (),
715- "Failed to perform [" + transportAction + "] on replica, message [" + detailedMessage (exp ) + "]" );
723+ "Failed to perform [" + transportAction + "] on replica, message [" + ExceptionsHelper . detailedMessage (exp ) + "]" );
716724 }
717725 finishIfPossible ();
718726 }
@@ -733,11 +741,7 @@ public void run() {
733741 try {
734742 shardOperationOnReplica (shardRequest );
735743 } catch (Throwable e ) {
736- if (!ignoreReplicaException (e )) {
737- logger .warn ("Failed to perform " + transportAction + " on replica " + shardIt .shardId (), e );
738- shardStateAction .shardFailed (shard , indexMetaData .getUUID (),
739- "Failed to perform [" + transportAction + "] on replica, message [" + detailedMessage (e ) + "]" );
740- }
744+ failReplicaIfNeeded (shard .index (), shard .id (), e );
741745 }
742746 if (counter .decrementAndGet () == 0 ) {
743747 listener .onResponse (response .response ());
@@ -751,11 +755,7 @@ public boolean isForceExecution() {
751755 }
752756 });
753757 } catch (Throwable e ) {
754- if (!ignoreReplicaException (e )) {
755- logger .warn ("Failed to perform " + transportAction + " on replica " + shardIt .shardId (), e );
756- shardStateAction .shardFailed (shard , indexMetaData .getUUID (),
757- "Failed to perform [" + transportAction + "] on replica, message [" + detailedMessage (e ) + "]" );
758- }
758+ failReplicaIfNeeded (shard .index (), shard .id (), e );
759759 // we want to decrement the counter here, in teh failure handling, cause we got rejected
760760 // from executing on the thread pool
761761 if (counter .decrementAndGet () == 0 ) {
@@ -766,18 +766,31 @@ public boolean isForceExecution() {
766766 try {
767767 shardOperationOnReplica (shardRequest );
768768 } catch (Throwable e ) {
769- if (!ignoreReplicaException (e )) {
770- logger .warn ("Failed to perform " + transportAction + " on replica" + shardIt .shardId (), e );
771- shardStateAction .shardFailed (shard , indexMetaData .getUUID (),
772- "Failed to perform [" + transportAction + "] on replica, message [" + detailedMessage (e ) + "]" );
773- }
769+ failReplicaIfNeeded (shard .index (), shard .id (), e );
774770 }
775771 if (counter .decrementAndGet () == 0 ) {
776772 listener .onResponse (response .response ());
777773 }
778774 }
779775 }
780776 }
777+
778+ }
779+
780+ private void failReplicaIfNeeded (String index , int shardId , Throwable t ) {
781+ if (!ignoreReplicaException (t )) {
782+ IndexService indexService = indicesService .indexService (index );
783+ if (indexService == null ) {
784+ logger .debug ("ignoring failed replica [{}][{}] because index was already removed." , index , shardId );
785+ return ;
786+ }
787+ IndexShard indexShard = indexService .shard (shardId );
788+ if (indexShard == null ) {
789+ logger .debug ("ignoring failed replica [{}][{}] because index was already removed." , index , shardId );
790+ return ;
791+ }
792+ indexShard .failShard (transportAction + " failed on replica" , t );
793+ }
781794 }
782795
783796 public static class PrimaryResponse <Response , ReplicaRequest > {
0 commit comments