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.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +388 -111
  3. data/{LICENSE → MIT-LICENSE} +2 -1
  4. data/README.md +90 -43
  5. data/Rakefile +21 -23
  6. data/bin/verify_kafka_warnings +44 -0
  7. data/dist/librdkafka-2.15.1.tar.gz +0 -0
  8. data/dist/patches/rdkafka_global_init.patch +15 -0
  9. data/docker-compose-ssl.yml +39 -0
  10. data/docker-compose.yml +22 -17
  11. data/ext/Rakefile +97 -30
  12. data/lib/rdkafka/abstract_handle.rb +113 -34
  13. data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
  14. data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
  15. data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
  16. data/lib/rdkafka/admin/config_binding_result.rb +30 -0
  17. data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
  18. data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
  19. data/lib/rdkafka/admin/create_acl_report.rb +26 -0
  20. data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
  21. data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
  22. data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
  23. data/lib/rdkafka/admin/create_topic_report.rb +4 -1
  24. data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
  25. data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
  26. data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
  27. data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
  28. data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
  29. data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
  30. data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
  31. data/lib/rdkafka/admin/delete_records_report.rb +25 -0
  32. data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
  33. data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
  34. data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
  35. data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
  36. data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
  37. data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
  38. data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
  39. data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
  40. data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
  41. data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
  42. data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
  43. data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
  44. data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
  45. data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
  46. data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
  47. data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
  48. data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
  49. data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
  50. data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
  51. data/lib/rdkafka/admin.rb +1381 -38
  52. data/lib/rdkafka/bindings/admin/acls.rb +85 -0
  53. data/lib/rdkafka/bindings/admin/configs.rb +37 -0
  54. data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
  55. data/lib/rdkafka/bindings/admin.rb +197 -0
  56. data/lib/rdkafka/bindings/callbacks.rb +100 -0
  57. data/lib/rdkafka/bindings/configuration.rb +50 -0
  58. data/lib/rdkafka/bindings/consumer.rb +83 -0
  59. data/lib/rdkafka/bindings/errors.rb +15 -0
  60. data/lib/rdkafka/bindings/handle.rb +16 -0
  61. data/lib/rdkafka/bindings/messages.rb +28 -0
  62. data/lib/rdkafka/bindings/metadata.rb +14 -0
  63. data/lib/rdkafka/bindings/polling.rb +17 -0
  64. data/lib/rdkafka/bindings/producer.rb +77 -0
  65. data/lib/rdkafka/bindings/structs.rb +62 -0
  66. data/lib/rdkafka/bindings.rb +69 -294
  67. data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
  68. data/lib/rdkafka/callbacks/base_handler.rb +62 -0
  69. data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
  70. data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
  71. data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
  72. data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
  73. data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
  74. data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
  75. data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
  76. data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
  77. data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
  78. data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
  79. data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
  80. data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
  81. data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
  82. data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
  83. data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
  84. data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
  85. data/lib/rdkafka/callbacks.rb +273 -52
  86. data/lib/rdkafka/clients.rb +81 -0
  87. data/lib/rdkafka/config.rb +333 -91
  88. data/lib/rdkafka/consumer/allocator.rb +38 -0
  89. data/lib/rdkafka/consumer/headers.rb +39 -25
  90. data/lib/rdkafka/consumer/message.rb +18 -20
  91. data/lib/rdkafka/consumer/partition.rb +15 -4
  92. data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
  93. data/lib/rdkafka/consumer.rb +765 -192
  94. data/lib/rdkafka/defaults.rb +123 -0
  95. data/lib/rdkafka/error.rb +40 -14
  96. data/lib/rdkafka/helpers/list_offsets.rb +127 -0
  97. data/lib/rdkafka/helpers/metadata.rb +29 -0
  98. data/lib/rdkafka/helpers/oauth.rb +95 -0
  99. data/lib/rdkafka/helpers/time.rb +19 -0
  100. data/lib/rdkafka/metadata.rb +138 -26
  101. data/lib/rdkafka/native_kafka.rb +142 -22
  102. data/lib/rdkafka/producer/delivery_handle.rb +27 -7
  103. data/lib/rdkafka/producer/delivery_report.rb +71 -4
  104. data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
  105. data/lib/rdkafka/producer.rb +422 -81
  106. data/lib/rdkafka/version.rb +6 -3
  107. data/lib/rdkafka.rb +73 -1
  108. data/package-lock.json +331 -0
  109. data/package.json +9 -0
  110. data/rdkafka.gemspec +78 -25
  111. data/renovate.json +111 -0
  112. metadata +114 -134
  113. data/.gitignore +0 -8
  114. data/.rspec +0 -1
  115. data/.semaphore/semaphore.yml +0 -27
  116. data/.yardopts +0 -2
  117. data/Gemfile +0 -5
  118. data/Guardfile +0 -19
  119. data/ext/README.md +0 -18
  120. data/spec/rdkafka/abstract_handle_spec.rb +0 -115
  121. data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
  122. data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
  123. data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
  124. data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
  125. data/spec/rdkafka/admin_spec.rb +0 -204
  126. data/spec/rdkafka/bindings_spec.rb +0 -136
  127. data/spec/rdkafka/callbacks_spec.rb +0 -22
  128. data/spec/rdkafka/config_spec.rb +0 -197
  129. data/spec/rdkafka/consumer/headers_spec.rb +0 -62
  130. data/spec/rdkafka/consumer/message_spec.rb +0 -141
  131. data/spec/rdkafka/consumer/partition_spec.rb +0 -59
  132. data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
  133. data/spec/rdkafka/consumer_spec.rb +0 -1106
  134. data/spec/rdkafka/error_spec.rb +0 -91
  135. data/spec/rdkafka/metadata_spec.rb +0 -80
  136. data/spec/rdkafka/native_kafka_spec.rb +0 -124
  137. data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
  138. data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
  139. data/spec/rdkafka/producer_spec.rb +0 -557
  140. data/spec/spec_helper.rb +0 -155
