-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumerbatch.go
195 lines (158 loc) · 5.03 KB
/
consumerbatch.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
package wkafka
import (
"context"
"errors"
"fmt"
"time"
"github.com/twmb/franz-go/pkg/kgo"
)
type consumerBatch[T any] struct {
*customer[T]
// Process is nil for DLQ consumer.
Process func(ctx context.Context, msg []T) error
IsDLQ bool
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 *kgo.Client) error {
for {
// flush the partition handler, it will be ready next poll
c.PartitionHandler.Flush()
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.batchIterationDLQ(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]) batchIteration(ctx context.Context, cl *kgo.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 < c.Cfg.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) {
continue
}
// add to records to commit, with skip or not
records = append(records, r)
// skip precheck and record section
/////////////////////////////////
if c.Skip(c.Cfg, r) {
c.Logger.Info("record skipped", "topic", r.Topic, "partition", r.Partition, "offset", r.Offset)
continue
}
if c.PreCheck != nil {
if err := c.PreCheck(ctx, r); err != nil {
if errors.Is(err, ErrSkip) {
continue
}
return fmt.Errorf("pre check failed: %w", err)
}
}
data, err := c.Decode(r.Value, r)
if err != nil {
if errors.Is(err, ErrSkip) {
continue
}
return fmt.Errorf("decode record failed: %w", err)
}
/////////////////////////////////
// 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)
start := time.Now()
if err := c.Process(ctxCallback, batch); err != nil {
c.Meter.Meter(start, int64(len(batch)), r.Topic, err, false)
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))
}
} else {
c.Meter.Meter(start, int64(len(batch)), r.Topic, err, false)
}
// if partitions are revoked then don't commit, filter out revoked records
records, _ = c.PartitionHandler.IsRevokedRecordBatch(records)
if len(records) != 0 {
if err := cl.CommitRecords(ctx, records...); err != nil {
return fmt.Errorf("commit batch records failed: %w", err)
}
}
batch = make([]T, 0, batchCount)
batchRecords = make([]*kgo.Record, 0, batchCount)
records = make([]*kgo.Record, 0, batchCount)
}
return nil
}
// batchIterationDLQ is the same as single interation DLQ.
func (c *consumerBatch[T]) batchIterationDLQ(ctx context.Context, cl *kgo.Client, fetch kgo.Fetches) error {
for iter := fetch.RecordIter(); !iter.Done(); {
r := iter.Next()
// check partition is revoked
if c.PartitionHandler.IsRevokedRecord(r) {
continue
}
// listening DLQ topics
// check partition is revoked and not commit it!
// when error return than it will not be committed
if err := c.DLQProcess.Iteration(ctx, r); err != nil {
if errors.Is(err, errPartitionRevoked) {
// don't commit revoked partition
// above check also skip others on that partition
continue
}
return wrapErr(r, err, c.IsDLQ)
}
// commit if not see any error
if err := cl.CommitRecords(ctx, r); err != nil {
return wrapErr(r, fmt.Errorf("commit records failed: %w", err), c.IsDLQ)
}
}
return nil
}