// 码 1 QuorumPeer.java:节点状态枚举
public enum ServerStatepublic enum ServerState {
LOOKING,
FOLLOWING,
LEADING,
OBSERVING
}
// 码 2 Vote.java:选票结构
public class Vote {
private final int version;
private final long id; //服务器ID
private final long zxid; //Epoch + Counter
private final long electionEpoch; //选举轮次
private final long peerEpoch; //被推举的Leader所在的选举轮次
private final ServerState state; //当前服务器状态
}
// 码 3 ZxidUtils.java:Zxid结构
public class ZxidUtils {
public static long getEpochFromZxid(long zxid) {
return zxid >> 32L;
}
public static long getCounterFromZxid(long zxid) {
return zxid & 0xffffffffL;
}
public static long makeZxid(long epoch, long counter) {
return (epoch << 32L) | (counter & 0xffffffffL);
}
public static String zxidToString(long zxid) {
return Long.toHexString(zxid);
}
}
// 码 4 FastLeaderElection.java:选票对比
protected boolean totalOrderPredicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch) {
LOG.debug(
"id: {}, proposed id: {}, zxid: 0x{}, proposed zxid: 0x{}",
newId,
curId,
Long.toHexString(newZxid),
Long.toHexString(curZxid));
if (self.getQuorumVerifier().getWeight(newId) == 0) {
return false;
}
/*
* We return true if one of the following three cases hold:
* 1- New epoch is higher
* 2- New epoch is the same as current epoch, but new zxid is higher
* 3- New epoch is the same as current epoch, new zxid is the same
* as current zxid, but server id is higher.
*/
return ((newEpoch > curEpoch)
|| ((newEpoch == curEpoch)
&& ((newZxid > curZxid)
|| ((newZxid == curZxid)
&& (newId > curId)))));
}
原文:https://www.cnblogs.com/aspirant/p/13308093.html