-
Notifications
You must be signed in to change notification settings - Fork 796
/
fetch.go
289 lines (247 loc) · 7.72 KB
/
fetch.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
package kafka
import (
"context"
"fmt"
"math"
"net"
"time"
"github.com/segmentio/kafka-go/protocol"
fetchAPI "github.com/segmentio/kafka-go/protocol/fetch"
)
// FetchRequest represents a request sent to a kafka broker to retrieve records
// from a topic partition.
type FetchRequest struct {
// Address of the kafka broker to send the request to.
Addr net.Addr
// Topic, partition, and offset to retrieve records from. The offset may be
// one of the special FirstOffset or LastOffset constants, in which case the
// request will automatically discover the first or last offset of the
// partition and submit the request for these.
Topic string
Partition int
Offset int64
// Size and time limits of the response returned by the broker.
MinBytes int64
MaxBytes int64
MaxWait time.Duration
// The isolation level for the request.
//
// Defaults to ReadUncommitted.
//
// This field requires the kafka broker to support the Fetch API in version
// 4 or above (otherwise the value is ignored).
IsolationLevel IsolationLevel
}
// FetchResponse represents a response from a kafka broker to a fetch request.
type FetchResponse struct {
// The amount of time that the broker throttled the request.
Throttle time.Duration
// The topic and partition that the response came for (will match the values
// in the request).
Topic string
Partition int
// Information about the topic partition layout returned from the broker.
//
// LastStableOffset requires the kafka broker to support the Fetch API in
// version 4 or above (otherwise the value is zero).
//
/// LogStartOffset requires the kafka broker to support the Fetch API in
// version 5 or above (otherwise the value is zero).
HighWatermark int64
LastStableOffset int64
LogStartOffset int64
// An error that may have occurred while attempting to fetch the records.
//
// The error contains both the kafka error code, and an error message
// returned by the kafka broker. Programs may use the standard errors.Is
// function to test the error against kafka error codes.
Error error
// The set of records returned in the response.
//
// The program is expected to call the RecordSet's Close method when it
// finished reading the records.
//
// Note that kafka may return record batches that start at an offset before
// the one that was requested. It is the program's responsibility to skip
// the offsets that it is not interested in.
Records RecordReader
}
// Fetch sends a fetch request to a kafka broker and returns the response.
//
// If the broker returned an invalid response with no topics, an error wrapping
// protocol.ErrNoTopic is returned.
//
// If the broker returned an invalid response with no partitions, an error
// wrapping ErrNoPartitions is returned.
func (c *Client) Fetch(ctx context.Context, req *FetchRequest) (*FetchResponse, error) {
timeout := c.timeout(ctx, math.MaxInt64)
maxWait := req.maxWait()
if maxWait < timeout {
timeout = maxWait
}
offset := req.Offset
switch offset {
case FirstOffset, LastOffset:
topic, partition := req.Topic, req.Partition
r, err := c.ListOffsets(ctx, &ListOffsetsRequest{
Addr: req.Addr,
Topics: map[string][]OffsetRequest{
topic: {{
Partition: partition,
Timestamp: offset,
}},
},
})
if err != nil {
return nil, fmt.Errorf("kafka.(*Client).Fetch: %w", err)
}
for _, p := range r.Topics[topic] {
if p.Partition == partition {
if p.Error != nil {
return nil, fmt.Errorf("kafka.(*Client).Fetch: %w", p.Error)
}
switch offset {
case FirstOffset:
offset = p.FirstOffset
case LastOffset:
offset = p.LastOffset
}
break
}
}
}
m, err := c.roundTrip(ctx, req.Addr, &fetchAPI.Request{
ReplicaID: -1,
MaxWaitTime: milliseconds(timeout),
MinBytes: int32(req.MinBytes),
MaxBytes: int32(req.MaxBytes),
IsolationLevel: int8(req.IsolationLevel),
SessionID: -1,
SessionEpoch: -1,
Topics: []fetchAPI.RequestTopic{{
Topic: req.Topic,
Partitions: []fetchAPI.RequestPartition{{
Partition: int32(req.Partition),
CurrentLeaderEpoch: -1,
FetchOffset: offset,
LogStartOffset: -1,
PartitionMaxBytes: int32(req.MaxBytes),
}},
}},
})
if err != nil {
return nil, fmt.Errorf("kafka.(*Client).Fetch: %w", err)
}
res := m.(*fetchAPI.Response)
if len(res.Topics) == 0 {
return nil, fmt.Errorf("kafka.(*Client).Fetch: %w", protocol.ErrNoTopic)
}
topic := &res.Topics[0]
if len(topic.Partitions) == 0 {
return nil, fmt.Errorf("kafka.(*Client).Fetch: %w", protocol.ErrNoPartition)
}
partition := &topic.Partitions[0]
ret := &FetchResponse{
Throttle: makeDuration(res.ThrottleTimeMs),
Topic: topic.Topic,
Partition: int(partition.Partition),
Error: makeError(res.ErrorCode, ""),
HighWatermark: partition.HighWatermark,
LastStableOffset: partition.LastStableOffset,
LogStartOffset: partition.LogStartOffset,
Records: partition.RecordSet.Records,
}
if partition.ErrorCode != 0 {
ret.Error = makeError(partition.ErrorCode, "")
}
if ret.Records == nil {
ret.Records = NewRecordReader()
}
return ret, nil
}
func (req *FetchRequest) maxWait() time.Duration {
if req.MaxWait > 0 {
return req.MaxWait
}
return defaultMaxWait
}
type fetchRequestV2 struct {
ReplicaID int32
MaxWaitTime int32
MinBytes int32
Topics []fetchRequestTopicV2
}
func (r fetchRequestV2) size() int32 {
return 4 + 4 + 4 + sizeofArray(len(r.Topics), func(i int) int32 { return r.Topics[i].size() })
}
func (r fetchRequestV2) writeTo(wb *writeBuffer) {
wb.writeInt32(r.ReplicaID)
wb.writeInt32(r.MaxWaitTime)
wb.writeInt32(r.MinBytes)
wb.writeArray(len(r.Topics), func(i int) { r.Topics[i].writeTo(wb) })
}
type fetchRequestTopicV2 struct {
TopicName string
Partitions []fetchRequestPartitionV2
}
func (t fetchRequestTopicV2) size() int32 {
return sizeofString(t.TopicName) +
sizeofArray(len(t.Partitions), func(i int) int32 { return t.Partitions[i].size() })
}
func (t fetchRequestTopicV2) writeTo(wb *writeBuffer) {
wb.writeString(t.TopicName)
wb.writeArray(len(t.Partitions), func(i int) { t.Partitions[i].writeTo(wb) })
}
type fetchRequestPartitionV2 struct {
Partition int32
FetchOffset int64
MaxBytes int32
}
func (p fetchRequestPartitionV2) size() int32 {
return 4 + 8 + 4
}
func (p fetchRequestPartitionV2) writeTo(wb *writeBuffer) {
wb.writeInt32(p.Partition)
wb.writeInt64(p.FetchOffset)
wb.writeInt32(p.MaxBytes)
}
type fetchResponseV2 struct {
ThrottleTime int32
Topics []fetchResponseTopicV2
}
func (r fetchResponseV2) size() int32 {
return 4 + sizeofArray(len(r.Topics), func(i int) int32 { return r.Topics[i].size() })
}
func (r fetchResponseV2) writeTo(wb *writeBuffer) {
wb.writeInt32(r.ThrottleTime)
wb.writeArray(len(r.Topics), func(i int) { r.Topics[i].writeTo(wb) })
}
type fetchResponseTopicV2 struct {
TopicName string
Partitions []fetchResponsePartitionV2
}
func (t fetchResponseTopicV2) size() int32 {
return sizeofString(t.TopicName) +
sizeofArray(len(t.Partitions), func(i int) int32 { return t.Partitions[i].size() })
}
func (t fetchResponseTopicV2) writeTo(wb *writeBuffer) {
wb.writeString(t.TopicName)
wb.writeArray(len(t.Partitions), func(i int) { t.Partitions[i].writeTo(wb) })
}
type fetchResponsePartitionV2 struct {
Partition int32
ErrorCode int16
HighwaterMarkOffset int64
MessageSetSize int32
MessageSet messageSet
}
func (p fetchResponsePartitionV2) size() int32 {
return 4 + 2 + 8 + 4 + p.MessageSet.size()
}
func (p fetchResponsePartitionV2) writeTo(wb *writeBuffer) {
wb.writeInt32(p.Partition)
wb.writeInt16(p.ErrorCode)
wb.writeInt64(p.HighwaterMarkOffset)
wb.writeInt32(p.MessageSetSize)
p.MessageSet.writeTo(wb)
}