@@ -1,1106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "ostruct"
5
- require 'securerandom'
6
-
7
- describe Rdkafka::Consumer do
8
- let(:consumer) { rdkafka_consumer_config.consumer }
9
- let(:producer) { rdkafka_producer_config.producer }
10
-
11
- after { consumer.close }
12
- after { producer.close }
13
-
14
- describe "#subscribe, #unsubscribe and #subscription" do
15
- it "should subscribe, unsubscribe and return the subscription" do
16
- expect(consumer.subscription).to be_empty
17
-
18
- consumer.subscribe("consume_test_topic")
19
-
20
- expect(consumer.subscription).not_to be_empty
21
- expected_subscription = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
22
- list.add_topic("consume_test_topic")
23
- end
24
- expect(consumer.subscription).to eq expected_subscription
25
-
26
- consumer.unsubscribe
27
-
28
- expect(consumer.subscription).to be_empty
29
- end
30
-
31
- it "should raise an error when subscribing fails" do
32
- expect(Rdkafka::Bindings).to receive(:rd_kafka_subscribe).and_return(20)
33
-
34
- expect {
35
- consumer.subscribe("consume_test_topic")
36
- }.to raise_error(Rdkafka::RdkafkaError)
37
- end
38
-
39
- it "should raise an error when unsubscribing fails" do
40
- expect(Rdkafka::Bindings).to receive(:rd_kafka_unsubscribe).and_return(20)
41
-
42
- expect {
43
- consumer.unsubscribe
44
- }.to raise_error(Rdkafka::RdkafkaError)
45
- end
46
-
47
- it "should raise an error when fetching the subscription fails" do
48
- expect(Rdkafka::Bindings).to receive(:rd_kafka_subscription).and_return(20)
49
-
50
- expect {
51
- consumer.subscription
52
- }.to raise_error(Rdkafka::RdkafkaError)
53
- end
54
- end
55
-
56
- describe "#pause and #resume" do
57
- context "subscription" do
58
- let(:timeout) { 2000 }
59
-
60
- before { consumer.subscribe("consume_test_topic") }
61
- after { consumer.unsubscribe }
62
-
63
- it "should pause and then resume" do
64
- # 1. partitions are assigned
65
- wait_for_assignment(consumer)
66
- expect(consumer.assignment).not_to be_empty
67
-
68
- # 2. send a first message
69
- send_one_message
70
-
71
- # 3. ensure that message is successfully consumed
72
- records = consumer.poll(timeout)
73
- expect(records).not_to be_nil
74
- consumer.commit
75
-
76
- # 4. send a second message
77
- send_one_message
78
-
79
- # 5. pause the subscription
80
- tpl = Rdkafka::Consumer::TopicPartitionList.new
81
- tpl.add_topic("consume_test_topic", (0..2))
82
- consumer.pause(tpl)
83
-
84
- # 6. ensure that messages are not available
85
- records = consumer.poll(timeout)
86
- expect(records).to be_nil
87
-
88
- # 7. resume the subscription
89
- tpl = Rdkafka::Consumer::TopicPartitionList.new
90
- tpl.add_topic("consume_test_topic", (0..2))
91
- consumer.resume(tpl)
92
-
93
- # 8. ensure that message is successfully consumed
94
- records = consumer.poll(timeout)
95
- expect(records).not_to be_nil
96
- end
97
- end
98
-
99
- it "should raise when not TopicPartitionList" do
100
- expect { consumer.pause(true) }.to raise_error(TypeError)
101
- expect { consumer.resume(true) }.to raise_error(TypeError)
102
- end
103
-
104
- it "should raise an error when pausing fails" do
105
- list = Rdkafka::Consumer::TopicPartitionList.new.tap { |tpl| tpl.add_topic('topic', (0..1)) }
106
-
107
- expect(Rdkafka::Bindings).to receive(:rd_kafka_pause_partitions).and_return(20)
108
- expect {
109
- consumer.pause(list)
110
- }.to raise_error do |err|
111
- expect(err).to be_instance_of(Rdkafka::RdkafkaTopicPartitionListError)
112
- expect(err.topic_partition_list).to be
113
- end
114
- end
115
-
116
- it "should raise an error when resume fails" do
117
- expect(Rdkafka::Bindings).to receive(:rd_kafka_resume_partitions).and_return(20)
118
- expect {
119
- consumer.resume(Rdkafka::Consumer::TopicPartitionList.new)
120
- }.to raise_error Rdkafka::RdkafkaError
121
- end
122
-
123
- def send_one_message
124
- producer.produce(
125
- topic: "consume_test_topic",
126
- payload: "payload 1",
127
- key: "key 1"
128
- ).wait
129
- end
130
- end
131
-
132
- describe "#seek" do
133
- it "should raise an error when seeking fails" do
134
- fake_msg = OpenStruct.new(topic: "consume_test_topic", partition: 0, offset: 0)
135
-
136
- expect(Rdkafka::Bindings).to receive(:rd_kafka_seek).and_return(20)
137
- expect {
138
- consumer.seek(fake_msg)
139
- }.to raise_error Rdkafka::RdkafkaError
140
- end
141
-
142
- context "subscription" do
143
- let(:timeout) { 1000 }
144
-
145
- before do
146
- consumer.subscribe("consume_test_topic")
147
-
148
- # 1. partitions are assigned
149
- wait_for_assignment(consumer)
150
- expect(consumer.assignment).not_to be_empty
151
-
152
- # 2. eat unrelated messages
153
- while(consumer.poll(timeout)) do; end
154
- end
155
- after { consumer.unsubscribe }
156
-
157
- def send_one_message(val)
158
- producer.produce(
159
- topic: "consume_test_topic",
160
- payload: "payload #{val}",
161
- key: "key 1",
162
- partition: 0
163
- ).wait
164
- end
165
-
166
- it "works when a partition is paused" do
167
- # 3. get reference message
168
- send_one_message(:a)
169
- message1 = consumer.poll(timeout)
170
- expect(message1&.payload).to eq "payload a"
171
-
172
- # 4. pause the subscription
173
- tpl = Rdkafka::Consumer::TopicPartitionList.new
174
- tpl.add_topic("consume_test_topic", 1)
175
- consumer.pause(tpl)
176
-
177
- # 5. seek to previous message
178
- consumer.seek(message1)
179
-
180
- # 6. resume the subscription
181
- tpl = Rdkafka::Consumer::TopicPartitionList.new
182
- tpl.add_topic("consume_test_topic", 1)
183
- consumer.resume(tpl)
184
-
185
- # 7. ensure same message is read again
186
- message2 = consumer.poll(timeout)
187
- consumer.commit
188
- expect(message1.offset).to eq message2.offset
189
- expect(message1.payload).to eq message2.payload
190
- end
191
-
192
- it "allows skipping messages" do
193
- # 3. send messages
194
- send_one_message(:a)
195
- send_one_message(:b)
196
- send_one_message(:c)
197
-
198
- # 4. get reference message
199
- message = consumer.poll(timeout)
200
- expect(message&.payload).to eq "payload a"
201
-
202
- # 5. seek over one message
203
- fake_msg = message.dup
204
- fake_msg.instance_variable_set(:@offset, fake_msg.offset + 2)
205
- consumer.seek(fake_msg)
206
-
207
- # 6. ensure that only one message is available
208
- records = consumer.poll(timeout)
209
- expect(records&.payload).to eq "payload c"
210
- records = consumer.poll(timeout)
211
- expect(records).to be_nil
212
- end
213
- end
214
- end
215
-
216
- describe "#assign and #assignment" do
217
- it "should return an empty assignment if nothing is assigned" do
218
- expect(consumer.assignment).to be_empty
219
- end
220
-
221
- it "should only accept a topic partition list in assign" do
222
- expect {
223
- consumer.assign("list")
224
- }.to raise_error TypeError
225
- end
226
-
227
- it "should raise an error when assigning fails" do
228
- expect(Rdkafka::Bindings).to receive(:rd_kafka_assign).and_return(20)
229
- expect {
230
- consumer.assign(Rdkafka::Consumer::TopicPartitionList.new)
231
- }.to raise_error Rdkafka::RdkafkaError
232
- end
233
-
234
- it "should assign specific topic/partitions and return that assignment" do
235
- tpl = Rdkafka::Consumer::TopicPartitionList.new
236
- tpl.add_topic("consume_test_topic", (0..2))
237
- consumer.assign(tpl)
238
-
239
- assignment = consumer.assignment
240
- expect(assignment).not_to be_empty
241
- expect(assignment.to_h["consume_test_topic"].length).to eq 3
242
- end
243
-
244
- it "should return the assignment when subscribed" do
245
- # Make sure there's a message
246
- producer.produce(
247
- topic: "consume_test_topic",
248
- payload: "payload 1",
249
- key: "key 1",
250
- partition: 0
251
- ).wait
252
-
253
- # Subscribe and poll until partitions are assigned
254
- consumer.subscribe("consume_test_topic")
255
- 100.times do
256
- consumer.poll(100)
257
- break unless consumer.assignment.empty?
258
- end
259
-
260
- assignment = consumer.assignment
261
- expect(assignment).not_to be_empty
262
- expect(assignment.to_h["consume_test_topic"].length).to eq 3
263
- end
264
-
265
- it "should raise an error when getting assignment fails" do
266
- expect(Rdkafka::Bindings).to receive(:rd_kafka_assignment).and_return(20)
267
- expect {
268
- consumer.assignment
269
- }.to raise_error Rdkafka::RdkafkaError
270
- end
271
- end
272
-
273
- describe "#close" do
274
- it "should close a consumer" do
275
- consumer.subscribe("consume_test_topic")
276
- 100.times do |i|
277
- producer.produce(
278
- topic: "consume_test_topic",
279
- payload: "payload #{i}",
280
- key: "key #{i}",
281
- partition: 0
282
- ).wait
283
- end
284
- consumer.close
285
- expect {
286
- consumer.poll(100)
287
- }.to raise_error(Rdkafka::ClosedConsumerError, /poll/)
288
- end
289
-
290
- context 'when there are outgoing operations in other threads' do
291
- it 'should wait and not crash' do
292
- times = []
293
-
294
- # Run a long running poll
295
- thread = Thread.new do
296
- times << Time.now
297
- consumer.subscribe("empty_test_topic")
298
- times << Time.now
299
- consumer.poll(1_000)
300
- times << Time.now
301
- end
302
-
303
- # Make sure it starts before we close
304
- sleep(0.1)
305
- consumer.close
306
- close_time = Time.now
307
- thread.join
308
-
309
- times.each { |op_time| expect(op_time).to be < close_time }
310
- end
311
- end
312
- end
313
-
314
- describe "#commit, #committed and #store_offset" do
315
- # Make sure there's a stored offset
316
- let!(:report) do
317
- producer.produce(
318
- topic: "consume_test_topic",
319
- payload: "payload 1",
320
- key: "key 1",
321
- partition: 0
322
- ).wait
323
- end
324
-
325
- let(:message) do
326
- wait_for_message(
327
- topic: "consume_test_topic",
328
- delivery_report: report,
329
- consumer: consumer
330
- )
331
- end
332
-
333
- it "should only accept a topic partition list in committed" do
334
- expect {
335
- consumer.committed("list")
336
- }.to raise_error TypeError
337
- end
338
-
339
- it "should commit in sync mode" do
340
- expect {
341
- consumer.commit(nil, true)
342
- }.not_to raise_error
343
- end
344
-
345
- it "should only accept a topic partition list in commit if not nil" do
346
- expect {
347
- consumer.commit("list")
348
- }.to raise_error TypeError
349
- end
350
-
351
- context "with a committed consumer" do
352
- before :all do
353
- # Make sure there are some messages.
354
- handles = []
355
- producer = rdkafka_producer_config.producer
356
- 10.times do
357
- (0..2).each do |i|
358
- handles << producer.produce(
359
- topic: "consume_test_topic",
360
- payload: "payload 1",
361
- key: "key 1",
362
- partition: i
363
- )
364
- end
365
- end
366
- handles.each(&:wait)
367
- producer.close
368
- end
369
-
370
- before do
371
- consumer.subscribe("consume_test_topic")
372
- wait_for_assignment(consumer)
373
- list = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
374
- list.add_topic_and_partitions_with_offsets("consume_test_topic", 0 => 1, 1 => 1, 2 => 1)
375
- end
376
- consumer.commit(list)
377
- end
378
-
379
- it "should commit a specific topic partion list" do
380
- list = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
381
- list.add_topic_and_partitions_with_offsets("consume_test_topic", 0 => 1, 1 => 2, 2 => 3)
382
- end
383
- consumer.commit(list)
384
-
385
- partitions = consumer.committed(list).to_h["consume_test_topic"]
386
- expect(partitions[0].offset).to eq 1
387
- expect(partitions[1].offset).to eq 2
388
- expect(partitions[2].offset).to eq 3
389
- end
390
-
391
- it "should raise an error when committing fails" do
392
- expect(Rdkafka::Bindings).to receive(:rd_kafka_commit).and_return(20)
393
-
394
- expect {
395
- consumer.commit
396
- }.to raise_error(Rdkafka::RdkafkaError)
397
- end
398
-
399
- it "should fetch the committed offsets for the current assignment" do
400
- partitions = consumer.committed.to_h["consume_test_topic"]
401
- expect(partitions).not_to be_nil
402
- expect(partitions[0].offset).to eq 1
403
- end
404
-
405
- it "should fetch the committed offsets for a specified topic partition list" do
406
- list = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
407
- list.add_topic("consume_test_topic", [0, 1, 2])
408
- end
409
- partitions = consumer.committed(list).to_h["consume_test_topic"]
410
- expect(partitions).not_to be_nil
411
- expect(partitions[0].offset).to eq 1
412
- expect(partitions[1].offset).to eq 1
413
- expect(partitions[2].offset).to eq 1
414
- end
415
-
416
- it "should raise an error when getting committed fails" do
417
- expect(Rdkafka::Bindings).to receive(:rd_kafka_committed).and_return(20)
418
- list = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
419
- list.add_topic("consume_test_topic", [0, 1, 2])
420
- end
421
- expect {
422
- consumer.committed(list)
423
- }.to raise_error Rdkafka::RdkafkaError
424
- end
425
-
426
- describe "#store_offset" do
427
- before do
428
- config = {}
429
- config[:'enable.auto.offset.store'] = false
430
- config[:'enable.auto.commit'] = false
431
- @new_consumer = rdkafka_consumer_config(config).consumer
432
- @new_consumer.subscribe("consume_test_topic")
433
- wait_for_assignment(@new_consumer)
434
- end
435
-
436
- after do
437
- @new_consumer.close
438
- end
439
-
440
- it "should store the offset for a message" do
441
- @new_consumer.store_offset(message)
442
- @new_consumer.commit
443
-
444
- list = Rdkafka::Consumer::TopicPartitionList.new.tap do |list|
445
- list.add_topic("consume_test_topic", [0, 1, 2])
446
- end
447
- partitions = @new_consumer.committed(list).to_h["consume_test_topic"]
448
- expect(partitions).not_to be_nil
449
- expect(partitions[message.partition].offset).to eq(message.offset + 1)
450
- end
451
-
452
- it "should raise an error with invalid input" do
453
- allow(message).to receive(:partition).and_return(9999)
454
- expect {
455
- @new_consumer.store_offset(message)
456
- }.to raise_error Rdkafka::RdkafkaError
457
- end
458
- end
459
- end
460
- end
461
-
462
- describe "#query_watermark_offsets" do
463
- it "should return the watermark offsets" do
464
- # Make sure there's a message
465
- producer.produce(
466
- topic: "watermarks_test_topic",
467
- payload: "payload 1",
468
- key: "key 1",
469
- partition: 0
470
- ).wait
471
-
472
- low, high = consumer.query_watermark_offsets("watermarks_test_topic", 0, 5000)
473
- expect(low).to eq 0
474
- expect(high).to be > 0
475
- end
476
-
477
- it "should raise an error when querying offsets fails" do
478
- expect(Rdkafka::Bindings).to receive(:rd_kafka_query_watermark_offsets).and_return(20)
479
- expect {
480
- consumer.query_watermark_offsets("consume_test_topic", 0, 5000)
481
- }.to raise_error Rdkafka::RdkafkaError
482
- end
483
- end
484
-
485
- describe "#lag" do
486
- let(:consumer) { rdkafka_consumer_config(:"enable.partition.eof" => true).consumer }
487
-
488
- it "should calculate the consumer lag" do
489
- # Make sure there's a message in every partition and
490
- # wait for the message to make sure everything is committed.
491
- (0..2).each do |i|
492
- producer.produce(
493
- topic: "consume_test_topic",
494
- key: "key lag #{i}",
495
- partition: i
496
- ).wait
497
- end
498
-
499
- # Consume to the end
500
- consumer.subscribe("consume_test_topic")
501
- eof_count = 0
502
- loop do
503
- begin
504
- consumer.poll(100)
505
- rescue Rdkafka::RdkafkaError => error
506
- if error.is_partition_eof?
507
- eof_count += 1
508
- end
509
- break if eof_count == 3
510
- end
511
- end
512
-
513
- # Commit
514
- consumer.commit
515
-
516
- # Create list to fetch lag for. TODO creating the list will not be necessary
517
- # after committed uses the subscription.
518
- list = consumer.committed(Rdkafka::Consumer::TopicPartitionList.new.tap do |l|
519
- l.add_topic("consume_test_topic", (0..2))
520
- end)
521
-
522
- # Lag should be 0 now
523
- lag = consumer.lag(list)
524
- expected_lag = {
525
- "consume_test_topic" => {
526
- 0 => 0,
527
- 1 => 0,
528
- 2 => 0
529
- }
530
- }
531
- expect(lag).to eq(expected_lag)
532
-
533
- # Produce message on every topic again
534
- (0..2).each do |i|
535
- producer.produce(
536
- topic: "consume_test_topic",
537
- key: "key lag #{i}",
538
- partition: i
539
- ).wait
540
- end
541
-
542
- # Lag should be 1 now
543
- lag = consumer.lag(list)
544
- expected_lag = {
545
- "consume_test_topic" => {
546
- 0 => 1,
547
- 1 => 1,
548
- 2 => 1
549
- }
550
- }
551
- expect(lag).to eq(expected_lag)
552
- end
553
-
554
- it "returns nil if there are no messages on the topic" do
555
- list = consumer.committed(Rdkafka::Consumer::TopicPartitionList.new.tap do |l|
556
- l.add_topic("consume_test_topic", (0..2))
557
- end)
558
-
559
- lag = consumer.lag(list)
560
- expected_lag = {
561
- "consume_test_topic" => {}
562
- }
563
- expect(lag).to eq(expected_lag)
564
- end
565
- end
566
-
567
- describe "#cluster_id" do
568
- it 'should return the current ClusterId' do
569
- consumer.subscribe("consume_test_topic")
570
- wait_for_assignment(consumer)
571
- expect(consumer.cluster_id).not_to be_empty
572
- end
573
- end
574
-
575
- describe "#member_id" do
576
- it 'should return the current MemberId' do
577
- consumer.subscribe("consume_test_topic")
578
- wait_for_assignment(consumer)
579
- expect(consumer.member_id).to start_with('rdkafka-')
580
- end
581
- end
582
-
583
- describe "#poll" do
584
- it "should return nil if there is no subscription" do
585
- expect(consumer.poll(1000)).to be_nil
586
- end
587
-
588
- it "should return nil if there are no messages" do
589
- consumer.subscribe("empty_test_topic")
590
- expect(consumer.poll(1000)).to be_nil
591
- end
592
-
593
- it "should return a message if there is one" do
594
- producer.produce(
595
- topic: "consume_test_topic",
596
- payload: "payload 1",
597
- key: "key 1"
598
- ).wait
599
- consumer.subscribe("consume_test_topic")
600
- message = consumer.each {|m| break m}
601
-
602
- expect(message).to be_a Rdkafka::Consumer::Message
603
- expect(message.payload).to eq('payload 1')
604
- expect(message.key).to eq('key 1')
605
- end
606
-
607
- it "should raise an error when polling fails" do
608
- message = Rdkafka::Bindings::Message.new.tap do |message|
609
- message[:err] = 20
610
- end
611
- message_pointer = message.to_ptr
612
- expect(Rdkafka::Bindings).to receive(:rd_kafka_consumer_poll).and_return(message_pointer)
613
- expect(Rdkafka::Bindings).to receive(:rd_kafka_message_destroy).with(message_pointer)
614
- expect {
615
- consumer.poll(100)
616
- }.to raise_error Rdkafka::RdkafkaError
617
- end
618
- end
619
-
620
- describe "#poll with headers" do
621
- it "should return message with headers using string keys (when produced with symbol keys)" do
622
- report = producer.produce(
623
- topic: "consume_test_topic",
624
- key: "key headers",
625
- headers: { foo: 'bar' }
626
- ).wait
627
-
628
- message = wait_for_message(topic: "consume_test_topic", consumer: consumer, delivery_report: report)
629
- expect(message).to be
630
- expect(message.key).to eq('key headers')
631
- expect(message.headers).to include('foo' => 'bar')
632
- end
633
-
634
- it "should return message with headers using string keys (when produced with string keys)" do
635
- report = producer.produce(
636
- topic: "consume_test_topic",
637
- key: "key headers",
638
- headers: { 'foo' => 'bar' }
639
- ).wait
640
-
641
- message = wait_for_message(topic: "consume_test_topic", consumer: consumer, delivery_report: report)
642
- expect(message).to be
643
- expect(message.key).to eq('key headers')
644
- expect(message.headers).to include('foo' => 'bar')
645
- end
646
-
647
- it "should return message with no headers" do
648
- report = producer.produce(
649
- topic: "consume_test_topic",
650
- key: "key no headers",
651
- headers: nil
652
- ).wait
653
-
654
- message = wait_for_message(topic: "consume_test_topic", consumer: consumer, delivery_report: report)
655
- expect(message).to be
656
- expect(message.key).to eq('key no headers')
657
- expect(message.headers).to be_empty
658
- end
659
-
660
- it "should raise an error when message headers aren't readable" do
661
- expect(Rdkafka::Bindings).to receive(:rd_kafka_message_headers).with(any_args) { 1 }
662
-
663
- report = producer.produce(
664
- topic: "consume_test_topic",
665
- key: "key err headers",
666
- headers: nil
667
- ).wait
668
-
669
- expect {
670
- wait_for_message(topic: "consume_test_topic", consumer: consumer, delivery_report: report)
671
- }.to raise_error do |err|
672
- expect(err).to be_instance_of(Rdkafka::RdkafkaError)
673
- expect(err.message).to start_with("Error reading message headers")
674
- end
675
- end
676
-
677
- it "should raise an error when the first message header aren't readable" do
678
- expect(Rdkafka::Bindings).to receive(:rd_kafka_header_get_all).with(any_args) { 1 }
679
-
680
- report = producer.produce(
681
- topic: "consume_test_topic",
682
- key: "key err headers",
683
- headers: { foo: 'bar' }
684
- ).wait
685
-
686
- expect {
687
- wait_for_message(topic: "consume_test_topic", consumer: consumer, delivery_report: report)
688
- }.to raise_error do |err|
689
- expect(err).to be_instance_of(Rdkafka::RdkafkaError)
690
- expect(err.message).to start_with("Error reading a message header at index 0")
691
- end
692
- end
693
- end
694
-
695
- describe "#each" do
696
- it "should yield messages" do
697
- handles = []
698
- 10.times do
699
- handles << producer.produce(
700
- topic: "consume_test_topic",
701
- payload: "payload 1",
702
- key: "key 1",
703
- partition: 0
704
- )
705
- end
706
- handles.each(&:wait)
707
-
708
- consumer.subscribe("consume_test_topic")
709
- # Check the first 10 messages. Then close the consumer, which
710
- # should break the each loop.
711
- consumer.each_with_index do |message, i|
712
- expect(message).to be_a Rdkafka::Consumer::Message
713
- break if i == 10
714
- end
715
- consumer.close
716
- end
717
- end
718
-
719
- describe "#each_batch" do
720
- let(:message_payload) { 'a' * 10 }
721
-
722
- before do
723
- @topic = SecureRandom.base64(10).tr('+=/', '')
724
- end
725
-
726
- after do
727
- @topic = nil
728
- end
729
-
730
- def topic_name
731
- @topic
732
- end
733
-
734
- def produce_n(n)
735
- handles = []
736
- n.times do |i|
737
- handles << producer.produce(
738
- topic: topic_name,
739
- payload: i % 10 == 0 ? nil : Time.new.to_f.to_s,
740
- key: i.to_s,
741
- partition: 0
742
- )
743
- end
744
- handles.each(&:wait)
745
- end
746
-
747
- def new_message
748
- instance_double("Rdkafka::Consumer::Message").tap do |message|
749
- allow(message).to receive(:payload).and_return(message_payload)
750
- end
751
- end
752
-
753
- it "retrieves messages produced into a topic" do
754
- # This is the only each_batch test that actually produces real messages
755
- # into a topic in the real kafka of the container.
756
- #
757
- # The other tests stub 'poll' which makes them faster and more reliable,
758
- # but it makes sense to keep a single test with a fully integrated flow.
759
- # This will help to catch breaking changes in the behavior of 'poll',
760
- # libdrkafka, or Kafka.
761
- #
762
- # This is, in effect, an integration test and the subsequent specs are
763
- # unit tests.
764
- admin = rdkafka_config.admin
765
- create_topic_handle = admin.create_topic(topic_name, 1, 1)
766
- create_topic_handle.wait(max_wait_timeout: 15.0)
767
- consumer.subscribe(topic_name)
768
- produce_n 42
769
- all_yields = []
770
- consumer.each_batch(max_items: 10) do |batch|
771
- all_yields << batch
772
- break if all_yields.flatten.size >= 42
773
- end
774
- expect(all_yields.flatten.first).to be_a Rdkafka::Consumer::Message
775
- expect(all_yields.flatten.size).to eq 42
776
- expect(all_yields.size).to be > 4
777
- expect(all_yields.flatten.map(&:key)).to eq (0..41).map { |x| x.to_s }
778
- admin.close
779
- end
780
-
781
- it "should batch poll results and yield arrays of messages" do
782
- consumer.subscribe(topic_name)
783
- all_yields = []
784
- expect(consumer)
785
- .to receive(:poll)
786
- .exactly(10).times
787
- .and_return(new_message)
788
- consumer.each_batch(max_items: 10) do |batch|
789
- all_yields << batch
790
- break if all_yields.flatten.size >= 10
791
- end
792
- expect(all_yields.first).to be_instance_of(Array)
793
- expect(all_yields.flatten.size).to eq 10
794
- non_empty_yields = all_yields.reject { |batch| batch.empty? }
795
- expect(non_empty_yields.size).to be < 10
796
- end
797
-
798
- it "should yield a partial batch if the timeout is hit with some messages" do
799
- consumer.subscribe(topic_name)
800
- poll_count = 0
801
- expect(consumer)
802
- .to receive(:poll)
803
- .at_least(3).times do
804
- poll_count = poll_count + 1
805
- if poll_count > 2
806
- sleep 0.1
807
- nil
808
- else
809
- new_message
810
- end
811
- end
812
- all_yields = []
813
- consumer.each_batch(max_items: 10) do |batch|
814
- all_yields << batch
815
- break if all_yields.flatten.size >= 2
816
- end
817
- expect(all_yields.flatten.size).to eq 2
818
- end
819
-
820
- it "should yield [] if nothing is received before the timeout" do
821
- admin = rdkafka_config.admin
822
- create_topic_handle = admin.create_topic(topic_name, 1, 1)
823
- create_topic_handle.wait(max_wait_timeout: 15.0)
824
- consumer.subscribe(topic_name)
825
- consumer.each_batch do |batch|
826
- expect(batch).to eq([])
827
- break
828
- end
829
- admin.close
830
- end
831
-
832
- it "should yield batchs of max_items in size if messages are already fetched" do
833
- yielded_batches = []
834
- expect(consumer)
835
- .to receive(:poll)
836
- .with(anything)
837
- .exactly(20).times
838
- .and_return(new_message)
839
-
840
- consumer.each_batch(max_items: 10, timeout_ms: 500) do |batch|
841
- yielded_batches << batch
842
- break if yielded_batches.flatten.size >= 20
843
- break if yielded_batches.size >= 20 # so failure doesn't hang
844
- end
845
- expect(yielded_batches.size).to eq 2
846
- expect(yielded_batches.map(&:size)).to eq 2.times.map { 10 }
847
- end
848
-
849
- it "should yield batchs as soon as bytes_threshold is hit" do
850
- yielded_batches = []
851
- expect(consumer)
852
- .to receive(:poll)
853
- .with(anything)
854
- .exactly(20).times
855
- .and_return(new_message)
856
-
857
- consumer.each_batch(bytes_threshold: message_payload.size * 4, timeout_ms: 500) do |batch|
858
- yielded_batches << batch
859
- break if yielded_batches.flatten.size >= 20
860
- break if yielded_batches.size >= 20 # so failure doesn't hang
861
- end
862
- expect(yielded_batches.size).to eq 5
863
- expect(yielded_batches.map(&:size)).to eq 5.times.map { 4 }
864
- end
865
-
866
- context "error raised from poll and yield_on_error is true" do
867
- it "should yield buffered exceptions on rebalance, then break" do
868
- config = rdkafka_consumer_config(
869
- {
870
- :"enable.auto.commit" => false,
871
- :"enable.auto.offset.store" => false
872
- }
873
- )
874
- consumer = config.consumer
875
- consumer.subscribe(topic_name)
876
- batches_yielded = []
877
- exceptions_yielded = []
878
- each_batch_iterations = 0
879
- poll_count = 0
880
- expect(consumer)
881
- .to receive(:poll)
882
- .with(anything)
883
- .exactly(3).times
884
- .and_wrap_original do |method, *args|
885
- poll_count = poll_count + 1
886
- if poll_count == 3
887
- raise Rdkafka::RdkafkaError.new(27,
888
- "partitions ... too ... heavy ... must ... rebalance")
889
- else
890
- new_message
891
- end
892
- end
893
- expect {
894
- consumer.each_batch(max_items: 30, yield_on_error: true) do |batch, pending_error|
895
- batches_yielded << batch
896
- exceptions_yielded << pending_error
897
- each_batch_iterations = each_batch_iterations + 1
898
- end
899
- }.to raise_error(Rdkafka::RdkafkaError)
900
- expect(poll_count).to eq 3
901
- expect(each_batch_iterations).to eq 1
902
- expect(batches_yielded.size).to eq 1
903
- expect(batches_yielded.first.size).to eq 2
904
- expect(exceptions_yielded.flatten.size).to eq 1
905
- expect(exceptions_yielded.flatten.first).to be_instance_of(Rdkafka::RdkafkaError)
906
- consumer.close
907
- end
908
- end
909
-
910
- context "error raised from poll and yield_on_error is false" do
911
- it "should yield buffered exceptions on rebalance, then break" do
912
- config = rdkafka_consumer_config(
913
- {
914
- :"enable.auto.commit" => false,
915
- :"enable.auto.offset.store" => false
916
- }
917
- )
918
- consumer = config.consumer
919
- consumer.subscribe(topic_name)
920
- batches_yielded = []
921
- exceptions_yielded = []
922
- each_batch_iterations = 0
923
- poll_count = 0
924
- expect(consumer)
925
- .to receive(:poll)
926
- .with(anything)
927
- .exactly(3).times
928
- .and_wrap_original do |method, *args|
929
- poll_count = poll_count + 1
930
- if poll_count == 3
931
- raise Rdkafka::RdkafkaError.new(27,
932
- "partitions ... too ... heavy ... must ... rebalance")
933
- else
934
- new_message
935
- end
936
- end
937
- expect {
938
- consumer.each_batch(max_items: 30, yield_on_error: false) do |batch, pending_error|
939
- batches_yielded << batch
940
- exceptions_yielded << pending_error
941
- each_batch_iterations = each_batch_iterations + 1
942
- end
943
- }.to raise_error(Rdkafka::RdkafkaError)
944
- expect(poll_count).to eq 3
945
- expect(each_batch_iterations).to eq 0
946
- expect(batches_yielded.size).to eq 0
947
- expect(exceptions_yielded.size).to eq 0
948
- consumer.close
949
- end
950
- end
951
- end
952
-
953
- describe "a rebalance listener" do
954
- let(:consumer) do
955
- config = rdkafka_consumer_config
956
- config.consumer_rebalance_listener = listener
957
- config.consumer
958
- end
959
-
960
- context "with a working listener" do
961
- let(:listener) do
962
- Struct.new(:queue) do
963
- def on_partitions_assigned(list)
964
- collect(:assign, list)
965
- end
966
-
967
- def on_partitions_revoked(list)
968
- collect(:revoke, list)
969
- end
970
-
971
- def collect(name, list)
972
- partitions = list.to_h.map { |key, values| [key, values.map(&:partition)] }.flatten
973
- queue << ([name] + partitions)
974
- end
975
- end.new([])
976
- end
977
-
978
- it "should get notifications" do
979
- notify_listener(listener)
980
-
981
- expect(listener.queue).to eq([
982
- [:assign, "consume_test_topic", 0, 1, 2],
983
- [:revoke, "consume_test_topic", 0, 1, 2]
984
- ])
985
- end
986
- end
987
-
988
- context "with a broken listener" do
989
- let(:listener) do
990
- Struct.new(:queue) do
991
- def on_partitions_assigned(list)
992
- queue << :assigned
993
- raise 'boom'
994
- end
995
-
996
- def on_partitions_revoked(list)
997
- queue << :revoked
998
- raise 'boom'
999
- end
1000
- end.new([])
1001
- end
1002
-
1003
- it 'should handle callback exceptions' do
1004
- notify_listener(listener)
1005
-
1006
- expect(listener.queue).to eq([:assigned, :revoked])
1007
- end
1008
- end
1009
- end
1010
-
1011
- context "methods that should not be called after a consumer has been closed" do
1012
- before do
1013
- consumer.close
1014
- end
1015
-
1016
- # Affected methods and a non-invalid set of parameters for the method
1017
- {
1018
- :subscribe => [ nil ],
1019
- :unsubscribe => nil,
1020
- :each_batch => nil,
1021
- :pause => [ nil ],
1022
- :resume => [ nil ],
1023
- :subscription => nil,
1024
- :assign => [ nil ],
1025
- :assignment => nil,
1026
- :committed => [],
1027
- :query_watermark_offsets => [ nil, nil ],
1028
- }.each do |method, args|
1029
- it "raises an exception if #{method} is called" do
1030
- expect {
1031
- if args.nil?
1032
- consumer.public_send(method)
1033
- else
1034
- consumer.public_send(method, *args)
1035
- end
1036
- }.to raise_exception(Rdkafka::ClosedConsumerError, /#{method.to_s}/)
1037
- end
1038
- end
1039
- end
1040
-
1041
- it "provides a finalizer that closes the native kafka client" do
1042
- expect(consumer.closed?).to eq(false)
1043
-
1044
- consumer.finalizer.call("some-ignored-object-id")
1045
-
1046
- expect(consumer.closed?).to eq(true)
1047
- end
1048
-
1049
- context "when the rebalance protocol is cooperative" do
1050
- let(:consumer) do
1051
- config = rdkafka_consumer_config(
1052
- {
1053
- :"partition.assignment.strategy" => "cooperative-sticky",
1054
- :"debug" => "consumer",
1055
- }
1056
- )
1057
- config.consumer_rebalance_listener = listener
1058
- config.consumer
1059
- end
1060
-
1061
- let(:listener) do
1062
- Struct.new(:queue) do
1063
- def on_partitions_assigned(list)
1064
- collect(:assign, list)
1065
- end
1066
-
1067
- def on_partitions_revoked(list)
1068
- collect(:revoke, list)
1069
- end
1070
-
1071
- def collect(name, list)
1072
- partitions = list.to_h.map { |key, values| [key, values.map(&:partition)] }.flatten
1073
- queue << ([name] + partitions)
1074
- end
1075
- end.new([])
1076
- end
1077
-
1078
- it "should be able to assign and unassign partitions using the cooperative partition assignment APIs" do
1079
- notify_listener(listener) do
1080
- handles = []
1081
- 10.times do
1082
- handles << producer.produce(
1083
- topic: "consume_test_topic",
1084
- payload: "payload 1",
1085
- key: "key 1",
1086
- partition: 0
1087
- )
1088
- end
1089
- handles.each(&:wait)
1090
-
1091
- consumer.subscribe("consume_test_topic")
1092
- # Check the first 10 messages. Then close the consumer, which
1093
- # should break the each loop.
1094
- consumer.each_with_index do |message, i|
1095
- expect(message).to be_a Rdkafka::Consumer::Message
1096
- break if i == 10
1097
- end
1098
- end
1099
-
1100
- expect(listener.queue).to eq([
1101
- [:assign, "consume_test_topic", 0, 1, 2],
1102
- [:revoke, "consume_test_topic", 0, 1, 2]
1103
- ])
1104
- end
1105
- end
1106
- end