forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
201 lines (184 loc) · 4.22 KB
/
window.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
package kapacitor
import (
"fmt"
"log"
"sync"
"time"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
)
type WindowNode struct {
node
w *pipeline.WindowNode
}
// Create a new WindowNode, which windows data for a period of time and emits the window.
func newWindowNode(et *ExecutingTask, n *pipeline.WindowNode) (*WindowNode, error) {
wn := &WindowNode{
w: n,
node: node{Node: n, et: et},
}
wn.node.runF = wn.runWindow
return wn, nil
}
func (w *WindowNode) runWindow() error {
windows := make(map[models.GroupID]*window)
// Loops through points windowing by group
for p, ok := w.ins[0].NextPoint(); ok; p, ok = w.ins[0].NextPoint() {
wnd := windows[p.Group]
if wnd == nil {
tags := p.Tags
if p.Group == models.NilGroup {
tags = nil
}
wnd = &window{
buf: &windowBuffer{logger: w.logger},
nextEmit: p.Time.Add(w.w.Every),
period: w.w.Period,
every: w.w.Every,
name: p.Name,
group: p.Group,
tags: tags,
}
windows[p.Group] = wnd
}
if !p.Time.Before(wnd.nextEmit) {
points := wnd.emit()
// Send window to all children
for _, child := range w.outs {
child.CollectBatch(points)
}
}
wnd.buf.insert(p)
}
return nil
}
type window struct {
buf *windowBuffer
nextEmit time.Time
period time.Duration
every time.Duration
name string
group models.GroupID
tags map[string]string
}
func (w *window) emit() models.Batch {
oldest := w.nextEmit.Add(-1 * w.period)
w.buf.purge(oldest)
w.nextEmit = w.nextEmit.Add(w.every)
batch := w.buf.batch()
batch.Name = w.name
batch.Group = w.group
batch.Tags = w.tags
return batch
}
// implements a purpose built ring buffer for the window of points
type windowBuffer struct {
sync.Mutex
window []models.Point
start int
stop int
size int
logger *log.Logger
}
// Insert a single point into the buffer.
func (b *windowBuffer) insert(p models.Point) {
b.Lock()
defer b.Unlock()
if b.size == cap(b.window) {
//Increase our buffer
c := 2 * (b.size + 1)
w := make([]models.Point, b.size+1, c)
if b.size == 0 {
//do nothing
} else if b.stop > b.start {
n := copy(w, b.window[b.start:b.stop])
if n != b.size {
panic(fmt.Sprintf("did not copy all the data: copied: %d size: %d start: %d stop: %d\n", n, b.size, b.start, b.stop))
}
} else {
n := 0
n += copy(w, b.window[b.start:])
n += copy(w[b.size-b.start:], b.window[:b.stop])
if n != b.size {
panic(fmt.Sprintf("did not copy all the data: copied: %d size: %d start: %d stop: %d\n", n, b.size, b.start, b.stop))
}
}
b.window = w
b.start = 0
b.stop = b.size
}
// Check if we need to wrap around
if len(b.window) == cap(b.window) && b.stop == len(b.window) {
b.stop = 0
}
// Insert point
if b.stop == len(b.window) {
b.window = append(b.window, p)
} else {
b.window[b.stop] = p
}
b.size++
b.stop++
}
// Purge expired data from the window.
func (b *windowBuffer) purge(oldest time.Time) {
b.Lock()
defer b.Unlock()
l := len(b.window)
if l == 0 {
return
}
if b.start < b.stop {
for ; b.start < b.stop; b.start++ {
if !b.window[b.start].Time.Before(oldest) {
break
}
}
b.size = b.stop - b.start
} else {
if !b.window[l-1].Time.Before(oldest) {
for ; b.start < l; b.start++ {
if !b.window[b.start].Time.Before(oldest) {
break
}
}
b.size = l - b.start + b.stop
} else {
for b.start = 0; b.start < b.stop; b.start++ {
if !b.window[b.start].Time.Before(oldest) {
break
}
}
b.size = b.stop - b.start
}
}
}
// Returns a copy of the current buffer.
func (b *windowBuffer) batch() models.Batch {
b.Lock()
defer b.Unlock()
batch := models.Batch{}
if b.size == 0 {
return batch
}
batch.Points = make([]models.TimeFields, b.size)
if b.stop > b.start {
for i, p := range b.window[b.start:b.stop] {
batch.Points[i] = models.TimeFields{Time: p.Time, Fields: p.Fields}
}
} else {
j := 0
l := len(b.window)
for i := b.start; i < l; i++ {
p := b.window[i]
batch.Points[j] = models.TimeFields{Time: p.Time, Fields: p.Fields}
j++
}
for i := 0; i < b.stop; i++ {
p := b.window[i]
batch.Points[j] = models.TimeFields{Time: p.Time, Fields: p.Fields}
j++
}
}
return batch
}