karafka-rdkafka 0.31.0-aarch64-linux-gnu → 0.32.1-aarch64-linux-gnu
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +100 -35
- data/README.md +14 -6
- data/ext/librdkafka.so +0 -0
- data/karafka-rdkafka.gemspec +4 -3
- data/lib/rdkafka/abstract_handle.rb +26 -11
- data/lib/rdkafka/admin/acl_binding_result.rb +4 -16
- data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +2 -2
- data/lib/rdkafka/admin/config_binding_result.rb +8 -0
- data/lib/rdkafka/admin/create_acl_report.rb +1 -3
- data/lib/rdkafka/admin/create_topic_report.rb +2 -6
- data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +2 -2
- data/lib/rdkafka/admin/delete_groups_report.rb +2 -6
- data/lib/rdkafka/admin/delete_topic_report.rb +2 -6
- data/lib/rdkafka/admin/describe_cluster_report.rb +3 -0
- data/lib/rdkafka/admin/list_consumer_group_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/list_consumer_group_offsets_report.rb +52 -0
- data/lib/rdkafka/admin.rb +180 -10
- data/lib/rdkafka/bindings/admin/configs.rb +12 -1
- data/lib/rdkafka/bindings/admin.rb +14 -0
- data/lib/rdkafka/bindings/callbacks.rb +16 -9
- data/lib/rdkafka/bindings/configuration.rb +3 -3
- data/lib/rdkafka/bindings/consumer.rb +44 -2
- data/lib/rdkafka/bindings/errors.rb +1 -1
- data/lib/rdkafka/bindings/handle.rb +1 -1
- data/lib/rdkafka/bindings.rb +3 -0
- data/lib/rdkafka/callbacks/list_consumer_group_offsets_handler.rb +62 -0
- data/lib/rdkafka/callbacks.rb +5 -6
- data/lib/rdkafka/config.rb +70 -0
- data/lib/rdkafka/consumer/allocator.rb +0 -2
- data/lib/rdkafka/consumer/topic_partition_list.rb +7 -7
- data/lib/rdkafka/consumer.rb +133 -52
- data/lib/rdkafka/defaults.rb +15 -6
- data/lib/rdkafka/helpers/message_build_error.rb +30 -0
- data/lib/rdkafka/native_kafka.rb +136 -41
- data/lib/rdkafka/producer/partition_count_cache.rb +258 -0
- data/lib/rdkafka/producer.rb +196 -81
- data/lib/rdkafka/share_consumer.rb +75 -38
- data/lib/rdkafka/version.rb +1 -1
- data/lib/rdkafka.rb +6 -2
- data/package-lock.json +3 -3
- data/renovate.json +32 -2
- metadata +7 -31
- data/lib/rdkafka/producer/partitions_count_cache.rb +0 -212
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
module Callbacks
|
|
5
|
+
# Handles `RD_KAFKA_EVENT_LISTCONSUMERGROUPOFFSETS_RESULT` events
|
|
6
|
+
# @private
|
|
7
|
+
class ListConsumerGroupOffsetsHandler < BaseHandler
|
|
8
|
+
class << self
|
|
9
|
+
# Resolves the list consumer group offsets handle from its result event
|
|
10
|
+
# @param event_ptr [FFI::Pointer] pointer to the event
|
|
11
|
+
# @return [void]
|
|
12
|
+
def call(event_ptr)
|
|
13
|
+
result = Rdkafka::Bindings.rd_kafka_event_ListConsumerGroupOffsets_result(event_ptr)
|
|
14
|
+
|
|
15
|
+
pointer_to_size_t = FFI::MemoryPointer.new(:size_t)
|
|
16
|
+
group_result_array = Rdkafka::Bindings.rd_kafka_ListConsumerGroupOffsets_result_groups(result, pointer_to_size_t)
|
|
17
|
+
group_results = GroupResult.create_group_results_from_array(pointer_to_size_t.read_int, group_result_array)
|
|
18
|
+
handle_ptr = Rdkafka::Bindings.rd_kafka_event_opaque(event_ptr)
|
|
19
|
+
|
|
20
|
+
if (handle = Rdkafka::Admin::ListConsumerGroupOffsetsHandle.remove(handle_ptr.address))
|
|
21
|
+
unless resolve_operation_error(event_ptr, handle)
|
|
22
|
+
group_result = group_results[0]
|
|
23
|
+
|
|
24
|
+
# librdkafka only supports one group per invocation
|
|
25
|
+
if group_result.nil?
|
|
26
|
+
handle[:response] = Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
27
|
+
handle.result = Rdkafka::Admin::ListConsumerGroupOffsetsReport.new(
|
|
28
|
+
FFI::Pointer::NULL,
|
|
29
|
+
FFI::Pointer::NULL
|
|
30
|
+
)
|
|
31
|
+
else
|
|
32
|
+
report = Rdkafka::Admin::ListConsumerGroupOffsetsReport.new(
|
|
33
|
+
group_result.error_string,
|
|
34
|
+
group_result.result_name,
|
|
35
|
+
Rdkafka::Bindings.rd_kafka_group_result_partitions(group_result_array.read_pointer)
|
|
36
|
+
)
|
|
37
|
+
handle.result = report
|
|
38
|
+
|
|
39
|
+
# The group level error can be clean while a partition failed (e.g.
|
|
40
|
+
# `unstable_offset_commit` with `require_stable_offsets`), which must not look
|
|
41
|
+
# like a committed offset of nil.
|
|
42
|
+
failed = report.partitions.find { |partition| partition[:error] }
|
|
43
|
+
|
|
44
|
+
if !group_result.result_error.zero?
|
|
45
|
+
handle[:response] = group_result.result_error
|
|
46
|
+
handle.broker_message = report.error_string
|
|
47
|
+
elsif failed
|
|
48
|
+
handle[:response] = failed[:error].rdkafka_response
|
|
49
|
+
handle.broker_message = "Failed for partition #{failed[:partition]} of '#{failed[:topic]}'"
|
|
50
|
+
else
|
|
51
|
+
handle[:response] = Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
handle.unlock
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/rdkafka/callbacks.rb
CHANGED
|
@@ -216,6 +216,7 @@ module Rdkafka
|
|
|
216
216
|
when Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATEPARTITIONS_RESULT then CreatePartitionsHandler
|
|
217
217
|
when Rdkafka::Bindings::RD_KAFKA_EVENT_DELETEGROUPS_RESULT then DeleteGroupsHandler
|
|
218
218
|
when Rdkafka::Bindings::RD_KAFKA_EVENT_ALTERCONSUMERGROUPOFFSETS_RESULT then AlterConsumerGroupOffsetsHandler
|
|
219
|
+
when Rdkafka::Bindings::RD_KAFKA_EVENT_LISTCONSUMERGROUPOFFSETS_RESULT then ListConsumerGroupOffsetsHandler
|
|
219
220
|
when Rdkafka::Bindings::RD_KAFKA_EVENT_DELETECONSUMERGROUPOFFSETS_RESULT then DeleteConsumerGroupOffsetsHandler
|
|
220
221
|
when Rdkafka::Bindings::RD_KAFKA_EVENT_DELETERECORDS_RESULT then DeleteRecordsHandler
|
|
221
222
|
when Rdkafka::Bindings::RD_KAFKA_EVENT_CREATEACLS_RESULT then CreateAclHandler
|
|
@@ -250,8 +251,6 @@ module Rdkafka
|
|
|
250
251
|
message = Rdkafka::Bindings::Message.new(message_ptr)
|
|
251
252
|
delivery_handle_ptr_address = message[:_private].address
|
|
252
253
|
if delivery_handle = Rdkafka::Producer::DeliveryHandle.remove(delivery_handle_ptr_address)
|
|
253
|
-
topic_name = Rdkafka::Bindings.rd_kafka_topic_name(message[:rkt])
|
|
254
|
-
|
|
255
254
|
# Update delivery handle
|
|
256
255
|
delivery_handle[:response] = message[:err]
|
|
257
256
|
delivery_handle[:partition] = message[:partition]
|
|
@@ -259,9 +258,9 @@ module Rdkafka
|
|
|
259
258
|
delivery_handle[:status] = Rdkafka::Bindings.rd_kafka_message_status(message_ptr)
|
|
260
259
|
delivery_handle[:latency] = Rdkafka::Bindings.rd_kafka_message_latency(message_ptr)
|
|
261
260
|
delivery_handle[:broker_id] = Rdkafka::Bindings.rd_kafka_message_broker_id(message_ptr)
|
|
262
|
-
# The topic is
|
|
263
|
-
#
|
|
264
|
-
#
|
|
261
|
+
# The topic is taken from the handle's `topic` Ruby attribute set during produce, not
|
|
262
|
+
# read back from librdkafka, which spares an FFI call and a native string copy for
|
|
263
|
+
# every message.
|
|
265
264
|
|
|
266
265
|
begin
|
|
267
266
|
# Call delivery callback on opaque
|
|
@@ -270,7 +269,7 @@ module Rdkafka
|
|
|
270
269
|
Rdkafka::Producer::DeliveryReport.new(
|
|
271
270
|
message[:partition],
|
|
272
271
|
message[:offset],
|
|
273
|
-
|
|
272
|
+
delivery_handle.topic,
|
|
274
273
|
message[:err],
|
|
275
274
|
delivery_handle.label,
|
|
276
275
|
status: delivery_handle[:status],
|
data/lib/rdkafka/config.rb
CHANGED
|
@@ -156,6 +156,25 @@ module Rdkafka
|
|
|
156
156
|
)
|
|
157
157
|
end
|
|
158
158
|
|
|
159
|
+
# Returns the time-to-live of the process-wide partitions count cache used by
|
|
160
|
+
# `produce(partition_key:)`.
|
|
161
|
+
#
|
|
162
|
+
# @return [Integer] time-to-live in milliseconds
|
|
163
|
+
def self.partition_count_cache_ttl_ms
|
|
164
|
+
Rdkafka::Producer.partition_count_cache.ttl_ms
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Sets the time-to-live of the process-wide partitions count cache shared by all the producers.
|
|
168
|
+
# Cached counts are kept, so it can be changed at any time.
|
|
169
|
+
#
|
|
170
|
+
# @param value [Integer] time-to-live in milliseconds
|
|
171
|
+
# (defaults to {Defaults::PARTITION_COUNT_CACHE_TTL_MS})
|
|
172
|
+
# @return [void]
|
|
173
|
+
# @raise [ArgumentError] when the value is not a positive Integer
|
|
174
|
+
def self.partition_count_cache_ttl_ms=(value)
|
|
175
|
+
Rdkafka::Producer.partition_count_cache.ttl_ms = value
|
|
176
|
+
end
|
|
177
|
+
|
|
159
178
|
# @private
|
|
160
179
|
def self.opaques
|
|
161
180
|
@@opaques
|
|
@@ -180,6 +199,7 @@ module Rdkafka
|
|
|
180
199
|
|
|
181
200
|
@config_hash = DEFAULT_CONFIG.merge(config_hash)
|
|
182
201
|
@consumer_rebalance_listener = nil
|
|
202
|
+
@consumer_offset_commit_listener = nil
|
|
183
203
|
@consumer_poll_set = true
|
|
184
204
|
end
|
|
185
205
|
|
|
@@ -207,6 +227,20 @@ module Rdkafka
|
|
|
207
227
|
# @return [Object, #on_partitions_assigned, #on_partitions_revoked] listener instance
|
|
208
228
|
attr_writer :consumer_rebalance_listener
|
|
209
229
|
|
|
230
|
+
# Get the result of every offset commit, including async commits that the broker rejects
|
|
231
|
+
#
|
|
232
|
+
# `#on_offsets_committed` gets an {RdkafkaError} (nil on success) and the
|
|
233
|
+
# {Consumer::TopicPartitionList} of the commit, whose partitions carry their own errors. It
|
|
234
|
+
# runs inside {Consumer#poll} (or {Consumer#events_poll} when `consumer_poll_set` is false)
|
|
235
|
+
# and inside {Consumer#close}, on the thread that calls them.
|
|
236
|
+
#
|
|
237
|
+
# `#on_offsets_commit_failed` gets the same arguments, but only for a failed commit: not on
|
|
238
|
+
# success and not on `no_offset` (nothing new to commit). A listener with only this method
|
|
239
|
+
# costs nothing for the other commits.
|
|
240
|
+
#
|
|
241
|
+
# @return [Object, #on_offsets_committed, #on_offsets_commit_failed] listener instance
|
|
242
|
+
attr_writer :consumer_offset_commit_listener
|
|
243
|
+
|
|
210
244
|
# Should we use a single queue for the underlying consumer and events.
|
|
211
245
|
#
|
|
212
246
|
# This is an advanced API that allows for more granular control of the polling process.
|
|
@@ -243,6 +277,11 @@ module Rdkafka
|
|
|
243
277
|
Rdkafka::Bindings.rd_kafka_conf_set_rebalance_cb(config, Rdkafka::Bindings::RebalanceCallback)
|
|
244
278
|
end
|
|
245
279
|
|
|
280
|
+
if @consumer_offset_commit_listener
|
|
281
|
+
opaque.consumer_offset_commit_listener = @consumer_offset_commit_listener
|
|
282
|
+
Rdkafka::Bindings.rd_kafka_conf_set_offset_commit_cb(config, Rdkafka::Bindings::OffsetCommitCallback)
|
|
283
|
+
end
|
|
284
|
+
|
|
246
285
|
build_native_client(
|
|
247
286
|
config,
|
|
248
287
|
:rd_kafka_consumer,
|
|
@@ -598,6 +637,7 @@ module Rdkafka
|
|
|
598
637
|
class Opaque
|
|
599
638
|
attr_accessor :producer
|
|
600
639
|
attr_accessor :consumer_rebalance_listener
|
|
640
|
+
attr_accessor :consumer_offset_commit_listener
|
|
601
641
|
# Backreference set for share consumers so conf-level callbacks (which receive the opaque)
|
|
602
642
|
# can hand instance-scoped data - currently the librdkafka client name - back to the
|
|
603
643
|
# ShareConsumer, which has no native accessor for it
|
|
@@ -635,5 +675,35 @@ module Rdkafka
|
|
|
635
675
|
|
|
636
676
|
consumer_rebalance_listener.on_partitions_revoked(list)
|
|
637
677
|
end
|
|
678
|
+
|
|
679
|
+
# Invokes the on_offsets_committed callback on the offset commit listener if set
|
|
680
|
+
# @param error [Rdkafka::RdkafkaError, nil] the commit error or nil on success
|
|
681
|
+
# @param list [Rdkafka::Consumer::TopicPartitionList] the offsets of the commit
|
|
682
|
+
def call_on_offsets_committed(error, list)
|
|
683
|
+
return unless consumer_offset_commit_listener
|
|
684
|
+
return unless consumer_offset_commit_listener.respond_to?(:on_offsets_committed)
|
|
685
|
+
|
|
686
|
+
consumer_offset_commit_listener.on_offsets_committed(error, list)
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
# Invokes the on_offsets_commit_failed callback on the offset commit listener if set
|
|
690
|
+
# @param error [Rdkafka::RdkafkaError] the commit error
|
|
691
|
+
# @param list [Rdkafka::Consumer::TopicPartitionList] the offsets of the commit
|
|
692
|
+
def call_on_offsets_commit_failed(error, list)
|
|
693
|
+
return unless consumer_offset_commit_listener
|
|
694
|
+
return unless consumer_offset_commit_listener.respond_to?(:on_offsets_commit_failed)
|
|
695
|
+
|
|
696
|
+
consumer_offset_commit_listener.on_offsets_commit_failed(error, list)
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
# @return [Boolean] true if the offset commit listener wants the result of every commit
|
|
700
|
+
def offsets_committed_listened?
|
|
701
|
+
consumer_offset_commit_listener.respond_to?(:on_offsets_committed)
|
|
702
|
+
end
|
|
703
|
+
|
|
704
|
+
# @return [Boolean] true if the offset commit listener wants the failed commits
|
|
705
|
+
def offsets_commit_failed_listened?
|
|
706
|
+
consumer_offset_commit_listener.respond_to?(:on_offsets_commit_failed)
|
|
707
|
+
end
|
|
638
708
|
end
|
|
639
709
|
end
|
|
@@ -38,7 +38,7 @@ module Rdkafka
|
|
|
38
38
|
# Calling this method multiple times for the same topic will overwrite the previous
|
|
39
39
|
# configuraton.
|
|
40
40
|
#
|
|
41
|
-
# @param
|
|
41
|
+
# @param topic_name [String] The topic's name
|
|
42
42
|
# @param partitions [Array<Integer>, Range<Integer>, Integer] The topic's partitions or
|
|
43
43
|
# partition count
|
|
44
44
|
#
|
|
@@ -52,14 +52,14 @@ module Rdkafka
|
|
|
52
52
|
#
|
|
53
53
|
# @example Add a topic with all topics up to a count
|
|
54
54
|
# tpl.add_topic("topic", 9)
|
|
55
|
-
def add_topic(
|
|
55
|
+
def add_topic(topic_name, partitions = nil)
|
|
56
56
|
if partitions.nil?
|
|
57
|
-
@data[
|
|
57
|
+
@data[topic_name.to_s] = nil
|
|
58
58
|
else
|
|
59
59
|
if partitions.is_a? Integer
|
|
60
60
|
partitions = (0..partitions - 1)
|
|
61
61
|
end
|
|
62
|
-
@data[
|
|
62
|
+
@data[topic_name.to_s] = partitions.map { |p| Partition.new(p, nil, Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR) }
|
|
63
63
|
end
|
|
64
64
|
end
|
|
65
65
|
|
|
@@ -67,12 +67,12 @@ module Rdkafka
|
|
|
67
67
|
# Calling this method multiple times for the same topic will overwrite the previous
|
|
68
68
|
# configuraton.
|
|
69
69
|
#
|
|
70
|
-
# @param
|
|
70
|
+
# @param topic_name [String] The topic's name
|
|
71
71
|
# @param partitions_with_offsets [Hash{Integer => Integer}, Array<Consumer::Partition>] The
|
|
72
72
|
# topic's partitions and offsets (Hash) or partitions with offsets and metadata (Array)
|
|
73
73
|
# @return [nil]
|
|
74
|
-
def add_topic_and_partitions_with_offsets(
|
|
75
|
-
@data[
|
|
74
|
+
def add_topic_and_partitions_with_offsets(topic_name, partitions_with_offsets)
|
|
75
|
+
@data[topic_name.to_s] = partitions_with_offsets.map do |p, o|
|
|
76
76
|
p.is_a?(Partition) ? p : Partition.new(p, o)
|
|
77
77
|
end
|
|
78
78
|
end
|
data/lib/rdkafka/consumer.rb
CHANGED
|
@@ -16,6 +16,15 @@ module Rdkafka
|
|
|
16
16
|
include Helpers::OAuth
|
|
17
17
|
include Helpers::Metadata
|
|
18
18
|
include Helpers::ListOffsets
|
|
19
|
+
include Helpers::MessageBuildError
|
|
20
|
+
|
|
21
|
+
# Reason passed to the error callback when closing the consumer fails
|
|
22
|
+
CLOSE_ERROR_REASON = "Consumer close failed"
|
|
23
|
+
|
|
24
|
+
# Frozen result of a batch poll that got no messages
|
|
25
|
+
EMPTY_BATCH = [].freeze
|
|
26
|
+
|
|
27
|
+
private_constant :EMPTY_BATCH
|
|
19
28
|
|
|
20
29
|
# @private
|
|
21
30
|
# @param native_kafka [NativeKafka] wrapper around the native Kafka consumer handle
|
|
@@ -25,36 +34,69 @@ module Rdkafka
|
|
|
25
34
|
# consumer queue without capturing `self` (capturing the consumer in its own finalizer would
|
|
26
35
|
# pin it and prevent it from ever being collected).
|
|
27
36
|
@consumer_queue_holder = []
|
|
37
|
+
# Set under the native access lock once `rd_kafka_consumer_close` ran
|
|
38
|
+
@consumer_closed = false
|
|
28
39
|
|
|
29
40
|
# Makes sure the consumer is closed (consumer queue destroyed and native client destroyed)
|
|
30
41
|
# before it gets GCed by Ruby.
|
|
31
42
|
ObjectSpace.define_finalizer(self, self.class.finalizer(native_kafka, @consumer_queue_holder))
|
|
32
43
|
end
|
|
33
44
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
class << self
|
|
46
|
+
# Builds the GC finalizer for a consumer. It mirrors {#close}: close the consumer, destroy the
|
|
47
|
+
# consumer-queue reference, then destroy the native client. The default
|
|
48
|
+
# `NativeKafka#finalizer` went straight to `rd_kafka_destroy`, leaving the consumer-queue
|
|
49
|
+
# reference (from `rd_kafka_queue_get_consumer`, taken by `poll_batch`) dangling - which can
|
|
50
|
+
# make `rd_kafka_destroy` block inside the finalizer (process hang at GC/shutdown) or leak the
|
|
51
|
+
# handle.
|
|
52
|
+
#
|
|
53
|
+
# @private
|
|
54
|
+
# @param native_kafka [NativeKafka] the wrapped native client
|
|
55
|
+
# @param queue_holder [Array] single-element holder carrying the consumer queue pointer
|
|
56
|
+
# (or empty)
|
|
57
|
+
# @return [Proc] finalizer proc that must not reference the consumer instance
|
|
58
|
+
def finalizer(native_kafka, queue_holder)
|
|
59
|
+
proc do
|
|
60
|
+
next if native_kafka.closed?
|
|
61
|
+
|
|
62
|
+
close_response = nil
|
|
63
|
+
|
|
64
|
+
native_kafka.synchronize do |inner|
|
|
65
|
+
close_response = Rdkafka::Bindings.rd_kafka_consumer_close(inner)
|
|
66
|
+
|
|
67
|
+
queue = queue_holder[0]
|
|
68
|
+
Rdkafka::Bindings.rd_kafka_queue_destroy(queue) if queue
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
report_close_error(native_kafka, close_response)
|
|
72
|
+
|
|
73
|
+
native_kafka.close
|
|
55
74
|
end
|
|
75
|
+
end
|
|
56
76
|
|
|
57
|
-
|
|
77
|
+
# Reports a failed `rd_kafka_consumer_close` (e.g. of a fatally-errored consumer) through
|
|
78
|
+
# {Bindings::ErrorCallback}, the same path as errors emitted by librdkafka, so it reaches
|
|
79
|
+
# {Config.error_callback} in the shape the callback expects (libraries such as karafka-core
|
|
80
|
+
# replace `ErrorCallback`). It runs after the close, but before the native client is
|
|
81
|
+
# destroyed, as the callback reads the client name and fatal error from it. Close runs during
|
|
82
|
+
# shutdown and from the GC finalizer, so the error is reported, never raised.
|
|
83
|
+
#
|
|
84
|
+
# @private
|
|
85
|
+
# @param native_kafka [NativeKafka] the wrapped native client
|
|
86
|
+
# @param response [Integer] result code of `rd_kafka_consumer_close`
|
|
87
|
+
# @return [nil]
|
|
88
|
+
def report_close_error(native_kafka, response)
|
|
89
|
+
return if response == Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
90
|
+
|
|
91
|
+
native_kafka.with_inner do |inner|
|
|
92
|
+
Rdkafka::Bindings::ErrorCallback.call(inner, response, CLOSE_ERROR_REASON, nil)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
nil
|
|
96
|
+
rescue Exception => err
|
|
97
|
+
Rdkafka::Config.logger.error("Unhandled exception: #{err.class} - #{err.message}")
|
|
98
|
+
|
|
99
|
+
nil
|
|
58
100
|
end
|
|
59
101
|
end
|
|
60
102
|
|
|
@@ -114,7 +156,7 @@ module Rdkafka
|
|
|
114
156
|
|
|
115
157
|
# Polls for events in a non-blocking loop, yielding the count after each iteration.
|
|
116
158
|
#
|
|
117
|
-
# This method processes events (stats, errors, etc.) in a single GVL
|
|
159
|
+
# This method processes events (stats, errors, etc.) in a single GVL session,
|
|
118
160
|
# which is more efficient than repeated individual polls. It uses non-blocking polls
|
|
119
161
|
# internally (no GVL release between polls).
|
|
120
162
|
#
|
|
@@ -135,8 +177,9 @@ module Rdkafka
|
|
|
135
177
|
# consumer.events_poll_nb_each do |_count|
|
|
136
178
|
# :stop if monotonic_now >= deadline
|
|
137
179
|
# end
|
|
138
|
-
# @note This method
|
|
139
|
-
# Other
|
|
180
|
+
# @note This method runs inside `with_inner` and does not hold a lock while it loops.
|
|
181
|
+
# Other operations on the same client run concurrently. `close` waits until this
|
|
182
|
+
# method returns.
|
|
140
183
|
# @note This method is thread-safe as it uses @native_kafka.with_inner synchronization
|
|
141
184
|
# @note Do NOT use this if `consumer_poll_set` was set to `true`
|
|
142
185
|
def events_poll_nb_each
|
|
@@ -153,7 +196,7 @@ module Rdkafka
|
|
|
153
196
|
|
|
154
197
|
# Polls for messages in a non-blocking loop, yielding each message to the caller.
|
|
155
198
|
#
|
|
156
|
-
# This method processes messages in a single GVL
|
|
199
|
+
# This method processes messages in a single GVL session until the queue is empty
|
|
157
200
|
# or the caller returns `:stop`. It handles the message pointer lifecycle internally,
|
|
158
201
|
# ensuring proper cleanup via `rd_kafka_message_destroy`.
|
|
159
202
|
#
|
|
@@ -162,7 +205,8 @@ module Rdkafka
|
|
|
162
205
|
# @yieldreturn [Symbol, Object] Return `:stop` to break the loop, any other value continues
|
|
163
206
|
# @return [nil]
|
|
164
207
|
# @raise [Rdkafka::ClosedConsumerError] if called on a closed consumer
|
|
165
|
-
# @raise [Rdkafka::RdkafkaError] if a Kafka error occurs while polling
|
|
208
|
+
# @raise [Rdkafka::RdkafkaError] if a Kafka error occurs while polling, or if a message cannot
|
|
209
|
+
# be built; `details` then holds its `topic`, `partition` and `offset`
|
|
166
210
|
#
|
|
167
211
|
# @example Process messages until queue is empty
|
|
168
212
|
# consumer.poll_nb_each do |message|
|
|
@@ -180,8 +224,9 @@ module Rdkafka
|
|
|
180
224
|
# `events_poll_nb_each` which uses `rd_kafka_poll` for event callbacks (delivery reports,
|
|
181
225
|
# statistics, etc.). For consumers, use this method to receive messages and
|
|
182
226
|
# `events_poll_nb_each` for processing background events.
|
|
183
|
-
# @note This method
|
|
184
|
-
#
|
|
227
|
+
# @note This method runs inside `with_inner` and does not hold a lock while it loops.
|
|
228
|
+
# Other operations on the same client run concurrently. `close` waits until this
|
|
229
|
+
# method returns.
|
|
185
230
|
# @note Timeout/max_messages logic should be implemented by the caller
|
|
186
231
|
def poll_nb_each
|
|
187
232
|
closed_consumer_check(__method__)
|
|
@@ -198,7 +243,13 @@ module Rdkafka
|
|
|
198
243
|
Rdkafka::RdkafkaError.validate!(native_message, client_ptr: inner)
|
|
199
244
|
end
|
|
200
245
|
|
|
201
|
-
|
|
246
|
+
begin
|
|
247
|
+
message = Consumer::Message.new(native_message)
|
|
248
|
+
rescue Rdkafka::RdkafkaError => e
|
|
249
|
+
raise message_build_error(native_message, e)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
result = yield message
|
|
202
253
|
break if result == :stop
|
|
203
254
|
ensure
|
|
204
255
|
Rdkafka::Bindings.rd_kafka_message_destroy(message_ptr)
|
|
@@ -213,16 +264,26 @@ module Rdkafka
|
|
|
213
264
|
return if closed?
|
|
214
265
|
ObjectSpace.undefine_finalizer(self)
|
|
215
266
|
|
|
216
|
-
|
|
217
|
-
|
|
267
|
+
close_response = nil
|
|
268
|
+
|
|
269
|
+
# The handle stays open until `NativeKafka#close`, so a late or retried closer can get here
|
|
270
|
+
# after the native consumer was already closed
|
|
271
|
+
@native_kafka.synchronize_unless_closed do |inner|
|
|
272
|
+
next if @consumer_closed
|
|
273
|
+
|
|
274
|
+
close_response = Rdkafka::Bindings.rd_kafka_consumer_close(inner)
|
|
218
275
|
|
|
219
276
|
if @consumer_queue
|
|
220
277
|
Rdkafka::Bindings.rd_kafka_queue_destroy(@consumer_queue)
|
|
221
278
|
@consumer_queue = nil
|
|
222
279
|
@consumer_queue_holder[0] = nil
|
|
223
280
|
end
|
|
281
|
+
|
|
282
|
+
@consumer_closed = true
|
|
224
283
|
end
|
|
225
284
|
|
|
285
|
+
self.class.report_close_error(@native_kafka, close_response) unless close_response.nil?
|
|
286
|
+
|
|
226
287
|
@native_kafka.close
|
|
227
288
|
end
|
|
228
289
|
|
|
@@ -469,12 +530,12 @@ module Rdkafka
|
|
|
469
530
|
|
|
470
531
|
# Query broker for low (oldest/beginning) and high (newest/end) offsets for a partition.
|
|
471
532
|
#
|
|
472
|
-
# @param
|
|
533
|
+
# @param topic_name [String] The topic to query
|
|
473
534
|
# @param partition [Integer] The partition to query
|
|
474
535
|
# @param timeout_ms [Integer] The timeout for querying the broker
|
|
475
536
|
# @return [Integer] The low and high watermark
|
|
476
537
|
# @raise [RdkafkaError] When querying the broker fails.
|
|
477
|
-
def query_watermark_offsets(
|
|
538
|
+
def query_watermark_offsets(topic_name, partition, timeout_ms = Defaults::CONSUMER_QUERY_WATERMARK_TIMEOUT_MS)
|
|
478
539
|
closed_consumer_check(__method__)
|
|
479
540
|
|
|
480
541
|
low = FFI::MemoryPointer.new(:int64, 1)
|
|
@@ -483,13 +544,13 @@ module Rdkafka
|
|
|
483
544
|
@native_kafka.with_inner do |inner|
|
|
484
545
|
response = Rdkafka::Bindings.rd_kafka_query_watermark_offsets(
|
|
485
546
|
inner,
|
|
486
|
-
|
|
547
|
+
topic_name,
|
|
487
548
|
partition,
|
|
488
549
|
low,
|
|
489
550
|
high,
|
|
490
551
|
timeout_ms
|
|
491
552
|
)
|
|
492
|
-
Rdkafka::RdkafkaError.validate!(response, "Error querying watermark offsets for partition #{partition} of #{
|
|
553
|
+
Rdkafka::RdkafkaError.validate!(response, "Error querying watermark offsets for partition #{partition} of #{topic_name}", client_ptr: inner)
|
|
493
554
|
end
|
|
494
555
|
|
|
495
556
|
[low.read_array_of_int64(1).first, high.read_array_of_int64(1).first]
|
|
@@ -651,12 +712,12 @@ module Rdkafka
|
|
|
651
712
|
# Seek to a particular message by providing the topic, partition and offset. The next poll on
|
|
652
713
|
# the topic/partition will return the message at the given offset.
|
|
653
714
|
#
|
|
654
|
-
# @param
|
|
715
|
+
# @param topic_name [String] The topic in which to seek
|
|
655
716
|
# @param partition [Integer] The partition number to seek
|
|
656
717
|
# @param offset [Integer] The partition offset to seek
|
|
657
718
|
# @return [nil]
|
|
658
719
|
# @raise [RdkafkaError] When seeking fails
|
|
659
|
-
def seek_by(
|
|
720
|
+
def seek_by(topic_name, partition, offset)
|
|
660
721
|
closed_consumer_check(__method__)
|
|
661
722
|
|
|
662
723
|
# rd_kafka_offset_store is one of the few calls that does not support
|
|
@@ -664,7 +725,7 @@ module Rdkafka
|
|
|
664
725
|
native_topic = @native_kafka.with_inner do |inner|
|
|
665
726
|
Rdkafka::Bindings.rd_kafka_topic_new(
|
|
666
727
|
inner,
|
|
667
|
-
|
|
728
|
+
topic_name,
|
|
668
729
|
nil
|
|
669
730
|
)
|
|
670
731
|
end
|
|
@@ -754,7 +815,8 @@ module Rdkafka
|
|
|
754
815
|
#
|
|
755
816
|
# @param timeout_ms [Integer] Timeout of this poll
|
|
756
817
|
# @return [Message, nil] A message or nil if there was no new message within the timeout
|
|
757
|
-
# @raise [RdkafkaError] When polling fails
|
|
818
|
+
# @raise [RdkafkaError] When polling fails, or when the message cannot be built (e.g. its
|
|
819
|
+
# headers cannot be read); `details` then holds its `topic`, `partition` and `offset`
|
|
758
820
|
def poll(timeout_ms)
|
|
759
821
|
closed_consumer_check(__method__)
|
|
760
822
|
|
|
@@ -768,7 +830,17 @@ module Rdkafka
|
|
|
768
830
|
native_message = Rdkafka::Bindings::Message.new(message_ptr)
|
|
769
831
|
|
|
770
832
|
# Create a message to pass out
|
|
771
|
-
|
|
833
|
+
if native_message[:err] == Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
834
|
+
# Assigned before returning: a `return` inside `begin/rescue` within this method's `ensure`
|
|
835
|
+
# compiles to a `throw`, which allocates on every message.
|
|
836
|
+
message = begin
|
|
837
|
+
Rdkafka::Consumer::Message.new(native_message)
|
|
838
|
+
rescue Rdkafka::RdkafkaError => e
|
|
839
|
+
raise message_build_error(native_message, e)
|
|
840
|
+
end
|
|
841
|
+
|
|
842
|
+
return message
|
|
843
|
+
end
|
|
772
844
|
|
|
773
845
|
# Raise error if needed - wrap just the validate! call with client access
|
|
774
846
|
@native_kafka.with_inner do |inner|
|
|
@@ -789,7 +861,8 @@ module Rdkafka
|
|
|
789
861
|
#
|
|
790
862
|
# @param timeout_ms [Integer] Timeout of this poll (default: 0 for non-blocking)
|
|
791
863
|
# @return [Message, nil] A message or nil if there was no new message within the timeout
|
|
792
|
-
# @raise [RdkafkaError] When polling fails
|
|
864
|
+
# @raise [RdkafkaError] When polling fails, or when the message cannot be built (e.g. its
|
|
865
|
+
# headers cannot be read); `details` then holds its `topic`, `partition` and `offset`
|
|
793
866
|
#
|
|
794
867
|
# @example Using with fiber scheduler
|
|
795
868
|
# # After receiving IO notification that messages are available
|
|
@@ -815,7 +888,11 @@ module Rdkafka
|
|
|
815
888
|
end
|
|
816
889
|
end
|
|
817
890
|
# Create a message to pass out
|
|
818
|
-
|
|
891
|
+
begin
|
|
892
|
+
Rdkafka::Consumer::Message.new(native_message)
|
|
893
|
+
rescue Rdkafka::RdkafkaError => e
|
|
894
|
+
raise message_build_error(native_message, e)
|
|
895
|
+
end
|
|
819
896
|
end
|
|
820
897
|
ensure
|
|
821
898
|
# Clean up rdkafka message if there is one
|
|
@@ -879,17 +956,19 @@ module Rdkafka
|
|
|
879
956
|
# rather than raised, so callers receive the complete batch - both messages and errors -
|
|
880
957
|
# and can decide how to handle each. This is particularly useful when multiple partitions
|
|
881
958
|
# signal EOF simultaneously: all signals appear in the returned array rather than only
|
|
882
|
-
# the first one being raised and the rest silently discarded.
|
|
959
|
+
# the first one being raised and the rest silently discarded. A message that cannot be built
|
|
960
|
+
# (e.g. its headers cannot be read) also comes inline as an {RdkafkaError} whose `details`
|
|
961
|
+
# hold its `topic`, `partition` and `offset`.
|
|
883
962
|
#
|
|
884
963
|
# @param timeout_ms [Integer] Timeout waiting for the first message (-1 for infinite)
|
|
885
964
|
# @param max_items [Integer] Maximum number of messages to return per call
|
|
886
|
-
# @return [Array<Message, RdkafkaError>] Batch of messages and/or error events in arrival order
|
|
965
|
+
# @return [Array<Message, RdkafkaError>] Batch of messages and/or error events in arrival order,
|
|
966
|
+
# or a frozen empty array when there are none
|
|
887
967
|
# @raise [ClosedConsumerError] When called on a closed consumer
|
|
888
968
|
def poll_batch(timeout_ms, max_items: 100)
|
|
889
969
|
closed_consumer_check(__method__)
|
|
890
970
|
|
|
891
971
|
buffer = batch_buffer(max_items)
|
|
892
|
-
results = []
|
|
893
972
|
|
|
894
973
|
count = @native_kafka.with_inner do |_inner|
|
|
895
974
|
Rdkafka::Bindings.rd_kafka_consume_batch_queue(
|
|
@@ -900,8 +979,9 @@ module Rdkafka
|
|
|
900
979
|
)
|
|
901
980
|
end
|
|
902
981
|
|
|
903
|
-
return
|
|
982
|
+
return EMPTY_BATCH if count <= 0
|
|
904
983
|
|
|
984
|
+
results = []
|
|
905
985
|
i = 0
|
|
906
986
|
begin
|
|
907
987
|
while i < count
|
|
@@ -930,7 +1010,7 @@ module Rdkafka
|
|
|
930
1010
|
# A message that fails to build (e.g. a header read error) is surfaced inline as an
|
|
931
1011
|
# error event rather than discarding the whole batch - including the messages already
|
|
932
1012
|
# built - and raising, which silently lost them once their offsets had been stored.
|
|
933
|
-
results << e
|
|
1013
|
+
results << message_build_error(native_message, e)
|
|
934
1014
|
end
|
|
935
1015
|
ensure
|
|
936
1016
|
Rdkafka::Bindings.rd_kafka_message_destroy(ptr)
|
|
@@ -956,7 +1036,8 @@ module Rdkafka
|
|
|
956
1036
|
# @param timeout_ms [Integer] Timeout waiting for the first message
|
|
957
1037
|
# (default: 0 for non-blocking)
|
|
958
1038
|
# @param max_items [Integer] Maximum number of messages to return per call
|
|
959
|
-
# @return [Array<Message, RdkafkaError>] Batch of messages and/or error events in arrival order
|
|
1039
|
+
# @return [Array<Message, RdkafkaError>] Batch of messages and/or error events in arrival order,
|
|
1040
|
+
# or a frozen empty array when there are none
|
|
960
1041
|
# @raise [ClosedConsumerError] When called on a closed consumer
|
|
961
1042
|
# @note Since the GVL is not released, a non-zero timeout_ms will block all Ruby
|
|
962
1043
|
# threads/fibers for the duration. Use {#poll_batch} if you need a blocking wait.
|
|
@@ -966,7 +1047,6 @@ module Rdkafka
|
|
|
966
1047
|
closed_consumer_check(__method__)
|
|
967
1048
|
|
|
968
1049
|
buffer = batch_buffer(max_items)
|
|
969
|
-
results = []
|
|
970
1050
|
|
|
971
1051
|
count = @native_kafka.with_inner do |_inner|
|
|
972
1052
|
Rdkafka::Bindings.rd_kafka_consume_batch_queue_nb(
|
|
@@ -977,8 +1057,9 @@ module Rdkafka
|
|
|
977
1057
|
)
|
|
978
1058
|
end
|
|
979
1059
|
|
|
980
|
-
return
|
|
1060
|
+
return EMPTY_BATCH if count <= 0
|
|
981
1061
|
|
|
1062
|
+
results = []
|
|
982
1063
|
i = 0
|
|
983
1064
|
begin
|
|
984
1065
|
while i < count
|
|
@@ -1007,7 +1088,7 @@ module Rdkafka
|
|
|
1007
1088
|
# A message that fails to build (e.g. a header read error) is surfaced inline as an
|
|
1008
1089
|
# error event rather than discarding the whole batch - including the messages already
|
|
1009
1090
|
# built - and raising, which silently lost them once their offsets had been stored.
|
|
1010
|
-
results << e
|
|
1091
|
+
results << message_build_error(native_message, e)
|
|
1011
1092
|
end
|
|
1012
1093
|
ensure
|
|
1013
1094
|
Rdkafka::Bindings.rd_kafka_message_destroy(ptr)
|