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.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +388 -111
  3. data/{LICENSE → MIT-LICENSE} +2 -1
  4. data/README.md +90 -43
  5. data/Rakefile +21 -23
  6. data/bin/verify_kafka_warnings +44 -0
  7. data/dist/librdkafka-2.15.1.tar.gz +0 -0
  8. data/dist/patches/rdkafka_global_init.patch +15 -0
  9. data/docker-compose-ssl.yml +39 -0
  10. data/docker-compose.yml +22 -17
  11. data/ext/Rakefile +97 -30
  12. data/lib/rdkafka/abstract_handle.rb +113 -34
  13. data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
  14. data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
  15. data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
  16. data/lib/rdkafka/admin/config_binding_result.rb +30 -0
  17. data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
  18. data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
  19. data/lib/rdkafka/admin/create_acl_report.rb +26 -0
  20. data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
  21. data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
  22. data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
  23. data/lib/rdkafka/admin/create_topic_report.rb +4 -1
  24. data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
  25. data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
  26. data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
  27. data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
  28. data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
  29. data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
  30. data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
  31. data/lib/rdkafka/admin/delete_records_report.rb +25 -0
  32. data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
  33. data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
  34. data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
  35. data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
  36. data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
  37. data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
  38. data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
  39. data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
  40. data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
  41. data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
  42. data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
  43. data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
  44. data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
  45. data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
  46. data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
  47. data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
  48. data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
  49. data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
  50. data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
  51. data/lib/rdkafka/admin.rb +1381 -38
  52. data/lib/rdkafka/bindings/admin/acls.rb +85 -0
  53. data/lib/rdkafka/bindings/admin/configs.rb +37 -0
  54. data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
  55. data/lib/rdkafka/bindings/admin.rb +197 -0
  56. data/lib/rdkafka/bindings/callbacks.rb +100 -0
  57. data/lib/rdkafka/bindings/configuration.rb +50 -0
  58. data/lib/rdkafka/bindings/consumer.rb +83 -0
  59. data/lib/rdkafka/bindings/errors.rb +15 -0
  60. data/lib/rdkafka/bindings/handle.rb +16 -0
  61. data/lib/rdkafka/bindings/messages.rb +28 -0
  62. data/lib/rdkafka/bindings/metadata.rb +14 -0
  63. data/lib/rdkafka/bindings/polling.rb +17 -0
  64. data/lib/rdkafka/bindings/producer.rb +77 -0
  65. data/lib/rdkafka/bindings/structs.rb +62 -0
  66. data/lib/rdkafka/bindings.rb +69 -294
  67. data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
  68. data/lib/rdkafka/callbacks/base_handler.rb +62 -0
  69. data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
  70. data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
  71. data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
  72. data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
  73. data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
  74. data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
  75. data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
  76. data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
  77. data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
  78. data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
  79. data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
  80. data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
  81. data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
  82. data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
  83. data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
  84. data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
  85. data/lib/rdkafka/callbacks.rb +273 -52
  86. data/lib/rdkafka/clients.rb +81 -0
  87. data/lib/rdkafka/config.rb +333 -91
  88. data/lib/rdkafka/consumer/allocator.rb +38 -0
  89. data/lib/rdkafka/consumer/headers.rb +39 -25
  90. data/lib/rdkafka/consumer/message.rb +18 -20
  91. data/lib/rdkafka/consumer/partition.rb +15 -4
  92. data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
  93. data/lib/rdkafka/consumer.rb +765 -192
  94. data/lib/rdkafka/defaults.rb +123 -0
  95. data/lib/rdkafka/error.rb +40 -14
  96. data/lib/rdkafka/helpers/list_offsets.rb +127 -0
  97. data/lib/rdkafka/helpers/metadata.rb +29 -0
  98. data/lib/rdkafka/helpers/oauth.rb +95 -0
  99. data/lib/rdkafka/helpers/time.rb +19 -0
  100. data/lib/rdkafka/metadata.rb +138 -26
  101. data/lib/rdkafka/native_kafka.rb +142 -22
  102. data/lib/rdkafka/producer/delivery_handle.rb +27 -7
  103. data/lib/rdkafka/producer/delivery_report.rb +71 -4
  104. data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
  105. data/lib/rdkafka/producer.rb +422 -81
  106. data/lib/rdkafka/version.rb +6 -3
  107. data/lib/rdkafka.rb +73 -1
  108. data/package-lock.json +331 -0
  109. data/package.json +9 -0
  110. data/rdkafka.gemspec +78 -25
  111. data/renovate.json +111 -0
  112. metadata +114 -134
  113. data/.gitignore +0 -8
  114. data/.rspec +0 -1
  115. data/.semaphore/semaphore.yml +0 -27
  116. data/.yardopts +0 -2
  117. data/Gemfile +0 -5
  118. data/Guardfile +0 -19
  119. data/ext/README.md +0 -18
  120. data/spec/rdkafka/abstract_handle_spec.rb +0 -115
  121. data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
  122. data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
  123. data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
  124. data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
  125. data/spec/rdkafka/admin_spec.rb +0 -204
  126. data/spec/rdkafka/bindings_spec.rb +0 -136
  127. data/spec/rdkafka/callbacks_spec.rb +0 -22
  128. data/spec/rdkafka/config_spec.rb +0 -197
  129. data/spec/rdkafka/consumer/headers_spec.rb +0 -62
  130. data/spec/rdkafka/consumer/message_spec.rb +0 -141
  131. data/spec/rdkafka/consumer/partition_spec.rb +0 -59
  132. data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
  133. data/spec/rdkafka/consumer_spec.rb +0 -1106
  134. data/spec/rdkafka/error_spec.rb +0 -91
  135. data/spec/rdkafka/metadata_spec.rb +0 -80
  136. data/spec/rdkafka/native_kafka_spec.rb +0 -124
  137. data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
  138. data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
  139. data/spec/rdkafka/producer_spec.rb +0 -557
  140. data/spec/spec_helper.rb +0 -155
data/lib/rdkafka/admin.rb CHANGED
@@ -1,10 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "objspace"
4
-
5
3
  module Rdkafka
4
+ # Admin client for Kafka administrative operations
6
5
  class Admin
6
+ include Helpers::OAuth
7
+ include Helpers::Metadata
8
+ include Helpers::ListOffsets
9
+
10
+ class << self
11
+ # Allows us to retrieve librdkafka errors with descriptions
12
+ # Useful for debugging and building UIs, etc.
13
+ #
14
+ # @return [Hash{Integer => Hash}] hash with errors mapped by code
15
+ def describe_errors
16
+ # Memory pointers for the array of structures and count
17
+ p_error_descs = FFI::MemoryPointer.new(:pointer)
18
+ p_count = FFI::MemoryPointer.new(:size_t)
19
+
20
+ # Call the attached function
21
+ Bindings.rd_kafka_get_err_descs(p_error_descs, p_count)
22
+
23
+ # Retrieve the number of items in the array
24
+ count = p_count.read_uint
25
+
26
+ # Get the pointer to the array of error descriptions
27
+ array_of_errors = FFI::Pointer.new(Bindings::NativeErrorDesc, p_error_descs.read_pointer)
28
+
29
+ errors = {}
30
+
31
+ count.times do |i|
32
+ # Get the pointer to each struct
33
+ error_ptr = array_of_errors[i]
34
+
35
+ # Create a new instance of NativeErrorDesc for each item
36
+ error_desc = Bindings::NativeErrorDesc.new(error_ptr)
37
+
38
+ # Read values from the struct
39
+ code = error_desc[:code]
40
+
41
+ name = ""
42
+ desc = ""
43
+
44
+ name = error_desc[:name].read_string unless error_desc[:name].null?
45
+ desc = error_desc[:desc].read_string unless error_desc[:desc].null?
46
+
47
+ errors[code] = { code: code, name: name, description: desc }
48
+ end
49
+
50
+ errors
51
+ end
52
+ end
53
+
7
54
  # @private
55
+ # @param native_kafka [NativeKafka] wrapper around the native Kafka handle
8
56
  def initialize(native_kafka)
9
57
  @native_kafka = native_kafka
10
58
 
@@ -12,8 +60,75 @@ module Rdkafka
12
60
  ObjectSpace.define_finalizer(self, native_kafka.finalizer)
13
61
  end
14
62
 
