This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
forked from elodina/go_kafka_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_level_client.go
355 lines (307 loc) · 12.2 KB
/
low_level_client.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package go_kafka_client
import (
"fmt"
"github.com/Shopify/sarama"
"github.com/stealthly/siesta"
"time"
)
// LowLevelClient is a low-level Kafka client that manages broker connections, responsible to fetch metadata and is able
// to handle Fetch and Offset requests.
//TODO not sure that's a good name for this interface
type LowLevelClient interface {
// This will be called right after connecting to ConsumerCoordinator so this client can initialize itself
// with bootstrap broker list for example. May return an error to signal this client is unable to work with given configuration.
Initialize() error
// This will be called each time the fetch request to Kafka should be issued. Topic, partition and offset are self-explanatory.
// Should return a slice of Messages and an error if a fetch error occurred.
// Note that for performance reasons it makes sense to keep open broker connections and reuse them on every fetch call.
Fetch(topic string, partition int32, offset int64) ([]*Message, error)
// This will be called when call to Fetch returns an error. As every client has different error mapping we ask here explicitly
// whether the returned error is an OffsetOutOfRange error. Should return true if it is, false otherwise.
IsOffsetOutOfRange(error) bool
// This will be called to handle OffsetOutOfRange error. OffsetTime will be either "smallest" or "largest".
// Should return a corresponding offset value and an error if it occurred.
GetAvailableOffset(topic string, partition int32, offsetTime string) (int64, error)
// This will be called to gracefully shutdown this client.
Close()
}
// SaramaClient implements LowLevelClient and uses github.com/Shopify/sarama as underlying implementation.
type SaramaClient struct {
config *ConsumerConfig
client sarama.Client
}
// Creates a new SaramaClient using a given ConsumerConfig.
func NewSaramaClient(config *ConsumerConfig) *SaramaClient {
return &SaramaClient{
config: config,
}
}
// Returns a string representation of this SaramaClient.
func (this *SaramaClient) String() string {
return "Sarama client"
}
// This will be called right after connecting to ConsumerCoordinator so this client can initialize itself
// with bootstrap broker list for example. May return an error to signal this client is unable to work with given configuration.
func (this *SaramaClient) Initialize() error {
bootstrapBrokers, err := BootstrapBrokers(this.config.Coordinator)
if err != nil {
return err
}
client, err := sarama.NewClient(bootstrapBrokers, nil)
if err != nil {
return err
}
this.client = client
return nil
}
// This will be called each time the fetch request to Kafka should be issued. Topic, partition and offset are self-explanatory.
// Returns slice of Messages and an error if a fetch error occurred.
func (this *SaramaClient) Fetch(topic string, partition int32, offset int64) ([]*Message, error) {
leader, err := this.client.Leader(topic, partition)
if err != nil {
this.client.RefreshMetadata(topic)
return nil, err
}
fetchRequest := new(sarama.FetchRequest)
fetchRequest.MinBytes = this.config.FetchMinBytes
fetchRequest.MaxWaitTime = this.config.FetchWaitMaxMs
Debugf(this, "Adding block: topic=%s, partition=%d, offset=%d, fetchsize=%d", topic, partition, offset, this.config.FetchMessageMaxBytes)
fetchRequest.AddBlock(topic, partition, offset, this.config.FetchMessageMaxBytes)
response, err := leader.Fetch(fetchRequest)
if err != nil {
this.client.RefreshMetadata(topic)
return nil, err
}
messages := make([]*Message, 0)
if response != nil {
Debug(this, "Processing fetch response")
for topic, partitionAndData := range response.Blocks {
for partition, data := range partitionAndData {
switch data.Err {
case sarama.ErrNoError:
{
if len(data.MsgSet.Messages) > 0 {
this.filterPartitionData(data, offset)
messages = this.collectMessages(data, topic, partition)
if this.config.Debug {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
for _, message := range messages {
message.DecodedKey = []int64{timestamp}
}
}
} else {
Debugf(this, "No messages in %s:%d at offset %d", topic, partition, offset)
}
}
default:
{
this.client.RefreshMetadata(topic)
return nil, data.Err
}
}
}
}
}
return messages, nil
}
// Checks whether the given error indicates an OffsetOutOfRange error.
func (this *SaramaClient) IsOffsetOutOfRange(err error) bool {
return err == sarama.ErrOffsetOutOfRange
}
// This will be called to handle OffsetOutOfRange error. OffsetTime will be either "smallest" or "largest".
func (this *SaramaClient) GetAvailableOffset(topic string, partition int32, offsetTime string) (int64, error) {
time := sarama.OffsetNewest
if offsetTime == "smallest" {
time = sarama.OffsetOldest
}
offset, err := this.client.GetOffset(topic, partition, time)
if err != nil {
return -1, nil
}
return offset, nil
}
// Gracefully shuts down this client.
func (this *SaramaClient) Close() {
this.client.Close()
}
func (this *SaramaClient) filterPartitionData(partitionData *sarama.FetchResponseBlock, requestedOffset int64) {
lowestCorrectIndex := 0
for i, v := range partitionData.MsgSet.Messages {
if v.Offset < requestedOffset {
lowestCorrectIndex = i + 1
} else {
break
}
}
partitionData.MsgSet.Messages = partitionData.MsgSet.Messages[lowestCorrectIndex:]
}
func (this *SaramaClient) collectMessages(partitionData *sarama.FetchResponseBlock, topic string, partition int32) []*Message {
messages := make([]*Message, 0)
for _, message := range partitionData.MsgSet.Messages {
if message.Msg.Set != nil {
for _, wrapped := range message.Msg.Set.Messages {
decodedKey, err := this.config.KeyDecoder.Decode(wrapped.Msg.Key)
if err != nil {
//TODO: what if we fail to decode the key: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
decodedValue, err := this.config.ValueDecoder.Decode(wrapped.Msg.Value)
if err != nil {
//TODO: what if we fail to decode the value: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
messages = append(messages, &Message{
Key: wrapped.Msg.Key,
Value: wrapped.Msg.Value,
DecodedKey: decodedKey,
DecodedValue: decodedValue,
Topic: topic,
Partition: partition,
Offset: wrapped.Offset,
})
}
} else {
decodedKey, err := this.config.KeyDecoder.Decode(message.Msg.Key)
if err != nil {
//TODO: what if we fail to decode the key: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
decodedValue, err := this.config.ValueDecoder.Decode(message.Msg.Value)
if err != nil {
//TODO: what if we fail to decode the value: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
messages = append(messages, &Message{
Key: message.Msg.Key,
Value: message.Msg.Value,
DecodedKey: decodedKey,
DecodedValue: decodedValue,
Topic: topic,
Partition: partition,
Offset: message.Offset,
})
}
}
return messages
}
// SiestaClient implements LowLevelClient and OffsetStorage and uses github.com/stealthly/siesta as underlying implementation.
type SiestaClient struct {
config *ConsumerConfig
connector siesta.Connector
}
// Creates a new SiestaClient using a given ConsumerConfig.
func NewSiestaClient(config *ConsumerConfig) *SiestaClient {
return &SiestaClient{
config: config,
}
}
// Returns a string representation of this SaramaClient.
func (this *SiestaClient) String() string {
return "Siesta client"
}
// This will be called right after connecting to ConsumerCoordinator so this client can initialize itself
// with bootstrap broker list for example. May return an error to signal this client is unable to work with given configuration.
func (this *SiestaClient) Initialize() error {
bootstrapBrokers, err := BootstrapBrokers(this.config.Coordinator)
if err != nil {
return err
}
connectorConfig := siesta.NewConnectorConfig()
connectorConfig.BrokerList = bootstrapBrokers
connectorConfig.ReadTimeout = this.config.SocketTimeout
connectorConfig.WriteTimeout = this.config.SocketTimeout
connectorConfig.ConnectTimeout = this.config.SocketTimeout
connectorConfig.FetchSize = this.config.FetchMessageMaxBytes
connectorConfig.ClientId = this.config.Clientid
this.connector, err = siesta.NewDefaultConnector(connectorConfig)
if err != nil {
return err
}
return nil
}
// This will be called each time the fetch request to Kafka should be issued. Topic, partition and offset are self-explanatory.
// Returns slice of Messages and an error if a fetch error occurred.
func (this *SiestaClient) Fetch(topic string, partition int32, offset int64) ([]*Message, error) {
Tracef(this, "Fetching %s %d from %d", topic, partition, offset)
response, err := this.connector.Fetch(topic, partition, offset)
if err != nil {
return nil, err
}
messages := make([]*Message, 0)
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
collector := func(topic string, partition int32, offset int64, key []byte, value []byte) {
decodedKey, err := this.config.KeyDecoder.Decode(key)
if err != nil {
//TODO: what if we fail to decode the key: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
decodedValue, err := this.config.ValueDecoder.Decode(value)
if err != nil {
//TODO: what if we fail to decode the value: fail-fast or fail-safe strategy?
Error(this, err.Error())
}
if this.config.Debug {
decodedKey = []int64{timestamp}
}
messages = append(messages, &Message{
Key: key,
Value: value,
DecodedKey: decodedKey,
DecodedValue: decodedValue,
Topic: topic,
Partition: partition,
Offset: offset,
})
}
return messages, response.CollectMessages(collector)
}
// Checks whether the given error indicates an OffsetOutOfRange error.
func (this *SiestaClient) IsOffsetOutOfRange(err error) bool {
return err == siesta.OffsetOutOfRange
}
// This will be called to handle OffsetOutOfRange error. OffsetTime will be either "smallest" or "largest".
func (this *SiestaClient) GetAvailableOffset(topic string, partition int32, offsetTime string) (int64, error) {
time := siesta.LatestTime
if offsetTime == "smallest" {
time = siesta.EarliestTime
}
return this.connector.GetAvailableOffset(topic, partition, time)
}
// Gets the offset for a given group, topic and partition.
// May return an error if fails to retrieve the offset.
func (this *SiestaClient) GetOffset(group string, topic string, partition int32) (int64, error) {
return this.connector.GetOffset(group, topic, partition)
}
// Commits the given offset for a given group, topic and partition.
// May return an error if fails to commit the offset.
func (this *SiestaClient) CommitOffset(group string, topic string, partition int32, offset int64) error {
return this.connector.CommitOffset(group, topic, partition, offset)
}
// Gracefully shuts down this client.
func (this *SiestaClient) Close() {
<-this.connector.Close()
}
// BootstrapBrokers queries the ConsumerCoordinator for all known brokers in the cluster to be used later as a bootstrap list for the LowLevelClient.
func BootstrapBrokers(coordinator ConsumerCoordinator) ([]string, error) {
bootstrapBrokers := make([]string, 0)
brokers, err := coordinator.GetAllBrokers()
if err != nil {
return nil, err
}
for _, broker := range brokers {
bootstrapBrokers = append(bootstrapBrokers, fmt.Sprintf("%s:%d", broker.Host, broker.Port))
}
return bootstrapBrokers, nil
}