-
Notifications
You must be signed in to change notification settings - Fork 542
/
statemachine.go
1051 lines (961 loc) · 27 KB
/
statemachine.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 2017-2021 Lei Ni (nilei81@gmail.com) and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package rsm implements State Machines used in Dragonboat.
This package is internally used by Dragonboat, applications are not expected to
import this package.
*/
package rsm
import (
"bytes"
"errors"
"sync"
"sync/atomic"
"github.com/lni/goutils/logutil"
"github.com/lni/dragonboat/v3/config"
"github.com/lni/dragonboat/v3/internal/raft"
"github.com/lni/dragonboat/v3/internal/server"
"github.com/lni/dragonboat/v3/internal/settings"
"github.com/lni/dragonboat/v3/internal/vfs"
"github.com/lni/dragonboat/v3/logger"
pb "github.com/lni/dragonboat/v3/raftpb"
sm "github.com/lni/dragonboat/v3/statemachine"
)
var (
plog = logger.GetLogger("rsm")
)
var (
// ErrRestoreSnapshot indicates there is error when trying to restore
// from a snapshot
ErrRestoreSnapshot = errors.New("failed to restore snapshot")
batchedEntryApply = settings.Soft.BatchedEntryApply
sessionBufferInitialCap uint64 = 128 * 1024
)
// SSReqType is the type of a snapshot request.
type SSReqType uint64
const (
// Periodic is the value to indicate periodic snapshot.
Periodic SSReqType = iota
// UserRequested is the value to indicate user requested snapshot.
UserRequested
// Exported is the value to indicate exported snapshot.
Exported
// Streaming is the value to indicate snapshot streaming.
Streaming
)
// SSEnv is the snapshot environment type.
type SSEnv = server.SSEnv
// SSRequest is the type for describing the details of a snapshot request.
type SSRequest struct {
Type SSReqType
Key uint64
Path string
OverrideCompaction bool
CompactionOverhead uint64
}
// Exported returns a boolean value indicating whether the snapshot request
// is to create an exported snapshot.
func (r *SSRequest) Exported() bool {
return r.Type == Exported
}
// Streaming returns a boolean value indicating whether the snapshot request
// is to stream snapshot.
func (r *SSRequest) Streaming() bool {
return r.Type == Streaming
}
// SSMeta is the metadata of a snapshot.
type SSMeta struct {
From uint64
Index uint64
Term uint64
OnDiskIndex uint64 // applied index of IOnDiskStateMachine
Request SSRequest
Membership pb.Membership
Type pb.StateMachineType
Session *bytes.Buffer
Ctx interface{}
CompressionType config.CompressionType
}
// Task describes a task that need to be handled by StateMachine.
type Task struct {
ClusterID uint64
NodeID uint64
Index uint64
Recover bool
Initial bool
Save bool
Stream bool
PeriodicSync bool
NewNode bool
SSRequest SSRequest
Entries []pb.Entry
}
// IsSnapshotTask returns a boolean flag indicating whether it is a snapshot
// task.
func (t *Task) IsSnapshotTask() bool {
return t.Recover || t.Save || t.Stream
}
func (t *Task) isSyncTask() bool {
if t.PeriodicSync && t.IsSnapshotTask() {
panic("invalid task")
}
return t.PeriodicSync
}
// SMFactoryFunc is the function type for creating an IStateMachine instance
type SMFactoryFunc func(clusterID uint64,
nodeID uint64, done <-chan struct{}) IManagedStateMachine
// INode is the interface of a dragonboat node.
type INode interface {
StepReady()
RestoreRemotes(pb.Snapshot)
ApplyUpdate(pb.Entry, sm.Result, bool, bool, bool)
ApplyConfigChange(pb.ConfigChange, uint64, bool)
NodeID() uint64
ClusterID() uint64
ShouldStop() <-chan struct{}
}
// ISnapshotter is the interface for the snapshotter object.
type ISnapshotter interface {
GetSnapshot(uint64) (pb.Snapshot, error)
GetMostRecentSnapshot() (pb.Snapshot, error)
Stream(IStreamable, SSMeta, pb.IChunkSink) error
Shrunk(ss pb.Snapshot) (bool, error)
Save(ISavable, SSMeta) (pb.Snapshot, SSEnv, error)
Load(pb.Snapshot, ILoadable, IRecoverable) error
IsNoSnapshotError(error) bool
}
// StateMachine is the state machine component in the replicated state machine
// scheme.
type StateMachine struct {
mu sync.RWMutex
// lastApplied is the last applied index visibile to other modules in the
// system. it is updated by only setting the last index and term values of
// the update batch. it is protected by its own mutex to minimize contention
// with the update thread
lastApplied struct {
sync.Mutex
index uint64
term uint64
}
// applied index and term values updated for each update entry. they are
// primarily used to ensure index values applied into the SM are continuous
// and the term values never move backward. they are only accessed when
// StateMachine.mu is locked
index uint64
term uint64
syncedIndex uint64
snapshotter ISnapshotter
node INode
sm IManagedStateMachine
sessions *SessionManager
members *membership
snapshotIndex uint64
onDiskInitIndex uint64
onDiskIndex uint64
taskQ *TaskQueue
onDiskSM bool
aborted bool
isWitness bool
sct config.CompressionType
fs vfs.IFS
}
// NewStateMachine creates a new application state machine object.
func NewStateMachine(sm IManagedStateMachine,
snapshotter ISnapshotter,
cfg config.Config, node INode, fs vfs.IFS) *StateMachine {
ordered := cfg.OrderedConfigChange
return &StateMachine{
snapshotter: snapshotter,
sm: sm,
onDiskSM: sm.OnDisk(),
taskQ: NewTaskQueue(),
node: node,
sessions: NewSessionManager(),
members: newMembership(node.ClusterID(), node.NodeID(), ordered),
isWitness: cfg.IsWitness,
sct: cfg.SnapshotCompressionType,
fs: fs,
}
}
// Type returns the state machine type.
func (s *StateMachine) Type() pb.StateMachineType {
return s.sm.Type()
}
// TaskQ returns the task queue.
func (s *StateMachine) TaskQ() *TaskQueue {
return s.taskQ
}
// TaskChanBusy returns whether the TaskC chan is busy. Busy is defined as
// having more than half of its buffer occupied.
func (s *StateMachine) TaskChanBusy() bool {
sz := s.taskQ.Size()
return sz*2 > taskQueueBusyCap
}
// Close closes the state machine.
func (s *StateMachine) Close() error {
return s.sm.Close()
}
// DestroyedC return a chan struct{} used to indicate whether the SM has been
// fully unloaded.
func (s *StateMachine) DestroyedC() <-chan struct{} {
return s.sm.DestroyedC()
}
// Recover applies the snapshot.
func (s *StateMachine) Recover(t Task) (uint64, error) {
ss, err := s.getSnapshot(t)
if err != nil {
return 0, err
}
if pb.IsEmptySnapshot(ss) {
return 0, nil
}
plog.Debugf("%s called Recover, %s, on disk idx %d",
s.id(), s.ssid(ss.Index), ss.OnDiskIndex)
if err := s.recover(ss, t.Initial); err != nil {
return 0, err
}
s.node.RestoreRemotes(ss)
plog.Debugf("%s restored %s", s.id(), s.ssid(ss.Index))
return ss.Index, nil
}
func (s *StateMachine) getSnapshot(t Task) (pb.Snapshot, error) {
if !t.Initial {
ss, err := s.snapshotter.GetSnapshot(t.Index)
if err != nil && !s.snapshotter.IsNoSnapshotError(err) {
plog.Errorf("%s, get snapshot failed: %v", s.id(), err)
return pb.Snapshot{}, ErrRestoreSnapshot
}
if s.snapshotter.IsNoSnapshotError(err) {
plog.Errorf("%s, no snapshot", s.id())
return pb.Snapshot{}, err
}
return ss, nil
}
ss, err := s.snapshotter.GetMostRecentSnapshot()
if s.snapshotter.IsNoSnapshotError(err) {
plog.Infof("%s no snapshot available during launch", s.id())
return pb.Snapshot{}, nil
}
return ss, nil
}
func (s *StateMachine) mustBeOnDiskSM() {
if !s.OnDiskStateMachine() {
panic("not an IOnDiskStateMachine")
}
}
func (s *StateMachine) recoverRequired(ss pb.Snapshot, init bool) bool {
s.mustBeOnDiskSM()
shrunk, err := s.snapshotter.Shrunk(ss)
if err != nil {
panic(err)
}
if !init && shrunk {
panic("not initial recovery but snapshot shrunk")
}
if init {
if shrunk {
return false
}
if ss.Imported {
return true
}
return ss.OnDiskIndex > s.onDiskInitIndex
}
return ss.OnDiskIndex > s.onDiskIndex
}
func (s *StateMachine) checkRecoverOnDiskSM(ss pb.Snapshot, init bool) {
s.mustBeOnDiskSM()
if ss.Imported && init {
return
}
if ss.OnDiskIndex <= s.onDiskInitIndex {
plog.Panicf("%s, ss.OnDiskIndex (%d) <= s.onDiskInitIndex (%d)",
s.id(), ss.OnDiskIndex, s.onDiskInitIndex)
}
if ss.OnDiskIndex <= s.onDiskIndex {
plog.Panicf("%s, ss.OnDiskInit (%d) <= s.onDiskIndex (%d)",
s.id(), ss.OnDiskIndex, s.onDiskIndex)
}
}
func (s *StateMachine) recover(ss pb.Snapshot, init bool) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.GetLastApplied() >= ss.Index {
return raft.ErrSnapshotOutOfDate
}
if s.aborted {
return sm.ErrSnapshotStopped
}
if ss.Witness || ss.Dummy {
s.apply(ss)
return nil
}
if !s.OnDiskStateMachine() {
return s.doRecover(ss, init)
}
if s.recoverRequired(ss, init) {
s.checkRecoverOnDiskSM(ss, init)
if err := s.doRecover(ss, init); err != nil {
return err
}
s.applyOnDisk(ss, init)
} else {
s.apply(ss)
}
return nil
}
func (s *StateMachine) doRecover(ss pb.Snapshot, init bool) error {
index := ss.Index
plog.Debugf("%s recovering from %s, init %t", s.id(), s.ssid(index), init)
s.logMembership("members", index, ss.Membership.Addresses)
s.logMembership("observers", index, ss.Membership.Observers)
s.logMembership("witnesses", index, ss.Membership.Witnesses)
if err := s.snapshotter.Load(ss, s.sessions, s.sm); err != nil {
plog.Errorf("%s failed to load %s, %v", s.id(), s.ssid(index), err)
if err == sm.ErrSnapshotStopped {
s.aborted = true
}
return err
}
s.apply(ss)
return nil
}
func (s *StateMachine) apply(ss pb.Snapshot) {
s.members.set(ss.Membership)
s.lastApplied.Lock()
defer s.lastApplied.Unlock()
s.lastApplied.index, s.lastApplied.term = ss.Index, ss.Term
s.index, s.term = ss.Index, ss.Term
}
func (s *StateMachine) applyOnDisk(ss pb.Snapshot, init bool) {
s.mustBeOnDiskSM()
s.onDiskIndex = ss.OnDiskIndex
if ss.Imported && init {
s.onDiskInitIndex = ss.OnDiskIndex
}
s.apply(ss)
}
//TODO: add test to cover the case when ReadyToStreamSnapshot returns false
// ReadyToStream returns a boolean flag to indicate whether the state machine
// is ready to stream snapshot. It can not stream a full snapshot when
// membership state is catching up with the all disk SM state. Meta only
// snapshot can be taken at any time.
func (s *StateMachine) ReadyToStream() bool {
if !s.OnDiskStateMachine() {
return true
}
return s.GetLastApplied() >= s.onDiskInitIndex
}
func (s *StateMachine) tryInjectTestFS() {
if nsm, ok := s.sm.(*NativeSM); ok {
if odsm, ok := nsm.sm.(*OnDiskStateMachine); ok {
odsm.SetTestFS(s.fs)
}
}
}
// OpenOnDiskStateMachine opens the on disk state machine.
func (s *StateMachine) OpenOnDiskStateMachine() (uint64, error) {
s.mustBeOnDiskSM()
s.mu.Lock()
defer s.mu.Unlock()
s.tryInjectTestFS()
index, err := s.sm.Open()
if err != nil {
plog.Errorf("%s failed to open on disk SM, %v", s.id(), err)
if err == sm.ErrOpenStopped {
s.aborted = true
}
return 0, err
}
plog.Infof("%s opened disk SM, index %d", s.id(), index)
s.onDiskInitIndex = index
s.onDiskIndex = index
return index, nil
}
// Offloaded marks the state machine as offloaded from the specified compone.
// It returns a boolean value indicating whether the node has been fully
// unloaded after unloading from the specified compone.
func (s *StateMachine) Offloaded() bool {
return s.sm.Offloaded()
}
// Loaded marks the state machine as loaded from the specified compone.
func (s *StateMachine) Loaded() {
s.sm.Loaded()
}
// Lookup queries the local state machine.
func (s *StateMachine) Lookup(query interface{}) (interface{}, error) {
if s.Concurrent() {
return s.concurrentLookup(query)
}
return s.lookup(query)
}
func (s *StateMachine) lookup(query interface{}) (interface{}, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.aborted {
return nil, ErrClusterClosed
}
return s.sm.Lookup(query)
}
func (s *StateMachine) concurrentLookup(query interface{}) (interface{}, error) {
return s.sm.ConcurrentLookup(query)
}
// NALookup queries the local state machine.
func (s *StateMachine) NALookup(query []byte) ([]byte, error) {
if s.Concurrent() {
return s.naConcurrentLookup(query)
}
return s.nalookup(query)
}
func (s *StateMachine) nalookup(query []byte) ([]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.aborted {
return nil, ErrClusterClosed
}
return s.sm.NALookup(query)
}
func (s *StateMachine) naConcurrentLookup(query []byte) ([]byte, error) {
return s.sm.NAConcurrentLookup(query)
}
// GetMembership returns the membership info maintained by the state machine.
func (s *StateMachine) GetMembership() pb.Membership {
s.mu.RLock()
defer s.mu.RUnlock()
return s.members.get()
}
// Concurrent returns a boolean flag indicating whether the state machine is
// capable of taking concurrent snapshot.
func (s *StateMachine) Concurrent() bool {
return s.sm.Concurrent()
}
// OnDiskStateMachine returns a boolean flag indicating whether it is an on
// disk state machine.
func (s *StateMachine) OnDiskStateMachine() bool {
return s.onDiskSM && !s.isWitness
}
// Save creates a snapshot.
func (s *StateMachine) Save(req SSRequest) (pb.Snapshot, SSEnv, error) {
if req.Streaming() {
panic("invalid snapshot request")
}
if s.isWitness {
plog.Panicf("witness %s saving snapshot", s.id())
}
if s.Concurrent() {
return s.concurrentSave(req)
}
return s.save(req)
}
// Stream starts to stream snapshot from the current SM to a remote node
// targeted by the provided sink.
func (s *StateMachine) Stream(sink pb.IChunkSink) error {
return s.stream(sink)
}
// Sync synchronizes state machine's in-core state with that on disk.
func (s *StateMachine) Sync() error {
return s.sync()
}
// GetHash returns the state machine hash.
func (s *StateMachine) GetHash() (uint64, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.sm.GetHash()
}
// GetSessionHash returns the session hash.
func (s *StateMachine) GetSessionHash() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.sessions.GetSessionHash()
}
// GetMembershipHash returns the hash of the membership instance.
func (s *StateMachine) GetMembershipHash() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.members.getHash()
}
// Handle pulls the committed record and apply it if there is any available.
func (s *StateMachine) Handle(batch []Task, apply []sm.Entry) (Task, error) {
batch = batch[:0]
apply = apply[:0]
processed := false
defer func() {
// give the node worker a chance to run when
// - the visible index value has been updated
// - taskC has been popped
if processed {
s.node.StepReady()
}
}()
if rec, ok := s.taskQ.Get(); ok {
processed = true
if rec.IsSnapshotTask() {
return rec, nil
}
if !rec.isSyncTask() {
batch = append(batch, rec)
} else {
if err := s.sync(); err != nil {
return Task{}, err
}
}
done := false
for !done {
if rec, ok := s.taskQ.Get(); ok {
if rec.IsSnapshotTask() {
if err := s.handle(batch, apply); err != nil {
return Task{}, err
}
return rec, nil
}
if !rec.isSyncTask() {
batch = append(batch, rec)
} else {
if err := s.sync(); err != nil {
return Task{}, err
}
}
} else {
done = true
}
}
}
return Task{}, s.handle(batch, apply)
}
func (s *StateMachine) isDummySnapshot(r SSRequest) bool {
return s.OnDiskStateMachine() && !r.Exported() && !r.Streaming()
}
func (s *StateMachine) logMembership(name string,
index uint64, members map[uint64]string) {
plog.Debugf("%d %s included in %s", len(members), name, s.ssid(index))
for nid, addr := range members {
plog.Debugf("\t%s : %s", logutil.NodeID(nid), addr)
}
}
func (s *StateMachine) getSSMeta(c interface{}, r SSRequest) (SSMeta, error) {
if s.members.isEmpty() {
plog.Panicf("%s, empty membership", s.id())
}
ct := s.sct
// never compress dummy snapshot file
if s.isDummySnapshot(r) {
ct = config.NoCompression
}
buf := bytes.NewBuffer(make([]byte, 0, sessionBufferInitialCap))
meta := SSMeta{
From: s.node.NodeID(),
Ctx: c,
Index: s.index,
Term: s.term,
OnDiskIndex: s.onDiskIndex,
Request: r,
Session: buf,
Membership: s.members.get(),
Type: s.sm.Type(),
CompressionType: ct,
}
s.logMembership("members", meta.Index, meta.Membership.Addresses)
if err := s.sessions.SaveSessions(meta.Session); err != nil {
return SSMeta{}, err
}
return meta, nil
}
// GetLastApplied returns the last applied value.
func (s *StateMachine) GetLastApplied() uint64 {
s.lastApplied.Lock()
defer s.lastApplied.Unlock()
return s.lastApplied.index
}
// SetLastApplied sets the last applied index to the specified value. This
// method is only used in tests.
func (s *StateMachine) SetLastApplied(index uint64) {
s.lastApplied.Lock()
defer s.lastApplied.Unlock()
s.lastApplied.index = index
}
// GetSyncedIndex returns the index value that is known to have been
// synchronized.
func (s *StateMachine) GetSyncedIndex() uint64 {
return atomic.LoadUint64(&s.syncedIndex)
}
func (s *StateMachine) setSyncedIndex(index uint64) {
if s.GetSyncedIndex() > index {
panic("synced index moving backward")
}
atomic.StoreUint64(&s.syncedIndex, index)
}
func (s *StateMachine) setApplied(index uint64, term uint64) {
if s.index+1 != index {
plog.Panicf("%s, applied index %d, new index %d",
s.id(), s.index, index)
}
if s.term > term {
plog.Panicf("%s, applied term %d, new term %d", s.id(), s.term, term)
}
s.index, s.term = index, term
}
func (s *StateMachine) setLastApplied(entries []pb.Entry) {
if len(entries) > 0 {
index := uint64(0)
term := uint64(0)
for idx, e := range entries {
if e.Index == 0 || e.Term == 0 {
plog.Panicf("invalid entry, %v", e)
}
if idx == 0 {
index = e.Index
term = e.Term
} else {
if e.Index != index+1 {
plog.Panicf("%s, index gap found, %d,%d", s.id(), index, e.Index)
}
if e.Term < term {
plog.Panicf("%s, term moving backward, %d, %d", s.id(), term, e.Term)
}
index = e.Index
term = e.Term
}
}
s.lastApplied.Lock()
defer s.lastApplied.Unlock()
if s.lastApplied.index+1 != entries[0].Index {
plog.Panicf("%s, gap between batches, %d, %d", s.id())
}
if s.lastApplied.term > entries[0].Term {
plog.Panicf("%s, invalid term", s.id())
}
s.lastApplied.index = entries[len(entries)-1].Index
s.lastApplied.term = entries[len(entries)-1].Term
}
}
func (s *StateMachine) savingDummySnapshot(r SSRequest) bool {
return s.OnDiskStateMachine() && !r.Streaming() && !r.Exported()
}
func (s *StateMachine) checkSnapshotStatus(r SSRequest) error {
if s.aborted {
return sm.ErrSnapshotStopped
}
index := s.GetLastApplied()
if index < s.snapshotIndex {
panic("s.index < s.snapshotIndex")
}
if !s.OnDiskStateMachine() {
if !r.Exported() && index > 0 && index == s.snapshotIndex {
return raft.ErrSnapshotOutOfDate
}
}
return nil
}
func (s *StateMachine) stream(sink pb.IChunkSink) error {
var err error
var meta SSMeta
if err := func() error {
s.mu.RLock()
defer s.mu.RUnlock()
meta, err = s.prepare(SSRequest{Type: Streaming})
return err
}(); err != nil {
return err
}
return s.snapshotter.Stream(s.sm, meta, sink)
}
func (s *StateMachine) concurrentSave(r SSRequest) (pb.Snapshot, SSEnv, error) {
var err error
var meta SSMeta
if err := func() error {
s.mu.RLock()
defer s.mu.RUnlock()
meta, err = s.prepare(r)
return err
}(); err != nil {
return pb.Snapshot{}, SSEnv{}, err
}
if err := s.sync(); err != nil {
return pb.Snapshot{}, SSEnv{}, err
}
return s.doSave(meta)
}
func (s *StateMachine) save(r SSRequest) (pb.Snapshot, SSEnv, error) {
s.mu.RLock()
defer s.mu.RUnlock()
meta, err := s.prepare(r)
if err != nil {
plog.Errorf("prepare snapshot failed %v", err)
return pb.Snapshot{}, SSEnv{}, err
}
return s.doSave(meta)
}
func (s *StateMachine) prepare(r SSRequest) (SSMeta, error) {
if err := s.checkSnapshotStatus(r); err != nil {
return SSMeta{}, err
}
var err error
var ctx interface{}
if s.Concurrent() && !s.savingDummySnapshot(r) {
ctx, err = s.sm.Prepare()
if err != nil {
return SSMeta{}, err
}
}
return s.getSSMeta(ctx, r)
}
func (s *StateMachine) sync() error {
if !s.OnDiskStateMachine() {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
index := s.GetLastApplied()
if err := s.sm.Sync(); err != nil {
return err
}
s.setSyncedIndex(index)
return nil
}
func (s *StateMachine) doSave(meta SSMeta) (pb.Snapshot, SSEnv, error) {
ss, env, err := s.snapshotter.Save(s.sm, meta)
if err != nil {
plog.Errorf("%s snapshotter.Save failed %v", s.id(), err)
return pb.Snapshot{}, env, err
}
s.snapshotIndex = meta.Index
return ss, env, nil
}
func getEntryTypes(entries []pb.Entry) (bool, bool) {
allUpdate := true
allNoOP := true
for _, v := range entries {
if allUpdate && !v.IsUpdateEntry() {
allUpdate = false
}
if allNoOP && !v.IsNoOPSession() {
allNoOP = false
}
}
return allUpdate, allNoOP
}
func (s *StateMachine) handle(t []Task, a []sm.Entry) error {
batch := batchedEntryApply && s.Concurrent()
for idx := range t {
if t[idx].IsSnapshotTask() || t[idx].isSyncTask() {
plog.Panicf("%s trying to handle a snapshot/sync request", s.id())
}
e := t[idx].Entries
update, noop := getEntryTypes(e)
if batch && update && noop {
if err := s.handleBatch(e, a); err != nil {
return err
}
} else {
for i := range e {
last := idx == len(t)-1 && i == len(e)-1
if err := s.handleEntry(e[i], last); err != nil {
return err
}
}
}
s.setLastApplied(e)
}
return nil
}
func isEmptyResult(result sm.Result) bool {
return result.Data == nil && result.Value == 0
}
func (s *StateMachine) entryInInitDiskSM(index uint64) bool {
if !s.OnDiskStateMachine() {
return false
}
return index <= s.onDiskInitIndex
}
func (s *StateMachine) setOnDiskIndex(first uint64, last uint64) {
if !s.OnDiskStateMachine() {
return
}
if first > last {
panic("first > last")
}
if first <= s.onDiskInitIndex {
plog.Panicf("first index %d, init on disk %d", first, s.onDiskInitIndex)
}
if first <= s.onDiskIndex {
plog.Panicf("first index %d, on disk index %d", first, s.onDiskIndex)
}
s.onDiskIndex = last
}
func (s *StateMachine) handleEntry(e pb.Entry, last bool) error {
if e.IsConfigChange() {
s.configChange(e)
} else {
if !e.IsSessionManaged() {
if e.IsEmpty() {
s.noop(e)
s.node.ApplyUpdate(e, sm.Result{}, false, true, last)
} else {
panic("not session managed, not empty")
}
} else {
if e.IsNewSessionRequest() {
r := s.registerSession(e)
s.node.ApplyUpdate(e, r, isEmptyResult(r), false, last)
} else if e.IsEndOfSessionRequest() {
r := s.unregisterSession(e)
s.node.ApplyUpdate(e, r, isEmptyResult(r), false, last)
} else {
if !s.entryInInitDiskSM(e.Index) {
r, ignored, rejected, err := s.update(e)
if err != nil {
return err
}
if !ignored {
s.node.ApplyUpdate(e, r, rejected, ignored, last)
}
} else {
// treat it as a NoOP entry
s.noop(pb.Entry{Index: e.Index, Term: e.Term})
}
}
}
}
return nil
}
func (s *StateMachine) onApplied(e pb.Entry,
result sm.Result, ignored bool, rejected bool, last bool) {
if !ignored {
s.node.ApplyUpdate(e, result, rejected, ignored, last)
}
}
func (s *StateMachine) handleBatch(input []pb.Entry, ents []sm.Entry) error {
if len(ents) != 0 {
panic("ents is not empty")
}
s.mu.Lock()
defer s.mu.Unlock()
skipped := 0
for _, e := range input {
if !s.entryInInitDiskSM(e.Index) {
ents = append(ents, sm.Entry{
Index: e.Index,
Cmd: GetPayload(e),
})
} else {
skipped++
s.setApplied(e.Index, e.Term)
}
}
if len(ents) > 0 {
results, err := s.sm.BatchedUpdate(ents)
if err != nil {
return err
}
for idx, e := range results {
ce := input[skipped+idx]
if ce.Index != e.Index {
// probably because user modified the Index value in results
plog.Panicf("%s alignment error, %d, %d, %d",
s.id(), ce.Index, e.Index, skipped)
}
last := ce.Index == input[len(input)-1].Index
s.onApplied(ce, e.Result, false, false, last)
s.setApplied(ce.Index, ce.Term)
}
s.setOnDiskIndex(ents[0].Index, ents[len(ents)-1].Index)
}
return nil
}
func (s *StateMachine) configChange(e pb.Entry) {
var cc pb.ConfigChange
if err := cc.Unmarshal(e.Cmd); err != nil {
panic(err)
}
rejected := true
func() {
s.mu.Lock()
defer s.mu.Unlock()
defer s.setApplied(e.Index, e.Term)
if s.members.handleConfigChange(cc, e.Index) {
rejected = false
}
}()
s.node.ApplyConfigChange(cc, e.Key, rejected)
}
func (s *StateMachine) registerSession(e pb.Entry) sm.Result {
s.mu.Lock()
defer s.mu.Unlock()
defer s.setApplied(e.Index, e.Term)
return s.sessions.RegisterClientID(e.ClientID)
}
func (s *StateMachine) unregisterSession(e pb.Entry) sm.Result {
s.mu.Lock()
defer s.mu.Unlock()
defer s.setApplied(e.Index, e.Term)
return s.sessions.UnregisterClientID(e.ClientID)
}
func (s *StateMachine) noop(e pb.Entry) {
s.mu.Lock()
defer s.mu.Unlock()
defer s.setApplied(e.Index, e.Term)
if !e.IsEmpty() || e.IsSessionManaged() {
panic("handle empty event called on non-empty event")
}
}
// result is a tuple of (result, should ignore, rejected, error)