-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults.go
55 lines (45 loc) · 1.07 KB
/
defaults.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
package raft
import (
"fmt"
"log"
"math/rand"
"os"
"go.etcd.io/raft/v3"
"go.etcd.io/raft/v3/raftpb"
)
var defaultLogger = &raft.DefaultLogger{Logger: log.New(os.Stderr, "raft", log.LstdFlags)}
func DefaultLogger(debug bool) raft.Logger {
if debug {
defaultLogger.EnableDebug()
}
return defaultLogger
}
func DefaultConfig() *raft.Config {
return &raft.Config{
ID: DefaultID(),
HeartbeatTick: HeartbeatTick,
ElectionTick: ElectionTick,
MaxSizePerMsg: MaxSizePerMsg,
MaxInflightMsgs: MaxInflightMsgs,
}
}
func DefaultID() uint64 {
return rand.Uint64()
}
func DefaultStorage() raft.Storage {
return raft.NewMemoryStorage()
}
func DefaultRaftStore(storage raft.Storage, hardState raftpb.HardState, entries []raftpb.Entry, snapshot raftpb.Snapshot) error {
s, ok := storage.(*raft.MemoryStorage)
if !ok {
return fmt.Errorf("failed to cast %v to raft.MemoryStorage", s)
}
s.Append(entries)
if !raft.IsEmptyHardState(hardState) {
s.SetHardState(hardState)
}
if !raft.IsEmptySnap(snapshot) {
s.ApplySnapshot(snapshot)
}
return nil
}