-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (81 loc) · 2.25 KB
/
main.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 main
import (
"github.com/1xyz/rafty/core"
"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
"os"
"time"
)
func toInt(opts docopt.Opts, key string) uint64 {
r, err := opts.Int(key)
if err != nil {
log.Panicf("opts.Int(id) %v", err)
}
return uint64(r)
}
func init() {
// Log as JSON instead of the default ASCII formatter.
// log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.ErrorLevel)
}
func propose(raftNet *core.RaftNet, nodeId uint64, key, value string) {
log.Errorf("Propose changes.....")
raftNet.ProposeChange(nodeId, key, value)
log.Infof("** Sleeping to visualize heartbeat between nodes **")
time.Sleep(2000 * time.Millisecond)
for k, v := range raftNet.ReadFromAllNodes(key) {
log.Errorf("** Result nodeID=%v key=%v value=%v", k, key, v)
}
}
func proposeWithMultipleChanges(raftNet *core.RaftNet) {
log.Errorf("Propose changes.....")
key := "beans"
raftNet.ProposeChange(1, key, "garbanzo")
raftNet.ProposeChange(2, key, "lima")
// raftNet.ProposeChange(2, key, "lima")
time.Sleep(10 * time.Millisecond)
for k, v := range raftNet.ReadFromAllNodes(key) {
log.Errorf("** Result nodeID=%v key=%v value=%v", k, key, v)
}
raftNet.ProposeChange(2, key, "rajma")
log.Infof("** Sleeping to visualize heartbeat between nodes **")
time.Sleep(2000 * time.Millisecond)
for k, v := range raftNet.ReadFromAllNodes(key) {
log.Errorf("** Result nodeID=%v key=%v value=%v", k, key, v)
}
}
func main() {
usage := `Rafty.
Usage:
rafty standalone <node-count>
rafty -h | --help
Options:
-h --help Show this screen.`
argv := os.Args[1:]
parser := &docopt.Parser{
HelpHandler: docopt.PrintHelpOnly,
OptionsFirst: true,
}
opts, err := parser.ParseArgs(usage, argv, "")
if err != nil {
log.Errorf("error %v", err)
return
}
standalone, err := opts.Bool("standalone")
if err != nil {
log.Errorf("opts.Bool(standalone) err=%v", err)
return
}
if standalone {
nc := toInt(opts, "<node-count>")
log.Infof("node-count = %d", nc)
raftNet := core.NewRaftNet(nc)
raftNet.Run()
propose(raftNet, 1, "hello", "world")
propose(raftNet, 2, "hello", "world2")
proposeWithMultipleChanges(raftNet)
// time.Sleep(60 * time.Second)
}
}