IOTXING

记录技术学习之路

0%

zookeeper源码学习-Vote

对象属性定义

1
2
3
4
5
6
7
8
9
private final int version;

private final long id;

private final long zxid;

private final long electionEpoch;

private final long peerEpoch;

实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public Vote(long id, long zxid) {
this.version = 0x0;
this.id = id;
this.zxid = zxid;
this.electionEpoch = -1;
this.peerEpoch = -1;
this.state = ServerState.LOOKING;
}

public Vote(long id, long zxid, long peerEpoch) {
this.version = 0x0;
this.id = id;
this.zxid = zxid;
this.electionEpoch = -1;
this.peerEpoch = peerEpoch;
this.state = ServerState.LOOKING;
}

public Vote(long id, long zxid, long electionEpoch, long peerEpoch) {
this.version = 0x0;
this.id = id;
this.zxid = zxid;
this.electionEpoch = electionEpoch;
this.peerEpoch = peerEpoch;
this.state = ServerState.LOOKING;
}

public Vote(int version, long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) {
this.version = version;
this.id = id;
this.zxid = zxid;
this.electionEpoch = electionEpoch;
this.state = state;
this.peerEpoch = peerEpoch;
}

public Vote(long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) {
this.id = id;
this.zxid = zxid;
this.electionEpoch = electionEpoch;
this.state = state;
this.peerEpoch = peerEpoch;
this.version = 0x0;
}

equals 判断是否相同

首先生成一个投票的实例,然后判断传进来的对象是否是投票

然后判断投票的状态,如果在要比较的节点或者传入的节点处于LOOKING状态,则比较所有的属性是否相同

如果不在LOOKING状态,则比较他们的id和朝代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public boolean equals(Object o) {
if (!(o instanceof Vote)) {
return false;
}
Vote other = (Vote) o;

if ((state == ServerState.LOOKING) || (other.state == ServerState.LOOKING)) {
return id == other.id
&& zxid == other.zxid
&& electionEpoch == other.electionEpoch
&& peerEpoch == other.peerEpoch;
} else {
/*
* There are two things going on in the logic below:
*
* 1. skip comparing the zxid and electionEpoch for votes for servers
* out of election.
*
* Need to skip those because they can be inconsistent due to
* scenarios described in QuorumPeer.updateElectionVote.
*
* And given that only one ensemble can be running at a single point
* in time and that each epoch is used only once, using only id and
* epoch to compare the votes is sufficient.
*
* {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1805}
*
* 2. skip comparing peerEpoch if if we're running with mixed ensemble
* with (version > 0x0) and without the change (version = 0x0)
* introduced in ZOOKEEPER-1732.
*
* {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1732}
*
* The server running with and without ZOOKEEPER-1732 will return
* different peerEpoch. During rolling upgrades, it's possible
* that 2/5 servers are returning epoch 1, while the other 2/5
* are returning epoch 2, the other server need to ignore the
* peerEpoch to be able to join it.
*/
if ((version > 0x0) ^ (other.version > 0x0)) {
return id == other.id;
} else {
return (id == other.id && peerEpoch == other.peerEpoch);
}
}
}