瀏覽代碼

Optimize calculating the presence of a quorum (#83638)

We don't need to create new `HashSet` and remove elements from it to get an intersection.
We only care about the amount of notes that have voted.
Artem Prigoda 3 年之前
父節點
當前提交
fb00992003

+ 7 - 3
server/src/main/java/org/elasticsearch/cluster/coordination/CoordinationMetadata.java

@@ -346,9 +346,13 @@ public class CoordinationMetadata implements Writeable, ToXContentFragment {
         }
 
         public boolean hasQuorum(Collection<String> votes) {
-            final HashSet<String> intersection = new HashSet<>(nodeIds);
-            intersection.retainAll(votes);
-            return intersection.size() * 2 > nodeIds.size();
+            int votedNodesCount = 0;
+            for (String nodeId : nodeIds) {
+                if (votes.contains(nodeId)) {
+                    votedNodesCount++;
+                }
+            }
+            return votedNodesCount * 2 > nodeIds.size();
         }
 
         public Set<String> getNodeIds() {