-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsumerbatch.go
More file actions
265 lines (213 loc) · 7.07 KB
/
consumerbatch.go
File metadata and controls
265 lines (213 loc) · 7.07 KB
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
package wkafka
import (
"context"
"errors"
"fmt"
"github.com/twmb/franz-go/pkg/kgo"
"golang.org/x/sync/errgroup"
)
type consumerBatch[T any] struct {
*customer[T]
// Process is nil for DLQ consumer.
Process func(ctx context.Context, msg []T) error
IsDLQ bool
Group groupRecords
PartitionHandler *partitionHandler
DLQProcess *dlqProcess[T]
}
func (c *consumerBatch[T]) setPreCheck(fn func(ctx context.Context, r *kgo.Record) error) {
c.PreCheck = fn
}
func (c *consumerBatch[T]) Consume(ctx context.Context, cl client) error {
for {
// flush the partition handler, it will be ready next poll
c.PartitionHandler.Flush()
// if block on poll then allow rebalance
cl.AllowRebalance()
fetch := cl.PollRecords(ctx, c.Cfg.MaxPollRecords)
if fetch.IsClientClosed() {
return errClientClosed
}
if err := fetch.Err(); err != nil {
return fmt.Errorf("poll fetch err: %w", err)
}
// TODO check is needed?
if fetch.Empty() {
continue
}
if c.IsDLQ {
if err := c.DLQProcess.Iteration(ctx, cl, fetch); err != nil {
return err
}
continue
}
if c.Cfg.Concurrent.Enabled && c.Cfg.Concurrent.Process > 1 {
if err := c.batchIterationConcurrent(ctx, cl, fetch); err != nil {
return err
}
continue
}
if err := c.batchIteration(ctx, cl, fetch); err != nil {
return err
}
}
}
/////////////////////////////////
// BATCH - ITERATION
/////////////////////////////////
func (c *consumerBatch[T]) batchIterationConcurrent(ctx context.Context, cl client, fetch kgo.Fetches) error {
for iter := fetch.RecordIter(); !iter.Done(); {
r := iter.Next()
// check partition is revoked if not then add to group
if !c.PartitionHandler.IsRevokedRecord(r) {
c.Group.Add(r)
}
if !iter.Done() && !c.Group.IsEnough() {
continue
}
errGroup, ctxGroup := errgroup.WithContext(ctx)
errGroup.SetLimit(c.Cfg.Concurrent.Process)
c.Group.Merge()
for records := range c.Group.Iter() {
errGroup.Go(func() error {
batch := make([]T, 0, len(records))
batchRecords := make([]*kgo.Record, 0, len(records))
for _, record := range records {
// skip precheck and record section
/////////////////////////////////
data, skip, err := c.batchRecord(ctxGroup, record)
if err != nil {
return err
}
if !skip {
// add to batch if record is not skipped
batch = append(batch, data)
batchRecords = append(batchRecords, record)
}
}
/////////////////////////////////
ctxCallback := context.WithValue(ctxGroup, KeyRecord, batchRecords)
if err := c.Process(ctxCallback, batch); err != nil {
errOrg, ok := IsDQLError(err)
if !ok {
// it is not DLQ error, return it
// this will fail the service
return fmt.Errorf("process batch failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
if c.ProduceDLQ != nil {
if err := c.ProduceDLQ(ctxGroup, errOrg, batchRecords); err != nil {
return fmt.Errorf("produce to DLQ failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
} else {
// returning a batch error could be confusing
return fmt.Errorf("process batch failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
}
return nil
})
}
if err := errGroup.Wait(); err != nil {
return fmt.Errorf("wait group failed: %w", err)
}
// commit all records in group
records := c.Group.AllRecords()
records, _ = c.PartitionHandler.IsRevokedRecordBatch(records)
if len(records) != 0 {
cl.MarkCommitRecords(records...)
}
c.Group.Reset()
}
return nil
}
func (c *consumerBatch[T]) batchIteration(ctx context.Context, cl client, fetch kgo.Fetches) error {
// get the batch count but not more than the number of records
batchCount := c.Cfg.BatchCount
if v := fetch.NumRecords(); v < batchCount {
batchCount = v
}
// batch to process
batch := make([]T, 0, batchCount)
// records is used to commit
records := make([]*kgo.Record, 0, batchCount)
// batchRecords is used to add to context so callback can see the records when needed
batchRecords := make([]*kgo.Record, 0, batchCount)
for iter := fetch.RecordIter(); !iter.Done(); {
r := iter.Next()
// ignore revoked partitions in our fetch, don't commit them
if !c.PartitionHandler.IsRevokedRecord(r) {
// add to records to commit, with skip or not
records = append(records, r)
// skip precheck and record section
/////////////////////////////////
data, skip, err := c.batchRecord(ctx, r)
if err != nil {
return err
}
if !skip {
// add to batch if record is not skipped
batch = append(batch, data)
batchRecords = append(batchRecords, r)
}
}
/////////////////////////////////
// fill the batch but if we are last record then process it
if !iter.Done() && len(batch) < batchCount {
continue
}
// add records to context so callback can see the records when needed
ctxCallback := context.WithValue(ctx, KeyRecord, batchRecords)
if err := c.Process(ctxCallback, batch); err != nil {
errOrg, ok := IsDQLError(err)
if !ok {
// it is not DLQ error, return it
// this will fail the service
return fmt.Errorf("process batch failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
if c.ProduceDLQ != nil {
if err := c.ProduceDLQ(ctx, errOrg, batchRecords); err != nil {
return fmt.Errorf("produce to DLQ failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
} else {
// returning a batch error could be confusing
return fmt.Errorf("process batch failed: %w; offsets: %s", err, errorOffsetList(batchRecords))
}
}
// if partitions are revoked then don't commit, filter out revoked records
records, _ = c.PartitionHandler.IsRevokedRecordBatch(records)
if len(records) != 0 {
cl.MarkCommitRecords(records...)
}
// Instead of allocating new slices, reuse the existing slices by reslicing to zero length.
// This avoids unnecessary allocations and keeps the underlying arrays.
batch = batch[:0]
batchRecords = batchRecords[:0]
records = records[:0]
}
return nil
}
// batchRecord returns true if the record was skipped, false if it was processed.
func (c *consumerBatch[T]) batchRecord(ctx context.Context, r *kgo.Record) (T, bool, error) {
var zero T
if c.Skip(c.Cfg, r) {
c.Logger.Debug("record skipped", "topic", r.Topic, "partition", r.Partition, "offset", r.Offset)
return zero, true, nil
}
if c.PreCheck != nil {
if err := c.PreCheck(ctx, r); err != nil {
if errors.Is(err, ErrSkip) {
c.Logger.Debug("record skipped on precheck", "topic", r.Topic, "partition", r.Partition, "offset", r.Offset, "error", err)
return zero, true, nil
}
return zero, false, fmt.Errorf("pre check failed: %w", wrapErr(r, err, false))
}
}
data, err := c.Decode(r.Value, r)
if err != nil {
if errors.Is(err, ErrSkip) {
c.Logger.Info("record skipped on decode", "topic", r.Topic, "partition", r.Partition, "offset", r.Offset, "error", err)
return zero, true, nil
}
return zero, false, fmt.Errorf("decode record failed: %w", wrapErr(r, err, false))
}
return data, false, nil
}