-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdraft.go
1318 lines (1151 loc) · 33.9 KB
/
draft.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package worker
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"math/rand"
"sync"
"time"
"google.golang.org/grpc"
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
"golang.org/x/net/context"
"golang.org/x/net/trace"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/protos"
"github.com/dgraph-io/dgraph/raftwal"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
const (
errorNodeIDExists = "Error Node ID already exists in the cluster"
// Tells how many Entries we want our modified etcd to send us without us having run Send() on
// the stream successfully.
kickBuffering = 5
)
type peerPoolEntry struct {
// Never the empty string. Possibly a bogus address -- bad port number, the value
// of *myAddr, or some screwed up Raft config.
addr string
// An owning reference to a pool for this peer (or nil if addr is sufficiently bogus).
poolOrNil *pool
// *** Fields below are only non-nil if poolOrNil is. ***
// If poolOrNil is non-nil, there's a goroutine running streamMsgApps.
// Cancellation -- close this to cancel streamMsgApps.
cancel func()
// Used to send MsgApp messages to the peer.
appMessages chan raftpb.Message
}
// peerPool stores the peers' addresses and our connections to them. It has exactly one
// entry for every peer other than ourselves. Some of these peers might be unreachable or
// have bogus (but never empty) addresses.
type peerPool struct {
sync.RWMutex
peers map[uint64]peerPoolEntry
// A waitGroup for the peerPoolEntrys' goroutines.
wg *sync.WaitGroup
}
var (
errNoPeerPoolEntry = fmt.Errorf("no peerPool entry")
errNoPeerPool = fmt.Errorf("no peerPool pool, could not connect")
)
// getPool returns the non-nil pool for a peer. This might error even if get(id)
// succeeds, if the pool is nil. This happens if the peer was configured so badly (it had
// a totally bogus addr) we can't make a pool. (A reasonable refactoring would have us
// make a pool, one that has a nil gRPC connection.)
//
// You must call pools().release on the pool.
func (p *peerPool) getPool(id uint64) (*pool, error) {
p.RLock()
defer p.RUnlock()
ent, ok := p.peers[id]
if !ok {
return nil, errNoPeerPoolEntry
}
if ent.poolOrNil == nil {
return nil, errNoPeerPool
}
ent.poolOrNil.AddOwner()
return ent.poolOrNil, nil
}
func (p *peerPool) get(id uint64) (string, bool) {
p.RLock()
defer p.RUnlock()
ret, ok := p.peers[id]
return ret.addr, ok
}
func (p *peerPool) getAppMessages(id uint64) (chan raftpb.Message, error) {
p.RLock()
defer p.RUnlock()
ent, ok := p.peers[id]
if !ok {
return nil, errNoPeerPoolEntry
}
if ent.poolOrNil == nil {
return nil, errNoPeerPool
}
return ent.appMessages, nil
}
func cleanupEntry(entry *peerPoolEntry) {
if entry.poolOrNil != nil {
entry.cancel()
pools().release(entry.poolOrNil)
}
}
func (p *peerPool) set(id uint64, entry peerPoolEntry) {
p.Lock()
old, ok := p.peers[id]
p.peers[id] = entry
p.Unlock()
if ok {
cleanupEntry(&old)
}
}
func (p *peerPool) clear() {
p.Lock()
peers := p.peers
p.peers = make(map[uint64]peerPoolEntry)
p.Unlock()
// Now decr its refcount / cleanup its goroutines.
for _, entry := range peers {
cleanupEntry(&entry)
}
}
func (p *peerPool) finishAndWait() {
p.clear()
p.wg.Done()
p.wg.Wait()
}
type proposalCtx struct {
ch chan error
ctx context.Context
}
type proposals struct {
sync.RWMutex
ids map[uint32]*proposalCtx
}
func (p *proposals) Store(pid uint32, pctx *proposalCtx) bool {
p.Lock()
defer p.Unlock()
if _, has := p.ids[pid]; has {
return false
}
p.ids[pid] = pctx
return true
}
func (p *proposals) Ctx(pid uint32) (context.Context, bool) {
p.RLock()
defer p.RUnlock()
if pd, has := p.ids[pid]; has {
return pd.ctx, true
}
return nil, false
}
func (p *proposals) Done(pid uint32, err error) {
p.Lock()
pd, has := p.ids[pid]
if has {
delete(p.ids, pid)
}
p.Unlock()
if !has {
return
}
pd.ch <- err
}
func (p *proposals) Has(pid uint32) bool {
p.RLock()
defer p.RUnlock()
_, has := p.ids[pid]
return has
}
type sendmsg struct {
to uint64
data []byte
}
type node struct {
x.SafeMutex
// SafeMutex is for fields which can be changed after init.
_confState *raftpb.ConfState
_raft raft.Node
// Fields which are never changed after init.
cfg *raft.Config
applyCh chan raftpb.Entry
ctx context.Context
stop chan struct{} // to send the stop signal to Run
done chan struct{} // to check whether node is running or not
kickCh chan kickValue // to remind etcd Raft to send more MsgApps to a peer
gid uint32
id uint64
messages chan sendmsg
peers peerPool
props proposals
raftContext *protos.RaftContext
store *raft.MemoryStorage
wal *raftwal.Wal
canCampaign bool
// applied is used to keep track of the applied RAFT proposals.
// The stages are proposed -> committed (accepted by cluster) ->
// applied (to PL) -> synced (to RocksDB).
applied x.WaterMark
}
// SetRaft would set the provided raft.Node to this node.
// It would check fail if the node is already set.
func (n *node) SetRaft(r raft.Node) {
n.Lock()
defer n.Unlock()
x.AssertTrue(n._raft == nil)
n._raft = r
}
// Raft would return back the raft.Node stored in the node.
func (n *node) Raft() raft.Node {
n.RLock()
defer n.RUnlock()
return n._raft
}
// SetConfState would store the latest ConfState generated by ApplyConfChange.
func (n *node) SetConfState(cs *raftpb.ConfState) {
n.Lock()
defer n.Unlock()
n._confState = cs
}
// ConfState would return the latest ConfState stored in node.
func (n *node) ConfState() *raftpb.ConfState {
n.RLock()
defer n.RUnlock()
return n._confState
}
func newNode(gid uint32, id uint64, myAddr string) *node {
x.Printf("Node with GroupID: %v, ID: %v\n", gid, id)
peers := peerPool{
peers: make(map[uint64]peerPoolEntry),
wg: new(sync.WaitGroup),
}
peers.wg.Add(1)
props := proposals{
ids: make(map[uint32]*proposalCtx),
}
store := raft.NewMemoryStorage()
rc := &protos.RaftContext{
Addr: myAddr,
Group: gid,
Id: id,
}
n := &node{
ctx: context.Background(),
id: id,
gid: gid,
store: store,
cfg: &raft.Config{
ID: id,
ElectionTick: 10,
HeartbeatTick: 1,
Storage: store,
MaxSizePerMsg: 4096,
MaxInflightMsgs: 256,
Logger: &raft.DefaultLogger{Logger: x.Logger},
},
applyCh: make(chan raftpb.Entry, numPendingMutations),
peers: peers,
props: props,
raftContext: rc,
messages: make(chan sendmsg, 1000),
stop: make(chan struct{}),
done: make(chan struct{}),
kickCh: make(chan kickValue, 500),
}
n.applied = x.WaterMark{Name: fmt.Sprintf("Committed: Group %d", n.gid)}
n.applied.Init()
return n
}
// Never returns ("", true)
func (n *node) GetPeer(pid uint64) (string, bool) {
return n.peers.get(pid)
}
// You must call release on the pool. Can error for some pid's for which GetPeer
// succeeds.
func (n *node) GetPeerPool(pid uint64) (*pool, error) {
return n.peers.getPool(pid)
}
// entry.Addr must not be empty.
func (n *node) SetPeer(pid uint64, entry peerPoolEntry) {
x.AssertTruef(entry.addr != "", "SetPeer for peer %d has empty addr.", pid)
n.peers.set(pid, entry)
}
// Connects the node and makes its peerPool refer to the constructed pool and address
// (possibly updating ourselves from the old address.) (Unless pid is ourselves, in which
// case this does nothing.)
func (n *node) Connect(pid uint64, addr string) {
if pid == n.id {
return
}
if paddr, ok := n.GetPeer(pid); ok && paddr == addr {
// Already connected.
return
}
// Here's what we do. Right now peerPool maps peer node id's to addr values. If
// a *pool can be created, good, but if not, we still create a peerPoolEntry with
// a nil *pool, no goroutine.
p, ok := pools().connect(n.ctx, addr)
var entry peerPoolEntry
if !ok {
// TODO: Note this fact in more general peer health info somehow.
x.Printf("Peer %d claims same host as me\n", pid)
entry = peerPoolEntry{addr: addr, cancel: nil, appMessages: nil, poolOrNil: nil}
} else {
// The goroutine for streamMsgApps owns a refcount on the pool too.
p.AddOwner()
ch := make(chan raftpb.Message, 1000)
n.peers.wg.Add(1)
ctx, cancel := context.WithCancel(n.ctx)
go streamMsgApps(ctx, n.peers.wg, ch, p, n.kickCh)
entry = peerPoolEntry{addr: addr, poolOrNil: p, cancel: cancel, appMessages: ch}
}
n.SetPeer(pid, entry)
}
func (n *node) AddToCluster(ctx context.Context, pid uint64) error {
addr, ok := n.GetPeer(pid)
x.AssertTruef(ok, "Unable to find conn pool for peer: %d", pid)
rc := &protos.RaftContext{
Addr: addr,
Group: n.raftContext.Group,
Id: pid,
}
rcBytes, err := rc.Marshal()
x.Check(err)
return n.Raft().ProposeConfChange(ctx, raftpb.ConfChange{
ID: pid,
Type: raftpb.ConfChangeAddNode,
NodeID: pid,
Context: rcBytes,
})
}
type header struct {
proposalId uint32
msgId uint16
}
func (h *header) Length() int {
return 6 // 4 bytes for proposalId, 2 bytes for msgId.
}
func (h *header) Encode() []byte {
result := make([]byte, h.Length())
binary.LittleEndian.PutUint32(result[0:4], h.proposalId)
binary.LittleEndian.PutUint16(result[4:6], h.msgId)
return result
}
func (h *header) Decode(in []byte) {
h.proposalId = binary.LittleEndian.Uint32(in[0:4])
h.msgId = binary.LittleEndian.Uint16(in[4:6])
}
func (n *node) ProposeAndWait(ctx context.Context, proposal *protos.Proposal) error {
if n.Raft() == nil {
return x.Errorf("RAFT isn't initialized yet")
}
pendingProposals <- struct{}{}
x.PendingProposals.Add(1)
defer func() { <-pendingProposals; x.PendingProposals.Add(-1) }()
if ctx.Err() != nil {
return ctx.Err()
}
// Do a type check here if schema is present
// In very rare cases invalid entries might pass through raft, which would
// be persisted, we do best effort schema check while writing
if proposal.Mutations != nil {
for _, edge := range proposal.Mutations.Edges {
if typ, err := schema.State().TypeOf(edge.Attr); err != nil {
continue
} else if err := validateAndConvert(edge, typ); err != nil {
return err
}
}
for _, schema := range proposal.Mutations.Schema {
if err := checkSchema(schema); err != nil {
return err
}
}
}
che := make(chan error, 1)
pctx := &proposalCtx{
ch: che,
ctx: ctx,
}
for {
id := rand.Uint32()
if n.props.Store(id, pctx) {
proposal.Id = id
break
}
}
sz := proposal.Size()
slice := make([]byte, sz)
upto, err := proposal.MarshalTo(slice)
if err != nil {
return err
}
// we don't timeout on a mutation which has already been proposed.
if err = n.Raft().Propose(ctx, slice[:upto]); err != nil {
return x.Wrapf(err, "While proposing")
}
// Wait for the proposal to be committed.
if proposal.Mutations != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Waiting for the proposal: mutations.")
}
} else if proposal.Membership != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Waiting for the proposal: membership update.")
}
} else {
log.Fatalf("Unknown proposal")
}
err = <-che
if err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf(err.Error())
}
}
return err
}
// appendSize32Data appends data to buf, prefixed with a 4-byte size. Returns total number of
// bytes appended.
func appendSize32Data(buf *bytes.Buffer, data []byte) int {
n := len(data)
x.Check(binary.Write(buf, binary.LittleEndian, uint32(n)))
x.Check2(buf.Write(data))
return 4 + n
}
func lastEntryIndex(entries []raftpb.Entry) (uint64, bool) {
if n := len(entries); n != 0 {
return entries[n-1].Index, true
}
return 0, false
}
func streamMsgApps(ctx context.Context, wg *sync.WaitGroup, msgCh chan raftpb.Message,
pl *pool, kickCh chan kickValue) {
defer wg.Done()
defer pools().release(pl)
client := protos.NewWorkerClient(pl.Get())
var stream protos.Worker_RaftMessageStreamClient
for {
select {
case msg := <-msgCh:
// I think every MsgApp will have at least one entry, but in principle it's possible
// that one wouldn't.
lastIndex, hasLastIndex := lastEntryIndex(msg.Entries)
to := msg.To
// (Try to) create or recreate the stream if it's not created.
if stream == nil {
var err error
stream, err = client.RaftMessageStream(ctx, grpc.FailFast(true))
if err != nil {
// Error opening RaftMessageStream. Maybe no connection.
continue
}
}
var buf bytes.Buffer
data := marshalMsgForSending(msg)
appendSize32Data(&buf, data)
p := &protos.Payload{Data: buf.Bytes()}
err := stream.Send(p)
if err != nil {
wg.Add(1)
go func(stream protos.Worker_RaftMessageStreamClient) {
stream.CloseAndRecv()
wg.Done()
}(stream)
x.Printf("Error sending to RaftMessageStream: %v", err)
stream = nil
} else {
// In principle we could consider msg.Index to be the "last sent index" if
// !hasLastIndex. It's always 1 less than the first entry's index. But why would
// we kick them if they didn't have any entries to send?
if hasLastIndex {
select {
case kickCh <- kickValue{to, lastIndex}:
default:
}
}
}
case <-ctx.Done():
if stream != nil {
_, err := stream.CloseAndRecv()
x.Printf("Error closing RaftMessageStream: %v", err)
}
return
}
}
}
type kickValue struct {
to uint64
lastIndexSent uint64
}
func (n *node) sendMsgApp(msg raftpb.Message) {
ch, err := n.peers.getAppMessages(msg.To)
if err != nil {
// No such peer. OK.
return
}
select {
case ch <- msg:
default:
x.Printf("Unable to push message to %v in sendMsgApp", msg.To)
}
}
func marshalMsgForSending(m raftpb.Message) []byte {
data, err := m.Marshal()
x.Check(err)
if m.Type != raftpb.MsgHeartbeat && m.Type != raftpb.MsgHeartbeatResp {
x.Printf("\t\tSENDING: %v %v-->%v\n", m.Type, m.From, m.To)
}
return data
}
const (
messageBatchSoftLimit = 10000000
)
func (n *node) batchAndSendMessages() {
batches := make(map[uint64]*bytes.Buffer)
for {
totalSize := 0
sm := <-n.messages
slurp_loop:
for {
var buf *bytes.Buffer
if b, ok := batches[sm.to]; !ok {
buf = new(bytes.Buffer)
batches[sm.to] = buf
} else {
buf = b
}
totalSize += appendSize32Data(buf, sm.data)
if totalSize > messageBatchSoftLimit {
// We limit the batch size, but we aren't pushing back on
// n.messages, because the loop below spawns a goroutine
// to do its dirty work.
break
}
select {
case sm = <-n.messages:
default:
break slurp_loop
}
}
for to, buf := range batches {
if buf.Len() == 0 {
continue
}
data := make([]byte, buf.Len())
copy(data, buf.Bytes())
go n.doSendMessage(to, data)
buf.Reset()
}
}
}
func (n *node) doSendMessage(to uint64, data []byte) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
pool, err := n.GetPeerPool(to)
if err != nil {
// No such peer exists or we got handed a bogus config (bad addr), so we
// can't send messages to this peer.
return
}
defer pools().release(pool)
conn := pool.Get()
c := protos.NewWorkerClient(conn)
p := &protos.Payload{Data: data}
ch := make(chan error, 1)
go func() {
_, err = c.RaftMessage(ctx, p)
ch <- err
}()
select {
case <-ctx.Done():
return
case <-ch:
// We don't need to do anything if we receive any error while sending message.
// RAFT would automatically retry.
return
}
}
func (n *node) processMutation(ctx context.Context, index uint64, m *protos.Mutations) error {
// TODO: Need to pass node and entry index.
rv := x.RaftValue{Group: n.gid, Index: index}
ctx = context.WithValue(ctx, "raft", rv)
if err := runMutations(ctx, m.Edges); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf(err.Error())
}
return err
}
return nil
}
func (n *node) processSchemaMutations(e raftpb.Entry, m *protos.Mutations) error {
// TODO: Need to pass node and entry index.
rv := x.RaftValue{Group: n.gid, Index: e.Index}
ctx := context.WithValue(n.ctx, "raft", rv)
if err := runSchemaMutations(ctx, m.Schema); err != nil {
if tr, ok := trace.FromContext(n.ctx); ok {
tr.LazyPrintf(err.Error())
}
return err
}
return nil
}
func (n *node) processMembership(index uint64, mm *protos.Membership) error {
x.AssertTrue(n.gid == 0)
x.Printf("group: %v Addr: %q leader: %v dead: %v\n",
mm.GroupId, mm.Addr, mm.Leader, mm.AmDead)
groups().applyMembershipUpdate(index, mm)
return nil
}
func (n *node) process(index uint64, proposal *protos.Proposal, pending chan struct{}) {
defer func() {
n.applied.Ch <- x.Mark{Index: index, Done: true}
posting.SyncMarkFor(n.gid).Ch <- x.Mark{Index: index, Done: true}
}()
pending <- struct{}{} // This will block until we can write to it.
x.ActiveMutations.Add(1)
defer x.ActiveMutations.Add(-1)
var err error
if proposal.Mutations != nil {
var ctx context.Context
var has bool
if ctx, has = n.props.Ctx(proposal.Id); !has {
ctx = n.ctx
}
err = n.processMutation(ctx, index, proposal.Mutations)
} else if proposal.Membership != nil {
err = n.processMembership(index, proposal.Membership)
}
n.props.Done(proposal.Id, err)
<-pending // Release one.
}
const numPendingMutations = 10000
func (n *node) processApplyCh() {
pending := make(chan struct{}, numPendingMutations)
for e := range n.applyCh {
mark := x.Mark{Index: e.Index, Done: true}
if len(e.Data) == 0 {
n.applied.Ch <- mark
posting.SyncMarkFor(n.gid).Ch <- mark
continue
}
if e.Type == raftpb.EntryConfChange {
var cc raftpb.ConfChange
cc.Unmarshal(e.Data)
if len(cc.Context) > 0 {
var rc protos.RaftContext
x.Check(rc.Unmarshal(cc.Context))
n.Connect(rc.Id, rc.Addr)
}
cs := n.Raft().ApplyConfChange(cc)
n.SetConfState(cs)
n.applied.Ch <- mark
posting.SyncMarkFor(n.gid).Ch <- mark
continue
}
x.AssertTrue(e.Type == raftpb.EntryNormal)
// The following effort is only to apply schema in a blocking fashion.
// Once we have a scheduler, this should go away.
// TODO: Move the following to scheduler.
// We derive the schema here if it's not present
// Since raft committed logs are serialized, we can derive
// schema here without any locking
proposal := &protos.Proposal{}
if err := proposal.Unmarshal(e.Data); err != nil {
log.Fatalf("Unable to unmarshal proposal: %v %q\n", err, e.Data)
}
if proposal.Mutations != nil {
// process schema mutations before
if proposal.Mutations.Schema != nil {
// Wait for applied watermark to reach till previous index
// All mutations before this should use old schema and after this
// should use new schema
n.waitForSyncMark(n.ctx, e.Index-1)
if err := n.processSchemaMutations(e, proposal.Mutations); err != nil {
n.applied.Ch <- mark
posting.SyncMarkFor(n.gid).Ch <- mark
n.props.Done(proposal.Id, err)
continue
}
}
// stores a map of predicate and type of first mutation for each predicate
schemaMap := make(map[string]types.TypeID)
for _, edge := range proposal.Mutations.Edges {
if _, ok := schemaMap[edge.Attr]; !ok {
schemaMap[edge.Attr] = posting.TypeID(edge)
}
}
for attr, storageType := range schemaMap {
if _, err := schema.State().TypeOf(attr); err != nil {
// Schema doesn't exist
// Since committed entries are serialized, updateSchemaIfMissing is not
// needed, In future if schema needs to be changed, it would flow through
// raft so there won't be race conditions between read and update schema
updateSchemaType(attr, storageType, e.Index, n.raftContext.Group)
}
}
}
go n.process(e.Index, proposal, pending)
}
}
func (n *node) saveToStorage(s raftpb.Snapshot, h raftpb.HardState,
es []raftpb.Entry) {
if !raft.IsEmptySnap(s) {
le, err := n.store.LastIndex()
if err != nil {
log.Fatalf("While retrieving last index: %v\n", err)
}
if s.Metadata.Index <= le {
return
}
if err := n.store.ApplySnapshot(s); err != nil {
log.Fatalf("Applying snapshot: %v", err)
}
}
if !raft.IsEmptyHardState(h) {
n.store.SetHardState(h)
}
n.store.Append(es)
}
func (n *node) retrieveSnapshot(peerID uint64) {
pool, err := n.GetPeerPool(peerID)
if err != nil {
// err is just going to be errNoConnection
log.Fatalf("Cannot retrieve snapshot from peer %v, no connection. Error: %v\n",
peerID, err)
}
defer pools().release(pool)
// Get index of last committed.
lastIndex, err := n.store.LastIndex()
x.Checkf(err, "Error while getting last index")
// Wait for watermarks to sync since populateShard writes directly to db, otherwise
// the values might get overwritten
// Safe to keep this line
n.syncAllMarks(n.ctx, lastIndex)
// Need to clear pl's stored in memory for the case when retrieving snapshot with
// index greater than this node's last index
// Should invalidate/remove pl's to this group only ideally
posting.EvictGroup(n.gid)
if _, err := populateShard(n.ctx, pool, n.gid); err != nil {
// TODO: We definitely don't want to just fall flat on our face if we can't
// retrieve a simple snapshot.
log.Fatalf("Cannot retrieve snapshot from peer %v, error: %v\n", peerID, err)
}
// Populate shard stores the streamed data directly into db, so we need to refresh
// schema for current group id
x.Checkf(schema.LoadFromDb(n.gid), "Error while initilizating schema")
}
func (n *node) sendMessages(rcBytes []byte, messages []raftpb.Message) {
for _, msg := range messages {
// NOTE: We can do some optimizations here to drop messages.
msg.Context = rcBytes
x.AssertTrue(n.id != msg.To)
if msg.Type == raftpb.MsgApp {
n.sendMsgApp(msg)
continue
}
data := marshalMsgForSending(msg)
select {
case n.messages <- sendmsg{to: msg.To, data: data}:
// pass
default:
// This would be very weird, but it's okay to drop messages. Raft can deal with it.
x.Printf("Unable to push message to channel in send")
}
}
}
func (n *node) Run() {
firstRun := true
var leader bool
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
rcBytes, err := n.raftContext.Marshal()
x.Check(err)
for {
select {
case <-ticker.C:
n.Raft().Tick()
case rd := <-n.Raft().Ready():
if rd.SoftState != nil {
if rd.RaftState == raft.StateFollower && leader {
// stepped down as leader do a sync membership immediately
groups().syncMemberships()
} else if rd.RaftState == raft.StateLeader && !leader {
// TODO:wait for apply watermark ??
leaseMgr().resetLease(n.gid)
groups().syncMemberships()
}
leader = rd.RaftState == raft.StateLeader
}
x.Check(n.wal.StoreSnapshot(n.gid, rd.Snapshot))
x.Check(n.wal.Store(n.gid, rd.HardState, rd.Entries))
n.saveToStorage(rd.Snapshot, rd.HardState, rd.Entries)
n.sendMessages(rcBytes, rd.Messages)
if !raft.IsEmptySnap(rd.Snapshot) {
// We don't send snapshots to other nodes. But, if we get one, that means
// either the leader is trying to bring us up to state; or this is the
// snapshot that I created. Only the former case should be handled.
var rc protos.RaftContext
x.Check(rc.Unmarshal(rd.Snapshot.Data))
x.AssertTrue(rc.Group == n.gid)
if rc.Id != n.id {
x.Printf("-------> SNAPSHOT [%d] from %d\n", n.gid, rc.Id)
n.retrieveSnapshot(rc.Id)
x.Printf("-------> SNAPSHOT [%d]. DONE.\n", n.gid)
} else {
x.Printf("-------> SNAPSHOT [%d] from %d [SELF]. Ignoring.\n", n.gid, rc.Id)
}
}
if len(rd.CommittedEntries) > 0 {
if tr, ok := trace.FromContext(n.ctx); ok {
tr.LazyPrintf("Found %d committed entries", len(rd.CommittedEntries))
}
}
for _, entry := range rd.CommittedEntries {
// Need applied watermarks for schema mutation also for read linearazibility
// Applied watermarks needs to be emitted as soon as possible sequentially.
// If we emit Mark{4, false} and Mark{4, true} before emitting Mark{3, false}
// then doneUntil would be set as 4 as soon as Mark{4,true} is done and before
// Mark{3, false} is emitted. So it's safer to emit watermarks as soon as
// possible sequentially
status := x.Mark{Index: entry.Index, Done: false}
n.applied.Ch <- status
posting.SyncMarkFor(n.gid).Ch <- status
// Just queue up to be processed. Don't wait on them.
n.applyCh <- entry
}
n.Raft().Advance()
if firstRun && n.canCampaign {
go n.Raft().Campaign(n.ctx)
firstRun = false
}
case kickValue := <-n.kickCh:
// Read as much as possible from kickCh, its purpose is to get us started sending, not
// keep a count of how many times to kick.
toKick := make(map[uint64]uint64) // Maps node id to lastIndex sent.
slurpKickCh:
for {
toKick[kickValue.to] = kickValue.lastIndexSent
select {
case kickValue = <-n.kickCh:
default:
break slurpKickCh
}
}
for id, lastIndexSent := range toKick {
n.Raft().Kick(id, lastIndexSent+kickBuffering)
}
case <-n.stop:
if peerId, has := groups().Peer(n.gid, Config.RaftId); has && n.AmLeader() {
n.Raft().TransferLeadership(n.ctx, Config.RaftId, peerId)
go func() {
// TODO: n.ctx is a background context, it can't time out.
select {
case <-n.ctx.Done(): // time out
if tr, ok := trace.FromContext(n.ctx); ok {
tr.LazyPrintf("context timed out while transfering leadership")
}
case <-time.After(1 * time.Second):
if tr, ok := trace.FromContext(n.ctx); ok {
tr.LazyPrintf("Timed out transfering leadership")
}
}
n.finishStop()
}()
} else {