15
- def finalizer
16
- ->(_) { close }
63
+ # Starts the native Kafka polling thread and kicks off the init polling
64
+ # @note Not needed to run unless explicit start was disabled
65
+ def start
66
+ @native_kafka.start
67
+ end
68
+
69
+ # @return [String] admin name
70
+ def name
71
+ @name ||= @native_kafka.with_inner do |inner|
72
+ ::Rdkafka::Bindings.rd_kafka_name(inner)
73
+ end
74
+ end
75
+
76
+ # Enable IO event notifications for fiber scheduler integration
77
+ # When admin operations complete, librdkafka will write to your FD
78
+ #
79
+ # @param fd [Integer] file descriptor to signal (from IO.pipe or eventfd)
80
+ # @param payload [String] data to write to fd (default: "\x01")
81
+ # @return [nil]
82
+ # @raise [ClosedInnerError] when the admin client is closed
83
+ def enable_queue_io_events(fd, payload = "\x01")
84
+ @native_kafka.enable_main_queue_io_events(fd, payload)
85
+ end
86
+
87
+ # Enable IO event notifications for background events
88
+ # @param fd [Integer] file descriptor to signal (from IO.pipe or eventfd)
89
+ # @param payload [String] data to write to fd (default: "\x01")
90
+ # @return [nil]
91
+ # @raise [ClosedInnerError] when the admin client is closed
92
+ def enable_background_queue_io_events(fd, payload = "\x01")
93
+ @native_kafka.enable_background_queue_io_events(fd, payload)
94
+ end
95
+
96
+ # Polls for events in a non-blocking loop, yielding the count after each iteration.
97
+ #
98
+ # This method processes events (stats, errors, etc.) in a single GVL/mutex session,
99
+ # which is more efficient than repeated individual polls. It uses non-blocking polls
100
+ # internally (no GVL release between polls).
101
+ #
102
+ # Yields the count of events processed after each poll iteration, allowing the caller
103
+ # to implement timeout or other termination logic by returning `:stop`.
104
+ #
105
+ # @yield [count] Called after each poll iteration
106
+ # @yieldparam count [Integer] Number of events processed in this iteration
107
+ # @yieldreturn [Symbol, Object] Return `:stop` to break the loop, any other value continues
108
+ # @return [nil]
109
+ # @raise [Rdkafka::ClosedAdminError] if called on a closed admin client
110
+ #
111
+ # @example Drain all pending events
112
+ # admin.events_poll_nb_each { |_count| }
113
+ #
114
+ # @example With timeout control
115
+ # deadline = monotonic_now + timeout_ms
116
+ # admin.events_poll_nb_each do |_count|
117
+ # :stop if monotonic_now >= deadline
118
+ # end
119
+ # @note This method holds the inner lock until the queue is empty or `:stop` is returned.
120
+ # Other admin operations will wait until this method returns.
121
+ # @note This method is thread-safe as it uses @native_kafka.with_inner synchronization
122
+ def events_poll_nb_each
123
+ closed_admin_check(__method__)
124
+
125
+ @native_kafka.with_inner do |inner|
126
+ loop do
127
+ count = Rdkafka::Bindings.rd_kafka_poll_nb(inner, 0)
128
+ break if count.zero?
129
+ break if yield(count) == :stop
130
+ end
131
+ end
17
132
  end
18
133
 
19
134
  # Close this admin instance
@@ -30,12 +145,16 @@ module Rdkafka
30
145
 
31
146
  # Create a topic with the given partition count and replication factor
32
147
  #
148
+ # @param topic_name [String] name of the topic to create
149
+ # @param partition_count [Integer] number of partitions for the topic
150
+ # @param replication_factor [Integer] replication factor for the topic
151
+ # @param topic_config [Hash] optional topic configuration settings
152
+ # @return [CreateTopicHandle] Create topic handle that can be used to wait for the result of
153
+ # creating the topic
33
154
  # @raise [ConfigError] When the partition count or replication factor are out of valid range
34
155
  # @raise [RdkafkaError] When the topic name is invalid or the topic already exists
35
156
  # @raise [RdkafkaError] When the topic configuration is invalid
36
- #
37
- # @return [CreateTopicHandle] Create topic handle that can be used to wait for the result of creating the topic
38
- def create_topic(topic_name, partition_count, replication_factor, topic_config={})
157
+ def create_topic(topic_name, partition_count, replication_factor, topic_config = {})
39
158
  closed_admin_check(__method__)
40
159
 
41
160
  # Create a rd_kafka_NewTopic_t representing the new topic
@@ -51,14 +170,12 @@ module Rdkafka
51
170
  raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
52
171
  end
53
172
 
54
- unless topic_config.nil?
55
- topic_config.each do |key, value|
56
- Rdkafka::Bindings.rd_kafka_NewTopic_set_config(
57
- new_topic_ptr,
58
- key.to_s,
59
- value.to_s
60
- )
61
- end
173
+ topic_config&.each do |key, value|
174
+ Rdkafka::Bindings.rd_kafka_NewTopic_set_config(
175
+ new_topic_ptr,
176
+ key.to_s,
177
+ value.to_s
178
+ )
62
179
  end
63
180
 
64
181
  # Note that rd_kafka_CreateTopics can create more than one topic at a time
@@ -78,7 +195,7 @@ module Rdkafka
78
195
  # Create and register the handle we will return to the caller
79
196
  create_topic_handle = CreateTopicHandle.new
80
197
  create_topic_handle[:pending] = true
81
- create_topic_handle[:response] = -1
198
+ create_topic_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
82
199
  CreateTopicHandle.register(create_topic_handle)
83
200
  admin_options_ptr = @native_kafka.with_inner do |inner|
84
201
  Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATETOPICS)
@@ -107,66 +224,1292 @@ module Rdkafka
107
224
  create_topic_handle
108
225
  end
109
226
 
110
- # Delete the named topic
227
+ # Sets the committed offsets of a consumer group from outside the group.
111
228
  #
112
- # @raise [RdkafkaError] When the topic name is invalid or the topic does not exist
229
+ # Kafka requires the group to be empty (no active members) for this to be accepted; when it
230
+ # is not, the broker rejects the request and the handle raises with `group_not_empty` rather
231
+ # than reporting success.
113
232
  #
114
- # @return [DeleteTopicHandle] Delete topic handle that can be used to wait for the result of deleting the topic
115
- def delete_topic(topic_name)
233
+ # @param group_id [String] the group whose offsets we want to set
234
+ # @param topic_partition_list [TopicPartitionList] partitions with the offsets to commit
235
+ # @return [AlterConsumerGroupOffsetsHandle] handle that can be used to wait for the result
236
+ # @raise [RdkafkaError] when altering the offsets fails
237
+ def alter_consumer_group_offsets(group_id, topic_partition_list)
116
238
  closed_admin_check(__method__)
117
239
 
118
- # Create a rd_kafka_DeleteTopic_t representing the topic to be deleted
119
- delete_topic_ptr = Rdkafka::Bindings.rd_kafka_DeleteTopic_new(FFI::MemoryPointer.from_string(topic_name))
240
+ native_tpl = topic_partition_list.to_native_tpl
120
241
 
