Skip to content

Commit 4380c3d

Browse files
authored
Remove electionValueForced (elastic#9)
* Remove electionValueForced * Replace believed-unnecessary check with an assertion
1 parent 47f2bb5 commit 4380c3d

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

core/src/main/java/org/elasticsearch/discovery/zen2/ConsensusState.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class ConsensusState<T extends ConsensusState.CommittedState> extends Abs
5050
private Optional<AcceptedState<T>> acceptedState;
5151
// transient state
5252
private boolean electionWon;
53-
private boolean electionValueForced;
5453
private NodeCollection joinVotes;
5554
private boolean publishPermitted;
5655
private NodeCollection publishVotes;
@@ -69,7 +68,6 @@ public ConsensusState(Settings settings, long currentTerm, T committedState, Opt
6968

7069
// transient state
7170
this.electionWon = false;
72-
this.electionValueForced = false;
7371
this.joinVotes = new NodeCollection();
7472
this.publishPermitted = false;
7573
this.publishVotes = new NodeCollection();
@@ -125,7 +123,6 @@ public Vote handleStartVote(long newTerm) {
125123
currentTerm = newTerm;
126124
joinVotes = new NodeCollection();
127125
electionWon = false;
128-
electionValueForced = false;
129126
publishPermitted = true;
130127
publishVotes = new NodeCollection();
131128

@@ -152,35 +149,26 @@ public Optional<PublishRequest<T>> handleVote(DiscoveryNode sourceNode, Vote vot
152149
throw new IllegalArgumentException("incoming slot " + vote.getFirstUncommittedSlot() + " higher than current slot " +
153150
firstUncommittedSlot());
154151
}
155-
if (vote.getFirstUncommittedSlot() == firstUncommittedSlot() && vote.getLastAcceptedTerm() != NO_TERM) {
156-
final long lastAcceptedTermInSlot = lastAcceptedTerm();
157-
if (vote.getLastAcceptedTerm() > lastAcceptedTermInSlot) {
158-
logger.debug("handleVote: ignored vote as voter has better last accepted term (expected: <=[{}], actual: [{}])",
159-
lastAcceptedTermInSlot, vote.getLastAcceptedTerm());
160-
throw new IllegalArgumentException("incoming last accepted term " + vote.getLastAcceptedTerm() + " higher than " +
161-
"current last accepted term " + lastAcceptedTermInSlot);
162-
}
163-
if (vote.getLastAcceptedTerm() < lastAcceptedTermInSlot && electionValueForced == false) {
164-
logger.debug("handleVote: ignored vote as voter has worse last accepted term and election value not forced " +
165-
"(expected: <=[{}], actual: [{}])", lastAcceptedTermInSlot, vote.getLastAcceptedTerm());
166-
throw new IllegalArgumentException("incoming last accepted term " + vote.getLastAcceptedTerm() + " lower than " +
167-
"current last accepted term " + lastAcceptedTermInSlot + " and election value not forced");
168-
}
169-
electionValueForced = true;
152+
final long lastAcceptedTerm = lastAcceptedTerm();
153+
if (vote.getFirstUncommittedSlot() == firstUncommittedSlot() && vote.getLastAcceptedTerm() > lastAcceptedTerm) {
154+
logger.debug("handleVote: ignored vote as voter has better last accepted term (expected: <=[{}], actual: [{}])",
155+
lastAcceptedTerm, vote.getLastAcceptedTerm());
156+
throw new IllegalArgumentException("incoming last accepted term " + vote.getLastAcceptedTerm() + " higher than " +
157+
"current last accepted term " + lastAcceptedTerm);
170158
}
171159

172160
logger.debug("handleVote: adding vote {} from [{}] for election at slot {}", vote, sourceNode.getId(), firstUncommittedSlot());
173161
joinVotes.add(sourceNode);
174162

175163
electionWon = isQuorumInCurrentConfiguration(joinVotes);
176164

177-
logger.debug("handleVote: electionWon={} publishPermitted={} electionValueForce={}",
178-
electionWon, publishPermitted, electionValueForced);
179-
if (electionWon && publishPermitted && electionValueForced) {
165+
logger.debug("handleVote: electionWon={} publishPermitted={} lastAcceptedTerm={}",
166+
electionWon, publishPermitted, lastAcceptedTerm);
167+
if (electionWon && publishPermitted && lastAcceptedTerm != NO_TERM) {
180168
logger.debug("handleVote: sending PublishRequest");
181169

182170
publishPermitted = false;
183-
assert acceptedState.isPresent(); // must be true because electionValueForced == true
171+
assert acceptedState.isPresent(); // must be true because lastAcceptedTerm != NO_TERM
184172
return Optional.of(new PublishRequest<>(firstUncommittedSlot(), currentTerm, acceptedState.get().getDiff()));
185173
}
186174

@@ -287,7 +275,6 @@ public void handleCommit(ApplyCommit applyCommit) {
287275
committedState = newCommittedState;
288276
acceptedState = Optional.empty();
289277
publishPermitted = true;
290-
electionValueForced = false;
291278
publishVotes = new NodeCollection();
292279
}
293280

@@ -318,7 +305,6 @@ public void applyCatchup(T newCommittedState) {
318305
persistence.persistCommittedState(newCommittedState);
319306
committedState = newCommittedState;
320307
acceptedState = Optional.empty();
321-
electionValueForced = false;
322308
joinVotes = new NodeCollection();
323309
electionWon = false;
324310
publishVotes = new NodeCollection();
@@ -341,10 +327,7 @@ public PublishRequest<T> handleClientValue(Diff<T> diff) {
341327
logger.debug("handleClientValue: ignored request as publishing is not permitted");
342328
throw new IllegalArgumentException("publishing not permitted");
343329
}
344-
if (electionValueForced) {
345-
logger.debug("handleClientValue: ignored request as election value is forced");
346-
throw new IllegalArgumentException("election value forced");
347-
}
330+
assert lastAcceptedTerm() == NO_TERM; // see https://github.com/elastic/elasticsearch-formal-models/issues/24
348331

349332
logger.trace("handleClientValue: processing request for slot [{}] and term [{}]", firstUncommittedSlot(), currentTerm);
350333
publishPermitted = false;

0 commit comments

Comments
 (0)