Skip to content

Commit 155a1f2

Browse files
committed
Optimize calculating the presence of a quorum
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.
1 parent d8ba3a3 commit 155a1f2

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/CoordinationMetadata.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,13 @@ public void writeTo(StreamOutput out) throws IOException {
346346
}
347347

348348
public boolean hasQuorum(Collection<String> votes) {
349-
final HashSet<String> intersection = new HashSet<>(nodeIds);
350-
intersection.retainAll(votes);
351-
return intersection.size() * 2 > nodeIds.size();
349+
int votedNodes = 0;
350+
for (String nodeId : nodeIds) {
351+
if (votes.contains(nodeId)) {
352+
votedNodes++;
353+
}
354+
}
355+
return votedNodes * 2 > nodeIds.size();
352356
}
353357

354358
public Set<String> getNodeIds() {

0 commit comments

Comments
 (0)