szetszwo commented on a change in pull request #182:
URL: https://github.com/apache/incubator-ratis/pull/182#discussion_r477537179
##########
File path:
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
##########
@@ -269,13 +271,30 @@ private int submitRequests(final long electionTerm, final
TermIndex lastEntry,
return submitted;
}
+ private Set<RaftPeerId> getHigherPriorityPeers(RaftConfiguration conf) {
+ Set<RaftPeerId> higherPriorityPeers = new HashSet<>();
+
+ int currPriority = conf.getPeer(server.getId()).getPriority();
+ Collection<RaftPeer> peers = conf.getPeers();
+
+ for (RaftPeer peer : peers) {
+ if (peer.getPriority() > currPriority) {
+ higherPriorityPeers.add(peer.getId());
+ }
+ }
+
+ return higherPriorityPeers;
+ }
+
private ResultAndTerm waitForResults(final long electionTerm, final int
submitted,
RaftConfiguration conf, Executor voteExecutor) throws
InterruptedException {
final Timestamp timeout =
Timestamp.currentTime().addTimeMs(server.getRandomTimeoutMs());
final Map<RaftPeerId, RequestVoteReplyProto> responses = new HashMap<>();
final List<Exception> exceptions = new ArrayList<>();
int waitForNum = submitted;
Collection<RaftPeerId> votedPeers = new ArrayList<>();
+ Set<RaftPeerId> higherPriorityPeers = getHigherPriorityPeers(conf);
+
while (waitForNum > 0 && shouldRun(electionTerm)) {
final TimeDuration waitTime = timeout.elapsedTime().apply(n -> -n);
if (waitTime.isNonPositive()) {
Review comment:
We need to change timeout behavior -- when timeout, if the server can
get majority, the election is passed even if higherPriorityPeers is non-empty.
##########
File path:
ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
##########
@@ -318,19 +318,24 @@ boolean recognizeCandidate(RaftPeerId candidateId, long
candidateTerm) {
return false;
}
- boolean isLogUpToDate(TermIndex candidateLastEntry) {
+ int compareLog(TermIndex candidateLastEntry) {
TermIndex local = log.getLastEntryTermIndex();
// need to take into account snapshot
SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
- if (local == null && snapshot == null) {
- return true;
+ if (local == null && snapshot == null) {
+ // If the lastEntry of candidate is null, the proto will transfer an
empty TermIndexProto,
+ // then term and index of candidateLastEntry will both be 0
+ if (candidateLastEntry.getTerm() == 0 && candidateLastEntry.getIndex()
== 0) {
+ return 0;
+ }
+ return -1;
} else if (candidateLastEntry == null) {
Review comment:
The case candidateLastEntry == null is never happen so that we should
remove it. (Or, if we want to support candidateLastEntry == null, we have to
check null in line 328.)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]