-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathKafkaConsumer.swift
955 lines (868 loc) · 40.2 KB
/
KafkaConsumer.swift
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-kafka-client open source project
//
// Copyright (c) 2022 Apple Inc. and the swift-kafka-client project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-kafka-client project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import NIOConcurrencyHelpers
import NIOCore
import ServiceLifecycle
// MARK: - KafkaConsumerEventsDelegate
/// `NIOAsyncSequenceProducerDelegate` for ``KafkaConsumerEvents``.
internal struct KafkaConsumerEventsDelegate: Sendable {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
}
extension KafkaConsumerEventsDelegate: NIOAsyncSequenceProducerDelegate {
func produceMore() {
return // No back pressure
}
func didTerminate() {
return // We have to call poll for events anyway, nothing to do here
}
}
// MARK: - KafkaConsumerMessagesDelegate
/// `NIOAsyncSequenceProducerDelegate` for ``KafkaConsumerMessages``.
internal struct KafkaConsumerMessagesDelegate: Sendable {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
}
extension KafkaConsumerMessagesDelegate: NIOAsyncSequenceProducerDelegate {
func produceMore() {
self.stateMachine.withLockedValue { $0.produceMore() }
}
func didTerminate() {
self.stateMachine.withLockedValue { $0.finishMessageConsumption() }
}
}
// MARK: - KafkaConsumerEvents
/// `AsyncSequence` implementation for handling ``KafkaConsumerEvent``s emitted by Kafka.
public struct KafkaConsumerEvents: Sendable, AsyncSequence {
public typealias Element = KafkaConsumerEvent
typealias BackPressureStrategy = NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure
typealias WrappedSequence = NIOAsyncSequenceProducer<Element, BackPressureStrategy, KafkaConsumerEventsDelegate>
let wrappedSequence: WrappedSequence
/// `AsynceIteratorProtocol` implementation for handling ``KafkaConsumerEvent``s emitted by Kafka.
public struct AsyncIterator: AsyncIteratorProtocol {
var wrappedIterator: WrappedSequence.AsyncIterator
public mutating func next() async -> Element? {
await self.wrappedIterator.next()
}
}
public func makeAsyncIterator() -> AsyncIterator {
return AsyncIterator(wrappedIterator: self.wrappedSequence.makeAsyncIterator())
}
}
// MARK: - KafkaConsumerMessages
/// `AsyncSequence` implementation for handling messages received from the Kafka cluster (``KafkaConsumerMessage``).
public struct KafkaConsumerMessages: Sendable, AsyncSequence {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
public typealias Element = KafkaConsumerMessage
typealias BackPressureStrategy = NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark
typealias WrappedSequence = NIOThrowingAsyncSequenceProducer<
Result<KafkaConsumerMessage, Error>,
Error,
BackPressureStrategy,
KafkaConsumerMessagesDelegate
>
let wrappedSequence: WrappedSequence
/// `AsynceIteratorProtocol` implementation for handling messages received from the Kafka cluster (``KafkaConsumerMessage``).
public struct AsyncIterator: AsyncIteratorProtocol {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
var wrappedIterator: WrappedSequence.AsyncIterator?
public mutating func next() async throws -> Element? {
guard let result = try await self.wrappedIterator?.next() else {
self.deallocateIterator()
return nil
}
switch result {
case .success(let message):
let action = self.stateMachine.withLockedValue { $0.storeOffset() }
switch action {
case .storeOffset(let client):
do {
try client.storeMessageOffset(message)
} catch {
self.deallocateIterator()
throw error
}
return message
case .terminateConsumerSequence:
self.deallocateIterator()
return nil
}
case .failure(let error):
self.deallocateIterator()
throw error
}
}
private mutating func deallocateIterator() {
self.wrappedIterator = nil
}
}
public func makeAsyncIterator() -> AsyncIterator {
return AsyncIterator(
stateMachine: self.stateMachine,
wrappedIterator: self.wrappedSequence.makeAsyncIterator()
)
}
}
// MARK: - KafkaConsumer
/// A ``KafkaConsumer `` can be used to consume messages from a Kafka cluster.
public final class KafkaConsumer: Sendable, Service {
typealias Producer = NIOThrowingAsyncSequenceProducer<
Result<KafkaConsumerMessage, Error>,
Error,
NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark,
KafkaConsumerMessagesDelegate
>
/// The configuration object of the consumer client.
private let configuration: KafkaConsumerConfiguration
/// A logger.
private let logger: Logger
/// State of the `KafkaConsumer`.
private let stateMachine: NIOLockedValueBox<StateMachine>
/// An asynchronous sequence containing messages from the Kafka cluster.
public let messages: KafkaConsumerMessages
// Private initializer, use factory method or convenience init to create KafkaConsumer
/// Initialize a new ``KafkaConsumer``.
/// To listen to incoming messages, please subscribe to a list of topics using ``subscribe()``
/// or assign the consumer to a particular topic + partition pair using ``assign(topic:partition:offset:)``.
///
/// - Parameters:
/// - client: Client used for handling the connection to the Kafka cluster.
/// - stateMachine: The state machine containing the state of the ``KafkaConsumer``.
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Throws: A ``KafkaError`` if the initialization failed.
private init(
client: RDKafkaClient,
stateMachine: NIOLockedValueBox<StateMachine>,
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws {
self.configuration = configuration
self.stateMachine = stateMachine
self.logger = logger
let sourceAndSequence = NIOThrowingAsyncSequenceProducer.makeSequence(
elementType: Result<KafkaConsumerMessage, Error>.self,
backPressureStrategy: {
switch configuration.backPressureStrategy._internal {
case .watermark(let lowWatermark, let highWatermark):
return NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark(
lowWatermark: lowWatermark,
highWatermark: highWatermark
)
}
}(),
finishOnDeinit: true,
delegate: KafkaConsumerMessagesDelegate(stateMachine: self.stateMachine)
)
self.messages = KafkaConsumerMessages(
stateMachine: self.stateMachine,
wrappedSequence: sourceAndSequence.sequence
)
self.stateMachine.withLockedValue {
$0.initialize(
client: client,
source: sourceAndSequence.source
)
}
}
/// Initialize a new ``KafkaConsumer``.
///
/// This creates a consumer without that does not listen to any events other than consumer messages.
///
/// - Parameters:
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Returns: The newly created ``KafkaConsumer``.
/// - Throws: A ``KafkaError`` if the initialization failed.
public convenience init(
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws {
var subscribedEvents: [RDKafkaEvent] = [.log]
// Only listen to offset commit events when autoCommit is false
if configuration.isAutoCommitEnabled == false {
subscribedEvents.append(.offsetCommit)
}
if configuration.metrics.enabled {
subscribedEvents.append(.statistics)
}
let client = try RDKafkaClient.makeClient(
type: .consumer,
configDictionary: configuration.dictionary,
events: subscribedEvents,
logger: logger
)
let stateMachine = NIOLockedValueBox(StateMachine())
try self.init(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)
}
/// Initialize a new ``KafkaConsumer`` and a ``KafkaConsumerEvents`` asynchronous sequence.
///
/// Use the asynchronous sequence to consume events.
///
/// - Important: When the asynchronous sequence is deinited the producer will be shut down and disallowed from sending more messages.
/// Additionally, make sure to consume the asynchronous sequence otherwise the events will be buffered in memory indefinitely.
///
/// - Parameters:
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Returns: A tuple containing the created ``KafkaConsumer`` and the ``KafkaConsumerEvents``
/// `AsyncSequence` used for receiving message events.
/// - Throws: A ``KafkaError`` if the initialization failed.
public static func makeConsumerWithEvents(
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws -> (KafkaConsumer, KafkaConsumerEvents) {
var subscribedEvents: [RDKafkaEvent] = [.log]
// Only listen to offset commit events when autoCommit is false
if configuration.isAutoCommitEnabled == false {
subscribedEvents.append(.offsetCommit)
}
if configuration.metrics.enabled {
subscribedEvents.append(.statistics)
}
let client = try RDKafkaClient.makeClient(
type: .consumer,
configDictionary: configuration.dictionary,
events: subscribedEvents,
logger: logger
)
let stateMachine = NIOLockedValueBox(StateMachine())
let consumer = try KafkaConsumer(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)
// Note:
// It's crucial to initialize the `sourceAndSequence` variable AFTER `client`.
// This order is important to prevent the accidental triggering of `KafkaConsumerCloseOnTerminate.didTerminate()`.
// If this order is not met and `RDKafkaClient.makeClient()` fails,
// it leads to a call to `stateMachine.messageSequenceTerminated()` while it's still in the `.uninitialized` state.
let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaConsumerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
finishOnDeinit: true,
delegate: KafkaConsumerEventsDelegate(stateMachine: stateMachine)
)
let eventsSequence = KafkaConsumerEvents(wrappedSequence: sourceAndSequence.sequence)
return (consumer, eventsSequence)
}
/// Subscribe to the given list of `topics`.
/// The partition assignment happens automatically using `KafkaConsumer`'s consumer group.
/// - Parameter topics: An array of topic names to subscribe to.
/// - Throws: A ``KafkaError`` if subscribing to the topic list failed.
private func subscribe(topics: [String]) throws {
let action = self.stateMachine.withLockedValue { $0.setUpConnection() }
switch action {
case .setUpConnection(let client):
let subscription = RDKafkaTopicPartitionList()
for topic in topics {
subscription.add(
topic: topic,
partition: KafkaPartition.unassigned
)
}
try client.subscribe(topicPartitionList: subscription)
case .consumerClosed:
throw KafkaError.connectionClosed(reason: "Consumer deinitialized before setup")
}
}
/// Assign the``KafkaConsumer`` to a specific `partition` of a `topic`.
/// - Parameter topic: Name of the topic that this ``KafkaConsumer`` will read from.
/// - Parameter partition: Partition that this ``KafkaConsumer`` will read from.
/// - Parameter offset: The offset to start consuming from.
/// Defaults to the end of the Kafka partition queue (meaning wait for next produced message).
/// - Throws: A ``KafkaError`` if the consumer could not be assigned to the topic + partition pair.
private func assign(
topic: String,
partition: KafkaPartition,
offset: KafkaOffset
) throws {
let action = self.stateMachine.withLockedValue { $0.setUpConnection() }
switch action {
case .setUpConnection(let client):
let assignment = RDKafkaTopicPartitionList()
assignment.setOffset(topic: topic, partition: partition, offset: Int64(offset.rawValue))
try client.assign(topicPartitionList: assignment)
case .consumerClosed:
throw KafkaError.connectionClosed(reason: "Consumer deinitialized before setup")
}
}
/// Start the ``KafkaConsumer``.
///
/// - Important: This method **must** be called and will run until either the calling task is cancelled or gracefully shut down.
public func run() async throws {
try await withGracefulShutdownHandler {
try await self._run()
} onGracefulShutdown: {
self.triggerGracefulShutdown()
}
}
private func _run() async throws {
switch self.configuration.consumptionStrategy._internal {
case .partition(topic: let topic, partition: let partition, offset: let offset):
try self.assign(topic: topic, partition: partition, offset: offset)
case .group(groupID: _, topics: let topics):
try self.subscribe(topics: topics)
}
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await self.eventRunLoop()
}
group.addTask {
try await self.messageRunLoop()
}
// Throw when one of the two child task throws
try await group.next()
try await group.next()
}
}
/// Run loop polling Kafka for new events.
private func eventRunLoop() async throws {
while !Task.isCancelled {
let nextAction = self.stateMachine.withLockedValue { $0.nextEventPollLoopAction() }
switch nextAction {
case .pollForEvents(let client):
// Event poll to serve any events queued inside of `librdkafka`.
let events = client.eventPoll()
for event in events {
switch event {
case .statistics(let statistics):
self.configuration.metrics.update(with: statistics)
default:
break
}
}
try await Task.sleep(for: self.configuration.pollInterval)
case .terminatePollLoop:
return
}
}
}
/// Run loop polling Kafka for new consumer messages.
private func messageRunLoop() async throws {
while !Task.isCancelled {
let nextAction = self.stateMachine.withLockedValue { $0.nextConsumerPollLoopAction() }
switch nextAction {
case .pollForAndYieldMessages(let client, let source):
// Poll for new consumer messages.
let messageResults = self.batchConsumerPoll(client: client)
if messageResults.isEmpty {
self.stateMachine.withLockedValue { $0.waitForNewMessages() }
} else {
let yieldResult = source.yield(contentsOf: messageResults)
switch yieldResult {
case .produceMore:
break
case .stopProducing:
self.stateMachine.withLockedValue { $0.stopProducing() }
case .dropped:
return
}
}
case .pollForMessagesIfAvailable(let client, let source):
let messageResults = self.batchConsumerPoll(client: client)
if messageResults.isEmpty {
// Still no new messages, so sleep.
try await Task.sleep(for: self.configuration.pollInterval)
} else {
// New messages were produced to the partition that we previously finished reading.
let yieldResult = source.yield(contentsOf: messageResults)
switch yieldResult {
case .produceMore:
break
case .stopProducing:
self.stateMachine.withLockedValue { $0.stopProducing() }
case .dropped:
return
}
}
case .suspendPollLoop:
try await Task.sleep(for: self.configuration.pollInterval)
case .terminatePollLoop:
return
}
}
}
/// Read `maxMessages` consumer messages from Kafka.
///
/// - Parameters:
/// - client: Client used for handling the connection to the Kafka cluster.
/// - maxMessages: Maximum amount of consumer messages to read in this invocation.
private func batchConsumerPoll(
client: RDKafkaClient,
maxMessages: Int = 100
) -> [Result<KafkaConsumerMessage, Error>] {
var messageResults = [Result<KafkaConsumerMessage, Error>]()
messageResults.reserveCapacity(maxMessages)
for _ in 0..<maxMessages {
var result: Result<KafkaConsumerMessage, Error>?
do {
if let message = try client.consumerPoll() {
result = .success(message)
}
} catch {
result = .failure(error)
}
guard let result else {
return messageResults
}
messageResults.append(result)
}
return messageResults
}
/// Mark all messages up to the passed message in the topic as read.
/// Schedules a commit and returns immediately.
/// Any errors encountered after scheduling the commit will be discarded.
///
/// This method is only used for manual offset management.
///
/// - Warning: This method fails if the ``KafkaConsumerConfiguration/isAutoCommitEnabled`` configuration property is set to `true` (default).
///
/// - Parameters:
/// - message: Last received message that shall be marked as read.
/// - Throws: A ``KafkaError`` if committing failed.
public func scheduleCommit(_ message: KafkaConsumerMessage) throws {
try scheduleCommit(
topic: message.topic,
partition: message.partition,
offset: message.offset)
}
/// Mark all messages up to the passed message in the topic as read.
/// Schedules a commit and returns immediately.
/// Any errors encountered after scheduling the commit will be discarded.
///
/// This method is only used for manual offset management.
///
/// - Warning: This method fails if the ``KafkaConsumerConfiguration/isAutoCommitEnabled`` configuration property is set to `true` (default).
///
/// - Parameters:
/// - topic: Topic where the message that should be marked as read resides.
/// - partition: Partition where the message that should be marked as read resides.
/// - offset: Offset of the message that shall be marked as read.
/// - Throws: A ``KafkaError`` if committing failed.
public func scheduleCommit(topic: String,
partition: KafkaPartition,
offset: KafkaOffset) throws {
let action = self.stateMachine.withLockedValue { $0.commit() }
switch action {
case .throwClosedError:
throw KafkaError.connectionClosed(reason: "Tried to commit message offset on a closed consumer")
case .commit(let client):
guard self.configuration.isAutoCommitEnabled == false else {
throw KafkaError.config(reason: "Committing manually only works if isAutoCommitEnabled set to false")
}
try client.scheduleCommit(
topic: topic,
partition: partition,
offset: offset)
}
}
@available(*, deprecated, renamed: "commit")
public func commitSync(_ message: KafkaConsumerMessage) async throws {
try await self.commit(message)
}
/// Mark all messages up to the passed message in the topic as read.
/// Awaits until the commit succeeds or an error is encountered.
///
/// This method is only used for manual offset management.
///
/// - Warning: This method fails if the ``KafkaConsumerConfiguration/isAutoCommitEnabled`` configuration property is set to `true` (default).
///
/// - Parameters:
/// - message: Last received message that shall be marked as read.
/// - Throws: A ``KafkaError`` if committing failed.
public func commit(_ message: KafkaConsumerMessage) async throws {
try await commit(topic: message.topic,
partition: message.partition,
offset: message.offset)
}
/// Mark all messages up to the passed message in the topic as read.
/// Awaits until the commit succeeds or an error is encountered.
///
/// This method is only used for manual offset management.
///
/// - Warning: This method fails if the ``KafkaConsumerConfiguration/isAutoCommitEnabled`` configuration property is set to `true` (default).
///
/// - Parameters:
/// - topic: Topic where the message that should be marked as read resides.
/// - partition: Partition where the message that should be marked as read resides.
/// - offset: Offset of the message that shall be marked as read.
/// - Throws: A ``KafkaError`` if committing failed.
public func commit(topic: String,
partition: KafkaPartition,
offset: KafkaOffset) async throws {
let action = self.stateMachine.withLockedValue { $0.commit() }
switch action {
case .throwClosedError:
throw KafkaError.connectionClosed(reason: "Tried to commit message offset on a closed consumer")
case .commit(let client):
guard self.configuration.isAutoCommitEnabled == false else {
throw KafkaError.config(reason: "Committing manually only works if isAutoCommitEnabled set to false")
}
try await client.commit(topic: topic,
partition: partition,
offset: offset)
}
}
/// This function is used to gracefully shut down a Kafka consumer client.
///
/// - Note: Invoking this function is not always needed as the ``KafkaConsumer``
/// will already shut down when consumption of the ``KafkaConsumerMessages`` has ended.
public func triggerGracefulShutdown() {
let action = self.stateMachine.withLockedValue { $0.finish() }
switch action {
case .triggerGracefulShutdown(let client):
self._triggerGracefulShutdown(
client: client,
logger: self.logger
)
case .none:
return
}
}
private func _triggerGracefulShutdown(
client: RDKafkaClient,
logger: Logger
) {
do {
try client.consumerClose()
} catch {
if let error = error as? KafkaError {
logger.error("Closing KafkaConsumer failed: \(error.description)")
} else {
logger.error("Caught unknown error: \(error)")
}
}
}
}
// MARK: - KafkaConsumer + StateMachine
extension KafkaConsumer {
/// State machine representing the state of the ``KafkaConsumer``.
struct StateMachine: Sendable {
/// State of the event loop fetching new consumer messages.
enum MessagePollLoopState {
/// The sequence can take more messages.
///
/// - Parameter source: The source for yielding new messages.
case running(source: Producer.Source)
/// Sequence suspended due to back pressure.
///
/// - Parameter source: The source for yielding new messages.
case suspended(source: Producer.Source)
/// We have read to the end of a partition and are now waiting for new messages
/// to be produced.
///
/// - Parameter source: The source for yielding new messages.
case waitingForMessages(source: Producer.Source)
/// The sequence has finished, and no more messages will be produced.
case finished
}
/// The state of the ``StateMachine``.
enum State: Sendable {
/// The state machine has been initialized with init() but is not yet Initialized
/// using `func initialize()` (required).
case uninitialized
/// We are in the process of initializing the ``KafkaConsumer``,
/// though ``subscribe()`` / ``assign()`` have not been invoked.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: The source for yielding new messages.
case initializing(
client: RDKafkaClient,
source: Producer.Source
)
/// The ``KafkaConsumer`` is consuming messages.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter state: State of the event loop fetching new consumer messages.
case running(client: RDKafkaClient, messagePollLoopState: MessagePollLoopState)
/// The ``KafkaConsumer/triggerGracefulShutdown()`` has been invoked.
/// We are now in the process of commiting our last state to the broker.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case finishing(client: RDKafkaClient)
/// The ``KafkaConsumer`` is closed.
case finished
}
/// The current state of the StateMachine.
var state: State = .uninitialized
/// Delayed initialization of `StateMachine` as the `source` and the `pollClosure` are
/// not yet available when the normal initialization occurs.
mutating func initialize(
client: RDKafkaClient,
source: Producer.Source
) {
guard case .uninitialized = self.state else {
fatalError("\(#function) can only be invoked in state .uninitialized, but was invoked in state \(self.state)")
}
self.state = .initializing(
client: client,
source: source
)
}
/// Action to be taken when wanting to poll for a new message.
enum EventPollLoopAction {
/// Serve any queued callbacks on the event queue.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case pollForEvents(client: RDKafkaClient)
/// Terminate the poll loop.
case terminatePollLoop
}
/// Returns the next action to be taken when wanting to poll.
/// - Returns: The next action to be taken when wanting to poll, or `nil` if there is no action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
mutating func nextEventPollLoopAction() -> EventPollLoopAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .running(let client, _):
return .pollForEvents(client: client)
case .finishing(let client):
if client.isConsumerClosed {
self.state = .finished
return .terminatePollLoop
} else {
return .pollForEvents(client: client)
}
case .finished:
return .terminatePollLoop
}
}
/// Action to be taken when wanting to poll for a new message.
enum ConsumerPollLoopAction {
/// Poll for a new ``KafkaConsumerMessage``.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case pollForAndYieldMessages(
client: RDKafkaClient,
source: Producer.Source
)
/// Poll for a new ``KafkaConsumerMessage`` or sleep for ``KafkaConsumerConfiguration/pollInterval``
/// if there are no new messages to read from the partition.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case pollForMessagesIfAvailable(
client: RDKafkaClient,
source: Producer.Source
)
/// Sleep for ``KafkaConsumerConfiguration/pollInterval``.
case suspendPollLoop
/// Terminate the poll loop.
case terminatePollLoop
}
/// Returns the next action to be taken when wanting to poll.
/// - Returns: The next action to be taken when wanting to poll, or `nil` if there is no action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
mutating func nextConsumerPollLoopAction() -> ConsumerPollLoopAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .running(let client, let consumerState):
switch consumerState {
case .running(let source):
return .pollForAndYieldMessages(client: client, source: source)
case .suspended(source: _):
return .suspendPollLoop
case .waitingForMessages(let source):
return .pollForMessagesIfAvailable(client: client, source: source)
case .finished:
return .terminatePollLoop
}
case .finishing, .finished:
return .terminatePollLoop
}
}
/// Action to be taken when wanting to set up the connection through ``subscribe()`` or ``assign()``.
enum SetUpConnectionAction {
/// Set up the connection through ``subscribe()`` or ``assign()``.
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case setUpConnection(client: RDKafkaClient)
/// The ``KafkaConsumer`` is closed.
case consumerClosed
}
/// Get action to be taken when wanting to set up the connection through ``subscribe()`` or ``assign()``.
///
/// - Returns: The action to be taken.
mutating func setUpConnection() -> SetUpConnectionAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing(let client, let source):
self.state = .running(client: client, messagePollLoopState: .running(source: source))
return .setUpConnection(client: client)
case .running:
fatalError("\(#function) should not be invoked more than once")
case .finishing:
fatalError("\(#function) should only be invoked when KafkaConsumer is running")
case .finished:
return .consumerClosed
}
}
/// Action to take when wanting to store a message offset (to be auto-committed by `librdkafka`).
enum StoreOffsetAction {
/// Store the message offset with the given `client`.
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case storeOffset(client: RDKafkaClient)
/// The consumer is in the process of `.finishing` or even `.finished`.
/// Stop yielding new elements and terminate the asynchronous sequence.
case terminateConsumerSequence
}
/// Get action to take when wanting to store a message offset (to be auto-committed by `librdkafka`).
func storeOffset() -> StoreOffsetAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .running(let client, _):
return .storeOffset(client: client)
case .finishing, .finished:
return .terminateConsumerSequence
}
}
/// Action to be taken when wanting to do a commit.
enum CommitAction {
/// Do a commit.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case commit(client: RDKafkaClient)
/// Throw an error. The ``KafkaConsumer`` is closed.
case throwClosedError
}
/// Get action to be taken when wanting to do a commit.
/// - Returns: The action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
func commit() -> CommitAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .running(let client, _):
return .commit(client: client)
case .finishing, .finished:
return .throwClosedError
}
}
/// Action to be taken when wanting to do close the consumer.
enum FinishAction {
/// Shut down the ``KafkaConsumer``.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case triggerGracefulShutdown(client: RDKafkaClient)
}
/// Get action to be taken when wanting to do close the consumer.
/// - Returns: The action to be taken, or `nil` if there is no action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
mutating func finish() -> FinishAction? {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .running(let client, _):
self.state = .finishing(client: client)
return .triggerGracefulShutdown(client: client)
case .finishing, .finished:
return nil
}
}
// MARK: - Consumer Messages Poll Loop Actions
/// The partition that was previously finished reading has got new messages produced to it.
mutating func newMessagesProduced() {
guard case .running(let client, let consumerState) = self.state else {
fatalError("\(#function) invoked while still in state \(self.state)")
}
switch consumerState {
case .running, .suspended, .finished:
fatalError("\(#function) should not be invoked in state \(self.state)")
case .waitingForMessages(let source):
self.state = .running(client: client, messagePollLoopState: .running(source: source))
}
}
/// The consumer has read to the end of a partition and shall now go into a sleep loop until new messages are produced.
mutating func waitForNewMessages() {
guard case .running(let client, let consumerState) = self.state else {
fatalError("\(#function) invoked while still in state \(self.state)")
}
switch consumerState {
case .running(let source):
self.state = .running(client: client, messagePollLoopState: .waitingForMessages(source: source))
case .suspended, .waitingForMessages, .finished:
fatalError("\(#function) should not be invoked in state \(self.state)")
}
}
/// ``KafkaConsumerMessages``'s back pressure mechanism asked us to produce more messages.
mutating func produceMore() {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
break // This case can be triggered by the KafkaConsumerMessagesDeletgate
case .running(let client, let consumerState):
switch consumerState {
case .running, .waitingForMessages, .finished:
break
case .suspended(let source):
self.state = .running(client: client, messagePollLoopState: .running(source: source))
}
case .finishing, .finished:
break
}
}
/// ``KafkaConsumerMessages``'s back pressure mechanism asked us to temporarily stop producing messages.
mutating func stopProducing() {
guard case .running(let client, let consumerState) = self.state else {
fatalError("\(#function) invoked while still in state \(self.state)")
}
switch consumerState {
case .suspended, .finished:
break
case .running(let source):
self.state = .running(client: client, messagePollLoopState: .suspended(source: source))
case .waitingForMessages(let source):
self.state = .running(client: client, messagePollLoopState: .suspended(source: source))
}
}
/// The ``KafkaConsumerMessages`` asynchronous sequence was terminated.
mutating func finishMessageConsumption() {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
self.state = .finished
case .running(let client, _):
self.state = .running(client: client, messagePollLoopState: .finished)
case .finishing, .finished:
break
}
}
}
}