forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodehost.go
1738 lines (1639 loc) · 57.2 KB
/
nodehost.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-2019 Lei Ni ([email protected])
//
// 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 dragonboat is a multi-group Raft implementation.
The NodeHost struct is the facade interface for all features provided by the
dragonboat package. Each NodeHost instance, identified by its RaftAddress
property, usually runs on a separate host managing its CPU, storage and network
resources. Each NodeHost can manage Raft nodes from many different Raft groups
known as Raft clusters. Each Raft cluster is identified by its ClusterID Each
Raft cluster usually consists of multiple nodes, identified by their NodeID
values. Nodes from the same Raft cluster are suppose to be distributed on
different NodeHost instances across the network, this brings fault tolerance
to node failures as application data stored in such a Raft cluster can be
available as long as the majority of its managing NodeHost instances (i.e. its
underlying hosts) are available.
User applications can leverage the power of the Raft protocol implemented in
dragonboat by implementing its IStateMachine component. IStateMachine is defined in
github.com/lni/dragonboat/statemachine. Each cluster node is associated with an
IStateMachine instance, it is in charge of updating, querying and snapshotting
application data, with minimum exposure to the complexity of the Raft protocol
implementation.
User applications can use NodeHost's APIs to update the state of their
IStateMachine instances, this is called making proposals. Once accepted by the
majority nodes of a Raft cluster, the proposal is considered as committed and
it will be applied on all member nodes of the Raft cluster. Applications can
also make linearizable reads to query the state of their IStateMachine
instances. Dragonboat employs the ReadIndex protocol invented by Diego Ongaro
to implement linearizable reads. Both read and write operations can be
initiated on any member nodes, although initiating from the leader nodes incurs
the lowest overhead.
Dragonboat guarantees the linearizability of your I/O when interacting with the
IStateMachine. In plain English, writes (via making proposal) to your Raft cluster
appears to be instantaneous, once a write is completed, all later reads
(linearizable read using the ReadIndex protocol as implemented and provided in
dragonboat) should return the value of that write or a later write. Once a value
is returned by a linearizable read, all later reads should return the same value
or the result of a later write.
To strictly provide such guarantee, we need to implement the at-most-once
semantic required by linearizability. For a client, when it retries the proposal
that failed to complete before its deadline during the previous attempt, it has
the risk to have the same proposal committed and applied twice into the
IStateMachine. Dragonboat prevents this by implementing the client session concept
described in Diego Ongaro's PhD thesis.
Dragonboat is a feature complete Multi-Group Raft implementation - snapshotting,
membership change, leadership transfer and non-voting members are also provided.
Dragonboat is also extensively optimized. The Raft protocol implementation is
fully pipelined, meaning proposals can start before the completion of previous
proposals. This is critical for system throughput in high latency environment.
Dragonboat is also fully batched, it batches internal operations whenever
possible to maximize system throughput.
*/
package dragonboat // github.com/lni/dragonboat
import (
"context"
"errors"
"reflect"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/lni/dragonboat/client"
"github.com/lni/dragonboat/config"
"github.com/lni/dragonboat/internal/cpp"
"github.com/lni/dragonboat/internal/logdb"
"github.com/lni/dragonboat/internal/raft"
"github.com/lni/dragonboat/internal/rsm"
"github.com/lni/dragonboat/internal/server"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/internal/transport"
"github.com/lni/dragonboat/internal/utils/fileutil"
"github.com/lni/dragonboat/internal/utils/lang"
"github.com/lni/dragonboat/internal/utils/logutil"
"github.com/lni/dragonboat/internal/utils/random"
"github.com/lni/dragonboat/internal/utils/stringutil"
"github.com/lni/dragonboat/internal/utils/syncutil"
"github.com/lni/dragonboat/raftio"
pb "github.com/lni/dragonboat/raftpb"
"github.com/lni/dragonboat/statemachine"
)
const (
unmanagedDeploymentID uint64 = transport.UnmanagedDeploymentID
// DragonboatMajor is the major version number
DragonboatMajor = 2
// DragonboatMinor is the minor version number
DragonboatMinor = 0
// DragonboatPatch is the patch version number
DragonboatPatch = 1
)
var (
// NodeHostInfoReportSecond defines how often NodeHost reports its info to
// optional Master servers. It is defined in the number of seconds.
NodeHostInfoReportSecond uint64 = settings.Soft.NodeHostInfoReportSecond
persistentLogReportCycle uint64 = settings.Soft.PersisentLogReportCycle
receiveQueueSize uint64 = settings.Soft.RaftNodeReceiveQueueLength
setDeploymentIDTimeoutSecond uint64 = settings.Soft.SetDeploymentIDTimeoutSecond
delaySampleRatio uint64 = settings.Soft.LatencySampleRatio
rsPoolSize uint64 = settings.Soft.NodeHostSyncPoolSize
streamConnections uint64 = settings.Soft.StreamConnections
monitorInterval = 100 * time.Millisecond
)
var (
// ErrClusterNotFound indicates that the specified cluster is not found.
ErrClusterNotFound = errors.New("cluster not found")
// ErrClusterAlreadyExist indicates that the specified cluster already exist.
ErrClusterAlreadyExist = errors.New("cluster already exist")
// ErrInvalidClusterSettings indicates that cluster settings specified for
// the StartCluster method are invalid.
ErrInvalidClusterSettings = errors.New("cluster settings are invalid")
// ErrDeadlineNotSet indicates that the context parameter provided does not
// carry a deadline.
ErrDeadlineNotSet = errors.New("deadline not set")
// ErrInvalidDeadline indicates that the specified deadline is invalid, e.g.
// time in the past.
ErrInvalidDeadline = errors.New("invalid deadline")
)
// MasterClientFactoryFunc is the factory function for creating a new
// IMasterClient instance.
type MasterClientFactoryFunc func(*NodeHost) IMasterClient
// NodeHost manages Raft clusters and enables them to share resources such as
// transport and persistent storage etc. NodeHost is also the central access
// point for Dragonboat functionalities provided to applications.
type NodeHost struct {
tick uint64
msgCount uint64
testPartitionState
clusterMu struct {
sync.RWMutex
stopped bool
csi uint64
clusters sync.Map
requests map[uint64]*server.MessageQueue
}
snapshotStatus *snapshotFeedback
cancel context.CancelFunc
serverCtx *server.Context
nhConfig config.NodeHostConfig
stopper *syncutil.Stopper
duStopper *syncutil.Stopper
nodes *transport.Nodes
region string
masterClient IMasterClient
deploymentID uint64
rsPool []*sync.Pool
execEngine *execEngine
logdb raftio.ILogDB
transport transport.ITransport
msgHandler *messageHandler
initializedC chan struct{}
transportLatency *sample
}
// NewNodeHost creates a new NodeHost instance. The returned NodeHost instance
// is configured using the specified NodeHostConfig instance. In a typical
// application, it is expected to have one NodeHost on each server.
func NewNodeHost(nhConfig config.NodeHostConfig) *NodeHost {
return NewNodeHostWithMasterClientFactory(nhConfig, nil)
}
// NewNodeHostWithMasterClientFactory creates a new NodeHost instance and uses
// the specified MasterClientFactoryFunc function to create the optional master
// client. Master server details should be provided in the MasterServers field
// in nhConfig. Note that Master Client and Master Servers are both optional.
func NewNodeHostWithMasterClientFactory(nhConfig config.NodeHostConfig,
factory MasterClientFactoryFunc) *NodeHost {
logBuildTagsAndVersion()
if err := nhConfig.Validate(); err != nil {
plog.Panicf("invalid nodehost config, %v", err)
}
nh := &NodeHost{
serverCtx: server.NewContext(nhConfig),
nhConfig: nhConfig,
stopper: syncutil.NewStopper(),
duStopper: syncutil.NewStopper(),
nodes: transport.NewNodes(streamConnections),
initializedC: make(chan struct{}),
transportLatency: newSample(),
}
nh.snapshotStatus = newSnapshotFeedback(nh.pushSnapshotStatus)
nh.msgHandler = newNodeHostMessageHandler(nh)
nh.clusterMu.requests = make(map[uint64]*server.MessageQueue)
nh.createPools()
nh.createTransport()
if nhConfig.MasterMode() {
plog.Infof("master servers specified, creating the master client")
nh.masterClient = newMasterClient(nh, factory)
plog.Infof("master client type: %s", nh.masterClient.Name())
} else {
plog.Infof("no master server specified, running in standalone mode")
did := unmanagedDeploymentID
if nhConfig.DeploymentID == 0 {
plog.Warningf("DeploymentID not set in NodeHostConfig")
nh.transport.SetUnmanagedDeploymentID()
} else {
did = nhConfig.DeploymentID
nh.transport.SetDeploymentID(nhConfig.DeploymentID)
}
plog.Infof("DeploymentID set to %d", unmanagedDeploymentID)
nh.deploymentID = did
nh.createLogDB(nhConfig, did)
}
ctx, cancel := context.WithCancel(context.Background())
nh.cancel = cancel
initializeFn := func() {
if err := nh.initialize(ctx, nhConfig); err != nil {
if err != context.Canceled && err != ErrCanceled {
plog.Panicf("nh.initialize failed %v", err)
}
}
}
if nh.masterMode() {
plog.Infof("master mode, nh.initialize() invoked in new goroutine")
nh.stopper.RunWorker(func() {
initializeFn()
})
} else {
plog.Infof("standalone mode, nh.initialize() directly invoked")
initializeFn()
}
nh.stopper.RunWorker(func() {
nh.masterRequestHandler(ctx)
})
// info reporting worker is using a different stopper as we need to stop it
// separately during monkey test
nh.duStopper.RunWorker(func() {
nh.reportingWorker(ctx)
})
nh.stopper.RunWorker(func() {
nh.nodeMonitorMain(ctx, nhConfig)
})
nh.stopper.RunWorker(func() {
nh.tickWorkerMain()
})
if !nh.masterMode() {
nh.waitUntilInitialized()
}
nh.logNodeHostDetails()
return nh
}
// NodeHostConfig returns the NodeHostConfig instance used for configuring this
// NodeHost instance.
func (nh *NodeHost) NodeHostConfig() config.NodeHostConfig {
return nh.nhConfig
}
// RaftAddress returns the Raft address of the NodeHost instance. The
// returned RaftAddress value is used to identify this NodeHost instance. It is
// also the address used for exchanging Raft messages and snapshots between
// distributed NodeHost instances.
func (nh *NodeHost) RaftAddress() string {
return nh.nhConfig.RaftAddress
}
// Stop stops all Raft nodes managed by the NodeHost instance, closes the
// transport and persistent storage modules.
func (nh *NodeHost) Stop() {
nh.clusterMu.Lock()
nh.clusterMu.stopped = true
nh.clusterMu.Unlock()
nh.transport.RemoveMessageHandler()
allNodes := make([]raftio.NodeInfo, 0)
nh.forEachCluster(func(cid uint64, node *node) bool {
nodeInfo := raftio.NodeInfo{
ClusterID: node.clusterID,
NodeID: node.nodeID,
}
allNodes = append(allNodes, nodeInfo)
return true
})
for _, node := range allNodes {
if err := nh.StopNode(node.ClusterID, node.NodeID); err != nil {
plog.Errorf("failed to remove cluster %s",
logutil.ClusterID(node.ClusterID))
}
}
nh.cancel()
plog.Debugf("%s is going to stop the nh stopper", nh.describe())
if nh.duStopper != nil {
nh.duStopper.Stop()
}
nh.stopper.Stop()
if nh.masterClient != nil {
nh.masterClient.Stop()
}
plog.Debugf("%s is going to stop the exec engine", nh.describe())
if nh.execEngine != nil {
nh.execEngine.stop()
}
plog.Debugf("%s is going to stop the tranport module", nh.describe())
nh.transport.Stop()
plog.Debugf("%s transport module stopped", nh.describe())
if nh.logdb != nil {
nh.logdb.Close()
} else {
// in standalone mode, when Stop() is called in the same goroutine as
// NewNodeHost, is nh.longdb == nil above is not going to happen
plog.Warningf("logdb not closed")
}
plog.Debugf("logdb closed, %s is now stopped", nh.describe())
nh.serverCtx.Stop()
plog.Debugf("serverCtx stopped on %s", nh.describe())
if delaySampleRatio > 0 {
nh.logTransportLatency()
}
}
// StartCluster adds the specified Raft cluster node to the NodeHost and starts
// the node to make it ready for accepting incoming requests. The input
// parameter nodes is a map of node ID to RaftAddress for indicating what
// are initial nodes when the Raft cluster is first created. The join flag
// indicates whether the node is a new node joining an existing cluster.
// createStateMachine is a factory function for creating the IStateMachine
// instance, config is the configuration instance that will be passed to the
// underlying Raft node object, the cluster ID and node ID of the involved node
// is given in the ClusterID and NodeID fields of the config object.
//
// Note that this method is not for changing the membership of the specified
// Raft cluster, it launches a node that is already a member of the Raft
// cluster.
//
// As a summary, when -
// - starting a brand new Raft cluster with initial member nodes, set join to
// false and specify all initial member node details in the nodes map.
// - restarting an crashed or stopped node, set join to false. the content of
// the nodes map is ignored.
// - joining a new node to an existing Raft cluster, set join to true and leave
// the nodes map empty. This requires the joining node to have already been
// added as a member of the Raft cluster.
func (nh *NodeHost) StartCluster(nodes map[uint64]string,
join bool, createStateMachine func(uint64, uint64) statemachine.IStateMachine,
config config.Config) error {
stopc := make(chan struct{})
cf := func(clusterID uint64, nodeID uint64,
done <-chan struct{}) rsm.IManagedStateMachine {
sm := createStateMachine(clusterID, nodeID)
return rsm.NewNativeStateMachine(sm, done)
}
return nh.startCluster(nodes, join, cf, stopc, config)
}
// StartClusterUsingPlugin adds a new cluster node to the NodeHost and start
// running the new node. Different from the StartCluster method in which you
// specify the factory function used for creating the IStateMachine instance,
// StartClusterUsingPlugin requires the full path of the CPP plugin you want
// the Raft cluster to use.
func (nh *NodeHost) StartClusterUsingPlugin(nodes map[uint64]string,
join bool, pluginFilename string, config config.Config) error {
stopc := make(chan struct{})
appName := fileutil.GetAppNameFromFilename(pluginFilename)
cf := func(clusterID uint64, nodeID uint64,
done <-chan struct{}) rsm.IManagedStateMachine {
return cpp.NewStateMachineWrapper(clusterID, nodeID, appName, done)
}
return nh.startCluster(nodes, join, cf, stopc, config)
}
// StopCluster removes and stops the Raft node associated with the specified
// Raft cluster from the NodeHost. The node to be removed and stopped is
// identified by the clusterID value.
//
// Note that this is not the membership change operation to remove the node
// from the Raft cluster.
func (nh *NodeHost) StopCluster(clusterID uint64) error {
return nh.stopNode(clusterID, 0, false)
}
// StopNode removes the specified Raft cluster node from the NodeHost and
// stops that running Raft node.
//
// Note that this is not the membership change operation to remove the node
// from the Raft cluster.
func (nh *NodeHost) StopNode(clusterID uint64, nodeID uint64) error {
return nh.stopNode(clusterID, nodeID, true)
}
// SyncPropose makes a synchronous proposal on the Raft cluster specified by
// the input client session object. It returns the result code returned by
// IStateMachine's Update method, or the error encountered. The input byte slice
// can be reused for other purposes immediate after the return of this method.
//
// After calling SyncPropose, unless NO-OP client session is used, it is
// caller's responsibility to update the client session instance accordingly
// based on SyncPropose's outcome. Basically, when a ErrTimeout error is
// returned, application can retry the same proposal without updating the
// client session instance. When ErrInvalidSession error is returned, it
// usually means the session instance has been evicted from the server side,
// the Raft paper recommends to crash the client in this highly unlikely
// event. When the proposal completed successfully, caller must call
// client.ProposalCompleted() to get it ready to be used in future proposals.
func (nh *NodeHost) SyncPropose(ctx context.Context,
session *client.Session, cmd []byte) (uint64, error) {
timeout, err := getTimeoutFromContext(ctx)
if err != nil {
return 0, err
}
rs, err := nh.Propose(session, cmd, timeout)
if err != nil {
return 0, err
}
select {
case s := <-rs.CompletedC:
if s.Timeout() {
return 0, ErrTimeout
} else if s.Completed() {
rs.Release()
return s.GetResult(), nil
} else if s.Terminated() {
return 0, ErrClusterClosed
} else if s.Rejected() {
return 0, ErrInvalidSession
}
panic("unknown CompletedC value")
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return 0, ErrCanceled
} else if ctx.Err() == context.DeadlineExceeded {
return 0, ErrTimeout
}
panic("unknown ctx error")
}
}
// SyncRead performs a synchronous linearizable read on the specified Raft
// cluster. The query byte slice specifies what to query, it will be passed to
// the Lookup method of the IStateMachine after the system determines that it is
// safe to perform the local read on IStateMachine. It returns the query result
// from IStateMachine's Lookup method or the error encountered.
func (nh *NodeHost) SyncRead(ctx context.Context, clusterID uint64,
query []byte) ([]byte, error) {
v, err := nh.linearizableRead(ctx, clusterID, func() (interface{}, error) {
return nh.ReadLocal(clusterID, query)
})
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return v.([]byte), nil
}
// Membership is the struct used to describe Raft cluster membership query
// results.
type Membership struct {
// ConfigChangeID is the Raft entry index of the last applied membership
// change entry.
ConfigChangeID uint64
// Nodes is a map of NodeID values to NodeHost Raft addresses for all regular
// Raft nodes.
Nodes map[uint64]string
// Observers is a map of NodeID values to NodeHost Raft addresses for all
// observers.
Observers map[uint64]string
// Removed is a set of NodeID values that have been removed from the Raft
// cluster. They are not allowed to be added back to the cluster.
Removed map[uint64]struct{}
}
// GetClusterMembership returns the membership information from the specified
// Raft cluster. This method guarantees that the returned membership
// information is linearizable. This is a synchronous method meaning it will
// only return after its confirmed completion, failure or timeout.
func (nh *NodeHost) GetClusterMembership(ctx context.Context,
clusterID uint64) (*Membership, error) {
v, err := nh.linearizableRead(ctx, clusterID, func() (interface{}, error) {
m, err := nh.getLocalMembership(clusterID)
return m, err
})
if err != nil {
return nil, err
}
r := v.(*Membership)
return r, nil
}
// GetLeaderID returns the leader node ID of the specified Raft cluster based
// on local node's knowledge. The returned boolean value indicates whether the
// leader information is available.
func (nh *NodeHost) GetLeaderID(clusterID uint64) (uint64, bool, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return 0, false, ErrClusterNotFound
}
nodeID, valid := v.getLeaderID()
return nodeID, valid, nil
}
// GetNoOPSession returns a NO-OP client session ready to be used for
// making proposals. The NO-OP client session is a dummy client session that
// will not be checked or enforced. Use this No-OP client session when you
// want to ignore features provided by client sessions. A NO-OP client session
// is not registered on the server side and thus not required to be closed at
// the end of its life cycle.
//
// Returned NO-OP client session instance can be concurrently used in multiple
// goroutines.
//
// Use this NO-OP client session when your IStateMachine provides idempotence in
// its own implementation.
func (nh *NodeHost) GetNoOPSession(clusterID uint64) *client.Session {
return client.NewNoOPSession(clusterID, nh.serverCtx.GetRandomSource())
}
// GetNewSession starts an synchronous proposal to create, register and return
// a new client session object. A client session object is used to ensure that
// a retried proposal, e.g. proposal retried after timeout, will not be applied
// more than once into the IStateMachine.
//
// Returned client session instance should not be used concurrently. Use
// multiple client sessions when you need to concurrently start multiple
// proposals.
func (nh *NodeHost) GetNewSession(ctx context.Context,
clusterID uint64) (*client.Session, error) {
timeout, err := getTimeoutFromContext(ctx)
if err != nil {
return nil, err
}
cs := client.NewSession(clusterID, nh.serverCtx.GetRandomSource())
cs.PrepareForRegister()
rs, err := nh.ProposeSession(cs, timeout)
if err != nil {
return nil, err
}
select {
case r := <-rs.CompletedC:
if r.Completed() && r.GetResult() == cs.ClientID {
cs.PrepareForPropose()
return cs, nil
} else if r.Rejected() {
return nil, ErrRejected
} else if r.Timeout() {
return nil, ErrTimeout
} else if r.Terminated() {
return nil, ErrClusterClosed
}
plog.Panicf("unknown code value %v, result %d, client id %d",
r, r.GetResult(), cs.ClientID)
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return nil, ErrCanceled
} else if ctx.Err() == context.DeadlineExceeded {
return nil, ErrTimeout
}
}
panic("should never reach here")
}
// CloseSession closes the specified client session by unregistering it
// from the system. This is a synchronous method meaning it will only return
// after its confirmed completion, failure or timeout.
//
// Closed client session should no longer be used in future proposals.
func (nh *NodeHost) CloseSession(ctx context.Context,
session *client.Session) error {
timeout, err := getTimeoutFromContext(ctx)
if err != nil {
return err
}
session.PrepareForUnregister()
rs, err := nh.ProposeSession(session, timeout)
if err != nil {
return err
}
select {
case r := <-rs.CompletedC:
if r.Completed() && r.GetResult() == session.ClientID {
return nil
} else if r.Rejected() {
return ErrRejected
} else if r.Timeout() {
return ErrTimeout
} else if r.Terminated() {
return ErrClusterClosed
}
plog.Panicf("unknown v code %v, client id %d",
r, session.ClientID)
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return ErrCanceled
} else if ctx.Err() == context.DeadlineExceeded {
return ErrTimeout
}
}
panic("should never reach here")
}
// Propose starts an asynchronous proposal on the Raft cluster specified in the
// Session object. The input byte slice can be reused for other purposes
// immediate after the return of this method.
//
// This method returns a RequestState instance or an error immediately.
// Application can wait on the CompleteC member channel of the returned
// RequestState instance to get notified for the outcome of the proposal and
// access to the result of the proposal.
//
// After the proposal is completed, i.e. RequestResult is received from the
// CompletedC channel of the returned RequestState, unless NO-OP client session
// is used, it is caller's responsibility to update the Session instance
// accordingly based on the RequestResult.Code value. Basically, when
// RequestTimeout is returned, you can retry the same proposal without updating
// your client session instance, when a RequestRejected value is returned, it
// usually means the session instance has been evicted from the server side,
// the Raft paper recommends you to crash your client in this highly unlikely
// event. When the proposal completed successfully with a RequestCompleted
// value, application must call client.ProposalCompleted() to get the client
// session ready to be used in future proposals.
func (nh *NodeHost) Propose(session *client.Session, cmd []byte,
timeout time.Duration) (*RequestState, error) {
return nh.propose(session, cmd, nil, timeout)
}
// ProposeSession starts an asynchronous proposal on the specified cluster
// for client session related operations. Depending on the state of the client
// session object, the supported operations are for registering or unregistering
// a client session. Application can select on the CompleteC member channel of
// the returned RequestState instance to get notified for the completion and
// result of the proposal.
func (nh *NodeHost) ProposeSession(session *client.Session,
timeout time.Duration) (*RequestState, error) {
v, ok := nh.getCluster(session.ClusterID)
if !ok {
return nil, ErrClusterNotFound
}
req, err := v.proposeSession(session, nil, timeout)
nh.execEngine.setNodeReady(session.ClusterID)
return req, err
}
// ReadIndex starts the asynchronous ReadIndex protocol used for linearizable
// read on the specified cluster. This method returns a RequestState instance
// or an error immediately. Application should wait on the CompleteC channel
// of the returned RequestState object to get notified on the outcome of the
// ReadIndex operation. On a successful completion, the ReadLocal method can
// then be invoked to query the state of the IStateMachine to complete the read
// operation with linearizability guarantee.
func (nh *NodeHost) ReadIndex(clusterID uint64,
timeout time.Duration) (*RequestState, error) {
return nh.readIndex(clusterID, nil, timeout)
}
// ReadLocal queries the IStateMachine instance associated with the specified
// cluster. To ensure the linearizability of your application's I/O, ReadLocal
// should only be called after receiving a RequestCompleted notification
// from the ReadIndex method family. See ReadIndex's example for more details.
func (nh *NodeHost) ReadLocal(clusterID uint64,
query []byte) ([]byte, error) {
v, ok := nh.getClusterNotLocked(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
// translate the rsm.ErrClusterClosed to ErrClusterClosed
// internally, the IManagedStateMachine might obtain a RLock before performing
// the local read. The critical section is used to make sure we don't read
// from a destroyed C++ StateMachine object
data, err := v.sm.Lookup(query)
if err == rsm.ErrClusterClosed {
return nil, ErrClusterClosed
}
return data, err
}
// RequestDeleteNode is a Raft cluster membership change method for requesting
// the specified node to be removed from the specified Raft cluster. It starts
// an asynchronous request to remove the node from the Raft cluster membership
// list. Application can wait on the CompleteC member of the returned
// RequestState instance to get notified for the outcome.
//
// It is not guaranteed that deleted node will automatically close itself and
// be removed from its managing NodeHost instance. It is application's
// responsibility to call RemoveCluster on the right NodeHost instance to
// actually have the cluster node removed from its managing NodeHost instance.
//
// When the raft cluster is created with the OrderedConfigChange config flag
// set as false, the configChangeIndex parameter is ignored. Otherwise, it
// should be set to the most recent Config Change Index value returned by the
// GetClusterMembership method. The requested delete node operation will be
// rejected if other membership change has happened since the last call to
// GetClusterMembership.
func (nh *NodeHost) RequestDeleteNode(clusterID uint64,
nodeID uint64,
configChangeIndex uint64, timeout time.Duration) (*RequestState, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
req, err := v.requestDeleteNodeWithOrderID(nodeID, configChangeIndex, timeout)
nh.execEngine.setNodeReady(clusterID)
return req, err
}
// RequestAddNode is a Raft cluster membership change method for requesting the
// specified node to be added to the specified Raft cluster. It starts an
// asynchronous request to add the node to the Raft cluster membership list.
// Application can wait on the CompleteC member of the returned RequestState
// instance to get notified for the outcome.
//
// If there is already an observer with the same nodeID in the cluster, it will
// be promoted to a regular node with voting power. The address parameter of the
// RequestAddNode call is ignored when promoting an observer to a regular node.
//
// After the node is successfully added to the Raft cluster, it is application's
// responsibility to call StartCluster on the right NodeHost instance to actually
// start the Raft cluster node.
//
// When the raft cluster is created with the OrderedConfigChange config flag
// set as false, the configChangeIndex parameter is ignored. Otherwise, it
// should be set to the most recent Config Change Index value returned by the
// GetClusterMembership method. The requested add node operation will be
// rejected if other membership change has happened since the last call to
// GetClusterMembership.
func (nh *NodeHost) RequestAddNode(clusterID uint64,
nodeID uint64, address string, configChangeIndex uint64,
timeout time.Duration) (*RequestState, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
req, err := v.requestAddNodeWithOrderID(nodeID,
address, configChangeIndex, timeout)
nh.execEngine.setNodeReady(clusterID)
return req, err
}
// RequestAddObserver is a Raft cluster membership change method for requesting
// the specified node to be added to the specified Raft cluster as an observer
// without voting power. It starts an asynchronous request to add the specified
// node as an observer.
//
// Such observer is able to receive replicated states from the leader node, but
// it is not allowed to vote for leader, it is not considered as a part of
// the quorum when replicating state. An observer can be promoted to a regular
// node with voting power by making a RequestAddNode call using its clusterID
// and nodeID values. An observer can be removed from the cluster by calling
// RequestDeleteNode with its clusterID and nodeID values.
//
// Application should later call StartCluster with config.Config.IsObserver
// set to true on the right NodeHost to actually start the observer instance.
//
// When the raft cluster is created with the OrderedConfigChange config flag
// set as false, the configChangeIndex parameter is ignored. Otherwise, it
// should be set to the most recent Config Change Index value returned by the
// GetClusterMembership method. The requested add observer operation will be
// rejected if other membership change has happened since the last call to
// GetClusterMembership.
func (nh *NodeHost) RequestAddObserver(clusterID uint64,
nodeID uint64, address string, configChangeIndex uint64,
timeout time.Duration) (*RequestState, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
req, err := v.requestAddObserverWithOrderID(nodeID,
address, configChangeIndex, timeout)
nh.execEngine.setNodeReady(clusterID)
return req, err
}
// RequestLeaderTransfer makes a request to transfer the leadership of the
// specified Raft cluster to the target node identified by targetNodeID. It
// returns an error if the request fails to be started. There is no guarantee
// that such request can be fulfilled, i.e. the leadership transfer can still
// fail after a successful return of the RequestLeaderTransfer method.
func (nh *NodeHost) RequestLeaderTransfer(clusterID uint64,
targetNodeID uint64) error {
v, ok := nh.getCluster(clusterID)
if !ok {
return ErrClusterNotFound
}
plog.Infof("RequestLeaderTransfer called on cluster %d target nodeid %d",
clusterID, targetNodeID)
v.requestLeaderTransfer(targetNodeID)
return nil
}
// GetNodeUser returns an INodeUser instance ready to be used to directly make
// proposals or read index operations without locating the node repeatedly in
// the NodeHost. A possible use case is when loading a large data set say with
// billions of proposals into the dragonboat based system.
func (nh *NodeHost) GetNodeUser(clusterID uint64) (INodeUser, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
nu := &nodeUser{
nh: nh,
node: v,
setNodeReady: nh.execEngine.setNodeReady,
}
return nu, nil
}
// HasNodeInfo returns a boolean value indicating whether the specified node
// has been bootstrapped on the current NodeHost instance.
func (nh *NodeHost) HasNodeInfo(clusterID uint64, nodeID uint64) bool {
_, err := nh.logdb.GetBootstrapInfo(clusterID, nodeID)
if err == raftio.ErrNoBootstrapInfo {
return false
}
if err != nil {
panic(err)
}
return true
}
func (nh *NodeHost) propose(s *client.Session,
cmd []byte, handler ICompleteHandler,
timeout time.Duration) (*RequestState, error) {
var st time.Time
sampled := delaySampled(s)
if sampled {
st = time.Now()
}
c, ok := nh.clusterMu.clusters.Load(s.ClusterID)
if !ok {
return nil, ErrClusterNotFound
}
v := c.(*node)
req, err := v.propose(s, cmd, handler, timeout)
nh.execEngine.setNodeReady(s.ClusterID)
if sampled {
nh.execEngine.ProposeDelay(s.ClusterID, st)
}
return req, err
}
func (nh *NodeHost) readIndex(clusterID uint64,
handler ICompleteHandler, timeout time.Duration) (*RequestState, error) {
n, ok := nh.getClusterNotLocked(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
req, err := n.read(handler, timeout)
if err != nil {
return nil, err
}
n.increaseReadReqCount()
nh.execEngine.setNodeReady(clusterID)
return req, err
}
func (nh *NodeHost) linearizableRead(ctx context.Context,
clusterID uint64, f func() (interface{}, error)) (interface{}, error) {
timeout, err := getTimeoutFromContext(ctx)
if err != nil {
return nil, err
}
rs, err := nh.ReadIndex(clusterID, timeout)
if err != nil {
return nil, err
}
select {
case s := <-rs.CompletedC:
if s.Timeout() {
return nil, ErrTimeout
} else if s.Completed() {
rs.Release()
return f()
} else if s.Terminated() {
return nil, ErrClusterClosed
}
panic("unknown completedc code")
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return nil, ErrCanceled
} else if ctx.Err() == context.DeadlineExceeded {
return nil, ErrTimeout
}
panic("unknown ctx error")
}
}
func (nh *NodeHost) getClusterNotLocked(clusterID uint64) (*node, bool) {
v, ok := nh.clusterMu.clusters.Load(clusterID)
if !ok {
return nil, false
}
return v.(*node), true
}
func (nh *NodeHost) getClusterAndQueueNotLocked(clusterID uint64) (*node,
*server.MessageQueue, bool) {
nh.clusterMu.RLock()
defer nh.clusterMu.RUnlock()
v, ok := nh.getClusterNotLocked(clusterID)
if !ok {
return nil, nil, false
}
q, ok := nh.clusterMu.requests[clusterID]
if !ok {
return nil, nil, false
}
return v, q, true
}
func (nh *NodeHost) getCluster(clusterID uint64) (*node, bool) {
nh.clusterMu.RLock()
v, ok := nh.clusterMu.clusters.Load(clusterID)
nh.clusterMu.RUnlock()
if !ok {
return nil, false
}
return v.(*node), true
}
func (nh *NodeHost) getLocalMembership(clusterID uint64) (*Membership, error) {
v, ok := nh.getCluster(clusterID)
if !ok {
return nil, ErrClusterNotFound
}
members, observers, removed, confChangeID := v.sm.GetMembership()
membership := &Membership{
Nodes: members,
Observers: observers,
Removed: removed,
ConfigChangeID: confChangeID,
}
return membership, nil
}
func (nh *NodeHost) forEachClusterRun(bf func() bool,
af func() bool, f func(uint64, *node) bool) {
nh.clusterMu.RLock()
defer nh.clusterMu.RUnlock()
if bf != nil {
if !bf() {
return
}
}
nh.clusterMu.clusters.Range(func(k, v interface{}) bool {
return f(k.(uint64), v.(*node))
})
if af != nil {
if !af() {
return
}
}
}
func (nh *NodeHost) forEachCluster(f func(uint64, *node) bool) {
nh.forEachClusterRun(nil, nil, f)
}
// there are two major reasons to bootstrap the cluster
// 1. check whether user is incorrectly specifying the startCluster parameters,
// e.g. call startCluster with join=true first, restart the NodeHost then
// call startCluster again with join=false and len(nodes) > 0
// 2. when restarting a node which is a part of the initial cluster members,
// we should allow the caller not to provide a non-empty nodes with all
// initial member info in it, but when it is necessary to bootstrap at
// the raft node level again, we need to get the initial member info from
// somewhere. bootstrap is the process that records such info
// 3. bootstrap record is used as the node info record in our default Log DB
// implementation
func (nh *NodeHost) bootstrapCluster(nodes map[uint64]string,
join bool, config config.Config) (map[uint64]string, bool, error) {
binfo, err := nh.logdb.GetBootstrapInfo(config.ClusterID, config.NodeID)
// bootstrap the cluster by recording a bootstrap info rec into the LogDB
if err == raftio.ErrNoBootstrapInfo {
var members map[uint64]string
if !join {
members = nodes
}
bootstrap := pb.Bootstrap{
Join: join,
Addresses: make(map[uint64]string),
}
for nid, addr := range nodes {
bootstrap.Addresses[nid] = stringutil.CleanAddress(addr)
}
err = nh.logdb.SaveBootstrapInfo(config.ClusterID,
config.NodeID, bootstrap)
plog.Infof("bootstrap for %s found node not bootstrapped, %v",
logutil.DescribeNode(config.ClusterID, config.NodeID), members)
return members, !join, err