121
- # Note that rd_kafka_DeleteTopics can create more than one topic at a time
122
- pointer_array = [delete_topic_ptr]
123
- topics_array_ptr = FFI::MemoryPointer.new(:pointer)
124
- topics_array_ptr.write_array_of_pointer(pointer_array)
242
+ begin
243
+ alter_ptr = Rdkafka::Bindings.rd_kafka_AlterConsumerGroupOffsets_new(
244
+ FFI::MemoryPointer.from_string(group_id),
245
+ native_tpl
246
+ )
247
+ rescue Exception
248
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(native_tpl)
249
+ raise
250
+ end
251
+
252
+ # librdkafka copies the list into the request object, so ours is no longer needed
253
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(native_tpl)
254
+
255
+ enqueue_group_offsets_op(
256
+ request_ptr: alter_ptr,
257
+ handle: AlterConsumerGroupOffsetsHandle.new,
258
+ admin_op: Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_ALTERCONSUMERGROUPOFFSETS,
259
+ destroy: Rdkafka::Bindings.method(:rd_kafka_AlterConsumerGroupOffsets_destroy),
260
+ enqueue: Rdkafka::Bindings.method(:rd_kafka_AlterConsumerGroupOffsets)
261
+ )
262
+ end
263
+
264
+ # Deletes the committed offsets of specific partitions of a consumer group, leaving the
265
+ # group and its remaining offsets in place.
266
+ #
267
+ # This is the partition scoped counterpart of {#delete_group}, which removes the whole group.
268
+ #
269
+ # @param group_id [String] the group whose offsets we want to delete
270
+ # @param topic_partition_list [TopicPartitionList] partitions whose offsets to delete
271
+ # @return [DeleteConsumerGroupOffsetsHandle] handle that can be used to wait for the result
272
+ # @raise [RdkafkaError] when deleting the offsets fails
273
+ def delete_consumer_group_offsets(group_id, topic_partition_list)
274
+ closed_admin_check(__method__)
275
+
276
+ native_tpl = topic_partition_list.to_native_tpl
277
+
278
+ begin
279
+ delete_ptr = Rdkafka::Bindings.rd_kafka_DeleteConsumerGroupOffsets_new(
280
+ FFI::MemoryPointer.from_string(group_id),
281
+ native_tpl
282
+ )
283
+ rescue Exception
284
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(native_tpl)
285
+ raise
286
+ end
287
+
288
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(native_tpl)
289
+
290
+ enqueue_group_offsets_op(
291
+ request_ptr: delete_ptr,
292
+ handle: DeleteConsumerGroupOffsetsHandle.new,
293
+ admin_op: Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETECONSUMERGROUPOFFSETS,
294
+ destroy: Rdkafka::Bindings.method(:rd_kafka_DeleteConsumerGroupOffsets_destroy),
295
+ enqueue: Rdkafka::Bindings.method(:rd_kafka_DeleteConsumerGroupOffsets)
296
+ )
297
+ end
298
+
299
+ # Deletes a consumer group
300
+ #
301
+ # @param group_id [String] the group id to delete
302
+ # @return [DeleteGroupsHandle] delete groups handle that can be used to wait for the result
303
+ # @raise [RdkafkaError] When deleting the group fails
304
+ def delete_group(group_id)
305
+ closed_admin_check(__method__)
306
+
307
+ # Create a rd_kafka_DeleteGroup_t representing the new topic
308
+ delete_groups_ptr = Rdkafka::Bindings.rd_kafka_DeleteGroup_new(
309
+ FFI::MemoryPointer.from_string(group_id)
310
+ )
311
+
312
+ pointer_array = [delete_groups_ptr]
313
+ groups_array_ptr = FFI::MemoryPointer.new(:pointer)
314
+ groups_array_ptr.write_array_of_pointer(pointer_array)
125
315
 
126
316
  # Get a pointer to the queue that our request will be enqueued on
127
317
  queue_ptr = @native_kafka.with_inner do |inner|
128
318
  Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
129
319
  end
130
320
  if queue_ptr.null?
131
- Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
321
+ Rdkafka::Bindings.rd_kafka_DeleteGroup_destroy(delete_groups_ptr)
132
322
  raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
133
323
  end
134
324
 
135
325
  # Create and register the handle we will return to the caller
136
- delete_topic_handle = DeleteTopicHandle.new
137
- delete_topic_handle[:pending] = true
138
- delete_topic_handle[:response] = -1
139
- DeleteTopicHandle.register(delete_topic_handle)
326
+ delete_groups_handle = DeleteGroupsHandle.new
327
+ delete_groups_handle[:pending] = true
328
+ delete_groups_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
329
+ DeleteGroupsHandle.register(delete_groups_handle)
140
330
  admin_options_ptr = @native_kafka.with_inner do |inner|
141
331
  Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETETOPICS)
142
332
  end
143
- Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_topic_handle.to_ptr)
333
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_groups_handle.to_ptr)
144
334
 
145
335
  begin
146
336
  @native_kafka.with_inner do |inner|
