rdkafka 0.13.0 → 0.31.0
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 +388 -111
- data/{LICENSE → MIT-LICENSE} +2 -1
- data/README.md +90 -43
- data/Rakefile +21 -23
- data/bin/verify_kafka_warnings +44 -0
- data/dist/librdkafka-2.15.1.tar.gz +0 -0
- data/dist/patches/rdkafka_global_init.patch +15 -0
- data/docker-compose-ssl.yml +39 -0
- data/docker-compose.yml +22 -17
- data/ext/Rakefile +97 -30
- data/lib/rdkafka/abstract_handle.rb +113 -34
- data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
- data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
- data/lib/rdkafka/admin/config_binding_result.rb +30 -0
- data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
- data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
- data/lib/rdkafka/admin/create_acl_report.rb +26 -0
- data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
- data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
- data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
- data/lib/rdkafka/admin/create_topic_report.rb +4 -1
- data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
- data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
- data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
- data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_records_report.rb +25 -0
- data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
- data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
- data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
- data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
- data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
- data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
- data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
- data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
- data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
- data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
- data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
- data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
- data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
- data/lib/rdkafka/admin.rb +1381 -38
- data/lib/rdkafka/bindings/admin/acls.rb +85 -0
- data/lib/rdkafka/bindings/admin/configs.rb +37 -0
- data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
- data/lib/rdkafka/bindings/admin.rb +197 -0
- data/lib/rdkafka/bindings/callbacks.rb +100 -0
- data/lib/rdkafka/bindings/configuration.rb +50 -0
- data/lib/rdkafka/bindings/consumer.rb +83 -0
- data/lib/rdkafka/bindings/errors.rb +15 -0
- data/lib/rdkafka/bindings/handle.rb +16 -0
- data/lib/rdkafka/bindings/messages.rb +28 -0
- data/lib/rdkafka/bindings/metadata.rb +14 -0
- data/lib/rdkafka/bindings/polling.rb +17 -0
- data/lib/rdkafka/bindings/producer.rb +77 -0
- data/lib/rdkafka/bindings/structs.rb +62 -0
- data/lib/rdkafka/bindings.rb +69 -294
- data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
- data/lib/rdkafka/callbacks/base_handler.rb +62 -0
- data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
- data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
- data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
- data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
- data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
- data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
- data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
- data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
- data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
- data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
- data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
- data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
- data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
- data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
- data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
- data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
- data/lib/rdkafka/callbacks.rb +273 -52
- data/lib/rdkafka/clients.rb +81 -0
- data/lib/rdkafka/config.rb +333 -91
- data/lib/rdkafka/consumer/allocator.rb +38 -0
- data/lib/rdkafka/consumer/headers.rb +39 -25
- data/lib/rdkafka/consumer/message.rb +18 -20
- data/lib/rdkafka/consumer/partition.rb +15 -4
- data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
- data/lib/rdkafka/consumer.rb +765 -192
- data/lib/rdkafka/defaults.rb +123 -0
- data/lib/rdkafka/error.rb +40 -14
- data/lib/rdkafka/helpers/list_offsets.rb +127 -0
- data/lib/rdkafka/helpers/metadata.rb +29 -0
- data/lib/rdkafka/helpers/oauth.rb +95 -0
- data/lib/rdkafka/helpers/time.rb +19 -0
- data/lib/rdkafka/metadata.rb +138 -26
- data/lib/rdkafka/native_kafka.rb +142 -22
- data/lib/rdkafka/producer/delivery_handle.rb +27 -7
- data/lib/rdkafka/producer/delivery_report.rb +71 -4
- data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
- data/lib/rdkafka/producer.rb +422 -81
- data/lib/rdkafka/version.rb +6 -3
- data/lib/rdkafka.rb +73 -1
- data/package-lock.json +331 -0
- data/package.json +9 -0
- data/rdkafka.gemspec +78 -25
- data/renovate.json +111 -0
- metadata +114 -134
- data/.gitignore +0 -8
- data/.rspec +0 -1
- data/.semaphore/semaphore.yml +0 -27
- data/.yardopts +0 -2
- data/Gemfile +0 -5
- data/Guardfile +0 -19
- data/ext/README.md +0 -18
- data/spec/rdkafka/abstract_handle_spec.rb +0 -115
- data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
- data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
- data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
- data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
- data/spec/rdkafka/admin_spec.rb +0 -204
- data/spec/rdkafka/bindings_spec.rb +0 -136
- data/spec/rdkafka/callbacks_spec.rb +0 -22
- data/spec/rdkafka/config_spec.rb +0 -197
- data/spec/rdkafka/consumer/headers_spec.rb +0 -62
- data/spec/rdkafka/consumer/message_spec.rb +0 -141
- data/spec/rdkafka/consumer/partition_spec.rb +0 -59
- data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
- data/spec/rdkafka/consumer_spec.rb +0 -1106
- data/spec/rdkafka/error_spec.rb +0 -91
- data/spec/rdkafka/metadata_spec.rb +0 -80
- data/spec/rdkafka/native_kafka_spec.rb +0 -124
- data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
- data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
- data/spec/rdkafka/producer_spec.rb +0 -557
- data/spec/spec_helper.rb +0 -155
data/lib/rdkafka/config.rb
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "logger"
|
|
4
|
-
|
|
5
3
|
module Rdkafka
|
|
6
4
|
# Configuration for a Kafka consumer or producer. You can create an instance and use
|
|
7
5
|
# the consumer and producer methods to create a client. Documentation of the available
|
|
8
|
-
# configuration options is available on
|
|
6
|
+
# configuration options is available on
|
|
7
|
+
# https://github.com/confluentinc/librdkafka/blob/master/CONFIGURATION.md.
|
|
9
8
|
class Config
|
|
10
|
-
#
|
|
11
|
-
@@logger = Logger.new(
|
|
12
|
-
#
|
|
9
|
+
# @!visibility private
|
|
10
|
+
@@logger = Logger.new($stdout)
|
|
11
|
+
# @!visibility private
|
|
13
12
|
@@statistics_callback = nil
|
|
14
|
-
#
|
|
13
|
+
# @!visibility private
|
|
15
14
|
@@error_callback = nil
|
|
16
|
-
#
|
|
17
|
-
@@opaques =
|
|
18
|
-
#
|
|
15
|
+
# @!visibility private
|
|
16
|
+
@@opaques = ObjectSpace::WeakMap.new
|
|
17
|
+
# @!visibility private
|
|
19
18
|
@@log_queue = Queue.new
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
# We memoize thread on first log flush. This allows us also to restart logger thread on forks
|
|
20
|
+
@@log_thread = nil
|
|
21
|
+
# @!visibility private
|
|
22
|
+
@@log_mutex = Mutex.new
|
|
23
|
+
# @!visibility private
|
|
24
|
+
@@oauthbearer_token_refresh_callback = nil
|
|
25
|
+
# @!visibility private
|
|
26
|
+
# Whether the partition key length passed to the partitioner is the byte length (`bytesize`)
|
|
27
|
+
# rather than the character length (`size`). `false` (character length) is the legacy default
|
|
28
|
+
# to avoid changing partition assignment for existing multibyte/binary keys; set to `true` to
|
|
29
|
+
# opt into the correct, other-client-consistent byte-length hashing.
|
|
30
|
+
@@partitioner_key_uses_bytesize = false
|
|
27
31
|
|
|
28
32
|
# Returns the current logger, by default this is a logger to stdout.
|
|
29
33
|
#
|
|
@@ -32,6 +36,24 @@ module Rdkafka
|
|
|
32
36
|
@@logger
|
|
33
37
|
end
|
|
34
38
|
|
|
39
|
+
# Makes sure that there is a thread for consuming logs
|
|
40
|
+
# We do not spawn thread immediately and we need to check if it operates to support forking
|
|
41
|
+
def self.ensure_log_thread
|
|
42
|
+
return if @@log_thread&.alive?
|
|
43
|
+
|
|
44
|
+
@@log_mutex.synchronize do
|
|
45
|
+
# Restart if dead (fork, crash)
|
|
46
|
+
@@log_thread = nil if @@log_thread && !@@log_thread.alive?
|
|
47
|
+
|
|
48
|
+
@@log_thread ||= Thread.start do
|
|
49
|
+
loop do
|
|
50
|
+
severity, msg = @@log_queue.pop
|
|
51
|
+
@@logger.add(severity, msg)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
35
57
|
# Returns a queue whose contents will be passed to the configured logger. Each entry
|
|
36
58
|
# should follow the format [Logger::Severity, String]. The benefit over calling the
|
|
37
59
|
# logger directly is that this is safe to use from trap contexts.
|
|
@@ -53,13 +75,13 @@ module Rdkafka
|
|
|
53
75
|
|
|
54
76
|
# Set a callback that will be called every time the underlying client emits statistics.
|
|
55
77
|
# You can configure if and how often this happens using `statistics.interval.ms`.
|
|
56
|
-
# The callback is called with a hash that's documented here:
|
|
57
|
-
#
|
|
58
|
-
# @param callback [Proc, #call] The callback
|
|
78
|
+
# The callback is called with a hash that's documented here:
|
|
79
|
+
# https://github.com/confluentinc/librdkafka/blob/master/STATISTICS.md
|
|
59
80
|
#
|
|
81
|
+
# @param callback [Proc, #call, nil] callable object or nil to clear
|
|
60
82
|
# @return [nil]
|
|
61
83
|
def self.statistics_callback=(callback)
|
|
62
|
-
raise TypeError.new("Callback has to be callable") unless callback.respond_to?(:call)
|
|
84
|
+
raise TypeError.new("Callback has to be callable") unless callback.respond_to?(:call) || callback.nil?
|
|
63
85
|
@@statistics_callback = callback
|
|
64
86
|
end
|
|
65
87
|
|
|
@@ -71,14 +93,14 @@ module Rdkafka
|
|
|
71
93
|
end
|
|
72
94
|
|
|
73
95
|
# Set a callback that will be called every time the underlying client emits an error.
|
|
74
|
-
# If this callback is not set, global errors such as brokers becoming unavailable will only be
|
|
96
|
+
# If this callback is not set, global errors such as brokers becoming unavailable will only be
|
|
97
|
+
# sent to the logger, as defined by librdkafka.
|
|
75
98
|
# The callback is called with an instance of RdKafka::Error.
|
|
76
99
|
#
|
|
77
|
-
# @param callback [Proc, #call]
|
|
78
|
-
#
|
|
100
|
+
# @param callback [Proc, #call, nil] callable object to handle errors or nil to clear
|
|
79
101
|
# @return [nil]
|
|
80
102
|
def self.error_callback=(callback)
|
|
81
|
-
raise TypeError.new("Callback has to be callable") unless callback.respond_to?(:call)
|
|
103
|
+
raise TypeError.new("Callback has to be callable") unless callback.respond_to?(:call) || callback.nil?
|
|
82
104
|
@@error_callback = callback
|
|
83
105
|
end
|
|
84
106
|
|
|
@@ -89,21 +111,63 @@ module Rdkafka
|
|
|
89
111
|
@@error_callback
|
|
90
112
|
end
|
|
91
113
|
|
|
114
|
+
# Sets the SASL/OAUTHBEARER token refresh callback.
|
|
115
|
+
# This callback will be triggered when it is time to refresh the client's OAUTHBEARER token
|
|
116
|
+
#
|
|
117
|
+
# @param callback [Proc, #call, nil] callable object to handle token refresh or nil to clear
|
|
118
|
+
# @return [nil]
|
|
119
|
+
def self.oauthbearer_token_refresh_callback=(callback)
|
|
120
|
+
raise TypeError.new("Callback has to be callable") unless callback.respond_to?(:call) || callback.nil?
|
|
121
|
+
@@oauthbearer_token_refresh_callback = callback
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Returns the current oauthbearer_token_refresh_callback callback, by default this is nil.
|
|
125
|
+
#
|
|
126
|
+
# @return [Proc, nil]
|
|
127
|
+
def self.oauthbearer_token_refresh_callback
|
|
128
|
+
@@oauthbearer_token_refresh_callback
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Whether the partitioner hashes the partition key by its byte length instead of its character
|
|
132
|
+
# length. Defaults to `false` (character length) to preserve existing partition assignment for
|
|
133
|
+
# multibyte/binary keys. Passing the byte length is the correct, other-client-consistent
|
|
134
|
+
# behavior; enable it (and be aware it can move such keys to a different partition) with
|
|
135
|
+
# `Rdkafka::Config.partitioner_key_uses_bytesize = true`. This default is expected to change in
|
|
136
|
+
# a future major version.
|
|
137
|
+
#
|
|
138
|
+
# @return [Boolean]
|
|
139
|
+
def self.partitioner_key_uses_bytesize
|
|
140
|
+
@@partitioner_key_uses_bytesize
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Sets whether the partitioner hashes the partition key by its byte length rather than its
|
|
144
|
+
# character length. The key length method is resolved here once rather than on every produced
|
|
145
|
+
# message, and each call re-resolves it, so changing the setting at runtime takes effect
|
|
146
|
+
# immediately.
|
|
147
|
+
#
|
|
148
|
+
# @param value [Boolean] true to hash by byte length, false (default) for character length
|
|
149
|
+
# @return [void]
|
|
150
|
+
def self.partitioner_key_uses_bytesize=(value)
|
|
151
|
+
@@partitioner_key_uses_bytesize = value
|
|
152
|
+
|
|
153
|
+
Rdkafka::Bindings.singleton_class.alias_method(
|
|
154
|
+
:partition_key_length,
|
|
155
|
+
value ? :partition_key_bytesize : :partition_key_size
|
|
156
|
+
)
|
|
157
|
+
end
|
|
158
|
+
|
|
92
159
|
# @private
|
|
93
160
|
def self.opaques
|
|
94
161
|
@@opaques
|
|
95
162
|
end
|
|
96
163
|
|
|
97
164
|
# Default config that can be overwritten.
|
|
98
|
-
DEFAULT_CONFIG = {
|
|
99
|
-
# Request api version so advanced features work
|
|
100
|
-
:"api.version.request" => true
|
|
101
|
-
}.freeze
|
|
165
|
+
DEFAULT_CONFIG = {}.freeze
|
|
102
166
|
|
|
103
167
|
# Required config that cannot be overwritten.
|
|
104
168
|
REQUIRED_CONFIG = {
|
|
105
169
|
# Enable log queues so we get callbacks in our own Ruby threads
|
|
106
|
-
|
|
170
|
+
"log.queue": true
|
|
107
171
|
}.freeze
|
|
108
172
|
|
|
109
173
|
# Returns a new config with the provided options which are merged with {DEFAULT_CONFIG}.
|
|
@@ -112,8 +176,11 @@ module Rdkafka
|
|
|
112
176
|
#
|
|
113
177
|
# @return [Config]
|
|
114
178
|
def initialize(config_hash = {})
|
|
179
|
+
Callbacks.ensure_ffi_running
|
|
180
|
+
|
|
115
181
|
@config_hash = DEFAULT_CONFIG.merge(config_hash)
|
|
116
182
|
@consumer_rebalance_listener = nil
|
|
183
|
+
@consumer_poll_set = true
|
|
117
184
|
end
|
|
118
185
|
|
|
119
186
|
# Set a config option.
|
|
@@ -137,43 +204,69 @@ module Rdkafka
|
|
|
137
204
|
|
|
138
205
|
# Get notifications on partition assignment/revocation for the subscribed topics
|
|
139
206
|
#
|
|
140
|
-
# @
|
|
141
|
-
|
|
142
|
-
@consumer_rebalance_listener = listener
|
|
143
|
-
end
|
|
207
|
+
# @return [Object, #on_partitions_assigned, #on_partitions_revoked] listener instance
|
|
208
|
+
attr_writer :consumer_rebalance_listener
|
|
144
209
|
|
|
145
|
-
#
|
|
210
|
+
# Should we use a single queue for the underlying consumer and events.
|
|
146
211
|
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
212
|
+
# This is an advanced API that allows for more granular control of the polling process.
|
|
213
|
+
# When this value is set to `false` (`true` by defualt), there will be two queues that need to
|
|
214
|
+
# be polled:
|
|
215
|
+
# - main librdkafka queue for events
|
|
216
|
+
# - consumer queue with messages and rebalances
|
|
217
|
+
#
|
|
218
|
+
# It is recommended to use the defaults and only set it to `false` in advance multi-threaded
|
|
219
|
+
# and complex cases where granular events handling control is needed.
|
|
149
220
|
#
|
|
221
|
+
# @return [Boolean]
|
|
222
|
+
attr_writer :consumer_poll_set
|
|
223
|
+
|
|
224
|
+
# Creates a consumer with this configuration.
|
|
225
|
+
#
|
|
226
|
+
# @param native_kafka_auto_start [Boolean] should the native kafka operations be started
|
|
227
|
+
# automatically. Defaults to true. Set to false only when doing complex initialization.
|
|
150
228
|
# @return [Consumer] The created consumer
|
|
151
|
-
|
|
229
|
+
#
|
|
230
|
+
# @raise [ConfigError] When the configuration contains invalid options
|
|
231
|
+
# @raise [ClientCreationError] When the native client cannot be created
|
|
232
|
+
def consumer(native_kafka_auto_start: true)
|
|
152
233
|
opaque = Opaque.new
|
|
153
234
|
config = native_config(opaque)
|
|
154
235
|
|
|
236
|
+
# Set callback to receive background events, so admin-style operations issued on the
|
|
237
|
+
# consumer handle (e.g. Consumer#list_offsets) get their results dispatched back to their
|
|
238
|
+
# handles. librdkafka spawns its internally-managed background thread because of this.
|
|
239
|
+
Rdkafka::Bindings.rd_kafka_conf_set_background_event_cb(config, Rdkafka::Callbacks::BackgroundEventCallbackFunction)
|
|
240
|
+
|
|
155
241
|
if @consumer_rebalance_listener
|
|
156
242
|
opaque.consumer_rebalance_listener = @consumer_rebalance_listener
|
|
157
243
|
Rdkafka::Bindings.rd_kafka_conf_set_rebalance_cb(config, Rdkafka::Bindings::RebalanceCallback)
|
|
158
244
|
end
|
|
159
245
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
246
|
+
build_native_client(
|
|
247
|
+
config,
|
|
248
|
+
:rd_kafka_consumer,
|
|
249
|
+
opaque: opaque,
|
|
250
|
+
run_polling_thread: false,
|
|
251
|
+
auto_start: native_kafka_auto_start
|
|
252
|
+
) do |kafka, native_kafka|
|
|
253
|
+
Rdkafka::Bindings.rd_kafka_poll_set_consumer(kafka) if @consumer_poll_set
|
|
254
|
+
Rdkafka::Consumer.new(native_kafka)
|
|
255
|
+
end
|
|
168
256
|
end
|
|
169
257
|
|
|
170
258
|
# Create a producer with this configuration.
|
|
171
259
|
#
|
|
260
|
+
# @param native_kafka_auto_start [Boolean] should the native kafka operations be started
|
|
261
|
+
# automatically. Defaults to true. Set to false only when doing complex initialization.
|
|
262
|
+
# @param native_kafka_poll_timeout_ms [Integer] ms poll time of the native Kafka
|
|
263
|
+
# @param run_polling_thread [Boolean] should the background polling thread be started.
|
|
264
|
+
# Defaults to true. Set to false when using the FD API for fiber scheduler integration.
|
|
265
|
+
# @return [Producer] The created producer
|
|
266
|
+
#
|
|
172
267
|
# @raise [ConfigError] When the configuration contains invalid options
|
|
173
268
|
# @raise [ClientCreationError] When the native client cannot be created
|
|
174
|
-
|
|
175
|
-
# @return [Producer] The created producer
|
|
176
|
-
def producer
|
|
269
|
+
def producer(native_kafka_auto_start: true, native_kafka_poll_timeout_ms: Defaults::NATIVE_KAFKA_POLL_TIMEOUT_MS, run_polling_thread: true)
|
|
177
270
|
# Create opaque
|
|
178
271
|
opaque = Opaque.new
|
|
179
272
|
# Create Kafka config
|
|
@@ -182,25 +275,90 @@ module Rdkafka
|
|
|
182
275
|
Rdkafka::Bindings.rd_kafka_conf_set_dr_msg_cb(config, Rdkafka::Callbacks::DeliveryCallbackFunction)
|
|
183
276
|
# Return producer with Kafka client
|
|
184
277
|
partitioner_name = self[:partitioner] || self["partitioner"]
|
|
185
|
-
|
|
186
|
-
|
|
278
|
+
|
|
279
|
+
build_native_client(
|
|
280
|
+
config,
|
|
281
|
+
:rd_kafka_producer,
|
|
282
|
+
opaque: opaque,
|
|
283
|
+
run_polling_thread: run_polling_thread,
|
|
284
|
+
auto_start: native_kafka_auto_start,
|
|
285
|
+
timeout_ms: native_kafka_poll_timeout_ms
|
|
286
|
+
) do |_kafka, native_kafka|
|
|
287
|
+
Rdkafka::Producer.new(native_kafka, partitioner_name).tap do |producer|
|
|
288
|
+
opaque.producer = producer
|
|
289
|
+
end
|
|
187
290
|
end
|
|
188
291
|
end
|
|
189
292
|
|
|
190
|
-
#
|
|
293
|
+
# Creates an admin instance with this configuration.
|
|
294
|
+
#
|
|
295
|
+
# @param native_kafka_auto_start [Boolean] should the native kafka operations be started
|
|
296
|
+
# automatically. Defaults to true. Set to false only when doing complex initialization.
|
|
297
|
+
# @param native_kafka_poll_timeout_ms [Integer] ms poll time of the native Kafka
|
|
298
|
+
# @param run_polling_thread [Boolean] should the background polling thread be started.
|
|
299
|
+
# Defaults to true. Set to false when using the FD API for fiber scheduler integration.
|
|
300
|
+
# @return [Admin] The created admin instance
|
|
191
301
|
#
|
|
192
302
|
# @raise [ConfigError] When the configuration contains invalid options
|
|
193
303
|
# @raise [ClientCreationError] When the native client cannot be created
|
|
194
|
-
|
|
195
|
-
# @return [Admin] The created admin instance
|
|
196
|
-
def admin
|
|
304
|
+
def admin(native_kafka_auto_start: true, native_kafka_poll_timeout_ms: Defaults::NATIVE_KAFKA_POLL_TIMEOUT_MS, run_polling_thread: true)
|
|
197
305
|
opaque = Opaque.new
|
|
198
306
|
config = native_config(opaque)
|
|
199
307
|
Rdkafka::Bindings.rd_kafka_conf_set_background_event_cb(config, Rdkafka::Callbacks::BackgroundEventCallbackFunction)
|
|
200
|
-
|
|
308
|
+
|
|
309
|
+
build_native_client(
|
|
310
|
+
config,
|
|
311
|
+
:rd_kafka_producer,
|
|
312
|
+
opaque: opaque,
|
|
313
|
+
run_polling_thread: run_polling_thread,
|
|
314
|
+
auto_start: native_kafka_auto_start,
|
|
315
|
+
timeout_ms: native_kafka_poll_timeout_ms
|
|
316
|
+
) do |_kafka, native_kafka|
|
|
317
|
+
Rdkafka::Admin.new(native_kafka)
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Returns all configuration properties and their current values for this config.
|
|
322
|
+
#
|
|
323
|
+
# Uses `rd_kafka_conf_dump` to retrieve every property (including defaults and
|
|
324
|
+
# internal properties like `client.software.name`) as a flat Hash.
|
|
325
|
+
#
|
|
326
|
+
# @return [Hash{Symbol => String}] property names mapped to their current values
|
|
327
|
+
#
|
|
328
|
+
# @raise [ConfigError] When the configuration contains invalid options
|
|
329
|
+
# @note The librdkafka C API does not distinguish between producer-only, consumer-only,
|
|
330
|
+
# and global properties at the configuration level. All properties are returned
|
|
331
|
+
# regardless of the intended client type.
|
|
332
|
+
#
|
|
333
|
+
# @note The returned Hash may include sensitive values such as authentication
|
|
334
|
+
# credentials and key passwords. Do not log or serialize the returned data
|
|
335
|
+
# unless you have explicitly redacted secret entries.
|
|
336
|
+
def describe_properties
|
|
337
|
+
config = nil
|
|
338
|
+
dump_ptr = nil
|
|
339
|
+
count = 0
|
|
340
|
+
|
|
341
|
+
config = native_config
|
|
342
|
+
count_ptr = Rdkafka::Bindings::SizePtr.new
|
|
343
|
+
dump_ptr = Rdkafka::Bindings.rd_kafka_conf_dump(config, count_ptr)
|
|
344
|
+
|
|
345
|
+
count = count_ptr[:value]
|
|
346
|
+
result = {}
|
|
347
|
+
|
|
348
|
+
(0...count).step(2) do |i|
|
|
349
|
+
key = dump_ptr.get_pointer(i * FFI::Pointer.size).read_string
|
|
350
|
+
value = dump_ptr.get_pointer((i + 1) * FFI::Pointer.size).read_string
|
|
351
|
+
result[key.to_sym] = value
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
result
|
|
355
|
+
ensure
|
|
356
|
+
Rdkafka::Bindings.rd_kafka_conf_dump_free(dump_ptr, count) if dump_ptr
|
|
357
|
+
Rdkafka::Bindings.rd_kafka_conf_destroy(config) if config
|
|
201
358
|
end
|
|
202
359
|
|
|
203
|
-
# Error that is returned by the underlying rdkafka error if an invalid configuration option is
|
|
360
|
+
# Error that is returned by the underlying rdkafka error if an invalid configuration option is
|
|
361
|
+
# present.
|
|
204
362
|
class ConfigError < RuntimeError; end
|
|
205
363
|
|
|
206
364
|
# Error that is returned by the underlying rdkafka library if the client cannot be created.
|
|
@@ -211,47 +369,118 @@ module Rdkafka
|
|
|
211
369
|
|
|
212
370
|
private
|
|
213
371
|
|
|
372
|
+
# Builds a public client around a native Kafka handle, destroying the current owner if setup
|
|
373
|
+
# fails before ownership is transferred to the caller.
|
|
374
|
+
# @param config [FFI::Pointer] native rdkafka configuration
|
|
375
|
+
# @param type [Symbol] native client type
|
|
376
|
+
# @param opaque [Object] callback context retained for the native client
|
|
377
|
+
# @param run_polling_thread [Boolean] whether to run a Ruby polling thread
|
|
378
|
+
# @param auto_start [Boolean] whether to start the polling thread after wrapping the client
|
|
379
|
+
# @param timeout_ms [Integer] polling timeout in milliseconds
|
|
380
|
+
# @yieldparam kafka [FFI::Pointer] native Kafka handle
|
|
381
|
+
# @yieldparam native_kafka [NativeKafka] Ruby owner of the native handle
|
|
382
|
+
# @return [Consumer, Producer, Admin] the constructed public client
|
|
383
|
+
# @private
|
|
384
|
+
def build_native_client(
|
|
385
|
+
config,
|
|
386
|
+
type,
|
|
387
|
+
opaque:,
|
|
388
|
+
run_polling_thread:,
|
|
389
|
+
auto_start:,
|
|
390
|
+
timeout_ms: Defaults::NATIVE_KAFKA_POLL_TIMEOUT_MS
|
|
391
|
+
)
|
|
392
|
+
kafka = native_kafka(config, type)
|
|
393
|
+
native_kafka = nil
|
|
394
|
+
client = nil
|
|
395
|
+
|
|
396
|
+
begin
|
|
397
|
+
native_kafka = Rdkafka::NativeKafka.new(
|
|
398
|
+
kafka,
|
|
399
|
+
run_polling_thread: run_polling_thread,
|
|
400
|
+
opaque: opaque,
|
|
401
|
+
auto_start: false,
|
|
402
|
+
timeout_ms: timeout_ms
|
|
403
|
+
)
|
|
404
|
+
client = yield(kafka, native_kafka)
|
|
405
|
+
native_kafka.start if auto_start
|
|
406
|
+
Rdkafka::Clients.register(client)
|
|
407
|
+
rescue Exception
|
|
408
|
+
owner = client || native_kafka
|
|
409
|
+
|
|
410
|
+
begin
|
|
411
|
+
owner ? owner.close : Rdkafka::Bindings.rd_kafka_destroy(kafka)
|
|
412
|
+
rescue Exception => err
|
|
413
|
+
# Never let a teardown failure replace the error that caused it: that error is what the
|
|
414
|
+
# caller needs to see, and it is re-raised below regardless.
|
|
415
|
+
Rdkafka::Config.logger.error("Unhandled exception: #{err.class} - #{err.message}")
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
raise
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
214
422
|
# This method is only intended to be used to create a client,
|
|
215
423
|
# using it in another way will leak memory.
|
|
424
|
+
#
|
|
425
|
+
# @param opaque [Object, nil] optional opaque pointer for callbacks
|
|
426
|
+
# @return [FFI::Pointer] native rdkafka configuration pointer
|
|
216
427
|
def native_config(opaque = nil)
|
|
217
|
-
Rdkafka::Bindings.rd_kafka_conf_new
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
428
|
+
config = Rdkafka::Bindings.rd_kafka_conf_new
|
|
429
|
+
|
|
430
|
+
# Create config
|
|
431
|
+
@config_hash.merge(REQUIRED_CONFIG).each do |key, value|
|
|
432
|
+
error_buffer = FFI::MemoryPointer.from_string(" " * 256)
|
|
433
|
+
result = Rdkafka::Bindings.rd_kafka_conf_set(
|
|
434
|
+
config,
|
|
435
|
+
key.to_s,
|
|
436
|
+
value.to_s,
|
|
437
|
+
error_buffer,
|
|
438
|
+
256
|
|
439
|
+
)
|
|
440
|
+
unless result == :config_ok
|
|
441
|
+
raise ConfigError.new(error_buffer.read_string)
|
|
231
442
|
end
|
|
443
|
+
end
|
|
232
444
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
445
|
+
# Set opaque pointer that's used as a proxy for callbacks
|
|
446
|
+
if opaque
|
|
447
|
+
pointer = ::FFI::Pointer.new(:pointer, opaque.object_id)
|
|
448
|
+
Rdkafka::Bindings.rd_kafka_conf_set_opaque(config, pointer)
|
|
237
449
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
450
|
+
# Store opaque with the pointer as key. We use this approach instead
|
|
451
|
+
# of trying to convert the pointer to a Ruby object because there is
|
|
452
|
+
# no risk of a segfault this way.
|
|
453
|
+
Rdkafka::Config.opaques[pointer.to_i] = opaque
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
# Set log callback
|
|
457
|
+
Rdkafka::Bindings.rd_kafka_conf_set_log_cb(config, Rdkafka::Bindings::LogCallback)
|
|
243
458
|
|
|
244
|
-
|
|
245
|
-
|
|
459
|
+
# Set stats callback
|
|
460
|
+
Rdkafka::Bindings.rd_kafka_conf_set_stats_cb(config, Rdkafka::Bindings::StatsCallback)
|
|
246
461
|
|
|
247
|
-
|
|
248
|
-
|
|
462
|
+
# Set error callback
|
|
463
|
+
Rdkafka::Bindings.rd_kafka_conf_set_error_cb(config, Rdkafka::Bindings::ErrorCallback)
|
|
249
464
|
|
|
250
|
-
|
|
251
|
-
|
|
465
|
+
# Set oauth callback
|
|
466
|
+
if Rdkafka::Config.oauthbearer_token_refresh_callback
|
|
467
|
+
Rdkafka::Bindings.rd_kafka_conf_set_oauthbearer_token_refresh_cb(config, Rdkafka::Bindings::OAuthbearerTokenRefreshCallback)
|
|
252
468
|
end
|
|
469
|
+
|
|
470
|
+
config
|
|
471
|
+
rescue Exception
|
|
472
|
+
# rd_kafka_conf_new allocates a conf the caller owns until it is handed to rd_kafka_new. If
|
|
473
|
+
# we raise before returning it (e.g. an invalid option in the loop above), destroy it here
|
|
474
|
+
# so the native config is not leaked.
|
|
475
|
+
Rdkafka::Bindings.rd_kafka_conf_destroy(config) if config
|
|
476
|
+
raise
|
|
253
477
|
end
|
|
254
478
|
|
|
479
|
+
# Creates a native Kafka handle
|
|
480
|
+
# @param config [FFI::Pointer] pointer to the native config
|
|
481
|
+
# @param type [Symbol] type of client (:rd_kafka_producer or :rd_kafka_consumer)
|
|
482
|
+
# @return [FFI::Pointer] pointer to the native Kafka handle
|
|
483
|
+
# @private
|
|
255
484
|
def native_kafka(config, type)
|
|
256
485
|
error_buffer = FFI::MemoryPointer.from_string(" " * 256)
|
|
257
486
|
handle = Rdkafka::Bindings.rd_kafka_new(
|
|
@@ -262,17 +491,23 @@ module Rdkafka
|
|
|
262
491
|
)
|
|
263
492
|
|
|
264
493
|
if handle.null?
|
|
494
|
+
# On success rd_kafka_new takes ownership of the conf and frees it itself; on failure
|
|
495
|
+
# librdkafka keeps application ownership (see rdkafka.c), so we must destroy it here to
|
|
496
|
+
# avoid leaking the conf on every failed client creation.
|
|
497
|
+
Rdkafka::Bindings.rd_kafka_conf_destroy(config)
|
|
265
498
|
raise ClientCreationError.new(error_buffer.read_string)
|
|
266
499
|
end
|
|
267
500
|
|
|
268
501
|
# Redirect log to handle's queue
|
|
269
|
-
Rdkafka::Bindings.
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
)
|
|
502
|
+
main_queue = Rdkafka::Bindings.rd_kafka_queue_get_main(handle)
|
|
503
|
+
Rdkafka::Bindings.rd_kafka_set_log_queue(handle, main_queue)
|
|
504
|
+
Rdkafka::Bindings.rd_kafka_queue_destroy(main_queue)
|
|
273
505
|
|
|
274
506
|
# Return handle which should be closed using rd_kafka_destroy after usage.
|
|
275
507
|
handle
|
|
508
|
+
rescue Exception
|
|
509
|
+
Rdkafka::Bindings.rd_kafka_destroy(handle) if handle && !handle.null?
|
|
510
|
+
raise
|
|
276
511
|
end
|
|
277
512
|
end
|
|
278
513
|
|
|
@@ -281,10 +516,15 @@ module Rdkafka
|
|
|
281
516
|
attr_accessor :producer
|
|
282
517
|
attr_accessor :consumer_rebalance_listener
|
|
283
518
|
|
|
519
|
+
# Invokes the delivery callback on the producer if one is set
|
|
520
|
+
# @param delivery_report [Rdkafka::Producer::DeliveryReport] the delivery report
|
|
521
|
+
# @param delivery_handle [Rdkafka::Producer::DeliveryHandle] the delivery handle
|
|
284
522
|
def call_delivery_callback(delivery_report, delivery_handle)
|
|
285
|
-
producer
|
|
523
|
+
producer&.call_delivery_callback(delivery_report, delivery_handle)
|
|
286
524
|
end
|
|
287
525
|
|
|
526
|
+
# Invokes the on_partitions_assigned callback on the rebalance listener if set
|
|
527
|
+
# @param list [Rdkafka::Consumer::TopicPartitionList] the assigned partitions
|
|
288
528
|
def call_on_partitions_assigned(list)
|
|
289
529
|
return unless consumer_rebalance_listener
|
|
290
530
|
return unless consumer_rebalance_listener.respond_to?(:on_partitions_assigned)
|
|
@@ -292,6 +532,8 @@ module Rdkafka
|
|
|
292
532
|
consumer_rebalance_listener.on_partitions_assigned(list)
|
|
293
533
|
end
|
|
294
534
|
|
|
535
|
+
# Invokes the on_partitions_revoked callback on the rebalance listener if set
|
|
536
|
+
# @param list [Rdkafka::Consumer::TopicPartitionList] the revoked partitions
|
|
295
537
|
def call_on_partitions_revoked(list)
|
|
296
538
|
return unless consumer_rebalance_listener
|
|
297
539
|
return unless consumer_rebalance_listener.respond_to?(:on_partitions_revoked)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
class Consumer
|
|
5
|
+
# Creates FFI pointers from the process-global allocator so that memory handed to librdkafka
|
|
6
|
+
# can be released by it with the matching `free`.
|
|
7
|
+
module Allocator
|
|
8
|
+
extend FFI::Library
|
|
9
|
+
|
|
10
|
+
ffi_lib FFI::CURRENT_PROCESS # use process-global malloc not FFI::Library::LIBC
|
|
11
|
+
attach_function :malloc, [:size_t], :pointer
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Return an FFI pointer to a string allocated from the global allocator
|
|
15
|
+
# so that it can be released by another library.
|
|
16
|
+
#
|
|
17
|
+
# @private
|
|
18
|
+
#
|
|
19
|
+
# @param string [String] Ruby string that will be copied into the new
|
|
20
|
+
# string pointer.
|
|
21
|
+
#
|
|
22
|
+
# @return [FFI::Pointer]
|
|
23
|
+
def string_pointer(string)
|
|
24
|
+
bytes = string.to_s
|
|
25
|
+
pointer = malloc(bytes.bytesize + 1)
|
|
26
|
+
Kernel.raise(NoMemoryError, "malloc failed") if pointer.null?
|
|
27
|
+
|
|
28
|
+
pointer.put_bytes(0, bytes)
|
|
29
|
+
pointer.put_char(bytes.bytesize, 0) # NUL terminator
|
|
30
|
+
|
|
31
|
+
pointer.autorelease = false
|
|
32
|
+
|
|
33
|
+
pointer
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|