-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathwolff_supervised_tests.erl
More file actions
826 lines (778 loc) · 34.4 KB
/
Copy pathwolff_supervised_tests.erl
File metadata and controls
826 lines (778 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
-module(wolff_supervised_tests).
-include("wolff.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("kafka_protocol/include/kpro.hrl").
-define(KEY, key(?FUNCTION_NAME)).
-define(HOSTS, [{"localhost", 9092}]).
supervised_client_test() ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"supervised-wolff-client">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = client_config(),
{ok, Client} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
%% start it again should result in the same client pid
?assertEqual({ok, Client},
wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg)),
ProducerCfg0 = producer_config(?FUNCTION_NAME),
ProducerCfg = ProducerCfg0#{required_acks => leader_only},
{ok, Producers} = wolff:start_producers(Client, <<"test-topic">>, ProducerCfg),
Msg = #{key => ?KEY, value => <<"value">>},
{Partition, BaseOffset} = wolff:send_sync(Producers, [Msg], 3000),
io:format(user, "\nmessage produced to partition ~p at offset ~p\n",
[Partition, BaseOffset]),
ok = wolff:stop_producers(Producers),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
?assertEqual(undefined, whereis(wolff_sup)),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
[1] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok.
supervised_producers_test_() ->
[{"atom-name", fun() -> test_supervised_producers(test_producers) end},
{"binary-name", fun() -> test_supervised_producers(<<"test-producers">>) end}
].
test_supervised_producers(Name) ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"supervised-producers">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = client_config(),
{ok, _ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg0 = producer_config(Name),
ProducerCfg = ProducerCfg0#{required_acks => all_isr},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg),
?assertEqual({ok, Producers}, wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg)),
Msg = #{key => ?KEY, value => <<"value">>},
Self = self(),
AckFun = fun(_Partition, _BaseOffset) -> Self ! acked, ok end,
{_Partition, _ProducerPid} = wolff:send(Producers, [Msg], AckFun),
receive acked -> ok end,
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
[1] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok.
%% Checks that having different producers to the same kafka topic works.
different_producers_same_topic_test_() ->
{timeout, 30, fun test_different_producers_same_topic/0}.
test_different_producers_same_topic() ->
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientId = <<"same-topic">>,
ClientCfg = client_config(),
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
Topic = <<"test-topic">>,
ProducerName0 = <<"p0">>,
Group0 = <<"a0">>,
ProducerCfg0 = (producer_config(ProducerName0))#{required_acks => all_isr, group => Group0},
{ok, Producers0} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg0),
?assertEqual({ok, Producers0},
wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg0)),
ProducerName1 = <<"p1">>,
Group1 = <<"a1">>,
ProducerCfg1 = (producer_config(ProducerName1))#{required_acks => all_isr, group => Group1},
{ok, Producers1} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg1),
?assertEqual({ok, Producers0},
wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg0)),
%% We can send from each producer.
Msg = #{key => ?KEY, value => <<"value">>},
Self = self(),
AckFun = fun(_Partition, _BaseOffset) -> Self ! acked, ok end,
{_Partition0, _ProducerPid0} = wolff:send(Producers0, [Msg], AckFun),
receive acked -> ok end,
{_Partition1A, _ProducerPid1A} = wolff:send(Producers1, [Msg], AckFun),
receive acked -> ok end,
%% Each replayq dir should be namespaced by the group
#{replayq_dir := BaseDir} = ProducerCfg0,
{ok, Files} = file:list_dir(BaseDir),
Dir0 = <<Group0/binary, $_, Topic/binary>>,
Dir1 = <<Group1/binary, $_, Topic/binary>>,
ReplayQDirs = [File || File <- Files,
filelib:is_dir(filename:join([BaseDir, File])),
lists:member(list_to_binary(File), [Dir0, Dir1])],
?assertMatch([_, _], ReplayQDirs, #{base_dir => Files}),
?assertMatch(#{metadata_ts := #{Topic := _},
known_topics := #{Topic := #{Group0 := true, Group1 := true}},
conns := #{{Topic, 0} := _}
}, sys:get_state(ClientPid)),
%% We now stop one of the producers. The other should keep working.
ok = wolff:stop_and_delete_supervised_producers(Producers0),
?assertMatch(#{metadata_ts := #{Topic := _},
known_topics := #{Topic := #{Group1 := true}},
conns := #{{Topic, 0} := _}
}, sys:get_state(ClientPid)),
?assertMatch(#{}, sys:get_state(ClientPid)),
{_Partition1B, _ProducerPid1B} = wolff:send(Producers1, [Msg], AckFun),
receive acked -> ok end,
ok = wolff:stop_and_delete_supervised_producers(Producers1),
EmptyMap = #{},
?assertMatch(#{metadata_ts := EmptyMap,
known_topics := EmptyMap,
conns := EmptyMap
},
sys:get_state(ClientPid)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
ok = application:stop(wolff),
ok.
client_restart_test() ->
ClientId = <<"client-restart-test">>,
Topic = <<"test-topic">>,
Partition = 0,
test_client_restart(ClientId, Topic, Partition).
%% test-topic-2 should have 2 partitions
client_restart_2_test() ->
ClientId = <<"client-restart-test-2">>,
Topic = <<"test-topic-2">>,
Partition = 0,
test_client_restart(ClientId, Topic, Partition).
test_client_restart(ClientId, Topic, Partition) ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{connection_strategy => per_broker},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg = #{replayq_dir => "test-data/client-restart-test",
required_acks => all_isr,
partitioner => Partition, %% always send to the same partition
partition_count_refresh_interval_seconds => 0,
name => ?FUNCTION_NAME
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
Msg1 = #{key => ?KEY, value => <<"1">>},
{_, Offset1} = wolff:send_sync(Producers, [Msg1], 5000),
erlang:exit(ClientPid, kill),
timer:sleep(5),
Msg2 = #{key => ?KEY, value => <<"2">>},
{_, _Offset2} = wolff:send_sync(Producers, [Msg2], 5000),
{ok, NewClientPid} = wolff_client_sup:find_client(ClientId),
ok = fetch_and_match(NewClientPid, Topic, Partition, Offset1, [Msg1, Msg2]),
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
[1,1] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok.
max_partitions_test() ->
ClientId = <<"max-partitions-test">>,
Topic = <<"test-topic-2">>,
Partition = 0,
MaxPartitions = 1,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{connection_strategy => per_partition},
{ok, _ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg = #{required_acks => all_isr,
partitioner => Partition,
max_partitions => MaxPartitions
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
%% the topic has two partitions, but limited only to started one producer
?assertMatch([_], wolff_producers:find_producers_by_client_topic(ClientId, ?NO_GROUP, Topic)),
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
ok.
%% Test against a bad host.
%% No connection will be established at all.
%% Producer workers should not crash, async APIs should work.
bad_host_test() ->
ClientId = <<"bad-host-test">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
{ok, _} = wolff:ensure_supervised_client(ClientId, [{"badhost", 9092}], #{}),
?assertMatch({error, _}, wolff:ensure_supervised_producers(ClientId, <<"t">>, #{name => ?FUNCTION_NAME})),
ok = wolff:stop_and_delete_supervised_client(ClientId).
producer_restart_test() ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"producer-restart">>,
Topic = <<"test-topic">>,
Partition = 0,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg = #{replayq_dir => "test-data/producer-restart-test",
required_acks => all_isr,
partitioner => Partition,
reconnect_delay_ms => 0,
name => ?FUNCTION_NAME
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
GetPid = fun() -> {ok, Pid} = wolff_producers:find_producer_by_partition(ClientId, ?NO_GROUP, Topic, Partition), Pid end,
Producer0 = GetPid(),
Msg0 = #{key => ?KEY, value => <<"0">>},
{0, Offset0} = wolff_producer:send_sync(Producer0, [Msg0], 2000),
%% the commit towards replayq is async, wait for it
timer:sleep(5),
DummyAckFun = fun(_, _) -> ok end,
Msg1 = #{key => ?KEY, value => <<"1">>},
{Partition, Producer0} = wolff:send(Producers, [Msg1], DummyAckFun),
erlang:exit(Producer0, kill),
%% let wolff_producers to pick up the EXIT signal and mark it down in ets
timer:sleep(10),
_Producer1 = wait_for_pid(GetPid),
Msg2 = #{key => ?KEY, value => <<"2">>},
_ = wolff:send_sync(Producers, [Msg2], 4000),
ok = fetch_and_match(ClientPid, Topic, Partition, Offset0, [Msg0, Msg1, Msg2]),
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
[1,2] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok.
stop_with_name_test() ->
ClientId = <<"stop-with-name">>,
Topic = <<"test-topic">>,
Partition = 0,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{},
{ok, _} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
Name = ?FUNCTION_NAME,
ProducerCfg = #{required_acks => all_isr,
partitioner => Partition,
reconnect_delay_ms => 0,
name => Name
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
ok.
partition_count_increase_test_() ->
{timeout, 30, %% it takes time to alter topic via cli in docker container
fun test_partition_count_increase/0}.
test_partition_count_increase() ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"test-add-more-partitions">>,
Topic = <<"test-topic-3">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
{ok, Connections0} = get_leader_connections(ClientPid, Topic),
Partitions0 = length(Connections0),
?assertEqual(Partitions0, count_partitions(Topic)),
%% always send to the last partition
Partitioner = fun(Count, _) -> Count - 1 end,
Name = ?FUNCTION_NAME,
IntervalSeconds = 1,
ProducerCfg = #{required_acks => all_isr,
partitioner => Partitioner,
reconnect_delay_ms => 0,
name => Name,
partition_count_refresh_interval_seconds => IntervalSeconds
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
Msg = #{key => ?KEY, value => <<"value">>},
{Partition0, Offset0} = wolff:send_sync(Producers, [Msg], 3000),
?assert(is_integer(Offset0), Offset0),
?assertEqual(Partitions0 - 1, Partition0),
%% create a new partition
ensure_partitions(Topic, Partitions0 + 1),
%% wait for the new partition to be discovered
timer:sleep(timer:seconds(IntervalSeconds * 2)),
{Partition1, Offset1} = wolff:send_sync(Producers, [Msg], 3000),
?assert(is_integer(Offset1), Offset1),
?assertEqual(Partitions0, Partition1),
[1,1] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok.
partition_count_decrease_test_() ->
{timeout, 30, %% it takes time to alter topic via cli in docker container
fun test_partition_count_decrease/0}.
test_partition_count_decrease() ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"recreate-topic-with-fewer-partitions">>,
Topic = <<"test-topic-fewer-partition">>,
Partitions0 = 3,
delete_topic(Topic),
create_topic(Topic, Partitions0),
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
{ok, Connections0} = get_leader_connections(ClientPid, Topic),
?assertEqual(Partitions0, length(Connections0)),
?assertEqual(Partitions0, count_partitions(Topic)),
%% always send to the last partition
Partitioner = fun(Count, _) -> Count - 1 end,
Name = ?FUNCTION_NAME,
IntervalSeconds = 1,
ProducerCfg = #{required_acks => all_isr,
partitioner => Partitioner,
reconnect_delay_ms => 0,
name => Name,
partition_count_refresh_interval_seconds => IntervalSeconds
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
Msg = #{key => ?KEY, value => <<"value">>},
{Partition0, _} = wolff:send_sync(Producers, [Msg], 3000),
?assertEqual(Partitions0 - 1, Partition0),
Self = self(),
AsyncSends = lists:seq(1, 10),
%% delete the topic
delete_topic(Topic),
AckFn = fun(I) -> fun(Partition, OffsetReply) -> Self ! {async_ack, I, Partition, OffsetReply}, ok end end,
lists:foreach(fun(I) -> wolff:send(Producers, [Msg], AckFn(I)) end, AsyncSends),
%% wait for the topic to be deleted
create_topic(Topic, Partitions0 - 1),
%% wait for partition_lost replies
lists:foreach(
fun(I) ->
receive
{async_ack, AckI, P, O} ->
?assertEqual(Partitions0 - 1, P),
?assertEqual(O, partition_lost),
?assertEqual(I, AckI)
after
10_000 ->
error(timeout)
end
end, AsyncSends),
{ok, Connections1} = get_leader_connections(ClientPid, Topic),
?assertEqual(Partitions0 - 1, length(Connections1)),
?assertEqual(Partitions0 - 1, count_partitions(Topic)),
?assertEqual(Partitions0 - 1, wolff_producers:get_partition_cnt(ClientId, ?NO_GROUP, Topic)),
{Partition1, _} = wolff:send_sync(Producers, [Msg], 3000),
?assertEqual(Partition0 - 1, Partition1),
[1,1] = get_telemetry_seq(CntrEventsTable, [wolff,success]),
%% 10 messages, 1 failed and retried, 9 in the backlog got discarded
[1,9] = get_telemetry_seq(CntrEventsTable, [wolff,failed]),
assert_last_event_is_zero(queuing, CntrEventsTable),
assert_last_event_is_zero(queuing_bytes, CntrEventsTable),
assert_last_event_is_zero(inflight, CntrEventsTable),
ets:delete(CntrEventsTable),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
ok.
%% The last partition is temporarily missing from metadata response.
%% The producer should retry to recover
last_partition_missing_in_metadata_response_test_() ->
{timeout, 30, %% it takes time to alter topic via cli in docker container
fun() -> test_partition_missing_in_metadata_response(2) end}.
%% The partition in the middle is temporarily missing from metadata response.
%% The producer should retry to recover
mid_partition_missing_in_metadata_response_test_() ->
{timeout, 30, %% it takes time to alter topic via cli in docker container
fun() -> test_partition_missing_in_metadata_response(1) end}.
test_partition_missing_in_metadata_response(ThePartition) ->
ClientId = <<"test-temporarily-malformed-metadata-response">>,
%% ensure the topic does not exist
Topic = <<"test-topic-5">>,
delete_topic(Topic),
Partitions = 3,
create_topic(Topic, Partitions),
io:format(user, "created topic ~s with ~p partitions\n", [Topic, Partitions]),
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
%% always refresh metadata
ClientCfg = #{min_metadata_refresh_interval => 0},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
{ok, Connections} = get_leader_connections(ClientPid, Topic),
?assertEqual(Partitions, length(Connections)),
%% always send it to the partition which is going to be missing
Partitioner = fun(_, _) -> ThePartition end,
ProducerCfg = #{required_acks => all_isr,
partitioner => Partitioner,
reconnect_delay_ms => 0,
%% 0 means no aut-refresh
partition_count_refresh_interval_seconds => 0
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
?assertEqual(Partitions, wolff_producers:get_partition_cnt(ClientId, ?NO_GROUP, Topic)),
Msg = #{key => ?KEY, value => <<"value">>},
?assertMatch({ThePartition, _}, wolff:send_sync(Producers, [Msg], 3000)),
%% mock a bad
%% Kill partition leader connection
Pid = get_partition_leader_connection(ClientPid, Topic, ThePartition),
exit(Pid, kill),
%% mock kafka_protcol to return metadata with ThePartition missing
meck:new(kpro, [passthrough, no_history]),
meck:expect(kpro, request_sync,
fun(Connection, Req, Timeout) ->
{ok, Rsp} = meck:passthrough([Connection, Req, Timeout]),
case Rsp of
#kpro_rsp{msg = #{topics := [#{partitions := PM0} = TopicMeta]} = Meta} ->
PM = lists:filter(fun(#{partition_index := P}) -> P =/= ThePartition end, PM0),
NewRsp = Rsp#kpro_rsp{msg =Meta#{topics := [TopicMeta#{partitions := PM}]}},
{ok, NewRsp};
_ ->
{ok, Rsp}
end
end),
?assertNot(is_pid(get_partition_leader_connection(ClientPid, Topic, ThePartition))),
%% the request will be buffered, but the call times out
Msg2 = #{key => ?KEY, value => <<"v">>},
?assertError(timeout, wolff:send_sync(Producers, [Msg2], 100)),
%% Wait for auto recover
Tester = self(),
meck:expect(kpro, send,
fun(Conn, Req) ->
Tester ! {sent, Conn},
meck:passthrough([Conn, Req])
end),
%% Now remove the injected error (malformed metadata response)
%% wolff_producer should now be able to recover
meck:expect(kpro, request_sync,
fun(Connection, Req, Timeout) ->
meck:passthrough([Connection, Req, Timeout])
end),
receive
{sent, NewConn} ->
?assertEqual(NewConn, get_partition_leader_connection(ClientPid, Topic, ThePartition))
after
2000 ->
%% wolff_producer retry delay is 0, but it randomize with extra 0-1000ms
error(timeout)
end,
meck:unload(kpro),
ok = fetch_and_match(ClientPid, Topic, ThePartition, 0, [Msg, Msg2]),
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
ok.
get_partition_leader_connection(Client, Topic, Partition) ->
ok = wolff_client:recv_leader_connection(Client, ?NO_GROUP, Topic, Partition, self(), all_partitions),
receive
{leader_connection, Pid} ->
Pid
after
20000 ->
error(timeout)
end.
topic_recreate_test_() ->
{timeout, 30, %% it takes time to alter topic via cli in docker container
fun test_topic_recreate/0}.
test_topic_recreate() ->
ClientId = <<"test-topic-recreate">>,
Topic = <<"test-topic-recreate">>,
%% ensure the topic does not exist
delete_topic(Topic),
Partitions0 = 3,
create_topic(Topic, Partitions0),
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{min_metadata_refresh_interval => 0},
{ok, ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
{ok, Connections0} = get_leader_connections(ClientPid, Topic),
?assertEqual(Partitions0, length(Connections0)),
Partitioner = fun(_Count, _Key) -> 0 end,
IntervalSeconds = 1,
Name = ?FUNCTION_NAME,
ProducerCfg = #{required_acks => all_isr,
partitioner => Partitioner,
reconnect_delay_ms => 0,
name => Name,
partition_count_refresh_interval_seconds => IntervalSeconds
},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, Topic, ProducerCfg),
?assertEqual(Partitions0, wolff_producers:get_partition_cnt(ClientId, ?NO_GROUP, Topic)),
Msg = #{key => ?KEY, value => <<"value">>},
?assertEqual({0, 0}, wolff:send_sync(Producers, [Msg], 3000)),
%% delete the topic
delete_topic(Topic),
Self = self(),
%% Send a message while the topic is deleted
AckFn = fun(Partition, OffsetReply) -> Self ! {async_ack, Partition, OffsetReply}, ok end,
{0, _} = wolff:send(Producers, [Msg], AckFn),
%% wait for the topic to be deleted
create_topic(Topic, Partitions0 - 1),
%% wait for the topic to be recreated
timer:sleep(timer:seconds(IntervalSeconds * 4)),
{ok, Connections1} = get_leader_connections(ClientPid, Topic),
?assertEqual(Partitions0 - 1, length(Connections1)),
%% ensure the producers are stopped
?assertEqual(Partitions0 - 1, wolff_producers:get_partition_cnt(ClientId, ?NO_GROUP, Topic)),
?assertEqual({0, 1}, wolff:send_sync(Producers, [Msg], 3000)),
receive
{async_ack, P, O} ->
?assertEqual(0, P),
?assertEqual(0, O)
after
1000 ->
error(timeout)
end,
%% cleanup
ok = wolff:stop_and_delete_supervised_producers(Producers),
?assertEqual([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
?assertEqual([], supervisor:which_children(wolff_client_sup)),
ok = application:stop(wolff),
ok.
non_existing_topic_test() ->
ClientId = atom_to_binary(?FUNCTION_NAME),
Topic = <<"non-existing-topic">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = #{},
{ok, _ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
?assertMatch({error, _}, wolff:ensure_supervised_producers(ClientId, Topic, #{name => ?FUNCTION_NAME})),
?assertMatch([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
ok.
start_producers_with_dead_client_test() ->
ClientId = atom_to_binary(?FUNCTION_NAME),
Topic = <<"non-existing-topic">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
?assertMatch({error, _}, wolff:ensure_supervised_producers(<<"never-started">>, Topic, #{name => ?FUNCTION_NAME})),
?assertMatch([], supervisor:which_children(wolff_producers_sup)),
ok = wolff:stop_and_delete_supervised_client(ClientId),
ok.
fail_retry_success_test() ->
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"supervised-producers">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = client_config(),
{ok, _ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg0 = producer_config(?FUNCTION_NAME),
ProducerCfg = ProducerCfg0#{required_acks => all_isr, reconnect_delay_ms => 10},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg),
Msg = #{key => ?KEY, value => <<"value">>},
Self = self(),
AckFun = fun(_Partition, _BaseOffset) -> Self ! acked, ok end,
try
telemetry:attach_many(tap, wolff_tests:telemetry_events(),
fun(EventId, MetricsData, Metadata, state) ->
Self ! {telemetry, EventId, MetricsData, Metadata},
ok
end,
state),
with_meck(kpro, find,
fun(error_code, _Resp) ->
?unknown_server_error;
(Code, Resp) ->
meck:passthrough([Code, Resp])
end,
fun() ->
{_Partition, _ProducerPid} = wolff:send(Producers, [Msg], AckFun),
{ok, _} = wait_telemetry_event([wolff, failed]),
ok
end),
{ok, _} = wait_telemetry_event([wolff, retried_success]),
[1] = get_telemetry_seq(CntrEventsTable, [wolff, failed]),
[1] = get_telemetry_seq(CntrEventsTable, [wolff, retried]),
[1] = get_telemetry_seq(CntrEventsTable, [wolff, retried_success]),
ok
after
ok = wolff:stop_and_delete_supervised_producers(Producers),
ok = wolff:stop_and_delete_supervised_client(ClientId),
ok = application:stop(wolff),
telemetry:detach(tap),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ets:delete(CntrEventsTable),
ok
end,
ok.
fail_retry_failed_test() ->
{timeout, 60000,
begin
CntrEventsTable = ets:new(cntr_events, [public]),
wolff_tests:install_event_logging(?FUNCTION_NAME, CntrEventsTable, false),
ClientId = <<"supervised-producers">>,
_ = application:stop(wolff), %% ensure stopped
{ok, _} = application:ensure_all_started(wolff),
ClientCfg = client_config(),
{ok, _ClientPid} = wolff:ensure_supervised_client(ClientId, ?HOSTS, ClientCfg),
ProducerCfg0 = producer_config(?FUNCTION_NAME),
ProducerCfg = ProducerCfg0#{required_acks => all_isr},
{ok, Producers} = wolff:ensure_supervised_producers(ClientId, <<"test-topic">>, ProducerCfg),
Msg = #{key => ?KEY, value => <<"value">>},
Self = self(),
AckFun = fun(_Partition, _BaseOffset) -> Self ! acked, ok end,
try
telemetry:attach_many(tap, wolff_tests:telemetry_events(),
fun(EventId, MetricsData, Metadata, state) ->
Self ! {telemetry, EventId, MetricsData, Metadata},
ok
end,
state),
with_meck(kpro, find,
fun(error_code, #{base_offset := _} = _Resp) ->
?unknown_server_error;
(Code, Resp) ->
meck:passthrough([Code, Resp])
end,
fun() ->
{_Partition, ProducerPid} = wolff:send(Producers, [Msg], AckFun),
{ok, _} = wait_telemetry_event([wolff, failed], #{timeout => 5_000}),
%% simulate a disconnection
ProducerPid ! {leader_connection, {down, test}},
{ok, _} = wait_telemetry_event([wolff, retried_failed], #{timeout => 30_000}),
ok
end),
{ok, _} = wait_telemetry_event([wolff, retried_success]),
[1 | _] = get_telemetry_seq(CntrEventsTable, [wolff, failed]),
[1 | _] = wolff_tests:get_telemetry_seq(CntrEventsTable, [wolff, retried]),
[1 | _] = wolff_tests:get_telemetry_seq(CntrEventsTable, [wolff, retried_failed]),
[1] = wolff_tests:get_telemetry_seq(CntrEventsTable, [wolff, retried_success]),
ok
after
ok = wolff:stop_and_delete_supervised_producers(Producers),
ok = wolff:stop_and_delete_supervised_client(ClientId),
ok = application:stop(wolff),
telemetry:detach(tap),
wolff_tests:deinstall_event_logging(?FUNCTION_NAME),
ets:delete(CntrEventsTable),
ok
end,
ok
end}.
%% helpers
with_meck(Mod, FnName, MockedFn, Action) ->
ok = meck:new(Mod, [non_strict, no_history, no_link, passthrough]),
ok = meck:expect(Mod, FnName, MockedFn),
try
Action()
after
meck:unload(Mod)
end.
wait_telemetry_event(EventName) ->
wait_telemetry_event(EventName, #{timeout => 10_000}).
wait_telemetry_event(EventName, Opts) ->
Timeout = maps:get(timeout, Opts, 10_000),
receive
{telemetry, EventName, MetricsData, Metadata} ->
{ok, {EventName, MetricsData, Metadata}}
after
Timeout ->
{timeout, EventName}
end.
wait_for_pid(F) ->
Pid = F(),
case is_pid(Pid) of
true -> Pid;
false -> timer:sleep(100), wait_for_pid(F)
end.
fetch_and_match(ClientPid, Topic, Partition, Offset, ExpectedMsgs) ->
{ok, Conns} = get_leader_connections(ClientPid, Topic),
{_, Conn} = lists:keyfind(Partition, 1, Conns),
Msgs0 = fetch(Conn, Topic, Partition, Offset, 1000),
Msgs = dedup(Msgs0, undefined),
match_messages(ExpectedMsgs, Msgs).
match_messages([], []) -> ok;
match_messages([], Received) -> erlang:throw({<<"received more than expected">>, Received});
match_messages(Expected, []) -> erlang:throw({<<"expected not received">>, Expected});
match_messages([E | TE], [R | TR]) ->
ok = match_message(E, R),
match_messages(TE, TR).
match_message(#{key := Key}, #kafka_message{key = Key}) -> ok;
match_message(#{key := Key1}, #kafka_message{key = Key2}) -> erlang:throw({Key1, Key2}).
dedup([], _) -> [];
dedup([#kafka_message{value = Value} | Rest], Value) ->
dedup(Rest, Value);
dedup([#kafka_message{value = V} = Msg | Rest], _LastV) ->
[Msg | dedup(Rest, V)].
fetch(Connection, Topic, Partition, Offset, MaxBytes) ->
Opts = #{ max_wait_time => 500
, max_bytes => MaxBytes
},
{ok, {_, Vsn}} = kpro:get_api_vsn_range(Connection, fetch),
Req = kpro_req_lib:fetch(Vsn, Topic, Partition, Offset, Opts),
{ok, #kpro_rsp{msg = Rsp}} = kpro:request_sync(Connection, Req, 5000),
[TopicRsp] = kpro:find(responses, Rsp),
[PartitionRsp] = kpro:find(partition_responses, TopicRsp),
Records = kpro:find(record_set, PartitionRsp),
Batches = kpro:decode_batches(Records),
case Batches of
?incomplete_batch(Size) ->
fetch(Connection, Topic, Partition, Offset, Size);
_ ->
lists:append([Msgs || {_Meta, Msgs} <- Batches])
end.
client_config() -> #{}.
producer_config(Name) ->
#{replayq_dir => "test-data",
name => Name
}.
key(Name) ->
iolist_to_binary(io_lib:format("~0p/~0p/~0p", [Name, calendar:local_time(), erlang:system_time()])).
count_partitions(Topic) ->
Cmd = kafka_topic_cmd_base(Topic) ++ " --describe | grep Configs: | awk -F'PartitionCount[: ]*' '{print $2}' | awk '{print $1}'",
Result = os:cmd(Cmd),
list_to_integer(string:strip(Result, right, $\n)).
ensure_partitions(Topic, Partitions) ->
Cmd = kafka_topic_cmd_base(Topic) ++ " --alter --partitions " ++ integer_to_list(Partitions),
Result = os:cmd(Cmd),
Pattern = "Adding partitions succeeded!",
?assert(Result =:= [] orelse string:str(Result, Pattern) > 0, Result),
ok.
kafka_topic_cmd_base(Topic) ->
wolff_test_utils:topics_cmd_base(Topic).
get_telemetry_seq(Table, Event) ->
wolff_tests:get_telemetry_seq(Table, Event).
assert_last_event_is_zero(Key, Tab) ->
case get_telemetry_seq(Tab, [wolff, Key]) of
[] ->
throw(no_event_recorded);
L ->
case lists:last(L) of
0 ->
ok;
X ->
throw({unexpected_eventual_state, X})
end
end.
get_leader_connections(Client, Topic) ->
wolff_client:get_leader_connections(Client, ?NO_GROUP, Topic).
create_topic(Topic, Partitions) ->
Cmd = kafka_topic_cmd_base(Topic) ++ " --create --replication-factor 1 --partitions " ++ integer_to_list(Partitions),
Result = os:cmd(Cmd),
Pattern = "Created topic",
?assert(string:str(Result, Pattern) > 0, Result),
ok.
delete_topic(Topic) ->
wolff_test_utils:delete_topic(Topic).