Class: Rdkafka::Admin

Inherits:
Object
  • Object
show all
Includes:
Helpers::OAuth
Defined in:
lib/rdkafka/admin.rb,
lib/rdkafka/admin/create_acl_handle.rb,
lib/rdkafka/admin/create_acl_report.rb,
lib/rdkafka/admin/delete_acl_handle.rb,
lib/rdkafka/admin/delete_acl_report.rb,
lib/rdkafka/admin/acl_binding_result.rb,
lib/rdkafka/admin/create_topic_handle.rb,
lib/rdkafka/admin/create_topic_report.rb,
lib/rdkafka/admin/delete_topic_handle.rb,
lib/rdkafka/admin/delete_topic_report.rb,
lib/rdkafka/admin/describe_acl_handle.rb,
lib/rdkafka/admin/describe_acl_report.rb,
lib/rdkafka/admin/delete_groups_handle.rb,
lib/rdkafka/admin/delete_groups_report.rb,
lib/rdkafka/admin/config_binding_result.rb,
lib/rdkafka/admin/describe_configs_handle.rb,
lib/rdkafka/admin/describe_configs_report.rb,
lib/rdkafka/admin/create_partitions_handle.rb,
lib/rdkafka/admin/create_partitions_report.rb,
lib/rdkafka/admin/config_resource_binding_result.rb,
lib/rdkafka/admin/incremental_alter_configs_handle.rb,
lib/rdkafka/admin/incremental_alter_configs_report.rb

Overview

Admin client for Kafka administrative operations

Defined Under Namespace

Classes: AclBindingResult, ConfigBindingResult, ConfigResourceBindingResult, CreateAclHandle, CreateAclReport, CreatePartitionsHandle, CreatePartitionsReport, CreateTopicHandle, CreateTopicReport, DeleteAclHandle, DeleteAclReport, DeleteGroupsHandle, DeleteGroupsReport, DeleteTopicHandle, DeleteTopicReport, DescribeAclHandle, DescribeAclReport, DescribeConfigsHandle, DescribeConfigsReport, IncrementalAlterConfigsHandle, IncrementalAlterConfigsReport

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::OAuth

#oauthbearer_set_token, #oauthbearer_set_token_failure

Constructor Details

#initialize(native_kafka) ⇒ Admin

Returns a new instance of Admin.

Parameters:

  • native_kafka (NativeKafka)

    wrapper around the native Kafka handle



54
55
56
57
58
59
# File 'lib/rdkafka/admin.rb', line 54

def initialize(native_kafka)
  @native_kafka = native_kafka

  # Makes sure, that native kafka gets closed before it gets GCed by Ruby
  ObjectSpace.define_finalizer(self, native_kafka.finalizer)
end

Class Method Details

.describe_errorsHash{Integer => Hash}

Allows us to retrieve librdkafka errors with descriptions Useful for debugging and building UIs, etc.

Returns:

  • (Hash{Integer => Hash})

    hash with errors mapped by code



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdkafka/admin.rb', line 13

def describe_errors
  # Memory pointers for the array of structures and count
  p_error_descs = FFI::MemoryPointer.new(:pointer)
  p_count = FFI::MemoryPointer.new(:size_t)

  # Call the attached function
  Bindings.rd_kafka_get_err_descs(p_error_descs, p_count)

  # Retrieve the number of items in the array
  count = p_count.read_uint

  # Get the pointer to the array of error descriptions
  array_of_errors = FFI::Pointer.new(Bindings::NativeErrorDesc, p_error_descs.read_pointer)

  errors = {}

  count.times do |i|
    # Get the pointer to each struct
    error_ptr = array_of_errors[i]

    # Create a new instance of NativeErrorDesc for each item
    error_desc = Bindings::NativeErrorDesc.new(error_ptr)

    # Read values from the struct
    code = error_desc[:code]

    name = ""
    desc = ""

    name = error_desc[:name].read_string unless error_desc[:name].null?
    desc = error_desc[:desc].read_string unless error_desc[:desc].null?

    errors[code] = { code: code, name: name, description: desc }
  end

  errors
end

