-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlearner.go
74 lines (63 loc) · 1.94 KB
/
learner.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
package paxos
import "log"
type learner struct {
id int
acceptedMsgs map[int]message
nt nodeNetwork
}
//Initilize learner and prepare message pool.
func NewLearner(id int, nt nodeNetwork, acceptorIDs ...int) *learner {
newLearner := &learner{id: id, nt: nt}
newLearner.acceptedMsgs = make(map[int]message)
for _, aceptId := range acceptorIDs {
newLearner.acceptedMsgs[aceptId] = message{}
}
return newLearner
}
//Run learner process and will return learn value if reach majority.
func (l *learner) run() string {
for {
m := l.nt.recev()
if m == nil {
continue
}
log.Println("Learner: recev msg:", *m)
l.handleRecevAccept(*m)
learnMsg, isLearn := l.chosen()
if isLearn == false {
continue
}
return learnMsg.getProposeVal()
}
}
//Check acceptor message and compare with local accpeted proposal to make sure it is most updated.
func (l *learner) handleRecevAccept(acceptMsg message) {
hasAcceptedMsg := l.acceptedMsgs[acceptMsg.from]
if hasAcceptedMsg.getProposeSeq() < acceptMsg.getProposeSeq() {
//get bigger num will replace it to keep it updated.
l.acceptedMsgs[acceptMsg.from] = acceptMsg
}
}
//Every acceptor might send different proposal ID accept mesage to learner.
//Learner only chosen if the accept count reach majority.
func (l *learner) chosen() (message, bool) {
acceptCount := make(map[int]int)
acceptMsgMap := make(map[int]message)
//Separate each acceptor message according their proposal ID and count it
for _, msg := range l.acceptedMsgs {
proposalNum := msg.getProposeSeq()
acceptCount[proposalNum]++
acceptMsgMap[proposalNum] = msg
}
//Check count if reach majority will return as chosen value.
for chosenNum, chosenMsg := range acceptMsgMap {
if acceptCount[chosenNum] > l.majority() {
return chosenMsg, true
}
}
return message{}, false
}
//Count for majority, need initilize the count at constructor.
func (l *learner) majority() int {
return len(l.acceptedMsgs)/2 + 1
}