Skip to content

Commit e7c325e

Browse files
committed
Fold JoinVoteCollection into VoteCollection
1 parent fbba2c3 commit e7c325e

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

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

+25-28
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class CoordinationState {
4949
private final PersistedState persistedState;
5050

5151
// transient state
52-
private JoinVoteCollection joinVotes;
52+
private VoteCollection joinVotes;
5353
private boolean startedJoinSinceLastReboot;
5454
private boolean electionWon;
5555
private long lastPublishedVersion;
@@ -64,7 +64,7 @@ public CoordinationState(DiscoveryNode localNode, PersistedState persistedState,
6464
this.electionStrategy = electionStrategy;
6565

6666
// transient state
67-
this.joinVotes = new JoinVoteCollection();
67+
this.joinVotes = new VoteCollection();
6868
this.startedJoinSinceLastReboot = false;
6969
this.electionWon = false;
7070
this.lastPublishedVersion = 0L;
@@ -193,7 +193,7 @@ public Join handleStartJoin(StartJoinRequest startJoinRequest) {
193193
lastPublishedConfiguration = getLastAcceptedConfiguration();
194194
startedJoinSinceLastReboot = true;
195195
electionWon = false;
196-
joinVotes = new JoinVoteCollection();
196+
joinVotes = new VoteCollection();
197197
publishVotes = new VoteCollection();
198198

199199
return new Join(localNode, startJoinRequest.getSourceNode(), getCurrentTerm(), getLastAcceptedTerm(),
@@ -494,18 +494,28 @@ default void markLastAcceptedStateAsCommitted() {
494494
}
495495

496496
/**
497-
* A collection of votes, used to calculate quorums.
497+
* A collection of votes, used to calculate quorums. Optionally records the Joins as well.
498498
*/
499499
public static class VoteCollection {
500500

501501
private final Map<String, DiscoveryNode> nodes;
502+
private final Set<Join> joins;
502503

503504
public boolean addVote(DiscoveryNode sourceNode) {
504505
return nodes.put(sourceNode.getId(), sourceNode) == null;
505506
}
506507

508+
public boolean addJoinVote(Join join) {
509+
final boolean added = addVote(join.getSourceNode());
510+
if (added) {
511+
joins.add(join);
512+
}
513+
return added;
514+
}
515+
507516
public VoteCollection() {
508517
nodes = new HashMap<>();
518+
joins = new HashSet<>();
509519
}
510520

511521
public boolean isQuorum(VotingConfiguration configuration) {
@@ -524,44 +534,31 @@ public Collection<DiscoveryNode> nodes() {
524534
return Collections.unmodifiableCollection(nodes.values());
525535
}
526536

537+
public Set<Join> getJoins() {
538+
return Collections.unmodifiableSet(joins);
539+
}
540+
527541
@Override
528542
public String toString() {
529-
return "VoteCollection{" + String.join(",", nodes.keySet()) + "}";
543+
return "VoteCollection{votes=" + nodes.keySet() + ", joins=" + joins + "}";
530544
}
531545

532546
@Override
533547
public boolean equals(Object o) {
534548
if (this == o) return true;
535-
if (o == null || getClass() != o.getClass()) return false;
549+
if (!(o instanceof VoteCollection)) return false;
536550

537551
VoteCollection that = (VoteCollection) o;
538552

539-
return nodes.equals(that.nodes);
553+
if (!nodes.equals(that.nodes)) return false;
554+
return joins.equals(that.joins);
540555
}
541556

542557
@Override
543558
public int hashCode() {
544-
return nodes.hashCode();
545-
}
546-
}
547-
548-
/**
549-
* A collection of votes, extending {@link VoteCollection}, which additionally records the Joins
550-
*/
551-
public static class JoinVoteCollection extends VoteCollection {
552-
553-
private final Set<Join> joins = new HashSet<>();
554-
555-
public boolean addJoinVote(Join join) {
556-
final boolean added = addVote(join.getSourceNode());
557-
if (added) {
558-
joins.add(join);
559-
}
560-
return added;
561-
}
562-
563-
public Set<Join> getJoins() {
564-
return Collections.unmodifiableSet(joins);
559+
int result = nodes.hashCode();
560+
result = 31 * result + joins.hashCode();
561+
return result;
565562
}
566563
}
567564
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.elasticsearch.cluster.coordination.ClusterFormationFailureHelper.ClusterFormationState;
3434
import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfigExclusion;
3535
import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfiguration;
36-
import org.elasticsearch.cluster.coordination.CoordinationState.JoinVoteCollection;
36+
import org.elasticsearch.cluster.coordination.CoordinationState.VoteCollection;
3737
import org.elasticsearch.cluster.coordination.FollowersChecker.FollowerCheckRequest;
3838
import org.elasticsearch.cluster.coordination.JoinHelper.InitialJoinAccumulator;
3939
import org.elasticsearch.cluster.metadata.MetaData;
@@ -1104,7 +1104,7 @@ protected void onFoundPeersUpdated() {
11041104
synchronized (mutex) {
11051105
final Iterable<DiscoveryNode> foundPeers = getFoundPeers();
11061106
if (mode == Mode.CANDIDATE) {
1107-
final CoordinationState.VoteCollection expectedVotes = new CoordinationState.VoteCollection();
1107+
final VoteCollection expectedVotes = new VoteCollection();
11081108
foundPeers.forEach(expectedVotes::addVote);
11091109
expectedVotes.addVote(Coordinator.this.getLocalNode());
11101110
final boolean foundQuorum = coordinationState.get().isElectionQuorum(expectedVotes);
@@ -1312,12 +1312,12 @@ public void onSuccess(String source) {
13121312
// abdicate to it. Assume that every node that completed the publication can provide
13131313
// a vote in that next election and has the latest state.
13141314
final long futureElectionTerm = state.term() + 1;
1315-
final JoinVoteCollection futureJoinVoteCollection = new JoinVoteCollection();
1316-
completedNodes().forEach(completedNode -> futureJoinVoteCollection.addJoinVote(
1315+
final VoteCollection futureVoteCollection = new VoteCollection();
1316+
completedNodes().forEach(completedNode -> futureVoteCollection.addJoinVote(
13171317
new Join(completedNode, node, futureElectionTerm, state.term(), state.version())));
13181318
return electionStrategy.isElectionQuorum(node, futureElectionTerm,
13191319
state.term(), state.version(), state.getLastCommittedConfiguration(),
1320-
state.getLastAcceptedConfiguration(), futureJoinVoteCollection);
1320+
state.getLastAcceptedConfiguration(), futureVoteCollection);
13211321
})
13221322
.collect(Collectors.toList());
13231323
if (masterCandidates.isEmpty() == false) {
@@ -1359,7 +1359,7 @@ private void handleAssociatedJoin(Join join) {
13591359
}
13601360

13611361
@Override
1362-
protected boolean isPublishQuorum(CoordinationState.VoteCollection votes) {
1362+
protected boolean isPublishQuorum(VoteCollection votes) {
13631363
assert Thread.holdsLock(mutex) : "Coordinator mutex not held";
13641364
return coordinationState.get().isPublishQuorum(votes);
13651365
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.apache.logging.log4j.Logger;
2424
import org.apache.logging.log4j.message.ParameterizedMessage;
2525
import org.elasticsearch.cluster.ClusterState;
26-
import org.elasticsearch.cluster.coordination.CoordinationState.JoinVoteCollection;
26+
import org.elasticsearch.cluster.coordination.CoordinationState.VoteCollection;
2727
import org.elasticsearch.cluster.node.DiscoveryNode;
2828
import org.elasticsearch.common.Nullable;
2929
import org.elasticsearch.common.collect.Tuple;
@@ -189,8 +189,8 @@ private void handlePreVoteResponse(final PreVoteResponse response, final Discove
189189

190190
preVotesReceived.put(sender, response);
191191

192-
// create a fake JoinVoteCollection based on the pre-votes and check if there is an election quorum
193-
final JoinVoteCollection voteCollection = new JoinVoteCollection();
192+
// create a fake VoteCollection based on the pre-votes and check if there is an election quorum
193+
final VoteCollection voteCollection = new VoteCollection();
194194
final DiscoveryNode localNode = clusterState.nodes().getLocalNode();
195195
final PreVoteResponse localPreVoteResponse = getPreVoteResponse();
196196

x-pack/plugin/voting-only-node/src/main/java/org/elasticsearch/cluster/coordination/VotingOnlyNodePlugin.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.action.ActionResponse;
1212
import org.elasticsearch.client.Client;
1313
import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfiguration;
14-
import org.elasticsearch.cluster.coordination.CoordinationState.JoinVoteCollection;
1514
import org.elasticsearch.cluster.coordination.CoordinationState.VoteCollection;
1615
import org.elasticsearch.cluster.coordination.VotingOnlyNodeFeatureSet.UsageTransportAction;
1716
import org.elasticsearch.cluster.node.DiscoveryNode;
@@ -172,10 +171,9 @@ public boolean isElectionQuorum(DiscoveryNode localNode, long localCurrentTerm,
172171
}
173172
// if there's a vote from a full master node with same last accepted term and version, that node should become master
174173
// instead, so we should stand down
175-
if (joinVotes instanceof JoinVoteCollection &&
176-
((JoinVoteCollection) joinVotes).getJoins().stream().anyMatch(join -> isFullMasterNode(join.getSourceNode()) &&
177-
join.getLastAcceptedTerm() == localAcceptedTerm &&
178-
join.getLastAcceptedVersion() == localAcceptedVersion)) {
174+
if (joinVotes.getJoins().stream().anyMatch(join -> isFullMasterNode(join.getSourceNode()) &&
175+
join.getLastAcceptedTerm() == localAcceptedTerm &&
176+
join.getLastAcceptedVersion() == localAcceptedVersion)) {
179177
return false;
180178
}
181179
}

0 commit comments

Comments
 (0)