rdkafka 0.13.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +388 -111
- data/{LICENSE → MIT-LICENSE} +2 -1
- data/README.md +90 -43
- data/Rakefile +21 -23
- data/bin/verify_kafka_warnings +44 -0
- data/dist/librdkafka-2.15.1.tar.gz +0 -0
- data/dist/patches/rdkafka_global_init.patch +15 -0
- data/docker-compose-ssl.yml +39 -0
- data/docker-compose.yml +22 -17
- data/ext/Rakefile +97 -30
- data/lib/rdkafka/abstract_handle.rb +113 -34
- data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
- data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
- data/lib/rdkafka/admin/config_binding_result.rb +30 -0
- data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
- data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
- data/lib/rdkafka/admin/create_acl_report.rb +26 -0
- data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
- data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
- data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
- data/lib/rdkafka/admin/create_topic_report.rb +4 -1
- data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
- data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
- data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
- data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
- data/lib/rdkafka/admin/delete_records_report.rb +25 -0
- data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
- data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
- data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
- data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
- data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
- data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
- data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
- data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
- data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
- data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
- data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
- data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
- data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
- data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
- data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
- data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
- data/lib/rdkafka/admin.rb +1381 -38
- data/lib/rdkafka/bindings/admin/acls.rb +85 -0
- data/lib/rdkafka/bindings/admin/configs.rb +37 -0
- data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
- data/lib/rdkafka/bindings/admin.rb +197 -0
- data/lib/rdkafka/bindings/callbacks.rb +100 -0
- data/lib/rdkafka/bindings/configuration.rb +50 -0
- data/lib/rdkafka/bindings/consumer.rb +83 -0
- data/lib/rdkafka/bindings/errors.rb +15 -0
- data/lib/rdkafka/bindings/handle.rb +16 -0
- data/lib/rdkafka/bindings/messages.rb +28 -0
- data/lib/rdkafka/bindings/metadata.rb +14 -0
- data/lib/rdkafka/bindings/polling.rb +17 -0
- data/lib/rdkafka/bindings/producer.rb +77 -0
- data/lib/rdkafka/bindings/structs.rb +62 -0
- data/lib/rdkafka/bindings.rb +69 -294
- data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
- data/lib/rdkafka/callbacks/base_handler.rb +62 -0
- data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
- data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
- data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
- data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
- data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
- data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
- data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
- data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
- data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
- data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
- data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
- data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
- data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
- data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
- data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
- data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
- data/lib/rdkafka/callbacks.rb +273 -52
- data/lib/rdkafka/clients.rb +81 -0
- data/lib/rdkafka/config.rb +333 -91
- data/lib/rdkafka/consumer/allocator.rb +38 -0
- data/lib/rdkafka/consumer/headers.rb +39 -25
- data/lib/rdkafka/consumer/message.rb +18 -20
- data/lib/rdkafka/consumer/partition.rb +15 -4
- data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
- data/lib/rdkafka/consumer.rb +765 -192
- data/lib/rdkafka/defaults.rb +123 -0
- data/lib/rdkafka/error.rb +40 -14
- data/lib/rdkafka/helpers/list_offsets.rb +127 -0
- data/lib/rdkafka/helpers/metadata.rb +29 -0
- data/lib/rdkafka/helpers/oauth.rb +95 -0
- data/lib/rdkafka/helpers/time.rb +19 -0
- data/lib/rdkafka/metadata.rb +138 -26
- data/lib/rdkafka/native_kafka.rb +142 -22
- data/lib/rdkafka/producer/delivery_handle.rb +27 -7
- data/lib/rdkafka/producer/delivery_report.rb +71 -4
- data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
- data/lib/rdkafka/producer.rb +422 -81
- data/lib/rdkafka/version.rb +6 -3
- data/lib/rdkafka.rb +73 -1
- data/package-lock.json +331 -0
- data/package.json +9 -0
- data/rdkafka.gemspec +78 -25
- data/renovate.json +111 -0
- metadata +114 -134
- data/.gitignore +0 -8
- data/.rspec +0 -1
- data/.semaphore/semaphore.yml +0 -27
- data/.yardopts +0 -2
- data/Gemfile +0 -5
- data/Guardfile +0 -19
- data/ext/README.md +0 -18
- data/spec/rdkafka/abstract_handle_spec.rb +0 -115
- data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
- data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
- data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
- data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
- data/spec/rdkafka/admin_spec.rb +0 -204
- data/spec/rdkafka/bindings_spec.rb +0 -136
- data/spec/rdkafka/callbacks_spec.rb +0 -22
- data/spec/rdkafka/config_spec.rb +0 -197
- data/spec/rdkafka/consumer/headers_spec.rb +0 -62
- data/spec/rdkafka/consumer/message_spec.rb +0 -141
- data/spec/rdkafka/consumer/partition_spec.rb +0 -59
- data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
- data/spec/rdkafka/consumer_spec.rb +0 -1106
- data/spec/rdkafka/error_spec.rb +0 -91
- data/spec/rdkafka/metadata_spec.rb +0 -80
- data/spec/rdkafka/native_kafka_spec.rb +0 -124
- data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
- data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
- data/spec/rdkafka/producer_spec.rb +0 -557
- data/spec/spec_helper.rb +0 -155
|
@@ -4,44 +4,49 @@ module Rdkafka
|
|
|
4
4
|
class Consumer
|
|
5
5
|
# Interface to return headers for a consumer message
|
|
6
6
|
module Headers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
super
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
7
|
+
# Empty frozen hash used when there are no headers
|
|
8
|
+
EMPTY_HEADERS = {}.freeze
|
|
9
|
+
|
|
10
|
+
# Key under which reusable scratch pointers are stored on the current thread/fiber
|
|
11
|
+
SCRATCH_KEY = :rdkafka_headers_scratch
|
|
12
|
+
|
|
13
|
+
private_constant :SCRATCH_KEY
|
|
18
14
|
|
|
19
15
|
# Reads a librdkafka native message's headers and returns them as a Ruby Hash
|
|
16
|
+
# where each key maps to either a String (single value) or Array<String> (multiple values)
|
|
17
|
+
# to support duplicate headers per KIP-82
|
|
20
18
|
#
|
|
21
|
-
# @
|
|
22
|
-
#
|
|
23
|
-
# @return [Hash<String, String>] headers Hash for the native_message
|
|
19
|
+
# @private
|
|
24
20
|
#
|
|
21
|
+
# @param native_message [Rdkafka::Bindings::Message] the native message to read headers from
|
|
22
|
+
# @return [Hash{String => String, Array<String>}] headers Hash for the native_message
|
|
25
23
|
# @raise [Rdkafka::RdkafkaError] when fail to read headers
|
|
26
|
-
#
|
|
27
|
-
# @private
|
|
28
24
|
def self.from_native(native_message)
|
|
29
|
-
|
|
25
|
+
# Scratch output pointers for the header FFI calls, reused across messages. They are
|
|
26
|
+
# stored in fiber-local storage (`Thread.current[]` is fiber-local by design), so each
|
|
27
|
+
# thread and each fiber gets its own set and reuse is safe with fiber schedulers. They
|
|
28
|
+
# never escape this method and all data is read out of them before returning.
|
|
29
|
+
# Allocating them per message would cost several native allocations for every consumed
|
|
30
|
+
# message.
|
|
31
|
+
headers_ptrptr, name_ptrptr, value_ptrptr, size_ptr =
|
|
32
|
+
Thread.current[SCRATCH_KEY] ||= [
|
|
33
|
+
FFI::MemoryPointer.new(:pointer),
|
|
34
|
+
FFI::MemoryPointer.new(:pointer),
|
|
35
|
+
FFI::MemoryPointer.new(:pointer),
|
|
36
|
+
Rdkafka::Bindings::SizePtr.new
|
|
37
|
+
].freeze
|
|
38
|
+
|
|
30
39
|
err = Rdkafka::Bindings.rd_kafka_message_headers(native_message, headers_ptrptr)
|
|
31
40
|
|
|
32
41
|
if err == Rdkafka::Bindings::RD_KAFKA_RESP_ERR__NOENT
|
|
33
|
-
return
|
|
42
|
+
return EMPTY_HEADERS
|
|
34
43
|
elsif err != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
35
44
|
raise Rdkafka::RdkafkaError.new(err, "Error reading message headers")
|
|
36
45
|
end
|
|
37
46
|
|
|
38
47
|
headers_ptr = headers_ptrptr.read_pointer
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
value_ptrptr = FFI::MemoryPointer.new(:pointer)
|
|
42
|
-
size_ptr = Rdkafka::Bindings::SizePtr.new
|
|
43
|
-
|
|
44
|
-
headers = HashWithSymbolKeysTreatedLikeStrings.new
|
|
49
|
+
headers = {}
|
|
45
50
|
|
|
46
51
|
idx = 0
|
|
47
52
|
loop do
|
|
@@ -65,10 +70,19 @@ module Rdkafka
|
|
|
65
70
|
size = size_ptr[:value]
|
|
66
71
|
|
|
67
72
|
value_ptr = value_ptrptr.read_pointer
|
|
68
|
-
|
|
69
73
|
value = value_ptr.read_string(size)
|
|
70
74
|
|
|
71
|
-
headers
|
|
75
|
+
if headers.key?(name)
|
|
76
|
+
# If we've seen this header before, convert to array if needed and append
|
|
77
|
+
if headers[name].is_a?(Array)
|
|
78
|
+
headers[name] << value
|
|
79
|
+
else
|
|
80
|
+
headers[name] = [headers[name], value]
|
|
81
|
+
end
|
|
82
|
+
else
|
|
83
|
+
# First occurrence - store as single value
|
|
84
|
+
headers[name] = value
|
|
85
|
+
end
|
|
72
86
|
|
|
73
87
|
idx += 1
|
|
74
88
|
end
|
|
@@ -28,37 +28,33 @@ module Rdkafka
|
|
|
28
28
|
# @return [Time, nil]
|
|
29
29
|
attr_reader :timestamp
|
|
30
30
|
|
|
31
|
-
# @return [Hash
|
|
31
|
+
# @return [Hash{String => String}] message headers
|
|
32
32
|
attr_reader :headers
|
|
33
33
|
|
|
34
34
|
# @private
|
|
35
|
+
# @param native_message [Rdkafka::Bindings::Message] native message struct from librdkafka
|
|
35
36
|
def initialize(native_message)
|
|
36
37
|
# Set topic
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
38
|
+
rkt = native_message[:rkt]
|
|
39
|
+
@topic = Rdkafka::Bindings.rd_kafka_topic_name(rkt) unless rkt.null?
|
|
40
40
|
# Set partition
|
|
41
41
|
@partition = native_message[:partition]
|
|
42
42
|
# Set payload
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
end
|
|
43
|
+
payload = native_message[:payload]
|
|
44
|
+
@payload = payload.read_string(native_message[:len]) unless payload.null?
|
|
46
45
|
# Set key
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
46
|
+
key = native_message[:key]
|
|
47
|
+
@key = key.read_string(native_message[:key_len]) unless key.null?
|
|
50
48
|
# Set offset
|
|
51
49
|
@offset = native_message[:offset]
|
|
52
50
|
# Set timestamp
|
|
53
51
|
raw_timestamp = Rdkafka::Bindings.rd_kafka_message_timestamp(native_message, nil)
|
|
54
52
|
@timestamp = if raw_timestamp && raw_timestamp > -1
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
nil
|
|
61
|
-
end
|
|
53
|
+
# Calculate seconds and microseconds
|
|
54
|
+
seconds = raw_timestamp / 1000
|
|
55
|
+
milliseconds = (raw_timestamp - seconds * 1000) * 1000
|
|
56
|
+
Time.at(seconds, milliseconds)
|
|
57
|
+
end
|
|
62
58
|
|
|
63
59
|
@headers = Headers.from_native(native_message)
|
|
64
60
|
end
|
|
@@ -71,6 +67,11 @@ module Rdkafka
|
|
|
71
67
|
"<Message in '#{topic}' with key '#{truncate(key)}', payload '#{truncate(payload)}', partition #{partition}, offset #{offset}, timestamp #{timestamp}#{is_headers}>"
|
|
72
68
|
end
|
|
73
69
|
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
# Truncates a string for display purposes
|
|
73
|
+
# @param string [String, nil] the string to truncate
|
|
74
|
+
# @return [String, nil] truncated string or nil
|
|
74
75
|
def truncate(string)
|
|
75
76
|
if string && string.length > 40
|
|
76
77
|
"#{string[0..39]}..."
|
|
@@ -78,9 +79,6 @@ module Rdkafka
|
|
|
78
79
|
string
|
|
79
80
|
end
|
|
80
81
|
end
|
|
81
|
-
|
|
82
|
-
private
|
|
83
|
-
|
|
84
82
|
end
|
|
85
83
|
end
|
|
86
84
|
end
|
|
@@ -16,11 +16,20 @@ module Rdkafka
|
|
|
16
16
|
# @return [Integer]
|
|
17
17
|
attr_reader :err
|
|
18
18
|
|
|
19
|
+
# Partition metadata in the context of a consumer
|
|
20
|
+
# @return [String, nil]
|
|
21
|
+
attr_reader :metadata
|
|
22
|
+
|
|
19
23
|
# @private
|
|
20
|
-
|
|
24
|
+
# @param partition [Integer] partition number
|
|
25
|
+
# @param offset [Integer, nil] partition offset
|
|
26
|
+
# @param err [Integer] error code from librdkafka
|
|
27
|
+
# @param metadata [String, nil] partition metadata
|
|
28
|
+
def initialize(partition, offset, err = Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR, metadata = nil)
|
|
21
29
|
@partition = partition
|
|
22
30
|
@offset = offset
|
|
23
31
|
@err = err
|
|
32
|
+
@metadata = metadata
|
|
24
33
|
end
|
|
25
34
|
|
|
26
35
|
# Human readable representation of this partition.
|
|
@@ -28,7 +37,8 @@ module Rdkafka
|
|
|
28
37
|
def to_s
|
|
29
38
|
message = "<Partition #{partition}"
|
|
30
39
|
message += " offset=#{offset}" if offset
|
|
31
|
-
message += " err=#{err}" if err !=
|
|
40
|
+
message += " err=#{err}" if err != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
|
|
41
|
+
message += " metadata=#{metadata}" if !metadata.nil?
|
|
32
42
|
message += ">"
|
|
33
43
|
message
|
|
34
44
|
end
|
|
@@ -40,11 +50,12 @@ module Rdkafka
|
|
|
40
50
|
end
|
|
41
51
|
|
|
42
52
|
# Whether another partition is equal to this
|
|
53
|
+
# @param other [Object] object to compare with
|
|
43
54
|
# @return [Boolean]
|
|
44
55
|
def ==(other)
|
|
45
56
|
self.class == other.class &&
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
partition == other.partition &&
|
|
58
|
+
offset == other.offset
|
|
48
59
|
end
|
|
49
60
|
end
|
|
50
61
|
end
|
|
@@ -6,10 +6,11 @@ module Rdkafka
|
|
|
6
6
|
class TopicPartitionList
|
|
7
7
|
# Create a topic partition list.
|
|
8
8
|
#
|
|
9
|
-
# @param data [Hash{String => nil,Partition}] The topic and partition data or nil to create an
|
|
9
|
+
# @param data [Hash{String => nil,Partition}] The topic and partition data or nil to create an
|
|
10
|
+
# empty list
|
|
10
11
|
#
|
|
11
12
|
# @return [TopicPartitionList]
|
|
12
|
-
def initialize(data=nil)
|
|
13
|
+
def initialize(data = nil)
|
|
13
14
|
@data = data || {}
|
|
14
15
|
end
|
|
15
16
|
|
|
@@ -18,10 +19,10 @@ module Rdkafka
|
|
|
18
19
|
def count
|
|
19
20
|
i = 0
|
|
20
21
|
@data.each do |_topic, partitions|
|
|
21
|
-
if partitions
|
|
22
|
-
|
|
22
|
+
i += if partitions
|
|
23
|
+
partitions.count
|
|
23
24
|
else
|
|
24
|
-
|
|
25
|
+
1
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
i
|
|
@@ -34,7 +35,14 @@ module Rdkafka
|
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
# Add a topic with optionally partitions to the list.
|
|
37
|
-
# Calling this method multiple times for the same topic will overwrite the previous
|
|
38
|
+
# Calling this method multiple times for the same topic will overwrite the previous
|
|
39
|
+
# configuraton.
|
|
40
|
+
#
|
|
41
|
+
# @param topic [String] The topic's name
|
|
42
|
+
# @param partitions [Array<Integer>, Range<Integer>, Integer] The topic's partitions or
|
|
43
|
+
# partition count
|
|
44
|
+
#
|
|
45
|
+
# @return [nil]
|
|
38
46
|
#
|
|
39
47
|
# @example Add a topic with unassigned partitions
|
|
40
48
|
# tpl.add_topic("topic")
|
|
@@ -44,34 +52,33 @@ module Rdkafka
|
|
|
44
52
|
#
|
|
45
53
|
# @example Add a topic with all topics up to a count
|
|
46
54
|
# tpl.add_topic("topic", 9)
|
|
47
|
-
|
|
48
|
-
# @param topic [String] The topic's name
|
|
49
|
-
# @param partitions [Array<Integer>, Range<Integer>, Integer] The topic's partitions or partition count
|
|
50
|
-
#
|
|
51
|
-
# @return [nil]
|
|
52
|
-
def add_topic(topic, partitions=nil)
|
|
55
|
+
def add_topic(topic, partitions = nil)
|
|
53
56
|
if partitions.nil?
|
|
54
57
|
@data[topic.to_s] = nil
|
|
55
58
|
else
|
|
56
59
|
if partitions.is_a? Integer
|
|
57
60
|
partitions = (0..partitions - 1)
|
|
58
61
|
end
|
|
59
|
-
@data[topic.to_s] = partitions.map { |p| Partition.new(p, nil,
|
|
62
|
+
@data[topic.to_s] = partitions.map { |p| Partition.new(p, nil, Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR) }
|
|
60
63
|
end
|
|
61
64
|
end
|
|
62
65
|
|
|
63
66
|
# Add a topic with partitions and offsets set to the list
|
|
64
|
-
# Calling this method multiple times for the same topic will overwrite the previous
|
|
67
|
+
# Calling this method multiple times for the same topic will overwrite the previous
|
|
68
|
+
# configuraton.
|
|
65
69
|
#
|
|
66
70
|
# @param topic [String] The topic's name
|
|
67
|
-
# @param partitions_with_offsets [Hash
|
|
68
|
-
#
|
|
71
|
+
# @param partitions_with_offsets [Hash{Integer => Integer}, Array<Consumer::Partition>] The
|
|
72
|
+
# topic's partitions and offsets (Hash) or partitions with offsets and metadata (Array)
|
|
69
73
|
# @return [nil]
|
|
70
74
|
def add_topic_and_partitions_with_offsets(topic, partitions_with_offsets)
|
|
71
|
-
@data[topic.to_s] = partitions_with_offsets.map
|
|
75
|
+
@data[topic.to_s] = partitions_with_offsets.map do |p, o|
|
|
76
|
+
p.is_a?(Partition) ? p : Partition.new(p, o)
|
|
77
|
+
end
|
|
72
78
|
end
|
|
73
79
|
|
|
74
|
-
# Return a `Hash` with the topics as keys and and an array of partition information as the
|
|
80
|
+
# Return a `Hash` with the topics as keys and and an array of partition information as the
|
|
81
|
+
# value if present.
|
|
75
82
|
#
|
|
76
83
|
# @return [Hash{String => Array<Partition>,nil}]
|
|
77
84
|
def to_h
|
|
@@ -84,17 +91,21 @@ module Rdkafka
|
|
|
84
91
|
"<TopicPartitionList: #{to_h}>"
|
|
85
92
|
end
|
|
86
93
|
|
|
94
|
+
# Check equality with another TopicPartitionList
|
|
95
|
+
# @param other [TopicPartitionList] object to compare with
|
|
96
|
+
# @return [Boolean]
|
|
87
97
|
def ==(other)
|
|
88
|
-
|
|
98
|
+
to_h == other.to_h
|
|
89
99
|
end
|
|
90
100
|
|
|
91
101
|
# Create a new topic partition list based of a native one.
|
|
92
102
|
#
|
|
93
|
-
# @
|
|
103
|
+
# @private
|
|
94
104
|
#
|
|
95
|
-
# @
|
|
105
|
+
# @param pointer [FFI::Pointer] Optional pointer to an existing native list. Its contents will
|
|
106
|
+
# be copied.
|
|
96
107
|
#
|
|
97
|
-
# @
|
|
108
|
+
# @return [TopicPartitionList]
|
|
98
109
|
def self.from_native_tpl(pointer)
|
|
99
110
|
# Data to be moved into the tpl
|
|
100
111
|
data = {}
|
|
@@ -104,16 +115,21 @@ module Rdkafka
|
|
|
104
115
|
native_tpl[:cnt].times do |i|
|
|
105
116
|
ptr = native_tpl[:elems] + (i * Rdkafka::Bindings::TopicPartition.size)
|
|
106
117
|
elem = Rdkafka::Bindings::TopicPartition.new(ptr)
|
|
107
|
-
if elem[:partition] ==
|
|
118
|
+
if elem[:partition] == Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
|
|
108
119
|
data[elem[:topic]] = nil
|
|
109
120
|
else
|
|
110
121
|
partitions = data[elem[:topic]] || []
|
|
111
122
|
offset = if elem[:offset] == Rdkafka::Bindings::RD_KAFKA_OFFSET_INVALID
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
partition = Partition.new(
|
|
123
|
+
nil
|
|
124
|
+
else
|
|
125
|
+
elem[:offset]
|
|
126
|
+
end
|
|
127
|
+
partition = Partition.new(
|
|
128
|
+
elem[:partition],
|
|
129
|
+
offset,
|
|
130
|
+
elem[:err],
|
|
131
|
+
elem[:metadata].null? ? nil : elem[:metadata].read_string(elem[:metadata_size])
|
|
132
|
+
)
|
|
117
133
|
partitions.push(partition)
|
|
118
134
|
data[elem[:topic]] = partitions
|
|
119
135
|
end
|
|
@@ -127,36 +143,54 @@ module Rdkafka
|
|
|
127
143
|
#
|
|
128
144
|
# The pointer will be cleaned by `rd_kafka_topic_partition_list_destroy` when GC releases it.
|
|
129
145
|
#
|
|
130
|
-
# @return [FFI::Pointer]
|
|
131
146
|
# @private
|
|
147
|
+
# @return [FFI::Pointer]
|
|
132
148
|
def to_native_tpl
|
|
133
149
|
tpl = Rdkafka::Bindings.rd_kafka_topic_partition_list_new(count)
|
|
134
150
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
partitions
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
topic,
|
|
141
|
-
p.partition
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
if p.offset
|
|
145
|
-
Rdkafka::Bindings.rd_kafka_topic_partition_list_set_offset(
|
|
151
|
+
begin
|
|
152
|
+
@data.each do |topic, partitions|
|
|
153
|
+
if partitions
|
|
154
|
+
partitions.each do |p|
|
|
155
|
+
ref = Rdkafka::Bindings.rd_kafka_topic_partition_list_add(
|
|
146
156
|
tpl,
|
|
147
157
|
topic,
|
|
148
|
-
p.partition
|
|
149
|
-
p.offset
|
|
158
|
+
p.partition
|
|
150
159
|
)
|
|
160
|
+
|
|
161
|
+
if p.metadata
|
|
162
|
+
part = Rdkafka::Bindings::TopicPartition.new(ref)
|
|
163
|
+
# The metadata string is owned by librdkafka once handed over and released here:
|
|
164
|
+
# https://github.com/confluentinc/librdkafka/blob/e03d3bb91ed92a38f38d9806b8d8deffe78a1de5/src/rdkafka_partition.c#L2682C18-L2682C18
|
|
165
|
+
str_ptr = Allocator.string_pointer(p.metadata)
|
|
166
|
+
part[:metadata] = str_ptr
|
|
167
|
+
part[:metadata_size] = p.metadata.bytesize
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
if p.offset
|
|
171
|
+
offset = p.offset.is_a?(Time) ? p.offset.to_f * 1_000 : p.offset
|
|
172
|
+
|
|
173
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_set_offset(
|
|
174
|
+
tpl,
|
|
175
|
+
topic,
|
|
176
|
+
p.partition,
|
|
177
|
+
offset
|
|
178
|
+
)
|
|
179
|
+
end
|
|
151
180
|
end
|
|
181
|
+
else
|
|
182
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_add(
|
|
183
|
+
tpl,
|
|
184
|
+
topic,
|
|
185
|
+
Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
|
|
186
|
+
)
|
|
152
187
|
end
|
|
153
|
-
else
|
|
154
|
-
Rdkafka::Bindings.rd_kafka_topic_partition_list_add(
|
|
155
|
-
tpl,
|
|
156
|
-
topic,
|
|
157
|
-
-1
|
|
158
|
-
)
|
|
159
188
|
end
|
|
189
|
+
rescue Exception
|
|
190
|
+
# The native list is caller-owned until it is handed to librdkafka. If population raises
|
|
191
|
+
# partway (e.g. an invalid partition), destroy it here so it is not leaked.
|
|
192
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl)
|
|
193
|
+
raise
|
|
160
194
|
end
|
|
161
195
|
|
|
162
196
|
tpl
|