generated from openacid/gotmpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainloop.go
72 lines (61 loc) · 1.46 KB
/
mainloop.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
package traft
import fmt "fmt"
type queryBody struct {
arg interface{}
rstCh chan *queryRst
}
type queryRst struct {
v interface{}
err error
}
// query the mainloop goroutine for something, by other goroutines, such as
// update traft state or get some info.
func (tr *TRaft) query(arg interface{}) *queryRst {
rstCh := make(chan *queryRst)
tr.actionCh <- &queryBody{arg, rstCh}
rst := <-rstCh
lg.Infow("chan-query",
// "arg", arg,
"rst.err", rst.err,
"rst.v", toStr(rst.v))
return rst
}
// Loop handles actions from other components.
// This is the only goroutine that is allowed to update traft state.
// Any info to send out of this goroutine must be cloned.
func (tr *TRaft) Loop() {
for {
select {
case <-tr.shutdown:
return
case a := <-tr.actionCh:
tr.checkStatus()
switch f := a.arg.(type) {
case func() error:
err := f()
a.rstCh <- &queryRst{err: err}
case func() interface{}:
v := f()
a.rstCh <- &queryRst{v: v}
default:
panic("unknown func signature:" + fmt.Sprintf("%v", a.arg))
}
tr.checkStatus()
}
}
}
// checkStatus checks if TRaft status violate consistency requirement.
// This is just a routine for debug.
func (tr *TRaft) checkStatus() {
id := tr.Id
me := tr.Status[id]
// committer can never greater than voted leader
if me.Committer.Cmp(me.VotedFor) > 0 {
panic(
fmt.Sprintf("Commiter > VotedFor: Id:%d %s %s",
id,
me.Committer.ShortStr(),
me.VotedFor.ShortStr(),
))
}
}