Instance Method Details

#closeObject

Close this admin instance



114
115
116
117
118
# File 'lib/rdkafka/admin.rb', line 114

def close
  return if closed?
  ObjectSpace.undefine_finalizer(self)
  @native_kafka.close
end

#closed?Boolean

Whether this admin has closed

Returns:

  • (Boolean)


121
122
123
# File 'lib/rdkafka/admin.rb', line 121

def closed?
  @native_kafka.closed?
end

#create_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:) ⇒ CreateAclHandle

Create acl

Parameters:

  • resource_type (Integer)

    rd_kafka_ResourceType_t value:

    • RD_KAFKA_RESOURCE_TOPIC = 2

    • RD_KAFKA_RESOURCE_GROUP = 3

    • RD_KAFKA_RESOURCE_BROKER = 4

  • resource_name (String)

    name of the resource

  • resource_pattern_type (Integer)

    rd_kafka_ResourcePatternType_t value:

    • RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0

    • RD_KAFKA_RESOURCE_PATTERN_ANY = 1

    • RD_KAFKA_RESOURCE_PATTERN_MATCH = 2

    • RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3

    • RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4

  • principal (String)

    principal (e.g., “User:alice”)

  • host (String)

    host address

  • operation (Integer)

    rd_kafka_AclOperation_t value:

    • RD_KAFKA_ACL_OPERATION_ALL = 2

    • RD_KAFKA_ACL_OPERATION_READ = 3

    • RD_KAFKA_ACL_OPERATION_WRITE = 4

    • RD_KAFKA_ACL_OPERATION_CREATE = 5

    • RD_KAFKA_ACL_OPERATION_DELETE = 6

    • RD_KAFKA_ACL_OPERATION_ALTER = 7

    • RD_KAFKA_ACL_OPERATION_DESCRIBE = 8

    • RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9

    • RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10

    • RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11

    • RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12

  • permission_type (Integer)

    rd_kafka_AclPermissionType_t value:

    • RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2

    • RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3

Returns:

  • (CreateAclHandle)

    Create acl handle that can be used to wait for the result of creating the acl

Raises:



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/rdkafka/admin.rb', line 417

