-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathclient_brokers_util.cpp
More file actions
1628 lines (1279 loc) · 61.6 KB
/
client_brokers_util.cpp
File metadata and controls
1628 lines (1279 loc) · 61.6 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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "client_common.h"
// XXX: This is only used for testing the client
void TankClient::abort_broker_req(broker_api_request *br_req) {
static constexpr bool trace{false};
if (trace) {
SLog(ansifmt::bold, ansifmt::inverse, "Aboring broker request to ", br_req->br->ep, ansifmt::reset, "\n");
}
process_undeliverable_broker_req(br_req, true, __LINE__);
}
// api request timed out
// we'll just make it ready
void TankClient::abort_api_request(api_request *api_req) {
static constexpr const bool trace{false};
TANK_EXPECT(api_req);
if (trace) {
SLog("API request timed out, will abort\n");
#ifdef TANK_RUNTIME_CHECKS
SLog("Request was initialised ", duration_repr(Timings::Milliseconds::ToMicros(now_ms - api_req->init_ms)), " ago\n");
#endif
}
make_api_req_ready(api_req, __LINE__);
}
// Whenever a broker fails or otherwise any associated payloads can't be delivered
// this is invoked to deal with them
void TankClient::flush_broker(broker *const br) {
enum {
trace = false,
};
std::vector<broker_api_request *> reqs, failed_reqs;
assert(not br->ch.get()); // should have shut down this connection
if (trace) {
SLog(ansifmt::color_red, ansifmt::bgcolor_brown, "BEGIN:flush_broker() ", ptr_repr(br), " ",
br->ep,
", outgoing content = ", br->outgoing_content.size(),
", pending_responses = ", br->pending_responses_list.size(),
ansifmt::reset, "\n");
}
for (auto it = br->outgoing_content.front(); it;) {
auto br_req = it->broker_req;
auto next = it->next;
TANK_EXPECT(br_req->have_payload);
br_req->have_payload = false;
reqs.emplace_back(br_req);
put_payload(it, __LINE__);
it = next;
}
br->outgoing_content.clear();
TANK_EXPECT(not br->outgoing_content.first_payload_partially_transferred);
// Once we have successfully scheduled a paylaod for transmission,
// we track the request in pending_responses_list.
//
// this method is ivoked when the connection to the peer is severed
// so we need to deal with all those such pending responses.
//
// We don't know wether the peer has actually processed the request(1)
// or maybe that it did and we didn't get the response for any reason(2).
// What this means is that we can't assume anything, but it is important
// to play it safe, so we will only retry the request if we know
// it is idempotent, otherwise we will fail it (network error).
for (auto it : br->pending_responses_list) {
auto breq = containerof(broker_api_request, pending_responses_list_ll, it);
assert(not breq->have_payload);
auto api_req = breq->api_req;
assert(api_req);
const bool is_idempotent =
(api_req->type == api_request::Type::Consume or
api_req->type == api_request::Type::DiscoverPartitions or
api_req->type == api_request::Type::ReloadConfig or
api_req->type == api_request::Type::DiscoverTopics or
api_req->type == api_request::Type::DiscoverTopology or
api_req->type == api_request::Type::SrvStatus);
if (trace) {
SLog("pending broker request resp, is_idempotent = ", is_idempotent, "\n");
}
if (is_idempotent) {
reqs.emplace_back(breq);
} else {
failed_reqs.emplace_back(breq);
}
}
br->pending_responses_list.reset();
if (trace) {
SLog("reqs.size = ", reqs.size(), ", failed_reqs.size = ", failed_reqs.size(), "\n");
}
// It's important that we place all dequeued rquests into reqs
// and then process them here after we have drained
// the outgoing_content list etc
for (auto br_req : reqs) {
process_undeliverable_broker_req(br_req, false, __LINE__);
}
for (auto br_req : failed_reqs) {
auto api_req = br_req->api_req;
unlink_broker_req(br_req, __LINE__);
if (trace) {
SLog("Considering FAILED request\n");
}
while (not br_req->partitions_list.empty()) {
auto req_part = containerof(request_partition_ctx,
partitions_list_ll, br_req->partitions_list.next);
capture_network_fault(api_req, req_part->topic, req_part->partition);
req_part->partitions_list_ll.detach();
discard_request_partition_ctx(api_req, req_part);
}
br_req->partitions_list.reset();
put_broker_api_request(br_req);
try_make_api_req_ready(api_req, __LINE__);
}
if (trace) {
SLog("END:flush_broker()\n");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
// each request_partition_ctx encapsulates both request and response specific state
// for e.g produce requests, that could be the actual content
//
// note that response specific state may have been migrated/merged into an api_request specific state
// we don't want to destroy/release resources twice
void TankClient::clear_request_partition_ctx(api_request *const api_req, request_partition_ctx *const par) {
enum {
trace = false,
};
assert(api_req);
assert(par);
const auto resp_valid = std::exchange(par->as_op.response_valid, false);
if (trace) {
SLog("Will clear_request_partition_ctx() for type ", unsigned(api_req->type), ", resp_valid = ", resp_valid, "\n");
}
switch (api_req->type) {
case api_request::Type::Produce:
[[fallthrough]];
case api_request::Type::ProduceWithSeqnum:
if (auto v = std::exchange(par->as_op.produce.payload.data, nullptr)) {
std::free(v);
}
break;
case api_request::Type::Consume:
if (resp_valid) {
auto &resp = par->as_op.consume.response;
if (resp.msgs.cnt <= sizeof_array(resp.msgs.list.small)) {
// SBO
} else {
std::free(resp.msgs.list.large);
}
if (const auto n = resp.used_buffers.size) {
for (size_t i{0}; i < n; ++i) {
put_buffer(resp.used_buffers.data[i]);
}
std::free(resp.used_buffers.data);
}
}
break;
case api_request::Type::DiscoverPartitions:
if (resp_valid) {
auto &resp = par->as_op.discover_partitions.response;
delete resp.all;
}
break;
case api_request::Type::DiscoverTopics:
if (resp_valid) {
auto &resp = par->as_op.discover_topics.response;
delete resp.all;
}
break;
case api_request::Type::DiscoverTopology:
if (resp_valid) {
auto &resp = par->as_op.discover_topology.response;
delete resp.all;
resp.cluster_name.try_free();
}
break;
case api_request::Type::CreateTopic:
case api_request::Type::ReloadConfig:
case api_request::Type::SrvStatus:
break;
default:
IMPLEMENT_ME();
}
}
void TankClient::abort_api_request_retry_bundles(api_request *api_req, std::vector<request_partition_ctx *> *contexts) {
enum {
trace = false,
};
assert(api_req);
if (trace) {
if (!api_req->retry_bundles_list.empty()) {
SLog("Aborting all outstanding retry bundles for api request(", api_req->retry_bundles_list.size(), ")\n");
}
}
while (not api_req->retry_bundles_list.empty()) {
auto rb = containerof(retry_bundle, retry_bundles_ll, api_req->retry_bundles_list.next);
contexts->insert(contexts->end(), rb->data, rb->data + rb->size);
eb64_delete(&rb->node);
rb->retry_bundles_ll.detach();
std::free(rb);
}
retry_bundles_next = eb_is_empty(&retry_bundles_ebt_root)
? std::numeric_limits<uint64_t>::max()
: eb64_first(&retry_bundles_ebt_root)->key;
}
void TankClient::abort_api_request_brokers_reqs(api_request *api_req, std::vector<request_partition_ctx *> *contexts, const uint32_t ref) {
[[maybe_unused]] static constexpr bool trace{false};
TANK_EXPECT(api_req);
if (trace) {
if (!api_req->broker_requests_list.empty()) {
SLog(ansifmt::bgcolor_red, "Aborting all outstanding brokers requests for api request", ansifmt::reset, " broker_requests_list.size = ", api_req->broker_requests_list.size(), ", ref = ", ref, "\n");
}
}
while (!api_req->broker_requests_list.empty()) {
auto br_req = containerof(broker_api_request, broker_requests_list_ll, api_req->broker_requests_list.next);
if (trace) {
SLog("Aborting broker ", br_req->br->ep, " partitions\n");
}
while (!br_req->partitions_list.empty()) {
auto part = containerof(request_partition_ctx, partitions_list_ll, br_req->partitions_list.next);
part->partitions_list_ll.detach();
contexts->emplace_back(part);
}
unlink_broker_req(br_req, __LINE__);
put_broker_api_request(br_req);
}
}
// ready api_requests (i.e their responses) are retained
// until the next reactor loop iteration, so that the callee/client that embeds the tank client
// gets the chance to use the collected responses. We don't want to reclaim/destroy resources
// associated with those ready responses before that happens.
void TankClient::gc_api_request(std::unique_ptr<api_request> api_req) {
assert(api_req);
assert(api_req->api_reqs_expirations_tree_node.node.leaf_p == nullptr);
enum {
trace = false,
};
std::vector<request_partition_ctx *> contexts; // TODO: maybe reuse?
auto api_req_ptr = api_req.get();
if (trace) {
SLog(ansifmt::bold, ansifmt::color_brown, "BEGIN: gc_api_request()", ansifmt::reset,
": broker_requests_lists.size = ", api_req->broker_requests_list.size(), ", ready_partitions.size = ", api_req->ready_partitions_list.size(), "\n");
}
abort_api_request_retry_bundles(api_req.get(), &contexts);
abort_api_request_brokers_reqs(api_req.get(), &contexts, __LINE__);
// reclaim-release resources associated with request_partition_ctx responses
for (auto it = api_req->ready_partitions_list.next; it != &api_req->ready_partitions_list;) {
auto next = it->next;
auto p = containerof(request_partition_ctx, partitions_list_ll, it);
discard_request_partition_ctx(api_req.get(), p);
it = next;
}
api_req->ready_partitions_list.reset();
switch (api_req->type) {
case api_request::Type::DiscoverTopology:
delete api_req->materialized_resp.discover_topology.v;
if (auto ptr = api_req->materialized_resp.discover_topology._cluster_name_ptr) {
std::free(ptr);
}
break;
case api_request::Type::DiscoverTopics:
delete api_req->materialized_resp.discover_topics.v;
break;
case api_request::Type::DiscoverPartitions:
if (auto p = api_req_ptr->materialized_resp.discover_partitions.v) {
std::free(p);
}
break;
case api_request::Type::CreateTopic:
api_req_ptr->as.create_topic.config.try_free();
break;
case api_request::Type::Produce:
case api_request::Type::ProduceWithSeqnum:
case api_request::Type::ReloadConfig:
case api_request::Type::SrvStatus:
case api_request::Type::Consume:
break;
default:
IMPLEMENT_ME();
}
// release collected contexts
for (auto part : contexts) {
discard_request_partition_ctx(api_req_ptr, part);
}
// release any retained memory buffers
for (auto b : api_req->managed_bufs) {
b->unlock();
release_mb(b);
}
api_req->managed_bufs.clear();
// reuse the api request(and we are done)
put_api_request(std::move(api_req));
if (trace) {
SLog("END: gc_api_request()\n");
}
}
void TankClient::try_make_api_req_ready(api_request *api_req, const uint32_t ref) {
assert(api_req);
if (likely(api_req->request_id) and api_req->ready()) {
make_api_req_ready(api_req, ref);
}
}
// All broker_api_request' associated with an api_request
// have been processed or failed, thereby making the api request ready
void TankClient::make_api_req_ready(api_request *api_req, const uint32_t ref) {
enum {
trace = false,
};
TANK_EXPECT(api_req);
const auto id = api_req->request_id;
const bool failed = api_req->failed();
std::vector<request_partition_ctx *> contexts;
if (0 == id) [[unlikely]] {
// was already made ready
// this shouldn't happen, but we may as well just do nothing here
return;
}
if (api_req->api_reqs_expirations_tree_node.node.leaf_p) {
// abort timer, in case it wasn't already aborted earlier (i.e if
// make_api_req_ready() wasn't a result of check_pending_api_responses() checks)
const auto k = api_req->api_reqs_expirations_tree_node.key;
eb64_delete(&api_req->api_reqs_expirations_tree_node);
TANK_EXPECT(api_req->api_reqs_expirations_tree_node.node.leaf_p == nullptr);
if (k <= api_reqs_expirations_tree_next) {
api_reqs_expirations_tree_next = eb_is_empty(&api_reqs_expirations_tree)
? std::numeric_limits<uint64_t>::max()
: eb64_first(&api_reqs_expirations_tree)->key;
}
if (trace) {
SLog("Unklinked from api_reqs_expirations_tree\n");
}
}
const auto it = pending_responses.find(id);
TANK_EXPECT(it != pending_responses.end());
if (trace) {
SLog(ansifmt::bold, ansifmt::color_green, "API request response is READY for ", id, ansifmt::reset, " (ref = ", ref, ")\n");
}
// if we have any outstanding work, abort it
// we will capture a timeout error for any such work(i.e request_partition_ctx')
abort_api_request_retry_bundles(api_req, &contexts);
abort_api_request_brokers_reqs(api_req, &contexts, ref);
for (const auto part : contexts) {
if (not failed) {
enum {
trace = false,
};
if (trace) {
SLog("Capturing timeout from make_api_req_ready() invoked at ", ref, "\n");
}
capture_timeout(api_req, part->topic, part->partition, __LINE__);
}
discard_request_partition_ctx(api_req, part);
}
// materialize whatever we have for this request
const auto retain_for_next_iteration = materialize_api_response(api_req);
// see earlier comment
api_req->request_id = 0;
// stop tracking this request
// it's no longer a pending response (i.e it's ready)
it->second.release(); // release because we will transfer ownership to ready_responses or via gc_api_request()
pending_responses.erase(it);
if (retain_for_next_iteration) {
// will be destroyed in begin_reactor_loop_iteration()
// gc_api_request() will be ivoked for this api request
ready_responses.emplace_back(api_req);
if (trace) {
SLog("Retained for next iteration\n");
}
} else {
gc_api_request(std::unique_ptr<api_request>(api_req));
if (trace) {
SLog("was OK to GC now\n");
}
}
}
// a connection's input buffer's data is required by
// an api_request, which means
// tha buffers' data need to be retained(no reallocations are possible anymore)
// and that buffer needs to be retained, and tracked by the api_request to be release()ed
// in gc_api_request()
void TankClient::retain_conn_inbuf(connection *const c, api_request *api_req) {
TANK_EXPECT(c);
TANK_EXPECT(api_req);
auto b = c->in.b;
TANK_EXPECT(b);
TANK_EXPECT(b->use_count());
retain_mb(b);
TANK_EXPECT(b->use_count() > 1);
b->lock(); // noone can resize it
api_req->managed_bufs.emplace_back(b);
// we will defer acting on the ready api request until we are done processing I/O
}
// build a payload for a specific broker_api_request
// this is the representation of that broker api request to be transmitted to the broker
TankClient::broker_outgoing_payload *TankClient::build_broker_req_payload(broker_api_request *const req) {
enum {
trace = false,
};
assert(req);
assert(not req->have_payload);
const auto api_req = req->api_req;
broker_outgoing_payload *payload;
assert(api_req);
if (trace) {
SLog("Building request payload for ", unsigned(api_req->type), "\n");
}
switch (api_req->type) {
case api_request::Type::Consume:
payload = build_consume_broker_req_payload(req);
break;
case api_request::Type::DiscoverTopics:
payload = build_discover_topics_broker_req_payload(req);
break;
case api_request::Type::DiscoverTopology:
payload = build_discover_topology_req_payload(req);
break;
case api_request::Type::DiscoverPartitions:
payload = build_discover_partitions_broker_req_payload(req);
break;
case api_request::Type::Produce:
case api_request::Type::ProduceWithSeqnum:
payload = build_produce_broker_req_payload(req);
break;
case api_request::Type::ReloadConfig:
payload = build_reload_partition_conf_broker_req_payload(req);
break;
case api_request::Type::CreateTopic:
payload = build_create_topic_broker_req_payload(req);
break;
case api_request::Type::SrvStatus:
payload = build_srv_status_broker_req_payload(req);
break;
default:
payload = nullptr;
break;
}
TANK_EXPECT(payload);
// have_payload will be reset to false once
// the payload has been transferred in full or released via e.g unlink_broker_req()
payload->broker_req = const_cast<broker_api_request *>(req);
req->have_payload = true;
return payload;
}
// once all request_partition_ctx has been generated by e.g consume()
// and associated with a broker who may be the current leader of the (topic, partition)
// this method's responsible for generating broker_api_request's for each of the brokers involved, and
// pairing all request_partition_ctx's with it
switch_dlist *TankClient::assign_req_partitions_to_api_req(api_request *const api_req, // will pair with this api_request,
std::vector<std::pair<broker *, request_partition_ctx *>> *contexts,
const uint32_t limit) {
enum {
trace = false,
};
auto *next = &api_req->broker_requests_list;
// sort by (ptr(broker) asc, topic asc, partition asc)
std::sort(contexts->begin(), contexts->end(), [](const auto &a, const auto &b) noexcept {
if (a.first < b.first) {
return true;
} else if (b.first < a.first) {
return false;
}
if (const auto r = a.second->topic.Cmp(b.second->topic); r < 0) {
return true;
} else if (0 == r) {
return a.second->partition < b.second->partition;
} else {
return false;
}
});
if (trace) {
SLog(ansifmt::bold, ansifmt::color_blue, ansifmt::inverse, "Assigning ", contexts->size(),
" to api requests (limit = ", limit, ")", ansifmt::reset, "\n");
}
for (const auto *p = contexts->data(), *const e = p + contexts->size(); p < e;) {
auto br = p->first;
auto broker_req = get_broker_api_request();
// a new broker request for this api request
// will be tracked individually via pending_brokers_requests
broker_req->br = br;
broker_req->api_req = api_req;
broker_req->id = next_broker_request_id++;
if (next == &api_req->broker_requests_list) {
next = &broker_req->broker_requests_list_ll;
}
// some requests may need too many iovecs
// so a single broker_outgoing_payload may not be able to hold in its iovecs::data
// everything, so limit can be set to something sane so that
// multiple broker requests to the same endpoint may be potentially generated to accomodate that need
//
// currently, only produce() overrides the default limit value
// XXX: verify this
const auto upto = std::min(e, p + limit);
do {
auto req_part = p->second;
// track this request partition of this broker
req_part->partitions_list_ll.reset();
broker_req->partitions_list.push_back(&req_part->partitions_list_ll);
if (trace) {
SLog("For broker ", br->ep, " (", req_part->topic, "/", req_part->partition, ")\n");
}
} while (++p < upto and p->first == br);
if (unlikely(broker_req->partitions_list.size() > 250)) {
// we do not currently allow more than 250 partitions per topic
// to support that, we 'd need to change the protocol (i.e use u16 instead of u8 for number of
// partitions in a topic, in a request and in a response) and possibly other implementation details
// TODO: do something more sensible here than aborting
std::abort();
}
// track this new broker api request
pending_brokers_requests.emplace(broker_req->id, broker_req);
// associate it with the api request
// (impotant to use push_front(), not push_back() here.)
api_req->broker_requests_list.push_front(&broker_req->broker_requests_list_ll);
}
if (trace) {
size_t n{0};
for (auto it = next; it != &api_req->broker_requests_list; it = it->next) {
++n;
}
SLog("New dinstinct brokers requests ", n, "\n");
}
return next;
}
// We will be tracking all known brokers
//
// Whenever a node fails, it will be moved to the tail, and we will always use the broker at the head
// this is so that we will always attempt to use a broker that hasn't failed.
//
// We need this functionality for when we haven't had the chance to associate a (topic, partition) to a leader
// and for when we need to issue a request that's not particular to a topic or a partition(e.g discover_partitions)
TankClient::broker *TankClient::any_broker() {
if (all_brokers.empty()) [[unlikely]] {
throw std::runtime_error("No brokers");
}
// always use head
return containerof(broker, all_brokers_ll, all_brokers.next);
}
TankClient::broker *TankClient::broker_by_endpoint(const Switch::endpoint e) {
const auto res = brokers.emplace(e, nullptr);
if (res.second) {
auto b = std::make_unique<broker>(e);
TANK_EXPECT(b->all_brokers_ll.empty());
all_brokers.push_front(&b->all_brokers_ll);
res.first->second = std::move(b);
}
return res.first->second.get();
}
void TankClient::shift_failed_broker(broker *br) {
TANK_EXPECT(br);
// because any_broker() always returns head, we 'll just move this to tail
br->all_brokers_ll.try_detach_and_reset();
all_brokers.push_front(&br->all_brokers_ll);
}
// this is important
// in flush_broker() we can check all outstanding request_partition_ctx, and
// partition_leader(ctx.topic, ctx.partition) == broker, we need to retry later for the same broker, if not
// then we mark the broker as failed and use any_broker() again to get another node, and if we can't then wait anyway
TankClient::broker *TankClient::partition_leader(const str_view8 topic, const uint16_t partition) {
if (const auto it = leaders.find(topic_partition{topic, partition}); it != leaders.end()) {
return broker_by_endpoint(it->second.leader);
}
// we don't know about that yet
return nullptr;
}
#ifdef TANK_SUPPORT_CONSUME_FLAGS
TankClient::broker *TankClient::partition_provider(const str_view8 topic, const uint16_t partition) {
if (const auto it = leaders.find(topic_partition{topic, partition}); it != leaders.end()) {
return broker_by_endpoint(it->second.provider);
}
return nullptr;
}
#endif
str_view8 TankClient::intern_topic(const str_view8 topic_name) {
const auto res = topics_intern_map.emplace(topic_name, true);
if (res.second) {
const_cast<str_view8 *>(&res.first->first)->p =
core_allocator.CopyOf(topic_name.data(), topic_name.size());
}
return res.first->first;
}
// Create a new payload, suitable for a broker API request
TankClient::broker_outgoing_payload *TankClient::new_req_payload(broker_api_request *req) {
auto payload = get_payload();
payload->broker_req = req;
return payload;
}
bool TankClient::schedule_broker_req(broker_api_request *const breq) {
auto payload = build_broker_req_payload(breq);
return schedule_broker_payload(breq, payload);
}
// Enqueues payload with a broker's outgoing payloads queue
bool TankClient::schedule_broker_payload([[maybe_unused]] broker_api_request *br_req,
broker_outgoing_payload *payload) {
enum {
trace = false,
};
assert(payload);
assert(payload->iovecs.size);
assert(payload->broker_req);
assert(payload->broker_req->br);
auto br = payload->broker_req->br;
if (trace) {
SLog("Scheduling payload for broker ", ptr_repr(br), " ", br->ep, " br->outgoing_content.size = ", br->outgoing_content.size(), ", payload->iovecs.size = ", payload->iovecs.size, "\n");
}
if (br->reachability == broker::Reachability::Blocked) {
enum {
trace = false,
};
if (now_ms > br->blocked_until) {
if (trace) {
SLog("Was blocked, no longer blocked, will give it another try\n");
}
br->consequtive_connection_failures = broker::max_consequtive_connection_failures - 1;
br->set_reachability(broker::Reachability::LikelyUnreachable, __LINE__);
br->blocked_until = 0;
try_stop_track_unreachable(br);
} else {
if (trace) {
SLog("Is blocked, can't try right now. Will be blocked for another ", duration_repr(Timings::Milliseconds::ToMicros(br->blocked_until - now_ms)), "\n");
}
put_payload(payload, __LINE__);
return false;
}
}
const auto before = br->outgoing_content.size();
br->outgoing_content.push_back(payload);
TANK_EXPECT(br->outgoing_content.size() == before + 1);
if (br->unreachable_brokers_tree_node.node.leaf_p) {
if (trace) {
SLog("Node is tracked as unreachable, will wait until it can become reachable again: consequtive_connection_failures = ",
br->consequtive_connection_failures, ", br->outgoing_content.size now = ", br->outgoing_content.size(), "\n");
}
return true;
}
return try_transmit(br);
}
// Each created API request is tracked
// the request id generated is what's passed to the library user
uint32_t TankClient::track_pending_resp(std::unique_ptr<api_request> req) {
enum {
trace = false,
};
TANK_EXPECT(req);
TANK_EXPECT(req->request_id == 0);
const auto id = next_api_request_id++;
req->request_id = id;
if (const auto expiration = req->expiration()) {
api_reqs_expirations_tree_next = std::min<uint64_t>(expiration, api_reqs_expirations_tree_next);
eb64_insert(&api_reqs_expirations_tree, &req->api_reqs_expirations_tree_node);
}
const auto res = pending_responses.emplace(id, std::move(req));
TANK_EXPECT(res.second);
if (trace) {
SLog(ansifmt::bold, ansifmt::bgcolor_green, "SCHEDULING new api request", ansifmt::reset, " ID=", id, "\n");
}
return id;
}
// an API request cannot be fullfilled so it needs to be aborted immediately
void TankClient::fail_api_request(std::unique_ptr<api_request> ar) {
static constexpr bool trace{false};
TANK_EXPECT(ar);
TANK_EXPECT(ar->api_reqs_expirations_tree_node.node.leaf_p == nullptr); // wasn't tracked -- see track_pending_resp()
TANK_EXPECT(ar->request_id == 0); // ^
std::vector<request_partition_ctx *> contexts;
if (trace) {
SLog("FAILING api request\n");
}
abort_api_request_retry_bundles(ar.get(), &contexts);
abort_api_request_brokers_reqs(ar.get(), &contexts, __LINE__);
for (auto part : contexts) {
// XXX: timeout?
discard_request_partition_ctx(ar.get(), part);
}
for (auto b : ar->managed_bufs) {
release_mb(b);
}
ar->managed_bufs.clear();
put_api_request(std::move(ar));
}
// Generate a new payload for each broker_api_request associated with this api_request and schedule them
uint32_t TankClient::schedule_new_api_req(std::unique_ptr<api_request> api_req) {
enum {
trace = false,
};
TANK_EXPECT(!api_req->broker_requests_list.empty());
if (trace) {
SLog("Scheduling new API request api_req->broker_requests_list.size() = ", api_req->broker_requests_list.size(), "\n");
}
for (auto it = api_req->broker_requests_list.next; it != &api_req->broker_requests_list; it = it->next) {
auto broker_req = containerof(broker_api_request, broker_requests_list_ll, it);
if (not schedule_broker_req(broker_req)) {
enum {
trace = false,
};
// we will need to _fail_ this request
//
// we have a few options here:
// - we return 0
// - we capture a fault for every broker broker request
//
// For now, we 'll just abort this API request and return 0
if (trace) {
SLog(ansifmt::bold, ansifmt::color_brown, ansifmt::inverse, "Unable to schedule API request broker request", ansifmt::reset, "\n");
}
fail_api_request(std::move(api_req));
return 0;
}
}
return track_pending_resp(std::move(api_req));
}
void TankClient::unlink_broker_req(broker_api_request *br_req, const size_t ref) {
enum {
trace = false,
};
assert(br_req);
connection *c{nullptr};
auto br = br_req->br;
if (trace) {
SLog("Unlinking broker_api_request(Boker ID:", br_req->id,
"), have_payload = ", br_req->have_payload,
", pending:", not br_req->pending_responses_list_ll.empty(), ", ref = ", ref, "\n");
}
if (br_req->have_payload) {
// this gets complicated fast
// we have generated a payload for this request, and that payload may not have been scheduled
// for transmission to the peer yet
assert(br);
c = br->ch.get();
if (br->can_safely_abort_broker_request_payload(br_req)) {
if (trace) {
SLog("can_safely_abort_broker_request_payload()\n");
}
if (auto payload = br->abort_broker_request_payload(br_req)) {
if (trace) {
SLog("Did abort payload, broker->outgoing_content.size = ", br->outgoing_content.size(), " for ", br->ep, "\n");
}
put_payload(payload, __LINE__);
c = nullptr;
} else if (trace) {
SLog("Was not able to abort payload\n");
}
} else {
// if it's not safe, we 'll remove it anyway
// because we will shut down the connection
if (trace) {
SLog("Not safe to abort payload\n");
}
if (auto payload = br->abort_broker_request_payload(br_req)) {
if (trace) {
SLog("Brute-force aborting payload\n");
}
put_payload(payload, __LINE__);
} else if (trace) {
SLog("odd: unable to brute-force abort payload\n");
}
}
br_req->have_payload = false;
}
br_req->broker_requests_list_ll.try_detach_and_reset(); // from api_req
br_req->pending_responses_list_ll.try_detach_and_reset(); // from broker
// this may have been erased from pending_brokers_requests
// see process_consume_content_impl()
pending_brokers_requests.erase(br_req->id);
if (c) {
// if we are going to shut down the connection, what are we going to do
// with all other payloads that were outstanding for that broker?
if (trace) {
SLog("Will shutdown connection because we couldn't abort the broker request payload. br->outgoing_content.size() = ", br->outgoing_content.size(), "\n");
}
shutdown(c, __LINE__, false);
if (br) {
assert(br->ch.get() == nullptr);
if (not br->outgoing_content.empty()) {
if (trace) {
SLog("There's outstanding outgoing content for that broker\n");
}
try_transmit(br);
} else if (trace) {
SLog("No need to try to deliver content now\n");
}
}
}
}
void TankClient::process_undeliverable_broker_req(broker_api_request *br_req, const bool reason_timeout, [[maybe_unused]] const uint32_t ref) {
enum {
trace = false,
};
TANK_EXPECT(br_req);
const auto br_req_id = br_req->id;
TANK_EXPECT(br_req_id);
auto api_req = br_req->api_req;
TANK_EXPECT(api_req);
const auto expiration = api_req->expiration();
auto br = br_req->br;
if (trace) {