Skip to content

Commit 58d1164

Browse files
authored
Improve reroute performance by optimising List.removeAll in LocalShardsBalancer to filter remote search shard from relocation decision (#14613)
Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
1 parent 501a702 commit 58d1164

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

server/src/main/java/org/opensearch/cluster/routing/allocation/allocator/LocalShardsBalancer.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.opensearch.gateway.PriorityComparator;
3333

3434
import java.util.ArrayList;
35-
import java.util.Arrays;
3635
import java.util.Collections;
3736
import java.util.Comparator;
3837
import java.util.HashMap;
@@ -41,7 +40,6 @@
4140
import java.util.List;
4241
import java.util.Map;
4342
import java.util.Set;
44-
import java.util.stream.Collectors;
4543
import java.util.stream.Stream;
4644
import java.util.stream.StreamSupport;
4745

@@ -779,15 +777,16 @@ void allocateUnassigned() {
779777
* if we allocate for instance (0, R, IDX1) we move the second replica to the secondary array and proceed with
780778
* the next replica. If we could not find a node to allocate (0,R,IDX1) we move all it's replicas to ignoreUnassigned.
781779
*/
782-
ShardRouting[] unassignedShards = unassigned.drain();
783-
List<ShardRouting> allUnassignedShards = Arrays.stream(unassignedShards).collect(Collectors.toList());
784-
List<ShardRouting> localUnassignedShards = allUnassignedShards.stream()
785-
.filter(shard -> RoutingPool.LOCAL_ONLY.equals(RoutingPool.getShardPool(shard, allocation)))
786-
.collect(Collectors.toList());
787-
allUnassignedShards.removeAll(localUnassignedShards);
788-
allUnassignedShards.forEach(shard -> routingNodes.unassigned().add(shard));
789-
unassignedShards = localUnassignedShards.toArray(new ShardRouting[0]);
790-
ShardRouting[] primary = unassignedShards;
780+
List<ShardRouting> primaryList = new ArrayList<>();
781+
for (ShardRouting shard : unassigned.drain()) {
782+
if (RoutingPool.LOCAL_ONLY.equals(RoutingPool.getShardPool(shard, allocation))) {
783+
primaryList.add(shard);
784+
} else {
785+
routingNodes.unassigned().add(shard);
786+
}
787+
}
788+
789+
ShardRouting[] primary = primaryList.toArray(new ShardRouting[0]);
791790
ShardRouting[] secondary = new ShardRouting[primary.length];
792791
int secondaryLength = 0;
793792
int primaryLength = primary.length;

0 commit comments

Comments
 (0)