generated from openacid/gotmpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
82 lines (65 loc) · 1.57 KB
/
server.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
package traft
// TRaftServer impl
import (
"time"
)
var leaderLease = int64(time.Second * 1)
// init a TRaft for test, all logs are `set x=lsn`
func (tr *TRaft) initTraft0(committer *LeaderId, votedFor *LeaderId, cmds...interface{}) {
id := tr.Id
tr.Status[id].Committer = committer.Clone()
tr.Status[id].VotedFor = votedFor.Clone()
tr.addLogs(cmds...)
tr.checkStatus()
}
// init a TRaft for test, all logs are `set x=lsn`
func (tr *TRaft) initTraft(
// proposer of the logs
committer *LeaderId,
// author of the logs
author *LeaderId,
// log seq numbers to generate.
lsns []int64,
nilLogs map[int64]bool,
committed []int64,
votedFor *LeaderId,
) {
id := tr.Id
tr.LogOffset, tr.Logs = buildPseudoLogs(author, lsns, nilLogs)
tr.Status[id].Committer = committer.Clone()
tr.Status[id].Accepted = NewTailBitmap(0, lsns...)
if committed == nil {
tr.Status[id].Committed = NewTailBitmap(0)
} else {
tr.Status[id].Committed = NewTailBitmap(0, committed...)
}
tr.Status[id].VotedFor = votedFor.Clone()
tr.checkStatus()
}
func buildPseudoLogs(
// author of the logs
author *LeaderId,
// log seq numbers to generate.
lsns []int64,
nilLogs map[int64]bool,
) (int64, []*LogRecord) {
logs := make([]*LogRecord, 0)
if len(lsns) == 0 {
return 0, logs
}
last := lsns[len(lsns)-1]
start := lsns[0]
for i := start; i <= last; i++ {
logs = append(logs, &LogRecord{})
}
for _, lsn := range lsns {
if nilLogs != nil && nilLogs[lsn] {
} else {
logs[lsn-start] = NewRecord(
author.Clone(),
lsn,
NewCmdI64("set", "x", lsn))
}
}
return start, logs
}