147
- Rdkafka::Bindings.rd_kafka_DeleteTopics(
337
+ Rdkafka::Bindings.rd_kafka_DeleteGroups(
148
338
  inner,
149
- topics_array_ptr,
339
+ groups_array_ptr,
150
340
  1,
151
341
  admin_options_ptr,
152
342
  queue_ptr
153
343
  )
154
344
  end
155
345
  rescue Exception
156
- DeleteTopicHandle.remove(delete_topic_handle.to_ptr.address)
346
+ DeleteGroupsHandle.remove(delete_groups_handle.to_ptr.address)
157
347
  raise
158
348
  ensure
159
349
  Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
160
350
  Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
161
- Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
351
+ Rdkafka::Bindings.rd_kafka_DeleteGroup_destroy(delete_groups_ptr)
162
352
  end
163
353
 
164
- delete_topic_handle
354
+ delete_groups_handle
165
355
  end
166
356
 
167
- private
357
+ # Deletes all messages in the given partitions up to (but not including) the given offset.
358
+ #
359
+ # The programmatic equivalent of `kafka-delete-records.sh`. Useful for GDPR/right-to-erasure
360
+ # compliance, clearing out poison messages, or retention cleanup outside of the configured
361
+ # time/size-based retention policy.
362
+ #
363
+ # @param topic_partition_offsets [Hash{String => Array<Hash>}] hash mapping topic names to
364
+ # arrays of partition delete specifications. Each specification is a hash with:
365
+ # - `:partition` [Integer] partition number
366
+ # - `:offset` [Symbol, Integer] delete all messages before this offset (exclusive) - an
367
+ # integer offset, or `:end` to delete all data currently in the partition
368
+ #
369
+ # @return [DeleteRecordsHandle] handle that can be used to wait for the result
370
+ # @raise [RdkafkaError] when deleting records fails
371
+ #
372
+ # @example Delete all messages before offset 100 in one partition, and all data in another
373
+ # report = admin.delete_records(
374
+ # "my_topic" => [
375
+ # { partition: 0, offset: 100 },
376
+ # { partition: 1, offset: :end }
377
+ # ]
378
+ # ).wait(max_wait_timeout_ms: 15_000)
379
+ #
380
+ # report.offsets.to_h
381
+ # # => { "my_topic" => [#<Partition @partition=0, @offset=100, @err=0>, ...] }
382
+ def delete_records(topic_partition_offsets)
383
+ closed_admin_check(__method__)
384
+
385
+ parsed = topic_partition_offsets.flat_map do |topic, partitions|
386
+ partitions.map do |spec|
387
+ offset = spec.fetch(:offset)
388
+
389
+ native_offset = case offset
390
+ when :end then Rdkafka::Bindings::RD_KAFKA_OFFSET_END
391
+ when Integer then offset
392
+ else
393
+ raise ArgumentError, "Unknown offset specification: #{offset.inspect}"
394
+ end
395
+
396
+ [topic, spec.fetch(:partition), native_offset]
397
+ end
398
+ end
399
+
400
+ tpl = Rdkafka::Bindings.rd_kafka_topic_partition_list_new(parsed.size)
401
+
402
+ parsed.each do |topic, partition, native_offset|
403
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_add(tpl, topic, partition)
404
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_set_offset(tpl, topic, partition, native_offset)
405
+ end
406
+
407
+ # rd_kafka_DeleteRecords_new copies the tpl it is given, so it does not need to (and must
408
+ # not) outlive this call - it is destroyed below, independently of the wrapping
409
+ # DeleteRecords_t object.
410
+ delete_records_ptr = Rdkafka::Bindings.rd_kafka_DeleteRecords_new(tpl)
411
+ Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl)
412
+
413
+ pointer_array = [delete_records_ptr]
414
+ records_array_ptr = FFI::MemoryPointer.new(:pointer)
415
+ records_array_ptr.write_array_of_pointer(pointer_array)
416
+
417
+ # Get a pointer to the queue that our request will be enqueued on
418
+ queue_ptr = @native_kafka.with_inner do |inner|
419
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
420
+ end
421
+ if queue_ptr.null?
422
+ Rdkafka::Bindings.rd_kafka_DeleteRecords_destroy(delete_records_ptr)
423
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
424
+ end
425
+
426
+ # Create and register the handle we will return to the caller
427
+ handle = DeleteRecordsHandle.new
428
+ handle[:pending] = true
429
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
430
+ DeleteRecordsHandle.register(handle)
431
+
432
+ admin_options_ptr = @native_kafka.with_inner do |inner|
433
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETERECORDS)
434
+ end
435
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
436
+
437
+ begin
438
+ @native_kafka.with_inner do |inner|
439
+ Rdkafka::Bindings.rd_kafka_DeleteRecords(
440
+ inner,
441
+ records_array_ptr,
442
+ 1,
443
+ admin_options_ptr,
444
+ queue_ptr
445
+ )
446
+ end
447
+ rescue Exception
448
+ DeleteRecordsHandle.remove(handle.to_ptr.address)
449
+ raise
450
+ ensure
451
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
452
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
453
+ Rdkafka::Bindings.rd_kafka_DeleteRecords_destroy(delete_records_ptr)
454
+ end
455
+
456
+ handle
457
+ end
458
+
459
+ # Lists consumer groups cluster-wide.
460
+ #
461
+ # librdkafka issues a single `ListConsumerGroups` request that is fanned out to every broker
462
+ # internally, so the result covers all consumer groups in the cluster, not only those
463
+ # coordinated by the connected broker.
464
+ #
465
+ # @return [ListConsumerGroupsHandle] handle that can be used to wait for the result
466
+ # @raise [RdkafkaError] when listing the consumer groups fails
467
+ #
468
+ # @example List every consumer group in the cluster and print its name and attributes
469
+ # report = admin.list_consumer_groups.wait(max_wait_timeout_ms: 15_000)
470
+ #
471
+ # report.groups.each do |group|
472
+ # puts "#{group[:group_id]} - #{group[:state_name]} " \
473
+ # "(simple: #{group[:is_simple_consumer_group]})"
474
+ # end
475
+ #
476
+ # # Any brokers that could not be reached are reported separately
477
+ # report.errors.each { |error| warn "partial listing error: #{error.message}" }
478
+ def list_consumer_groups
479
+ closed_admin_check(__method__)
480
+
481
+ # Get a pointer to the queue that our request will be enqueued on
482
+ queue_ptr = @native_kafka.with_inner do |inner|
483
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
484
+ end
485
+
486
+ if queue_ptr.null?
487
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
488
+ end
489
+
490
+ # Create and register the handle we will return to the caller
491
+ handle = ListConsumerGroupsHandle.new
492
+ handle[:pending] = true
493
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
494
+ ListConsumerGroupsHandle.register(handle)
495
+
496
+ admin_options_ptr = @native_kafka.with_inner do |inner|
497
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
498
+ inner,
499
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_LISTCONSUMERGROUPS
500
+ )
501
+ end
502
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
503
+
504
+ begin
505
+ @native_kafka.with_inner do |inner|
506
+ Rdkafka::Bindings.rd_kafka_ListConsumerGroups(
507
+ inner,
508
+ admin_options_ptr,
509
+ queue_ptr
510
+ )
511
+ end
512
+ rescue Exception
513
+ ListConsumerGroupsHandle.remove(handle.to_ptr.address)
514
+ raise
515
+ ensure
516
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
517
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
518
+ end
519
+
520
+ handle
521
+ end
522
+
523
+ # Describes the cluster: its id, the current controller and the broker nodes (with racks).
524
+ #
525
+ # Unlike {#metadata}, this does not fetch any topic metadata.
526
+ #
527
+ # @param include_authorized_operations [Boolean] when true, also return the operations this
528
+ # client is authorized to perform on the cluster
529
+ # @return [DescribeClusterHandle] handle that can be used to wait for the result
530
+ # @raise [RdkafkaError] when describing the cluster fails
531
+ #
532
+ # @example Print the cluster id, the controller and every broker
533
+ # report = admin.describe_cluster.wait(max_wait_timeout_ms: 15_000)
534
+ #
535
+ # puts "cluster: #{report.cluster_id}, controller: #{report.controller[:id]}"
536
+ # report.nodes.each do |node|
537
+ # puts "#{node[:id]} #{node[:host]}:#{node[:port]} #{node[:rack]}"
538
+ # end
539
+ def describe_cluster(include_authorized_operations: false)
540
+ closed_admin_check(__method__)
541
+
542
+ queue_ptr = @native_kafka.with_inner do |inner|
543
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
544
+ end
545
+
546
+ if queue_ptr.null?
547
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
548
+ end
549
+
550
+ handle = DescribeClusterHandle.new
551
+ handle[:pending] = true
552
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
553
+ DescribeClusterHandle.register(handle)
554
+
555
+ admin_options_ptr = @native_kafka.with_inner do |inner|
556
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
557
+ inner,
558
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBECLUSTER
559
+ )
560
+ end
561
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
562
+
563
+ begin
564
+ set_include_authorized_operations(admin_options_ptr) if include_authorized_operations
565
+
566
+ @native_kafka.with_inner do |inner|
567
+ Rdkafka::Bindings.rd_kafka_DescribeCluster(
568
+ inner,
569
+ admin_options_ptr,
570
+ queue_ptr
571
+ )
572
+ end
573
+ rescue Exception
574
+ DescribeClusterHandle.remove(handle.to_ptr.address)
575
+ raise
576
+ ensure
577
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
578
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
579
+ end
580
+
581
+ handle
582
+ end
583
+
584
+ # Describes the given topics: their topic ids, partitions (leader, replicas and in-sync
585
+ # replicas, with racks) and, optionally, the operations this client is authorized to perform.
586
+ #
587
+ # A topic that cannot be described (e.g. it does not exist) does not fail the whole request;
588
+ # its entry in the report carries an `:error` instead.
589
+ #
590
+ # @param topic_names [Array<String>] names of the topics to describe
591
+ # @param include_authorized_operations [Boolean] when true, also return the operations this
592
+ # client is authorized to perform on each topic
593
+ # @return [DescribeTopicsHandle] handle that can be used to wait for the result
594
+ # @raise [ArgumentError] when topic_names is not an Array of Strings
595
+ # @raise [RdkafkaError] when describing the topics fails
596
+ #
597
+ # @example Print the topic id and partition leaders of a topic
598
+ # report = admin.describe_topics(["events"]).wait(max_wait_timeout_ms: 15_000)
599
+ #
600
+ # report.topics.each do |topic|
601
+ # next warn("#{topic[:name]}: #{topic[:error].message}") if topic[:error]
602
+ #
603
+ # puts "#{topic[:name]} (#{topic[:topic_id]})"
604
+ # topic[:partitions].each do |partition|
605
+ # puts " #{partition[:partition]} -> #{partition[:leader]&.fetch(:id)}"
606
+ # end
607
+ # end
608
+ def describe_topics(topic_names, include_authorized_operations: false)
609
+ closed_admin_check(__method__)
610
+
611
+ unless topic_names.is_a?(Array) && topic_names.all?(String)
612
+ raise ArgumentError, "topic_names must be an Array of Strings"
613
+ end
614
+
615
+ queue_ptr = @native_kafka.with_inner do |inner|
616
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
617
+ end
618
+
619
+ if queue_ptr.null?
620
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
621
+ end
622
+
623
+ handle = DescribeTopicsHandle.new
624
+ handle[:pending] = true
625
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
626
+ DescribeTopicsHandle.register(handle)
627
+
628
+ admin_options_ptr = @native_kafka.with_inner do |inner|
629
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
630
+ inner,
631
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBETOPICS
632
+ )
633
+ end
634
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
635
+
636
+ topic_collection_ptr = nil
637
+
638
+ begin
639
+ set_include_authorized_operations(admin_options_ptr) if include_authorized_operations
640
+
641
+ # librdkafka copies the names, so the Ruby-owned strings only need to outlive this call
642
+ name_ptrs = topic_names.map { |name| FFI::MemoryPointer.from_string(name) }
643
+ names_array_ptr = FFI::MemoryPointer.new(:pointer, [name_ptrs.size, 1].max)
644
+ names_array_ptr.write_array_of_pointer(name_ptrs)
645
+
646
+ topic_collection_ptr = Rdkafka::Bindings.rd_kafka_TopicCollection_of_topic_names(
647
+ names_array_ptr,
648
+ name_ptrs.size
649
+ )
650
+
651
+ @native_kafka.with_inner do |inner|
652
+ Rdkafka::Bindings.rd_kafka_DescribeTopics(
653
+ inner,
654
+ topic_collection_ptr,
655
+ admin_options_ptr,
656
+ queue_ptr
657
+ )
658
+ end
659
+ rescue Exception
660
+ DescribeTopicsHandle.remove(handle.to_ptr.address)
661
+ raise
662
+ ensure
663
+ Rdkafka::Bindings.rd_kafka_TopicCollection_destroy(topic_collection_ptr) if topic_collection_ptr
664
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
665
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
666
+ end
667
+
668
+ handle
669
+ end
670
+
671
+ # Describes the given consumer groups: their state, partition assignor, coordinator and
672
+ # members with their partition assignments.
673
+ #
674
+ # Each requested group is reported separately, with its own `:error`. A group id the cluster
675
+ # does not know is not an error: it is reported in the `Dead` state with no members.
676
+ #
677
+ # @param group_ids [Array<String>] ids of the consumer groups to describe
678
+ # @param include_authorized_operations [Boolean] whether to also fetch the ACL operations the
679
+ # client is authorized to perform on each group (reported as `:authorized_operations`)
680
+ # @return [DescribeConsumerGroupsHandle] handle that can be used to wait for the result
681
+ # @raise [RdkafkaError] when describing the consumer groups fails as a whole
682
+ #
683
+ # @example Describe a consumer group and print its members and their assignments
684
+ # report = admin.describe_consumer_groups(["example-group"]).wait(max_wait_timeout_ms: 15_000)
685
+ #
686
+ # report.groups.each do |group|
687
+ # next warn "#{group[:group_id]}: #{group[:error].message}" if group[:error]
688
+ #
689
+ # puts "#{group[:group_id]} - #{group[:state_name]} (#{group[:members].size} members)"
690
+ #
691
+ # group[:members].each do |member|
692
+ # puts " #{member[:client_id]}@#{member[:host]}: #{member[:assignment]}"
693
+ # end
694
+ # end
695
+ def describe_consumer_groups(group_ids, include_authorized_operations: false)
696
+ closed_admin_check(__method__)
697
+
698
+ group_id_ptrs = group_ids.map { |group_id| FFI::MemoryPointer.from_string(group_id) }
699
+ group_ids_ptr = FFI::MemoryPointer.new(:pointer, group_id_ptrs.size)
700
+ group_ids_ptr.write_array_of_pointer(group_id_ptrs)
701
+
702
+ # Get a pointer to the queue that our request will be enqueued on
703
+ queue_ptr = @native_kafka.with_inner do |inner|
704
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
705
+ end
706
+
707
+ if queue_ptr.null?
708
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
709
+ end
710
+
711
+ # Create and register the handle we will return to the caller
712
+ handle = DescribeConsumerGroupsHandle.new
713
+ handle[:pending] = true
714
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
715
+ DescribeConsumerGroupsHandle.register(handle)
716
+
717
+ admin_options_ptr = @native_kafka.with_inner do |inner|
718
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
719
+ inner,
720
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBECONSUMERGROUPS
721
+ )
722
+ end
723
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
724
+
725
+ begin
726
+ set_include_authorized_operations(admin_options_ptr) if include_authorized_operations
727
+
728
+ @native_kafka.with_inner do |inner|
729
+ Rdkafka::Bindings.rd_kafka_DescribeConsumerGroups(
730
+ inner,
731
+ group_ids_ptr,
732
+ group_id_ptrs.size,
733
+ admin_options_ptr,
734
+ queue_ptr
735
+ )
736
+ end
737
+ rescue Exception
738
+ DescribeConsumerGroupsHandle.remove(handle.to_ptr.address)
739
+ raise
740
+ ensure
741
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
742
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
743
+ end
744
+
745
+ handle
746
+ end
747
+
748
+ # Deletes the named topic
749
+ #
750
+ # @param topic_name [String] name of the topic to delete
751
+ # @return [DeleteTopicHandle] Delete topic handle that can be used to wait for the result of
752
+ # deleting the topic
753
+ # @raise [RdkafkaError] When the topic name is invalid or the topic does not exist
754
+ def delete_topic(topic_name)
755
+ closed_admin_check(__method__)
756
+
757
+ # Create a rd_kafka_DeleteTopic_t representing the topic to be deleted
758
+ delete_topic_ptr = Rdkafka::Bindings.rd_kafka_DeleteTopic_new(FFI::MemoryPointer.from_string(topic_name))
759
+
760
+ # Note that rd_kafka_DeleteTopics can create more than one topic at a time
761
+ pointer_array = [delete_topic_ptr]
762
+ topics_array_ptr = FFI::MemoryPointer.new(:pointer)
763
+ topics_array_ptr.write_array_of_pointer(pointer_array)
764
+
765
+ # Get a pointer to the queue that our request will be enqueued on
766
+ queue_ptr = @native_kafka.with_inner do |inner|
767
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
768
+ end
769
+ if queue_ptr.null?
770
+ Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
771
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
772
+ end
773
+
774
+ # Create and register the handle we will return to the caller
775
+ delete_topic_handle = DeleteTopicHandle.new
776
+ delete_topic_handle[:pending] = true
777
+ delete_topic_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
778
+ DeleteTopicHandle.register(delete_topic_handle)
779
+ admin_options_ptr = @native_kafka.with_inner do |inner|
780
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETETOPICS)
781
+ end
782
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_topic_handle.to_ptr)
783
+
784
+ begin
785
+ @native_kafka.with_inner do |inner|
786
+ Rdkafka::Bindings.rd_kafka_DeleteTopics(
787
+ inner,
788
+ topics_array_ptr,
789
+ 1,
790
+ admin_options_ptr,
791
+ queue_ptr
792
+ )
793
+ end
794
+ rescue Exception
795
+ DeleteTopicHandle.remove(delete_topic_handle.to_ptr.address)
796
+ raise
797
+ ensure
798
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
799
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
800
+ Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
801
+ end
802
+
803
+ delete_topic_handle
804
+ end
805
+
806
+ # Creates extra partitions for a given topic
807
+ #
808
+ # @param topic_name [String] name of the topic
809
+ # @param partition_count [Integer] how many partitions we want to end up with for given topic
810
+ # @return [CreatePartitionsHandle] Create partitions handle that can be used to wait for the
811
+ # result
812
+ # @raise [ConfigError] When the partition count or replication factor are out of valid range
813
+ # @raise [RdkafkaError] When the topic name is invalid or the topic already exists
814
+ # @raise [RdkafkaError] When the topic configuration is invalid
815
+ def create_partitions(topic_name, partition_count)
816
+ closed_admin_check(__method__)
817
+
818
+ @native_kafka.with_inner do |inner|
819
+ error_buffer = FFI::MemoryPointer.from_string(" " * 256)
820
+ new_partitions_ptr = Rdkafka::Bindings.rd_kafka_NewPartitions_new(
821
+ FFI::MemoryPointer.from_string(topic_name),
822
+ partition_count,
823
+ error_buffer,
824
+ 256
825
+ )
826
+ if new_partitions_ptr.null?
827
+ raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
828
+ end
829
+
830
+ pointer_array = [new_partitions_ptr]
831
+ topics_array_ptr = FFI::MemoryPointer.new(:pointer)
832
+ topics_array_ptr.write_array_of_pointer(pointer_array)
833
+
834
+ # Get a pointer to the queue that our request will be enqueued on
835
+ queue_ptr = Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
836
+ if queue_ptr.null?
837
+ Rdkafka::Bindings.rd_kafka_NewPartitions_destroy(new_partitions_ptr)
838
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
839
+ end
840
+
841
+ # Create and register the handle we will return to the caller
842
+ create_partitions_handle = CreatePartitionsHandle.new
843
+ create_partitions_handle[:pending] = true
844
+ create_partitions_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
845
+ CreatePartitionsHandle.register(create_partitions_handle)
846
+ admin_options_ptr = Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATEPARTITIONS)
847
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, create_partitions_handle.to_ptr)
848
+
849
+ begin
850
+ Rdkafka::Bindings.rd_kafka_CreatePartitions(
851
+ inner,
852
+ topics_array_ptr,
853
+ 1,
854
+ admin_options_ptr,
855
+ queue_ptr
856
+ )
857
+ rescue Exception
858
+ CreatePartitionsHandle.remove(create_partitions_handle.to_ptr.address)
859
+ raise
860
+ ensure
861
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
862
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
863
+ Rdkafka::Bindings.rd_kafka_NewPartitions_destroy(new_partitions_ptr)
864
+ end
865
+
866
+ create_partitions_handle
867
+ end
868
+ end
869
+
870
+ # Create acl
871
+ #
872
+ # @param resource_type [Integer] rd_kafka_ResourceType_t value:
873
+ # - RD_KAFKA_RESOURCE_TOPIC = 2
874
+ # - RD_KAFKA_RESOURCE_GROUP = 3
875
+ # - RD_KAFKA_RESOURCE_BROKER = 4
876
+ # @param resource_name [String] name of the resource
877
+ # @param resource_pattern_type [Integer] rd_kafka_ResourcePatternType_t value:
878
+ # - RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0
879
+ # - RD_KAFKA_RESOURCE_PATTERN_ANY = 1
880
+ # - RD_KAFKA_RESOURCE_PATTERN_MATCH = 2
881
+ # - RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3
882
+ # - RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4
883
+ # @param principal [String] principal (e.g., "User:alice")
884
+ # @param host [String] host address
885
+ # @param operation [Integer] rd_kafka_AclOperation_t value:
886
+ # - RD_KAFKA_ACL_OPERATION_ALL = 2
887
+ # - RD_KAFKA_ACL_OPERATION_READ = 3
888
+ # - RD_KAFKA_ACL_OPERATION_WRITE = 4
889
+ # - RD_KAFKA_ACL_OPERATION_CREATE = 5
890
+ # - RD_KAFKA_ACL_OPERATION_DELETE = 6
891
+ # - RD_KAFKA_ACL_OPERATION_ALTER = 7
892
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE = 8
893
+ # - RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9
894
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10
895
+ # - RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11
896
+ # - RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12
897
+ # @param permission_type [Integer] rd_kafka_AclPermissionType_t value:
898
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2
899
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3
900
+ # @return [CreateAclHandle] Create acl handle that can be used to wait for the result of
901
+ # creating the acl
902
+ # @raise [RdkafkaError]
903
+ def create_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
904
+ closed_admin_check(__method__)
905
+
906
+ # Create a rd_kafka_AclBinding_t representing the new acl
907
+ error_buffer = FFI::MemoryPointer.from_string(" " * 256)
908
+ new_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBinding_new(
909
+ resource_type,
910
+ FFI::MemoryPointer.from_string(resource_name),
911
+ resource_pattern_type,
912
+ FFI::MemoryPointer.from_string(principal),
913
+ FFI::MemoryPointer.from_string(host),
914
+ operation,
915
+ permission_type,
916
+ error_buffer,
917
+ 256
918
+ )
919
+ if new_acl_ptr.null?
920
+ raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
921
+ end
922
+
923
+ # Note that rd_kafka_CreateAcls can create more than one acl at a time
924
+ pointer_array = [new_acl_ptr]
925
+ acls_array_ptr = FFI::MemoryPointer.new(:pointer)
926
+ acls_array_ptr.write_array_of_pointer(pointer_array)
927
+
928
+ # Get a pointer to the queue that our request will be enqueued on
929
+ queue_ptr = @native_kafka.with_inner do |inner|
930
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
931
+ end
932
+
933
+ if queue_ptr.null?
934
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
935
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
936
+ end
937
+
938
+ # Create and register the handle that we will return to the caller
939
+ create_acl_handle = CreateAclHandle.new
940
+ create_acl_handle[:pending] = true
941
+ create_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
942
+ CreateAclHandle.register(create_acl_handle)
943
+
944
+ admin_options_ptr = @native_kafka.with_inner do |inner|
945
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATEACLS)
946
+ end
947
+
948
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, create_acl_handle.to_ptr)
949
+
950
+ begin
951
+ @native_kafka.with_inner do |inner|
952
+ Rdkafka::Bindings.rd_kafka_CreateAcls(
953
+ inner,
954
+ acls_array_ptr,
955
+ 1,
956
+ admin_options_ptr,
957
+ queue_ptr
958
+ )
959
+ end
960
+ rescue Exception
961
+ CreateAclHandle.remove(create_acl_handle.to_ptr.address)
962
+ raise
963
+ ensure
964
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
965
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
966
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
967
+ end
968
+
969
+ create_acl_handle
970
+ end
971
+
972
+ # Delete acl
973
+ #
974
+ # @param resource_type [Integer] rd_kafka_ResourceType_t value:
975
+ # - RD_KAFKA_RESOURCE_TOPIC = 2
976
+ # - RD_KAFKA_RESOURCE_GROUP = 3
977
+ # - RD_KAFKA_RESOURCE_BROKER = 4
978
+ # @param resource_name [String, nil] name of the resource or nil for any
979
+ # @param resource_pattern_type [Integer] rd_kafka_ResourcePatternType_t value:
980
+ # - RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0
981
+ # - RD_KAFKA_RESOURCE_PATTERN_ANY = 1
982
+ # - RD_KAFKA_RESOURCE_PATTERN_MATCH = 2
983
+ # - RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3
984
+ # - RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4
985
+ # @param principal [String, nil] principal (e.g., "User:alice") or nil for any
986
+ # @param host [String, nil] host address or nil for any
987
+ # @param operation [Integer] rd_kafka_AclOperation_t value:
988
+ # - RD_KAFKA_ACL_OPERATION_ALL = 2
989
+ # - RD_KAFKA_ACL_OPERATION_READ = 3
990
+ # - RD_KAFKA_ACL_OPERATION_WRITE = 4
991
+ # - RD_KAFKA_ACL_OPERATION_CREATE = 5
992
+ # - RD_KAFKA_ACL_OPERATION_DELETE = 6
993
+ # - RD_KAFKA_ACL_OPERATION_ALTER = 7
994
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE = 8
995
+ # - RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9
996
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10
997
+ # - RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11
998
+ # - RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12
999
+ # @param permission_type [Integer] rd_kafka_AclPermissionType_t value:
1000
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2
1001
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3
1002
+ # @return [DeleteAclHandle] Delete acl handle that can be used to wait for the result of
1003
+ # deleting the acl
1004
+ # @raise [RdkafkaError]
1005
+ def delete_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
1006
+ closed_admin_check(__method__)
1007
+
1008
+ # Create a rd_kafka_AclBinding_t representing the acl to be deleted
1009
+ error_buffer = FFI::MemoryPointer.from_string(" " * 256)
1010
+
1011
+ delete_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBindingFilter_new(
1012
+ resource_type,
1013
+ resource_name ? FFI::MemoryPointer.from_string(resource_name) : nil,
1014
+ resource_pattern_type,
1015
+ principal ? FFI::MemoryPointer.from_string(principal) : nil,
1016
+ host ? FFI::MemoryPointer.from_string(host) : nil,
1017
+ operation,
1018
+ permission_type,
1019
+ error_buffer,
1020
+ 256
1021
+ )
1022
+
1023
+ if delete_acl_ptr.null?
1024
+ raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
1025
+ end
1026
+
1027
+ # Note that rd_kafka_DeleteAcls can delete more than one acl at a time
1028
+ pointer_array = [delete_acl_ptr]
1029
+ acls_array_ptr = FFI::MemoryPointer.new(:pointer)
1030
+ acls_array_ptr.write_array_of_pointer(pointer_array)
1031
+
1032
+ # Get a pointer to the queue that our request will be enqueued on
1033
+ queue_ptr = @native_kafka.with_inner do |inner|
1034
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
1035
+ end
1036
+
1037
+ if queue_ptr.null?
1038
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(delete_acl_ptr)
1039
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
1040
+ end
1041
+
1042
+ # Create and register the handle that we will return to the caller
1043
+ delete_acl_handle = DeleteAclHandle.new
1044
+ delete_acl_handle[:pending] = true
1045
+ delete_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
1046
+ DeleteAclHandle.register(delete_acl_handle)
1047
+
1048
+ admin_options_ptr = @native_kafka.with_inner do |inner|
1049
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETEACLS)
1050
+ end
1051
+
1052
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_acl_handle.to_ptr)
1053
+
1054
+ begin
1055
+ @native_kafka.with_inner do |inner|
1056
+ Rdkafka::Bindings.rd_kafka_DeleteAcls(
1057
+ inner,
1058
+ acls_array_ptr,
1059
+ 1,
1060
+ admin_options_ptr,
1061
+ queue_ptr
1062
+ )
1063
+ end
1064
+ rescue Exception
1065
+ DeleteAclHandle.remove(delete_acl_handle.to_ptr.address)
1066
+ raise
1067
+ ensure
1068
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
1069
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
1070
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(delete_acl_ptr)
1071
+ end
1072
+
1073
+ delete_acl_handle
1074
+ end
1075
+
1076
+ # Describe acls
1077
+ #
1078
+ # @param resource_type [Integer] rd_kafka_ResourceType_t value:
1079
+ # - RD_KAFKA_RESOURCE_TOPIC = 2
1080
+ # - RD_KAFKA_RESOURCE_GROUP = 3
1081
+ # - RD_KAFKA_RESOURCE_BROKER = 4
1082
+ # @param resource_name [String, nil] name of the resource or nil for any
1083
+ # @param resource_pattern_type [Integer] rd_kafka_ResourcePatternType_t value:
1084
+ # - RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0
1085
+ # - RD_KAFKA_RESOURCE_PATTERN_ANY = 1
1086
+ # - RD_KAFKA_RESOURCE_PATTERN_MATCH = 2
1087
+ # - RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3
1088
+ # - RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4
1089
+ # @param principal [String, nil] principal (e.g., "User:alice") or nil for any
1090
+ # @param host [String, nil] host address or nil for any
1091
+ # @param operation [Integer] rd_kafka_AclOperation_t value:
1092
+ # - RD_KAFKA_ACL_OPERATION_ALL = 2
1093
+ # - RD_KAFKA_ACL_OPERATION_READ = 3
1094
+ # - RD_KAFKA_ACL_OPERATION_WRITE = 4
1095
+ # - RD_KAFKA_ACL_OPERATION_CREATE = 5
1096
+ # - RD_KAFKA_ACL_OPERATION_DELETE = 6
1097
+ # - RD_KAFKA_ACL_OPERATION_ALTER = 7
1098
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE = 8
1099
+ # - RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9
1100
+ # - RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10
1101
+ # - RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11
1102
+ # - RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12
1103
+ # @param permission_type [Integer] rd_kafka_AclPermissionType_t value:
1104
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2
1105
+ # - RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3
1106
+ # @return [DescribeAclHandle] Describe acl handle that can be used to wait for the result of
1107
+ # fetching acls
1108
+ # @raise [RdkafkaError]
1109
+ def describe_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
1110
+ closed_admin_check(__method__)
1111
+
1112
+ # Create a rd_kafka_AclBinding_t with the filters to fetch existing acls
1113
+ error_buffer = FFI::MemoryPointer.from_string(" " * 256)
1114
+ describe_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBindingFilter_new(
1115
+ resource_type,
1116
+ resource_name ? FFI::MemoryPointer.from_string(resource_name) : nil,
1117
+ resource_pattern_type,
1118
+ principal ? FFI::MemoryPointer.from_string(principal) : nil,
1119
+ host ? FFI::MemoryPointer.from_string(host) : nil,
1120
+ operation,
1121
+ permission_type,
1122
+ error_buffer,
1123
+ 256
1124
+ )
1125
+ if describe_acl_ptr.null?
1126
+ raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
1127
+ end
1128
+
1129
+ # Get a pointer to the queue that our request will be enqueued on
1130
+ queue_ptr = @native_kafka.with_inner do |inner|
1131
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
1132
+ end
1133
+
1134
+ if queue_ptr.null?
1135
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(describe_acl_ptr)
1136
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
1137
+ end
1138
+
1139
+ # Create and register the handle that we will return to the caller
1140
+ describe_acl_handle = DescribeAclHandle.new
1141
+ describe_acl_handle[:pending] = true
1142
+ describe_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
1143
+ DescribeAclHandle.register(describe_acl_handle)
1144
+
1145
+ admin_options_ptr = @native_kafka.with_inner do |inner|
1146
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBEACLS)
1147
+ end
1148
+
1149
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, describe_acl_handle.to_ptr)
1150
+
1151
+ begin
1152
+ @native_kafka.with_inner do |inner|
1153
+ Rdkafka::Bindings.rd_kafka_DescribeAcls(
1154
+ inner,
1155
+ describe_acl_ptr,
1156
+ admin_options_ptr,
1157
+ queue_ptr
1158
+ )
1159
+ end
1160
+ rescue Exception
1161
+ DescribeAclHandle.remove(describe_acl_handle.to_ptr.address)
1162
+ raise
1163
+ ensure
1164
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
1165
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
1166
+ Rdkafka::Bindings.rd_kafka_AclBinding_destroy(describe_acl_ptr)
1167
+ end
1168
+
1169
+ describe_acl_handle
1170
+ end
1171
+
1172
+ # Describe configs
1173
+ #
1174
+ # @param resources [Array<Hash>] Array where elements are hashes with two keys:
1175
+ # - `:resource_type` - numerical resource type based on Kafka API
1176
+ # - `:resource_name` - string with resource name
1177
+ # @return [DescribeConfigsHandle] Describe config handle that can be used to wait for the
1178
+ # result of fetching resources with their appropriate configs
1179
+ #
1180
+ # @raise [RdkafkaError]
1181
+ #
1182
+ # @note Several resources can be requested at one go, but only one broker at a time
1183
+ def describe_configs(resources)
1184
+ closed_admin_check(__method__)
1185
+
1186
+ # Parse user input before allocating any native resources or registering the handle, so a
1187
+ # missing :resource_type / :resource_name key raises with nothing to clean up. Previously the
1188
+ # KeyError leaked the background queue, the AdminOptions, the registered handle and any
1189
+ # ConfigResources already built.
1190
+ parsed_resources = resources.map do |resource_details|
1191
+ [resource_details.fetch(:resource_type), resource_details.fetch(:resource_name)]
1192
+ end
1193
+
1194
+ handle = DescribeConfigsHandle.new
1195
+ handle[:pending] = true
1196
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
1197
+
1198
+ # All native allocation happens inside the begin so a raise at any point (a null background
1199
+ # queue, a non-String resource name making `from_string` raise mid-build, etc.) is cleaned up
1200
+ # by the ensure rather than leaking the queue, the AdminOptions, the handle and the
1201
+ # ConfigResources already built.
1202
+ queue_ptr = nil
1203
+ admin_options_ptr = nil
1204
+ pointer_array = []
1205
+ registered = false
1206
+
1207
+ begin
1208
+ queue_ptr = @native_kafka.with_inner do |inner|
1209
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
1210
+ end
1211
+
1212
+ if queue_ptr.null?
1213
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
1214
+ end
1215
+
1216
+ admin_options_ptr = @native_kafka.with_inner do |inner|
1217
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
1218
+ inner,
1219
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS
1220
+ )
1221
+ end
1222
+
1223
+ # A NULL AdminOptions would segfault the moment we set its opaque pointer, so reject it
1224
+ # before registering the handle (the ensure frees the queue already acquired).
1225
+ if admin_options_ptr.null?
1226
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_AdminOptions_new was NULL")
1227
+ end
1228
+
1229
+ DescribeConfigsHandle.register(handle)
1230
+ registered = true
1231
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
1232
+
1233
+ # Build resources one at a time so a raise on a later element still leaves the earlier ones
1234
+ # in pointer_array for the ensure to destroy.
1235
+ parsed_resources.each do |resource_type, resource_name|
1236
+ resource_ptr = Rdkafka::Bindings.rd_kafka_ConfigResource_new(
1237
+ resource_type,
1238
+ FFI::MemoryPointer.from_string(resource_name)
1239
+ )
1240
+
1241
+ # librdkafka returns NULL for an empty resource name or a negative resource type. Passing
1242
+ # NULL on to DescribeConfigs and then destroying it both dereference NULL and segfault, so
1243
+ # reject it here (the ensure frees everything already built).
1244
+ if resource_ptr.null?
1245
+ raise Rdkafka::Config::ConfigError.new(
1246
+ "rd_kafka_ConfigResource_new was NULL for #{resource_name.inspect} (type #{resource_type})"
1247
+ )
1248
+ end
1249
+
1250
+ pointer_array << resource_ptr
1251
+ end
1252
+
1253
+ configs_array_ptr = FFI::MemoryPointer.new(:pointer, pointer_array.size)
1254
+ configs_array_ptr.write_array_of_pointer(pointer_array)
1255
+
1256
+ @native_kafka.with_inner do |inner|
1257
+ Rdkafka::Bindings.rd_kafka_DescribeConfigs(
1258
+ inner,
1259
+ configs_array_ptr,
1260
+ pointer_array.size,
1261
+ admin_options_ptr,
1262
+ queue_ptr
1263
+ )
1264
+ end
1265
+ rescue Exception
1266
+ DescribeConfigsHandle.remove(handle.to_ptr.address) if registered
1267
+
1268
+ raise
1269
+ ensure
1270
+ if admin_options_ptr && !admin_options_ptr.null?
1271
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
1272
+ end
1273
+
1274
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr) if queue_ptr && !queue_ptr.null?
1275
+
1276
+ pointer_array.each do |config_resource_ptr|
1277
+ next if config_resource_ptr.null?
1278
+
1279
+ Rdkafka::Bindings.rd_kafka_ConfigResource_destroy(config_resource_ptr)
1280
+ end
1281
+ end
1282
+
1283
+ handle
1284
+ end
1285
+
1286
+ # Alters in an incremental way all the configs provided for given resources
1287
+ #
1288
+ # @param resources_with_configs [Array<Hash>] resources with the configs key that contains
1289
+ # name, value and the proper op_type to perform on this value.
1290
+ #
1291
+ # @return [IncrementalAlterConfigsHandle] Incremental alter configs handle that can be used to
1292
+ # wait for the result of altering resources with their appropriate configs
1293
+ #
1294
+ # @raise [RdkafkaError]
1295
+ #
1296
+ # @note Several resources can be requested at one go, but only one broker at a time
1297
+ # @note The results won't contain altered values but only the altered resources
1298
+ def incremental_alter_configs(resources_with_configs)
1299
+ closed_admin_check(__method__)
1300
+
1301
+ # Parse user input before allocating native resources or registering the handle, so a missing
1302
+ # key raises with nothing to clean up. Previously the KeyError leaked the background queue,
1303
+ # the AdminOptions, the registered handle and any ConfigResources already built.
1304
+ parsed_resources = resources_with_configs.map do |resource_details|
1305
+ [
1306
+ resource_details.fetch(:resource_type),
1307
+ resource_details.fetch(:resource_name),
1308
+ resource_details.fetch(:configs).map do |config|
1309
+ [config.fetch(:name), config.fetch(:op_type), config.fetch(:value)]
1310
+ end
1311
+ ]
1312
+ end
1313
+
1314
+ handle = IncrementalAlterConfigsHandle.new
1315
+ handle[:pending] = true
1316
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
1317
+
1318
+ # All native allocation happens inside the begin so a raise at any point (a null background
1319
+ # queue, a non-String resource name, or FFI marshaling of a config name/value failing while
1320
+ # adding configs) is cleaned up by the ensure rather than leaking the queue, the AdminOptions,
1321
+ # the handle and the ConfigResources already built.
1322
+ queue_ptr = nil
1323
+ admin_options_ptr = nil
1324
+ pointer_array = []
1325
+ registered = false
1326
+ add_error = nil
1327
+
1328
+ begin
1329
+ queue_ptr = @native_kafka.with_inner do |inner|
1330
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
1331
+ end
1332
+
1333
+ if queue_ptr.null?
1334
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
1335
+ end
1336
+
1337
+ admin_options_ptr = @native_kafka.with_inner do |inner|
1338
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(
1339
+ inner,
1340
+ Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_INCREMENTALALTERCONFIGS
1341
+ )
1342
+ end
1343
+
1344
+ # A NULL AdminOptions would segfault the moment we set its opaque pointer, so reject it
1345
+ # before registering the handle (the ensure frees the queue already acquired).
1346
+ if admin_options_ptr.null?
1347
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_AdminOptions_new was NULL")
1348
+ end
1349
+
1350
+ IncrementalAlterConfigsHandle.register(handle)
1351
+ registered = true
1352
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
1353
+
1354
+ parsed_resources.each do |resource_type, resource_name, configs|
1355
+ # First build the appropriate resource representation
1356
+ resource_ptr = Rdkafka::Bindings.rd_kafka_ConfigResource_new(
1357
+ resource_type,
1358
+ FFI::MemoryPointer.from_string(resource_name)
1359
+ )
1360
+
1361
+ # librdkafka returns NULL for an empty resource name or a negative resource type. Passing
1362
+ # NULL on to add_incremental_config/IncrementalAlterConfigs and then destroying it both
1363
+ # dereference NULL and segfault, so reject it here (the ensure frees everything built).
1364
+ if resource_ptr.null?
1365
+ raise Rdkafka::Config::ConfigError.new(
1366
+ "rd_kafka_ConfigResource_new was NULL for #{resource_name.inspect} (type #{resource_type})"
1367
+ )
1368
+ end
1369
+
1370
+ # Track it immediately so a raise while adding its configs still frees it via the ensure.
1371
+ pointer_array << resource_ptr
1372
+
1373
+ configs.each do |name, op_type, value|
1374
+ # rd_kafka_ConfigResource_add_incremental_config returns a non-NULL rd_kafka_error_t for
1375
+ # an invalid op_type, an empty/nil name, or a nil value on a non-delete op. The result
1376
+ # used to be ignored: the entry was silently dropped, the alter request still reported
1377
+ # success, and the error object leaked. Capture the first error (always destroying the
1378
+ # native error object) and raise it below, before the request is sent.
1379
+ error_ptr = Bindings.rd_kafka_ConfigResource_add_incremental_config(
1380
+ resource_ptr,
1381
+ name,
1382
+ op_type,
1383
+ value
1384
+ )
1385
+
1386
+ unless error_ptr.null?
1387
+ code = Rdkafka::Bindings.rd_kafka_error_code(error_ptr)
1388
+ Rdkafka::Bindings.rd_kafka_error_destroy(error_ptr)
1389
+
1390
+ unless code == Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
1391
+ add_error ||= Rdkafka::RdkafkaError.new(
1392
+ code,
1393
+ "rd_kafka_ConfigResource_add_incremental_config"
1394
+ )
1395
+ end
1396
+ end
1397
+ end
1398
+ end
1399
+
1400
+ configs_array_ptr = FFI::MemoryPointer.new(:pointer, pointer_array.size)
1401
+ configs_array_ptr.write_array_of_pointer(pointer_array)
1402
+
1403
+ # Raise only after the full array is built so the ensure below frees every ConfigResource
1404
+ # we created, and before the request is sent so a rejected entry can't report success.
1405
+ raise add_error if add_error
1406
+
1407
+ @native_kafka.with_inner do |inner|
1408
+ Rdkafka::Bindings.rd_kafka_IncrementalAlterConfigs(
1409
+ inner,
1410
+ configs_array_ptr,
1411
+ pointer_array.size,
1412
+ admin_options_ptr,
1413
+ queue_ptr
1414
+ )
1415
+ end
1416
+ rescue Exception
1417
+ IncrementalAlterConfigsHandle.remove(handle.to_ptr.address) if registered
1418
+
1419
+ raise
1420
+ ensure
1421
+ if admin_options_ptr && !admin_options_ptr.null?
1422
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
1423
+ end
1424
+
1425
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr) if queue_ptr && !queue_ptr.null?
1426
+
1427
+ pointer_array.each do |config_resource_ptr|
1428
+ next if config_resource_ptr.null?
1429
+
1430
+ Rdkafka::Bindings.rd_kafka_ConfigResource_destroy(config_resource_ptr)
1431
+ end
1432
+ end
1433
+
1434
+ handle
1435
+ end
1436
+
1437
+ private
1438
+
1439
+ # Shared enqueue path for the two consumer group offset admin operations. They differ only
1440
+ # in their C entry points, so keeping one body avoids two near identical copies of the
1441
+ # queue/handle/options dance and its cleanup.
1442
+ #
1443
+ # @param request_ptr [FFI::Pointer] the op specific request object
1444
+ # @param handle [AbstractHandle] the handle to register and return
1445
+ # @param admin_op [Integer] the RD_KAFKA_ADMIN_OP_* constant for this operation
1446
+ # @param destroy [Method] bound function used to free the request object
1447
+ # @param enqueue [Method] bound function used to enqueue the request
1448
+ # @return [AbstractHandle] the registered handle
1449
+ def enqueue_group_offsets_op(request_ptr:, handle:, admin_op:, destroy:, enqueue:)
1450
+ array_ptr = FFI::MemoryPointer.new(:pointer)
1451
+ array_ptr.write_array_of_pointer([request_ptr])
1452
+
1453
+ queue_ptr = @native_kafka.with_inner do |inner|
1454
+ Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
1455
+ end
1456
+
1457
+ if queue_ptr.null?
1458
+ destroy.call(request_ptr)
1459
+ raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
1460
+ end
1461
+
1462
+ handle[:pending] = true
1463
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
1464
+ handle.class.register(handle)
1465
+
1466
+ admin_options_ptr = @native_kafka.with_inner do |inner|
1467
+ Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, admin_op)
1468
+ end
1469
+ Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
1470
+
1471
+ begin
1472
+ @native_kafka.with_inner do |inner|
1473
+ enqueue.call(inner, array_ptr, 1, admin_options_ptr, queue_ptr)
1474
+ end
1475
+ rescue Exception
1476
+ handle.class.remove(handle.to_ptr.address)
1477
+ raise
1478
+ ensure
1479
+ Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
1480
+ Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
1481
+ destroy.call(request_ptr)
1482
+ end
1483
+
1484
+ handle
1485
+ end
1486
+
1487
+ # Asks librdkafka to include authorized operations in a describe result
1488
+ # @param admin_options_ptr [FFI::Pointer] the admin options to update
1489
+ # @raise [RdkafkaError] when librdkafka rejects the option
1490
+ def set_include_authorized_operations(admin_options_ptr)
1491
+ error_ptr = Rdkafka::Bindings.rd_kafka_AdminOptions_set_include_authorized_operations(admin_options_ptr, 1)
1492
+
1493
+ return if error_ptr.null?
1494
+
1495
+ begin
1496
+ string_ptr = Rdkafka::Bindings.rd_kafka_error_string(error_ptr)
1497
+
1498
+ raise RdkafkaError.new(
1499
+ Rdkafka::Bindings.rd_kafka_error_code(error_ptr),
1500
+ broker_message: string_ptr.null? ? nil : string_ptr.read_string
1501
+ )
1502
+ ensure
1503
+ Rdkafka::Bindings.rd_kafka_error_destroy(error_ptr)
1504
+ end
1505
+ end
1506
+
1507
+ # Checks if the admin is closed and raises an error if so
1508
+ # @param method [Symbol] name of the calling method for error context
1509
+ # @raise [ClosedAdminError] when the admin is closed
168
1510
  def closed_admin_check(method)
169
1511
  raise Rdkafka::ClosedAdminError.new(method) if closed?
170
1512
  end
1513
+ alias_method :closed_check, :closed_admin_check
171
1514
  end
172
1515
  end