diff --git a/core/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java b/core/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java index 8a7340f2ae4..da014e15b6b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java +++ b/core/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java @@ -153,22 +153,22 @@ public class ShardStateAction extends AbstractComponent { @Override public BatchResult execute(ClusterState currentState, List tasks) throws Exception { BatchResult.Builder builder = BatchResult.builder(); - ClusterState accumulator = ClusterState.builder(currentState).build(); + List shardRoutingsToBeApplied = new ArrayList<>(tasks.size()); for (ShardRoutingEntry task : tasks) { task.processed = true; - try { - RoutingAllocation.Result result = allocationService.applyFailedShard( - currentState, - new FailedRerouteAllocation.FailedShard(task.shardRouting, task.message, task.failure)); - builder.success(task); - if (result.changed()) { - accumulator = ClusterState.builder(accumulator).routingResult(result).build(); - } - } catch (Throwable t) { - builder.failure(task, t); - } + shardRoutingsToBeApplied.add(new FailedRerouteAllocation.FailedShard(task.shardRouting, task.message, task.failure)); } - return builder.build(accumulator); + ClusterState maybeUpdatedState = currentState; + try { + RoutingAllocation.Result result = allocationService.applyFailedShards(currentState, shardRoutingsToBeApplied); + if (result.changed()) { + maybeUpdatedState = ClusterState.builder(currentState).routingResult(result).build(); + } + builder.successes(tasks); + } catch (Throwable t) { + builder.failures(tasks, t); + } + return builder.build(maybeUpdatedState); } @Override