forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
96 lines (87 loc) · 2.03 KB
/
sample.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
package kapacitor
import (
"errors"
"log"
"sync"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type SampleNode struct {
node
s *pipeline.SampleNode
cardinalityMu sync.RWMutex
counts map[models.GroupID]int64
duration time.Duration
}
// Create a new SampleNode which filters data from a source.
func newSampleNode(et *ExecutingTask, n *pipeline.SampleNode, l *log.Logger) (*SampleNode, error) {
sn := &SampleNode{
node: node{Node: n, et: et, logger: l},
s: n,
counts: make(map[models.GroupID]int64),
duration: n.Duration,
}
sn.node.runF = sn.runSample
if n.Duration == 0 && n.N == 0 {
return nil, errors.New("invalid sample rate: must be positive integer or duration")
}
return sn, nil
}
func (s *SampleNode) runSample([]byte) error {
s.nodeCardinality.ValueF = func() int64 {
s.cardinalityMu.RLock()
l := len(s.counts)
s.cardinalityMu.RUnlock()
return int64(l)
}
switch s.Wants() {
case pipeline.StreamEdge:
for p, ok := s.ins[0].NextPoint(); ok; p, ok = s.ins[0].NextPoint() {
s.timer.Start()
if s.shouldKeep(p.Group, p.Time) {
s.timer.Pause()
for _, child := range s.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
s.timer.Resume()
}
s.timer.Stop()
}
case pipeline.BatchEdge:
for b, ok := s.ins[0].NextBatch(); ok; b, ok = s.ins[0].NextBatch() {
s.timer.Start()
if s.shouldKeep(b.Group, b.TMax) {
s.timer.Pause()
for _, child := range s.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
s.timer.Resume()
}
s.timer.Stop()
}
}
return nil
}
func (s *SampleNode) shouldKeep(group models.GroupID, t time.Time) bool {
if s.duration != 0 {
keepTime := t.Truncate(s.duration)
return t.Equal(keepTime)
} else {
s.cardinalityMu.RLock()
count := s.counts[group]
s.cardinalityMu.RUnlock()
keep := count%s.s.N == 0
count++
s.cardinalityMu.Lock()
s.counts[group] = count
s.cardinalityMu.Unlock()
return keep
}
}