forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.go
181 lines (165 loc) · 3.62 KB
/
edge.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
package kapacitor
import (
"errors"
"expvar"
"fmt"
"log"
"os"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
"github.com/influxdb/kapacitor/wlog"
)
const (
statCollected = "collected"
statEmitted = "emitted"
)
type StreamCollector interface {
CollectPoint(models.Point) error
Close()
}
type BatchCollector interface {
CollectBatch(models.Batch) error
Close()
}
type Edge struct {
stream chan models.Point
batch chan models.Batch
reduce chan *MapResult
logger *log.Logger
closed bool
statMap *expvar.Map
}
func newEdge(name string, t pipeline.EdgeType) *Edge {
sm := &expvar.Map{}
sm.Init()
sm.Add(statCollected, 0)
sm.Add(statEmitted, 0)
e := &Edge{statMap: sm}
e.logger = wlog.New(os.Stderr, fmt.Sprintf("[edge:%s] ", name), log.LstdFlags)
switch t {
case pipeline.StreamEdge:
e.stream = make(chan models.Point)
case pipeline.BatchEdge:
e.batch = make(chan models.Batch)
case pipeline.ReduceEdge:
e.reduce = make(chan *MapResult)
}
return e
}
func (e *Edge) Close() {
if e.closed {
return
}
e.closed = true
e.logger.Printf(
"I! closing c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
if e.stream != nil {
close(e.stream)
}
if e.batch != nil {
close(e.batch)
}
if e.reduce != nil {
close(e.reduce)
}
}
func (e *Edge) NextPoint() (p models.Point, ok bool) {
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! next point c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
p, ok = <-e.stream
if ok {
e.statMap.Add(statEmitted, 1)
}
return
}
func (e *Edge) NextBatch() (b models.Batch, ok bool) {
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! next batch c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
b, ok = <-e.batch
if ok {
e.statMap.Add(statEmitted, 1)
}
return
}
func (e *Edge) NextMaps() (m *MapResult, ok bool) {
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! next maps c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
m, ok = <-e.reduce
if ok {
e.statMap.Add(statEmitted, 1)
}
return
}
func (e *Edge) recover(errp *error) {
if r := recover(); r != nil {
msg := fmt.Sprintf("%s", r)
if msg == "send on closed channel" {
*errp = errors.New(msg)
} else {
panic(r)
}
}
}
func (e *Edge) CollectPoint(p models.Point) (err error) {
defer e.recover(&err)
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! collect point c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
e.statMap.Add(statCollected, 1)
e.stream <- p
return
}
func (e *Edge) CollectBatch(b models.Batch) (err error) {
defer e.recover(&err)
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! collect batch c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
e.statMap.Add(statCollected, 1)
e.batch <- b
return
}
func (e *Edge) CollectMaps(m *MapResult) (err error) {
defer e.recover(&err)
if wlog.LogLevel == wlog.DEBUG {
// Explicitly check log level since this is expensive and frequent
e.logger.Printf(
"D! collect maps c: %s e: %s\n",
e.statMap.Get(statCollected),
e.statMap.Get(statEmitted),
)
}
e.statMap.Add(statCollected, 1)
e.reduce <- m
return
}