forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kapacitor_loopback.go
126 lines (108 loc) · 2.65 KB
/
kapacitor_loopback.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
package kapacitor
import (
"fmt"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type KapacitorLoopbackNode struct {
node
k *pipeline.KapacitorLoopbackNode
pointsWritten *expvar.Int
begin edge.BeginBatchMessage
}
func newKapacitorLoopbackNode(et *ExecutingTask, n *pipeline.KapacitorLoopbackNode, d NodeDiagnostic) (*KapacitorLoopbackNode, error) {
kn := &KapacitorLoopbackNode{
node: node{Node: n, et: et, diag: d},
k: n,
}
kn.node.runF = kn.runOut
// Check that a loop has not been created within this task
for _, dbrp := range et.Task.DBRPs {
if dbrp.Database == n.Database && dbrp.RetentionPolicy == n.RetentionPolicy {
return nil, fmt.Errorf("loop detected on dbrp: %v", dbrp)
}
}
return kn, nil
}
func (n *KapacitorLoopbackNode) runOut([]byte) error {
n.pointsWritten = &expvar.Int{}
n.statMap.Set(statsInfluxDBPointsWritten, n.pointsWritten)
consumer := edge.NewConsumerWithReceiver(
n.ins[0],
n,
)
return consumer.Consume()
}
func (n *KapacitorLoopbackNode) Point(p edge.PointMessage) error {
n.timer.Start()
defer n.timer.Stop()
p = p.ShallowCopy()
if n.k.Database != "" {
p.SetDatabase(n.k.Database)
}
if n.k.RetentionPolicy != "" {
p.SetRetentionPolicy(n.k.RetentionPolicy)
}
if n.k.Measurement != "" {
p.SetName(n.k.Measurement)
}
if len(n.k.Tags) > 0 {
tags := p.Tags().Copy()
for k, v := range n.k.Tags {
tags[k] = v
}
p.SetTags(tags)
}
n.timer.Pause()
err := n.et.tm.WriteKapacitorPoint(p)
n.timer.Resume()
if err != nil {
n.diag.Error("failed to write point over loopback", err)
} else {
n.pointsWritten.Add(1)
}
return nil
}
func (n *KapacitorLoopbackNode) BeginBatch(begin edge.BeginBatchMessage) error {
n.begin = begin
return nil
}
func (n *KapacitorLoopbackNode) BatchPoint(bp edge.BatchPointMessage) error {
tags := bp.Tags()
if len(n.k.Tags) > 0 {
tags = bp.Tags().Copy()
for k, v := range n.k.Tags {
tags[k] = v
}
}
p := edge.NewPointMessage(
n.begin.Name(),
n.k.Database,
n.k.RetentionPolicy,
models.Dimensions{},
bp.Fields(),
tags,
bp.Time(),
)
n.timer.Pause()
err := n.et.tm.WriteKapacitorPoint(p)
n.timer.Resume()
if err != nil {
n.diag.Error("failed to write point over loopback", err)
} else {
n.pointsWritten.Add(1)
}
return nil
}
func (n *KapacitorLoopbackNode) EndBatch(edge.EndBatchMessage) error {
return nil
}
func (n *KapacitorLoopbackNode) Barrier(edge.BarrierMessage) error {
return nil
}
func (n *KapacitorLoopbackNode) DeleteGroup(edge.DeleteGroupMessage) error {
return nil
}
func (n *KapacitorLoopbackNode) Done() {}