forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
94 lines (88 loc) · 3.03 KB
/
config.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
package papillon
import (
"fmt"
"time"
)
const (
minCheckInterval = 10 * time.Millisecond
minHeartBeatInterval = time.Millisecond * 10
)
type Config struct {
ElectionTimeout time.Duration
HeartbeatTimeout time.Duration
LeaderLeaseTimeout time.Duration
ApplyBatch bool
MaxAppendEntries uint64
CommitTimeout time.Duration
SnapshotInterval time.Duration
SnapshotThreshold uint64
TrailingLogs uint64
Logger Logger
LocalID string
LeadershipCatchUpRounds uint
LeadershipLostShutDown bool
Debug bool // 开启后使用 expvar 包导出 raft 状态信息,可以使用 /debug/vars 路由进行访问
}
type ReloadableConfig struct {
TrailingLogs uint64
SnapshotInterval time.Duration
SnapshotThreshold uint64
HeartbeatTimeout time.Duration
ElectionTimeout time.Duration
}
func DefaultConfig() *Config {
return &Config{
HeartbeatTimeout: 1000 * time.Millisecond,
ElectionTimeout: 1000 * time.Millisecond,
CommitTimeout: 50 * time.Millisecond,
MaxAppendEntries: 64,
TrailingLogs: 10240,
SnapshotInterval: 120 * time.Second,
SnapshotThreshold: 8192,
LeaderLeaseTimeout: 500 * time.Millisecond,
}
}
func ValidateConfig(c *Config) (bool, string) {
if len(c.LocalID) == 0 {
return false, "LocalID is blank"
}
if c.TrailingLogs < 1 {
return false, "TrailingLogs must greater than 1"
}
if c.SnapshotThreshold < 0 {
return false, "SnapshotThreshold must greater than 0"
}
if c.MaxAppendEntries < 1 {
return false, "MaxAppendEntries must greater than 1"
}
maximumAppendEntries := uint64(1024)
if c.MaxAppendEntries > maximumAppendEntries {
return false, fmt.Sprintf("MaxAppendEntries must less than or equal to %d", maximumAppendEntries)
}
minimumTimeout := 5 * time.Millisecond
if c.HeartbeatTimeout < minimumTimeout {
return false, fmt.Sprintf("HeartbeatTimeout must greater than :%s", minimumTimeout)
}
if c.ElectionTimeout < minimumTimeout {
return false, fmt.Sprintf("ElectionTimeout must greater than :%s", minimumTimeout)
}
if c.SnapshotInterval < minimumTimeout {
return false, fmt.Sprintf("SnapshotInterval must greater than :%s", minimumTimeout)
}
if c.LeaderLeaseTimeout < minCheckInterval {
return false, fmt.Sprintf("LeaderLeaseTimeout must greater than :%s", minCheckInterval)
}
minimumCommitTimeout := time.Millisecond
if c.CommitTimeout < minimumCommitTimeout {
return false, fmt.Sprintf("CommitTimeout must greater than :%s", minimumCommitTimeout)
}
// 处理投票时实现一种租期机制,如果能正常接受到了心跳,则拒绝投票请求,如果选举超时果断则会多发送无效请求
if c.ElectionTimeout < c.HeartbeatTimeout {
return false, fmt.Sprintf("ElectionTimeout must greater than or equal HeartbeatTimeout")
}
// 确保领导者至少可以先下台
if c.LeaderLeaseTimeout > c.HeartbeatTimeout {
return false, fmt.Sprintf("LeaderLeaseTimeout must greater than or equal HeartbeatTimeout")
}
return true, ""
}