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

Refactor/implementation smell #1097

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1272,34 +1272,42 @@ private void stepDown(final long term, final boolean wakeupCandidate, final Stat
if (!this.state.isActive()) {
return;
}

handleStateSpecificActions(status);
resetLeaderIdAndState(term, status);
handleReplicatorActions(wakeupCandidate);
handleTransferActions();
restartElectionTimer();
}

private void handleStateSpecificActions(final Status status) {
if (this.state == State.STATE_CANDIDATE) {
stopVoteTimer();
} else if (this.state.compareTo(State.STATE_TRANSFERRING) <= 0) {
stopStepDownTimer();
this.ballotBox.clearPendingTasks();
// signal fsm leader stop immediately
if (this.state == State.STATE_LEADER) {
onLeaderStop(status);
}
}
// reset leader_id
resetLeaderId(PeerId.emptyPeer(), status);
}

// soft state in memory
private void resetLeaderIdAndState(long term, Status status) {
resetLeaderId(PeerId.emptyPeer(), status);
this.state = State.STATE_FOLLOWER;
this.confCtx.reset();
updateLastLeaderTimestamp(Utils.monotonicMs());
if (this.snapshotExecutor != null) {
this.snapshotExecutor.interruptDownloadingSnapshots(term);
}

// meta state
if (term > this.currTerm) {
this.currTerm = term;
this.votedId = PeerId.emptyPeer();
this.metaStorage.setTermAndVotedFor(term, this.votedId);
}
}

private void handleReplicatorActions(boolean wakeupCandidate) {
if (wakeupCandidate) {
this.wakingCandidate = this.replicatorGroup.stopAllAndFindTheNextCandidate(this.conf);
if (this.wakingCandidate != null) {
Expand All @@ -1308,15 +1316,18 @@ private void stepDown(final long term, final boolean wakeupCandidate, final Stat
} else {
this.replicatorGroup.stopAll();
}
}

private void handleTransferActions() {
if (this.stopTransferArg != null) {
if (this.transferTimer != null) {
this.transferTimer.cancel(true);
}
// There is at most one StopTransferTimer at the same term, it's safe to
// mark stopTransferArg to NULL
this.stopTransferArg = null;
}
// Learner node will not trigger the election timer.
}

private void restartElectionTimer() {
if (!isLearner()) {
this.electionTimer.restart();
} else {
Expand Down Expand Up @@ -1732,7 +1743,7 @@ private boolean isLeaderLeaseValid() {
if (checkLeaderLease(monotonicNowMs)) {
return true;
}
checkDeadNodes0(this.conf.getConf().getPeers(), monotonicNowMs, false, null);
checkAndUpdateLeaderWithDeadNodes(this.conf.getConf().getPeers(), monotonicNowMs, false, null);
return checkLeaderLease(monotonicNowMs);
}

Expand Down Expand Up @@ -2213,7 +2224,7 @@ private boolean checkDeadNodes(final Configuration conf, final long monotonicNow
// Ensure quorum nodes alive.
final List<PeerId> peers = conf.listPeers();
final Configuration deadNodes = new Configuration();
if (checkDeadNodes0(peers, monotonicNowMs, true, deadNodes)) {
if (checkAndUpdateLeaderWithDeadNodes(peers, monotonicNowMs, true, deadNodes)) {
return true;
}
if (stepDownOnCheckFail) {
Expand All @@ -2227,8 +2238,8 @@ private boolean checkDeadNodes(final Configuration conf, final long monotonicNow
return false;
}

private boolean checkDeadNodes0(final List<PeerId> peers, final long monotonicNowMs, final boolean checkReplicator,
final Configuration deadNodes) {
private boolean checkAndUpdateLeaderWithDeadNodes(final List<PeerId> peers, final long monotonicNowMs,
final boolean checkReplicator, final Configuration deadNodes) {
final int leaderLeaseTimeoutMs = this.options.getLeaderLeaseTimeoutMs();
int aliveCount = 0;
long startLease = Long.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ public FailoverClosureImpl(CompletableFuture<T> future, boolean retryOnInvalidEp
this.retryRunner = retryRunner;
}

private boolean hasRetriesLeft(int retriesLeft) {
return retriesLeft > 0;
}

private boolean isInvalidPeerError(Errors error) {
return ErrorsHelper.isInvalidPeer(error);
}

private boolean isInvalidEpochError(Errors error) {
return ErrorsHelper.isInvalidEpoch(error);
}

public boolean shouldRetryOnError(int retriesLeft, boolean retryOnInvalidEpoch, Errors error) {
return hasRetriesLeft(retriesLeft)
&& (isInvalidPeerError(error) || (retryOnInvalidEpoch && isInvalidEpochError(error)));
}

@SuppressWarnings("unchecked")
@Override
public void run(final Status status) {
Expand All @@ -66,8 +83,7 @@ public void run(final Status status) {
}

final Errors error = getError();
if (this.retriesLeft > 0
&& (ErrorsHelper.isInvalidPeer(error) || (this.retryOnInvalidEpoch && ErrorsHelper.isInvalidEpoch(error)))) {
if (shouldRetryOnError(this.retriesLeft, this.retryOnInvalidEpoch, error)) {
LOG.warn("[Failover] status: {}, error: {}, [{}] retries left.", status, error, this.retriesLeft);
this.retryRunner.run(error);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public long hash(final String k) {
break;
case KETAMA_HASH:
byte[] bKey = computeMd5(k);
rv = (long) (bKey[3] & 0xFF) << 24 | (long) (bKey[2] & 0xFF) << 16 | (long) (bKey[1] & 0xFF) << 8
| bKey[0] & 0xFF;
long fourthByteShifted = (long) (bKey[3] & 0xFF) << 24; // Shifted left by 24 bits
long thirdByteShifted = (long) (bKey[2] & 0xFF) << 16; // Shifted left by 16 bits
long secondByteShifted = (long) (bKey[1] & 0xFF) << 8; // Shifted left by 8 bits
long firstByte = bKey[0] & 0xFF;
rv = fourthByteShifted | thirdByteShifted | secondByteShifted | firstByte;
break;

}
Expand Down
Loading