소스 검색

Remove side-effects in streams in PrimaryShardAllocator (#89218)

`map` should be a side-effect free function, because it's a non-terminal operation.
If we want to have side effect, we should use `forEach` which is terminal.
Artem Prigoda 3 년 전
부모
커밋
f1071cab36
1개의 변경된 파일9개의 추가작업 그리고 10개의 파일을 삭제
  1. 9 10
      server/src/main/java/org/elasticsearch/gateway/PrimaryShardAllocator.java

+ 9 - 10
server/src/main/java/org/elasticsearch/gateway/PrimaryShardAllocator.java

@@ -242,19 +242,18 @@ public abstract class PrimaryShardAllocator extends BaseGatewayShardAllocator {
         Collection<NodeGatewayStartedShards> ineligibleShards;
         if (nodesToAllocate != null) {
             final Set<DiscoveryNode> discoNodes = new HashSet<>();
-            nodeResults.addAll(
-                Stream.of(nodesToAllocate.yesNodeShards, nodesToAllocate.throttleNodeShards, nodesToAllocate.noNodeShards)
-                    .flatMap(Collection::stream)
-                    .map(dnode -> {
-                        discoNodes.add(dnode.nodeShardState.getNode());
-                        return new NodeAllocationResult(
+            Stream.of(nodesToAllocate.yesNodeShards, nodesToAllocate.throttleNodeShards, nodesToAllocate.noNodeShards)
+                .flatMap(Collection::stream)
+                .forEach(dnode -> {
+                    discoNodes.add(dnode.nodeShardState.getNode());
+                    nodeResults.add(
+                        new NodeAllocationResult(
                             dnode.nodeShardState.getNode(),
                             shardStoreInfo(dnode.nodeShardState, inSyncAllocationIds),
                             dnode.decision
-                        );
-                    })
-                    .toList()
-            );
+                        )
+                    );
+                });
             ineligibleShards = fetchedShardData.getData()
                 .values()
                 .stream()