-
Notifications
You must be signed in to change notification settings - Fork 4
/
vote.go
132 lines (126 loc) · 4.05 KB
/
vote.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package server
import (
"context"
"fmt"
pb "github.com/PomeloCloud/BFTRaft4go/proto/server"
"sync"
"time"
)
func RequestVoteRequestSignData(req *pb.RequestVoteRequest) []byte {
return []byte(fmt.Sprint(req.Group, "-", req.Term, "-", req.LogIndex, "-", req.Term, "-", req.CandidateId))
}
func RequestVoteResponseSignData(res *pb.RequestVoteResponse) []byte {
return []byte(fmt.Sprint(res.Group, "-", res.Term, "-", res.LogIndex, "-", res.Term, "-", res.CandidateId, "-", res.Granted))
}
func (s *BFTRaftServer) BecomeCandidate(meta *RTGroupMeta) {
RefreshTimer(meta, 10)
meta.Role = CANDIDATE
group := meta.Group
group.Term++
term := group.Term
s.SaveGroup(meta.Group)
meta.Votes = []*pb.RequestVoteResponse{}
meta.IsNewTerm = true
lastEntry := s.LastLogEntry(group.Id)
request := &pb.RequestVoteRequest{
Group: group.Id,
Term: term,
LogIndex: lastEntry.Index,
LogTerm: lastEntry.Term,
CandidateId: meta.Peer,
Signature: []byte{},
}
request.Signature = s.Sign(RequestVoteRequestSignData(request))
lock := sync.Mutex{}
votes := 0
voteReceived := make(chan *pb.RequestVoteResponse)
adequateVotes := make(chan bool, 1)
for _, peer := range meta.GroupPeers {
nodeId := peer.Host
if nodeId == s.Id {
continue
}
node := s.GetNode(nodeId)
go func() {
if client, err := s.ClusterClients.Get(node.ServerAddr); err == nil {
if voteResponse, err := client.rpc.RequestVote(context.Background(), request); err == nil {
publicKey := s.GetNodePublicKey(nodeId)
signData := RequestVoteResponseSignData(voteResponse)
if VerifySign(publicKey, voteResponse.Signature, signData) == nil {
if voteResponse.Granted && voteResponse.LogIndex <= lastEntry.Index {
lock.Lock()
votes++
voteReceived <- voteResponse
lock.Unlock()
}
}
}
}
}()
}
go func() {
// Here we can follow the rule of Raft by expecting majority votes
// or follow the PBFT rule by expecting n - f votes
// I will use the rule from Raft first
expectedVotes := len(meta.GroupPeers) / 2 // ExpectedHonestPeers(s.GroupPeersSlice(group.Id))
for vote, voted := <-voteReceived; voted; {
if votes > expectedVotes {
meta.Votes = append(meta.Votes, vote)
adequateVotes <- true
break
}
}
}()
go func() {
select {
case <-adequateVotes:
s.BecomeLeader(meta)
case <-time.After(5000 * time.Second):
close(voteReceived)
}
}()
}
func (s *BFTRaftServer) BecomeLeader(meta *RTGroupMeta) {
// when this peer become the leader of the group
// it need to send it's vote to followers to claim it's authority
// this only need to be done once in each term
// so we just send the 'AppendEntry' request in this function
// we can use a dedicated rpc protocol for this, but no bother
meta.Role = LEADER
meta.Group.LeaderPeer = meta.Peer // set self to leader for next following requests
s.SaveGroup(meta.Group)
s.SendFollowersHeartbeat(context.Background(), meta.Peer, meta.Group)
}
func (s *BFTRaftServer) BecomeFollower(meta *RTGroupMeta, appendEntryReq *pb.AppendEntriesRequest) bool {
// first we need to verify the leader got all of the votes required
expectedVotes := ExpectedHonestPeers(s.GroupPeersSlice(meta.Group.Id))
if len(appendEntryReq.QuorumVotes) < expectedVotes {
return false
}
votes := map[uint64]bool{}
for _, vote := range appendEntryReq.QuorumVotes {
votePeer, foundCandidate := meta.GroupPeers[vote.Voter]
if !foundCandidate || vote.Term <= meta.Group.Term {
continue
}
// check their signatures
signData := RequestVoteResponseSignData(vote)
publicKey := s.GetNodePublicKey(votePeer.Host)
if VerifySign(publicKey, vote.Signature, signData) != nil {
continue
}
// check their properties to avoid forging
if vote.Group == meta.Group.Id && vote.CandidateId == appendEntryReq.LeaderId && vote.Granted {
votes[votePeer.Id] = true
}
}
if len(votes) > expectedVotes {
// received enough votes, will transform to follower
meta.Role = FOLLOWER
meta.Group.LeaderPeer = appendEntryReq.LeaderId
s.SaveGroup(meta.Group)
return true
} else {
return false
}
}