forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.go
126 lines (110 loc) · 2.47 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
package kapacitor
import (
"errors"
"fmt"
"log"
"os"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
"github.com/influxdb/kapacitor/wlog"
)
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
collected int64
emitted int64
logger *log.Logger
closed bool
}
func newEdge(name string, t pipeline.EdgeType) *Edge {
l := wlog.New(os.Stderr, fmt.Sprintf("[edge:%s] ", name), log.LstdFlags)
switch t {
case pipeline.StreamEdge:
return &Edge{stream: make(chan models.Point), logger: l}
case pipeline.BatchEdge:
return &Edge{batch: make(chan models.Batch), logger: l}
case pipeline.ReduceEdge:
return &Edge{reduce: make(chan *MapResult), logger: l}
}
return nil
}
func (e *Edge) Close() {
if e.closed {
return
}
e.closed = true
e.logger.Printf("I! closing c: %d e: %d\n", e.collected, e.emitted)
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) {
e.logger.Printf("D! next point c: %d e: %d\n", e.collected, e.emitted)
p, ok = <-e.stream
if ok {
e.emitted++
}
return
}
func (e *Edge) NextBatch() (b models.Batch, ok bool) {
e.logger.Printf("D! next batch c: %d e: %d\n", e.collected, e.emitted)
b, ok = <-e.batch
if ok {
e.emitted++
}
return
}
func (e *Edge) NextMaps() (m *MapResult, ok bool) {
e.logger.Printf("D! next maps c: %d e: %d\n", e.collected, e.emitted)
m, ok = <-e.reduce
if m != nil {
e.emitted++
}
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)
e.logger.Printf("D! collect point c: %d e: %d\n", e.collected, e.emitted)
e.collected++
e.stream <- p
return
}
func (e *Edge) CollectBatch(b models.Batch) (err error) {
defer e.recover(&err)
e.logger.Printf("D! collect batch c: %d e: %d\n", e.collected, e.emitted)
e.collected++
e.batch <- b
return
}
func (e *Edge) CollectMaps(m *MapResult) (err error) {
defer e.recover(&err)
e.logger.Printf("D! collect maps c: %d e: %d\n", e.collected, e.emitted)
e.collected++
e.reduce <- m
return
}