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
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
# Default timeout and timing values used throughout rdkafka-ruby.
|
|
5
|
+
#
|
|
6
|
+
# All timeout values can be overridden per-call via method parameters. These constants provide
|
|
7
|
+
# a central place to understand and reference the default values used across the library.
|
|
8
|
+
#
|
|
9
|
+
# @example Overriding a timeout per-call
|
|
10
|
+
# consumer.committed(timeout_ms: 5_000) # Use 5 seconds instead of default 2 seconds
|
|
11
|
+
#
|
|
12
|
+
# @example Checking the default value
|
|
13
|
+
# Rdkafka::Defaults::CONSUMER_COMMITTED_TIMEOUT_MS # => 2000
|
|
14
|
+
# @note These are rdkafka-ruby defaults, not librdkafka configuration options.
|
|
15
|
+
# For librdkafka options, see:
|
|
16
|
+
# https://github.com/confluentinc/librdkafka/blob/master/CONFIGURATION.md
|
|
17
|
+
module Defaults
|
|
18
|
+
# Consumer timeouts (in milliseconds)
|
|
19
|
+
|
|
20
|
+
# Default timeout for fetching committed offsets
|
|
21
|
+
# @see Consumer#committed
|
|
22
|
+
CONSUMER_COMMITTED_TIMEOUT_MS = 2_000
|
|
23
|
+
|
|
24
|
+
# Default timeout for fetching the cluster id
|
|
25
|
+
# @see Consumer#cluster_id
|
|
26
|
+
CONSUMER_CLUSTER_ID_TIMEOUT_MS = 1_000
|
|
27
|
+
|
|
28
|
+
# Default timeout for querying watermark offsets
|
|
29
|
+
# @see Consumer#query_watermark_offsets
|
|
30
|
+
CONSUMER_QUERY_WATERMARK_TIMEOUT_MS = 1_000
|
|
31
|
+
|
|
32
|
+
# Default timeout for lag calculation watermark queries
|
|
33
|
+
# @see Consumer#lag
|
|
34
|
+
CONSUMER_LAG_TIMEOUT_MS = 1_000
|
|
35
|
+
|
|
36
|
+
# Default timeout for offsets_for_times operation
|
|
37
|
+
# @see Consumer#offsets_for_times
|
|
38
|
+
CONSUMER_OFFSETS_FOR_TIMES_TIMEOUT_MS = 1_000
|
|
39
|
+
|
|
40
|
+
# Default poll timeout for Consumer#each iterator
|
|
41
|
+
# @see Consumer#each
|
|
42
|
+
CONSUMER_POLL_TIMEOUT_MS = 250
|
|
43
|
+
|
|
44
|
+
# Seek operation timeout (0 = non-blocking)
|
|
45
|
+
# @see Consumer#seek_by
|
|
46
|
+
CONSUMER_SEEK_TIMEOUT_MS = 0
|
|
47
|
+
|
|
48
|
+
# Events poll timeout (0 = non-blocking/async)
|
|
49
|
+
# @see Consumer#events_poll
|
|
50
|
+
CONSUMER_EVENTS_POLL_TIMEOUT_MS = 0
|
|
51
|
+
|
|
52
|
+
# Producer timeouts (in milliseconds)
|
|
53
|
+
|
|
54
|
+
# Default timeout for producer flush operation
|
|
55
|
+
# @see Producer#flush
|
|
56
|
+
PRODUCER_FLUSH_TIMEOUT_MS = 5_000
|
|
57
|
+
|
|
58
|
+
# Default flush timeout during purge operation
|
|
59
|
+
# @see Producer#purge
|
|
60
|
+
PRODUCER_PURGE_FLUSH_TIMEOUT_MS = 100
|
|
61
|
+
|
|
62
|
+
# Metadata timeouts (in milliseconds)
|
|
63
|
+
|
|
64
|
+
# Default timeout for metadata requests
|
|
65
|
+
# @see Admin#metadata
|
|
66
|
+
# @see Metadata#initialize
|
|
67
|
+
METADATA_TIMEOUT_MS = 2_000
|
|
68
|
+
|
|
69
|
+
# Handle wait timeouts (in milliseconds)
|
|
70
|
+
|
|
71
|
+
# Default maximum wait timeout for async handles (delivery, admin operations)
|
|
72
|
+
# @see AbstractHandle#wait
|
|
73
|
+
HANDLE_WAIT_TIMEOUT_MS = 60_000
|
|
74
|
+
|
|
75
|
+
# Native Kafka polling (in milliseconds)
|
|
76
|
+
|
|
77
|
+
# Default poll timeout for producer/admin native polling thread
|
|
78
|
+
# @see Config#producer
|
|
79
|
+
# @see Config#admin
|
|
80
|
+
NATIVE_KAFKA_POLL_TIMEOUT_MS = 100
|
|
81
|
+
|
|
82
|
+
# Internal timing (in milliseconds)
|
|
83
|
+
|
|
84
|
+
# Sleep interval during purge wait loop
|
|
85
|
+
# @see Producer#purge
|
|
86
|
+
PRODUCER_PURGE_SLEEP_INTERVAL_MS = 1
|
|
87
|
+
|
|
88
|
+
# Sleep interval while waiting for operations to complete in NativeKafka#synchronize
|
|
89
|
+
# @see NativeKafka#synchronize
|
|
90
|
+
NATIVE_KAFKA_SYNCHRONIZE_SLEEP_INTERVAL_MS = 10
|
|
91
|
+
|
|
92
|
+
# Base backoff factor for metadata retry in milliseconds (multiplied by 2^attempt)
|
|
93
|
+
# @see Metadata#initialize
|
|
94
|
+
METADATA_RETRY_BACKOFF_BASE_MS = 100
|
|
95
|
+
|
|
96
|
+
# Maximum backoff time between metadata retries. Caps the exponential backoff so a long retry
|
|
97
|
+
# sequence against an unhealthy cluster cannot block the calling thread for minutes.
|
|
98
|
+
# @see Metadata#initialize
|
|
99
|
+
METADATA_RETRY_BACKOFF_MAX_MS = 1_000
|
|
100
|
+
|
|
101
|
+
# Soft wall-clock budget for the whole metadata retry loop; past it (and past
|
|
102
|
+
# METADATA_MIN_ATTEMPTS) the loop stops so a synchronous fetch cannot block the caller for long
|
|
103
|
+
# @see Metadata#initialize
|
|
104
|
+
METADATA_RETRY_BUDGET_MS = 5_000
|
|
105
|
+
|
|
106
|
+
# Cache settings (in milliseconds)
|
|
107
|
+
|
|
108
|
+
# Default time-to-live for cached partition counts
|
|
109
|
+
# @see Producer::PartitionsCountCache
|
|
110
|
+
PARTITIONS_COUNT_CACHE_TTL_MS = 30_000
|
|
111
|
+
|
|
112
|
+
# Configuration values (not time-based)
|
|
113
|
+
|
|
114
|
+
# Maximum number of metadata fetch retry attempts
|
|
115
|
+
# @see Metadata#initialize
|
|
116
|
+
METADATA_MAX_RETRIES = 10
|
|
117
|
+
|
|
118
|
+
# Minimum metadata fetch attempts before the retry budget may end the loop, so a slow broker
|
|
119
|
+
# (whose requests each consume the full timeout) still gets a few tries
|
|
120
|
+
# @see Metadata#initialize
|
|
121
|
+
METADATA_MIN_ATTEMPTS = 3
|
|
122
|
+
end
|
|
123
|
+
end
|
data/lib/rdkafka/error.rb
CHANGED
|
@@ -18,12 +18,21 @@ module Rdkafka
|
|
|
18
18
|
# @return [String]
|
|
19
19
|
attr_reader :broker_message
|
|
20
20
|
|
|
21
|
+
# The name of the rdkafka instance that generated this error
|
|
22
|
+
# @return [String, nil]
|
|
23
|
+
attr_reader :instance_name
|
|
24
|
+
|
|
21
25
|
# @private
|
|
22
|
-
|
|
26
|
+
# @param response [Integer] the raw error response code from librdkafka
|
|
27
|
+
# @param message_prefix [String, nil] optional prefix for error messages
|
|
28
|
+
# @param broker_message [String, nil] optional error message from the broker
|
|
29
|
+
# @param instance_name [String, nil] optional name of the rdkafka instance
|
|
30
|
+
def initialize(response, message_prefix = nil, broker_message: nil, instance_name: nil)
|
|
23
31
|
raise TypeError.new("Response has to be an integer") unless response.is_a? Integer
|
|
24
32
|
@rdkafka_response = response
|
|
25
33
|
@message_prefix = message_prefix
|
|
26
34
|
@broker_message = broker_message
|
|
35
|
+
@instance_name = instance_name
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
# This error's code, for example `:partition_eof`, `:msg_size_too_large`.
|
|
@@ -31,7 +40,7 @@ module Rdkafka
|
|
|
31
40
|
def code
|
|
32
41
|
code = Rdkafka::Bindings.rd_kafka_err2name(@rdkafka_response).downcase
|
|
33
42
|
if code[0] == "_"
|
|
34
|
-
code[1
|
|
43
|
+
code[1..].to_sym
|
|
35
44
|
else
|
|
36
45
|
code.to_sym
|
|
37
46
|
end
|
|
@@ -41,11 +50,16 @@ module Rdkafka
|
|
|
41
50
|
# @return [String]
|
|
42
51
|
def to_s
|
|
43
52
|
message_prefix_part = if message_prefix
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
"#{message_prefix} - "
|
|
54
|
+
else
|
|
55
|
+
""
|
|
56
|
+
end
|
|
57
|
+
instance_name_part = if instance_name
|
|
58
|
+
" [#{instance_name}]"
|
|
59
|
+
else
|
|
60
|
+
""
|
|
61
|
+
end
|
|
62
|
+
"#{message_prefix_part}#{Rdkafka::Bindings.rd_kafka_err2str(@rdkafka_response)} (#{code})#{instance_name_part}"
|
|
49
63
|
end
|
|
50
64
|
|
|
51
65
|
# Whether this error indicates the partition is EOF.
|
|
@@ -55,8 +69,10 @@ module Rdkafka
|
|
|
55
69
|
end
|
|
56
70
|
|
|
57
71
|
# Error comparison
|
|
58
|
-
|
|
59
|
-
|
|
72
|
+
# @param other [Object] object to compare with
|
|
73
|
+
# @return [Boolean]
|
|
74
|
+
def ==(other)
|
|
75
|
+
other.is_a?(self.class) && (to_s == other.to_s)
|
|
60
76
|
end
|
|
61
77
|
end
|
|
62
78
|
|
|
@@ -66,7 +82,10 @@ module Rdkafka
|
|
|
66
82
|
attr_reader :topic_partition_list
|
|
67
83
|
|
|
68
84
|
# @private
|
|
69
|
-
|
|
85
|
+
# @param response [Integer] the raw error response code from librdkafka
|
|
86
|
+
# @param topic_partition_list [TopicPartitionList] the topic partition list with error info
|
|
87
|
+
# @param message_prefix [String, nil] optional prefix for error messages
|
|
88
|
+
def initialize(response, topic_partition_list, message_prefix = nil)
|
|
70
89
|
super(response, message_prefix)
|
|
71
90
|
@topic_partition_list = topic_partition_list
|
|
72
91
|
end
|
|
@@ -74,28 +93,35 @@ module Rdkafka
|
|
|
74
93
|
|
|
75
94
|
# Error class for public consumer method calls on a closed consumer.
|
|
76
95
|
class ClosedConsumerError < BaseError
|
|
96
|
+
# @param method [Symbol] the method that was called
|
|
77
97
|
def initialize(method)
|
|
78
|
-
super("Illegal call to #{method
|
|
98
|
+
super("Illegal call to #{method} on a closed consumer")
|
|
79
99
|
end
|
|
80
100
|
end
|
|
81
101
|
|
|
82
102
|
# Error class for public producer method calls on a closed producer.
|
|
83
103
|
class ClosedProducerError < BaseError
|
|
104
|
+
# @param method [Symbol] the method that was called
|
|
84
105
|
def initialize(method)
|
|
85
|
-
super("Illegal call to #{method
|
|
106
|
+
super("Illegal call to #{method} on a closed producer")
|
|
86
107
|
end
|
|
87
108
|
end
|
|
88
109
|
|
|
89
|
-
# Error class for public
|
|
110
|
+
# Error class for public admin method calls on a closed admin.
|
|
90
111
|
class ClosedAdminError < BaseError
|
|
112
|
+
# @param method [Symbol] the method that was called
|
|
91
113
|
def initialize(method)
|
|
92
|
-
super("Illegal call to #{method
|
|
114
|
+
super("Illegal call to #{method} on a closed admin")
|
|
93
115
|
end
|
|
94
116
|
end
|
|
95
117
|
|
|
118
|
+
# Error class for calls on a closed inner librdkafka instance.
|
|
96
119
|
class ClosedInnerError < BaseError
|
|
97
120
|
def initialize
|
|
98
121
|
super("Illegal call to a closed inner librdkafka instance")
|
|
99
122
|
end
|
|
100
123
|
end
|
|
124
|
+
|
|
125
|
+
# Error class for librdkafka library loading failures (e.g., glibc compatibility issues).
|
|
126
|
+
class LibraryLoadError < BaseError; end
|
|
101
127
|
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
module Helpers
|
|
5
|
+
# Shared `#list_offsets` implementation for Admin and Consumer.
|
|
6
|
+
#
|
|
7
|
+
# The librdkafka Admin API is handle-agnostic: `rd_kafka_ListOffsets()` can be issued on any
|
|
8
|
+
# `rd_kafka_t` instance regardless of its type, so this Ruby-level implementation is identical
|
|
9
|
+
# for admin and consumer clients. Includers must provide a private `#closed_check(method)`
|
|
10
|
+
# that raises their own `Closed*Error`, and must have the background event callback
|
|
11
|
+
# registered on their native config (see `Config#admin` and `Config#consumer`) so the result
|
|
12
|
+
# event is dispatched back to the returned handle.
|
|
13
|
+
module ListOffsets
|
|
14
|
+
# Queries partition offsets by specification (earliest, latest, max_timestamp, or by
|
|
15
|
+
# timestamp) without requiring a consumer group.
|
|
16
|
+
#
|
|
17
|
+
# The query is batched: all requested partitions are carried in one `ListOffsets` request
|
|
18
|
+
# that librdkafka fans out to the involved partition leaders internally and concurrently.
|
|
19
|
+
#
|
|
20
|
+
# @param topic_partition_offsets [Hash{String => Array<Hash>}] hash mapping topic names to
|
|
21
|
+
# arrays of partition offset specifications. Each specification is a hash with:
|
|
22
|
+
# - `:partition` [Integer] partition number
|
|
23
|
+
# - `:offset` [Symbol, Integer] offset specification - `:earliest`, `:latest`,
|
|
24
|
+
# `:max_timestamp`, or an integer timestamp in milliseconds
|
|
25
|
+
# @param isolation_level [Integer, nil] optional isolation level:
|
|
26
|
+
# - `RD_KAFKA_ISOLATION_LEVEL_READ_UNCOMMITTED` (0) - default
|
|
27
|
+
# - `RD_KAFKA_ISOLATION_LEVEL_READ_COMMITTED` (1)
|
|
28
|
+
#
|
|
29
|
+
# @return [Admin::ListOffsetsHandle] handle that can be used to wait for the result
|
|
30
|
+
#
|
|
31
|
+
# @raise [ClosedAdminError, ClosedConsumerError] when the client is closed
|
|
32
|
+
# @raise [ConfigError] when the background queue is unavailable
|
|
33
|
+
#
|
|
34
|
+
# @example Query earliest and latest offsets
|
|
35
|
+
# handle = client.list_offsets(
|
|
36
|
+
# { "my_topic" => [
|
|
37
|
+
# { partition: 0, offset: :earliest },
|
|
38
|
+
# { partition: 1, offset: :latest }
|
|
39
|
+
# ] }
|
|
40
|
+
# )
|
|
41
|
+
# report = handle.wait(max_wait_timeout_ms: 15_000)
|
|
42
|
+
# report.offsets
|
|
43
|
+
# # => [{ topic: "my_topic", partition: 0, offset: 0, ... }, ...]
|
|
44
|
+
def list_offsets(topic_partition_offsets, isolation_level: nil)
|
|
45
|
+
closed_check(__method__)
|
|
46
|
+
|
|
47
|
+
# Parse and validate every offset spec before allocating the native list, so a missing key
|
|
48
|
+
# or an unknown offset specification raises with nothing to clean up. Previously the
|
|
49
|
+
# ArgumentError (or KeyError) was raised after `rd_kafka_topic_partition_list_new`, leaking
|
|
50
|
+
# the native list.
|
|
51
|
+
parsed = topic_partition_offsets.flat_map do |topic, partitions|
|
|
52
|
+
partitions.map do |spec|
|
|
53
|
+
offset = spec.fetch(:offset)
|
|
54
|
+
|
|
55
|
+
native_offset = case offset
|
|
56
|
+
when :earliest then Rdkafka::Bindings::RD_KAFKA_OFFSET_SPEC_EARLIEST
|
|
57
|
+
when :latest then Rdkafka::Bindings::RD_KAFKA_OFFSET_SPEC_LATEST
|
|
58
|
+
when :max_timestamp then Rdkafka::Bindings::RD_KAFKA_OFFSET_SPEC_MAX_TIMESTAMP
|
|
59
|
+
when Integer then offset
|
|
60
|
+
else
|
|
61
|
+
raise ArgumentError, "Unknown offset specification: #{offset.inspect}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
[topic, spec.fetch(:partition), native_offset]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Build native topic partition list
|
|
69
|
+
tpl = Rdkafka::Bindings.rd_kafka_topic_partition_list_new(parsed.size)
|
|
70
|
+
|
|
71
|
+
parsed.each do |topic, partition, native_offset|
|
|
72
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_add(tpl, topic, partition)
|
|
73
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_set_offset(tpl, topic, partition, native_offset)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Get a pointer to the queue that our request will be enqueued on
|
|
77
|
+
queue_ptr = @native_kafka.with_inner do |inner|
|
|
78
|
+
Rdkafka::Bindings.rd_kafka_queue_get_background(inner)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
if queue_ptr.null?
|
|
82
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl)
|
|
83
|
+
raise Rdkafka::Config::ConfigError.new("rd_kafka_queue_get_background was NULL")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Create and register the handle we will return to the caller
|
|
87
|
+
handle = Admin::ListOffsetsHandle.new
|
|
88
|
+
handle[:pending] = true
|
|
89
|
+
handle[:response] = Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
|
|
90
|
+
|
|
91
|
+
admin_options_ptr = @native_kafka.with_inner do |inner|
|
|
92
|
+
Rdkafka::Bindings.rd_kafka_AdminOptions_new(
|
|
93
|
+
inner,
|
|
94
|
+
Rdkafka::Bindings::RD_KAFKA_ADMIN_OP_LISTOFFSETS
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
if isolation_level
|
|
99
|
+
Rdkafka::Bindings.rd_kafka_AdminOptions_set_isolation_level(admin_options_ptr, isolation_level)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
Admin::ListOffsetsHandle.register(handle)
|
|
103
|
+
Rdkafka::Bindings.rd_kafka_AdminOptions_set_opaque(admin_options_ptr, handle.to_ptr)
|
|
104
|
+
|
|
105
|
+
begin
|
|
106
|
+
@native_kafka.with_inner do |inner|
|
|
107
|
+
Rdkafka::Bindings.rd_kafka_ListOffsets(
|
|
108
|
+
inner,
|
|
109
|
+
tpl,
|
|
110
|
+
admin_options_ptr,
|
|
111
|
+
queue_ptr
|
|
112
|
+
)
|
|
113
|
+
end
|
|
114
|
+
rescue Exception
|
|
115
|
+
Admin::ListOffsetsHandle.remove(handle.to_ptr.address)
|
|
116
|
+
raise
|
|
117
|
+
ensure
|
|
118
|
+
Rdkafka::Bindings.rd_kafka_AdminOptions_destroy(admin_options_ptr)
|
|
119
|
+
Rdkafka::Bindings.rd_kafka_queue_destroy(queue_ptr)
|
|
120
|
+
Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
handle
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
module Helpers
|
|
5
|
+
# Shared `#metadata` implementation for Admin, Consumer and Producer.
|
|
6
|
+
#
|
|
7
|
+
# `rd_kafka_metadata()` is handle-agnostic in librdkafka - it works on any `rd_kafka_t`
|
|
8
|
+
# (consumer, producer or admin) - so this Ruby-level implementation is identical across all
|
|
9
|
+
# three client types. Includers must provide a private `#closed_check(method)` that raises
|
|
10
|
+
# their own `Closed*Error`.
|
|
11
|
+
module Metadata
|
|
12
|
+
# Performs the metadata request using this client
|
|
13
|
+
#
|
|
14
|
+
# @param topic_name [String, nil] metadata about particular topic or all if nil
|
|
15
|
+
# @param timeout_ms [Integer] metadata request timeout
|
|
16
|
+
# @return [Rdkafka::Metadata] requested metadata
|
|
17
|
+
def metadata(topic_name = nil, timeout_ms = Defaults::METADATA_TIMEOUT_MS)
|
|
18
|
+
closed_check(__method__)
|
|
19
|
+
|
|
20
|
+
@native_kafka.with_inner do |inner|
|
|
21
|
+
# Must stay fully qualified: this module is itself named `Metadata`, so a bare
|
|
22
|
+
# `Metadata.new` here would resolve to `Rdkafka::Helpers::Metadata` (no `.new`) instead
|
|
23
|
+
# of this class.
|
|
24
|
+
Rdkafka::Metadata.new(inner, topic_name, timeout_ms)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module Rdkafka
|
|
2
|
+
module Helpers
|
|
3
|
+
# OAuth helper methods for setting and refreshing SASL/OAUTHBEARER tokens
|
|
4
|
+
module OAuth
|
|
5
|
+
# Set the OAuthBearer token
|
|
6
|
+
#
|
|
7
|
+
# @param token [String] the mandatory token value to set, often (but not necessarily) a JWS
|
|
8
|
+
# compact serialization as per https://tools.ietf.org/html/rfc7515#section-3.1.
|
|
9
|
+
# @param lifetime_ms [Integer] when the token expires, in terms of the number of milliseconds
|
|
10
|
+
# since the epoch. See https://currentmillis.com/.
|
|
11
|
+
# @param principal_name [String] the mandatory Kafka principal name associated with the token.
|
|
12
|
+
# @param extensions [Hash] optional SASL extensions key-value pairs to be communicated to the
|
|
13
|
+
# broker as additional key-value pairs during the initial client response as per
|
|
14
|
+
# https://tools.ietf.org/html/rfc7628#section-3.1.
|
|
15
|
+
# @return [Integer] 0 on success
|
|
16
|
+
def oauthbearer_set_token(token:, lifetime_ms:, principal_name:, extensions: nil)
|
|
17
|
+
error_buffer = FFI::MemoryPointer.from_string(" " * 256)
|
|
18
|
+
extensions_ptr, extensions_str_ptrs = map_extensions(extensions)
|
|
19
|
+
|
|
20
|
+
begin
|
|
21
|
+
response = @native_kafka.with_inner do |inner|
|
|
22
|
+
Rdkafka::Bindings.rd_kafka_oauthbearer_set_token(
|
|
23
|
+
inner, token, lifetime_ms, principal_name,
|
|
24
|
+
extensions_ptr, extension_size(extensions), error_buffer, 256
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
ensure
|
|
28
|
+
extensions_str_ptrs&.each { |ptr| ptr.free }
|
|
29
|
+
extensions_ptr&.free
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
return response if response.zero?
|
|
33
|
+
|
|
34
|
+
oauthbearer_set_token_failure("Failed to set token: #{error_buffer.read_string}")
|
|
35
|
+
|
|
36
|
+
response
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Marks failed oauth token acquire in librdkafka
|
|
40
|
+
#
|
|
41
|
+
# @param reason [String] human readable error reason for failing to acquire token
|
|
42
|
+
def oauthbearer_set_token_failure(reason)
|
|
43
|
+
@native_kafka.with_inner do |inner|
|
|
44
|
+
Rdkafka::Bindings.rd_kafka_oauthbearer_set_token_failure(
|
|
45
|
+
inner,
|
|
46
|
+
reason
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Convert extensions hash to FFI::MemoryPointer (`const char **`).
|
|
54
|
+
#
|
|
55
|
+
# @param extensions [Hash, nil] extension key-value pairs
|
|
56
|
+
# @return [Array<FFI::MemoryPointer, Array<FFI::MemoryPointer>>] array pointer and string
|
|
57
|
+
# pointers
|
|
58
|
+
# @note The returned pointers must be freed manually (autorelease = false).
|
|
59
|
+
def map_extensions(extensions)
|
|
60
|
+
return [nil, nil] if extensions.nil? || extensions.empty?
|
|
61
|
+
|
|
62
|
+
# https://github.com/confluentinc/librdkafka/blob/master/src/rdkafka_sasl_oauthbearer.c#L327-L347
|
|
63
|
+
|
|
64
|
+
# The method argument is const char **
|
|
65
|
+
array_ptr = FFI::MemoryPointer.new(:pointer, extension_size(extensions))
|
|
66
|
+
array_ptr.autorelease = false
|
|
67
|
+
str_ptrs = []
|
|
68
|
+
|
|
69
|
+
# Element i is the key, i + 1 is the value.
|
|
70
|
+
extensions.each_with_index do |(k, v), i|
|
|
71
|
+
k_ptr = FFI::MemoryPointer.from_string(k.to_s)
|
|
72
|
+
k_ptr.autorelease = false
|
|
73
|
+
str_ptrs << k_ptr
|
|
74
|
+
v_ptr = FFI::MemoryPointer.from_string(v.to_s)
|
|
75
|
+
v_ptr.autorelease = false
|
|
76
|
+
str_ptrs << v_ptr
|
|
77
|
+
array_ptr[i * 2].put_pointer(0, k_ptr)
|
|
78
|
+
array_ptr[i * 2 + 1].put_pointer(0, v_ptr)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
[array_ptr, str_ptrs]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Returns the extension size (number of keys + values).
|
|
85
|
+
#
|
|
86
|
+
# @param extensions [Hash, nil] extension key-value pairs
|
|
87
|
+
# @return [Integer] non-negative even number representing keys + values count
|
|
88
|
+
# @see github.com/confluentinc/librdkafka/blob/master/src/rdkafka_sasl_oauthbearer.c#L327-L347
|
|
89
|
+
def extension_size(extensions)
|
|
90
|
+
return 0 unless extensions
|
|
91
|
+
extensions.size * 2
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
# Namespace for some small utilities used in multiple components
|
|
5
|
+
module Helpers
|
|
6
|
+
# Time related methods used across Karafka
|
|
7
|
+
module Time
|
|
8
|
+
# @return [Float] current monotonic time in seconds with microsecond precision
|
|
9
|
+
def monotonic_now
|
|
10
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# @return [Integer] current monotonic time in milliseconds
|
|
14
|
+
def monotonic_now_ms
|
|
15
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :millisecond)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|