Skip to content

Commit 3361bbe

Browse files
committed
Make NodeSorters iterable
1 parent e50bd1b commit 3361bbe

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private void balance() {
384384
}
385385

386386
// Balance each partition
387-
for (NodeSorter nodeSorter : nodeSorters.allNodeSorters()) {
387+
for (NodeSorter nodeSorter : nodeSorters) {
388388
balanceByWeights(nodeSorter);
389389
}
390390
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
import org.elasticsearch.cluster.routing.RoutingNode;
1313
import org.elasticsearch.cluster.routing.ShardRouting;
14+
import org.elasticsearch.common.collect.Iterators;
1415

15-
import java.util.Collection;
16-
import java.util.List;
16+
import java.util.Iterator;
1717

1818
public class GlobalBalancingWeightsFactory implements BalancingWeightsFactory {
1919

@@ -59,13 +59,13 @@ public NodeSorters createNodeSorters(BalancedShardsAllocator.ModelNode[] modelNo
5959
private record GlobalNodeSorters(BalancedShardsAllocator.NodeSorter nodeSorter) implements NodeSorters {
6060

6161
@Override
62-
public Collection<BalancedShardsAllocator.NodeSorter> allNodeSorters() {
63-
return List.of(nodeSorter);
62+
public BalancedShardsAllocator.NodeSorter sorterForShard(ShardRouting shard) {
63+
return nodeSorter;
6464
}
6565

6666
@Override
67-
public BalancedShardsAllocator.NodeSorter sorterForShard(ShardRouting shard) {
68-
return nodeSorter;
67+
public Iterator<BalancedShardsAllocator.NodeSorter> iterator() {
68+
return Iterators.single(nodeSorter);
6969
}
7070
}
7171
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,14 @@
1111

1212
import org.elasticsearch.cluster.routing.ShardRouting;
1313

14-
import java.util.Collection;
15-
1614
/**
1715
* NodeSorters is just a cache of
1816
* {@link org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.NodeSorter}
1917
* instances for each cluster partition
18+
* <p>
19+
* The returned iterator will return a node sorter for each partition in the cluster.
2020
*/
21-
public interface NodeSorters {
22-
23-
/**
24-
* Get the {@link BalancedShardsAllocator.NodeSorter}s for all partitions
25-
*/
26-
Collection<BalancedShardsAllocator.NodeSorter> allNodeSorters();
21+
public interface NodeSorters extends Iterable<BalancedShardsAllocator.NodeSorter> {
2722

2823
/**
2924
* Get the {@link BalancedShardsAllocator.NodeSorter} for the specified shard

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
import org.elasticsearch.test.gateway.TestGatewayAllocator;
5252

5353
import java.util.Arrays;
54-
import java.util.Collection;
5554
import java.util.Collections;
5655
import java.util.HashMap;
56+
import java.util.Iterator;
5757
import java.util.List;
5858
import java.util.Map;
5959
import java.util.Set;
@@ -844,9 +844,10 @@ public NodeSorters createNodeSorters(
844844
);
845845
}
846846
return new NodeSorters() {
847+
847848
@Override
848-
public Collection<BalancedShardsAllocator.NodeSorter> allNodeSorters() {
849-
return prefixNodeSorters.values();
849+
public Iterator<BalancedShardsAllocator.NodeSorter> iterator() {
850+
return prefixNodeSorters.values().iterator();
850851
}
851852

852853
@Override

0 commit comments

Comments
 (0)