-
Notifications
You must be signed in to change notification settings - Fork 74
/
fetcher.go
434 lines (391 loc) · 13.9 KB
/
fetcher.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* 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"
"math"
"sync"
"time"
)
type consumerFetcherManager struct {
config *ConsumerConfig
numStreams int
closeFinished chan bool
updateLock sync.RWMutex
partitionMap map[TopicAndPartition]*partitionTopicInfo
fetcherRoutineMap map[int]*consumerFetcherRoutine
shuttingDown bool
updateInProgress bool
updatedCond *sync.Cond
disconnectChannelsForPartition chan TopicAndPartition
metrics *ConsumerMetrics
client LowLevelClient
}
func (m *consumerFetcherManager) String() string {
return fmt.Sprintf("%s-manager", m.config.Consumerid)
}
func newConsumerFetcherManager(config *ConsumerConfig, disconnectChannelsForPartition chan TopicAndPartition, metrics *ConsumerMetrics) *consumerFetcherManager {
manager := &consumerFetcherManager{
config: config,
closeFinished: make(chan bool),
partitionMap: make(map[TopicAndPartition]*partitionTopicInfo),
fetcherRoutineMap: make(map[int]*consumerFetcherRoutine),
disconnectChannelsForPartition: disconnectChannelsForPartition,
client: config.LowLevelClient,
metrics: metrics,
}
manager.updatedCond = sync.NewCond(manager.updateLock.RLocker())
return manager
}
func (m *consumerFetcherManager) startConnections(topicInfos []*partitionTopicInfo, numStreams int) {
if Logger.IsAllowed(DebugLevel) {
Debug(m, "Fetcher Manager started")
Debugf(m, "TopicInfos = %s", topicInfos)
}
m.numStreams = numStreams
partitionInfos := make(map[TopicAndPartition]*partitionTopicInfo)
m.updateInProgress = true
inWriteLock(&m.updateLock, func() {
if Logger.IsAllowed(DebugLevel) {
Debug(m, "Updating fetcher configuration")
}
newPartitionMap := make(map[TopicAndPartition]*partitionTopicInfo)
for _, providedInfo := range topicInfos {
topicAndPartition := TopicAndPartition{providedInfo.Topic, providedInfo.Partition}
if currentInfo, alreadyFetching := m.partitionMap[topicAndPartition]; alreadyFetching {
newPartitionMap[topicAndPartition] = currentInfo
continue
} else {
newPartitionMap[topicAndPartition] = providedInfo
partitionInfos[topicAndPartition] = providedInfo
}
}
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "Got new list of partitions to process %v", newPartitionMap)
Debugf(m, "All partitions map: %v", m.partitionMap)
}
//receive obsolete partitions map
for k := range newPartitionMap {
delete(m.partitionMap, k)
}
//removing obsolete partitions and tearing down associated jobs
topicPartitionsToRemove := make([]TopicAndPartition, 0)
for tp := range m.partitionMap {
topicPartitionsToRemove = append(topicPartitionsToRemove, tp)
}
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "There are obsolete partitions %v", topicPartitionsToRemove)
}
//removing unnecessary partition-fetchRoutine bindings
for _, fetcher := range m.fetcherRoutineMap {
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "Fetcher %s parition map before obsolete partitions removal", fetcher, fetcher.partitionMap)
}
fetcher.removePartitions(topicPartitionsToRemove)
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "Fetcher %s parition map after obsolete partitions removal", fetcher, fetcher.partitionMap)
}
}
for tp := range m.partitionMap {
m.disconnectChannelsForPartition <- tp
delete(m.partitionMap, tp)
}
m.shutdownIdleFetchers()
//updating partitions map with requested partitions
for k, v := range newPartitionMap {
m.partitionMap[k] = v
}
m.addFetcherForPartitions(partitionInfos)
m.updateInProgress = false
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "Applied new partition map %v", m.partitionMap)
}
})
if Logger.IsAllowed(DebugLevel) {
Debug(m, "Notifying all waiters about completed update")
}
m.updatedCond.Broadcast()
}
func (m *consumerFetcherManager) addFetcherForPartitions(partitionInfos map[TopicAndPartition]*partitionTopicInfo) {
if Logger.IsAllowed(InfoLevel) {
Infof(m, "Adding fetcher for partitions %v", partitionInfos)
}
partitionsPerFetcher := make(map[int]map[TopicAndPartition]*partitionTopicInfo)
for topicAndPartition, info := range partitionInfos {
fetcherId := m.getFetcherId(topicAndPartition.Topic, topicAndPartition.Partition)
if partitionsPerFetcher[fetcherId] == nil {
partitionsPerFetcher[fetcherId] = make(map[TopicAndPartition]*partitionTopicInfo)
}
partitionsPerFetcher[fetcherId][topicAndPartition] = info
}
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "partitionsPerFetcher: %v", partitionsPerFetcher)
}
for fetcherId, partitionInfos := range partitionsPerFetcher {
if m.fetcherRoutineMap[fetcherId] == nil {
if Logger.IsAllowed(DebugLevel) {
Debugf(m, "Starting new fetcher")
}
fetcherRoutine := newConsumerFetcher(m,
fmt.Sprintf("ConsumerFetcherRoutine-%s-%d", m.config.Consumerid, fetcherId))
m.fetcherRoutineMap[fetcherId] = fetcherRoutine
go fetcherRoutine.start()
}
m.fetcherRoutineMap[fetcherId].addPartitions(partitionInfos)
}
}
func (m *consumerFetcherManager) getFetcherId(topic string, partitionId int32) int {
return int(math.Abs(float64(31*hash(topic)+partitionId))) % int(m.numStreams)
}
func (m *consumerFetcherManager) shutdownIdleFetchers() {
if Logger.IsAllowed(DebugLevel) {
Debug(m, "Shutting down idle fetchers")
}
for key, fetcher := range m.fetcherRoutineMap {
if len(fetcher.partitionMap) <= 0 {
Debugf(m, "There is idle fetcher: %s", fetcher)
<-fetcher.close()
delete(m.fetcherRoutineMap, key)
}
}
if Logger.IsAllowed(DebugLevel) {
Debug(m, "Closed idle fetchers")
}
}
func (m *consumerFetcherManager) closeAllFetchers() {
Info(m, "Closing fetchers")
inWriteLock(&m.updateLock, func() {
Debug(m, "Stopping all message buffers and removing paritions")
for k, v := range m.partitionMap {
v.Buffer.stop()
delete(m.partitionMap, k)
}
Debugf(m, "Trying to close %d fetchers", len(m.fetcherRoutineMap))
for key, fetcher := range m.fetcherRoutineMap {
Debugf(m, "Closing %s", fetcher)
<-fetcher.close()
Debugf(m, "Closed %s", fetcher)
delete(m.fetcherRoutineMap, key)
}
})
}
func (m *consumerFetcherManager) close() <-chan bool {
Info(m, "Closing manager")
go func() {
Info(m, "Setting shutdown flag")
m.shuttingDown = true
m.closeAllFetchers()
m.updatedCond.Broadcast()
m.partitionMap = nil
m.closeFinished <- true
Info(m, "Successfully closed all fetcher manager routines")
}()
return m.closeFinished
}
type consumerFetcherRoutine struct {
manager *consumerFetcherManager
name string
partitionMap map[TopicAndPartition]*partitionTopicInfo
lock sync.RWMutex
closeFinished chan bool
fetchStopper chan bool
askNext chan TopicAndPartition
}
func (f *consumerFetcherRoutine) String() string {
return f.name
}
func newConsumerFetcher(m *consumerFetcherManager, name string) *consumerFetcherRoutine {
return &consumerFetcherRoutine{
manager: m,
name: name,
partitionMap: make(map[TopicAndPartition]*partitionTopicInfo),
closeFinished: make(chan bool),
fetchStopper: make(chan bool),
askNext: make(chan TopicAndPartition, m.config.AskNextChannelSize),
}
}
func (f *consumerFetcherRoutine) start() {
if Logger.IsAllowed(InfoLevel) {
Info(f, "Fetcher started")
}
for {
if Logger.IsAllowed(TraceLevel) {
Trace(f, "Waiting for asknext or die")
}
ts := time.Now()
select {
case nextTopicPartition := <-f.askNext:
{
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
f.manager.metrics.fetchersIdle().Update(time.Since(ts))
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Received asknext for %s", &nextTopicPartition)
}
inReadLock(&f.lock, func() {
if !f.manager.shuttingDown {
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Partition map: %v", f.partitionMap)
}
if _, exists := f.partitionMap[nextTopicPartition]; !exists {
if Logger.IsAllowed(WarnLevel) {
Warnf(f, "Message buffer for partition %s has been terminated. Aborting processing task...", nextTopicPartition)
}
return
}
offset := f.partitionMap[nextTopicPartition].FetchedOffset
var messages []*Message
var err error
f.manager.metrics.fetchDuration().Time(func() {
messages, err = f.manager.client.Fetch(nextTopicPartition.Topic, nextTopicPartition.Partition, offset)
})
f.manager.metrics.numFetchedMessages().Inc(int64(len(messages)))
if err != nil {
switch f.manager.client.GetErrorType(err) {
case ErrorTypeOffsetOutOfRange:
{
Warnf(f, "Current offset %d for topic %s and partition %s is out of range.", offset, nextTopicPartition.Topic, nextTopicPartition.Partition)
f.handleOffsetOutOfRange(&nextTopicPartition)
}
case ErrorTypeCorruptedResponse:
{
Warnf(f, "Got a corrupted fetch response: %s", err)
f.partitionMap[nextTopicPartition].FetchedOffset = f.partitionMap[nextTopicPartition].FetchedOffset + 1
}
case ErrorTypeOther:
{
Warnf(f, "Got a fetch error for topic %s, partition %d: %s", nextTopicPartition.Topic, nextTopicPartition.Partition, err)
//TODO new backoff type?
time.Sleep(1 * time.Second)
}
}
}
if f.manager.config.Debug {
for _, message := range messages {
message.DecodedKey = append([]int64{timestamp}, message.DecodedKey.([]int64)...)
}
}
f.processPartitionData(nextTopicPartition, messages)
}
})
}
case <-f.fetchStopper:
{
if Logger.IsAllowed(InfoLevel) {
Info(f, "Stopped fetcher")
}
return
}
}
}
}
func (f *consumerFetcherRoutine) addPartitions(partitionTopicInfos map[TopicAndPartition]*partitionTopicInfo) {
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Adding partitions: %v", partitionTopicInfos)
}
newPartitions := make(map[TopicAndPartition]chan TopicAndPartition)
inWriteLock(&f.lock, func() {
for topicAndPartition, info := range partitionTopicInfos {
if _, contains := f.partitionMap[topicAndPartition]; !contains {
f.partitionMap[topicAndPartition] = info
validOffset := info.FetchedOffset + 1
if isOffsetInvalid(info.FetchedOffset) {
f.handleOffsetOutOfRange(&topicAndPartition)
} else {
f.partitionMap[topicAndPartition].FetchedOffset = validOffset
}
f.partitionMap[topicAndPartition].Buffer.start(f.askNext)
newPartitions[topicAndPartition] = f.askNext
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Owner of %s", topicAndPartition)
}
}
}
})
for topicAndPartition, askNext := range newPartitions {
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Sending ask next to %s for %s", f, topicAndPartition)
}
Loop:
for {
timeout := time.NewTimer(1 * time.Second)
select {
case askNext <- topicAndPartition:
timeout.Stop()
break Loop
case <-timeout.C:
{
if f.manager.shuttingDown {
return
}
}
}
}
if Logger.IsAllowed(DebugLevel) {
Debugf(f, "Sent ask next to %s for %s", f, topicAndPartition)
}
}
}
func (f *consumerFetcherRoutine) processPartitionData(topicAndPartition TopicAndPartition, messages []*Message) {
if Logger.IsAllowed(TraceLevel) {
Trace(f, "Trying to acquire lock for partition processing")
Tracef(f, "Processing partition data for %s", topicAndPartition)
}
if len(messages) > 0 {
f.partitionMap[topicAndPartition].FetchedOffset = messages[len(messages)-1].Offset + 1
}
go f.partitionMap[topicAndPartition].Buffer.addBatch(messages)
if Logger.IsAllowed(TraceLevel) {
Tracef(f, "Sent partition data to %s", topicAndPartition)
}
}
func (f *consumerFetcherRoutine) handleOffsetOutOfRange(topicAndPartition *TopicAndPartition) {
newOffset, err := f.manager.client.GetAvailableOffset(topicAndPartition.Topic, topicAndPartition.Partition, f.manager.config.AutoOffsetReset)
if err != nil {
Errorf(f, "Cannot get available offset for %s. Reason: %s", topicAndPartition, err)
return
}
// Do not use a lock here just because it's faster and it will be checked afterwards if we should still fetch that TopicPartition
// This just guarantees we dont get a nil pointer dereference here
if topicInfo, exists := f.partitionMap[*topicAndPartition]; exists {
topicInfo.FetchedOffset = newOffset
f.partitionMap[*topicAndPartition].FetchedOffset = newOffset
}
}
func (f *consumerFetcherRoutine) removeAllPartitions() {
partitions := make([]TopicAndPartition, 0)
for topicPartition, _ := range f.partitionMap {
partitions = append(partitions, topicPartition)
}
f.removePartitions(partitions)
}
func (f *consumerFetcherRoutine) removePartitions(partitions []TopicAndPartition) {
if Logger.IsAllowed(DebugLevel) {
Debug(f, "Remove partitions")
}
inWriteLock(&f.lock, func() {
for _, topicAndPartition := range partitions {
delete(f.partitionMap, topicAndPartition)
}
})
}
func (f *consumerFetcherRoutine) close() <-chan bool {
Info(f, "Closing fetcher")
go func() {
f.fetchStopper <- true
f.removeAllPartitions()
Debug(f, "Sending close finished")
f.closeFinished <- true
Debug(f, "Sent close finished")
}()
return f.closeFinished
}