-
Notifications
You must be signed in to change notification settings - Fork 451
/
entry.go
1097 lines (990 loc) · 33.1 KB
/
entry.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2016 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 aggregator
import (
"container/list"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/m3db/m3/src/aggregator/bitset"
"github.com/m3db/m3/src/aggregator/rate"
"github.com/m3db/m3/src/aggregator/runtime"
"github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/metrics/metric"
"github.com/m3db/m3/src/metrics/metric/aggregated"
"github.com/m3db/m3/src/metrics/metric/id"
metricid "github.com/m3db/m3/src/metrics/metric/id"
"github.com/m3db/m3/src/metrics/metric/unaggregated"
"github.com/m3db/m3/src/metrics/policy"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/uber-go/tally"
)
const (
// initialAggregationCapacity is the initial number of slots
// allocated for aggregation metadata.
initialAggregationCapacity = 2
)
var (
errEntryClosed = errors.New("entry is closed")
errWriteValueRateLimitExceeded = errors.New("write value rate limit is exceeded")
errEmptyMetadatas = errors.New("empty metadata list")
errNoApplicableMetadata = errors.New("no applicable metadata")
errNoPipelinesInMetadata = errors.New("no pipelines in metadata")
errTooFarInTheFuture = xerrors.NewInvalidParamsError(errors.New("too far in the future"))
errTooFarInThePast = xerrors.NewInvalidParamsError(errors.New("too far in the past"))
errArrivedTooLate = xerrors.NewInvalidParamsError(errors.New("arrived too late"))
errTimestampFormat = time.RFC822Z
)
type rateLimitEntryMetrics struct {
valueRateLimitExceeded tally.Counter
droppedValues tally.Counter
}
func newRateLimitEntryMetrics(scope tally.Scope) rateLimitEntryMetrics {
return rateLimitEntryMetrics{
valueRateLimitExceeded: scope.Counter("value-rate-limit-exceeded"),
droppedValues: scope.Counter("dropped-values"),
}
}
type untimedEntryMetrics struct {
rateLimit rateLimitEntryMetrics
emptyMetadatas tally.Counter
noApplicableMetadata tally.Counter
noPipelinesInMetadata tally.Counter
emptyPipeline tally.Counter
noAggregationInPipeline tally.Counter
staleMetadata tally.Counter
tombstonedMetadata tally.Counter
metadatasUpdates tally.Counter
}
func newUntimedEntryMetrics(scope tally.Scope) untimedEntryMetrics {
return untimedEntryMetrics{
rateLimit: newRateLimitEntryMetrics(scope),
emptyMetadatas: scope.Counter("empty-metadatas"),
noApplicableMetadata: scope.Counter("no-applicable-metadata"),
noPipelinesInMetadata: scope.Counter("no-pipelines-in-metadata"),
emptyPipeline: scope.Counter("empty-pipeline"),
noAggregationInPipeline: scope.Counter("no-aggregation-in-pipeline"),
staleMetadata: scope.Counter("stale-metadata"),
tombstonedMetadata: scope.Counter("tombstoned-metadata"),
metadatasUpdates: scope.Counter("metadatas-updates"),
}
}
type timedEntryMetrics struct {
rateLimit rateLimitEntryMetrics
tooFarInTheFuture tally.Counter
tooFarInThePast tally.Counter
noPipelinesInMetadata tally.Counter
tombstonedMetadata tally.Counter
metadataUpdates tally.Counter
metadatasUpdates tally.Counter
}
func newTimedEntryMetrics(scope tally.Scope) timedEntryMetrics {
return timedEntryMetrics{
rateLimit: newRateLimitEntryMetrics(scope),
tooFarInTheFuture: scope.Counter("too-far-in-the-future"),
tooFarInThePast: scope.Counter("too-far-in-the-past"),
noPipelinesInMetadata: scope.Counter("no-pipelines-in-metadata"),
tombstonedMetadata: scope.Counter("tombstoned-metadata"),
metadataUpdates: scope.Counter("metadata-updates"),
metadatasUpdates: scope.Counter("metadatas-updates"),
}
}
type forwardedEntryMetrics struct {
rateLimit rateLimitEntryMetrics
arrivedTooLate tally.Counter
duplicateSources tally.Counter
metadataUpdates tally.Counter
}
func newForwardedEntryMetrics(scope tally.Scope) forwardedEntryMetrics {
return forwardedEntryMetrics{
rateLimit: newRateLimitEntryMetrics(scope),
arrivedTooLate: scope.Counter("arrived-too-late"),
duplicateSources: scope.Counter("duplicate-sources"),
metadataUpdates: scope.Counter("metadata-updates"),
}
}
type entryMetrics struct {
untimed untimedEntryMetrics
timed timedEntryMetrics
forwarded forwardedEntryMetrics
}
func newEntryMetrics(scope tally.Scope) entryMetrics {
untimedEntryScope := scope.Tagged(map[string]string{"entry-type": "untimed"})
timedEntryScope := scope.Tagged(map[string]string{"entry-type": "timed"})
forwardedEntryScope := scope.Tagged(map[string]string{"entry-type": "forwarded"})
return entryMetrics{
untimed: newUntimedEntryMetrics(untimedEntryScope),
timed: newTimedEntryMetrics(timedEntryScope),
forwarded: newForwardedEntryMetrics(forwardedEntryScope),
}
}
// Entry keeps track of a metric's aggregations alongside the aggregation
// metadatas including storage policies, aggregation types, and remaining pipeline
// steps if any.
//
// TODO(xichen): make the access time per aggregation key for entries associated
// with forwarded metrics so we can reclaim aggregation elements associated with
// individual aggregation keys even though the entry is still active.
// nolint: maligned
type Entry struct {
sync.RWMutex
closed bool
opts Options
rateLimiter *rate.Limiter
hasDefaultMetadatas bool
cutoverNanos int64
lists *metricLists
numWriters int32
lastAccessNanos int64
aggregations aggregationValues
metrics entryMetrics
// The entry keeps a decompressor to reuse the bitset in it, so we can
// save some heap allocations.
decompressor aggregation.IDDecompressor
}
// NewEntry creates a new entry.
func NewEntry(lists *metricLists, runtimeOpts runtime.Options, opts Options) *Entry {
scope := opts.InstrumentOptions().MetricsScope().SubScope("entry")
e := &Entry{
aggregations: make(aggregationValues, 0, initialAggregationCapacity),
metrics: newEntryMetrics(scope),
decompressor: aggregation.NewPooledIDDecompressor(opts.AggregationTypesOptions().TypesPool()),
}
e.ResetSetData(lists, runtimeOpts, opts)
return e
}
// IncWriter increases the writer count.
func (e *Entry) IncWriter() { atomic.AddInt32(&e.numWriters, 1) }
// DecWriter decreases the writer count.
func (e *Entry) DecWriter() { atomic.AddInt32(&e.numWriters, -1) }
// ResetSetData resets the entry and sets initial data.
// NB(xichen): we need to reset the options here to use the correct
// time lock contained in the options.
func (e *Entry) ResetSetData(lists *metricLists, runtimeOpts runtime.Options, opts Options) {
e.Lock()
e.closed = false
e.opts = opts
e.resetRateLimiterWithLock(runtimeOpts)
e.hasDefaultMetadatas = false
e.cutoverNanos = uninitializedCutoverNanos
e.lists = lists
e.numWriters = 0
e.recordLastAccessed(e.opts.ClockOptions().NowFn()())
e.Unlock()
}
// SetRuntimeOptions updates the parameters of the rate limiter.
func (e *Entry) SetRuntimeOptions(opts runtime.Options) {
e.Lock()
if e.closed {
e.Unlock()
return
}
e.resetRateLimiterWithLock(opts)
e.Unlock()
}
// AddUntimed adds an untimed metric along with its metadatas.
func (e *Entry) AddUntimed(
metricUnion unaggregated.MetricUnion,
metadatas metadata.StagedMetadatas,
) error {
switch metricUnion.Type {
case metric.TimerType:
var err error
if err = e.applyValueRateLimit(
int64(len(metricUnion.BatchTimerVal)),
e.metrics.untimed.rateLimit,
); err == nil {
err = e.writeBatchTimerWithMetadatas(metricUnion, metadatas)
}
if metricUnion.BatchTimerVal != nil && metricUnion.TimerValPool != nil {
metricUnion.TimerValPool.Put(metricUnion.BatchTimerVal)
}
return err
default:
// For counters and gauges, there is a single value in the metric union.
if err := e.applyValueRateLimit(1, e.metrics.untimed.rateLimit); err != nil {
return err
}
return e.addUntimed(metricUnion, metadatas)
}
}
// AddTimed adds a timed metric alongside its metadata.
func (e *Entry) AddTimed(
metric aggregated.Metric,
metadata metadata.TimedMetadata,
) error {
if err := e.applyValueRateLimit(1, e.metrics.timed.rateLimit); err != nil {
return err
}
return e.addTimed(metric, metadata, nil)
}
// AddTimedWithStagedMetadatas adds a timed metric with staged metadatas.
func (e *Entry) AddTimedWithStagedMetadatas(
metric aggregated.Metric,
metas metadata.StagedMetadatas,
) error {
if err := e.applyValueRateLimit(1, e.metrics.timed.rateLimit); err != nil {
return err
}
return e.addTimed(metric, metadata.TimedMetadata{}, metas)
}
// AddForwarded adds a forwarded metric alongside its metadata.
func (e *Entry) AddForwarded(
metric aggregated.ForwardedMetric,
metadata metadata.ForwardMetadata,
) error {
if err := e.applyValueRateLimit(1, e.metrics.forwarded.rateLimit); err != nil {
return err
}
return e.addForwarded(metric, metadata)
}
// ShouldExpire returns whether the entry should expire.
func (e *Entry) ShouldExpire(now time.Time) bool {
e.RLock()
if e.closed {
e.RUnlock()
return false
}
e.RUnlock()
return e.shouldExpire(now)
}
// TryExpire attempts to expire the entry, returning true
// if the entry is expired, and false otherwise.
func (e *Entry) TryExpire(now time.Time) bool {
e.Lock()
if e.closed {
e.Unlock()
return false
}
if !e.shouldExpire(now) {
e.Unlock()
return false
}
e.closed = true
// Empty out the aggregation elements so they don't hold references
// to other objects after being put back to pool to reduce GC overhead.
for i := range e.aggregations {
e.aggregations[i].elem.Value.(metricElem).MarkAsTombstoned()
e.aggregations[i] = aggregationValue{}
}
e.aggregations = e.aggregations[:0]
e.lists = nil
pool := e.opts.EntryPool()
e.Unlock()
pool.Put(e)
return true
}
func (e *Entry) writeBatchTimerWithMetadatas(
metric unaggregated.MetricUnion,
metadatas metadata.StagedMetadatas,
) error {
// If there is no limit on the maximum batch size per write, write
// all timers at once.
maxTimerBatchSizePerWrite := e.opts.MaxTimerBatchSizePerWrite()
if maxTimerBatchSizePerWrite == 0 {
return e.addUntimed(metric, metadatas)
}
// Otherwise, honor maximum timer batch size.
var (
timerValues = metric.BatchTimerVal
numTimerValues = len(timerValues)
start, end int
)
for start = 0; start < numTimerValues; start = end {
end = start + maxTimerBatchSizePerWrite
if end > numTimerValues {
end = numTimerValues
}
splitTimer := metric
splitTimer.BatchTimerVal = timerValues[start:end]
if err := e.addUntimed(splitTimer, metadatas); err != nil {
return err
}
}
return nil
}
func (e *Entry) addUntimed(
metric unaggregated.MetricUnion,
metadatas metadata.StagedMetadatas,
) error {
timeLock := e.opts.TimeLock()
timeLock.RLock()
// NB(xichen): it is important that we determine the current time
// within the time lock. This ensures time ordering by wrapping
// actions that need to happen before a given time within a read lock,
// so it is guaranteed that actions before when a write lock is acquired
// must have all completed. This is used to ensure we never write metrics
// for times that have already been flushed.
currTime := e.opts.ClockOptions().NowFn()()
e.recordLastAccessed(currTime)
e.RLock()
if e.closed {
e.RUnlock()
timeLock.RUnlock()
return errEntryClosed
}
// Fast exit path for the common case where the metric has default metadatas for aggregation.
hasDefaultMetadatas := metadatas.IsDefault()
if e.hasDefaultMetadatas && hasDefaultMetadatas {
err := e.addUntimedWithLock(currTime, metric)
e.RUnlock()
timeLock.RUnlock()
return err
}
sm, err := e.activeStagedMetadataWithLock(currTime, metadatas)
if err != nil {
e.RUnlock()
timeLock.RUnlock()
return err
}
// If the metadata indicates the (rollup) metric has been tombstoned, the metric is
// not ingested for aggregation. However, we do not update the policies asssociated
// with this entry and mark it tombstoned because there may be a different raw metric
// generating this same (rollup) metric that is actively emitting, meaning this entry
// may still be very much alive.
if sm.Tombstoned {
e.RUnlock()
timeLock.RUnlock()
e.metrics.untimed.tombstonedMetadata.Inc(1)
return nil
}
// It is expected that there is at least one pipeline in the metadata.
if len(sm.Pipelines) == 0 {
e.RUnlock()
timeLock.RUnlock()
e.metrics.untimed.noPipelinesInMetadata.Inc(1)
return errNoPipelinesInMetadata
}
if !e.shouldUpdateStagedMetadatasWithLock(sm) {
err = e.addUntimedWithLock(currTime, metric)
e.RUnlock()
timeLock.RUnlock()
return err
}
e.RUnlock()
e.Lock()
if e.closed {
e.Unlock()
timeLock.RUnlock()
return errEntryClosed
}
if e.shouldUpdateStagedMetadatasWithLock(sm) {
err := e.updateStagedMetadatasWithLock(metric.ID, metric.Type,
hasDefaultMetadatas, sm)
if err != nil {
// NB(xichen): if an error occurred during policy update, the policies
// will remain as they are, i.e., there are no half-updated policies.
e.Unlock()
timeLock.RUnlock()
return err
}
e.metrics.untimed.metadatasUpdates.Inc(1)
}
err = e.addUntimedWithLock(currTime, metric)
e.Unlock()
timeLock.RUnlock()
return err
}
// NB(xichen): we assume the metadatas are sorted by their cutover times
// in ascending order.
func (e *Entry) activeStagedMetadataWithLock(
t time.Time,
metadatas metadata.StagedMetadatas,
) (metadata.StagedMetadata, error) {
// If we have no metadata to apply, simply bail.
if len(metadatas) == 0 {
e.metrics.untimed.emptyMetadatas.Inc(1)
return metadata.DefaultStagedMetadata, errEmptyMetadatas
}
timeNanos := t.UnixNano()
for idx := len(metadatas) - 1; idx >= 0; idx-- {
if metadatas[idx].CutoverNanos <= timeNanos {
return metadatas[idx], nil
}
}
e.metrics.untimed.noApplicableMetadata.Inc(1)
return metadata.DefaultStagedMetadata, errNoApplicableMetadata
}
// NB: The metadata passed in is guaranteed to have cut over based on the current time.
func (e *Entry) shouldUpdateStagedMetadatasWithLock(sm metadata.StagedMetadata) bool {
// If this is a stale metadata, we don't update the existing metadata.
if e.cutoverNanos > sm.CutoverNanos {
e.metrics.untimed.staleMetadata.Inc(1)
return false
}
// If this is a newer metadata, we always update.
if e.cutoverNanos < sm.CutoverNanos {
return true
}
// Iterate over the list of pipelines and check whether we have metadata changes.
// NB: If the incoming metadata have the same set of aggregation keys as the cached
// set but also have duplicates, there is no need to update metadatas as long as
// the cached set has all aggregation keys in the incoming metadata and vice versa.
bs := bitset.New(uint(len(e.aggregations)))
for _, pipeline := range sm.Pipelines {
storagePolicies := e.storagePolicies(pipeline.StoragePolicies)
for _, storagePolicy := range storagePolicies {
key := aggregationKey{
aggregationID: pipeline.AggregationID,
storagePolicy: storagePolicy,
pipeline: pipeline.Pipeline,
idPrefixSuffixType: WithPrefixWithSuffix,
}
idx := e.aggregations.index(key)
if idx < 0 {
return true
}
bs.Set(uint(idx))
}
}
return !bs.All(uint(len(e.aggregations)))
}
func (e *Entry) storagePolicies(policies policy.StoragePolicies) policy.StoragePolicies {
if !policies.IsDefault() {
return policies
}
return e.opts.DefaultStoragePolicies()
}
func (e *Entry) maybeCopyIDWithLock(id metricid.RawID) metricid.RawID {
// If there are existing elements for this id, try reusing
// the id from the elements because those are owned by us.
if len(e.aggregations) > 0 {
return e.aggregations[0].elem.Value.(metricElem).ID()
}
// Otherwise it is necessary to make a copy because it's not owned by us.
elemID := make(metricid.RawID, len(id))
copy(elemID, id)
return elemID
}
// addAggregationKey adds a new aggregation key to the list of new aggregations.
func (e *Entry) addNewAggregationKeyWithLock(
metricType metric.Type,
metricID metricid.RawID,
key aggregationKey,
listID metricListID,
newAggregations aggregationValues,
) (aggregationValues, error) {
// Remove duplicate aggregation pipelines.
if newAggregations.contains(key) {
return newAggregations, nil
}
if idx := e.aggregations.index(key); idx >= 0 {
newAggregations = append(newAggregations, e.aggregations[idx])
return newAggregations, nil
}
aggTypes, err := e.decompressor.Decompress(key.aggregationID)
if err != nil {
return nil, err
}
var newElem metricElem
switch metricType {
case metric.CounterType:
newElem = e.opts.CounterElemPool().Get()
case metric.TimerType:
newElem = e.opts.TimerElemPool().Get()
case metric.GaugeType:
newElem = e.opts.GaugeElemPool().Get()
default:
return nil, errInvalidMetricType
}
// NB: The pipeline may not be owned by us and as such we need to make a copy here.
key.pipeline = key.pipeline.Clone()
if err = newElem.ResetSetData(metricID, key.storagePolicy, aggTypes, key.pipeline, key.numForwardedTimes, key.idPrefixSuffixType); err != nil {
return nil, err
}
list, err := e.lists.FindOrCreate(listID)
if err != nil {
return nil, err
}
newListElem, err := list.PushBack(newElem)
if err != nil {
return nil, err
}
newAggregations = append(newAggregations, aggregationValue{key: key, elem: newListElem})
return newAggregations, nil
}
func (e *Entry) removeOldAggregations(newAggregations aggregationValues) {
for _, val := range e.aggregations {
if !newAggregations.contains(val.key) {
val.elem.Value.(metricElem).MarkAsTombstoned()
}
}
}
func (e *Entry) updateStagedMetadatasWithLock(
metricID id.RawID,
metricType metric.Type,
hasDefaultMetadatas bool,
sm metadata.StagedMetadata,
) error {
var (
elemID = e.maybeCopyIDWithLock(metricID)
newAggregations = make(aggregationValues, 0, initialAggregationCapacity)
)
// Update the metadatas.
for _, pipeline := range sm.Pipelines {
storagePolicies := e.storagePolicies(pipeline.StoragePolicies)
for _, storagePolicy := range storagePolicies {
key := aggregationKey{
aggregationID: pipeline.AggregationID,
storagePolicy: storagePolicy,
pipeline: pipeline.Pipeline,
idPrefixSuffixType: WithPrefixWithSuffix,
}
listID := standardMetricListID{
resolution: storagePolicy.Resolution().Window,
}.toMetricListID()
var err error
newAggregations, err = e.addNewAggregationKeyWithLock(metricType, elemID, key, listID, newAggregations)
if err != nil {
return err
}
}
}
// Mark the outdated elements as tombstoned.
e.removeOldAggregations(newAggregations)
// Replace the existing aggregations with new aggregations.
e.aggregations = newAggregations
e.hasDefaultMetadatas = hasDefaultMetadatas
e.cutoverNanos = sm.CutoverNanos
return nil
}
func (e *Entry) addUntimedWithLock(timestamp time.Time, mu unaggregated.MetricUnion) error {
multiErr := xerrors.NewMultiError()
for _, val := range e.aggregations {
if err := val.elem.Value.(metricElem).AddUnion(timestamp, mu); err != nil {
multiErr = multiErr.Add(err)
}
}
return multiErr.FinalError()
}
func (e *Entry) addTimed(
metric aggregated.Metric,
metadata metadata.TimedMetadata,
stagedMetadatas metadata.StagedMetadatas,
) error {
timeLock := e.opts.TimeLock()
timeLock.RLock()
// NB(xichen): it is important that we determine the current time
// within the time lock. This ensures time ordering by wrapping
// actions that need to happen before a given time within a read lock,
// so it is guaranteed that actions before when a write lock is acquired
// must have all completed. This is used to ensure we never write metrics
// for times that have already been flushed.
currTime := e.opts.ClockOptions().NowFn()()
e.recordLastAccessed(currTime)
e.RLock()
if e.closed {
e.RUnlock()
timeLock.RUnlock()
return errEntryClosed
}
// Reject datapoints that arrive too late or too early.
if err := e.checkTimestampForTimedMetric(
metric,
currTime.UnixNano(),
metadata.StoragePolicy.Resolution().Window,
); err != nil {
e.RUnlock()
timeLock.RUnlock()
return err
}
// Only apply processing of staged metadatas if has sent staged metadatas
// that isn't the default staged metadatas.
hasDefaultMetadatas := stagedMetadatas.IsDefault()
if len(stagedMetadatas) > 0 && !hasDefaultMetadatas {
sm, err := e.activeStagedMetadataWithLock(currTime, stagedMetadatas)
if err != nil {
e.RUnlock()
timeLock.RUnlock()
return err
}
// If the metadata indicates the (rollup) metric has been tombstoned, the metric is
// not ingested for aggregation. However, we do not update the policies asssociated
// with this entry and mark it tombstoned because there may be a different raw metric
// generating this same (rollup) metric that is actively emitting, meaning this entry
// may still be very much alive.
if sm.Tombstoned {
e.RUnlock()
timeLock.RUnlock()
e.metrics.timed.tombstonedMetadata.Inc(1)
return nil
}
// It is expected that there is at least one pipeline in the metadata.
if len(sm.Pipelines) == 0 {
e.RUnlock()
timeLock.RUnlock()
e.metrics.timed.noPipelinesInMetadata.Inc(1)
return errNoPipelinesInMetadata
}
if !e.shouldUpdateStagedMetadatasWithLock(sm) {
err = e.addTimedWithStagedMetadatasAndLock(metric)
e.RUnlock()
timeLock.RUnlock()
return err
}
e.RUnlock()
e.Lock()
if e.closed {
e.Unlock()
timeLock.RUnlock()
return errEntryClosed
}
if e.shouldUpdateStagedMetadatasWithLock(sm) {
err := e.updateStagedMetadatasWithLock(metric.ID, metric.Type,
hasDefaultMetadatas, sm)
if err != nil {
// NB(xichen): if an error occurred during policy update, the policies
// will remain as they are, i.e., there are no half-updated policies.
e.Unlock()
timeLock.RUnlock()
return err
}
e.metrics.timed.metadatasUpdates.Inc(1)
}
err = e.addTimedWithStagedMetadatasAndLock(metric)
e.Unlock()
timeLock.RUnlock()
return err
}
// Check if we should update metadata, and add metric if not.
key := aggregationKey{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,
idPrefixSuffixType: NoPrefixNoSuffix,
}
if idx := e.aggregations.index(key); idx >= 0 {
err := e.addTimedWithLock(e.aggregations[idx], metric)
e.RUnlock()
timeLock.RUnlock()
return err
}
e.RUnlock()
e.Lock()
if e.closed {
e.Unlock()
timeLock.RUnlock()
return errEntryClosed
}
if idx := e.aggregations.index(key); idx >= 0 {
err := e.addTimedWithLock(e.aggregations[idx], metric)
e.Unlock()
timeLock.RUnlock()
return err
}
// Update metatadata if not exists, and add metric.
if err := e.updateTimedMetadataWithLock(metric, metadata); err != nil {
e.Unlock()
timeLock.RUnlock()
return err
}
idx := e.aggregations.index(key)
err := e.addTimedWithLock(e.aggregations[idx], metric)
e.Unlock()
timeLock.RUnlock()
return err
}
func (e *Entry) checkTimestampForTimedMetric(
metric aggregated.Metric,
currNanos int64,
resolution time.Duration,
) error {
metricTimeNanos := metric.TimeNanos
timedBufferFuture := e.opts.BufferForFutureTimedMetric()
if metricTimeNanos-currNanos > timedBufferFuture.Nanoseconds() {
e.metrics.timed.tooFarInTheFuture.Inc(1)
if !e.opts.VerboseErrors() {
// Don't return verbose errors if not enabled.
return errTooFarInTheFuture
}
timestamp := time.Unix(0, metricTimeNanos)
futureLimit := time.Unix(0, currNanos+timedBufferFuture.Nanoseconds())
err := fmt.Errorf("datapoint for aggregation too far in future: "+
"id=%s, off_by=%s, timestamp=%s, future_limit=%s, "+
"timestamp_unix_nanos=%d, future_limit_unix_nanos=%d",
metric.ID, timestamp.Sub(futureLimit).String(),
timestamp.Format(errTimestampFormat),
futureLimit.Format(errTimestampFormat),
timestamp.UnixNano(), futureLimit.UnixNano())
return xerrors.NewRenamedError(errTooFarInTheFuture, err)
}
bufferPastFn := e.opts.BufferForPastTimedMetricFn()
timedBufferPast := bufferPastFn(resolution)
if currNanos-metricTimeNanos > timedBufferPast.Nanoseconds() {
e.metrics.timed.tooFarInThePast.Inc(1)
if !e.opts.VerboseErrors() {
// Don't return verbose errors if not enabled.
return errTooFarInThePast
}
timestamp := time.Unix(0, metricTimeNanos)
pastLimit := time.Unix(0, currNanos-timedBufferPast.Nanoseconds())
err := fmt.Errorf("datapoint for aggregation too far in past: "+
"id=%s, off_by=%s, timestamp=%s, past_limit=%s, "+
"timestamp_unix_nanos=%d, past_limit_unix_nanos=%d",
metric.ID, pastLimit.Sub(timestamp).String(),
timestamp.Format(errTimestampFormat),
pastLimit.Format(errTimestampFormat),
timestamp.UnixNano(), pastLimit.UnixNano())
return xerrors.NewRenamedError(errTooFarInThePast, err)
}
return nil
}
func (e *Entry) updateTimedMetadataWithLock(
metric aggregated.Metric,
metadata metadata.TimedMetadata,
) error {
var (
elemID = e.maybeCopyIDWithLock(metric.ID)
err error
)
// Update the timed metadata.
key := aggregationKey{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,
idPrefixSuffixType: NoPrefixNoSuffix,
}
listID := timedMetricListID{
resolution: metadata.StoragePolicy.Resolution().Window,
}.toMetricListID()
newAggregations, err := e.addNewAggregationKeyWithLock(metric.Type, elemID, key, listID, e.aggregations)
if err != nil {
return err
}
e.aggregations = newAggregations
e.metrics.timed.metadataUpdates.Inc(1)
return nil
}
func (e *Entry) addTimedWithLock(
value aggregationValue,
metric aggregated.Metric,
) error {
timestamp := time.Unix(0, metric.TimeNanos)
return value.elem.Value.(metricElem).AddValue(timestamp, metric.Value)
}
func (e *Entry) addTimedWithStagedMetadatasAndLock(
metric aggregated.Metric,
) error {
timestamp := time.Unix(0, metric.TimeNanos)
multiErr := xerrors.NewMultiError()
for _, val := range e.aggregations {
if err := val.elem.Value.(metricElem).AddValue(timestamp, metric.Value); err != nil {
multiErr = multiErr.Add(err)
}
}
return multiErr.FinalError()
}
func (e *Entry) addForwarded(
metric aggregated.ForwardedMetric,
metadata metadata.ForwardMetadata,
) error {
timeLock := e.opts.TimeLock()
timeLock.RLock()
// NB(xichen): it is important that we determine the current time
// within the time lock. This ensures time ordering by wrapping
// actions that need to happen before a given time within a read lock,
// so it is guaranteed that actions before when a write lock is acquired
// must have all completed. This is used to ensure we never write metrics
// for times that have already been flushed.
currTime := e.opts.ClockOptions().NowFn()()
e.recordLastAccessed(currTime)
e.RLock()
if e.closed {
e.RUnlock()
timeLock.RUnlock()
return errEntryClosed
}
// Reject datapoints that arrive too late.
if err := e.checkLatenessForForwardedMetric(
metric,
currTime.UnixNano(),
metadata.StoragePolicy.Resolution().Window,
metadata.NumForwardedTimes,
); err != nil {
e.RUnlock()
timeLock.RUnlock()
return err
}
// Check if we should update metadata, and add metric if not.
key := aggregationKey{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,
pipeline: metadata.Pipeline,
numForwardedTimes: metadata.NumForwardedTimes,
idPrefixSuffixType: WithPrefixWithSuffix,
}
if idx := e.aggregations.index(key); idx >= 0 {
err := e.addForwardedWithLock(e.aggregations[idx], metric, metadata.SourceID)
e.RUnlock()
timeLock.RUnlock()
return err
}
e.RUnlock()
e.Lock()
if e.closed {
e.Unlock()
timeLock.RUnlock()
return errEntryClosed
}
if idx := e.aggregations.index(key); idx >= 0 {
err := e.addForwardedWithLock(e.aggregations[idx], metric, metadata.SourceID)
e.Unlock()
timeLock.RUnlock()
return err
}
// Update metatadata if not exists, and add metric.
if err := e.updateForwardMetadataWithLock(metric, metadata); err != nil {
e.Unlock()
timeLock.RUnlock()
return err
}
idx := e.aggregations.index(key)
err := e.addForwardedWithLock(e.aggregations[idx], metric, metadata.SourceID)
e.Unlock()
timeLock.RUnlock()
return err
}
func (e *Entry) checkLatenessForForwardedMetric(
metric aggregated.ForwardedMetric,
currNanos int64,
resolution time.Duration,
numForwardedTimes int,
) error {
metricTimeNanos := metric.TimeNanos
maxAllowedForwardingDelayFn := e.opts.MaxAllowedForwardingDelayFn()
maxLatenessAllowed := maxAllowedForwardingDelayFn(resolution, numForwardedTimes)
if currNanos-metricTimeNanos <= maxLatenessAllowed.Nanoseconds() {
return nil
}
e.metrics.forwarded.arrivedTooLate.Inc(1)
if !e.opts.VerboseErrors() {
// Don't return verbose errors if not enabled.
return errArrivedTooLate
}
timestamp := time.Unix(0, metricTimeNanos)
pastLimit := time.Unix(0, currNanos-maxLatenessAllowed.Nanoseconds())
err := fmt.Errorf("datapoint for aggregation forwarded too late: "+
"id=%s, off_by=%s, timestamp=%s, past_limit=%s, "+
"timestamp_unix_nanos=%d, past_limit_unix_nanos=%d",
metric.ID, maxLatenessAllowed.String(),
timestamp.Format(errTimestampFormat),
pastLimit.Format(errTimestampFormat),
timestamp.UnixNano(), pastLimit.UnixNano())
return xerrors.NewRenamedError(errArrivedTooLate, err)
}
func (e *Entry) updateForwardMetadataWithLock(
metric aggregated.ForwardedMetric,
metadata metadata.ForwardMetadata,
) error {
var (
elemID = e.maybeCopyIDWithLock(metric.ID)
err error
)
// Update the forward metadata.
key := aggregationKey{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,