karafka-rdkafka 0.32.0-aarch64-linux-gnu → 0.32.1-aarch64-linux-gnu
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 +28 -11
- data/README.md +12 -5
- data/ext/librdkafka.so +0 -0
- data/lib/rdkafka/abstract_handle.rb +26 -11
- data/lib/rdkafka/admin/config_binding_result.rb +8 -0
- data/lib/rdkafka/admin.rb +1 -1
- data/lib/rdkafka/bindings/admin/configs.rb +11 -0
- data/lib/rdkafka/bindings/callbacks.rb +3 -3
- data/lib/rdkafka/config.rb +3 -3
- data/lib/rdkafka/consumer.rb +58 -17
- data/lib/rdkafka/defaults.rb +11 -4
- data/lib/rdkafka/helpers/message_build_error.rb +30 -0
- data/lib/rdkafka/native_kafka.rb +116 -35
- data/lib/rdkafka/producer/{partitions_count_cache.rb → partition_count_cache.rb} +64 -101
- data/lib/rdkafka/producer.rb +56 -11
- data/lib/rdkafka/share_consumer.rb +11 -4
- data/lib/rdkafka/version.rb +1 -1
- data/lib/rdkafka.rb +2 -1
- data/renovate.json +28 -5
- metadata +3 -2
data/lib/rdkafka/native_kafka.rb
CHANGED
|
@@ -4,6 +4,13 @@ module Rdkafka
|
|
|
4
4
|
# @private
|
|
5
5
|
# A wrapper around a native kafka that polls and cleanly exits
|
|
6
6
|
class NativeKafka
|
|
7
|
+
# Defers every asynchronous interrupt (`Thread#raise`, `Thread#kill`) for the decrement of the
|
|
8
|
+
# operations in progress counter. Frozen and `compare_by_identity`, so MRI uses it without
|
|
9
|
+
# copying it on every call.
|
|
10
|
+
DEFER_INTERRUPTS = { Object => :never }.compare_by_identity.freeze
|
|
11
|
+
|
|
12
|
+
private_constant :DEFER_INTERRUPTS
|
|
13
|
+
|
|
7
14
|
# Creates a new NativeKafka wrapper
|
|
8
15
|
# @param inner [FFI::Pointer] pointer to the native Kafka handle
|
|
9
16
|
# @param run_polling_thread [Boolean] whether to run a background polling thread
|
|
@@ -84,26 +91,50 @@ module Rdkafka
|
|
|
84
91
|
# @raise [ClosedInnerError] when the inner handle is nil
|
|
85
92
|
def with_inner
|
|
86
93
|
owned = @access_mutex.owned?
|
|
94
|
+
# Set in the same step as the increment, so the `ensure` undoes only an increment that
|
|
95
|
+
# happened, even when `Thread#raise`, `Timeout` or `Thread#kill` interrupts this method
|
|
96
|
+
counted = false
|
|
97
|
+
released = false
|
|
87
98
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
else
|
|
91
|
-
@access_mutex.synchronize do
|
|
92
|
-
@operations_done.wait(@access_mutex) while @synchronizing
|
|
99
|
+
begin
|
|
100
|
+
if owned
|
|
93
101
|
@operations_in_progress += 1
|
|
102
|
+
counted = true
|
|
103
|
+
else
|
|
104
|
+
@access_mutex.synchronize do
|
|
105
|
+
@operations_done.wait(@access_mutex) while @synchronizing
|
|
106
|
+
@operations_in_progress += 1
|
|
107
|
+
counted = true
|
|
108
|
+
end
|
|
94
109
|
end
|
|
95
|
-
end
|
|
96
110
|
|
|
97
|
-
begin
|
|
98
111
|
@inner.nil? ? raise(ClosedInnerError) : yield(@inner)
|
|
99
112
|
ensure
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
113
|
+
# Deferred from the first step of the `ensure`: an interrupt delivered before the decrement
|
|
114
|
+
# (even at the branches below) would skip it and leave `#synchronize` (and so `#close`)
|
|
115
|
+
# waiting forever. JRuby delivers a `Thread#kill` that arrived during a blocking native
|
|
116
|
+
# call at its first chance after the call, the lock below. Inline, not in
|
|
117
|
+
# `#release_operation`: an extra method call costs every call
|
|
118
|
+
begin
|
|
119
|
+
Thread.handle_interrupt(DEFER_INTERRUPTS) do
|
|
120
|
+
if !counted
|
|
121
|
+
# Interrupted before the increment: nothing to undo
|
|
122
|
+
elsif owned
|
|
123
|
+
@operations_in_progress -= 1
|
|
124
|
+
counted = false
|
|
125
|
+
else
|
|
126
|
+
# Not `lock` / `unlock`: a signal trap handler still runs (and can raise) under the mask
|
|
127
|
+
@access_mutex.synchronize do
|
|
128
|
+
@operations_in_progress -= 1
|
|
129
|
+
counted = false
|
|
130
|
+
@operations_done.broadcast if @synchronizing && @operations_in_progress.zero?
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
released = true
|
|
106
135
|
end
|
|
136
|
+
ensure
|
|
137
|
+
release_operation(owned, counted) unless released
|
|
107
138
|
end
|
|
108
139
|
end
|
|
109
140
|
end
|
|
@@ -113,23 +144,16 @@ module Rdkafka
|
|
|
113
144
|
# @yield [FFI::Pointer] the inner native Kafka handle
|
|
114
145
|
# @return [Object] the result of the block
|
|
115
146
|
def synchronize(&block)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
@synchronizing = true
|
|
119
|
-
|
|
120
|
-
begin
|
|
121
|
-
# Wait for any commands using the inner to finish
|
|
122
|
-
# This can take a while on blocking operations like polling but is essential not to
|
|
123
|
-
# proceed with certain types of operations like resources destruction as it can cause the
|
|
124
|
-
# process to hang or crash
|
|
125
|
-
@operations_done.wait(@access_mutex) until @operations_in_progress.zero?
|
|
147
|
+
exclusively { with_inner(&block) }
|
|
148
|
+
end
|
|
126
149
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
# Executes a block while holding exclusive access to the native Kafka handle, unless the handle
|
|
151
|
+
# was closed, also by another thread while this one waited for the access
|
|
152
|
+
# @param block [Proc] block to execute with the native handle
|
|
153
|
+
# @yield [FFI::Pointer] the inner native Kafka handle
|
|
154
|
+
# @return [Object, nil] the result of the block or nil when the handle is closed
|
|
155
|
+
def synchronize_unless_closed(&block)
|
|
156
|
+
exclusively { with_inner(&block) unless @inner.nil? }
|
|
133
157
|
end
|
|
134
158
|
|
|
135
159
|
# Returns a finalizer proc for closing this native Kafka handle
|
|
@@ -217,7 +241,10 @@ module Rdkafka
|
|
|
217
241
|
def close(object_id = nil)
|
|
218
242
|
return if closed?
|
|
219
243
|
|
|
220
|
-
|
|
244
|
+
exclusively do
|
|
245
|
+
# Another thread closed the handle while this one waited for the access
|
|
246
|
+
return if @inner.nil?
|
|
247
|
+
|
|
221
248
|
# Indicate to the outside world that we are closing
|
|
222
249
|
@closing = true
|
|
223
250
|
|
|
@@ -233,11 +260,6 @@ module Rdkafka
|
|
|
233
260
|
# Destroy the client after locking both mutexes
|
|
234
261
|
@poll_mutex.lock
|
|
235
262
|
|
|
236
|
-
# This check prevents a race condition, where we would enter the close in two threads
|
|
237
|
-
# and after unlocking the primary one that hold the lock but finished, ours would be unlocked
|
|
238
|
-
# and would continue to run, trying to destroy inner twice
|
|
239
|
-
return unless @inner
|
|
240
|
-
|
|
241
263
|
yield if block_given?
|
|
242
264
|
|
|
243
265
|
Rdkafka::Bindings.rd_kafka_destroy(@inner)
|
|
@@ -247,5 +269,64 @@ module Rdkafka
|
|
|
247
269
|
@poll_mutex = nil
|
|
248
270
|
end
|
|
249
271
|
end
|
|
272
|
+
|
|
273
|
+
private
|
|
274
|
+
|
|
275
|
+
# Executes a block while holding `@access_mutex`, once all operations in progress are done
|
|
276
|
+
# @yield block to execute with exclusive access
|
|
277
|
+
# @return [Object] the result of the block
|
|
278
|
+
def exclusively
|
|
279
|
+
@access_mutex.synchronize do
|
|
280
|
+
@operations_done.wait(@access_mutex) while @synchronizing
|
|
281
|
+
@synchronizing = true
|
|
282
|
+
|
|
283
|
+
begin
|
|
284
|
+
# Wait for any commands using the inner to finish
|
|
285
|
+
# This can take a while on blocking operations like polling but is essential not to
|
|
286
|
+
# proceed with certain types of operations like resources destruction as it can cause the
|
|
287
|
+
# process to hang or crash
|
|
288
|
+
@operations_done.wait(@access_mutex) until @operations_in_progress.zero?
|
|
289
|
+
|
|
290
|
+
yield
|
|
291
|
+
ensure
|
|
292
|
+
@synchronizing = false
|
|
293
|
+
@operations_done.broadcast
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Finishes the release of `#with_inner` when a signal trap handler raised during it
|
|
299
|
+
#
|
|
300
|
+
# A trap handler still runs (and can raise) under the mask, so the `ensure` calls this method
|
|
301
|
+
# again until the release is done, then the error propagates.
|
|
302
|
+
#
|
|
303
|
+
# @param owned [Boolean] whether the caller already holds `@access_mutex`
|
|
304
|
+
# @param counted [Boolean] whether the counter still has to be decremented
|
|
305
|
+
def release_operation(owned, counted)
|
|
306
|
+
released = false
|
|
307
|
+
|
|
308
|
+
Thread.handle_interrupt(DEFER_INTERRUPTS) do
|
|
309
|
+
if owned
|
|
310
|
+
if counted
|
|
311
|
+
@operations_in_progress -= 1
|
|
312
|
+
counted = false
|
|
313
|
+
end
|
|
314
|
+
else
|
|
315
|
+
# Not `lock` / `unlock`: a raise in a trap handler would leave the lock held
|
|
316
|
+
@access_mutex.synchronize do
|
|
317
|
+
if counted
|
|
318
|
+
@operations_in_progress -= 1
|
|
319
|
+
counted = false
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
@operations_done.broadcast if @synchronizing && @operations_in_progress.zero?
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
released = true
|
|
327
|
+
end
|
|
328
|
+
ensure
|
|
329
|
+
release_operation(owned, counted) unless released
|
|
330
|
+
end
|
|
250
331
|
end
|
|
251
332
|
end
|
|
@@ -17,7 +17,7 @@ module Rdkafka
|
|
|
17
17
|
#
|
|
18
18
|
# 2. Edge case handling
|
|
19
19
|
# If a user configures `statistics.interval.ms` much higher than the default cache TTL
|
|
20
|
-
# ({Defaults::
|
|
20
|
+
# ({Defaults::PARTITION_COUNT_CACHE_TTL_MS}ms), the cache will still function correctly.
|
|
21
21
|
# When statistics updates don't occur frequently enough, the cache entries will expire
|
|
22
22
|
# naturally, triggering a blocking refresh when needed.
|
|
23
23
|
#
|
|
@@ -33,8 +33,9 @@ module Rdkafka
|
|
|
33
33
|
# the cache updated, benefiting all other clients.
|
|
34
34
|
#
|
|
35
35
|
# 5. Thread-safety approach
|
|
36
|
-
#
|
|
37
|
-
#
|
|
36
|
+
# One cache-wide mutex guards every read and write of the cached entries, so the cache stays
|
|
37
|
+
# consistent on Rubies without a GVL (JRuby). The metadata request that refreshes an entry
|
|
38
|
+
# runs outside of it, so a slow lookup for one topic never blocks the others.
|
|
38
39
|
#
|
|
39
40
|
# 6. Topic recreation handling
|
|
40
41
|
# If a topic is deleted and recreated with fewer partitions, the cache keeps reporting the
|
|
@@ -46,11 +47,11 @@ module Rdkafka
|
|
|
46
47
|
# 7. Cluster outages
|
|
47
48
|
# If the refresh of an expired entry fails on a transient error (for example all brokers
|
|
48
49
|
# are unreachable), the last known count is served instead of raising, and the next refresh
|
|
49
|
-
# is attempted after {Defaults::
|
|
50
|
+
# is attempted after {Defaults::PARTITION_COUNT_CACHE_STALE_BACKOFF_MS}. This lets
|
|
50
51
|
# `partition_key` produces keep buffering in librdkafka during an outage. Without a known
|
|
51
52
|
# count the error is raised. Only one caller refreshes an expired entry at a time; the
|
|
52
53
|
# others get the known count meanwhile.
|
|
53
|
-
class
|
|
54
|
+
class PartitionCountCache
|
|
54
55
|
include Helpers::Time
|
|
55
56
|
|
|
56
57
|
# Refresh errors after which the last known count is still served
|
|
@@ -71,24 +72,22 @@ module Rdkafka
|
|
|
71
72
|
# @param ttl [Integer, nil] DEPRECATED: Use ttl_ms instead.
|
|
72
73
|
# Time-to-live in seconds for cached values. Will be removed in v1.0.0.
|
|
73
74
|
# @param ttl_ms [Integer, nil] Time-to-live in milliseconds for cached values.
|
|
74
|
-
# Defaults to {Defaults::
|
|
75
|
+
# Defaults to {Defaults::PARTITION_COUNT_CACHE_TTL_MS}.
|
|
75
76
|
def initialize(ttl = :not_provided, ttl_ms: :not_provided)
|
|
76
77
|
@counts = {}
|
|
77
|
-
@
|
|
78
|
-
# Used only for @mutex_hash access to ensure thread-safety when creating new mutexes
|
|
79
|
-
@mutex_for_hash = Mutex.new
|
|
78
|
+
@mutex = Mutex.new
|
|
80
79
|
|
|
81
80
|
# Determine which TTL value to use
|
|
82
81
|
if ttl != :not_provided && ttl_ms != :not_provided
|
|
83
|
-
warn "DEPRECATION WARNING: Both ttl and ttl_ms were provided to
|
|
82
|
+
warn "DEPRECATION WARNING: Both ttl and ttl_ms were provided to PartitionCountCache. " \
|
|
84
83
|
"Using ttl_ms. The ttl parameter is deprecated and will be removed in v1.0.0."
|
|
85
84
|
@ttl_ms = ttl_ms
|
|
86
85
|
elsif ttl != :not_provided
|
|
87
|
-
warn "DEPRECATION WARNING: ttl (seconds) parameter for
|
|
86
|
+
warn "DEPRECATION WARNING: ttl (seconds) parameter for PartitionCountCache is deprecated. " \
|
|
88
87
|
"Use ttl_ms (milliseconds) instead. This parameter will be removed in v1.0.0."
|
|
89
88
|
@ttl_ms = (ttl * 1000).to_i
|
|
90
89
|
elsif ttl_ms == :not_provided
|
|
91
|
-
@ttl_ms = Defaults::
|
|
90
|
+
@ttl_ms = Defaults::PARTITION_COUNT_CACHE_TTL_MS
|
|
92
91
|
else
|
|
93
92
|
@ttl_ms = ttl_ms
|
|
94
93
|
end
|
|
@@ -118,51 +117,52 @@ module Rdkafka
|
|
|
118
117
|
# @raise [RdkafkaError] when the refresh fails and there is no last known count to serve, or
|
|
119
118
|
# when it fails on a non-transient error
|
|
120
119
|
#
|
|
121
|
-
# @note The
|
|
122
|
-
# since partition counts typically only increase during normal operation.
|
|
120
|
+
# @note The metadata request in the block runs outside of the cache-wide mutex.
|
|
123
121
|
def get(topic_name)
|
|
124
|
-
current_info =
|
|
122
|
+
current_info = expired_at = lease = nil
|
|
125
123
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
124
|
+
# `next` instead of `return`: a return from the block allocates on every cache hit
|
|
125
|
+
cached_count = @mutex.synchronize do
|
|
126
|
+
current_info = @counts[topic_name]
|
|
127
|
+
|
|
128
|
+
next current_info[1] if current_info && !expired?(current_info[0])
|
|
129
|
+
|
|
130
|
+
if current_info
|
|
131
|
+
# While the refresh runs, other callers see a fresh entry and get the known count
|
|
132
|
+
# instead of running their own metadata request. The lease outlasts a failed refresh
|
|
133
|
+
# even with a TTL shorter than one metadata request.
|
|
134
|
+
expired_at = current_info[0]
|
|
135
|
+
lease = monotonic_now_ms + Defaults::PARTITION_COUNT_CACHE_STALE_BACKOFF_MS
|
|
136
|
+
current_info[0] = lease
|
|
137
|
+
end
|
|
129
138
|
|
|
130
|
-
|
|
139
|
+
nil
|
|
131
140
|
end
|
|
132
141
|
|
|
133
|
-
return
|
|
142
|
+
return cached_count if cached_count
|
|
134
143
|
|
|
135
|
-
current_info
|
|
144
|
+
return refresh(topic_name, current_info, expired_at, lease) { yield } if current_info
|
|
145
|
+
|
|
146
|
+
set(topic_name, yield)
|
|
147
|
+
|
|
148
|
+
@mutex.synchronize { @counts[topic_name][1] }
|
|
136
149
|
end
|
|
137
150
|
|
|
138
151
|
# Update partition count for a topic when needed
|
|
139
152
|
#
|
|
140
153
|
# This method updates the partition count for a topic in the cache.
|
|
141
|
-
#
|
|
154
|
+
# The check and the update run under the cache-wide mutex, so concurrent updates never
|
|
155
|
+
# interleave.
|
|
142
156
|
#
|
|
143
157
|
# @param topic_name [String] Kafka topic name
|
|
144
158
|
# @param new_count [Integer] New partition count value
|
|
145
159
|
#
|
|
146
|
-
# @note We prioritize higher partition counts
|
|
147
|
-
# a
|
|
148
|
-
#
|
|
160
|
+
# @note We prioritize higher partition counts. Within the TTL window a lower value is treated
|
|
161
|
+
# as a stale/racy read and ignored, since partition counts only grow during normal
|
|
162
|
+
# operation. Once the entry has expired a lower value is an authoritative refresh (e.g. the
|
|
163
|
+
# topic was recreated with fewer partitions) and is adopted.
|
|
149
164
|
def set(topic_name, new_count)
|
|
150
|
-
|
|
151
|
-
current_info = @counts[topic_name]
|
|
152
|
-
|
|
153
|
-
# Within the TTL window a lower value is treated as a stale/racy read and ignored, since
|
|
154
|
-
# partition counts only grow during normal operation. Once the entry has expired a lower
|
|
155
|
-
# value is an authoritative refresh (e.g. the topic was recreated with fewer partitions),
|
|
156
|
-
# so we fall through and adopt it below.
|
|
157
|
-
if current_info && new_count < current_info[1] && !expired?(current_info[0])
|
|
158
|
-
refresh_timestamp(topic_name)
|
|
159
|
-
|
|
160
|
-
return
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
# Only lock the specific topic mutex
|
|
164
|
-
mutex_for(topic_name).synchronize do
|
|
165
|
-
# Check again inside the lock as another thread might have updated
|
|
165
|
+
@mutex.synchronize do
|
|
166
166
|
current_info = @counts[topic_name]
|
|
167
167
|
|
|
168
168
|
if current_info.nil?
|
|
@@ -182,58 +182,16 @@ module Rdkafka
|
|
|
182
182
|
end
|
|
183
183
|
|
|
184
184
|
# @return [Hash] hash with ttls and partitions counts array
|
|
185
|
+
# @note Returns the live hash without locking: do not read it while other threads use the
|
|
186
|
+
# cache.
|
|
185
187
|
def to_h
|
|
186
188
|
@counts
|
|
187
189
|
end
|
|
188
190
|
|
|
189
191
|
private
|
|
190
192
|
|
|
191
|
-
#
|
|
192
|
-
#
|
|
193
|
-
# This method ensures that each topic has its own mutex,
|
|
194
|
-
# allowing operations on different topics to proceed in parallel.
|
|
195
|
-
#
|
|
196
|
-
# @param topic_name [String] Kafka topic name
|
|
197
|
-
# @return [Mutex] Mutex for the specified topic
|
|
198
|
-
#
|
|
199
|
-
# @note We use a separate mutex (@mutex_for_hash) to protect the creation
|
|
200
|
-
# of new topic mutexes. This pattern allows fine-grained locking while
|
|
201
|
-
# maintaining thread-safety.
|
|
202
|
-
def mutex_for(topic_name)
|
|
203
|
-
mutex = @mutex_hash[topic_name]
|
|
204
|
-
|
|
205
|
-
return mutex if mutex
|
|
206
|
-
|
|
207
|
-
# Use a separate mutex to protect the creation of new topic mutexes
|
|
208
|
-
@mutex_for_hash.synchronize do
|
|
209
|
-
# Check again in case another thread created it
|
|
210
|
-
@mutex_hash[topic_name] ||= Mutex.new
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
@mutex_hash[topic_name]
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Update the timestamp without acquiring the mutex
|
|
217
|
-
#
|
|
218
|
-
# This is an optimization that allows refreshing the TTL of existing entries
|
|
219
|
-
# without the overhead of mutex acquisition.
|
|
220
|
-
#
|
|
221
|
-
# @param topic_name [String] Kafka topic name
|
|
222
|
-
#
|
|
223
|
-
# @note This method is safe for refreshing existing data regardless of count
|
|
224
|
-
# because it only updates the timestamp, which doesn't affect the correctness
|
|
225
|
-
# of concurrent operations.
|
|
226
|
-
def refresh_timestamp(topic_name)
|
|
227
|
-
current_info = @counts[topic_name]
|
|
228
|
-
|
|
229
|
-
return unless current_info
|
|
230
|
-
|
|
231
|
-
# Update the timestamp in-place
|
|
232
|
-
current_info[0] = monotonic_now_ms
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
# Refreshes an expired entry. While the refresh runs, other callers see a fresh entry and get
|
|
236
|
-
# the known count instead of running their own metadata request.
|
|
193
|
+
# Refreshes an expired entry that {#get} leased. Runs the metadata request outside of the
|
|
194
|
+
# cache-wide mutex.
|
|
237
195
|
#
|
|
238
196
|
# The block performs an authoritative metadata read. We hand it to `set`, which adopts a
|
|
239
197
|
# higher count always and a lower count once the entry has expired (e.g. the topic was
|
|
@@ -242,43 +200,45 @@ module Rdkafka
|
|
|
242
200
|
#
|
|
243
201
|
# @param topic_name [String] Kafka topic name
|
|
244
202
|
# @param current_info [Array<Integer>] cached timestamp and partition count
|
|
203
|
+
# @param expired_at [Integer] timestamp of the entry before it was leased
|
|
204
|
+
# @param lease [Integer] timestamp {#get} set on the entry for the refresh
|
|
245
205
|
# @yieldreturn [Integer] Current partition count retrieved from Kafka
|
|
246
206
|
# @return [Integer] Partition count for the topic
|
|
247
207
|
# @raise [RdkafkaError] when the refresh fails on a non-transient error
|
|
248
|
-
def refresh(topic_name, current_info)
|
|
249
|
-
expired_at = current_info[0]
|
|
250
|
-
# Outlasts a failed refresh even with a TTL shorter than one metadata request
|
|
251
|
-
lease = monotonic_now_ms + Defaults::PARTITIONS_COUNT_CACHE_STALE_BACKOFF_MS
|
|
252
|
-
current_info[0] = lease
|
|
253
|
-
|
|
208
|
+
def refresh(topic_name, current_info, expired_at, lease)
|
|
254
209
|
begin
|
|
255
210
|
new_count = yield
|
|
256
211
|
rescue RdkafkaError => e
|
|
257
212
|
raise unless STALE_FALLBACK_ERRORS.include?(e.code)
|
|
258
213
|
ensure
|
|
259
|
-
|
|
260
|
-
|
|
214
|
+
@mutex.synchronize do
|
|
215
|
+
# Hand the entry back as expired, unless a concurrent write refreshed it meanwhile
|
|
216
|
+
current_info[0] = expired_at if current_info[0] == lease
|
|
217
|
+
end
|
|
261
218
|
end
|
|
262
219
|
|
|
263
220
|
if new_count.nil?
|
|
264
|
-
|
|
265
|
-
|
|
221
|
+
return @mutex.synchronize do
|
|
222
|
+
# A concurrent write refreshed the entry, so it keeps its full TTL
|
|
223
|
+
next current_info[1] unless current_info[0] == expired_at
|
|
266
224
|
|
|
267
|
-
|
|
225
|
+
serve_stale(current_info)
|
|
226
|
+
end
|
|
268
227
|
end
|
|
269
228
|
|
|
270
229
|
set(topic_name, new_count)
|
|
271
230
|
|
|
272
|
-
@counts[topic_name][1]
|
|
231
|
+
@mutex.synchronize { @counts[topic_name][1] }
|
|
273
232
|
end
|
|
274
233
|
|
|
275
234
|
# Keeps serving an expired entry after a failed refresh and schedules the next refresh
|
|
276
|
-
# after {Defaults::
|
|
235
|
+
# after {Defaults::PARTITION_COUNT_CACHE_STALE_BACKOFF_MS}. Call it under the cache-wide
|
|
236
|
+
# mutex.
|
|
277
237
|
#
|
|
278
238
|
# @param current_info [Array<Integer>] cached timestamp and partition count
|
|
279
239
|
# @return [Integer] last known partition count
|
|
280
240
|
def serve_stale(current_info)
|
|
281
|
-
current_info[0] = monotonic_now_ms - @ttl_ms + Defaults::
|
|
241
|
+
current_info[0] = monotonic_now_ms - @ttl_ms + Defaults::PARTITION_COUNT_CACHE_STALE_BACKOFF_MS
|
|
282
242
|
|
|
283
243
|
current_info[1]
|
|
284
244
|
end
|
|
@@ -291,5 +251,8 @@ module Rdkafka
|
|
|
291
251
|
monotonic_now_ms - timestamp > @ttl_ms
|
|
292
252
|
end
|
|
293
253
|
end
|
|
254
|
+
|
|
255
|
+
# @deprecated Use {PartitionCountCache} instead. It will be removed in v1.0.0.
|
|
256
|
+
PartitionsCountCache = PartitionCountCache
|
|
294
257
|
end
|
|
295
258
|
end
|
data/lib/rdkafka/producer.rb
CHANGED
|
@@ -9,23 +9,39 @@ module Rdkafka
|
|
|
9
9
|
include Helpers::Metadata
|
|
10
10
|
|
|
11
11
|
# @!visibility private
|
|
12
|
-
@@
|
|
12
|
+
@@partition_count_cache = PartitionCountCache.new
|
|
13
13
|
|
|
14
14
|
# Global (process wide) partitions cache. We use it to store number of topics partitions,
|
|
15
15
|
# either from the librdkafka statistics (if enabled) or via direct inline calls every now and
|
|
16
16
|
# then. Since the partitions count can only grow and should be same for all consumers and
|
|
17
17
|
# producers, we can use a global cache as long as we ensure that updates only move up.
|
|
18
18
|
#
|
|
19
|
-
# @return [Rdkafka::Producer::
|
|
19
|
+
# @return [Rdkafka::Producer::PartitionCountCache]
|
|
20
20
|
# @note It is critical to remember, that not all users may have statistics callbacks enabled,
|
|
21
21
|
# hence we should not make assumption that this cache is always updated from the stats.
|
|
22
|
+
def self.partition_count_cache
|
|
23
|
+
@@partition_count_cache
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @param partition_count_cache [Rdkafka::Producer::PartitionCountCache]
|
|
27
|
+
def self.partition_count_cache=(partition_count_cache)
|
|
28
|
+
@@partition_count_cache = partition_count_cache
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @deprecated Use {.partition_count_cache} instead
|
|
32
|
+
# @return [Rdkafka::Producer::PartitionCountCache]
|
|
22
33
|
def self.partitions_count_cache
|
|
23
|
-
|
|
34
|
+
warn "DEPRECATION WARNING: Rdkafka::Producer.partitions_count_cache is deprecated. " \
|
|
35
|
+
"Use Rdkafka::Producer.partition_count_cache instead. It will be removed in v1.0.0."
|
|
36
|
+
partition_count_cache
|
|
24
37
|
end
|
|
25
38
|
|
|
26
|
-
# @
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
# @deprecated Use {.partition_count_cache=} instead
|
|
40
|
+
# @param partition_count_cache [Rdkafka::Producer::PartitionCountCache]
|
|
41
|
+
def self.partitions_count_cache=(partition_count_cache)
|
|
42
|
+
warn "DEPRECATION WARNING: Rdkafka::Producer.partitions_count_cache= is deprecated. " \
|
|
43
|
+
"Use Rdkafka::Producer.partition_count_cache= instead. It will be removed in v1.0.0."
|
|
44
|
+
self.partition_count_cache = partition_count_cache
|
|
29
45
|
end
|
|
30
46
|
|
|
31
47
|
# Empty hash used as a default
|
|
@@ -59,6 +75,31 @@ module Rdkafka
|
|
|
59
75
|
# resolved and this is normalized in all the places
|
|
60
76
|
class TopicHandleCreationError < RuntimeError; end
|
|
61
77
|
|
|
78
|
+
# Raised by {#partition_count} when the partition count of a topic could not be fetched.
|
|
79
|
+
# Keeps the code and flags of the original error, which is also its `cause`.
|
|
80
|
+
class PartitionCountError < RdkafkaError
|
|
81
|
+
# @return [String] topic whose partition count could not be fetched
|
|
82
|
+
attr_reader :topic_name
|
|
83
|
+
|
|
84
|
+
# @param topic_name [String] topic whose partition count could not be fetched
|
|
85
|
+
# @param error [RdkafkaError] the original metadata error
|
|
86
|
+
def initialize(topic_name, error)
|
|
87
|
+
super(
|
|
88
|
+
error.rdkafka_response,
|
|
89
|
+
"Could not fetch the partition count of topic '#{topic_name}', so messages with a " \
|
|
90
|
+
"partition_key for it cannot be partitioned",
|
|
91
|
+
broker_message: error.broker_message,
|
|
92
|
+
fatal: error.fatal?,
|
|
93
|
+
retryable: error.retryable?,
|
|
94
|
+
abortable: error.abortable?,
|
|
95
|
+
details: error.details,
|
|
96
|
+
instance_name: error.instance_name
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
@topic_name = topic_name
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
62
103
|
# @private
|
|
63
104
|
# Returns the current delivery callback, by default this is nil.
|
|
64
105
|
#
|
|
@@ -383,8 +424,10 @@ module Rdkafka
|
|
|
383
424
|
# Partition count for a given topic.
|
|
384
425
|
#
|
|
385
426
|
# @param topic_name [String] The topic name.
|
|
386
|
-
# @return [Integer] partition count for a given topic or `RD_KAFKA_PARTITION_UA (-1)` if
|
|
387
|
-
#
|
|
427
|
+
# @return [Integer] partition count for a given topic or `RD_KAFKA_PARTITION_UA (-1)` if the
|
|
428
|
+
# topic does not exist.
|
|
429
|
+
# @raise [PartitionCountError] when the partition count could not be fetched, e.g. when no
|
|
430
|
+
# broker is reachable and no count is cached. Its `cause` is the original metadata error.
|
|
388
431
|
#
|
|
389
432
|
# @note If 'allow.auto.create.topics' is set to true in the broker, the topic will be
|
|
390
433
|
# auto-created after returning nil.
|
|
@@ -400,7 +443,7 @@ module Rdkafka
|
|
|
400
443
|
def partition_count(topic_name)
|
|
401
444
|
closed_producer_check(__method__)
|
|
402
445
|
|
|
403
|
-
self.class.
|
|
446
|
+
self.class.partition_count_cache.get(topic_name) do
|
|
404
447
|
topic_metadata = nil
|
|
405
448
|
|
|
406
449
|
@native_kafka.with_inner do |inner|
|
|
@@ -412,8 +455,10 @@ module Rdkafka
|
|
|
412
455
|
# A missing topic is an expected, cacheable outcome (the topic may be auto-created on
|
|
413
456
|
# produce, or simply not exist yet). Returning RD_KAFKA_PARTITION_UA from inside the block
|
|
414
457
|
# caches it, so we don't re-run a blocking metadata query on every produce(partition_key:)
|
|
415
|
-
# to that topic. Anything else is a real error
|
|
416
|
-
|
|
458
|
+
# to that topic. Anything else is a real error.
|
|
459
|
+
unless e.code == :unknown_topic_or_part
|
|
460
|
+
raise(PartitionCountError.new(topic_name, e), cause: e)
|
|
461
|
+
end
|
|
417
462
|
|
|
418
463
|
Rdkafka::Bindings::RD_KAFKA_PARTITION_UA
|
|
419
464
|
end
|
|
@@ -36,6 +36,7 @@ module Rdkafka
|
|
|
36
36
|
class ShareConsumer
|
|
37
37
|
include Enumerable
|
|
38
38
|
include Helpers::OAuth
|
|
39
|
+
include Helpers::MessageBuildError
|
|
39
40
|
|
|
40
41
|
# Mapping of friendly acknowledge types to their librdkafka enum values
|
|
41
42
|
ACKNOWLEDGE_TYPES = {
|
|
@@ -223,8 +224,11 @@ module Rdkafka
|
|
|
223
224
|
#
|
|
224
225
|
# @param timeout_ms [Integer] Timeout of this poll
|
|
225
226
|
# @return [Array<ShareConsumer::Message, RdkafkaError>] polled messages, empty when the
|
|
226
|
-
# timeout expired without records. A message that fails to build (
|
|
227
|
-
# `RdkafkaError` rather than a `ShareConsumer::Message
|
|
227
|
+
# timeout expired without records. A message that fails to build (e.g. its headers cannot
|
|
228
|
+
# be read) comes inline as an `RdkafkaError` rather than a `ShareConsumer::Message`;
|
|
229
|
+
# `details` then holds its `topic`, `partition` and `offset`, so in explicit
|
|
230
|
+
# acknowledgement mode it can be acknowledged (see {#acknowledge}); in implicit mode the
|
|
231
|
+
# next poll accepts it.
|
|
228
232
|
# @raise [RdkafkaError] When polling fails at the batch level (for example unacknowledged
|
|
229
233
|
# records in explicit mode)
|
|
230
234
|
def poll(timeout_ms = Defaults::SHARE_CONSUMER_POLL_TIMEOUT_MS)
|
|
@@ -264,7 +268,7 @@ module Rdkafka
|
|
|
264
268
|
# error rather than discarding the whole batch - including the messages already
|
|
265
269
|
# built - and raising, which would silently lose them. Mirrors how
|
|
266
270
|
# Consumer#poll_batch handles the same failure mode.
|
|
267
|
-
e
|
|
271
|
+
message_build_error(native_message, e)
|
|
268
272
|
end
|
|
269
273
|
end
|
|
270
274
|
ensure
|
|
@@ -325,7 +329,10 @@ module Rdkafka
|
|
|
325
329
|
# Acknowledgements are accumulated locally and sent to the broker on the next {#poll},
|
|
326
330
|
# {#commit_sync} or {#commit_async}.
|
|
327
331
|
#
|
|
328
|
-
# @param message [ShareConsumer::Message] the message to
|
|
332
|
+
# @param message [ShareConsumer::Message, #topic, #partition, #offset] the message to
|
|
333
|
+
# acknowledge: a {ShareConsumer::Message}, or any object responding to `#topic`,
|
|
334
|
+
# `#partition` and `#offset`, such as one built from the `details` of a message build
|
|
335
|
+
# error returned by {#poll}
|
|
329
336
|
# @param type [Symbol] `:accept` (processed successfully), `:release` (make it available for
|
|
330
337
|
# redelivery) or `:reject` (do not deliver again, archive)
|
|
331
338
|
# @return [nil]
|
data/lib/rdkafka/version.rb
CHANGED
data/lib/rdkafka.rb
CHANGED
|
@@ -11,6 +11,7 @@ require "rdkafka/helpers/time"
|
|
|
11
11
|
require "rdkafka/helpers/oauth"
|
|
12
12
|
require "rdkafka/helpers/metadata"
|
|
13
13
|
require "rdkafka/helpers/list_offsets"
|
|
14
|
+
require "rdkafka/helpers/message_build_error"
|
|
14
15
|
require "rdkafka/abstract_handle"
|
|
15
16
|
require "rdkafka/admin"
|
|
16
17
|
require "rdkafka/admin/alter_consumer_group_offsets_handle"
|
|
@@ -86,7 +87,7 @@ require "rdkafka/share_consumer"
|
|
|
86
87
|
require "rdkafka/share_consumer/message"
|
|
87
88
|
require "rdkafka/metadata"
|
|
88
89
|
require "rdkafka/native_kafka"
|
|
89
|
-
require "rdkafka/producer/
|
|
90
|
+
require "rdkafka/producer/partition_count_cache"
|
|
90
91
|
require "rdkafka/producer"
|
|
91
92
|
require "rdkafka/producer/delivery_handle"
|
|
92
93
|
require "rdkafka/producer/delivery_report"
|