diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java index 449867b4fa9..4fda8391d2b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java @@ -20,6 +20,7 @@ package org.elasticsearch.cluster.routing; import com.carrotsearch.hppc.IntSet; +import com.google.common.base.Predicate; import com.google.common.collect.*; import org.elasticsearch.cluster.*; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -27,7 +28,6 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.index.IndexNotFoundException; -import org.elasticsearch.index.shard.ShardId; import java.io.IOException; import java.util.ArrayList; @@ -162,28 +162,7 @@ public class RoutingTable implements Iterable, Diffable set = new ArrayList<>(); - for (String index : indices) { - IndexRoutingTable indexRoutingTable = index(index); - if (indexRoutingTable == null) { - continue; - // we simply ignore indices that don't exists (make sense for operations that use it currently) - } - for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { - for (ShardRouting shardRouting : indexShardRoutingTable) { - if (shardRouting.active()) { - set.add(shardRouting.shardsIt()); - if (includeRelocationTargets && shardRouting.relocating()) { - set.add(new PlainShardIterator(shardRouting.shardId(), Collections.singletonList(shardRouting.buildTargetRelocatingShard()))); - } - } else if (includeEmpty) { // we need this for counting properly, just make it an empty one - set.add(new PlainShardIterator(shardRouting.shardId(), Collections.emptyList())); - } - } - } - } - return new GroupShardsIterator(set); + return allSatisfyingPredicateShardsGrouped(indices, includeEmpty, includeRelocationTargets, ACTIVE_PREDICATE); } public GroupShardsIterator allAssignedShardsGrouped(String[] indices, boolean includeEmpty) { @@ -198,6 +177,25 @@ public class RoutingTable implements Iterable, Diffable ACTIVE_PREDICATE = new Predicate() { + @Override + public boolean apply(ShardRouting shardRouting) { + return shardRouting.active(); + } + }; + + private static Predicate ASSIGNED_PREDICATE = new Predicate() { + @Override + public boolean apply(ShardRouting shardRouting) { + return shardRouting.assignedToNode(); + } + }; + + // TODO: replace with JDK 8 native java.util.function.Predicate + private GroupShardsIterator allSatisfyingPredicateShardsGrouped(String[] indices, boolean includeEmpty, boolean includeRelocationTargets, Predicate predicate) { // use list here since we need to maintain identity across shards ArrayList set = new ArrayList<>(); for (String index : indices) { @@ -208,7 +206,7 @@ public class RoutingTable implements Iterable, Diffable