Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize DLedgerLeaderElector.maintainAsCandidate Lock #312

Open
supervate opened this issue Oct 8, 2023 · 0 comments
Open

Optimize DLedgerLeaderElector.maintainAsCandidate Lock #312

supervate opened this issue Oct 8, 2023 · 0 comments

Comments

@supervate
Copy link
Contributor

supervate commented Oct 8, 2023

This is the original code

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());
            }
            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;
    }
}

We can delete the synchronized by use CAS. code as following

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;
}
supervate added a commit to supervate/dledger that referenced this issue Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant