/
batch.go
212 lines (174 loc) · 4.2 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
package pipeline
import (
"sync"
"time"
"github.com/ozonru/file.d/logger"
"github.com/ozonru/file.d/longpanic"
)
type Batch struct {
Events []*Event
seq int64
size int
timeout time.Duration
startTime time.Time
}
func newBatch(size int, timeout time.Duration) *Batch {
if size <= 0 {
logger.Fatalf("why batch size is 0?")
}
return &Batch{
size: size,
timeout: timeout,
Events: make([]*Event, 0, size),
}
}
func (b *Batch) reset() {
b.Events = b.Events[:0]
b.startTime = time.Now()
}
func (b *Batch) append(e *Event) {
b.Events = append(b.Events, e)
}
func (b *Batch) isReady() bool {
l := len(b.Events)
isFull := l == b.size
isTimeout := l > 0 && time.Now().Sub(b.startTime) > b.timeout
return isFull || isTimeout
}
type Batcher struct {
pipelineName string
outputType string
outFn BatcherOutFn
maintenanceFn BatcherMaintenanceFn
controller OutputPluginController
workerCount int
batchSize int
flushTimeout time.Duration
maintenanceInterval time.Duration
shouldStop bool
batch *Batch
// cycle of batches: freeBatches => fullBatches, fullBatches => freeBatches
freeBatches chan *Batch
fullBatches chan *Batch
mu *sync.Mutex
seqMu *sync.Mutex
cond *sync.Cond
outSeq int64
commitSeq int64
}
type (
BatcherOutFn func(*WorkerData, *Batch)
BatcherMaintenanceFn func(*WorkerData)
)
func NewBatcher(
pipelineName string,
outputType string,
outFn BatcherOutFn,
maintenanceFn BatcherMaintenanceFn,
controller OutputPluginController,
workers int,
batchSize int,
flushTimeout time.Duration,
maintenanceInterval time.Duration,
) *Batcher {
return &Batcher{
pipelineName: pipelineName,
outputType: outputType,
outFn: outFn,
maintenanceFn: maintenanceFn,
controller: controller,
workerCount: workers,
batchSize: batchSize,
flushTimeout: flushTimeout,
maintenanceInterval: maintenanceInterval,
}
}
func (b *Batcher) Start() {
b.mu = &sync.Mutex{}
b.seqMu = &sync.Mutex{}
b.cond = sync.NewCond(b.seqMu)
b.freeBatches = make(chan *Batch, b.workerCount)
b.fullBatches = make(chan *Batch, b.workerCount)
for i := 0; i < b.workerCount; i++ {
b.freeBatches <- newBatch(b.batchSize, b.flushTimeout)
longpanic.Go(b.work)
}
longpanic.Go(b.heartbeat)
}
type WorkerData interface{}
func (b *Batcher) work() {
t := time.Now()
events := make([]*Event, 0, 0)
data := WorkerData(nil)
for batch := range b.fullBatches {
b.outFn(&data, batch)
events = b.commitBatch(events, batch)
shouldRunMaintenance := b.maintenanceFn != nil && b.maintenanceInterval != 0 && time.Now().Sub(t) > b.maintenanceInterval
if shouldRunMaintenance {
t = time.Now()
b.maintenanceFn(&data)
}
}
}
func (b *Batcher) commitBatch(events []*Event, batch *Batch) []*Event {
// we need to release batch first and then commit events
// so lets exchange local slice with batch slice to avoid data copying
tmp := events
events = batch.Events
batch.Events = tmp
batchSeq := batch.seq
// lets 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++
for _, e := range events {
b.controller.Commit(e)
}
b.cond.Broadcast()
b.seqMu.Unlock()
b.freeBatches <- batch
return events
}
func (b *Batcher) heartbeat() {
for {
if b.shouldStop {
return
}
b.mu.Lock()
batch := b.getBatch()
b.trySendBatchAndUnlock(batch)
time.Sleep(time.Millisecond * 100)
}
}
func (b *Batcher) Add(event *Event) {
b.mu.Lock()
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.isReady() {
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.shouldStop = true
close(b.freeBatches)
close(b.fullBatches)
}