-
Notifications
You must be signed in to change notification settings - Fork 451
/
server.go
532 lines (460 loc) · 16.1 KB
/
server.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright (c) 2018 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 server
import (
"context"
"fmt"
"math/rand"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/m3db/m3/src/cmd/services/m3coordinator/downsample"
dbconfig "github.com/m3db/m3/src/cmd/services/m3dbnode/config"
"github.com/m3db/m3/src/cmd/services/m3query/config"
"github.com/m3db/m3/src/dbnode/client"
"github.com/m3db/m3/src/dbnode/serialize"
"github.com/m3db/m3/src/query/api/v1/httpd"
m3dbcluster "github.com/m3db/m3/src/query/cluster/m3db"
"github.com/m3db/m3/src/query/executor"
"github.com/m3db/m3/src/query/policy/filter"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/storage/fanout"
"github.com/m3db/m3/src/query/storage/local"
"github.com/m3db/m3/src/query/storage/remote"
"github.com/m3db/m3/src/query/stores/m3db"
tsdbRemote "github.com/m3db/m3/src/query/tsdb/remote"
"github.com/m3db/m3/src/query/util/logging"
clusterclient "github.com/m3db/m3cluster/client"
etcdclient "github.com/m3db/m3cluster/client/etcd"
"github.com/m3db/m3metrics/aggregation"
"github.com/m3db/m3metrics/policy"
"github.com/m3db/m3x/clock"
xconfig "github.com/m3db/m3x/config"
"github.com/m3db/m3x/ident"
"github.com/m3db/m3x/instrument"
"github.com/m3db/m3x/pool"
xsync "github.com/m3db/m3x/sync"
xtime "github.com/m3db/m3x/time"
"github.com/pkg/errors"
"github.com/uber-go/tally"
"go.uber.org/zap"
"google.golang.org/grpc"
)
const (
defaultWorkerPoolCount = 4096
defaultWorkerPoolSize = 20
)
var (
defaultLocalConfiguration = &config.LocalConfiguration{
Namespace: "default",
Retention: 2 * 24 * time.Hour,
}
)
type cleanupFn func() error
// RunOptions provides options for running the server
// with backwards compatibility if only solely adding fields.
type RunOptions struct {
// ConfigFile is the config file to use.
ConfigFile string
// Config is an alternate way to provide configuration and will be used
// instead of parsing ConfigFile if ConfigFile is not specified.
Config config.Configuration
// DBConfig is the local M3DB config when running embedded.
DBConfig *dbconfig.DBConfiguration
// DBClient is the local M3DB client when running embedded.
DBClient <-chan client.Client
// ClusterClient is the local M3DB cluster client when running embedded.
ClusterClient <-chan clusterclient.Client
// InterruptCh is a programmatic interrupt channel to supply to
// interrupt and shutdown the server.
InterruptCh <-chan error
}
// Run runs the server programmatically given a filename for the configuration file.
func Run(runOpts RunOptions) {
rand.Seed(time.Now().UnixNano())
var cfg config.Configuration
if runOpts.ConfigFile != "" {
if err := xconfig.LoadFile(&cfg, runOpts.ConfigFile, xconfig.Options{}); err != nil {
fmt.Fprintf(os.Stderr, "unable to load %s: %v", runOpts.ConfigFile, err)
os.Exit(1)
}
} else {
cfg = runOpts.Config
}
logging.InitWithCores(nil)
ctx := context.Background()
logger := logging.WithContext(ctx)
defer logger.Sync()
scope, closer, err := cfg.Metrics.NewRootScope()
if err != nil {
logger.Fatal("could not connect to metrics", zap.Any("error", err))
}
// Close metrics scope
defer func() {
logger.Info("closing metrics scope")
if err := closer.Close(); err != nil {
logger.Error("unable to close metrics scope", zap.Error(err))
}
}()
var (
backendStorage storage.Storage
clusterClient clusterclient.Client
downsampler downsample.Downsampler
enabled bool
)
// For grpc backend, we need to setup only the grpc client and a storage accompanying that client.
// For m3db backend, we need to make connections to the m3db cluster which generates a session and use the storage with the session.
if cfg.Backend == config.GRPCStorageType {
backendStorage, enabled, err = remoteClient(cfg)
if err != nil {
logger.Fatal("unable to setup grpc backend", zap.Error(err))
}
if !enabled {
logger.Fatal("need remote clients for grpc backend")
}
logger.Info("setup grpc backend")
} else {
var cleanup cleanupFn
backendStorage, clusterClient, downsampler, cleanup, err = newM3DBStorage(runOpts, cfg, logger, scope)
if err != nil {
logger.Fatal("unable to setup m3db backend", zap.Error(err))
}
defer cleanup()
}
engine := executor.NewEngine(backendStorage)
handler, err := httpd.NewHandler(backendStorage, downsampler, engine,
clusterClient, cfg, runOpts.DBConfig, scope)
if err != nil {
logger.Fatal("unable to set up handlers", zap.Error(err))
}
if err := handler.RegisterRoutes(); err != nil {
logger.Fatal("unable to register routes", zap.Error(err))
}
listenAddress, err := cfg.ListenAddress.Resolve()
if err != nil {
logger.Fatal("unable to get listen address", zap.Error(err))
}
srv := &http.Server{Addr: listenAddress, Handler: handler.Router}
defer func() {
logger.Info("closing server")
if err := srv.Shutdown(ctx); err != nil {
logger.Error("error closing server", zap.Error(err))
}
}()
go func() {
logger.Info("starting server", zap.String("address", listenAddress))
if err := srv.ListenAndServe(); err != nil {
logger.Error("server error while listening",
zap.String("address", listenAddress), zap.Error(err))
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
var interruptCh <-chan error = make(chan error)
if runOpts.InterruptCh != nil {
interruptCh = runOpts.InterruptCh
}
select {
case <-sigChan:
case <-interruptCh:
}
}
// make connections to the m3db cluster(s) and generate sessions for those clusters along with the storage
func newM3DBStorage(
runOpts RunOptions,
cfg config.Configuration,
logger *zap.Logger,
scope tally.Scope,
) (storage.Storage, clusterclient.Client, downsample.Downsampler, cleanupFn, error) {
var clusterClientCh <-chan clusterclient.Client
if runOpts.ClusterClient != nil {
clusterClientCh = runOpts.ClusterClient
}
var (
clusterManagementClient clusterclient.Client
err error
)
if clusterClientCh == nil {
var etcdCfg *etcdclient.Configuration
switch {
case cfg.ClusterManagement != nil:
etcdCfg = &cfg.ClusterManagement.Etcd
case len(cfg.Clusters) == 1 &&
cfg.Clusters[0].Client.EnvironmentConfig.Service != nil:
etcdCfg = cfg.Clusters[0].Client.EnvironmentConfig.Service
}
if etcdCfg != nil {
// We resolved an etcd configuration for cluster management endpoints
clusterSvcClientOpts := etcdCfg.NewOptions()
clusterManagementClient, err = etcdclient.NewConfigServiceClient(clusterSvcClientOpts)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "unable to create cluster management etcd client")
}
clusterClientSendableCh := make(chan clusterclient.Client, 1)
clusterClientSendableCh <- clusterManagementClient
clusterClientCh = clusterClientSendableCh
}
}
clusters, err := initClusters(cfg, runOpts.DBClient, logger)
if err != nil {
return nil, nil, nil, nil, err
}
workerPoolCount := cfg.DecompressWorkerPoolCount
if workerPoolCount == 0 {
workerPoolCount = defaultWorkerPoolCount
}
workerPoolSize := cfg.DecompressWorkerPoolSize
if workerPoolSize == 0 {
workerPoolSize = defaultWorkerPoolSize
}
instrumentOptions := instrument.NewOptions().
SetZapLogger(logger).
SetMetricsScope(scope.SubScope("series-decompression-pool"))
poolOptions := pool.NewObjectPoolOptions().
SetSize(workerPoolCount).
SetInstrumentOptions(instrumentOptions)
objectPool := pool.NewObjectPool(poolOptions)
objectPool.Init(func() interface{} {
workerPool := xsync.NewWorkerPool(workerPoolSize)
workerPool.Init()
return workerPool
})
fanoutStorage, storageCleanup, err := newStorages(logger, clusters, cfg, objectPool)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "unable to set up storages")
}
var clusterClient clusterclient.Client
if clusterClientCh != nil {
// Only use a cluster client if we are going to receive one, that
// way passing nil to httpd NewHandler disables the endpoints entirely
clusterClient = m3dbcluster.NewAsyncClient(func() (clusterclient.Client, error) {
return <-clusterClientCh, nil
}, nil)
}
var (
namespaces = clusters.ClusterNamespaces()
downsampler downsample.Downsampler
)
if n := namespaces.NumAggregatedClusterNamespaces(); n > 0 {
logger.Info("configuring downsampler to use with aggregated cluster namespaces",
zap.Int("numAggregatedClusterNamespaces", n))
autoMappingRules, err := newDownsamplerAutoMappingRules(namespaces)
if err != nil {
return nil, nil, nil, nil, err
}
downsampler, err = newDownsampler(clusterManagementClient,
fanoutStorage, autoMappingRules, instrumentOptions)
if err != nil {
return nil, nil, nil, nil, err
}
}
cleanup := func() error {
lastErr := storageCleanup()
// Don't want to quit on the first error since the full cleanup is important
if lastErr != nil {
logger.Error("error during storage cleanup", zap.Error(lastErr))
}
if err := clusters.Close(); err != nil {
lastErr = errors.Wrap(err, "unable to close M3DB cluster sessions")
// Make sure the previous error is at least logged
logger.Error("error during cluster cleanup", zap.Error(err))
}
return lastErr
}
return fanoutStorage, clusterClient, downsampler, cleanup, nil
}
func newDownsampler(
clusterManagementClient clusterclient.Client,
storage storage.Storage,
autoMappingRules []downsample.MappingRule,
instrumentOpts instrument.Options,
) (downsample.Downsampler, error) {
if clusterManagementClient == nil {
return nil, fmt.Errorf("no configured cluster management config, " +
"must set this config for downsampler")
}
kvStore, err := clusterManagementClient.KV()
if err != nil {
return nil, errors.Wrap(err, "unable to create KV store from the "+
"cluster management config client")
}
tagEncoderOptions := serialize.NewTagEncoderOptions()
tagDecoderOptions := serialize.NewTagDecoderOptions()
tagEncoderPoolOptions := pool.NewObjectPoolOptions().
SetInstrumentOptions(instrumentOpts.
SetMetricsScope(instrumentOpts.MetricsScope().
SubScope("tag-encoder-pool")))
tagDecoderPoolOptions := pool.NewObjectPoolOptions().
SetInstrumentOptions(instrumentOpts.
SetMetricsScope(instrumentOpts.MetricsScope().
SubScope("tag-decoder-pool")))
downsampler, err := downsample.NewDownsampler(downsample.DownsamplerOptions{
Storage: storage,
RulesKVStore: kvStore,
AutoMappingRules: autoMappingRules,
ClockOptions: clock.NewOptions(),
InstrumentOptions: instrumentOpts,
TagEncoderOptions: tagEncoderOptions,
TagDecoderOptions: tagDecoderOptions,
TagEncoderPoolOptions: tagEncoderPoolOptions,
TagDecoderPoolOptions: tagDecoderPoolOptions,
})
if err != nil {
return nil, errors.Wrap(err, "unable to create downsampler")
}
return downsampler, nil
}
func newDownsamplerAutoMappingRules(
namespaces []local.ClusterNamespace,
) ([]downsample.MappingRule, error) {
var autoMappingRules []downsample.MappingRule
for _, namespace := range namespaces {
opts := namespace.Options()
attrs := opts.Attributes()
if attrs.MetricsType == storage.AggregatedMetricsType {
downsampleOpts, err := opts.DownsampleOptions()
if err != nil {
errFmt := "unable to resolve downsample options for namespace: %v"
return nil, fmt.Errorf(errFmt, namespace.NamespaceID().String())
}
if downsampleOpts.All {
storagePolicy := policy.NewStoragePolicy(attrs.Resolution,
xtime.Second, attrs.Retention)
autoMappingRules = append(autoMappingRules, downsample.MappingRule{
// NB(r): By default we will apply just keep all last values
// since coordinator only uses downsampling with Prometheus
// remote write endpoint.
// More rich static configuration mapping rules can be added
// in the future but they are currently not required.
Aggregations: []aggregation.Type{aggregation.Last},
Policies: policy.StoragePolicies{storagePolicy},
})
}
}
}
return autoMappingRules, nil
}
func initClusters(cfg config.Configuration, dbClientCh <-chan client.Client, logger *zap.Logger) (local.Clusters, error) {
var (
clusters local.Clusters
err error
)
if len(cfg.Clusters) > 0 {
opts := local.ClustersStaticConfigurationOptions{
AsyncSessions: true,
}
clusters, err = cfg.Clusters.NewClusters(opts)
if err != nil {
return nil, errors.Wrap(err, "unable to connect to clusters")
}
} else {
localCfg := cfg.Local
if localCfg == nil {
localCfg = defaultLocalConfiguration
}
if dbClientCh == nil {
return nil, errors.New("no clusters configured and not running local cluster")
}
session := m3db.NewAsyncSession(func() (client.Client, error) {
return <-dbClientCh, nil
}, nil)
clusters, err = local.NewClusters(local.UnaggregatedClusterNamespaceDefinition{
NamespaceID: ident.StringID(localCfg.Namespace),
Session: session,
Retention: localCfg.Retention,
})
if err != nil {
return nil, errors.Wrap(err, "unable to connect to clusters")
}
}
for _, namespace := range clusters.ClusterNamespaces() {
logger.Info("resolved cluster namespace",
zap.String("namespace", namespace.NamespaceID().String()))
}
return clusters, nil
}
func newStorages(
logger *zap.Logger,
clusters local.Clusters,
cfg config.Configuration,
workerPool pool.ObjectPool,
) (storage.Storage, cleanupFn, error) {
cleanup := func() error { return nil }
localStorage := local.NewStorage(clusters, workerPool)
stores := []storage.Storage{localStorage}
remoteEnabled := false
if cfg.RPC != nil && cfg.RPC.Enabled {
logger.Info("rpc enabled")
server, err := startGrpcServer(logger, localStorage, cfg.RPC)
if err != nil {
return nil, nil, err
}
cleanup = func() error {
server.GracefulStop()
return nil
}
remoteStorage, enabled, err := remoteClient(cfg)
if err != nil {
return nil, nil, err
}
if enabled {
stores = append(stores, remoteStorage)
remoteEnabled = enabled
}
}
readFilter := filter.LocalOnly
if remoteEnabled {
readFilter = filter.AllowAll
}
fanoutStorage := fanout.NewStorage(stores, readFilter, filter.LocalOnly)
return fanoutStorage, cleanup, nil
}
func remoteClient(cfg config.Configuration) (storage.Storage, bool, error) {
if cfg.RPC == nil {
return nil, false, nil
}
if remotes := cfg.RPC.RemoteListenAddresses; len(remotes) > 0 {
client, err := tsdbRemote.NewGrpcClient(remotes)
if err != nil {
return nil, false, err
}
remoteStorage := remote.NewStorage(client)
return remoteStorage, true, nil
}
return nil, false, nil
}
func startGrpcServer(logger *zap.Logger, storage storage.Storage, cfg *config.RPCConfiguration) (*grpc.Server, error) {
logger.Info("creating gRPC server")
server := tsdbRemote.CreateNewGrpcServer(storage)
waitForStart := make(chan struct{})
var startErr error
go func() {
logger.Info("starting gRPC server on port", zap.String("rpc", cfg.ListenAddress))
err := tsdbRemote.StartNewGrpcServer(server, cfg.ListenAddress, waitForStart)
// TODO: consider removing logger.Fatal here and pass back error through a channel
if err != nil {
startErr = errors.Wrap(err, "unable to start gRPC server")
}
}()
<-waitForStart
return server, startErr
}