-
Notifications
You must be signed in to change notification settings - Fork 3
/
sample_batch.go
280 lines (252 loc) · 7.88 KB
/
sample_batch.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package bitflow
import (
"fmt"
"sync"
"time"
"github.com/antongulenko/golib"
log "github.com/sirupsen/logrus"
)
type BatchProcessor struct {
NoopProcessor
checker HeaderChecker
samples []*Sample
shutdown bool
Steps []BatchProcessingStep
FlushTimeout time.Duration // If > 0, flush when no new samples are received for the given duration. The wall-time is used for this (not sample timestamps)
SampleTimestampFlushTimeout time.Duration // If > 0, flush when a sample is received with a timestamp jump bigger than this
lastAutoFlushError error
lastSample time.Time // Wall time when receiving last sample
lastSampleTimestamp time.Time // Timestamp of last sample
FlushTags []string // If set, flush every time any of these tags change
lastFlushTags []string
flushHeader *Header
flushTrigger *golib.TimeoutCond // Used to trigger flush and to notify about finished flush. Relies on Sample()/Close() being synchronized externally.
flushError error
}
type BatchProcessingStep interface {
ProcessBatch(header *Header, samples []*Sample) (*Header, []*Sample, error)
String() string
}
type ResizingBatchProcessingStep interface {
BatchProcessingStep
OutputSampleSize(sampleSize int) int
}
func (p *BatchProcessor) OutputSampleSize(sampleSize int) int {
for _, step := range p.Steps {
if step, ok := step.(ResizingBatchProcessingStep); ok {
sampleSize = step.OutputSampleSize(sampleSize)
}
}
return sampleSize
}
func (p *BatchProcessor) Add(step BatchProcessingStep) *BatchProcessor {
p.Steps = append(p.Steps, step)
return p
}
func (p *BatchProcessor) ContainedStringers() []fmt.Stringer {
res := make([]fmt.Stringer, len(p.Steps))
for i, step := range p.Steps {
res[i] = step
}
return res
}
func (p *BatchProcessor) Start(wg *sync.WaitGroup) golib.StopChan {
p.flushTrigger = golib.NewTimeoutCond(new(sync.Mutex))
wg.Add(1)
go p.loopFlush(wg)
return p.NoopProcessor.Start(wg)
}
func (p *BatchProcessor) Sample(sample *Sample, header *Header) (err error) {
oldHeader := p.checker.LastHeader
flush := p.checker.InitializedHeaderChanged(header)
if len(p.FlushTags) > 0 {
values := make([]string, len(p.FlushTags))
for i, tag := range p.FlushTags {
values[i] = sample.Tag(tag)
if oldHeader != nil && len(p.lastFlushTags) > i && p.lastFlushTags[i] != values[i] {
flush = true
}
}
p.lastFlushTags = values
}
if p.SampleTimestampFlushTimeout > 0 {
if !p.lastSampleTimestamp.IsZero() && sample.Time.Sub(p.lastSampleTimestamp) >= p.SampleTimestampFlushTimeout {
flush = true
}
p.lastSampleTimestamp = sample.Time
}
if flush {
err = p.triggerFlush(oldHeader, false)
}
if p.FlushTimeout > 0 {
p.lastSample = time.Now()
if err == nil {
err = p.lastAutoFlushError
}
p.lastAutoFlushError = nil
}
p.samples = append(p.samples, sample)
return
}
func (p *BatchProcessor) Close() {
defer p.NoopProcessor.Close()
header := p.checker.LastHeader
if header == nil {
log.Warnln(p.String(), "received no samples")
}
if err := p.triggerFlush(header, true); err != nil {
p.Error(err)
}
}
func (p *BatchProcessor) triggerFlush(header *Header, shutdown bool) error {
p.flushTrigger.L.Lock()
defer p.flushTrigger.L.Unlock()
p.flushHeader = header
p.flushTrigger.Broadcast()
p.shutdown = shutdown
for p.flushHeader != nil {
p.flushTrigger.Wait() // Will be notified after flush is finished
}
res := p.flushError
p.flushError = nil
return res
}
func (p *BatchProcessor) loopFlush(wg *sync.WaitGroup) {
defer wg.Done()
for p.waitAndExecuteFlush() {
}
}
func (p *BatchProcessor) waitAndExecuteFlush() bool {
p.flushTrigger.L.Lock()
defer p.flushTrigger.L.Unlock()
for p.flushHeader == nil && !p.shutdown && !p.flushTimedOut() {
if p.FlushTimeout > 0 {
p.flushTrigger.WaitTimeout(p.FlushTimeout)
} else {
p.flushTrigger.Wait()
}
}
if p.flushHeader == nil && !p.shutdown {
// Automatic flush after timeout
err := p.executeFlush(p.checker.LastHeader)
if err != nil {
log.Errorf("%v: Error during automatic flush (will be returned when next sample arrives): %v", p, err)
p.lastAutoFlushError = fmt.Errorf("Error during previous auto-flush: %v", err)
}
p.lastSample = time.Now()
} else {
p.flushError = p.executeFlush(p.flushHeader)
p.flushTrigger.Broadcast()
}
p.flushHeader = nil
return !p.shutdown
}
func (p *BatchProcessor) flushTimedOut() bool {
if p.FlushTimeout <= 0 || p.lastSample.IsZero() {
return false
}
return time.Now().Sub(p.lastSample) >= p.FlushTimeout
}
func (p *BatchProcessor) executeFlush(header *Header) error {
samples := p.samples
if len(samples) == 0 || header == nil {
return nil
}
p.samples = nil // Allow garbage collection
if samples, header, err := p.executeSteps(samples, header); err != nil {
return err
} else {
if header == nil {
return fmt.Errorf("Cannot flush %v samples because nil-header was returned by last batch processing step", len(samples))
}
if len(samples) > 0 {
log.Println("Flushing", len(samples), "batched samples with", len(header.Fields), "metrics")
for _, sample := range samples {
if err := p.NoopProcessor.Sample(sample, header); err != nil {
return fmt.Errorf("Error flushing batch: %v", err)
}
}
}
return nil
}
}
func (p *BatchProcessor) executeSteps(samples []*Sample, header *Header) ([]*Sample, *Header, error) {
if len(p.Steps) > 0 {
log.Debugln("Executing", len(p.Steps), "batch processing step(s)")
for i, step := range p.Steps {
if len(samples) == 0 {
log.Warnln("Cannot execute remaining", len(p.Steps)-i, "batch step(s) because the batch with", len(header.Fields), "has no samples")
break
} else {
log.Println("Executing", step, "on", len(samples), "samples with", len(header.Fields), "metrics")
var err error
header, samples, err = step.ProcessBatch(header, samples)
if err != nil {
return nil, nil, err
}
}
}
}
return samples, header, nil
}
func (p *BatchProcessor) String() string {
extra := "s"
if len(p.Steps) == 1 {
extra = ""
}
flushed := ""
if len(p.FlushTags) > 0 {
flushed = fmt.Sprintf(", flushed with tags %v", p.FlushTags)
}
if p.FlushTimeout > 0 {
flushed += fmt.Sprintf(", auto-flushed after %v", p.FlushTimeout)
}
if p.SampleTimestampFlushTimeout > 0 {
flushed += fmt.Sprintf(", flushed when sample timestamp difference over %v", p.SampleTimestampFlushTimeout)
}
return fmt.Sprintf("BatchProcessor (%v step%s%s)", len(p.Steps), extra, flushed)
}
func (p *BatchProcessor) MergeProcessor(other SampleProcessor) bool {
if otherBatch, ok := other.(*BatchProcessor); ok && p.compatibleParameters(otherBatch) {
p.Steps = append(p.Steps, otherBatch.Steps...)
return true
} else {
return false
}
}
func (p *BatchProcessor) compatibleParameters(other *BatchProcessor) bool {
if (other.FlushTimeout != 0 && other.FlushTimeout != p.FlushTimeout) ||
(other.SampleTimestampFlushTimeout != 0 && other.SampleTimestampFlushTimeout != p.SampleTimestampFlushTimeout) {
return false
}
if len(other.FlushTags) == 0 {
return true
}
return golib.EqualStrings(p.FlushTags, other.FlushTags)
}
// ==================== Simple implementation ====================
type SimpleBatchProcessingStep struct {
Description string
Process func(header *Header, samples []*Sample) (*Header, []*Sample, error)
OutputSampleSizeFunc func(sampleSize int) int
}
func (s *SimpleBatchProcessingStep) ProcessBatch(header *Header, samples []*Sample) (*Header, []*Sample, error) {
if process := s.Process; process == nil {
return nil, nil, fmt.Errorf("%v: Process function is not set", s)
} else {
return process(header, samples)
}
}
func (s *SimpleBatchProcessingStep) String() string {
if s.Description == "" {
return "SimpleBatchProcessingStep"
} else {
return s.Description
}
}
func (s *SimpleBatchProcessingStep) OutputSampleSize(sampleSize int) int {
if f := s.OutputSampleSizeFunc; f != nil {
return f(sampleSize)
}
return sampleSize
}