-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathingest.go
477 lines (409 loc) · 13.7 KB
/
ingest.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
// Copyright (c) 2019 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 ingestcarbon
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"regexp"
"sync"
"time"
"github.com/m3db/m3/src/cmd/services/m3coordinator/downsample"
"github.com/m3db/m3/src/cmd/services/m3coordinator/ingest"
"github.com/m3db/m3/src/cmd/services/m3query/config"
"github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/carbon"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/query/graphite/graphite"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/ts"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/pool"
m3xserver "github.com/m3db/m3/src/x/server"
xsync "github.com/m3db/m3/src/x/sync"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
maxResourcePoolNameSize = 1024
maxPooledTagsSize = 16
defaultResourcePoolSize = 4096
)
var (
// Used for parsing carbon names into tags.
carbonSeparatorByte = byte('.')
carbonSeparatorBytes = []byte{carbonSeparatorByte}
errCannotGenerateTagsFromEmptyName = errors.New("cannot generate tags from empty name")
errIOptsMustBeSet = errors.New("carbon ingester options: instrument options must be st")
errWorkerPoolMustBeSet = errors.New("carbon ingester options: worker pool must be set")
)
// Options configures the ingester.
type Options struct {
InstrumentOptions instrument.Options
WorkerPool xsync.PooledWorkerPool
}
// CarbonIngesterRules contains the carbon ingestion rules.
type CarbonIngesterRules struct {
Rules []config.CarbonIngesterRuleConfiguration
}
// Validate validates the options struct.
func (o *Options) Validate() error {
if o.InstrumentOptions == nil {
return errIOptsMustBeSet
}
if o.WorkerPool == nil {
return errWorkerPoolMustBeSet
}
return nil
}
// NewIngester returns an ingester for carbon metrics.
func NewIngester(
downsamplerAndWriter ingest.DownsamplerAndWriter,
rules CarbonIngesterRules,
opts Options,
) (m3xserver.Handler, error) {
err := opts.Validate()
if err != nil {
return nil, err
}
tagOpts := models.NewTagOptions().SetIDSchemeType(models.TypeGraphite)
err = tagOpts.Validate()
if err != nil {
return nil, err
}
compiledRules, err := compileRules(rules)
if err != nil {
return nil, err
}
poolOpts := pool.NewObjectPoolOptions().
SetInstrumentOptions(opts.InstrumentOptions).
SetRefillLowWatermark(0).
SetRefillHighWatermark(0).
SetSize(defaultResourcePoolSize)
resourcePool := pool.NewObjectPool(poolOpts)
resourcePool.Init(func() interface{} {
return &lineResources{
name: make([]byte, 0, maxResourcePoolNameSize),
datapoints: make([]ts.Datapoint, 1),
tags: make([]models.Tag, 0, maxPooledTagsSize),
}
})
return &ingester{
downsamplerAndWriter: downsamplerAndWriter,
opts: opts,
logger: opts.InstrumentOptions.Logger(),
tagOpts: tagOpts,
metrics: newCarbonIngesterMetrics(
opts.InstrumentOptions.MetricsScope()),
rules: compiledRules,
lineResourcesPool: resourcePool,
}, nil
}
type ingester struct {
downsamplerAndWriter ingest.DownsamplerAndWriter
opts Options
logger *zap.Logger
metrics carbonIngesterMetrics
tagOpts models.TagOptions
rules []ruleAndRegex
lineResourcesPool pool.ObjectPool
}
func (i *ingester) Handle(conn net.Conn) {
var (
// Interfaces require a context be passed, but M3DB client already has timeouts
// built in and allocating a new context each time is expensive so we just pass
// the same context always and rely on M3DB client timeouts.
ctx = context.Background()
wg = sync.WaitGroup{}
s = carbon.NewScanner(conn, i.opts.InstrumentOptions)
logger = i.opts.InstrumentOptions.Logger()
)
logger.Debug("handling new carbon ingestion connection")
for s.Scan() {
name, timestamp, value := s.Metric()
resources := i.getLineResources()
// Copy name since scanner bytes are recycled.
resources.name = append(resources.name[:0], name...)
wg.Add(1)
i.opts.WorkerPool.Go(func() {
ok := i.write(ctx, resources, timestamp, value)
if ok {
i.metrics.success.Inc(1)
}
// The contract is that after the DownsamplerAndWriter returns, any resources
// that it needed to hold onto have already been copied.
i.putLineResources(resources)
wg.Done()
})
i.metrics.malformed.Inc(int64(s.MalformedCount))
s.MalformedCount = 0
}
if err := s.Err(); err != nil {
logger.Error("encountered error during carbon ingestion when scanning connection", zap.Error(err))
}
logger.Debug("waiting for outstanding carbon ingestion writes to complete")
wg.Wait()
logger.Debug("all outstanding writes completed, shutting down carbon ingestion handler")
// Don't close the connection, that is the server's responsibility.
}
func (i *ingester) write(
ctx context.Context,
resources *lineResources,
timestamp time.Time,
value float64,
) bool {
downsampleAndStoragePolicies := ingest.WriteOptions{
// Set both of these overrides to true to indicate that only the exact mapping
// rules and storage policies that we provide should be used and that all
// default behavior (like performing all possible downsamplings and writing
// all data to the unaggregated namespace in storage) should be ignored.
DownsampleOverride: true,
WriteOverride: true,
}
matched := 0
defer func() {
if matched == 0 {
// No policies matched.
debugLog := i.logger.Check(zapcore.DebugLevel, "no rules matched carbon metric, skipping")
if debugLog != nil {
debugLog.Write(zap.ByteString("name", resources.name))
}
return
}
debugLog := i.logger.Check(zapcore.DebugLevel, "successfully wrote carbon metric")
if debugLog != nil {
debugLog.Write(zap.ByteString("name", resources.name),
zap.Int("matchedRules", matched))
}
}()
for _, rule := range i.rules {
if rule.rule.Pattern == graphite.MatchAllPattern || rule.regexp.Match(resources.name) {
// Each rule should only have either mapping rules or storage policies so
// one of these should be a no-op.
downsampleAndStoragePolicies.DownsampleMappingRules = rule.mappingRules
downsampleAndStoragePolicies.WriteStoragePolicies = rule.storagePolicies
debugLog := i.logger.Check(zapcore.DebugLevel, "carbon metric matched by pattern")
if debugLog != nil {
debugLog.Write(zap.ByteString("name", resources.name),
zap.String("pattern", rule.rule.Pattern),
zap.Any("mappingRules", rule.mappingRules),
zap.Any("storagePolicies", rule.storagePolicies))
}
// Break because we only want to apply one rule per metric based on which
// ever one matches first.
err := i.writeWithOptions(ctx, resources, timestamp, value,
downsampleAndStoragePolicies)
if err != nil {
return false
}
matched++
// If continue is not specified then we matched the current set of rules.
if !rule.rule.Continue {
break
}
}
}
return matched > 0
}
func (i *ingester) writeWithOptions(
ctx context.Context,
resources *lineResources,
timestamp time.Time,
value float64,
opts ingest.WriteOptions,
) error {
resources.datapoints[0] = ts.Datapoint{Timestamp: timestamp, Value: value}
tags, err := GenerateTagsFromNameIntoSlice(resources.name, i.tagOpts, resources.tags)
if err != nil {
i.logger.Error("err generating tags from carbon",
zap.String("name", string(resources.name)), zap.Error(err))
i.metrics.malformed.Inc(1)
return err
}
err = i.downsamplerAndWriter.Write(
ctx, tags, resources.datapoints, xtime.Second, nil, opts,
)
if err != nil {
i.logger.Error("err writing carbon metric",
zap.String("name", string(resources.name)), zap.Error(err))
i.metrics.err.Inc(1)
return err
}
return nil
}
func (i *ingester) Close() {
// We don't maintain any state in-between connections so there is nothing to do here.
}
func newCarbonIngesterMetrics(m tally.Scope) carbonIngesterMetrics {
return carbonIngesterMetrics{
success: m.Counter("success"),
err: m.Counter("error"),
malformed: m.Counter("malformed"),
}
}
type carbonIngesterMetrics struct {
success tally.Counter
err tally.Counter
malformed tally.Counter
}
// GenerateTagsFromName accepts a carbon metric name and blows it up into a list of
// key-value pair tags such that an input like:
// foo.bar.baz
// becomes
// __g0__:foo
// __g1__:bar
// __g2__:baz
func GenerateTagsFromName(
name []byte,
opts models.TagOptions,
) (models.Tags, error) {
return generateTagsFromName(name, opts, nil)
}
// GenerateTagsFromNameIntoSlice does the same thing as GenerateTagsFromName except
// it allows the caller to provide the slice into which the tags are appended.
func GenerateTagsFromNameIntoSlice(
name []byte,
opts models.TagOptions,
tags []models.Tag,
) (models.Tags, error) {
return generateTagsFromName(name, opts, tags)
}
func generateTagsFromName(
name []byte,
opts models.TagOptions,
tags []models.Tag,
) (models.Tags, error) {
if len(name) == 0 {
return models.EmptyTags(), errCannotGenerateTagsFromEmptyName
}
numTags := bytes.Count(name, carbonSeparatorBytes) + 1
if cap(tags) >= numTags {
tags = tags[:0]
} else {
tags = make([]models.Tag, 0, numTags)
}
startIdx := 0
tagNum := 0
for i, charByte := range name {
if charByte == carbonSeparatorByte {
if i+1 < len(name) && name[i+1] == carbonSeparatorByte {
return models.EmptyTags(),
fmt.Errorf("carbon metric: %s has duplicate separator", string(name))
}
tags = append(tags, models.Tag{
Name: graphite.TagName(tagNum),
Value: name[startIdx:i],
})
startIdx = i + 1
tagNum++
}
}
// Write out the final tag since the for loop above will miss anything
// after the final separator. Note, that we make sure that the final
// character in the name is not the separator because in that case there
// would be no additional tag to add. I.E if the input was:
// foo.bar.baz
// then the for loop would append foo and bar, but we would still need to
// append baz, however, if the input was:
// foo.bar.baz.
// then the foor loop would have appended foo, bar, and baz already.
if name[len(name)-1] != carbonSeparatorByte {
tags = append(tags, models.Tag{
Name: graphite.TagName(tagNum),
Value: name[startIdx:],
})
}
return models.Tags{Opts: opts, Tags: tags}, nil
}
// Compile all the carbon ingestion rules into regexp so that we can
// perform matching. Also, generate all the mapping rules and storage
// policies that we will need to pass to the DownsamplerAndWriter upfront
// so that we don't need to create them each time.
//
// Note that only one rule will be applied per metric and rules are applied
// such that the first one that matches takes precedence. As a result we need
// to make sure to maintain the order of the rules when we generate the compiled ones.
func compileRules(rules CarbonIngesterRules) ([]ruleAndRegex, error) {
compiledRules := []ruleAndRegex{}
for _, rule := range rules.Rules {
compiled, err := regexp.Compile(rule.Pattern)
if err != nil {
return nil, err
}
storagePolicies := []policy.StoragePolicy{}
for _, currPolicy := range rule.Policies {
storagePolicy := policy.NewStoragePolicy(
currPolicy.Resolution, xtime.Second, currPolicy.Retention)
storagePolicies = append(storagePolicies, storagePolicy)
}
compiledRule := ruleAndRegex{
rule: rule,
regexp: compiled,
}
if rule.Aggregation.EnabledOrDefault() {
compiledRule.mappingRules = []downsample.AutoMappingRule{
downsample.AutoMappingRule{
Aggregations: []aggregation.Type{rule.Aggregation.TypeOrDefault()},
Policies: storagePolicies,
},
}
} else {
compiledRule.storagePolicies = storagePolicies
}
compiledRules = append(compiledRules, compiledRule)
}
return compiledRules, nil
}
func (i *ingester) getLineResources() *lineResources {
return i.lineResourcesPool.Get().(*lineResources)
}
func (i *ingester) putLineResources(l *lineResources) {
tooLargeForPool := cap(l.name) > maxResourcePoolNameSize ||
len(l.datapoints) > 1 || // We always write one datapoint at a time.
cap(l.datapoints) > 1 ||
cap(l.tags) > maxPooledTagsSize
if tooLargeForPool {
return
}
// Reset.
l.name = l.name[:0]
l.datapoints[0] = ts.Datapoint{}
for i := range l.tags {
// Free pointers.
l.tags[i] = models.Tag{}
}
l.tags = l.tags[:0]
i.lineResourcesPool.Put(l)
}
type lineResources struct {
name []byte
datapoints []ts.Datapoint
tags []models.Tag
}
type ruleAndRegex struct {
rule config.CarbonIngesterRuleConfiguration
regexp *regexp.Regexp
mappingRules []downsample.AutoMappingRule
storagePolicies []policy.StoragePolicy
}