forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
499 lines (441 loc) · 12.4 KB
/
groups.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package worker
import (
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/raftwal"
"github.com/dgraph-io/dgraph/store"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/x"
)
var (
groupIds = flag.String("groups", "0,1", "RAFT groups handled by this server.")
myAddr = flag.String("my", "",
"addr:port of this server, so other Dgraph servers can talk to this.")
peerAddr = flag.String("peer", "", "IP_ADDRESS:PORT of any healthy peer.")
raftId = flag.Uint64("idx", 1, "RAFT ID that this server will use to join RAFT groups.")
emptyMembershipUpdate task.MembershipUpdate
)
type server struct {
NodeId uint64 // Raft Id associated with the raft node.
Addr string // The public address of the server serving this node.
Leader bool // Set to true if the node is a leader of the group.
RaftIdx uint64 // The raft index which applied this membership update in group zero.
}
type servers struct {
list []server
}
type groupi struct {
x.SafeMutex
ctx context.Context
cancel context.CancelFunc
wal *raftwal.Wal
// local stores the groupId to node map for this server.
local map[uint32]*node
// all stores the groupId to servers map for the entire cluster.
all map[uint32]*servers
num uint32
lastUpdate uint64
}
var gr *groupi
func groups() *groupi {
return gr
}
// StartRaftNodes will read the WAL dir, create the RAFT groups,
// and either start or restart RAFT nodes.
// This function triggers RAFT nodes to be created, and is the entrace to the RAFT
// world from main.go.
func StartRaftNodes(walDir string) {
gr = new(groupi)
gr.ctx, gr.cancel = context.WithCancel(context.Background())
// Successfully connect with the peer, before doing anything else.
if len(*peerAddr) > 0 {
pools().connect(*peerAddr)
// Force run syncMemberships with this peer, so our nodes know if they have other
// servers who are serving the same groups. That way, they can talk to them
// and try to join their clusters. Otherwise, they'll start off as a single-node
// cluster.
// IMPORTANT: Don't run any nodes until we have done at least one full sync for membership
// information with the cluster. If you start this node too quickly, just
// after starting the leader of group zero, that leader might not have updated
// itself in the memberships; and hence this node would think that no one is handling
// group zero. Therefore, we MUST wait to get pass a last update raft index of zero.
for gr.LastUpdate() == 0 {
time.Sleep(time.Second)
fmt.Println("Last update raft index for membership information is zero. Syncing...")
gr.syncMemberships()
}
fmt.Printf("Last update is now: %d\n", gr.LastUpdate())
}
x.Checkf(os.MkdirAll(walDir, 0700), "Error while creating WAL dir.")
wals, err := store.NewSyncStore(walDir)
x.Checkf(err, "Error initializing wal store")
gr.wal = raftwal.Init(wals, *raftId)
if len(*myAddr) == 0 {
*myAddr = fmt.Sprintf("localhost:%d", *workerPort)
}
for _, id := range strings.Split(*groupIds, ",") {
gid, err := strconv.ParseUint(id, 0, 32)
x.Checkf(err, "Unable to parse group id: %v", id)
node := groups().newNode(uint32(gid), *raftId, *myAddr)
go node.InitAndStartNode(gr.wal)
}
go gr.periodicSyncMemberships() // Now set it to be run periodically.
}
func (g *groupi) Node(groupId uint32) *node {
g.RLock()
defer g.RUnlock()
if n, has := g.local[groupId]; has {
return n
}
return nil
}
func (g *groupi) ServesGroup(groupId uint32) bool {
g.RLock()
defer g.RUnlock()
_, has := g.local[groupId]
return has
}
func (g *groupi) newNode(groupId uint32, nodeId uint64, publicAddr string) *node {
g.Lock()
defer g.Unlock()
if g.local == nil {
g.local = make(map[uint32]*node)
}
node := newNode(groupId, nodeId, publicAddr)
if _, has := g.local[groupId]; has {
x.AssertTruef(false, "Didn't expect a node in RAFT group mapping: %v", groupId)
}
g.local[groupId] = node
return node
}
func (g *groupi) Server(id uint64, groupId uint32) (rs server, found bool) {
g.RLock()
defer g.RUnlock()
if g.all == nil {
return server{}, false
}
sl := g.all[groupId]
if sl == nil {
return server{}, false
}
for _, s := range sl.list {
if s.NodeId == id {
return s, true
}
}
return server{}, false
}
func (g *groupi) AnyServer(group uint32) string {
g.RLock()
defer g.RUnlock()
all := g.all[group]
if all == nil {
return ""
}
sz := len(all.list)
idx := rand.Intn(sz)
return all.list[idx].Addr
}
func (g *groupi) HasPeer(group uint32) bool {
g.RLock()
defer g.RUnlock()
all := g.all[group]
if all == nil {
return false
}
return len(all.list) > 0
}
// Leader will try to retrun the leader of a given group, based on membership information.
// There is currently no guarantee that the returned server is the leader of the group.
func (g *groupi) Leader(group uint32) (uint64, string) {
g.RLock()
defer g.RUnlock()
all := g.all[group]
if all == nil {
return 0, ""
}
return all.list[0].NodeId, all.list[0].Addr
}
func (g *groupi) KnownGroups() (gids []uint32) {
g.RLock()
defer g.RUnlock()
for gid := range g.all {
gids = append(gids, gid)
}
return
}
func (g *groupi) isDuplicate(gid uint32, nid uint64, addr string, leader bool) bool {
g.RLock()
defer g.RUnlock()
return g.duplicate(gid, nid, addr, leader)
}
// duplicate requires at least a read mutex lock to be held by the caller.
// duplicate will return true if we already have a server which matches the arguments
// provided to the function exactly. This is used to avoid re-applying the same update.
func (g *groupi) duplicate(gid uint32, nid uint64, addr string, leader bool) bool {
g.AssertRLock()
sl := g.all[gid]
if sl == nil {
return false
}
for _, s := range sl.list {
if s.NodeId == nid && s.Addr == addr && s.Leader == leader {
return true
}
}
return false
}
func (g *groupi) LastUpdate() uint64 {
g.RLock()
defer g.RUnlock()
return g.lastUpdate
}
func (g *groupi) TouchLastUpdate(u uint64) {
g.Lock()
defer g.Unlock()
if g.lastUpdate < u {
g.lastUpdate = u
}
}
// syncMemberships needs to be called in an periodic loop.
// How syncMemberships works:
// - Each server iterates over all the nodes it's serving, present in local.
// - If serving group zero, propose membership status updates directly via RAFT.
// - Otherwise, generates a membership update, which includes status of all serving nodes.
// - Check if it has address of a server from group zero. If so, use that.
// - Otherwise, use the peer information passed down via flags.
// - Send update via UpdateMembership call to the peer.
// - If the peer doesn't serve group zero, it would return back a redirect with the right address.
// - Otherwise, it would iterate over the memberships, check for duplicates, and apply updates.
// - Once iteration is over without errors, it would return back all new updates.
// - These updates are then applied to groups().all state via applyMembershipUpdate.
func (g *groupi) syncMemberships() {
if g.ServesGroup(0) {
// This server serves group zero.
g.RLock()
defer g.RUnlock()
for _, n := range g.local {
rc := n.raftContext
if g.duplicate(rc.Group, rc.Id, rc.Addr, n.AmLeader()) {
continue
}
go func(rc *task.RaftContext, amleader bool) {
mm := &task.Membership{
Leader: amleader,
Id: rc.Id,
GroupId: rc.Group,
Addr: rc.Addr,
}
zero := g.Node(0)
x.AssertTruef(zero != nil, "Expected node 0")
if err := zero.ProposeAndWait(zero.ctx, &task.Proposal{Membership: mm}); err != nil {
x.TraceError(g.ctx, err)
}
}(rc, n.AmLeader())
}
return
}
// This server doesn't serve group zero.
// Generate membership update of all local nodes.
var mu task.MembershipUpdate
{
g.RLock()
for _, n := range g.local {
rc := n.raftContext
mu.Members = append(mu.Members,
&task.Membership{
Leader: n.AmLeader(),
Id: rc.Id,
GroupId: rc.Group,
Addr: rc.Addr,
})
}
mu.LastUpdate = g.lastUpdate
g.RUnlock()
}
// Send an update to peer.
var pl *pool
addr := g.AnyServer(0)
UPDATEMEMBERSHIP:
if len(addr) > 0 {
pl = pools().get(addr)
} else {
pl = pools().any()
}
conn, err := pl.Get()
if err == errNoConnection {
fmt.Println("Unable to sync memberships. No valid connection")
return
}
x.Check(err)
defer pl.Put(conn)
c := NewWorkerClient(conn)
update, err := c.UpdateMembership(g.ctx, &mu)
if err != nil {
x.TraceError(g.ctx, err)
return
}
// Check if we got a redirect.
if update.Redirect {
addr = update.RedirectAddr
if len(addr) == 0 {
return
}
fmt.Printf("Got redirect for: %q\n", addr)
pools().connect(addr)
goto UPDATEMEMBERSHIP
}
var lu uint64
for _, mm := range update.Members {
g.applyMembershipUpdate(update.LastUpdate, mm)
if lu < update.LastUpdate {
lu = update.LastUpdate
}
}
g.TouchLastUpdate(lu)
}
func (g *groupi) periodicSyncMemberships() {
t := time.NewTicker(10 * time.Second)
for {
select {
case <-t.C:
g.syncMemberships()
case <-g.ctx.Done():
return
}
}
}
// raftIdx is the RAFT index corresponding to the application of this
// membership update in group zero.
func (g *groupi) applyMembershipUpdate(raftIdx uint64, mm *task.Membership) {
update := server{
NodeId: mm.Id,
Addr: mm.Addr,
Leader: mm.Leader,
RaftIdx: raftIdx,
}
if update.Addr != *myAddr {
go pools().connect(update.Addr)
}
fmt.Println("----------------------------")
fmt.Printf("====== APPLYING MEMBERSHIP UPDATE: %+v\n", update)
fmt.Println("----------------------------")
g.Lock()
defer g.Unlock()
if g.all == nil {
g.all = make(map[uint32]*servers)
}
sl := g.all[mm.GroupId]
if sl == nil {
sl = new(servers)
g.all[mm.GroupId] = sl
}
for {
// Remove all instances of the provided node. There should only be one.
found := false
for i, s := range sl.list {
if s.NodeId == update.NodeId {
found = true
sl.list[i] = sl.list[len(sl.list)-1]
sl.list = sl.list[:len(sl.list)-1]
}
}
if !found {
break
}
}
// Append update to the list. If it's a leader, move it to index zero.
sl.list = append(sl.list, update)
last := len(sl.list) - 1
if update.Leader {
sl.list[0], sl.list[last] = sl.list[last], sl.list[0]
}
// Update all servers upwards of index zero as followers.
for i := 1; i < len(sl.list); i++ {
sl.list[i].Leader = false
}
// Print out the entire list.
for gid, sl := range g.all {
fmt.Printf("Group: %v. List: %+v\n", gid, sl.list)
}
}
// MembershipUpdateAfter generates the Flatbuffer response containing all the
// membership updates after the provided raft index.
func (g *groupi) MembershipUpdateAfter(ridx uint64) *task.MembershipUpdate {
g.RLock()
defer g.RUnlock()
maxIdx := ridx
out := new(task.MembershipUpdate)
for gid, peers := range g.all {
for _, s := range peers.list {
if s.RaftIdx <= ridx {
continue
}
if s.RaftIdx > maxIdx {
maxIdx = s.RaftIdx
}
out.Members = append(out.Members,
&task.Membership{
Leader: s.Leader,
Id: s.NodeId,
GroupId: gid,
Addr: s.Addr,
})
}
}
out.LastUpdate = maxIdx
return out
}
// UpdateMembership is the RPC call for updating membership for servers
// which don't serve group zero.
func (w *grpcWorker) UpdateMembership(ctx context.Context,
update *task.MembershipUpdate) (*task.MembershipUpdate, error) {
if ctx.Err() != nil {
return &emptyMembershipUpdate, ctx.Err()
}
if !groups().ServesGroup(0) {
addr := groups().AnyServer(0)
// fmt.Printf("I don't serve group zero. But, here's who does: %v\n", addr)
return &task.MembershipUpdate{
Redirect: true,
RedirectAddr: addr,
}, nil
}
che := make(chan error, len(update.Members))
for _, mm := range update.Members {
if groups().isDuplicate(mm.GroupId, mm.Id, mm.Addr, mm.Leader) {
che <- nil
continue
}
mmNew := &task.Membership{
Leader: mm.Leader,
Id: mm.Id,
GroupId: mm.GroupId,
Addr: mm.Addr,
}
go func(mmNew *task.Membership) {
zero := groups().Node(0)
che <- zero.ProposeAndWait(zero.ctx, &task.Proposal{Membership: mmNew})
}(mmNew)
}
for _ = range update.Members {
select {
case <-ctx.Done():
return &emptyMembershipUpdate, ctx.Err()
case err := <-che:
if err != nil {
return &emptyMembershipUpdate, err
}
}
}
// Find all membership updates since the provided lastUpdate. LastUpdate is
// the last raft index that the caller has recorded an update for.
reply := groups().MembershipUpdateAfter(update.LastUpdate)
return reply, nil
}