/
raft_impl.go
88 lines (69 loc) · 1.69 KB
/
raft_impl.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
package cluster
import (
"github.com/hashicorp/raft"
)
var _ Interface = &Impl{}
// Impl is the interface that must be implemented by a raft server.
type Impl struct {
m Master
}
func (i *Impl) State() raft.RaftState {
return i.m.Raft.State()
}
func (i *Impl) Peers() ([]string, error) {
return i.m.Peers, nil
}
func (i *Impl) Close() error {
// close the raft node
err := i.m.Raft.Shutdown().Error()
if err != nil {
return nil
}
// close the raft log store
err = i.m.RaftLog.Close()
return err
}
func (i *Impl) IsLeader() bool {
return i.m.Raft.State() == raft.Leader
}
func (i *Impl) IsCandidate() bool {
return i.m.Raft.State() == raft.Candidate
}
func (i *Impl) Leader() string {
address, _ := i.m.Raft.LeaderWithID()
return string(address)
}
func (i *Impl) Apply(b []byte) error {
// call the *Cluster.Apply method
af := i.m.Raft.Apply(b, 0)
// Error will only return generic errors related to raft
if err := af.Error(); err != nil {
return err
}
// *Cluster.Apply will return an error by Response, not by Error
// so here we must check whether the response is an error
response := af.Response()
if err, ok := response.(error); ok {
return err
}
return nil
}
func (i *Impl) AddServer(addr string) error {
return i.m.Raft.AddVoter(raft.ServerID(addr), raft.ServerAddress(addr), 0, 0).Error()
}
func (i *Impl) ShowDebugInfo(witch string) ([]byte, error) {
//TODO implement me
panic("implement me")
}
func (i *Impl) UserSnapshot() error {
sf := i.m.Raft.Snapshot()
return sf.Error()
}
func (i *Impl) Put(key, value []byte) error {
//TODO implement me
panic("implement me")
}
func (i *Impl) Get(key []byte) ([]byte, error) {
//TODO implement me
panic("implement me")
}