{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/46535244","id":46535244,"diff_hunk":"@@ -670,134 +706,61 @@ protected Releasable getIndexShardOperationsCounter(ShardId shardId) {\n         return new IndexShardReference(indexShard);\n     }\n \n-    private void failReplicaIfNeeded(String index, int shardId, Throwable t) {\n-        logger.trace(\"failure on replica [{}][{}]\", t, index, shardId);\n-        if (ignoreReplicaException(t) == false) {\n-            IndexService indexService = indicesService.indexService(index);\n-            if (indexService == null) {\n-                logger.debug(\"ignoring failed replica [{}][{}] because index was already removed.\", index, shardId);\n-                return;\n-            }\n-            IndexShard indexShard = indexService.getShardOrNull(shardId);\n-            if (indexShard == null) {\n-                logger.debug(\"ignoring failed replica [{}][{}] because index was already removed.\", index, shardId);\n-                return;\n-            }\n-            indexShard.failShard(actionName + \" failed on replica\", t);\n-        }\n-    }\n-\n     /**\n-     * inner class is responsible for send the requests to all replica shards and manage the responses\n+     * Responsible for sending replica requests (see {@link AsyncReplicaAction}) to nodes with replica copy, including\n+     * relocating copies\n      */\n     final class ReplicationPhase extends AbstractRunnable {\n \n         private final ReplicaRequest replicaRequest;\n         private final Response finalResponse;\n         private final ShardIterator shardIt;\n-        private final ActionListener<Response> listener;\n+        private final TransportChannel channel;\n         private final AtomicBoolean finished = new AtomicBoolean(false);\n         private final AtomicInteger success = new AtomicInteger(1); // We already wrote into the primary shard\n         private final ConcurrentMap<String, Throwable> shardReplicaFailures = ConcurrentCollections.newConcurrentMap();\n-        private final IndexMetaData indexMetaData;\n-        private final ShardRouting originalPrimaryShard;\n         private final AtomicInteger pending;\n         private final int totalShards;\n-        private final ClusterStateObserver observer;\n+        private final ClusterState clusterState;\n         private final Releasable indexShardReference;\n         private final TimeValue shardFailedTimeout;\n+        private final boolean executeOnReplica;\n+        private final String indexUUID;\n \n-        /**\n-         * the constructor doesn't take any action, just calculates state. Call {@link #run()} to start\n-         * replicating.\n-         */\n-        public ReplicationPhase(ShardIterator originalShardIt, ReplicaRequest replicaRequest, Response finalResponse,\n-                                ClusterStateObserver observer, ShardRouting originalPrimaryShard,\n-                                InternalRequest internalRequest, ActionListener<Response> listener, Releasable indexShardReference,\n+        public ReplicationPhase(ReplicaRequest replicaRequest, Response finalResponse, ShardId shardId,\n+                                IndexMetaData indexMetaData, TransportChannel channel, Releasable indexShardReference,\n                                 TimeValue shardFailedTimeout) {\n             this.replicaRequest = replicaRequest;\n-            this.listener = listener;\n+            this.channel = channel;\n             this.finalResponse = finalResponse;\n-            this.originalPrimaryShard = originalPrimaryShard;\n-            this.observer = observer;\n-            indexMetaData = observer.observedState().metaData().index(internalRequest.concreteIndex());\n             this.indexShardReference = indexShardReference;\n             this.shardFailedTimeout = shardFailedTimeout;\n-\n-            ShardRouting shard;\n-            // we double check on the state, if it got changed we need to make sure we take the latest one cause\n-            // maybe a replica shard started its recovery process and we need to apply it there...\n-\n-            // we also need to make sure if the new state has a new primary shard (that we indexed to before) started\n-            // and assigned to another node (while the indexing happened). In that case, we want to apply it on the\n-            // new primary shard as well...\n-            ClusterState newState = clusterService.state();\n-\n-            int numberOfUnassignedOrIgnoredReplicas = 0;\n+            this.executeOnReplica = shouldExecuteReplication(indexMetaData.getSettings());\n+            this.indexUUID = indexMetaData.getIndexUUID();\n+\n+            // we get a new state to route replication operations based on the latest shard routings and states\n+            this.clusterState = clusterService.state();\n+            this.shardIt = clusterService.operationRouting().shards(clusterState, shardId.getIndex(), shardId.id()).shardsIt();\n+            // we calculate number of target nodes to send replication operations, including nodes with relocating shards\n+            int numberOfIgnoredShardInstances = 0;\n             int numberOfPendingShardInstances = 0;\n-            if (observer.observedState() != newState) {\n-                observer.reset(newState);\n-                shardIt = shards(newState, internalRequest);\n-                while ((shard = shardIt.nextOrNull()) != null) {\n-                    if (shard.primary()) {\n-                        if (originalPrimaryShard.currentNodeId().equals(shard.currentNodeId()) == false) {\n-                            // there is a new primary, we'll have to replicate to it.\n-                            numberOfPendingShardInstances++;\n-                        }\n-                        if (shard.relocating()) {\n-                            numberOfPendingShardInstances++;\n-                        }\n-                    } else if (shouldExecuteReplication(indexMetaData.getSettings()) == false) {\n-                        // If the replicas use shadow replicas, there is no reason to\n-                        // perform the action on the replica, so skip it and\n-                        // immediately return\n-\n-                        // this delays mapping updates on replicas because they have\n-                        // to wait until they get the new mapping through the cluster\n-                        // state, which is why we recommend pre-defined mappings for\n-                        // indices using shadow replicas\n-                        numberOfUnassignedOrIgnoredReplicas++;\n-                    } else if (shard.unassigned()) {\n-                        numberOfUnassignedOrIgnoredReplicas++;\n-                    } else if (shard.relocating()) {\n-                        // we need to send to two copies\n-                        numberOfPendingShardInstances += 2;\n-                    } else {\n+            String localNodeId = clusterState.nodes().localNodeId();\n+            for (ShardRouting shard : shardIt.asUnordered()) {","path":"core/src/main/java/org/elasticsearch/action/support/replication/TransportReplicationAction.java","position":743,"original_position":743,"commit_id":"2dc693856f7ffe0c337def828528f2bc48c10507","original_commit_id":"2dc693856f7ffe0c337def828528f2bc48c10507","user":{"login":"bleskes","id":1006375,"avatar_url":"https://avatars.githubusercontent.com/u/1006375?v=3","gravatar_id":"","url":"https://api.github.com/users/bleskes","html_url":"https://github.com/bleskes","followers_url":"https://api.github.com/users/bleskes/followers","following_url":"https://api.github.com/users/bleskes/following{/other_user}","gists_url":"https://api.github.com/users/bleskes/gists{/gist_id}","starred_url":"https://api.github.com/users/bleskes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bleskes/subscriptions","organizations_url":"https://api.github.com/users/bleskes/orgs","repos_url":"https://api.github.com/users/bleskes/repos","events_url":"https://api.github.com/users/bleskes/events{/privacy}","received_events_url":"https://api.github.com/users/bleskes/received_events","type":"User","site_admin":false},"body":"let's just skip the iterator and use IndexShardRoutingTable.shards() (put it in a variable and use it in do run as well)","created_at":"2015-12-03T10:31:34Z","updated_at":"2015-12-03T10:31:34Z","html_url":"https://github.com/elastic/elasticsearch/pull/14852#discussion_r46535244","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/46535244"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/14852#discussion_r46535244"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852","id":51182662,"html_url":"https://github.com/elastic/elasticsearch/pull/14852","diff_url":"https://github.com/elastic/elasticsearch/pull/14852.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/14852.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/14852","number":14852,"state":"open","locked":false,"title":"Decouple routing and primary operation logic in TransportReplicationAction","user":{"login":"areek","id":753679,"avatar_url":"https://avatars.githubusercontent.com/u/753679?v=3","gravatar_id":"","url":"https://api.github.com/users/areek","html_url":"https://github.com/areek","followers_url":"https://api.github.com/users/areek/followers","following_url":"https://api.github.com/users/areek/following{/other_user}","gists_url":"https://api.github.com/users/areek/gists{/gist_id}","starred_url":"https://api.github.com/users/areek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/areek/subscriptions","organizations_url":"https://api.github.com/users/areek/orgs","repos_url":"https://api.github.com/users/areek/repos","events_url":"https://api.github.com/users/areek/events{/privacy}","received_events_url":"https://api.github.com/users/areek/received_events","type":"User","site_admin":false},"body":"Currently the logic to route an operation to the primary and the local primary operation is tightly coupled in `TransportReplicationAction#PrimaryPhase`. This change decouples the routing and retrying logic to `ReroutePhase` and the primary operation to `PrimaryPhase`, allowing to perform more informed shard level operations in the future.","created_at":"2015-11-19T05:43:51Z","updated_at":"2015-12-03T10:31:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8bc222eb6253deb95b5d42111611f0de69acf35f","assignee":{"login":"bleskes","id":1006375,"avatar_url":"https://avatars.githubusercontent.com/u/1006375?v=3","gravatar_id":"","url":"https://api.github.com/users/bleskes","html_url":"https://github.com/bleskes","followers_url":"https://api.github.com/users/bleskes/followers","following_url":"https://api.github.com/users/bleskes/following{/other_user}","gists_url":"https://api.github.com/users/bleskes/gists{/gist_id}","starred_url":"https://api.github.com/users/bleskes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bleskes/subscriptions","organizations_url":"https://api.github.com/users/bleskes/orgs","repos_url":"https://api.github.com/users/bleskes/repos","events_url":"https://api.github.com/users/bleskes/events{/privacy}","received_events_url":"https://api.github.com/users/bleskes/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/14852/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/2dc693856f7ffe0c337def828528f2bc48c10507","head":{"label":"areek:refactor_primary_phase","ref":"refactor_primary_phase","sha":"2dc693856f7ffe0c337def828528f2bc48c10507","user":{"login":"areek","id":753679,"avatar_url":"https://avatars.githubusercontent.com/u/753679?v=3","gravatar_id":"","url":"https://api.github.com/users/areek","html_url":"https://github.com/areek","followers_url":"https://api.github.com/users/areek/followers","following_url":"https://api.github.com/users/areek/following{/other_user}","gists_url":"https://api.github.com/users/areek/gists{/gist_id}","starred_url":"https://api.github.com/users/areek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/areek/subscriptions","organizations_url":"https://api.github.com/users/areek/orgs","repos_url":"https://api.github.com/users/areek/repos","events_url":"https://api.github.com/users/areek/events{/privacy}","received_events_url":"https://api.github.com/users/areek/received_events","type":"User","site_admin":false},"repo":{"id":16661385,"name":"elasticsearch","full_name":"areek/elasticsearch","owner":{"login":"areek","id":753679,"avatar_url":"https://avatars.githubusercontent.com/u/753679?v=3","gravatar_id":"","url":"https://api.github.com/users/areek","html_url":"https://github.com/areek","followers_url":"https://api.github.com/users/areek/followers","following_url":"https://api.github.com/users/areek/following{/other_user}","gists_url":"https://api.github.com/users/areek/gists{/gist_id}","starred_url":"https://api.github.com/users/areek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/areek/subscriptions","organizations_url":"https://api.github.com/users/areek/orgs","repos_url":"https://api.github.com/users/areek/repos","events_url":"https://api.github.com/users/areek/events{/privacy}","received_events_url":"https://api.github.com/users/areek/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/areek/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/areek/elasticsearch","forks_url":"https://api.github.com/repos/areek/elasticsearch/forks","keys_url":"https://api.github.com/repos/areek/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/areek/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/areek/elasticsearch/teams","hooks_url":"https://api.github.com/repos/areek/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/areek/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/areek/elasticsearch/events","assignees_url":"https://api.github.com/repos/areek/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/areek/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/areek/elasticsearch/tags","blobs_url":"https://api.github.com/repos/areek/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/areek/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/areek/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/areek/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/areek/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/areek/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/areek/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/areek/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/areek/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/areek/elasticsearch/subscription","commits_url":"https://api.github.com/repos/areek/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/areek/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/areek/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/areek/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/areek/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/areek/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/areek/elasticsearch/merges","archive_url":"https://api.github.com/repos/areek/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/areek/elasticsearch/downloads","issues_url":"https://api.github.com/repos/areek/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/areek/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/areek/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/areek/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/areek/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/areek/elasticsearch/releases{/id}","created_at":"2014-02-09T06:24:53Z","updated_at":"2015-05-01T17:04:19Z","pushed_at":"2015-12-02T19:32:42Z","git_url":"git://github.com/areek/elasticsearch.git","ssh_url":"git@github.com:areek/elasticsearch.git","clone_url":"https://github.com/areek/elasticsearch.git","svn_url":"https://github.com/areek/elasticsearch","homepage":"http://elasticsearch.org","size":175841,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"elastic:master","ref":"master","sha":"29853744a3be46724c176647682aa26d161ec2ec","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","created_at":"2010-02-08T13:20:56Z","updated_at":"2015-12-03T08:28:18Z","pushed_at":"2015-12-03T10:08:39Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":225560,"stargazers_count":13811,"watchers_count":13811,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4580,"mirror_url":null,"open_issues_count":1260,"forks":4580,"open_issues":1260,"watchers":13811,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/14852"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/14852"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/14852/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14852/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/2dc693856f7ffe0c337def828528f2bc48c10507"}}}}