ruby-kafka 0.7.10 → 1.5.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/.circleci/config.yml +179 -0
- data/.github/workflows/stale.yml +19 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +40 -0
- data/README.md +167 -0
- data/lib/kafka/async_producer.rb +60 -42
- data/lib/kafka/client.rb +92 -6
- data/lib/kafka/cluster.rb +82 -24
- data/lib/kafka/connection.rb +3 -0
- data/lib/kafka/consumer.rb +61 -11
- data/lib/kafka/consumer_group/assignor.rb +63 -0
- data/lib/kafka/consumer_group.rb +29 -6
- data/lib/kafka/crc32_hash.rb +15 -0
- data/lib/kafka/datadog.rb +20 -13
- data/lib/kafka/digest.rb +22 -0
- data/lib/kafka/fetcher.rb +5 -2
- data/lib/kafka/interceptors.rb +33 -0
- data/lib/kafka/murmur2_hash.rb +17 -0
- data/lib/kafka/offset_manager.rb +12 -1
- data/lib/kafka/partitioner.rb +8 -3
- data/lib/kafka/producer.rb +13 -5
- data/lib/kafka/prometheus.rb +78 -79
- data/lib/kafka/protocol/add_offsets_to_txn_response.rb +2 -0
- data/lib/kafka/protocol/encoder.rb +1 -1
- data/lib/kafka/protocol/join_group_request.rb +8 -2
- data/lib/kafka/protocol/join_group_response.rb +9 -1
- data/lib/kafka/protocol/metadata_response.rb +1 -1
- data/lib/kafka/protocol/offset_fetch_request.rb +3 -1
- data/lib/kafka/protocol/record_batch.rb +2 -2
- data/lib/kafka/protocol/sasl_handshake_request.rb +1 -1
- data/lib/kafka/protocol/sync_group_response.rb +5 -2
- data/lib/kafka/protocol/txn_offset_commit_response.rb +34 -5
- data/lib/kafka/round_robin_assignment_strategy.rb +37 -39
- data/lib/kafka/sasl/awsmskiam.rb +133 -0
- data/lib/kafka/sasl_authenticator.rb +15 -2
- data/lib/kafka/ssl_context.rb +6 -5
- data/lib/kafka/tagged_logger.rb +1 -0
- data/lib/kafka/transaction_manager.rb +30 -10
- data/lib/kafka/version.rb +1 -1
- data/ruby-kafka.gemspec +5 -4
- metadata +39 -13
data/lib/kafka/prometheus.rb
CHANGED
@@ -42,11 +42,11 @@ module Kafka
|
|
42
42
|
class ConnectionSubscriber < ActiveSupport::Subscriber
|
43
43
|
def initialize
|
44
44
|
super
|
45
|
-
@api_calls = Prometheus.registry.counter(:api_calls, 'Total calls')
|
46
|
-
@api_latency = Prometheus.registry.histogram(:api_latency, 'Latency',
|
47
|
-
@api_request_size = Prometheus.registry.histogram(:api_request_size, 'Request size',
|
48
|
-
@api_response_size = Prometheus.registry.histogram(:api_response_size, 'Response size',
|
49
|
-
@api_errors = Prometheus.registry.counter(:api_errors, 'Errors')
|
45
|
+
@api_calls = Prometheus.registry.counter(:api_calls, docstring: 'Total calls', labels: [:client, :api, :broker])
|
46
|
+
@api_latency = Prometheus.registry.histogram(:api_latency, docstring: 'Latency', buckets: LATENCY_BUCKETS, labels: [:client, :api, :broker])
|
47
|
+
@api_request_size = Prometheus.registry.histogram(:api_request_size, docstring: 'Request size', buckets: SIZE_BUCKETS, labels: [:client, :api, :broker])
|
48
|
+
@api_response_size = Prometheus.registry.histogram(:api_response_size, docstring: 'Response size', buckets: SIZE_BUCKETS, labels: [:client, :api, :broker])
|
49
|
+
@api_errors = Prometheus.registry.counter(:api_errors, docstring: 'Errors', labels: [:client, :api, :broker])
|
50
50
|
end
|
51
51
|
|
52
52
|
def request(event)
|
@@ -58,34 +58,34 @@ module Kafka
|
|
58
58
|
request_size = event.payload.fetch(:request_size, 0)
|
59
59
|
response_size = event.payload.fetch(:response_size, 0)
|
60
60
|
|
61
|
-
@api_calls.increment(key)
|
62
|
-
@api_latency.observe(
|
63
|
-
@api_request_size.observe(
|
64
|
-
@api_response_size.observe(
|
65
|
-
@api_errors.increment(key) if event.payload.key?(:exception)
|
61
|
+
@api_calls.increment(labels: key)
|
62
|
+
@api_latency.observe(event.duration, labels: key)
|
63
|
+
@api_request_size.observe(request_size, labels: key)
|
64
|
+
@api_response_size.observe(response_size, labels: key)
|
65
|
+
@api_errors.increment(labels: key) if event.payload.key?(:exception)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
class ConsumerSubscriber < ActiveSupport::Subscriber
|
70
70
|
def initialize
|
71
71
|
super
|
72
|
-
@process_messages = Prometheus.registry.counter(:consumer_process_messages, 'Total messages')
|
73
|
-
@process_message_errors = Prometheus.registry.counter(:consumer_process_message_errors, 'Total errors')
|
72
|
+
@process_messages = Prometheus.registry.counter(:consumer_process_messages, docstring: 'Total messages', labels: [:client, :group_id, :topic, :partition])
|
73
|
+
@process_message_errors = Prometheus.registry.counter(:consumer_process_message_errors, docstring: 'Total errors', labels: [:client, :group_id, :topic, :partition])
|
74
74
|
@process_message_latency =
|
75
|
-
Prometheus.registry.histogram(:consumer_process_message_latency, 'Latency',
|
76
|
-
@offset_lag = Prometheus.registry.gauge(:consumer_offset_lag, 'Offset lag')
|
77
|
-
@time_lag = Prometheus.registry.gauge(:consumer_time_lag, 'Time lag of message')
|
78
|
-
@process_batch_errors = Prometheus.registry.counter(:consumer_process_batch_errors, 'Total errors in batch')
|
75
|
+
Prometheus.registry.histogram(:consumer_process_message_latency, docstring: 'Latency', buckets: LATENCY_BUCKETS, labels: [:client, :group_id, :topic, :partition])
|
76
|
+
@offset_lag = Prometheus.registry.gauge(:consumer_offset_lag, docstring: 'Offset lag', labels: [:client, :group_id, :topic, :partition])
|
77
|
+
@time_lag = Prometheus.registry.gauge(:consumer_time_lag, docstring: 'Time lag of message', labels: [:client, :group_id, :topic, :partition])
|
78
|
+
@process_batch_errors = Prometheus.registry.counter(:consumer_process_batch_errors, docstring: 'Total errors in batch', labels: [:client, :group_id, :topic, :partition])
|
79
79
|
@process_batch_latency =
|
80
|
-
Prometheus.registry.histogram(:consumer_process_batch_latency, 'Latency in batch',
|
81
|
-
@batch_size = Prometheus.registry.histogram(:consumer_batch_size, 'Size of batch',
|
82
|
-
@join_group = Prometheus.registry.histogram(:consumer_join_group, 'Time to join group',
|
83
|
-
@join_group_errors = Prometheus.registry.counter(:consumer_join_group_errors, 'Total error in joining group')
|
84
|
-
@sync_group = Prometheus.registry.histogram(:consumer_sync_group, 'Time to sync group',
|
85
|
-
@sync_group_errors = Prometheus.registry.counter(:consumer_sync_group_errors, 'Total error in syncing group')
|
86
|
-
@leave_group = Prometheus.registry.histogram(:consumer_leave_group, 'Time to leave group',
|
87
|
-
@leave_group_errors = Prometheus.registry.counter(:consumer_leave_group_errors, 'Total error in leaving group')
|
88
|
-
@pause_duration = Prometheus.registry.gauge(:consumer_pause_duration, 'Pause duration')
|
80
|
+
Prometheus.registry.histogram(:consumer_process_batch_latency, docstring: 'Latency in batch', buckets: LATENCY_BUCKETS, labels: [:client, :group_id, :topic, :partition])
|
81
|
+
@batch_size = Prometheus.registry.histogram(:consumer_batch_size, docstring: 'Size of batch', buckets: SIZE_BUCKETS, labels: [:client, :group_id, :topic, :partition])
|
82
|
+
@join_group = Prometheus.registry.histogram(:consumer_join_group, docstring: 'Time to join group', buckets: DELAY_BUCKETS, labels: [:client, :group_id])
|
83
|
+
@join_group_errors = Prometheus.registry.counter(:consumer_join_group_errors, docstring: 'Total error in joining group', labels: [:client, :group_id])
|
84
|
+
@sync_group = Prometheus.registry.histogram(:consumer_sync_group, docstring: 'Time to sync group', buckets: DELAY_BUCKETS, labels: [:client, :group_id])
|
85
|
+
@sync_group_errors = Prometheus.registry.counter(:consumer_sync_group_errors, docstring: 'Total error in syncing group', labels: [:client, :group_id])
|
86
|
+
@leave_group = Prometheus.registry.histogram(:consumer_leave_group, docstring: 'Time to leave group', buckets: DELAY_BUCKETS, labels: [:client, :group_id])
|
87
|
+
@leave_group_errors = Prometheus.registry.counter(:consumer_leave_group_errors, docstring: 'Total error in leaving group', labels: [:client, :group_id])
|
88
|
+
@pause_duration = Prometheus.registry.gauge(:consumer_pause_duration, docstring: 'Pause duration', labels: [:client, :group_id, :topic, :partition])
|
89
89
|
end
|
90
90
|
|
91
91
|
def process_message(event)
|
@@ -102,18 +102,18 @@ module Kafka
|
|
102
102
|
time_lag = create_time && ((Time.now - create_time) * 1000).to_i
|
103
103
|
|
104
104
|
if event.payload.key?(:exception)
|
105
|
-
@process_message_errors.increment(key)
|
105
|
+
@process_message_errors.increment(labels: key)
|
106
106
|
else
|
107
|
-
@process_message_latency.observe(
|
108
|
-
@process_messages.increment(key)
|
107
|
+
@process_message_latency.observe(event.duration, labels: key)
|
108
|
+
@process_messages.increment(labels: key)
|
109
109
|
end
|
110
110
|
|
111
|
-
@offset_lag.set(
|
111
|
+
@offset_lag.set(offset_lag, labels: key)
|
112
112
|
|
113
113
|
# Not all messages have timestamps.
|
114
114
|
return unless time_lag
|
115
115
|
|
116
|
-
@time_lag.set(
|
116
|
+
@time_lag.set(time_lag, labels: key)
|
117
117
|
end
|
118
118
|
|
119
119
|
def process_batch(event)
|
@@ -126,10 +126,10 @@ module Kafka
|
|
126
126
|
message_count = event.payload.fetch(:message_count)
|
127
127
|
|
128
128
|
if event.payload.key?(:exception)
|
129
|
-
@process_batch_errors.increment(key)
|
129
|
+
@process_batch_errors.increment(labels: key)
|
130
130
|
else
|
131
|
-
@process_batch_latency.observe(
|
132
|
-
@process_messages.increment(
|
131
|
+
@process_batch_latency.observe(event.duration, labels: key)
|
132
|
+
@process_messages.increment(by: message_count, labels: key)
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
@@ -143,29 +143,29 @@ module Kafka
|
|
143
143
|
offset_lag = event.payload.fetch(:offset_lag)
|
144
144
|
batch_size = event.payload.fetch(:message_count)
|
145
145
|
|
146
|
-
@batch_size.observe(
|
147
|
-
@offset_lag.set(
|
146
|
+
@batch_size.observe(batch_size, labels: key)
|
147
|
+
@offset_lag.set(offset_lag, labels: key)
|
148
148
|
end
|
149
149
|
|
150
150
|
def join_group(event)
|
151
151
|
key = { client: event.payload.fetch(:client_id), group_id: event.payload.fetch(:group_id) }
|
152
|
-
@join_group.observe(
|
152
|
+
@join_group.observe(event.duration, labels: key)
|
153
153
|
|
154
|
-
@join_group_errors.increment(key) if event.payload.key?(:exception)
|
154
|
+
@join_group_errors.increment(labels: key) if event.payload.key?(:exception)
|
155
155
|
end
|
156
156
|
|
157
157
|
def sync_group(event)
|
158
158
|
key = { client: event.payload.fetch(:client_id), group_id: event.payload.fetch(:group_id) }
|
159
|
-
@sync_group.observe(
|
159
|
+
@sync_group.observe(event.duration, labels: key)
|
160
160
|
|
161
|
-
@sync_group_errors.increment(key) if event.payload.key?(:exception)
|
161
|
+
@sync_group_errors.increment(labels: key) if event.payload.key?(:exception)
|
162
162
|
end
|
163
163
|
|
164
164
|
def leave_group(event)
|
165
165
|
key = { client: event.payload.fetch(:client_id), group_id: event.payload.fetch(:group_id) }
|
166
|
-
@leave_group.observe(
|
166
|
+
@leave_group.observe(event.duration, labels: key)
|
167
167
|
|
168
|
-
@leave_group_errors.increment(key) if event.payload.key?(:exception)
|
168
|
+
@leave_group_errors.increment(labels: key) if event.payload.key?(:exception)
|
169
169
|
end
|
170
170
|
|
171
171
|
def pause_status(event)
|
@@ -177,28 +177,28 @@ module Kafka
|
|
177
177
|
}
|
178
178
|
|
179
179
|
duration = event.payload.fetch(:duration)
|
180
|
-
@pause_duration.set(
|
180
|
+
@pause_duration.set(duration, labels: key)
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
184
|
class ProducerSubscriber < ActiveSupport::Subscriber
|
185
185
|
def initialize
|
186
186
|
super
|
187
|
-
@produce_messages = Prometheus.registry.counter(:producer_produced_messages, 'Produced messages total')
|
187
|
+
@produce_messages = Prometheus.registry.counter(:producer_produced_messages, docstring: 'Produced messages total', labels: [:client, :topic])
|
188
188
|
@produce_message_size =
|
189
|
-
Prometheus.registry.histogram(:producer_message_size, 'Message size',
|
190
|
-
@buffer_size = Prometheus.registry.histogram(:producer_buffer_size, 'Buffer size',
|
191
|
-
@buffer_fill_ratio = Prometheus.registry.histogram(:producer_buffer_fill_ratio, 'Buffer fill ratio')
|
192
|
-
@buffer_fill_percentage = Prometheus.registry.histogram(:producer_buffer_fill_percentage, 'Buffer fill percentage')
|
193
|
-
@produce_errors = Prometheus.registry.counter(:producer_produce_errors, 'Produce errors')
|
194
|
-
@deliver_errors = Prometheus.registry.counter(:producer_deliver_errors, 'Deliver error')
|
189
|
+
Prometheus.registry.histogram(:producer_message_size, docstring: 'Message size', buckets: SIZE_BUCKETS, labels: [:client, :topic])
|
190
|
+
@buffer_size = Prometheus.registry.histogram(:producer_buffer_size, docstring: 'Buffer size', buckets: SIZE_BUCKETS, labels: [:client])
|
191
|
+
@buffer_fill_ratio = Prometheus.registry.histogram(:producer_buffer_fill_ratio, docstring: 'Buffer fill ratio', labels: [:client])
|
192
|
+
@buffer_fill_percentage = Prometheus.registry.histogram(:producer_buffer_fill_percentage, docstring: 'Buffer fill percentage', labels: [:client])
|
193
|
+
@produce_errors = Prometheus.registry.counter(:producer_produce_errors, docstring: 'Produce errors', labels: [:client, :topic])
|
194
|
+
@deliver_errors = Prometheus.registry.counter(:producer_deliver_errors, docstring: 'Deliver error', labels: [:client])
|
195
195
|
@deliver_latency =
|
196
|
-
Prometheus.registry.histogram(:producer_deliver_latency, 'Delivery latency',
|
197
|
-
@deliver_messages = Prometheus.registry.counter(:producer_deliver_messages, 'Total count of delivered messages')
|
198
|
-
@deliver_attempts = Prometheus.registry.histogram(:producer_deliver_attempts, 'Delivery attempts')
|
199
|
-
@ack_messages = Prometheus.registry.counter(:producer_ack_messages, 'Ack')
|
200
|
-
@ack_delay = Prometheus.registry.histogram(:producer_ack_delay, 'Ack delay',
|
201
|
-
@ack_errors = Prometheus.registry.counter(:producer_ack_errors, 'Ack errors')
|
196
|
+
Prometheus.registry.histogram(:producer_deliver_latency, docstring: 'Delivery latency', buckets: LATENCY_BUCKETS, labels: [:client])
|
197
|
+
@deliver_messages = Prometheus.registry.counter(:producer_deliver_messages, docstring: 'Total count of delivered messages', labels: [:client])
|
198
|
+
@deliver_attempts = Prometheus.registry.histogram(:producer_deliver_attempts, docstring: 'Delivery attempts', labels: [:client])
|
199
|
+
@ack_messages = Prometheus.registry.counter(:producer_ack_messages, docstring: 'Ack', labels: [:client, :topic])
|
200
|
+
@ack_delay = Prometheus.registry.histogram(:producer_ack_delay, docstring: 'Ack delay', buckets: LATENCY_BUCKETS, labels: [:client, :topic])
|
201
|
+
@ack_errors = Prometheus.registry.counter(:producer_ack_errors, docstring: 'Ack errors', labels: [:client, :topic])
|
202
202
|
end
|
203
203
|
|
204
204
|
def produce_message(event)
|
@@ -212,20 +212,20 @@ module Kafka
|
|
212
212
|
buffer_fill_percentage = buffer_fill_ratio * 100.0
|
213
213
|
|
214
214
|
# This gets us the write rate.
|
215
|
-
@produce_messages.increment(key)
|
216
|
-
@produce_message_size.observe(
|
215
|
+
@produce_messages.increment(labels: key)
|
216
|
+
@produce_message_size.observe(message_size, labels: key)
|
217
217
|
|
218
218
|
# This gets us the avg/max buffer size per producer.
|
219
|
-
@buffer_size.observe({ client: client }
|
219
|
+
@buffer_size.observe(buffer_size, labels: { client: client })
|
220
220
|
|
221
221
|
# This gets us the avg/max buffer fill ratio per producer.
|
222
|
-
@buffer_fill_ratio.observe({ client: client }
|
223
|
-
@buffer_fill_percentage.observe({ client: client }
|
222
|
+
@buffer_fill_ratio.observe(buffer_fill_ratio, labels: { client: client })
|
223
|
+
@buffer_fill_percentage.observe(buffer_fill_percentage, labels: { client: client })
|
224
224
|
end
|
225
225
|
|
226
226
|
def buffer_overflow(event)
|
227
227
|
key = { client: event.payload.fetch(:client_id), topic: event.payload.fetch(:topic) }
|
228
|
-
@produce_errors.increment(key)
|
228
|
+
@produce_errors.increment(labels: key)
|
229
229
|
end
|
230
230
|
|
231
231
|
def deliver_messages(event)
|
@@ -233,40 +233,40 @@ module Kafka
|
|
233
233
|
message_count = event.payload.fetch(:delivered_message_count)
|
234
234
|
attempts = event.payload.fetch(:attempts)
|
235
235
|
|
236
|
-
@deliver_errors.increment(key) if event.payload.key?(:exception)
|
237
|
-
@deliver_latency.observe(
|
236
|
+
@deliver_errors.increment(labels: key) if event.payload.key?(:exception)
|
237
|
+
@deliver_latency.observe(event.duration, labels: key)
|
238
238
|
|
239
239
|
# Messages delivered to Kafka:
|
240
|
-
@deliver_messages.increment(
|
240
|
+
@deliver_messages.increment(by: message_count, labels: key)
|
241
241
|
|
242
242
|
# Number of attempts to deliver messages:
|
243
|
-
@deliver_attempts.observe(
|
243
|
+
@deliver_attempts.observe(attempts, labels: key)
|
244
244
|
end
|
245
245
|
|
246
246
|
def ack_message(event)
|
247
247
|
key = { client: event.payload.fetch(:client_id), topic: event.payload.fetch(:topic) }
|
248
248
|
|
249
249
|
# Number of messages ACK'd for the topic.
|
250
|
-
@ack_messages.increment(key)
|
250
|
+
@ack_messages.increment(labels: key)
|
251
251
|
|
252
252
|
# Histogram of delay between a message being produced and it being ACK'd.
|
253
|
-
@ack_delay.observe(
|
253
|
+
@ack_delay.observe(event.payload.fetch(:delay), labels: key)
|
254
254
|
end
|
255
255
|
|
256
256
|
def topic_error(event)
|
257
257
|
key = { client: event.payload.fetch(:client_id), topic: event.payload.fetch(:topic) }
|
258
258
|
|
259
|
-
@ack_errors.increment(key)
|
259
|
+
@ack_errors.increment(labels: key)
|
260
260
|
end
|
261
261
|
end
|
262
262
|
|
263
263
|
class AsyncProducerSubscriber < ActiveSupport::Subscriber
|
264
264
|
def initialize
|
265
265
|
super
|
266
|
-
@queue_size = Prometheus.registry.histogram(:async_producer_queue_size, 'Queue size',
|
267
|
-
@queue_fill_ratio = Prometheus.registry.histogram(:async_producer_queue_fill_ratio, 'Queue fill ratio')
|
268
|
-
@produce_errors = Prometheus.registry.counter(:async_producer_produce_errors, 'Producer errors')
|
269
|
-
@dropped_messages = Prometheus.registry.counter(:async_producer_dropped_messages, 'Dropped messages')
|
266
|
+
@queue_size = Prometheus.registry.histogram(:async_producer_queue_size, docstring: 'Queue size', buckets: SIZE_BUCKETS, labels: [:client, :topic])
|
267
|
+
@queue_fill_ratio = Prometheus.registry.histogram(:async_producer_queue_fill_ratio, docstring: 'Queue fill ratio', labels: [:client, :topic])
|
268
|
+
@produce_errors = Prometheus.registry.counter(:async_producer_produce_errors, docstring: 'Producer errors', labels: [:client, :topic])
|
269
|
+
@dropped_messages = Prometheus.registry.counter(:async_producer_dropped_messages, docstring: 'Dropped messages', labels: [:client])
|
270
270
|
end
|
271
271
|
|
272
272
|
def enqueue_message(event)
|
@@ -277,29 +277,28 @@ module Kafka
|
|
277
277
|
queue_fill_ratio = queue_size.to_f / max_queue_size.to_f
|
278
278
|
|
279
279
|
# This gets us the avg/max queue size per producer.
|
280
|
-
@queue_size.observe(
|
280
|
+
@queue_size.observe(queue_size, labels: key)
|
281
281
|
|
282
282
|
# This gets us the avg/max queue fill ratio per producer.
|
283
|
-
@queue_fill_ratio.observe(
|
283
|
+
@queue_fill_ratio.observe(queue_fill_ratio, labels: key)
|
284
284
|
end
|
285
285
|
|
286
286
|
def buffer_overflow(event)
|
287
287
|
key = { client: event.payload.fetch(:client_id), topic: event.payload.fetch(:topic) }
|
288
|
-
@produce_errors.increment(key)
|
288
|
+
@produce_errors.increment(labels: key)
|
289
289
|
end
|
290
290
|
|
291
291
|
def drop_messages(event)
|
292
292
|
key = { client: event.payload.fetch(:client_id) }
|
293
293
|
message_count = event.payload.fetch(:message_count)
|
294
|
-
|
295
|
-
@dropped_messages.increment(key, message_count)
|
294
|
+
@dropped_messages.increment(by: message_count, labels: key)
|
296
295
|
end
|
297
296
|
end
|
298
297
|
|
299
298
|
class FetcherSubscriber < ActiveSupport::Subscriber
|
300
299
|
def initialize
|
301
300
|
super
|
302
|
-
@queue_size = Prometheus.registry.gauge(:fetcher_queue_size, 'Queue size')
|
301
|
+
@queue_size = Prometheus.registry.gauge(:fetcher_queue_size, docstring: 'Queue size', labels: [:client, :group_id])
|
303
302
|
end
|
304
303
|
|
305
304
|
def loop(event)
|
@@ -307,7 +306,7 @@ module Kafka
|
|
307
306
|
client = event.payload.fetch(:client_id)
|
308
307
|
group_id = event.payload.fetch(:group_id)
|
309
308
|
|
310
|
-
@queue_size.set({ client: client, group_id: group_id }
|
309
|
+
@queue_size.set(queue_size, labels: { client: client, group_id: group_id })
|
311
310
|
end
|
312
311
|
end
|
313
312
|
end
|
@@ -126,7 +126,7 @@ module Kafka
|
|
126
126
|
# Writes an integer under varints serializing to the IO object.
|
127
127
|
# https://developers.google.com/protocol-buffers/docs/encoding#varints
|
128
128
|
#
|
129
|
-
# @param
|
129
|
+
# @param int [Integer]
|
130
130
|
# @return [nil]
|
131
131
|
def write_varint(int)
|
132
132
|
int = int << 1
|
@@ -7,13 +7,14 @@ module Kafka
|
|
7
7
|
class JoinGroupRequest
|
8
8
|
PROTOCOL_TYPE = "consumer"
|
9
9
|
|
10
|
-
def initialize(group_id:, session_timeout:, member_id:, topics: [])
|
10
|
+
def initialize(group_id:, session_timeout:, rebalance_timeout:, member_id:, topics: [], protocol_name:, user_data: nil)
|
11
11
|
@group_id = group_id
|
12
12
|
@session_timeout = session_timeout * 1000 # Kafka wants ms.
|
13
|
+
@rebalance_timeout = rebalance_timeout * 1000 # Kafka wants ms.
|
13
14
|
@member_id = member_id || ""
|
14
15
|
@protocol_type = PROTOCOL_TYPE
|
15
16
|
@group_protocols = {
|
16
|
-
|
17
|
+
protocol_name => ConsumerGroupProtocol.new(topics: topics, user_data: user_data),
|
17
18
|
}
|
18
19
|
end
|
19
20
|
|
@@ -21,6 +22,10 @@ module Kafka
|
|
21
22
|
JOIN_GROUP_API
|
22
23
|
end
|
23
24
|
|
25
|
+
def api_version
|
26
|
+
1
|
27
|
+
end
|
28
|
+
|
24
29
|
def response_class
|
25
30
|
JoinGroupResponse
|
26
31
|
end
|
@@ -28,6 +33,7 @@ module Kafka
|
|
28
33
|
def encode(encoder)
|
29
34
|
encoder.write_string(@group_id)
|
30
35
|
encoder.write_int32(@session_timeout)
|
36
|
+
encoder.write_int32(@rebalance_timeout)
|
31
37
|
encoder.write_string(@member_id)
|
32
38
|
encoder.write_string(@protocol_type)
|
33
39
|
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module Kafka
|
4
4
|
module Protocol
|
5
5
|
class JoinGroupResponse
|
6
|
+
Metadata = Struct.new(:version, :topics, :user_data)
|
7
|
+
|
6
8
|
attr_reader :error_code
|
7
9
|
|
8
10
|
attr_reader :generation_id, :group_protocol
|
@@ -25,7 +27,13 @@ module Kafka
|
|
25
27
|
group_protocol: decoder.string,
|
26
28
|
leader_id: decoder.string,
|
27
29
|
member_id: decoder.string,
|
28
|
-
members: Hash[
|
30
|
+
members: Hash[
|
31
|
+
decoder.array do
|
32
|
+
member_id = decoder.string
|
33
|
+
d = Decoder.from_string(decoder.bytes)
|
34
|
+
[member_id, Metadata.new(d.int16, d.array { d.string }, d.bytes)]
|
35
|
+
end
|
36
|
+
],
|
29
37
|
)
|
30
38
|
end
|
31
39
|
end
|
@@ -77,7 +77,7 @@ module Kafka
|
|
77
77
|
record_batch_encoder.write_int8(MAGIC_BYTE)
|
78
78
|
|
79
79
|
body = encode_record_batch_body
|
80
|
-
crc = Digest::CRC32c.checksum(body)
|
80
|
+
crc = ::Digest::CRC32c.checksum(body)
|
81
81
|
|
82
82
|
record_batch_encoder.write_int32(crc)
|
83
83
|
record_batch_encoder.write(body)
|
@@ -213,7 +213,7 @@ module Kafka
|
|
213
213
|
end
|
214
214
|
|
215
215
|
def mark_control_record
|
216
|
-
if
|
216
|
+
if is_control_batch
|
217
217
|
record = @records.first
|
218
218
|
record.is_control_record = true unless record.nil?
|
219
219
|
end
|
@@ -8,7 +8,7 @@ module Kafka
|
|
8
8
|
|
9
9
|
class SaslHandshakeRequest
|
10
10
|
|
11
|
-
SUPPORTED_MECHANISMS = %w(GSSAPI PLAIN SCRAM-SHA-256 SCRAM-SHA-512 OAUTHBEARER)
|
11
|
+
SUPPORTED_MECHANISMS = %w(AWS_MSK_IAM GSSAPI PLAIN SCRAM-SHA-256 SCRAM-SHA-512 OAUTHBEARER)
|
12
12
|
|
13
13
|
def initialize(mechanism)
|
14
14
|
unless SUPPORTED_MECHANISMS.include?(mechanism)
|
@@ -13,9 +13,12 @@ module Kafka
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.decode(decoder)
|
16
|
+
error_code = decoder.int16
|
17
|
+
member_assignment_bytes = decoder.bytes
|
18
|
+
|
16
19
|
new(
|
17
|
-
error_code:
|
18
|
-
member_assignment: MemberAssignment.decode(Decoder.from_string(
|
20
|
+
error_code: error_code,
|
21
|
+
member_assignment: member_assignment_bytes ? MemberAssignment.decode(Decoder.from_string(member_assignment_bytes)) : nil
|
19
22
|
)
|
20
23
|
end
|
21
24
|
end
|
@@ -1,17 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Kafka
|
2
4
|
module Protocol
|
3
5
|
class TxnOffsetCommitResponse
|
6
|
+
class PartitionError
|
7
|
+
attr_reader :partition, :error_code
|
8
|
+
|
9
|
+
def initialize(partition:, error_code:)
|
10
|
+
@partition = partition
|
11
|
+
@error_code = error_code
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class TopicPartitionsError
|
16
|
+
attr_reader :topic, :partitions
|
17
|
+
|
18
|
+
def initialize(topic:, partitions:)
|
19
|
+
@topic = topic
|
20
|
+
@partitions = partitions
|
21
|
+
end
|
22
|
+
end
|
4
23
|
|
5
|
-
attr_reader :
|
24
|
+
attr_reader :errors
|
6
25
|
|
7
|
-
def initialize(
|
8
|
-
@
|
26
|
+
def initialize(errors:)
|
27
|
+
@errors = errors
|
9
28
|
end
|
10
29
|
|
11
30
|
def self.decode(decoder)
|
12
31
|
_throttle_time_ms = decoder.int32
|
13
|
-
|
14
|
-
|
32
|
+
errors = decoder.array do
|
33
|
+
TopicPartitionsError.new(
|
34
|
+
topic: decoder.string,
|
35
|
+
partitions: decoder.array do
|
36
|
+
PartitionError.new(
|
37
|
+
partition: decoder.int32,
|
38
|
+
error_code: decoder.int16
|
39
|
+
)
|
40
|
+
end
|
41
|
+
)
|
42
|
+
end
|
43
|
+
new(errors: errors)
|
15
44
|
end
|
16
45
|
end
|
17
46
|
end
|
@@ -1,54 +1,52 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "kafka/protocol/member_assignment"
|
4
|
-
|
5
1
|
module Kafka
|
6
2
|
|
7
|
-
# A
|
8
|
-
#
|
3
|
+
# A round robin assignment strategy inpired on the
|
4
|
+
# original java client round robin assignor. It's capable
|
5
|
+
# of handling identical as well as different topic subscriptions
|
6
|
+
# accross the same consumer group.
|
9
7
|
class RoundRobinAssignmentStrategy
|
10
|
-
def
|
11
|
-
|
8
|
+
def protocol_name
|
9
|
+
"roundrobin"
|
12
10
|
end
|
13
11
|
|
14
12
|
# Assign the topic partitions to the group members.
|
15
13
|
#
|
16
|
-
# @param
|
17
|
-
# @param
|
18
|
-
#
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
14
|
+
# @param cluster [Kafka::Cluster]
|
15
|
+
# @param members [Hash<String, Kafka::Protocol::JoinGroupResponse::Metadata>] a hash
|
16
|
+
# mapping member ids to metadata
|
17
|
+
# @param partitions [Array<Kafka::ConsumerGroup::Assignor::Partition>] a list of
|
18
|
+
# partitions the consumer group processes
|
19
|
+
# @return [Hash<String, Array<Kafka::ConsumerGroup::Assignor::Partition>] a hash
|
20
|
+
# mapping member ids to partitions.
|
21
|
+
def call(cluster:, members:, partitions:)
|
22
|
+
partitions_per_member = Hash.new {|h, k| h[k] = [] }
|
23
|
+
relevant_partitions = valid_sorted_partitions(members, partitions)
|
24
|
+
members_ids = members.keys
|
25
|
+
iterator = (0...members.size).cycle
|
26
|
+
idx = iterator.next
|
27
|
+
|
28
|
+
relevant_partitions.each do |partition|
|
29
|
+
topic = partition.topic
|
30
|
+
|
31
|
+
while !members[members_ids[idx]].topics.include?(topic)
|
32
|
+
idx = iterator.next
|
32
33
|
end
|
33
|
-
|
34
|
+
|
35
|
+
partitions_per_member[members_ids[idx]] << partition
|
36
|
+
idx = iterator.next
|
34
37
|
end
|
35
38
|
|
36
|
-
partitions_per_member
|
37
|
-
|
38
|
-
end.values
|
39
|
+
partitions_per_member
|
40
|
+
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
42
|
+
def valid_sorted_partitions(members, partitions)
|
43
|
+
subscribed_topics = members.map do |id, metadata|
|
44
|
+
metadata && metadata.topics
|
45
|
+
end.flatten.compact
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
retry
|
47
|
+
partitions
|
48
|
+
.select { |partition| subscribed_topics.include?(partition.topic) }
|
49
|
+
.sort_by { |partition| partition.topic }
|
52
50
|
end
|
53
51
|
end
|
54
52
|
end
|