forked from rubrikinc/kronos
/
start.go
257 lines (235 loc) · 6.78 KB
/
start.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package cli
import (
"context"
"io/ioutil"
"net"
"net/http"
// net/http/pprof is included for profiling
_ "net/http/pprof"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/rubrikinc/kronos/kronosutil/log"
"github.com/BMWE30/kronos/oracle"
"github.com/rubrikinc/kronos/pb"
"github.com/BMWE30/kronos/server"
"github.com/rubrikinc/kronos/tm"
)
const (
advertiseHostFlag = "advertise-host"
dataDirFlag = "data-dir"
grpcPortFlag = "grpc-port"
manageOracleTickIntervalFlag = "manage-oracle-tick-interval"
oracleTimeCapDeltaFlag = "oracle-time-cap-delta"
raftPortFlag = "raft-port"
raftSnapCountFlag = "raft-snap-count"
seedHostsFlag = "seed-hosts"
)
var startCtx struct {
advertiseHost string
dataDir string
grpcPort string
manageOracleTickInterval time.Duration
oracleTimeCapDelta time.Duration
pprofAddr string
raftPort string
seedHosts string
raftSnapCount uint64
driftClock struct {
servicePort string
startConfigFile string
enable bool
}
}
var startCmd = &cobra.Command{
Use: "start",
Short: "Start a kronos node",
Long: `
Start a kronos node with synchronizes time with other kronos nodes and returns
nearly the same time on all the nodes irrespective of system time.
The peers of this node are specified via seed-hosts.
`,
Run: func(cmd *cobra.Command, args []string) { runStart() },
}
func init() {
ctx := context.TODO()
startCmd.Flags().StringVar(
&startCtx.advertiseHost,
advertiseHostFlag,
"",
"advertise-host of kronos",
)
if err := startCmd.MarkFlagRequired(advertiseHostFlag); err != nil {
log.Fatal(ctx, err)
}
startCmd.Flags().StringVar(
&startCtx.grpcPort,
grpcPortFlag,
server.KronosDefaultGRPCPort,
"GRPC port of kronos",
)
startCmd.Flags().StringVar(
&startCtx.seedHosts,
seedHostsFlag,
"",
"Comma separated list of kronos seed hosts in the cluster",
)
if err := startCmd.MarkFlagRequired(seedHostsFlag); err != nil {
log.Fatal(ctx, err)
}
startCmd.Flags().Uint64Var(
&startCtx.raftSnapCount,
raftSnapCountFlag,
0,
"Number of raft entries after which a snapshot is triggered."+
"If the value is <= 0, a default value is used",
)
startCmd.Flags().StringVar(
&startCtx.raftPort,
raftPortFlag,
server.KronosDefaultRaftPort,
"Raft port of kronos",
)
startCmd.Flags().StringVar(
&startCtx.dataDir,
dataDirFlag,
"./",
"Data directory for storing wal and snapshots",
)
startCmd.Flags().DurationVar(
&startCtx.manageOracleTickInterval,
manageOracleTickIntervalFlag,
3*time.Second,
"manageOracleTickInterval is the period where an action is taken based "+
"on the state of the oracle state machine",
)
startCmd.Flags().DurationVar(
&startCtx.oracleTimeCapDelta,
oracleTimeCapDeltaFlag,
server.DefaultOracleTimeCapDelta,
"OracleTimeCapDelta is the delta added to current KronosTime to determine the time cap."+
"No server will return a KronosTime more than the time cap known to it."+
" The time cap is persisted in the oracle state machine and is used to"+
" ensure monotonicity on cluster restarts. The delta should at least be twice"+
" as much as "+manageOracleTickIntervalFlag,
)
startCmd.Flags().BoolVar(
&startCtx.driftClock.enable,
"use-drift-clock",
false,
"Whether to use drifting clock",
)
startCmd.Flags().StringVar(
&startCtx.driftClock.servicePort,
"drift-port",
server.KronosDefaultDriftServerPort,
"Drift time service port",
)
// using a file to get a drift config so that we can avoid changing goreman
// Procfile to change the time config during the test.
startCmd.Flags().StringVar(
&startCtx.driftClock.startConfigFile,
"drift-start-config",
"",
"Drift config file to start with",
)
startCmd.Flags().StringVar(
&startCtx.pprofAddr,
"pprof-addr",
net.JoinHostPort("localhost", server.KronosDefaultPProfPort),
"Address to enable pprof on. Empty string disables pprof",
)
}
func initHTTPPprof(ctx context.Context) {
if startCtx.pprofAddr == "" {
return
}
log.Infof(ctx, "Starting http pprof. To see debug info go to http://%s/debug/pprof", startCtx.pprofAddr)
go func() {
if err := http.ListenAndServe(startCtx.pprofAddr, nil); err != nil {
log.Fatal(ctx, "HTTP pprof ListenAndServer error", err)
}
}()
}
func runStart() {
ctx := context.Background()
// oracleTimeCapDelta should at least be twice as much as
// manageOracleTickInterval otherwise KronosTime can hang by hitting
// Time Cap
tickIntervalUpperBound := startCtx.oracleTimeCapDelta / 2
if startCtx.manageOracleTickInterval > tickIntervalUpperBound {
log.Fatalf(
ctx,
"%s should at most be %s (%s / 2)",
manageOracleTickIntervalFlag,
tickIntervalUpperBound,
oracleTimeCapDeltaFlag,
)
}
initHTTPPprof(ctx)
var clock tm.Clock
if startCtx.driftClock.enable {
lis, err := net.Listen(
"tcp",
net.JoinHostPort(startCtx.advertiseHost, startCtx.driftClock.servicePort),
)
if err != nil {
log.Fatalf(ctx, "Failed to listen: %v", err)
}
server := grpc.NewServer()
var driftTimeConfig *kronospb.DriftTimeConfig
if startCtx.driftClock.startConfigFile != "" {
content, err := ioutil.ReadFile(startCtx.driftClock.startConfigFile)
if err != nil {
log.Fatalf(ctx, "Failed to read file: %s", startCtx.driftClock.startConfigFile)
}
driftTimeConfig = &kronospb.DriftTimeConfig{}
if err := protoutil.Unmarshal(content, driftTimeConfig); err != nil {
log.Fatalf(
ctx,
"Failed to unmarshal proto from file: %s",
startCtx.driftClock.startConfigFile,
)
}
}
driftServer := tm.NewUpdateDriftClockServer(driftTimeConfig)
kronospb.RegisterUpdateDriftTimeServiceServer(server, driftServer)
go func() {
if err := server.Serve(lis); err != nil {
log.Fatal(ctx, err)
}
}()
clock = driftServer.Clock
} else {
clock = tm.NewMonotonicClock()
}
config := server.Config{
Clock: clock,
ManageOracleTickInterval: startCtx.manageOracleTickInterval,
OracleTimeCapDelta: startCtx.oracleTimeCapDelta,
RaftConfig: &oracle.RaftConfig{
CertsDir: kronosCertsDir(),
DataDir: startCtx.dataDir,
GRPCHostPort: &kronospb.NodeAddr{
Host: startCtx.advertiseHost,
Port: startCtx.grpcPort,
},
RaftHostPort: &kronospb.NodeAddr{
Host: startCtx.advertiseHost,
Port: startCtx.raftPort,
},
SeedHosts: strings.Split(startCtx.seedHosts, ","),
SnapCount: startCtx.raftSnapCount,
},
}
server, err := server.NewKronosServer(ctx, config, server.GenerateNewNodeID())
if err != nil {
log.Fatal(ctx, err)
}
defer server.Stop()
if err := server.RunServer(ctx); err != nil {
log.Fatal(ctx, err)
}
}