/
batch.go
320 lines (263 loc) · 7.04 KB
/
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package pipeline
import (
"context"
"sync"
"time"
"github.com/ozontech/file.d/logger"
"github.com/ozontech/file.d/metric"
"github.com/prometheus/client_golang/prometheus"
)
type BatchStatus byte
const (
BatchStatusNotReady BatchStatus = iota
BatchStatusMaxSizeExceeded
BatchStatusTimeoutExceeded
)
type Batch struct {
events []*Event
// hasIterableEvents is the truth if the Batch contains iterable events
hasIterableEvents bool
// eventsSize contains total size of the Events in bytes
eventsSize int
seq int64
timeout time.Duration
startTime time.Time
// maxSizeCount max events per batch
maxSizeCount int
// maxSizeBytes max size of events per batch in bytes
maxSizeBytes int
status BatchStatus
}
func NewPreparedBatch(events []*Event) *Batch {
b := &Batch{}
b.reset()
b.events = events
return b
}
func newBatch(maxSizeCount, maxSizeBytes int, timeout time.Duration) *Batch {
if maxSizeCount < 0 {
logger.Fatalf("why batch max count less than 0?")
}
if maxSizeBytes < 0 {
logger.Fatalf("why batch size less than 0?")
}
if maxSizeCount == 0 && maxSizeBytes == 0 {
logger.Fatalf("batch limits are not set")
}
b := &Batch{
maxSizeCount: maxSizeCount,
maxSizeBytes: maxSizeBytes,
timeout: timeout,
events: make([]*Event, 0, maxSizeCount),
}
b.reset()
return b
}
func (b *Batch) reset() {
b.events = b.events[:0]
b.eventsSize = 0
b.status = BatchStatusNotReady
b.hasIterableEvents = false
b.startTime = time.Now()
}
func (b *Batch) append(e *Event) {
b.hasIterableEvents = b.hasIterableEvents || !e.IsChildParentKind()
b.events = append(b.events, e)
b.eventsSize += e.Size
}
func (b *Batch) updateStatus() BatchStatus {
l := len(b.events)
if len(b.events) == 0 {
// batch is empty
return BatchStatusNotReady
}
switch {
case (b.maxSizeCount != 0 && l >= b.maxSizeCount) || (b.maxSizeBytes != 0 && b.maxSizeBytes <= b.eventsSize):
b.status = BatchStatusMaxSizeExceeded
case l > 0 && time.Since(b.startTime) > b.timeout:
b.status = BatchStatusTimeoutExceeded
default:
b.status = BatchStatusNotReady
}
return b.status
}
func (b *Batch) ForEach(cb func(event *Event)) {
for _, event := range b.events {
if event.IsChildParentKind() {
continue
}
cb(event)
}
}
type Batcher struct {
opts BatcherOptions
batch *Batch
// cycle of batches: freeBatches => fullBatches, fullBatches => freeBatches
freeBatches chan *Batch
fullBatches chan *Batch
workersWg sync.WaitGroup
mu sync.Mutex
shouldStop bool
seqMu *sync.Mutex
cond *sync.Cond
outSeq int64
commitSeq int64
batchOutFnSeconds prometheus.Observer
commitWaitingSeconds prometheus.Observer
workersInProgress prometheus.Gauge
batchesDoneByMaxSize prometheus.Counter
batchesDoneByTimeout prometheus.Counter
}
type (
BatcherOutFn func(*WorkerData, *Batch)
BatcherMaintenanceFn func(*WorkerData)
BatcherOptions struct {
PipelineName string
OutputType string
OutFn BatcherOutFn
MaintenanceFn BatcherMaintenanceFn
Controller OutputPluginController
Workers int
BatchSizeCount int
BatchSizeBytes int
FlushTimeout time.Duration
MaintenanceInterval time.Duration
MetricCtl *metric.Ctl
}
)
func NewBatcher(opts BatcherOptions) *Batcher { // nolint: gocritic // hugeParam is ok here
ctl := opts.MetricCtl
jobsDone := ctl.RegisterCounterVec("batcher_jobs_done_total", "", "status")
freeBatches := make(chan *Batch, opts.Workers)
fullBatches := make(chan *Batch, opts.Workers)
for i := 0; i < opts.Workers; i++ {
freeBatches <- newBatch(opts.BatchSizeCount, opts.BatchSizeBytes, opts.FlushTimeout)
}
seqMu := &sync.Mutex{}
return &Batcher{
seqMu: seqMu,
cond: sync.NewCond(seqMu),
freeBatches: freeBatches,
fullBatches: fullBatches,
opts: opts,
batchOutFnSeconds: ctl.RegisterHistogram("batcher_out_fn_seconds", "", metric.SecondsBucketsLong),
commitWaitingSeconds: ctl.RegisterHistogram("batcher_commit_waiting_seconds", "", metric.SecondsBucketsDetailed),
workersInProgress: ctl.RegisterGauge("batcher_workers_in_progress", ""),
batchesDoneByMaxSize: jobsDone.WithLabelValues("max_size_exceeded"),
batchesDoneByTimeout: jobsDone.WithLabelValues("timeout_exceeded"),
}
}
func (b *Batcher) Start(ctx context.Context) {
go func() {
<-ctx.Done()
b.Stop()
}()
b.workersWg.Add(b.opts.Workers)
for i := 0; i < b.opts.Workers; i++ {
go b.work()
}
go b.heartbeat()
}
type WorkerData any
func (b *Batcher) work() {
defer b.workersWg.Done()
t := time.Now()
data := WorkerData(nil)
for batch := range b.fullBatches {
b.workersInProgress.Inc()
if batch.hasIterableEvents {
now := time.Now()
b.opts.OutFn(&data, batch)
b.batchOutFnSeconds.Observe(time.Since(now).Seconds())
}
status := b.commitBatch(batch)
shouldRunMaintenance := b.opts.MaintenanceFn != nil && b.opts.MaintenanceInterval != 0 && time.Since(t) > b.opts.MaintenanceInterval
if shouldRunMaintenance {
t = time.Now()
b.opts.MaintenanceFn(&data)
}
b.workersInProgress.Dec()
switch status {
case BatchStatusMaxSizeExceeded:
b.batchesDoneByMaxSize.Inc()
case BatchStatusTimeoutExceeded:
b.batchesDoneByTimeout.Inc()
default:
logger.Panic("unreachable")
}
}
}
func (b *Batcher) commitBatch(batch *Batch) BatchStatus {
batchSeq := batch.seq
// When we sent the batch, we don’t need buffers and insaneJSON.Root.
// We can copy the information we need and release the events to reuse them.
// But at the moment it's hard to maintain -- plugins
// can reuse specific Event fields in the Commit func,
// like .Root, .streamName, .Buf
now := time.Now()
// let's restore the sequence of batches to make sure input will commit offsets incrementally
b.seqMu.Lock()
for b.commitSeq != batchSeq {
b.cond.Wait()
}
b.commitSeq++
b.commitWaitingSeconds.Observe(time.Since(now).Seconds())
for i := range batch.events {
b.opts.Controller.Commit(batch.events[i])
}
status := batch.status
b.freeBatches <- batch
b.cond.Broadcast()
b.seqMu.Unlock()
return status
}
func (b *Batcher) heartbeat() {
for {
b.mu.Lock()
if b.shouldStop {
b.mu.Unlock()
return
}
batch := b.getBatch()
b.trySendBatchAndUnlock(batch)
time.Sleep(time.Millisecond * 100)
}
}
func (b *Batcher) Add(event *Event) {
b.mu.Lock()
if b.shouldStop {
b.mu.Unlock()
return
}
batch := b.getBatch()
batch.append(event)
b.trySendBatchAndUnlock(batch)
}
// trySendBatch mu should be locked, and it'll be unlocked after execution of this function
func (b *Batcher) trySendBatchAndUnlock(batch *Batch) {
if batch.updateStatus() == BatchStatusNotReady {
b.mu.Unlock()
return
}
batch.seq = b.outSeq
b.outSeq++
b.batch = nil
b.mu.Unlock()
b.fullBatches <- batch
}
func (b *Batcher) getBatch() *Batch {
if b.batch == nil {
b.batch = <-b.freeBatches
b.batch.reset()
}
return b.batch
}
func (b *Batcher) Stop() {
b.mu.Lock()
if !b.shouldStop {
b.shouldStop = true
close(b.fullBatches)
}
b.mu.Unlock()
b.workersWg.Wait()
}