forked from influxdata/influxdb
/
main.go
99 lines (83 loc) · 1.83 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
95
96
97
98
99
package main
import (
"coordinator"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
p "path"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, `Usage:
go run tools/inspect-raft/main.go inspect
go run tools/inspect-raft/main.go change`,
)
os.Exit(1)
}
inspect(os.Args[1:])
}
func inspect(args []string) {
flagSet := flag.NewFlagSet("inspect", flag.ExitOnError)
path := flagSet.String("path", "", "Path to the raft directory")
log := flagSet.Bool("log", false, "By default print the snapshot. If this flag is set, print the log instead")
flagSet.Parse(args)
if path == nil || *path == "" {
fmt.Fprintln(os.Stderr, "Path must be set to a value. Run inspect -h for more info")
os.Exit(1)
}
_, err := ioutil.ReadDir(p.Join(*path, "snapshot"))
if err != nil {
panic(err)
}
// TODO: open the snapshot
if !*log {
// s := load(*path)
return
}
// Open the log file and print the commands
logFile := p.Join(*path, "log.old")
f, err := os.Open(logFile)
if err != nil {
panic(err)
}
defer f.Close()
w, err := os.OpenFile(p.Join(*path, "log"), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
if err != nil {
panic(err)
}
defer w.Close()
raftPort := 8090
protobufPort := 8099
i := 0
for {
entry := &LogEntry{}
if err := entry.Decode(f); err != nil {
if err == io.EOF {
break
}
panic(err)
}
cmd := &coordinator.InfluxJoinCommand{}
if entry.LogEntry.GetCommandName() != "join" {
goto write
}
err = json.Unmarshal(entry.Command, cmd)
if err != nil {
panic(err)
}
cmd.ConnectionString = fmt.Sprintf("http://localhost:%d", raftPort+i)
cmd.ProtobufConnectionString = fmt.Sprintf("localhost:%d", protobufPort+i)
entry.Command, err = json.Marshal(cmd)
if err != nil {
panic(err)
}
i += 10
write:
if err := entry.Encode(w); err != nil {
panic(err)
}
}
}