-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (191 loc) · 5.8 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
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
package main
import (
"flag"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/ulysseses/raft"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
addr = ":3001"
cpuProfile = ""
memProfile = ""
id = uint64(0)
addresses = map[uint64]string{}
consistency = raft.ConsistencyLease
lease = 500 * time.Millisecond
enableLogging = false
debug = false
readTimeout = 3 * time.Second
proposeTimeout = 3 * time.Second
)
func init() {
flag.StringVar(&addr, "addr", addr, "address for http kv store")
flag.StringVar(&cpuProfile, "cpuProfile", cpuProfile, "write cpu profile to a file")
flag.StringVar(&memProfile, "memProfile", memProfile, "write memory profile to a file")
flag.Uint64Var(&id, "id", id, "Raft node ID")
flag.Var(
&mapValue{m: &addresses},
"addresses",
"addresses specified as a |-separated string of key-value pairs, "+
"which themselves are separated by commas. E.g. "+
"\"1,tcp://localhost:8081|2,tcp://localhost:8082|3,tcp://localhost:8083\"")
flag.Var(
&consistencyValue{c: &consistency}, "consistency",
"consistency level: lease, strict, or stale")
flag.DurationVar(&lease, "lease", lease, "lease duration; used only if -consistency=lease")
flag.BoolVar(&enableLogging, "enableLogging", enableLogging, "enable logging")
flag.BoolVar(&debug, "debug", debug, "enable debug logs")
flag.DurationVar(
&readTimeout, "readTimeout", readTimeout,
"timeout for read requests to raft cluster")
flag.DurationVar(
&proposeTimeout, "proposeTimeout", proposeTimeout,
"timeout for proposals to raft cluster")
}
func globalLogger(debug bool) *zap.Logger {
loggerCfg := zap.NewProductionConfig()
if debug {
loggerCfg.Level.SetLevel(zapcore.DebugLevel)
}
logger, err := loggerCfg.Build()
if err != nil {
panic(err)
}
return logger
}
func profile(logger *zap.Logger, cpuProfile, memProfile string) {
signalChan := make(chan os.Signal, 2)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
var cpuProfChan, memProfChan chan struct{}
var cpuProfAckChan, memProfAckChan chan struct{}
go func() {
<-signalChan
select {
case cpuProfChan <- struct{}{}:
select {
case <-cpuProfAckChan:
case <-time.After(2 * time.Second):
logger.Error("could not write cpu profile out (timed out after 2 seconds)")
}
default:
}
select {
case memProfChan <- struct{}{}:
select {
case <-memProfAckChan:
case <-time.After(2 * time.Second):
logger.Error("could not write mem profile out (timed out after 2 seconds)")
}
default:
}
os.Exit(0)
}()
if cpuProfile != "" {
cpuProfChan = make(chan struct{})
cpuProfAckChan = make(chan struct{})
logger.Info("writing cpu profile", zap.String("cpuProfile", cpuProfile))
f, err := os.Create(cpuProfile)
if err != nil {
logger.Fatal("could not create CPU profile", zap.Error(err))
}
if err := pprof.StartCPUProfile(f); err != nil {
logger.Fatal("could not start CPU profile", zap.Error(err))
}
go func(f *os.File) {
<-cpuProfChan
logger.Info("profile: caught interrupt, stopping cpu profile")
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
logger.Error("failed to close file", zap.Error(err))
}
cpuProfAckChan <- struct{}{}
}(f)
}
if memProfile != "" {
memProfChan = make(chan struct{})
memProfAckChan = make(chan struct{})
logger.Info("writing mem profile", zap.String("memProfile", memProfile))
f, err := os.Create(memProfile)
if err != nil {
logger.Fatal("could not create memory profile", zap.Error(err))
}
go func(f *os.File) {
<-memProfChan
logger.Info("profile: caught interrupt, stopping mem profile")
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
logger.Error("could not write memory profile", zap.Error(err))
}
if err := f.Close(); err != nil {
logger.Error("failed to close file", zap.Error(err))
}
memProfAckChan <- struct{}{}
}(f)
}
}
func main() {
flag.Parse()
gl := globalLogger(debug)
profile(gl, cpuProfile, memProfile)
// Configure Raft.
pConfigOpts := []raft.ProtocolConfigOption{
raft.WithConsistency(consistency),
raft.WithLease(lease),
}
tConfigOpts := []raft.TransportConfigOption{raft.WithAddresses(addresses)}
nConfigOpts := []raft.NodeConfigOption{}
pConfigOpts = append(pConfigOpts, raft.WithProtocolDebug(debug))
tConfigOpts = append(tConfigOpts, raft.WithTransportDebug(debug))
nConfigOpts = append(nConfigOpts, raft.WithNodeDebug(debug))
if enableLogging {
pConfigOpts = append(pConfigOpts, raft.AddProtocolLogger())
tConfigOpts = append(tConfigOpts, raft.AddTransportLogger())
nConfigOpts = append(nConfigOpts, raft.AddNodeLogger())
}
pConfig := raft.NewProtocolConfig(id, pConfigOpts...)
tConfig := raft.NewTransportConfig(id, tConfigOpts...)
nConfig := raft.NewNodeConfig(id, nConfigOpts...)
tr, err := tConfig.Build()
if err != nil {
gl.Fatal("Failed to build Transport", zap.Error(err))
}
psm, err := pConfig.Build(tr)
if err != nil {
gl.Fatal("Failed to build ProtocolStateMachine", zap.Error(err))
}
kvStore := newKVStore()
node, err := nConfig.Build(psm, tr, kvStore)
if err != nil {
gl.Fatal("Failed to build Node", zap.Error(err))
}
kvStore.node = node
// Start Raft node.
node.Start()
defer func() {
if err := node.Stop(); err != nil {
gl.Error("node stopped with error", zap.Error(err))
}
}()
// Start HTTPKVAPI.
httpKVAPILogger := gl
if !enableLogging {
httpKVAPILogger = nil
}
httpKVAPI := &httpKVAPI{
kvStore: kvStore,
logger: httpKVAPILogger,
readTimeout: readTimeout,
proposeTimeout: proposeTimeout,
}
gl.Info("starting http kv store backed by raft", zap.String("addr", addr))
if err := fasthttp.ListenAndServe(addr, httpKVAPI.Route); err != nil {
gl.Error("failed to listen", zap.Error(err))
}
}