Skip to content

Commit a06527f

Browse files
committed
Iterable instead
1 parent bcfa3d9 commit a06527f

File tree

8 files changed

+30
-24
lines changed

8 files changed

+30
-24
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
package org.elasticsearch.cluster.routing;
1111

1212
import org.elasticsearch.cluster.node.DiscoveryNode;
13-
import org.elasticsearch.common.collect.Iterators;
1413
import org.elasticsearch.common.util.Maps;
14+
import org.elasticsearch.common.util.iterable.Iterables;
1515
import org.elasticsearch.core.Nullable;
1616
import org.elasticsearch.index.Index;
1717
import org.elasticsearch.index.shard.ShardId;
@@ -214,16 +214,16 @@ void remove(ShardRouting shard) {
214214
assert invariant();
215215
}
216216

217-
public Iterator<ShardRouting> initializing() {
218-
return Iterators.assertReadOnly(initializingShards.iterator());
217+
public Iterable<ShardRouting> initializing() {
218+
return Iterables.assertReadOnly(initializingShards);
219219
}
220220

221-
public Iterator<ShardRouting> relocating() {
222-
return Iterators.assertReadOnly(relocatingShards.iterator());
221+
public Iterable<ShardRouting> relocating() {
222+
return Iterables.assertReadOnly(relocatingShards);
223223
}
224224

225-
public Iterator<ShardRouting> started() {
226-
return Iterators.assertReadOnly(startedShards.iterator());
225+
public Iterable<ShardRouting> started() {
226+
return Iterables.assertReadOnly(startedShards);
227227
}
228228

229229
/**

server/src/main/java/org/elasticsearch/cluster/routing/allocation/RoutingAllocation.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ private static Map<String, Long> unaccountedSearchableSnapshotSizes(ClusterState
175175
DiskUsage usage = clusterInfo.getNodeMostAvailableDiskUsages().get(node.nodeId());
176176
ClusterInfo.ReservedSpace reservedSpace = clusterInfo.getReservedSpace(node.nodeId(), usage != null ? usage.path() : "");
177177
long totalSize = 0;
178-
final var startedShardsIterator = node.started();
179-
while (startedShardsIterator.hasNext()) {
180-
final var shard = startedShardsIterator.next();
178+
for (ShardRouting shard : node.started()) {
181179
if (shard.getExpectedShardSize() > 0
182180
&& clusterState.metadata().indexMetadata(shard.index()).isSearchableSnapshot()
183181
&& reservedSpace.containsShardId(shard.shardId()) == false

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceComputer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public DesiredBalance compute(
298298
if (rerouteExplanation.decisions().type() != Decision.Type.NO) {
299299
final Iterator<ShardRouting> initializingShardsIterator = routingNodes.node(
300300
routingAllocation.nodes().resolveNode(command.toNode()).getId()
301-
).initializing();
301+
).initializing().iterator();
302302
assert initializingShardsIterator.hasNext();
303303
final var initializingShard = initializingShardsIterator.next();
304304
assert initializingShardsIterator.hasNext() == false
@@ -538,11 +538,11 @@ static void maybeSimulateAlreadyStartedShards(
538538
// Find all shards that are started in RoutingNodes but have no data on corresponding node in ClusterInfo
539539
final var startedShards = new ArrayList<ShardRouting>();
540540
for (var routingNode : routingNodes) {
541-
routingNode.started().forEachRemaining(shardRouting -> {
541+
for (var shardRouting : routingNode.started()) {
542542
if (clusterInfo.hasShardMoved(shardRouting)) {
543543
startedShards.add(shardRouting);
544544
}
545-
});
545+
}
546546
}
547547
if (startedShards.isEmpty()) {
548548
return;

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ public static long sizeOfUnaccountedShards(
108108
// no longer initializing because their recovery failed or was cancelled.
109109

110110
// Where reserved space is unavailable (e.g. stats are out-of-sync) compute a conservative estimate for initialising shards
111-
final var initializingShardsIterator = node.initializing();
112-
while (initializingShardsIterator.hasNext()) {
113-
final var routing = initializingShardsIterator.next();
111+
for (ShardRouting routing : node.initializing()) {
114112
// Space needs to be reserved only when initializing shards that are going to use additional space
115113
// that is not yet accounted for by `reservedSpace` in case of lengthy recoveries
116114
if (shouldReserveSpaceForInitializingShard(routing, metadata) && reservedSpace.containsShardId(routing.shardId()) == false) {
@@ -137,9 +135,7 @@ public static long sizeOfUnaccountedShards(
137135
totalSize += sizeOfUnaccountableSearchableSnapshotShards;
138136

139137
if (subtractShardsMovingAway) {
140-
final var relocatingShardsIterator = node.relocating();
141-
while (relocatingShardsIterator.hasNext()) {
142-
final var routing = relocatingShardsIterator.next();
138+
for (ShardRouting routing : node.relocating()) {
143139
if (dataPath.equals(clusterInfo.getDataPath(routing))) {
144140
ProjectMetadata project = metadata.projectFor(routing.index());
145141
totalSize -= getExpectedShardSize(

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ThrottlingAllocationDecider.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
127127

128128
// Count the primaries currently doing recovery on the node, to ensure the primariesInitialRecoveries setting is obeyed.
129129
int primariesInRecovery = 0;
130-
final var initializingShardsIterator = node.initializing();
131130
final var returnUnexplainedDecision = allocation.debugDecision() == false;
132-
while (initializingShardsIterator.hasNext()) {
133-
final var shard = initializingShardsIterator.next();
131+
for (ShardRouting shard : node.initializing()) {
134132
// when a primary shard is INITIALIZING, it can be because of *initial recovery* or *relocation from another node*
135133
// we only count initial recoveries here, so we need to make sure that relocating node is null
136134
if (shard.primary() && shard.relocatingNodeId() == null) {

server/src/main/java/org/elasticsearch/common/util/iterable/Iterables.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
package org.elasticsearch.common.util.iterable;
1111

12+
import org.elasticsearch.common.collect.Iterators;
13+
import org.elasticsearch.core.Assertions;
14+
1215
import java.util.ArrayList;
1316
import java.util.Iterator;
1417
import java.util.List;
@@ -81,4 +84,15 @@ public static <T> int indexOf(Iterable<T> iterable, Predicate<T> predicate) {
8184
public static long size(Iterable<?> iterable) {
8285
return StreamSupport.stream(iterable.spliterator(), false).count();
8386
}
87+
88+
/**
89+
* Adds a wrapper around {@code iterable} which asserts that {@link Iterator#remove()} is not called on the iterator it returns.
90+
*/
91+
public static <T> Iterable<T> assertReadOnly(Iterable<T> iterable) {
92+
if (Assertions.ENABLED) {
93+
return () -> Iterators.assertReadOnly(iterable.iterator());
94+
} else {
95+
return iterable;
96+
}
97+
}
8498
}

server/src/test/java/org/elasticsearch/cluster/routing/RoutingNodeTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void testReturnStartedShards() {
161161

162162
private static Set<ShardId> startedShardsSet(RoutingNode routingNode) {
163163
final var result = new HashSet<ShardId>();
164-
routingNode.started().forEachRemaining(shardRouting -> result.add(shardRouting.shardId()));
164+
routingNode.started().forEach(shardRouting -> result.add(shardRouting.shardId()));
165165
return result;
166166
}
167167

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceComputerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public void allocate(RoutingAllocation allocation) {
582582
allocation.routingNodes().getRelocatingShardCount(),
583583
equalTo(0)
584584
);
585-
assertThat(Iterators.toList(allocation.routingNodes().node("node-2").started()), hasSize(2));
585+
assertThat(Iterators.toList(allocation.routingNodes().node("node-2").started().iterator()), hasSize(2));
586586
}
587587

588588
@Override

0 commit comments

Comments
 (0)