-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcluster.go
322 lines (271 loc) · 6.63 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"context"
"errors"
"log"
"reflect"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-discovery"
"github.com/libp2p/go-libp2p-gorpc"
)
const (
bootstrapCount = 3
bootstrapTimeout = 5 * time.Second
)
type Cluster struct {
ctx context.Context
id peer.ID
host host.Host
config Config
rpcServer *rpc.Server
rpcClient *rpc.Client
routingDiscovery *discovery.RoutingDiscovery
peerManager *PeerManager
replicationService *ReplicationService
}
func NewCluster(
ctx context.Context,
host host.Host,
config Config,
replicationService *ReplicationService,
peerManager *PeerManager,
) (*Cluster, error) {
kdht, err := NewKDHT(ctx, host, config)
if err != nil {
return nil, err
}
c := &Cluster{
ctx: ctx,
id: host.ID(),
host: host,
config: config,
rpcServer: nil,
rpcClient: nil,
routingDiscovery: kdht,
replicationService: replicationService,
peerManager: peerManager,
}
// Add ourselves
id := c.ID(ctx)
c.peerManager.AddPeer(*id)
// Start discovering peers
go c.discover()
err = c.setupRpc()
if err != nil {
return nil, err
}
c.setupRPCClients()
return c, nil
}
func (c *Cluster) Start() error {
c.bootstrap()
err := c.initializeState()
if err != nil {
log.Printf("Failed to initialize state: %v", err)
return err
}
return nil
}
func (c *Cluster) setupRpc() error {
rpcServer := rpc.NewServer(c.host, protocol.ID(c.config.ProtocolID))
replicationRpc := ReplicationRPCAPI{r: c.replicationService}
err := rpcServer.Register(&replicationRpc)
if err != nil {
return err
}
clusterRpc := ClusterRPCAPI{c: c}
err = rpcServer.Register(&clusterRpc)
if err != nil {
return err
}
c.rpcServer = rpcServer
c.rpcClient = rpc.NewClientWithServer(c.host, protocol.ID(c.config.ProtocolID), rpcServer)
return nil
}
func (c *Cluster) setupRPCClients() {
c.replicationService.rpcClient = c.rpcClient
}
func (c *Cluster) discover() {
for {
discovery.Advertise(c.ctx, c.routingDiscovery, c.config.Rendezvous)
// log.Println("Successfully announced!")
// log.Println("Searching for other peers...")
peerChan, err := c.routingDiscovery.FindPeers(c.ctx, c.config.Rendezvous)
if err != nil {
log.Fatal(err)
}
for p := range peerChan {
if p.ID == c.id {
continue
}
if c.host.Network().Connectedness(p.ID) != network.Connected {
_, err := c.host.Network().DialPeer(c.ctx, p.ID)
if err != nil {
// log.Printf("Failed to connect to %s: %-v", p.ID, err)
continue
}
}
if !c.peerManager.IsKnownPeer(p.ID) {
var id ID
err = c.rpcClient.Call(
p.ID,
ClusterServiceName,
ClusterIDFuncName,
struct{}{},
&id,
)
if err != nil {
log.Printf("Failed to get peer ID for %q: %-v", p.ID, err)
continue
}
c.peerManager.AddPeer(id)
}
}
time.Sleep(time.Second * 1)
}
}
func (c *Cluster) bootstrap() bool {
log.Printf("Waiting until %d peers are discovered", bootstrapCount)
ticker := time.NewTicker(bootstrapTimeout)
defer ticker.Stop()
for {
select {
case <-ticker.C:
peers := c.peerManager.AllIDs()
if len(peers) >= bootstrapCount {
log.Printf("Discovered %d peers", bootstrapCount)
return true
}
}
}
}
func (c *Cluster) initializeState() error {
log.Println("Getting state from other peers")
// Get state from other peers
peers := c.peerManager.AllIDs().Without(c.id).PeerIDs()
lenPeers := len(peers)
ctxts := Ctxts(lenPeers)
states := make([]*State, lenPeers)
errs := c.rpcClient.MultiCall(
ctxts,
peers,
ReplicationServiceName,
ReplicationStateFuncName,
struct{}{},
CopyStateToIfaces(states),
)
log.Printf("Received state from %d peers, comparing states", lenPeers)
var statesToCompare []State
for i, err := range errs {
if err != nil {
return err
}
statesToCompare = append(statesToCompare, *states[i])
}
if len(statesToCompare) > 0 {
for _, state1 := range states {
if len(state1.Proposed) == 0 && len(state1.Stored) == 0 {
continue
}
for _, state2 := range states {
if len(state2.Proposed) == 0 && len(state2.Stored) == 0 {
continue
}
if !Equal(state1.Stored, state2.Stored) || !Equal(state1.Proposed, state2.Proposed) {
return errors.New("peers disagree on state")
}
}
}
log.Printf("All states are equal, adopted state")
c.replicationService.SetState(&statesToCompare[0])
}
return nil
}
func (c *Cluster) Peers(ctx context.Context) ([]*ID, error) {
peers := c.peerManager.AllIDs().PeerIDs()
lenPeers := len(peers)
ctxts := Ctxts(lenPeers)
ids := make([]*ID, lenPeers)
errs := c.rpcClient.MultiCall(
ctxts,
peers,
ClusterServiceName,
ClusterIDFuncName,
struct{}{},
CopyIDsToIfaces(ids),
)
for _, err := range errs {
if err != nil {
log.Printf("Err %-v", err)
}
}
return ids, nil
}
func (c *Cluster) Clusters() map[string][]string {
clusters := map[string][]string{}
for _, p := range c.peerManager.peers {
clusters[p.ClusterID] = append(clusters[p.ClusterID], p.ID.Pretty())
}
return clusters
}
func (c *Cluster) ID(ctx context.Context) *ID {
// msgpack decode error [pos 108]: interface conversion: *multiaddr.Multiaddr is not
// encoding.BinaryUnmarshaler: missing method UnmarshalBinary
var addrs []string
for _, addr := range c.host.Addrs() {
addrs = append(addrs, addr.String())
}
return &ID{
ID: c.id,
ClusterID: c.config.ClusterID,
Addresses: addrs,
Peers: c.host.Peerstore().Peers(),
}
}
func (c *Cluster) RemovePeer(p peer.ID) error {
log.Printf("Peer %q left", p.Pretty())
return c.peerManager.RemovePeer(p)
}
func (c *Cluster) Exit() {
peers := c.peerManager.AllIDs().Without(c.id)
lenPeers := len(peers)
replies := make([]struct{}, lenPeers)
c.rpcClient.MultiCall(
Ctxts(lenPeers),
peers.PeerIDs(),
ClusterServiceName,
ClusterRemovePeerFuncName,
c.id,
CopyEmptyStructToIfaces(replies),
)
}
func Equal(first map[string][]byte, second map[string][]byte) bool {
return reflect.DeepEqual(first, second)
}
func Ctxts(n int) []context.Context {
ctxs := make([]context.Context, n)
for i := 0; i < n; i++ {
ctxs[i] = context.Background()
}
return ctxs
}
func CopyIDsToIfaces(in []*ID) []interface{} {
ifaces := make([]interface{}, len(in))
for i := range in {
in[i] = &ID{}
ifaces[i] = in[i]
}
return ifaces
}
func CopyStateToIfaces(in []*State) []interface{} {
ifaces := make([]interface{}, len(in))
for i := range in {
in[i] = &State{}
ifaces[i] = in[i]
}
return ifaces
}