-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathraft.go
544 lines (474 loc) · 15.4 KB
/
raft.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
package raft
import (
"context"
"os"
"time"
"github.com/shaj13/raft/internal/membership"
"github.com/shaj13/raft/internal/raftengine"
"github.com/shaj13/raft/internal/raftpb"
"github.com/shaj13/raft/internal/storage"
"github.com/shaj13/raft/internal/transport"
"github.com/shaj13/raft/raftlog"
"go.etcd.io/etcd/raft/v3"
)
// None is a placeholder node ID used to identify non-existence.
const None = raft.None
const (
// VoterMember participate in elections and log entry commitment, It is the default type.
VoterMember MemberType = raftpb.VoterMember
// RemovedMember represents an removed raft node.
RemovedMember MemberType = raftpb.RemovedMember
// LearnerMember will receive log entries, but it won't participate in elections or log entry commitment.
LearnerMember MemberType = raftpb.LearnerMember
// StagingMember will receive log entries, but it won't participate in elections or log entry commitment,
// and once it receives enough log entries to be sufficiently caught up to
// the leader's log, the leader will promote him to VoterMember.
StagingMember MemberType = raftpb.StagingMember
)
// MemberType used to distinguish members (voter, learner, etc).
type MemberType = raftpb.MemberType
// RawMember represents a raft cluster member and holds its metadata.
type RawMember = raftpb.Member
// Member represents a raft cluster member.
type Member interface {
ID() uint64
Address() string
ActiveSince() time.Time
IsActive() bool
Type() MemberType
Raw() RawMember
}
// StateMachine define an interface that must be implemented by
// application to make use of the raft replicated log.
type StateMachine = raftengine.StateMachine
// Option configures raft node using the functional options paradigm popularized by Rob Pike and Dave Cheney.
// If you're unfamiliar with this style,
// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
type Option interface {
apply(c *config)
}
// StartOption configures how we start the raft node using the functional options paradigm ,
// popularized by Rob Pike and Dave Cheney. If you're unfamiliar with this style,
// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
type StartOption interface {
apply(c *startConfig)
}
// startOptionFunc implements StartOption interface.
type startOptionFunc func(c *startConfig)
// apply the configuration to the provided config.
func (fn startOptionFunc) apply(c *startConfig) {
fn(c)
}
// OptionFunc implements Option interface.
type optionFunc func(c *config)
// apply the configuration to the provided config.
func (fn optionFunc) apply(c *config) {
fn(c)
}
// WithLinearizableReadSafe guarantees the linearizability of the read request by
// communicating with the quorum. It is the default and suggested option.
func WithLinearizableReadSafe() Option {
return optionFunc(func(c *config) {
// no-op.
})
}
// WithLinearizableReadLeaseBased ensures linearizability of the read only request by
// relying on the leader lease. It can be affected by clock drift.
// If the clock drift is unbounded, leader might keep the lease longer than it
// should (clock can move backward/pause without any bound). ReadIndex is not safe
// in that case.
func WithLinearizableReadLeaseBased() Option {
return optionFunc(func(c *config) {
c.rcfg.ReadOnlyOption = raft.ReadOnlyLeaseBased
})
}
// WithTickInterval is the time interval to,
// increments the internal logical clock for,
// the current raft member by a single tick.
//
// Default Value: 100'ms.
func WithTickInterval(d time.Duration) Option {
return optionFunc(func(c *config) {
c.tickInterval = d
})
}
// WithStreamTimeOut is the timeout on the streaming messages to other raft members.
//
// Default Value: 10's.
func WithStreamTimeOut(d time.Duration) Option {
return optionFunc(func(c *config) {
c.streamTimeOut = d
})
}
// WithDrainTimeOut is the timeout on the streaming pending messages to other raft members.
// Drain can be very useful for graceful shutdown.
//
// Default Value: 10's.
func WithDrainTimeOut(d time.Duration) Option {
return optionFunc(func(c *config) {
c.drainTimeOut = d
})
}
// WithStateDIR is the directory to store durable state (WAL logs and Snapshots).
//
// Default Value: os.TempDir().
func WithStateDIR(dir string) Option {
return optionFunc(func(c *config) {
c.statedir = dir
})
}
// WithMaxSnapshotFiles is the number of snapshots to keep beyond the
// current snapshot.
//
// Default Value: 5.
func WithMaxSnapshotFiles(max int) Option {
return optionFunc(func(c *config) {
c.maxSnapshotFiles = max
})
}
// WithSnapshotInterval is the number of log entries between snapshots.
//
// Default Value: 1000.
func WithSnapshotInterval(i uint64) Option {
return optionFunc(func(c *config) {
c.snapInterval = i
})
}
// WithElectionTick is the number of node tick (WithTickInterval) invocations that must
// pass between elections. That is, if a follower does not receive any message from the
// leader of current term before ElectionTick has elapsed, it will become candidate and
// start an election. ElectionTick must be greater than HeartbeatTick. We suggest
// ElectionTick = 10 * HeartbeatTick to avoid unnecessary leader switching.
//
// Default Value: 10.
func WithElectionTick(tick int) Option {
return optionFunc(func(c *config) {
c.rcfg.ElectionTick = tick
})
}
// WithHeartbeatTick is the number of node tick (WithTickInterval) invocations that
// must pass between heartbeats. That is, a leader sends heartbeat messages to
// maintain its leadership every HeartbeatTick ticks.
//
// Default Value: 1.
func WithHeartbeatTick(tick int) Option {
return optionFunc(func(c *config) {
c.rcfg.HeartbeatTick = tick
})
}
// WithMaxSizePerMsg limits the max byte size of each append message. Smaller
// value lowers the raft recovery cost(initial probing and message lost
// during normal operation). On the other side, it might affect the
// throughput during normal replication. Note: math.MaxUint64 for unlimited,
// 0 for at most one entry per message.
//
// Default Value: 1024 * 1024.
func WithMaxSizePerMsg(max uint64) Option {
return optionFunc(func(c *config) {
c.rcfg.MaxSizePerMsg = max
})
}
// WithMaxCommittedSizePerReady limits the size of the committed entries which
// can be applied.
//
// Default Value: 0.
func WithMaxCommittedSizePerReady(max uint64) Option {
return optionFunc(func(c *config) {
c.rcfg.MaxCommittedSizePerReady = max
})
}
// WithMaxUncommittedEntriesSize limits the aggregate byte size of the
// uncommitted entries that may be appended to a leader's log. Once this
// limit is exceeded, proposals will begin to return ErrProposalDropped
// errors. Note: 0 for no limit.
//
// Default Value: 1 << 30.
func WithMaxUncommittedEntriesSize(max uint64) Option {
return optionFunc(func(c *config) {
c.rcfg.MaxUncommittedEntriesSize = max
})
}
// WithMaxInflightMsgs limits the max number of in-flight append messages during
// optimistic replication phase. The application transportation layer usually
// has its own sending buffer over TCP/UDP. Setting MaxInflightMsgs to avoid
// overflowing that sending buffer.
//
// Default Value: 256.
func WithMaxInflightMsgs(max int) Option {
return optionFunc(func(c *config) {
c.rcfg.MaxInflightMsgs = max
})
}
// WithCheckQuorum specifies if the leader should check quorum activity. Leader
// steps down when quorum is not active for an electionTimeout.
//
// Default Value: false.
func WithCheckQuorum() Option {
return optionFunc(func(c *config) {
c.rcfg.CheckQuorum = true
})
}
// WithPreVote enables the Pre-Vote algorithm described in raft thesis section
// 9.6. This prevents disruption when a node that has been partitioned away
// rejoins the cluster.
//
// Default Value: false.
func WithPreVote() Option {
return optionFunc(func(c *config) {
c.rcfg.PreVote = true
})
}
// WithDisableProposalForwarding set to true means that followers will drop
// proposals, rather than forwarding them to the leader. One use case for
// this feature would be in a situation where the Raft leader is used to
// compute the data of a proposal, for example, adding a timestamp from a
// hybrid logical clock to data in a monotonically increasing way. Forwarding
// should be disabled to prevent a follower with an inaccurate hybrid
// logical clock from assigning the timestamp and then forwarding the data
// to the leader.
//
// Default Value: false.
func WithDisableProposalForwarding() Option {
return optionFunc(func(c *config) {
c.rcfg.DisableProposalForwarding = true
})
}
// WithContext set raft node parent ctx, The provided ctx must be non-nil.
//
// The context controls the entire lifetime of the raft node:
// obtaining a connection, sending the msgs, reading the response, and process msgs.
//
// Default Value: context.Background().
func WithContext(ctx context.Context) Option {
return optionFunc(func(c *config) {
c.ctx = ctx
})
}
// WithLogger sets logger that is used to generates lines of output.
//
// Default Value: raftlog.DefaultLogger.
func WithLogger(lg raftlog.Logger) Option {
return optionFunc(func(c *config) {
c.logger = lg
})
}
// WithPipelining is the process to send successive requests,
// over the same persistent connection, without waiting for the answer.
// This avoids latency of the connection. Theoretically,
// performance could also be improved if two or more requests were to be packed into the same connection.
//
// Note: pipelining spawn 4 goroutines per remote member connection.
func WithPipelining() Option {
return optionFunc(func(c *config) {
c.pipelining = true
})
}
// WithJoin send rpc request to join an existing cluster.
func WithJoin(addr string, timeout time.Duration) StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.Join(addr, timeout)
c.appendOperator(opr)
})
}
// WithForceJoin send rpc request to join an existing cluster even if already part of a cluster.
func WithForceJoin(addr string, timeout time.Duration) StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.ForceJoin(addr, timeout)
c.appendOperator(opr)
})
}
// WithInitCluster initialize a new cluster and create first raft node.
func WithInitCluster() StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.InitCluster()
c.appendOperator(opr)
})
}
// WithForceNewCluster initialize a new cluster from state dir. One use case for
// this feature would be in restoring cluster quorum.
//
// Note: ForceNewCluster preserve the same node id.
func WithForceNewCluster() StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.ForceNewCluster()
c.appendOperator(opr)
})
}
// WithRestore initialize a new cluster from snapshot file. One use case for
// this feature would be in restoring cluster data.
func WithRestore(path string) StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.Restore(path)
c.appendOperator(opr)
})
}
// WithRestart restart raft node from state dir.
func WithRestart() StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.Restart()
c.appendOperator(opr)
})
}
// WithMembers add the given members to the raft node.
//
// WithMembers safe to be used with initiate cluster kind options,
// ("WithForceNewCluster", "WithRestore", "WithInitCluster")
// Otherwise, it may conflicts with other options like WithJoin.
//
// As long as only one url member, WithMembers will only set the current node,
// then it will be safe to be composed with other options even "WithJoin".
//
// WithMembers and WithInitCluster must be applied to all cluster nodes when they are composed,
// Otherwise, the quorum will be lost and the cluster become unavailable.
//
// Node A:
// n.Start(WithInitCluster(), WithMembers(<node A>, <node B>))
//
// Node B:
// n.Start(WithInitCluster(), WithMembers(<node B>, <node A>))
//
// Note: first member will be assigned to the current node.
func WithMembers(membs ...RawMember) StartOption {
return startOptionFunc(func(c *startConfig) {
opr := raftengine.Members(membs...)
c.appendOperator(opr)
})
}
// WithAddress set the raft node address.
func WithAddress(addr string) StartOption {
return startOptionFunc(func(c *startConfig) {
c.addr = addr
})
}
// WithFallback can be used if other options do not succeed.
//
// WithFallback(
// WithJoin(),
// WithRestart,
// )
//
func WithFallback(opts ...StartOption) StartOption {
return startOptionFunc(func(c *startConfig) {
// create new startConfig annd apply all opts,
// then copy all operators to fallback.
nc := new(startConfig)
nc.apply(opts...)
opr := raftengine.Fallback(nc.operators...)
c.appendOperator(opr)
})
}
type startConfig struct {
operators []raftengine.Operator
addr string
}
func (c *startConfig) appendOperator(opr raftengine.Operator) {
c.operators = append(c.operators, opr)
}
func (c *startConfig) apply(opts ...StartOption) {
for _, opt := range opts {
opt.apply(c)
}
}
type config struct {
ctx context.Context
rcfg *raft.Config
tickInterval time.Duration
streamTimeOut time.Duration
drainTimeOut time.Duration
statedir string
maxSnapshotFiles int
snapInterval uint64
groupID uint64
controller transport.Controller
storage storage.Storage
pool membership.Pool
dial transport.Dial
engine raftengine.Engine
mux raftengine.Mux
fsm StateMachine
logger raftlog.Logger
pipelining bool
}
func (c *config) Logger() raftlog.Logger {
return c.logger
}
func (c *config) Context() context.Context {
return c.ctx
}
func (c *config) GroupID() uint64 {
return c.groupID
}
func (c *config) TickInterval() time.Duration {
return c.tickInterval
}
func (c *config) StreamTimeout() time.Duration {
return c.streamTimeOut
}
func (c *config) DrainTimeout() time.Duration {
return c.drainTimeOut
}
func (c *config) Snapshotter() storage.Snapshotter {
return c.storage.Snapshotter()
}
func (c *config) StateDir() string {
return c.statedir
}
func (c *config) MaxSnapshotFiles() int {
return c.maxSnapshotFiles
}
func (c *config) Controller() transport.Controller {
return c.controller
}
func (c *config) Storage() storage.Storage {
return c.storage
}
func (c *config) SnapInterval() uint64 {
return c.snapInterval
}
func (c *config) RaftConfig() *raft.Config {
return c.rcfg
}
func (c *config) Pool() membership.Pool {
return c.pool
}
func (c *config) Dial() transport.Dial {
return c.dial
}
func (c *config) Reporter() membership.Reporter {
return c.engine
}
func (c *config) StateMachine() raftengine.StateMachine {
return c.fsm
}
func (c *config) Mux() raftengine.Mux {
return c.mux
}
func (c *config) AllowPipelining() bool {
return c.pipelining
}
func newConfig(opts ...Option) *config {
c := &config{
rcfg: &raft.Config{
ElectionTick: 10,
HeartbeatTick: 1,
MaxSizePerMsg: 1024 * 1024,
MaxInflightMsgs: 256,
MaxUncommittedEntriesSize: 1 << 30,
},
ctx: context.Background(),
tickInterval: time.Millisecond * 100,
streamTimeOut: time.Second * 10,
drainTimeOut: time.Second * 10,
maxSnapshotFiles: 5,
snapInterval: 1000,
logger: raftlog.DefaultLogger,
statedir: os.TempDir(),
pipelining: false,
}
for _, opt := range opts {
opt.apply(c)
}
return c
}