-
Notifications
You must be signed in to change notification settings - Fork 451
/
main.go
178 lines (154 loc) · 5.41 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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/m3db/m3/src/cmd/services/r2ctl/config"
"github.com/m3db/m3/src/ctl/auth"
"github.com/m3db/m3/src/ctl/server/http"
"github.com/m3db/m3/src/ctl/service/health"
"github.com/m3db/m3/src/ctl/service/r2"
"github.com/m3db/m3/src/x/clock"
xconfig "github.com/m3db/m3/src/x/config"
"github.com/m3db/m3/src/x/config/configflag"
"github.com/m3db/m3/src/x/etcd"
"github.com/m3db/m3/src/x/instrument"
)
const (
portEnvVar = "R2CTL_PORT"
r2apiPrefix = "/r2/v1/"
gracefulShutdownTimeout = 15 * time.Second
)
func main() {
configOpts := configflag.Options{
ConfigFiles: configflag.FlagStringSlice{Value: []string{"m3ctl.yml"}},
}
configOpts.Register()
flag.Parse()
// Set globals for etcd related packages.
etcd.SetGlobals()
var cfg config.Configuration
if err := configOpts.MainLoad(&cfg, xconfig.Options{}); err != nil {
// NB(r): Use fmt.Fprintf(os.Stderr, ...) to avoid etcd.SetGlobals()
// sending stdlib "log" to black hole. Don't remove unless with good reason.
fmt.Fprintf(os.Stderr, "error loading config: %v\n", err)
os.Exit(1)
}
rawLogger, err := cfg.Logging.BuildLogger()
if err != nil {
// NB(r): Use fmt.Fprintf(os.Stderr, ...) to avoid etcd.SetGlobals()
// sending stdlib "log" to black hole. Don't remove unless with good reason.
fmt.Fprintf(os.Stderr, "error creating logger: %v\n", err)
os.Exit(1)
}
defer rawLogger.Sync()
xconfig.WarnOnDeprecation(cfg, rawLogger)
logger := rawLogger.Sugar()
envPort := os.Getenv(portEnvVar)
if envPort != "" {
if p, err := strconv.Atoi(envPort); err == nil {
logger.Infof("using env supplied port var: %s=%d", portEnvVar, p)
cfg.HTTP.Port = p
} else {
logger.Fatalf("%s (%s) is not a valid port number", envPort, portEnvVar)
}
}
if cfg.HTTP.Port == 0 {
logger.Fatalf("no valid port configured. Can't start.")
}
scope, closer, err := cfg.Metrics.NewRootScope()
if err != nil {
logger.Fatalf("error creating metrics root scope: %v", err)
}
defer closer.Close()
instrumentOpts := instrument.NewOptions().
SetLogger(rawLogger).
SetMetricsScope(scope).
SetTimerOptions(instrument.TimerOptions{StandardSampleRate: cfg.Metrics.SampleRate()}).
SetReportInterval(cfg.Metrics.ReportInterval())
// Create R2 store.
storeScope := scope.SubScope("r2-store")
store, err := cfg.Store.NewR2Store(instrumentOpts.SetMetricsScope(storeScope))
if err != nil {
logger.Fatalf("error initializing backing store: %v", err)
}
// Create R2 service.
authService := auth.NewNoopAuth()
if cfg.Auth != nil {
authService = cfg.Auth.NewSimpleAuth()
}
r2ServiceScope := scope.Tagged(map[string]string{
"service-name": "r2",
})
r2ServiceInstrumentOpts := instrumentOpts.SetMetricsScope(r2ServiceScope)
r2Service := r2.NewService(
r2apiPrefix,
authService,
store,
r2ServiceInstrumentOpts,
clock.NewOptions(),
)
// Create health service.
healthServiceScope := scope.Tagged(map[string]string{
"service-name": "health",
})
healthServiceInstrumentOpts := instrumentOpts.SetMetricsScope(healthServiceScope)
healthService := health.NewService(healthServiceInstrumentOpts)
// Create HTTP server.
listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Host, cfg.HTTP.Port)
httpServerScope := scope.Tagged(map[string]string{
"server-type": "http",
})
httpServerInstrumentOpts := instrumentOpts.SetMetricsScope(httpServerScope)
httpServerOpts := cfg.HTTP.NewServerOptions(httpServerInstrumentOpts)
server, err := http.NewServer(listenAddr, httpServerOpts, r2Service, healthService)
if err != nil {
logger.Fatalf("could not create new server: %v", err)
}
logger.Infof("starting http server on: %s", listenAddr)
if err := server.ListenAndServe(); err != nil {
logger.Fatalf("could not start serving traffic: %v", err)
}
// Handle interrupts.
logger.Warnf("interrupt: %v", interrupt())
doneCh := make(chan struct{})
go func() {
server.Close()
logger.Infof("http server closed")
doneCh <- struct{}{}
}()
select {
case <-doneCh:
logger.Infof("clean shutdown")
case <-time.After(gracefulShutdownTimeout):
logger.Warnf("forced shutdown due to timeout after waiting for %v", gracefulShutdownTimeout)
}
}
func interrupt() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
return fmt.Errorf("%s", <-c)
}