-
Notifications
You must be signed in to change notification settings - Fork 113
/
runtime.go
371 lines (320 loc) · 10.1 KB
/
runtime.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package runtime
import (
"fmt"
"io"
"log"
"net"
"os"
"runtime"
"strconv"
"strings"
"github.com/cs3org/reva/cmd/revad/internal/grace"
"github.com/cs3org/reva/pkg/logger"
"github.com/cs3org/reva/pkg/registry/memory"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rhttp"
"github.com/cs3org/reva/pkg/sharedconf"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/cs3org/reva/pkg/utils"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
// Run runs a reva server with the given config file and pid file.
func Run(mainConf map[string]interface{}, pidFile, logLevel string) {
logConf := parseLogConfOrDie(mainConf["log"], logLevel)
logger := initLogger(logConf)
RunWithOptions(mainConf, pidFile, WithLogger(logger))
}
// RunWithOptions runs a reva server with the given config file, pid file and options.
func RunWithOptions(mainConf map[string]interface{}, pidFile string, opts ...Option) {
options := newOptions(opts...)
parseSharedConfOrDie(mainConf["shared"])
coreConf := parseCoreConfOrDie(mainConf["core"])
// TODO: one can pass the options from the config file to registry.New() and initialize a registry based upon config files.
if options.Registry != nil {
utils.GlobalRegistry = options.Registry
} else if _, ok := mainConf["registry"]; ok {
for _, services := range mainConf["registry"].(map[string]interface{}) {
for sName, nodes := range services.(map[string]interface{}) {
for _, instance := range nodes.([]interface{}) {
if err := utils.GlobalRegistry.Add(memory.NewService(sName, instance.(map[string]interface{})["nodes"].([]interface{}))); err != nil {
panic(err)
}
}
}
}
}
run(mainConf, coreConf, options.Logger, pidFile)
}
type coreConf struct {
MaxCPUs string `mapstructure:"max_cpus"`
TracingEnabled bool `mapstructure:"tracing_enabled"`
TracingEndpoint string `mapstructure:"tracing_endpoint"`
TracingCollector string `mapstructure:"tracing_collector"`
TracingServiceName string `mapstructure:"tracing_service_name"`
// TracingService specifies the service. i.e OpenCensus, OpenTelemetry, OpenTracing...
TracingService string `mapstructure:"tracing_service"`
}
func run(mainConf map[string]interface{}, coreConf *coreConf, logger *zerolog.Logger, filename string) {
host, _ := os.Hostname()
logger.Info().Msgf("host info: %s", host)
if coreConf.TracingEnabled {
initTracing(coreConf)
}
initCPUCount(coreConf, logger)
servers := initServers(mainConf, logger)
watcher, err := initWatcher(logger, filename)
if err != nil {
log.Panic(err)
}
listeners := initListeners(watcher, servers, logger)
start(mainConf, servers, listeners, logger, watcher)
}
func initListeners(watcher *grace.Watcher, servers map[string]grace.Server, log *zerolog.Logger) map[string]net.Listener {
listeners, err := watcher.GetListeners(servers)
if err != nil {
log.Error().Err(err).Msg("error getting sockets")
watcher.Exit(1)
}
return listeners
}
func initWatcher(log *zerolog.Logger, filename string) (*grace.Watcher, error) {
watcher, err := handlePIDFlag(log, filename)
// TODO(labkode): maybe pidfile can be created later on? like once a server is going to be created?
if err != nil {
log.Error().Err(err).Msg("error creating grace watcher")
os.Exit(1)
}
return watcher, err
}
func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[string]grace.Server {
servers := map[string]grace.Server{}
if isEnabledHTTP(mainConf) {
s, err := getHTTPServer(mainConf["http"], log)
if err != nil {
log.Error().Err(err).Msg("error creating http server")
os.Exit(1)
}
servers["http"] = s
}
if isEnabledGRPC(mainConf) {
s, err := getGRPCServer(mainConf["grpc"], log)
if err != nil {
log.Error().Err(err).Msg("error creating grpc server")
os.Exit(1)
}
servers["grpc"] = s
}
if len(servers) == 0 {
log.Info().Msg("nothing to do, no grpc/http enabled_services declared in config")
os.Exit(1)
}
return servers
}
func initTracing(conf *coreConf) {
rtrace.SetTraceProvider(conf.TracingCollector, conf.TracingEndpoint)
}
func initCPUCount(conf *coreConf, log *zerolog.Logger) {
ncpus, err := adjustCPU(conf.MaxCPUs)
if err != nil {
log.Error().Err(err).Msg("error adjusting number of cpus")
os.Exit(1)
}
// log.Info().Msgf("%s", getVersionString())
log.Info().Msgf("running on %d cpus", ncpus)
}
func initLogger(conf *logConf) *zerolog.Logger {
log, err := newLogger(conf)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating logger, exiting ...")
os.Exit(1)
}
return log
}
func handlePIDFlag(l *zerolog.Logger, pidFile string) (*grace.Watcher, error) {
var opts []grace.Option
opts = append(opts, grace.WithPIDFile(pidFile))
opts = append(opts, grace.WithLogger(l.With().Str("pkg", "grace").Logger()))
w := grace.NewWatcher(opts...)
err := w.WritePID()
if err != nil {
return nil, err
}
return w, nil
}
func start(mainConf map[string]interface{}, servers map[string]grace.Server, listeners map[string]net.Listener, log *zerolog.Logger, watcher *grace.Watcher) {
if isEnabledHTTP(mainConf) {
go func() {
if err := servers["http"].(*rhttp.Server).Start(listeners["http"]); err != nil {
log.Error().Err(err).Msg("error starting the http server")
watcher.Exit(1)
}
}()
}
if isEnabledGRPC(mainConf) {
go func() {
if err := servers["grpc"].(*rgrpc.Server).Start(listeners["grpc"]); err != nil {
log.Error().Err(err).Msg("error starting the grpc server")
watcher.Exit(1)
}
}()
}
watcher.TrapSignals()
}
func newLogger(conf *logConf) (*zerolog.Logger, error) {
// TODO(labkode): use debug level rather than info as default until reaching a stable version.
// Helps having smaller development files.
if conf.Level == "" {
conf.Level = zerolog.DebugLevel.String()
}
var opts []logger.Option
opts = append(opts, logger.WithLevel(conf.Level))
w, err := getWriter(conf.Output)
if err != nil {
return nil, err
}
opts = append(opts, logger.WithWriter(w, logger.Mode(conf.Mode)))
l := logger.New(opts...)
sub := l.With().Int("pid", os.Getpid()).Logger()
return &sub, nil
}
func getWriter(out string) (io.Writer, error) {
if out == "stderr" || out == "" {
return os.Stderr, nil
}
if out == "stdout" {
return os.Stdout, nil
}
fd, err := os.OpenFile(out, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
err = errors.Wrap(err, "error creating log file: "+out)
return nil, err
}
return fd, nil
}
func getGRPCServer(conf interface{}, l *zerolog.Logger) (*rgrpc.Server, error) {
sub := l.With().Str("pkg", "rgrpc").Logger()
s, err := rgrpc.NewServer(conf, sub)
if err != nil {
err = errors.Wrap(err, "main: error creating grpc server")
return nil, err
}
return s, nil
}
func getHTTPServer(conf interface{}, l *zerolog.Logger) (*rhttp.Server, error) {
sub := l.With().Str("pkg", "rhttp").Logger()
s, err := rhttp.New(conf, sub)
if err != nil {
err = errors.Wrap(err, "main: error creating http server")
return nil, err
}
return s, nil
}
// adjustCPU parses string cpu and sets GOMAXPROCS
// according to its value. It accepts either
// a number (e.g. 3) or a percent (e.g. 50%).
// Default is to use all available cores.
func adjustCPU(cpu string) (int, error) {
var numCPU int
availCPU := runtime.NumCPU()
if cpu != "" {
if strings.HasSuffix(cpu, "%") {
// Percent
var percent float32
pctStr := cpu[:len(cpu)-1]
pctInt, err := strconv.Atoi(pctStr)
if err != nil || pctInt < 1 || pctInt > 100 {
return 0, fmt.Errorf("invalid CPU value: percentage must be between 1-100")
}
percent = float32(pctInt) / 100
numCPU = int(float32(availCPU) * percent)
} else {
// Number
num, err := strconv.Atoi(cpu)
if err != nil || num < 1 {
return 0, fmt.Errorf("invalid CPU value: provide a number or percent greater than 0")
}
numCPU = num
}
} else {
numCPU = availCPU
}
if numCPU > availCPU || numCPU == 0 {
numCPU = availCPU
}
runtime.GOMAXPROCS(numCPU)
return numCPU, nil
}
func parseCoreConfOrDie(v interface{}) *coreConf {
c := &coreConf{}
if err := mapstructure.Decode(v, c); err != nil {
fmt.Fprintf(os.Stderr, "error decoding core config: %s\n", err.Error())
os.Exit(1)
}
return c
}
func parseSharedConfOrDie(v interface{}) {
if err := sharedconf.Decode(v); err != nil {
fmt.Fprintf(os.Stderr, "error decoding shared config: %s\n", err.Error())
os.Exit(1)
}
}
func parseLogConfOrDie(v interface{}, logLevel string) *logConf {
c := &logConf{}
if err := mapstructure.Decode(v, c); err != nil {
fmt.Fprintf(os.Stderr, "error decoding log config: %s\n", err.Error())
os.Exit(1)
}
// if mode is not set, we use console mode, easier for devs
if c.Mode == "" {
c.Mode = "console"
}
// Give priority to the log level passed through the command line.
if logLevel != "" {
c.Level = logLevel
}
return c
}
type logConf struct {
Output string `mapstructure:"output"`
Mode string `mapstructure:"mode"`
Level string `mapstructure:"level"`
}
func isEnabledHTTP(conf map[string]interface{}) bool {
return isEnabled("http", conf)
}
func isEnabledGRPC(conf map[string]interface{}) bool {
return isEnabled("grpc", conf)
}
func isEnabled(key string, conf map[string]interface{}) bool {
if a, ok := conf[key]; ok {
if b, ok := a.(map[string]interface{}); ok {
if c, ok := b["services"]; ok {
if d, ok := c.(map[string]interface{}); ok {
if len(d) > 0 {
return true
}
}
}
}
}
return false
}