Skip to content

Commit

Permalink
[ISSUE openmessaging#312] Optimize DLedgerLeaderElector.maintainAsCan…
Browse files Browse the repository at this point in the history
…didate Lock
  • Loading branch information
supervate committed Oct 8, 2023
1 parent d34f270 commit 376d9c8
Showing 1 changed file with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,33 +454,38 @@ private void maintainAsCandidate() throws Exception {
if (x.getVoteResult() != VoteResponse.RESULT.UNKNOWN) {
validNum.incrementAndGet();
}
synchronized (knownMaxTermInGroup) {
switch (x.getVoteResult()) {
case ACCEPT:
acceptedNum.incrementAndGet();
break;
case REJECT_ALREADY_HAS_LEADER:
alreadyHasLeader.compareAndSet(false, true);
break;
case REJECT_TERM_SMALL_THAN_LEDGER:
case REJECT_EXPIRED_VOTE_TERM:
if (x.getTerm() > knownMaxTermInGroup.get()) {
knownMaxTermInGroup.set(x.getTerm());
switch (x.getVoteResult()) {
case ACCEPT:
acceptedNum.incrementAndGet();
break;
case REJECT_ALREADY_HAS_LEADER:
alreadyHasLeader.compareAndSet(false, true);
break;
case REJECT_TERM_SMALL_THAN_LEDGER:
case REJECT_EXPIRED_VOTE_TERM:
// cas update
for (;;) {
long maxTermInGroup = knownMaxTermInGroup.get();
if (x.getTerm() <= maxTermInGroup) {
break;
}
if (knownMaxTermInGroup.compareAndSet(maxTermInGroup, x.getTerm())) {
break;
}
break;
case REJECT_EXPIRED_LEDGER_TERM:
case REJECT_SMALL_LEDGER_END_INDEX:
biggerLedgerNum.incrementAndGet();
break;
case REJECT_TERM_NOT_READY:
notReadyTermNum.incrementAndGet();
break;
case REJECT_ALREADY_VOTED:
case REJECT_TAKING_LEADERSHIP:
default:
break;

}
}
break;
case REJECT_EXPIRED_LEDGER_TERM:
case REJECT_SMALL_LEDGER_END_INDEX:
biggerLedgerNum.incrementAndGet();
break;
case REJECT_TERM_NOT_READY:
notReadyTermNum.incrementAndGet();
break;
case REJECT_ALREADY_VOTED:
case REJECT_TAKING_LEADERSHIP:
default:
break;

}
if (alreadyHasLeader.get()
|| memberState.isQuorum(acceptedNum.get())
Expand Down

0 comments on commit 376d9c8

Please sign in to comment.