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,212 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rdkafka
|
|
4
|
+
class Producer
|
|
5
|
+
# Caching mechanism for Kafka topic partition counts to avoid frequent cluster queries
|
|
6
|
+
#
|
|
7
|
+
# This cache is designed to optimize the process of obtaining partition counts for topics.
|
|
8
|
+
# It uses several strategies to minimize Kafka cluster queries:
|
|
9
|
+
#
|
|
10
|
+
# @note Design considerations:
|
|
11
|
+
#
|
|
12
|
+
# 1. Statistics-based updates
|
|
13
|
+
# When statistics callbacks are enabled (via `statistics.interval.ms`), we leverage
|
|
14
|
+
# this data to proactively update the partition counts cache. This approach costs
|
|
15
|
+
# approximately 0.02ms of processing time during each statistics interval (typically
|
|
16
|
+
# every 5 seconds) but eliminates the need for explicit blocking metadata queries.
|
|
17
|
+
#
|
|
18
|
+
# 2. Edge case handling
|
|
19
|
+
# If a user configures `statistics.interval.ms` much higher than the default cache TTL
|
|
20
|
+
# ({Defaults::PARTITIONS_COUNT_CACHE_TTL_MS}ms), the cache will still function correctly.
|
|
21
|
+
# When statistics updates don't occur frequently enough, the cache entries will expire
|
|
22
|
+
# naturally, triggering a blocking refresh when needed.
|
|
23
|
+
#
|
|
24
|
+
# 3. User configuration awareness
|
|
25
|
+
# The cache respects user-defined settings. If `topic.metadata.refresh.interval.ms` is
|
|
26
|
+
# set very high, the responsibility for potentially stale data falls on the user. This
|
|
27
|
+
# is an explicit design choice to honor user configuration preferences and align with
|
|
28
|
+
# librdkafka settings.
|
|
29
|
+
#
|
|
30
|
+
# 4. Process-wide efficiency
|
|
31
|
+
# Since this cache is shared across all Rdkafka producers and consumers within a process,
|
|
32
|
+
# having multiple clients improves overall efficiency. Each client contributes to keeping
|
|
33
|
+
# the cache updated, benefiting all other clients.
|
|
34
|
+
#
|
|
35
|
+
# 5. Thread-safety approach
|
|
36
|
+
# The implementation uses fine-grained locking with per-topic mutexes to minimize
|
|
37
|
+
# contention in multi-threaded environments while ensuring data consistency.
|
|
38
|
+
#
|
|
39
|
+
# 6. Topic recreation handling
|
|
40
|
+
# If a topic is deleted and recreated with fewer partitions, the cache keeps reporting the
|
|
41
|
+
# higher count only until the entry's TTL expires. The first refresh after expiry performs
|
|
42
|
+
# an authoritative metadata read and adopts the lower count. Within the TTL window a lower
|
|
43
|
+
# value is still ignored, so a transient or racy lower read cannot clobber a correct higher
|
|
44
|
+
# count.
|
|
45
|
+
class PartitionsCountCache
|
|
46
|
+
include Helpers::Time
|
|
47
|
+
|
|
48
|
+
# Creates a new partition count cache
|
|
49
|
+
#
|
|
50
|
+
# @param ttl [Integer, nil] DEPRECATED: Use ttl_ms instead.
|
|
51
|
+
# Time-to-live in seconds for cached values. Will be removed in v1.0.0.
|
|
52
|
+
# @param ttl_ms [Integer, nil] Time-to-live in milliseconds for cached values.
|
|
53
|
+
# Defaults to {Defaults::PARTITIONS_COUNT_CACHE_TTL_MS}.
|
|
54
|
+
def initialize(ttl = :not_provided, ttl_ms: :not_provided)
|
|
55
|
+
@counts = {}
|
|
56
|
+
@mutex_hash = {}
|
|
57
|
+
# Used only for @mutex_hash access to ensure thread-safety when creating new mutexes
|
|
58
|
+
@mutex_for_hash = Mutex.new
|
|
59
|
+
|
|
60
|
+
# Determine which TTL value to use
|
|
61
|
+
if ttl != :not_provided && ttl_ms != :not_provided
|
|
62
|
+
warn "DEPRECATION WARNING: Both ttl and ttl_ms were provided to PartitionsCountCache. " \
|
|
63
|
+
"Using ttl_ms. The ttl parameter is deprecated and will be removed in v1.0.0."
|
|
64
|
+
@ttl_ms = ttl_ms
|
|
65
|
+
elsif ttl != :not_provided
|
|
66
|
+
warn "DEPRECATION WARNING: ttl (seconds) parameter for PartitionsCountCache is deprecated. " \
|
|
67
|
+
"Use ttl_ms (milliseconds) instead. This parameter will be removed in v1.0.0."
|
|
68
|
+
@ttl_ms = (ttl * 1000).to_i
|
|
69
|
+
elsif ttl_ms == :not_provided
|
|
70
|
+
@ttl_ms = Defaults::PARTITIONS_COUNT_CACHE_TTL_MS
|
|
71
|
+
else
|
|
72
|
+
@ttl_ms = ttl_ms
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Reads partition count for a topic with automatic refresh when expired
|
|
77
|
+
#
|
|
78
|
+
# This method will return the cached partition count if available and not expired.
|
|
79
|
+
# If the value is expired or not available, it will execute the provided block
|
|
80
|
+
# to fetch the current value from Kafka.
|
|
81
|
+
#
|
|
82
|
+
# @param topic [String] Kafka topic name
|
|
83
|
+
# @yield Block that returns the current partition count when cache needs refreshing
|
|
84
|
+
# @yieldreturn [Integer] Current partition count retrieved from Kafka
|
|
85
|
+
# @return [Integer] Partition count for the topic
|
|
86
|
+
#
|
|
87
|
+
# @note The implementation prioritizes read performance over write consistency
|
|
88
|
+
# since partition counts typically only increase during normal operation.
|
|
89
|
+
def get(topic)
|
|
90
|
+
current_info = @counts[topic]
|
|
91
|
+
|
|
92
|
+
if current_info.nil? || expired?(current_info[0])
|
|
93
|
+
# The cached entry is missing or expired, so the block performs an authoritative metadata
|
|
94
|
+
# read. We hand it to `set`, which adopts a higher count always and a lower count once the
|
|
95
|
+
# entry has expired (e.g. the topic was recreated with fewer partitions). We then return
|
|
96
|
+
# whatever `set` settled on so a concurrent refresh that wrote a higher value still wins.
|
|
97
|
+
new_count = yield
|
|
98
|
+
set(topic, new_count)
|
|
99
|
+
|
|
100
|
+
return @counts[topic][1]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
current_info[1]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Update partition count for a topic when needed
|
|
107
|
+
#
|
|
108
|
+
# This method updates the partition count for a topic in the cache.
|
|
109
|
+
# It uses a mutex to ensure thread-safety during updates.
|
|
110
|
+
#
|
|
111
|
+
# @param topic [String] Kafka topic name
|
|
112
|
+
# @param new_count [Integer] New partition count value
|
|
113
|
+
#
|
|
114
|
+
# @note We prioritize higher partition counts and only accept them when using
|
|
115
|
+
# a mutex to ensure consistency. This design decision is based on the fact that
|
|
116
|
+
# partition counts in Kafka only increase during normal operation.
|
|
117
|
+
def set(topic, new_count)
|
|
118
|
+
# First check outside mutex to avoid unnecessary locking
|
|
119
|
+
current_info = @counts[topic]
|
|
120
|
+
|
|
121
|
+
# Within the TTL window a lower value is treated as a stale/racy read and ignored, since
|
|
122
|
+
# partition counts only grow during normal operation. Once the entry has expired a lower
|
|
123
|
+
# value is an authoritative refresh (e.g. the topic was recreated with fewer partitions),
|
|
124
|
+
# so we fall through and adopt it below.
|
|
125
|
+
if current_info && new_count < current_info[1] && !expired?(current_info[0])
|
|
126
|
+
refresh_timestamp(topic)
|
|
127
|
+
|
|
128
|
+
return
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Only lock the specific topic mutex
|
|
132
|
+
mutex_for(topic).synchronize do
|
|
133
|
+
# Check again inside the lock as another thread might have updated
|
|
134
|
+
current_info = @counts[topic]
|
|
135
|
+
|
|
136
|
+
if current_info.nil?
|
|
137
|
+
# Create new entry
|
|
138
|
+
@counts[topic] = [monotonic_now_ms, new_count]
|
|
139
|
+
elsif new_count > current_info[1] || expired?(current_info[0])
|
|
140
|
+
# A higher count always wins; a lower count is accepted only when the existing entry
|
|
141
|
+
# has expired, so a concurrent fresh higher value (which reset the timestamp) is never
|
|
142
|
+
# clobbered by a stale lower one.
|
|
143
|
+
current_info[0] = monotonic_now_ms
|
|
144
|
+
current_info[1] = new_count
|
|
145
|
+
else
|
|
146
|
+
# Same or lower count within the TTL window: refresh the timestamp only
|
|
147
|
+
current_info[0] = monotonic_now_ms
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# @return [Hash] hash with ttls and partitions counts array
|
|
153
|
+
def to_h
|
|
154
|
+
@counts
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
private
|
|
158
|
+
|
|
159
|
+
# Get or create a mutex for a specific topic
|
|
160
|
+
#
|
|
161
|
+
# This method ensures that each topic has its own mutex,
|
|
162
|
+
# allowing operations on different topics to proceed in parallel.
|
|
163
|
+
#
|
|
164
|
+
# @param topic [String] Kafka topic name
|
|
165
|
+
# @return [Mutex] Mutex for the specified topic
|
|
166
|
+
#
|
|
167
|
+
# @note We use a separate mutex (@mutex_for_hash) to protect the creation
|
|
168
|
+
# of new topic mutexes. This pattern allows fine-grained locking while
|
|
169
|
+
# maintaining thread-safety.
|
|
170
|
+
def mutex_for(topic)
|
|
171
|
+
mutex = @mutex_hash[topic]
|
|
172
|
+
|
|
173
|
+
return mutex if mutex
|
|
174
|
+
|
|
175
|
+
# Use a separate mutex to protect the creation of new topic mutexes
|
|
176
|
+
@mutex_for_hash.synchronize do
|
|
177
|
+
# Check again in case another thread created it
|
|
178
|
+
@mutex_hash[topic] ||= Mutex.new
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
@mutex_hash[topic]
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Update the timestamp without acquiring the mutex
|
|
185
|
+
#
|
|
186
|
+
# This is an optimization that allows refreshing the TTL of existing entries
|
|
187
|
+
# without the overhead of mutex acquisition.
|
|
188
|
+
#
|
|
189
|
+
# @param topic [String] Kafka topic name
|
|
190
|
+
#
|
|
191
|
+
# @note This method is safe for refreshing existing data regardless of count
|
|
192
|
+
# because it only updates the timestamp, which doesn't affect the correctness
|
|
193
|
+
# of concurrent operations.
|
|
194
|
+
def refresh_timestamp(topic)
|
|
195
|
+
current_info = @counts[topic]
|
|
196
|
+
|
|
197
|
+
return unless current_info
|
|
198
|
+
|
|
199
|
+
# Update the timestamp in-place
|
|
200
|
+
current_info[0] = monotonic_now_ms
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Check if a timestamp has expired based on the TTL
|
|
204
|
+
#
|
|
205
|
+
# @param timestamp [Integer] Monotonic timestamp in milliseconds to check
|
|
206
|
+
# @return [Boolean] true if expired, false otherwise
|
|
207
|
+
def expired?(timestamp)
|
|
208
|
+
monotonic_now_ms - timestamp > @ttl_ms
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|