@@ -49,7 +49,7 @@ public class CoordinationState {
49
49
private final PersistedState persistedState ;
50
50
51
51
// transient state
52
- private JoinVoteCollection joinVotes ;
52
+ private VoteCollection joinVotes ;
53
53
private boolean startedJoinSinceLastReboot ;
54
54
private boolean electionWon ;
55
55
private long lastPublishedVersion ;
@@ -64,7 +64,7 @@ public CoordinationState(DiscoveryNode localNode, PersistedState persistedState,
64
64
this .electionStrategy = electionStrategy ;
65
65
66
66
// transient state
67
- this .joinVotes = new JoinVoteCollection ();
67
+ this .joinVotes = new VoteCollection ();
68
68
this .startedJoinSinceLastReboot = false ;
69
69
this .electionWon = false ;
70
70
this .lastPublishedVersion = 0L ;
@@ -193,7 +193,7 @@ public Join handleStartJoin(StartJoinRequest startJoinRequest) {
193
193
lastPublishedConfiguration = getLastAcceptedConfiguration ();
194
194
startedJoinSinceLastReboot = true ;
195
195
electionWon = false ;
196
- joinVotes = new JoinVoteCollection ();
196
+ joinVotes = new VoteCollection ();
197
197
publishVotes = new VoteCollection ();
198
198
199
199
return new Join (localNode , startJoinRequest .getSourceNode (), getCurrentTerm (), getLastAcceptedTerm (),
@@ -494,18 +494,28 @@ default void markLastAcceptedStateAsCommitted() {
494
494
}
495
495
496
496
/**
497
- * A collection of votes, used to calculate quorums.
497
+ * A collection of votes, used to calculate quorums. Optionally records the Joins as well.
498
498
*/
499
499
public static class VoteCollection {
500
500
501
501
private final Map <String , DiscoveryNode > nodes ;
502
+ private final Set <Join > joins ;
502
503
503
504
public boolean addVote (DiscoveryNode sourceNode ) {
504
505
return nodes .put (sourceNode .getId (), sourceNode ) == null ;
505
506
}
506
507
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
+
507
516
public VoteCollection () {
508
517
nodes = new HashMap <>();
518
+ joins = new HashSet <>();
509
519
}
510
520
511
521
public boolean isQuorum (VotingConfiguration configuration ) {
@@ -524,44 +534,31 @@ public Collection<DiscoveryNode> nodes() {
524
534
return Collections .unmodifiableCollection (nodes .values ());
525
535
}
526
536
537
+ public Set <Join > getJoins () {
538
+ return Collections .unmodifiableSet (joins );
539
+ }
540
+
527
541
@ Override
528
542
public String toString () {
529
- return "VoteCollection{" + String . join ( "," , nodes . keySet ()) + "}" ;
543
+ return "VoteCollection{votes= " + nodes . keySet () + ", joins=" + joins + "}" ;
530
544
}
531
545
532
546
@ Override
533
547
public boolean equals (Object o ) {
534
548
if (this == o ) return true ;
535
- if (o == null || getClass () != o . getClass ( )) return false ;
549
+ if (!( o instanceof VoteCollection )) return false ;
536
550
537
551
VoteCollection that = (VoteCollection ) o ;
538
552
539
- return nodes .equals (that .nodes );
553
+ if (!nodes .equals (that .nodes )) return false ;
554
+ return joins .equals (that .joins );
540
555
}
541
556
542
557
@ Override
543
558
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 ;
565
562
}
566
563
}
567
564
}
0 commit comments