From 18f15f0a6fda1c24fd3c296ab94bf9fe63bf9183 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sun, 25 Sep 2011 22:17:09 +0300 Subject: [PATCH] use index iteration over iterator --- .../elasticsearch/cluster/routing/RoutingNodes.java | 4 +++- .../decider/ClusterRebalanceAllocationDecider.java | 12 +++++++++--- .../ConcurrentRebalanceAllocationDecider.java | 7 +++++-- .../RebalanceOnlyWhenActiveAllocationDecider.java | 4 ++-- .../decider/ThrottlingAllocationDecider.java | 10 ++++++++-- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java index fdedd008a4d..014b2d23aec 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java @@ -176,7 +176,9 @@ public class RoutingNodes implements Iterable { public MutableShardRouting findPrimaryForReplica(ShardRouting shard) { assert !shard.primary(); for (RoutingNode routingNode : nodesToShards.values()) { - for (MutableShardRouting shardRouting : routingNode) { + List shards = routingNode.shards(); + for (int i = 0; i < shards.size(); i++) { + MutableShardRouting shardRouting = shards.get(i); if (shardRouting.shardId().equals(shard.shardId()) && shardRouting.primary()) { return shardRouting; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java index 159984e4da8..b4dcac950a8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java @@ -26,6 +26,8 @@ import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import java.util.List; + public class ClusterRebalanceAllocationDecider extends AllocationDecider { public static enum ClusterRebalanceType { @@ -49,7 +51,7 @@ public class ClusterRebalanceAllocationDecider extends AllocationDecider { logger.warn("[cluster.routing.allocation.allow_rebalance] has a wrong value {}, defaulting to 'indices_all_active'", allowRebalance); type = ClusterRebalanceType.INDICES_ALL_ACTIVE; } - logger.debug("using [allow_rebalance] with [{}]", type.toString().toLowerCase()); + logger.debug("using [cluster.routing.allocation.allow_rebalance] with [{}]", type.toString().toLowerCase()); } @Override public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { @@ -60,7 +62,9 @@ public class ClusterRebalanceAllocationDecider extends AllocationDecider { } } for (RoutingNode node : allocation.routingNodes()) { - for (MutableShardRouting shard : node) { + List shards = node.shards(); + for (int i = 0; i < shards.size(); i++) { + MutableShardRouting shard = shards.get(i); if (shard.primary() && !shard.active()) { return false; } @@ -73,7 +77,9 @@ public class ClusterRebalanceAllocationDecider extends AllocationDecider { return false; } for (RoutingNode node : allocation.routingNodes()) { - for (MutableShardRouting shard : node) { + List shards = node.shards(); + for (int i = 0; i < shards.size(); i++) { + MutableShardRouting shard = shards.get(i); if (!shard.active()) { return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java index 1a9bb313b35..39b29817451 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java @@ -29,6 +29,8 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.node.settings.NodeSettingsService; +import java.util.List; + public class ConcurrentRebalanceAllocationDecider extends AllocationDecider { static { @@ -62,8 +64,9 @@ public class ConcurrentRebalanceAllocationDecider extends AllocationDecider { } int rebalance = 0; for (RoutingNode node : allocation.routingNodes()) { - for (MutableShardRouting shard : node) { - if (shard.state() == ShardRoutingState.RELOCATING) { + List shards = node.shards(); + for (int i = 0; i < shards.size(); i++) { + if (shards.get(i).state() == ShardRoutingState.RELOCATING) { rebalance++; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/RebalanceOnlyWhenActiveAllocationDecider.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/RebalanceOnlyWhenActiveAllocationDecider.java index 32c6df5082d..23dc2cd4a32 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/RebalanceOnlyWhenActiveAllocationDecider.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/RebalanceOnlyWhenActiveAllocationDecider.java @@ -40,8 +40,8 @@ public class RebalanceOnlyWhenActiveAllocationDecider extends AllocationDecider List shards = allocation.routingNodes().shardsRoutingFor(shardRouting); // its ok to check for active here, since in relocation, a shard is split into two in routing // nodes, once relocating, and one initializing - for (ShardRouting allShard : shards) { - if (!allShard.active()) { + for (int i = 0; i < shards.size(); i++) { + if (!shards.get(i).active()) { return false; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ThrottlingAllocationDecider.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ThrottlingAllocationDecider.java index e0da196bae3..ff9b0fa1dad 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ThrottlingAllocationDecider.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ThrottlingAllocationDecider.java @@ -29,6 +29,8 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.node.settings.NodeSettingsService; +import java.util.List; + /** */ public class ThrottlingAllocationDecider extends AllocationDecider { @@ -65,7 +67,9 @@ public class ThrottlingAllocationDecider extends AllocationDecider { // primary is unassigned, means we are going to do recovery from gateway // count *just the primary* currently doing recovery on the node and check against concurrent_recoveries int primariesInRecovery = 0; - for (MutableShardRouting shard : node) { + List shards = node.shards(); + for (int i = 0; i < shards.size(); i++) { + MutableShardRouting shard = shards.get(i); if (shard.state() == ShardRoutingState.INITIALIZING && shard.primary()) { primariesInRecovery++; } @@ -82,7 +86,9 @@ public class ThrottlingAllocationDecider extends AllocationDecider { // count the number of recoveries on the node, its for both target (INITIALIZING) and source (RELOCATING) int currentRecoveries = 0; - for (MutableShardRouting shard : node) { + List shards = node.shards(); + for (int i = 0; i < shards.size(); i++) { + MutableShardRouting shard = shards.get(i); if (shard.state() == ShardRoutingState.INITIALIZING || shard.state() == ShardRoutingState.RELOCATING) { currentRecoveries++; }