-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster.go
196 lines (168 loc) · 4.31 KB
/
cluster.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package drax
import (
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/cpuguy83/drax/api"
"github.com/cpuguy83/drax/rpc"
libkvstore "github.com/docker/libkv/store"
"github.com/hashicorp/raft"
)
var (
defaultTimeout = 5 * time.Second
)
// Cluster is used to manage all the cluster state for a given node
type Cluster struct {
home, addr, peerAddr string
store *store
chShutdown chan struct{}
chErrors chan error
server *rpc.Server
l net.Listener
r *Raft
mu sync.Mutex
rpcDialer rpc.DialerFn
peers raft.PeerStore
logger io.Writer
}
// New creates a new Cluster and starts it
func New(l net.Listener, rpcDialer rpc.DialerFn, home, addr, peer string, logger io.Writer) (*Cluster, error) {
if err := os.MkdirAll(home, 0600); err != nil {
return nil, fmt.Errorf("error creating home dir: %v", err)
}
c := &Cluster{
home: home,
addr: addr,
peerAddr: peer,
chShutdown: make(chan struct{}),
chErrors: make(chan error, 1),
l: l,
logger: logger,
rpcDialer: rpcDialer,
}
return c, c.start()
}
func (c *Cluster) start() error {
c.store = newStore()
cfg := raft.DefaultConfig()
cfg.ShutdownOnRemove = false
if c.logger != nil {
cfg.LogOutput = c.logger
}
raftStream := rpc.NewStreamLayer(c.l.Addr(), byte(raftMessage), c.rpcDialer)
raftTransport := raft.NewNetworkTransport(raftStream, 3, defaultTimeout, os.Stdout)
peerStore := newPeerStore(c.home, raftTransport)
c.peers = peerStore
peers, err := peerStore.Peers()
if err != nil {
return err
}
nPeers := len(peers)
if nPeers <= 1 && c.peerAddr == "" {
cfg.EnableSingleNode = true
}
kvRaft, err := newRaft(filepath.Join(c.home, "raft"), c.addr, peerStore, (*storeFSM)(c.store), raftTransport, cfg)
if err != nil {
return err
}
c.store.r = kvRaft
c.store.dialer = c.rpcDialer
kvRaft.store = c.store
kvRaft.stream = raftStream
nodeRPCStream := rpc.NewStreamLayer(c.l.Addr(), byte(api.RPCMessage), c.rpcDialer)
nodeRPC := &nodeRPC{nodeRPCStream, kvRaft}
go nodeRPC.handleConns()
clientRPCStream := rpc.NewStreamLayer(c.l.Addr(), byte(api.ClientMessage), c.rpcDialer)
clientRPC := &clientRPC{clientRPCStream, c.store}
go clientRPC.handleConns()
handlers := map[byte]rpc.Handler{
byte(raftMessage): raftStream,
byte(api.RPCMessage): nodeRPCStream,
byte(api.ClientMessage): clientRPCStream,
}
c.server = rpc.NewServer(c.l, handlers)
c.r = kvRaft
go c.store.waitLeader()
go c.waitLeader()
if c.peerAddr != "" && nPeers <= 1 {
res, err := nodeRPCStream.RPC(c.peerAddr, &rpc.Request{
Method: addNode,
Args: []string{c.addr},
})
if err != nil {
return err
}
if res.Err != "" && !strings.Contains(res.Err, "peer already known") {
return fmt.Errorf(res.Err)
}
}
return nil
}
// Shutdown stops the local cluster node
func (c *Cluster) Shutdown() {
c.mu.Lock()
defer c.mu.Unlock()
select {
case <-c.chShutdown:
c.chErrors <- fmt.Errorf("already shutdown")
return
default:
}
c.r.Close()
close(c.chShutdown)
}
// KVStore provides access to the underlying KV store
func (c *Cluster) KVStore() libkvstore.Store {
return c.store
}
// ShutdownCh returns a channel that can be watched to determine if the cluster has been shut down
func (c *Cluster) ShutdownCh() <-chan struct{} {
return c.chShutdown
}
// Errors returns a channel receiver that callers can use to listen for cluster errors
func (c *Cluster) Errors() <-chan error {
return c.chErrors
}
func (c *Cluster) waitLeader() {
leaderCh := c.r.LeaderCh()
for {
logrus.Info("cluster: waiting for leader")
select {
case <-c.chShutdown:
return
case state := <-leaderCh:
if state == raft.Leader {
c.handleLeader(leaderCh)
}
}
}
}
func (c *Cluster) handleLeader(leaderCh <-chan interface{}) {
for {
select {
case <-c.chShutdown:
return
case state := <-leaderCh:
if state != raft.Leader {
return
}
default:
}
// Handle cluster-level leadership resonsibilities
time.Sleep(5 * time.Second)
}
}
// Addr cluster is available on
func (c *Cluster) Addr() string {
return c.addr
}
// Home is the home path where the cluster state is stored
func (c *Cluster) Home() string {
return c.home
}