This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
392 lines (342 loc) · 12 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// qan-api2
// Copyright (C) 2019 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"context"
_ "expvar" // register /debug/vars
"fmt"
"html/template"
"log"
"net"
"net/http"
_ "net/http/pprof" // register /debug/pprof
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
grpc_gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jmoiron/sqlx"
qanpb "github.com/percona/pmm/api/qanpb"
"github.com/percona/pmm/utils/sqlmetrics"
"github.com/percona/pmm/version"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"google.golang.org/grpc"
channelz "google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/percona/qan-api2/models"
aservice "github.com/percona/qan-api2/services/analytics"
rservice "github.com/percona/qan-api2/services/receiver"
"github.com/percona/qan-api2/utils/interceptors"
"github.com/percona/qan-api2/utils/logger"
)
const (
shutdownTimeout = 3 * time.Second
defaultDsnF = "clickhouse://%s?database=%s&block_size=%s&pool_size=%s"
maxIdleConns = 5
maxOpenConns = 10
)
// runGRPCServer runs gRPC server until context is canceled, then gracefully stops it.
func runGRPCServer(ctx context.Context, db *sqlx.DB, mbm *models.MetricsBucket, bind string) {
l := logrus.WithField("component", "gRPC")
lis, err := net.Listen("tcp", bind)
if err != nil {
l.Fatalf("Cannot start gRPC server on: %v", err)
}
l.Infof("Starting server on http://%s/ ...", bind)
rm := models.NewReporter(db)
mm := models.NewMetrics(db)
grpcServer := grpc.NewServer(
// Do not increase that value. If larger requests are required (there are errors in logs),
// implement request slicing on pmm-managed side:
// send B/N requests with N buckets in each instead of 1 huge request with B buckets.
grpc.MaxRecvMsgSize(20*1024*1024), //nolint:gomnd
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
interceptors.Unary,
grpc_validator.UnaryServerInterceptor(),
)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
interceptors.Stream,
grpc_validator.StreamServerInterceptor(),
)),
)
aserv := aservice.NewService(rm, mm)
qanpb.RegisterCollectorServer(grpcServer, rservice.NewService(mbm))
qanpb.RegisterProfileServer(grpcServer, aserv)
qanpb.RegisterObjectDetailsServer(grpcServer, aserv)
qanpb.RegisterMetricsNamesServer(grpcServer, aserv)
qanpb.RegisterFiltersServer(grpcServer, aserv)
reflection.Register(grpcServer)
if l.Logger.GetLevel() >= logrus.DebugLevel {
l.Debug("Reflection and channelz are enabled.")
reflection.Register(grpcServer)
channelz.RegisterChannelzServiceToServer(grpcServer)
l.Debug("RPC response latency histogram enabled.")
grpc_prometheus.EnableHandlingTimeHistogram()
}
grpc_prometheus.Register(grpcServer)
// run server until it is stopped gracefully or not
go func() {
for {
err = grpcServer.Serve(lis)
if err == nil || err == grpc.ErrServerStopped {
break
}
l.Errorf("Failed to serve: %s", err)
}
l.Info("Server stopped.")
}()
<-ctx.Done()
// try to stop server gracefully, then not
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
go func() {
<-ctx.Done()
grpcServer.Stop()
}()
grpcServer.GracefulStop()
cancel()
}
// runJSONServer runs gRPC-gateway until context is canceled, then gracefully stops it.
func runJSONServer(ctx context.Context, grpcBindF, jsonBindF string) {
l := logrus.WithField("component", "JSON")
l.Infof("Starting server on http://%s/ ...", jsonBindF)
marshaller := &grpc_gateway.JSONPb{
MarshalOptions: protojson.MarshalOptions{ //nolint:exhaustivestruct
UseEnumNumbers: false,
EmitUnpopulated: false,
UseProtoNames: true,
Indent: " ",
},
UnmarshalOptions: protojson.UnmarshalOptions{ //nolint:exhaustivestruct
DiscardUnknown: true,
},
}
proxyMux := grpc_gateway.NewServeMux(
grpc_gateway.WithMarshalerOption(grpc_gateway.MIMEWildcard, marshaller),
)
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
type registrar func(context.Context, *grpc_gateway.ServeMux, string, []grpc.DialOption) error
for _, r := range []registrar{
qanpb.RegisterObjectDetailsHandlerFromEndpoint,
qanpb.RegisterProfileHandlerFromEndpoint,
qanpb.RegisterMetricsNamesHandlerFromEndpoint,
qanpb.RegisterFiltersHandlerFromEndpoint,
} {
if err := r(ctx, proxyMux, grpcBindF, opts); err != nil {
l.Panic(err)
}
}
mux := http.NewServeMux()
mux.Handle("/", proxyMux)
server := &http.Server{
Addr: jsonBindF,
ErrorLog: log.New(os.Stderr, "runJSONServer: ", 0),
Handler: mux,
}
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
l.Panic(err)
}
l.Println("Server stopped.")
}()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
if err := server.Shutdown(ctx); err != nil {
l.Errorf("Failed to shutdown gracefully: %s \n", err)
server.Close()
}
cancel()
}
// runDebugServer runs debug server until context is canceled, then gracefully stops it.
func runDebugServer(ctx context.Context, debugBindF string) {
handler := promhttp.HandlerFor(prom.DefaultGatherer, promhttp.HandlerOpts{
ErrorLog: logrus.WithField("component", "metrics"),
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle("/debug/metrics", promhttp.InstrumentMetricHandler(prom.DefaultRegisterer, handler))
l := logrus.WithField("component", "debug")
handlers := []string{
"/debug/metrics", // by http.Handle above
"/debug/vars", // by expvar
"/debug/requests", // by golang.org/x/net/trace imported by google.golang.org/grpc
"/debug/events", // by golang.org/x/net/trace imported by google.golang.org/grpc
"/debug/pprof", // by net/http/pprof
}
for i, h := range handlers {
handlers[i] = "http://" + debugBindF + h
}
var buf bytes.Buffer
err := template.Must(template.New("debug").Parse(`
<html>
<body>
<ul>
{{ range . }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</body>
</html>
`)).Execute(&buf, handlers)
if err != nil {
l.Panic(err)
}
http.HandleFunc("/debug", func(rw http.ResponseWriter, req *http.Request) {
rw.Write(buf.Bytes())
})
l.Infof("Starting server on http://%s/debug\nRegistered handlers:\n\t%s", debugBindF, strings.Join(handlers, "\n\t"))
server := &http.Server{
Addr: debugBindF,
ErrorLog: log.New(os.Stderr, "runDebugServer: ", 0),
}
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
l.Panic(err)
}
l.Info("Server stopped.")
}()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
if err := server.Shutdown(ctx); err != nil {
l.Errorf("Failed to shutdown gracefully: %s", err)
}
cancel()
}
func main() {
log.SetFlags(0)
log.SetPrefix("stdlog: ")
kingpin.Version(version.ShortInfo())
kingpin.HelpFlag.Short('h')
grpcBindF := kingpin.Flag("grpc-bind", "GRPC bind address and port").Default("127.0.0.1:9911").String()
jsonBindF := kingpin.Flag("json-bind", "JSON bind address and port").Default("127.0.0.1:9922").String()
debugBindF := kingpin.Flag("listen-debug-addr", "Debug server listen address").Default("127.0.0.1:9933").String()
dataRetentionF := kingpin.Flag("data-retention", "QAN data Retention (in days)").Default("30").Uint()
dsnF := kingpin.Flag("dsn", "ClickHouse database DSN. Can be override with database/host/port options").Default(defaultDsnF).String()
clickHouseDatabaseF := kingpin.Flag("clickhouse-name", "Clickhouse database name").Default("pmm").Envar("PERCONA_TEST_PMM_CLICKHOUSE_DATABASE").String()
clickhouseAddrF := kingpin.Flag("clickhouse-addr", "Clickhouse database address").Default("127.0.0.1:9000").Envar("PERCONA_TEST_PMM_CLICKHOUSE_ADDR").String()
clickhouseBlockSizeF := kingpin.Flag("clickhouse-block-size", "Number of rows that can be load from table in one cycle").
Default("10000").Envar("PERCONA_TEST_PMM_CLICKHOUSE_BLOCK_SIZE").String()
clickhousePoolSizeF := kingpin.Flag("clickhouse-pool-size", "Controls how much queries can be run simultaneously").
Default("2").Envar("PERCONA_TEST_PMM_CLICKHOUSE_POOL_SIZE").String()
debugF := kingpin.Flag("debug", "Enable debug logging").Bool()
traceF := kingpin.Flag("trace", "Enable trace logging (implies debug)").Bool()
kingpin.Parse()
log.Printf("%s.", version.ShortInfo())
logrus.SetFormatter(&logrus.TextFormatter{
// Enable multiline-friendly formatter in both development (with terminal) and production (without terminal):
// https://github.com/sirupsen/logrus/blob/839c75faf7f98a33d445d181f3018b5c3409a45e/text_formatter.go#L176-L178
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05.000-07:00",
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
_, function = filepath.Split(f.Function)
// keep a single directory name as a compromise between brevity and unambiguity
var dir string
dir, file = filepath.Split(f.File)
dir = filepath.Base(dir)
file = fmt.Sprintf("%s/%s:%d", dir, file, f.Line)
return
},
})
if *debugF {
logrus.SetLevel(logrus.DebugLevel)
}
if *traceF {
logrus.SetLevel(logrus.TraceLevel)
grpclog.SetLoggerV2(&logger.GRPC{Entry: logrus.WithField("component", "grpclog")})
logrus.SetReportCaller(true)
}
logrus.Infof("Log level: %s.", logrus.GetLevel())
l := logrus.WithField("component", "main")
ctx, cancel := context.WithCancel(context.Background())
ctx = logger.Set(ctx, "main")
defer l.Info("Done.")
var dsn string
if *dsnF == defaultDsnF {
dsn = fmt.Sprintf(defaultDsnF, *clickhouseAddrF, *clickHouseDatabaseF, *clickhouseBlockSizeF, *clickhousePoolSizeF)
} else {
dsn = *dsnF
}
l.Info("DNS: ", dsn)
db := NewDB(dsn, maxIdleConns, maxOpenConns)
prom.MustRegister(sqlmetrics.NewCollector("clickhouse", "qan-api2", db.DB))
// handle termination signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, unix.SIGTERM, unix.SIGINT)
go func() {
s := <-signals
signal.Stop(signals)
log.Printf("Got %s, shutting down...\n", unix.SignalName(s.(unix.Signal)))
cancel()
}()
var wg sync.WaitGroup
// run ingestion in a separate goroutine
mbm := models.NewMetricsBucket(db)
prom.MustRegister(mbm)
mbmCtx, mbmCancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
mbm.Run(mbmCtx)
}()
wg.Add(1)
go func() {
defer func() {
// stop ingestion only after gRPC server is fully stopped to properly insert the last batch
mbmCancel()
wg.Done()
}()
runGRPCServer(ctx, db, mbm, *grpcBindF)
}()
wg.Add(1)
go func() {
defer wg.Done()
runJSONServer(ctx, *grpcBindF, *jsonBindF)
}()
wg.Add(1)
go func() {
defer wg.Done()
runDebugServer(ctx, *debugBindF)
}()
ticker := time.NewTicker(24 * time.Hour)
wg.Add(1)
go func() {
defer wg.Done()
for {
// Drop old partitions once in 24h.
DropOldPartition(db, *dataRetentionF)
select {
case <-ctx.Done():
return
case <-ticker.C:
// nothing
}
}
}()
wg.Wait()
}