def create_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
  closed_admin_check(__method__)

  # Create a rd_kafka_AclBinding_t representing the new acl
  error_buffer = FFI::MemoryPointer.from_string(" " * 256)
  new_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBinding_new(
    resource_type,
    FFI::MemoryPointer.from_string(resource_name),
    resource_pattern_type,
    FFI::MemoryPointer.from_string(principal),
    FFI::MemoryPointer.from_string(host),
    operation,
    permission_type,
    error_buffer,
    256
  )
  if new_acl_ptr.null?
    raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
  end

  # Note that rd_kafka_CreateAcls can create more than one acl at a time
  pointer_array = [new_acl_ptr]
  acls_array_ptr = FFI::MemoryPointer.new(:pointer)
  acls_array_ptr.write_array_of_pointer(pointer_array)

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end

  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle that we will return to the caller
  create_acl_handle = CreateAclHandle.new
  create_acl_handle[:pending] = true
  create_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  CreateAclHandle.register(create_acl_handle)

  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATEACLS)
  end

  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, create_acl_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_CreateAcls(
        inner,
        acls_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    CreateAclHandle.remove(create_acl_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
  end

  create_acl_handle
end

#create_partitions(topic_name, partition_count) ⇒ CreatePartitionsHandle

Creates extra partitions for a given topic

Parameters:

  • topic_name (String)

    name of the topic

  • partition_count (Integer)

    how many partitions we want to end up with for given topic

Returns:

Raises:

  • (ConfigError)

    When the partition count or replication factor are out of valid range

  • (RdkafkaError)

    When the topic name is invalid or the topic already exists

  • (RdkafkaError)

    When the topic configuration is invalid



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rdkafka/admin.rb', line 330

def create_partitions(topic_name, partition_count)
  closed_admin_check(__method__)

  @native_kafka.with_inner do |inner|
    error_buffer = FFI::MemoryPointer.from_string(" " * 256)
    new_partitions_ptr = Rdkafka::Bindings.rd_kafka_NewPartitions_new(
      FFI::MemoryPointer.from_string(topic_name),
      partition_count,
      error_buffer,
      256
    )
    if new_partitions_ptr.null?
      raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
    end

    pointer_array = [new_partitions_ptr]
    topics_array_ptr = FFI::MemoryPointer.new(:pointer)
    topics_array_ptr.write_array_of_pointer(pointer_array)

    # Get a pointer to the queue that our request will be enqueued on
    queue_ptr = Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
    if queue_ptr.null?
      Rdkafka::Bindings.rd_kafka_NewPartitions_destroy(new_partitions_ptr)
      raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
    end

    # Create and register the handle we will return to the caller
    create_partitions_handle = CreatePartitionsHandle.new
    create_partitions_handle[:pending] = true
    create_partitions_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
    CreatePartitionsHandle.register(create_partitions_handle)
    admin_options_ptr = Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATEPARTITIONS)
    Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, create_partitions_handle.to_ptr)

    begin
      Rdkafka::Bindings.rd_kafka_CreatePartitions(
        inner,
        topics_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    rescue Exception
      CreatePartitionsHandle.remove(create_partitions_handle.to_ptr.address)
      raise
    ensure
      Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
      Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
      Rdkafka::Bindings.rd_kafka_NewPartitions_destroy(new_partitions_ptr)
    end

    create_partitions_handle
  end
end

#create_topic(topic_name, partition_count, replication_factor, topic_config = {}) ⇒ CreateTopicHandle

Create a topic with the given partition count and replication factor

Parameters:

  • topic_name (String)

    name of the topic to create

  • partition_count (Integer)

    number of partitions for the topic

  • replication_factor (Integer)

    replication factor for the topic

  • topic_config (Hash) (defaults to: {})

    optional topic configuration settings

Returns:

  • (CreateTopicHandle)

    Create topic handle that can be used to wait for the result of creating the topic

Raises:

  • (ConfigError)

    When the partition count or replication factor are out of valid range

  • (RdkafkaError)

    When the topic name is invalid or the topic already exists

  • (RdkafkaError)

    When the topic configuration is invalid



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rdkafka/admin.rb', line 136

def create_topic(topic_name, partition_count, replication_factor, topic_config = {})
  closed_admin_check(__method__)

  # Create a rd_kafka_NewTopic_t representing the new topic
  error_buffer = FFI::MemoryPointer.from_string(" " * 256)
  new_topic_ptr = Rdkafka::Bindings.rd_kafka_NewTopic_new(
    FFI::MemoryPointer.from_string(topic_name),
    partition_count,
    replication_factor,
    error_buffer,
    256
  )
  if new_topic_ptr.null?
    raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
  end

  topic_config&.each do |key, value|
    Rdkafka::Bindings.rd_kafka_NewTopic_set_config(
      new_topic_ptr,
      key.to_s,
      value.to_s
    )
  end

  # Note that rd_kafka_CreateTopics can create more than one topic at a time
  pointer_array = [new_topic_ptr]
  topics_array_ptr = FFI::MemoryPointer.new(:pointer)
  topics_array_ptr.write_array_of_pointer(pointer_array)

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end
  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_NewTopic_destroy(new_topic_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle we will return to the caller
  create_topic_handle = CreateTopicHandle.new
  create_topic_handle[:pending] = true
  create_topic_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  CreateTopicHandle.register(create_topic_handle)
  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_CREATETOPICS)
  end
  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, create_topic_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_CreateTopics(
        inner,
        topics_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    CreateTopicHandle.remove(create_topic_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_NewTopic_destroy(new_topic_ptr)
  end

  create_topic_handle
end

#delete_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:) ⇒ DeleteAclHandle

Delete acl

Parameters:

  • resource_type (Integer)

    rd_kafka_ResourceType_t value:

    • RD_KAFKA_RESOURCE_TOPIC = 2

    • RD_KAFKA_RESOURCE_GROUP = 3

    • RD_KAFKA_RESOURCE_BROKER = 4

  • resource_name (String, nil)

    name of the resource or nil for any

  • resource_pattern_type (Integer)

    rd_kafka_ResourcePatternType_t value:

    • RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0

    • RD_KAFKA_RESOURCE_PATTERN_ANY = 1

    • RD_KAFKA_RESOURCE_PATTERN_MATCH = 2

    • RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3

    • RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4

  • principal (String, nil)

    principal (e.g., “User:alice”) or nil for any

  • host (String, nil)

    host address or nil for any

  • operation (Integer)

    rd_kafka_AclOperation_t value:

    • RD_KAFKA_ACL_OPERATION_ALL = 2

    • RD_KAFKA_ACL_OPERATION_READ = 3

    • RD_KAFKA_ACL_OPERATION_WRITE = 4

    • RD_KAFKA_ACL_OPERATION_CREATE = 5

    • RD_KAFKA_ACL_OPERATION_DELETE = 6

    • RD_KAFKA_ACL_OPERATION_ALTER = 7

    • RD_KAFKA_ACL_OPERATION_DESCRIBE = 8

    • RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9

    • RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10

    • RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11

    • RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12

  • permission_type (Integer)

    rd_kafka_AclPermissionType_t value:

    • RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2

    • RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3

Returns:

  • (DeleteAclHandle)

    Delete acl handle that can be used to wait for the result of deleting the acl

Raises:



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/rdkafka/admin.rb', line 518

def delete_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
  closed_admin_check(__method__)

  # Create a rd_kafka_AclBinding_t representing the acl to be deleted
  error_buffer = FFI::MemoryPointer.from_string(" " * 256)

  delete_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBindingFilter_new(
    resource_type,
    resource_name ? FFI::MemoryPointer.from_string(resource_name) : nil,
    resource_pattern_type,
    principal ? FFI::MemoryPointer.from_string(principal) : nil,
    host ? FFI::MemoryPointer.from_string(host) : nil,
    operation,
    permission_type,
    error_buffer,
    256
  )

  if delete_acl_ptr.null?
    raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
  end

  # Note that rd_kafka_DeleteAcls can delete more than one acl at a time
  pointer_array = [delete_acl_ptr]
  acls_array_ptr = FFI::MemoryPointer.new(:pointer)
  acls_array_ptr.write_array_of_pointer(pointer_array)

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end

  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle that we will return to the caller
  delete_acl_handle = DeleteAclHandle.new
  delete_acl_handle[:pending] = true
  delete_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  DeleteAclHandle.register(delete_acl_handle)

  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETEACLS)
  end

  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_acl_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_DeleteAcls(
        inner,
        acls_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    DeleteAclHandle.remove(delete_acl_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(delete_acl_ptr)
  end

  delete_acl_handle
end

#delete_group(group_id) ⇒ DeleteGroupsHandle

Deletes a consumer group

Parameters:

  • group_id (String)

    the group id to delete

Returns:

Raises:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rdkafka/admin.rb', line 211

def delete_group(group_id)
  closed_admin_check(__method__)

  # Create a rd_kafka_DeleteGroup_t representing the new topic
  delete_groups_ptr = Rdkafka::Bindings.rd_kafka_DeleteGroup_new(
    FFI::MemoryPointer.from_string(group_id)
  )

  pointer_array = [delete_groups_ptr]
  groups_array_ptr = FFI::MemoryPointer.new(:pointer)
  groups_array_ptr.write_array_of_pointer(pointer_array)

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end
  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle we will return to the caller
  delete_groups_handle = DeleteGroupsHandle.new
  delete_groups_handle[:pending] = true
  delete_groups_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  DeleteGroupsHandle.register(delete_groups_handle)
  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETETOPICS)
  end
  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_groups_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_DeleteGroups(
        inner,
        groups_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    DeleteGroupsHandle.remove(delete_groups_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_DeleteGroup_destroy(delete_groups_ptr)
  end

  delete_groups_handle
end

#delete_topic(topic_name) ⇒ DeleteTopicHandle

Deletes the named topic

Parameters:

  • topic_name (String)

    name of the topic to delete

Returns:

  • (DeleteTopicHandle)

    Delete topic handle that can be used to wait for the result of deleting the topic

Raises:

  • (RdkafkaError)

    When the topic name is invalid or the topic does not exist



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rdkafka/admin.rb', line 270

def delete_topic(topic_name)
  closed_admin_check(__method__)

  # Create a rd_kafka_DeleteTopic_t representing the topic to be deleted
  delete_topic_ptr = Rdkafka::Bindings.rd_kafka_DeleteTopic_new(FFI::MemoryPointer.from_string(topic_name))

  # Note that rd_kafka_DeleteTopics can create more than one topic at a time
  pointer_array = [delete_topic_ptr]
  topics_array_ptr = FFI::MemoryPointer.new(:pointer)
  topics_array_ptr.write_array_of_pointer(pointer_array)

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end
  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle we will return to the caller
  delete_topic_handle = DeleteTopicHandle.new
  delete_topic_handle[:pending] = true
  delete_topic_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  DeleteTopicHandle.register(delete_topic_handle)
  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DELETETOPICS)
  end
  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, delete_topic_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_DeleteTopics(
        inner,
        topics_array_ptr,
        1,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    DeleteTopicHandle.remove(delete_topic_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_DeleteTopic_destroy(delete_topic_ptr)
  end

  delete_topic_handle
end

#describe_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:) ⇒ DescribeAclHandle

Describe acls

Parameters:

  • resource_type (Integer)

    rd_kafka_ResourceType_t value:

    • RD_KAFKA_RESOURCE_TOPIC = 2

    • RD_KAFKA_RESOURCE_GROUP = 3

    • RD_KAFKA_RESOURCE_BROKER = 4

  • resource_name (String, nil)

    name of the resource or nil for any

  • resource_pattern_type (Integer)

    rd_kafka_ResourcePatternType_t value:

    • RD_KAFKA_RESOURCE_PATTERN_UNKNOWN = 0

    • RD_KAFKA_RESOURCE_PATTERN_ANY = 1

    • RD_KAFKA_RESOURCE_PATTERN_MATCH = 2

    • RD_KAFKA_RESOURCE_PATTERN_LITERAL = 3

    • RD_KAFKA_RESOURCE_PATTERN_PREFIXED = 4

  • principal (String, nil)

    principal (e.g., “User:alice”) or nil for any

  • host (String, nil)

    host address or nil for any

  • operation (Integer)

    rd_kafka_AclOperation_t value:

    • RD_KAFKA_ACL_OPERATION_ALL = 2

    • RD_KAFKA_ACL_OPERATION_READ = 3

    • RD_KAFKA_ACL_OPERATION_WRITE = 4

    • RD_KAFKA_ACL_OPERATION_CREATE = 5

    • RD_KAFKA_ACL_OPERATION_DELETE = 6

    • RD_KAFKA_ACL_OPERATION_ALTER = 7

    • RD_KAFKA_ACL_OPERATION_DESCRIBE = 8

    • RD_KAFKA_ACL_OPERATION_CLUSTER_ACTION = 9

    • RD_KAFKA_ACL_OPERATION_DESCRIBE_CONFIGS = 10

    • RD_KAFKA_ACL_OPERATION_ALTER_CONFIGS = 11

    • RD_KAFKA_ACL_OPERATION_IDEMPOTENT_WRITE = 12

  • permission_type (Integer)

    rd_kafka_AclPermissionType_t value:

    • RD_KAFKA_ACL_PERMISSION_TYPE_DENY = 2

    • RD_KAFKA_ACL_PERMISSION_TYPE_ALLOW = 3

Returns:

  • (DescribeAclHandle)

    Describe acl handle that can be used to wait for the result of fetching acls

Raises:



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/rdkafka/admin.rb', line 621

def describe_acl(resource_type:, resource_name:, resource_pattern_type:, principal:, host:, operation:, permission_type:)
  closed_admin_check(__method__)

  # Create a rd_kafka_AclBinding_t with the filters to fetch existing acls
  error_buffer = FFI::MemoryPointer.from_string(" " * 256)
  describe_acl_ptr = Rdkafka::Bindings.rd_kafka_AclBindingFilter_new(
    resource_type,
    resource_name ? FFI::MemoryPointer.from_string(resource_name) : nil,
    resource_pattern_type,
    principal ? FFI::MemoryPointer.from_string(principal) : nil,
    host ? FFI::MemoryPointer.from_string(host) : nil,
    operation,
    permission_type,
    error_buffer,
    256
  )
  if describe_acl_ptr.null?
    raise Rdkafka::Config::ConfigError.new(error_buffer.read_string)
  end

  # Get a pointer to the queue that our request will be enqueued on
  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end

  if queue_ptr.null?
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(new_acl_ptr)
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  # Create and register the handle that we will return to the caller
  describe_acl_handle = DescribeAclHandle.new
  describe_acl_handle[:pending] = true
  describe_acl_handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
  DescribeAclHandle.register(describe_acl_handle)

  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(inner, Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBEACLS)
  end

  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, describe_acl_handle.to_ptr)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_DescribeAcls(
        inner,
        describe_acl_ptr,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    DescribeAclHandle.remove(describe_acl_handle.to_ptr.address)
    raise
  ensure
    Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
    Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
    Rdkafka::Bindings.rd_kafka_AclBinding_destroy(describe_acl_ptr)
  end

  describe_acl_handle
end

#describe_configs(resources) ⇒ DescribeConfigsHandle

Note:

Several resources can be requested at one go, but only one broker at a time

Describe configs

Parameters:

  • resources (Array<Hash>)

    Array where elements are hashes with two keys:

    • ‘:resource_type` - numerical resource type based on Kafka API

    • ‘:resource_name` - string with resource name

Returns:

  • (DescribeConfigsHandle)

    Describe config handle that can be used to wait for the result of fetching resources with their appropriate configs

Raises:



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/rdkafka/admin.rb', line 695

def describe_configs(resources)
  closed_admin_check(__method__)

  handle = DescribeConfigsHandle.new
  handle[:pending] = true
  handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA

  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end

  if queue_ptr.null?
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(
      inner,
      Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS
    )
  end

  DescribeConfigsHandle.register(handle)
  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)

  pointer_array = resources.map do |resource_details|
    Rdkafka::Bindings.rd_kafka_ConfigResource_new(
      resource_details.fetch(:resource_type),
      FFI::MemoryPointer.from_string(
        resource_details.fetch(:resource_name)
      )
    )
  end

  configs_array_ptr = FFI::MemoryPointer.new(:pointer, pointer_array.size)
  configs_array_ptr.write_array_of_pointer(pointer_array)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_DescribeConfigs(
        inner,
        configs_array_ptr,
        pointer_array.size,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    DescribeConfigsHandle.remove(handle.to_ptr.address)

    raise
  ensure
    if configs_array_ptr
      Rdkafka::Bindings.rd_kafka_ConfigResource_destroy_array(
        configs_array_ptr,
        pointer_array.size
      )
    end
  end

  handle
end

#enable_background_queue_io_events(fd, payload = "\x01") ⇒ nil

Enable IO event notifications for background events

Parameters:

  • fd (Integer)

    file descriptor to signal (from IO.pipe or eventfd)

  • payload (String) (defaults to: "\x01")

    data to write to fd (default: “x01”)

Returns:

  • (nil)

Raises:



90
91
92
# File 'lib/rdkafka/admin.rb', line 90

def enable_background_queue_io_events(fd, payload = "\x01")
  @native_kafka.enable_background_queue_io_events(fd, payload)
end

#enable_queue_io_events(fd, payload = "\x01") ⇒ nil

Enable IO event notifications for fiber scheduler integration When admin operations complete, librdkafka will write to your FD

Parameters:

  • fd (Integer)

    file descriptor to signal (from IO.pipe or eventfd)

  • payload (String) (defaults to: "\x01")

    data to write to fd (default: “x01”)

Returns:

  • (nil)

Raises:



81
82
83
# File 'lib/rdkafka/admin.rb', line 81

def enable_queue_io_events(fd, payload = "\x01")
  @native_kafka.enable_main_queue_io_events(fd, payload)
end

#finalizerProc

Returns finalizer proc for closing the admin.

Returns:

  • (Proc)

    finalizer proc for closing the admin



96
97
98
# File 'lib/rdkafka/admin.rb', line 96

def finalizer
  ->(_) { close }
end

#incremental_alter_configs(resources_with_configs) ⇒ IncrementalAlterConfigsHandle

Note:

Several resources can be requested at one go, but only one broker at a time

Note:

The results won’t contain altered values but only the altered resources

Alters in an incremental way all the configs provided for given resources

name, value and the proper op_type to perform on this value.

Parameters:

  • resources_with_configs (Array<Hash>)

    resources with the configs key that contains

Returns:

  • (IncrementalAlterConfigsHandle)

    Incremental alter configs handle that can be used to wait for the result of altering resources with their appropriate configs

Raises:



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/rdkafka/admin.rb', line 770

def incremental_alter_configs(resources_with_configs)
  closed_admin_check(__method__)

  handle = IncrementalAlterConfigsHandle.new
  handle[:pending] = true
  handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA

  queue_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
  end

  if queue_ptr.null?
    raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
  end

  admin_options_ptr = @native_kafka.with_inner do |inner|
    Rdkafka::Bindings.rd_kafka_AdminOptions_new(
      inner,
      Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_INCREMENTALALTERCONFIGS
    )
  end

  IncrementalAlterConfigsHandle.register(handle)
  Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)

  # Tu poprawnie tworzyc
  pointer_array = resources_with_configs.map do |resource_details|
    # First build the appropriate resource representation
    resource_ptr = Rdkafka::Bindings.rd_kafka_ConfigResource_new(
      resource_details.fetch(:resource_type),
      FFI::MemoryPointer.from_string(
        resource_details.fetch(:resource_name)
      )
    )

    resource_details.fetch(:configs).each do |config|
      Bindings.rd_kafka_ConfigResource_add_incremental_config(
        resource_ptr,
        config.fetch(:name),
        config.fetch(:op_type),
        config.fetch(:value)
      )
    end

    resource_ptr
  end

  configs_array_ptr = FFI::MemoryPointer.new(:pointer, pointer_array.size)
  configs_array_ptr.write_array_of_pointer(pointer_array)

  begin
    @native_kafka.with_inner do |inner|
      Rdkafka::Bindings.rd_kafka_IncrementalAlterConfigs(
        inner,
        configs_array_ptr,
        pointer_array.size,
        admin_options_ptr,
        queue_ptr
      )
    end
  rescue Exception
    IncrementalAlterConfigsHandle.remove(handle.to_ptr.address)

    raise
  ensure
    if configs_array_ptr
      Rdkafka::Bindings.rd_kafka_ConfigResource_destroy_array(
        configs_array_ptr,
        pointer_array.size
      )
    end
  end

  handle
end

#metadata(topic_name = nil, timeout_ms = Defaults::METADATA_TIMEOUT_MS) ⇒ Metadata

Performs the metadata request using admin

Parameters:

  • topic_name (String, nil) (defaults to: nil)

    metadat about particular topic or all if nil

  • timeout_ms (Integer) (defaults to: Defaults::METADATA_TIMEOUT_MS)

    metadata request timeout

Returns:



105
106
107
108
109
110
111
# File 'lib/rdkafka/admin.rb', line 105

def (topic_name = nil, timeout_ms = Defaults::METADATA_TIMEOUT_MS)
  closed_admin_check(__method__)

  @native_kafka.with_inner do |inner|
    Metadata.new(inner, topic_name, timeout_ms)
  end
end

#nameString

Returns admin name.

Returns:

  • (String)

    admin name



68
69
70
71
72
# File 'lib/rdkafka/admin.rb', line 68

def name
  @name ||= @native_kafka.with_inner do |inner|
    ::Rdkafka::Bindings.rd_kafka_name(inner)
  end
end

#startObject

Note:

Not needed to run unless explicit start was disabled

Starts the native Kafka polling thread and kicks off the init polling



63
64
65
# File 'lib/rdkafka/admin.rb', line 63

def start
  @native_kafka.start
end