-
Notifications
You must be signed in to change notification settings - Fork 70
/
tank_client.h
1180 lines (983 loc) · 48.1 KB
/
tank_client.h
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
// Tank, because its a large container of liquid or gas
// and data flow (as in, liquid), and also, this is a Trinity character name
#pragma once
#include "common.h"
#include <atomic>
#include <compress.h>
#include <network.h>
#include <queue>
#include <vector>
#include <unordered_map>
#include <switch.h>
#include <switch_ll.h>
#include <switch_mallocators.h>
#include <switch_vector.h>
#include "client_common.h"
#include <ext/ebtree/eb64tree.h>
#include <unordered_set>
// XXX: experimental, see TODO
// this does seem to work, although the performance improvement is rather small
// it's definitely not ready yet, but once it is, we should be able to better understand the performance delta
//
// the idea is that by interleaving parsing of partitions/messages with network packets collection (i.e
// packets arriving from peer and stored in the socket's queue) we can produce responses faster because
// while the CPU is busy parsing the messages, packets are accumulated, and when its done parsing, we 'll have
// more-fresh packets to process
//#define TANK_CLIENT_FAST_CONSUME 1
// The problem we want to solve is how to *reliably* abort broker requests
//
// We need to abort a broker request if:
// - the broker request times-out
// - the api request is aborted(times-out? etc), and we subsequently wish to abort all associated broker requests
//
// Once a broker request is associated with a broker:
// - its payload may not be materialized yet
// - its payload may be scheduled for transfer, in full
// - its payload may be scheduled for transfer, in part
//
// Problems include:
// - If we generate a timeout for a (request, topic, partition) but the payload of that request is transmitted anyway, then
// what we report to the client(failure) is misleading.
//
// Important to consider:
// - Once we have scheduled for transmission via writev() 1+ bytes of a materialized request payload, we can't abort it
// without closing the connection and migrating (broker, etc) to another connection, because of TCP semantics
// - We need to be able to quickly figure out if there is a payload for a specific broker request so that we can
// remove it from the broker's outgoing_content list (iff we haven't transferred any bytes yet, see above).
// We can just scan the outgoing_content list for that (we associate the broker_requests with the payload so
// that's easy), but because the search is linear, this could potentially take too long.
// - if the broker's connection is closed, we can trivially abort everything
//#define HAVE_NETIO_THROTTLE 1
// specialize here otherwise compiler will fail (leaders)
namespace std {
template <>
struct hash<std::pair<str_view8, uint16_t>> {
using argument_type = std::pair<str_view8, uint16_t>;
using result_type = std::size_t;
inline result_type operator()(const argument_type &v) const noexcept {
size_t hash{2166136261U};
for (const auto c : v.first) {
hash = (hash * 16777619) ^ c;
}
hash ^= std::hash<uint16_t>{}(v.second) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
};
} // namespace std
// TankClient is not marked as final, because we need to use TEST_CASE_METHOD() (see catch2)
class TankClient {
public:
enum class ProduceFlags : uint8_t {
};
enum class RetryStrategy : uint8_t {
RetryNever = 0,
RetryAlways
};
enum class CompressionStrategy : uint8_t {
CompressNever = 0,
CompressAlways,
CompressIntelligently
};
struct msg final {
strwlen32_t content;
uint64_t ts;
strwlen8_t key;
};
using topic_partition = std::pair<strwlen8_t, uint16_t>;
struct consume_ctx final {
Switch::endpoint leader;
strwlen8_t topic;
uint16_t partitionId;
uint64_t absSeqNum;
uint32_t fetchSize;
};
struct produce_ctx final {
Switch::endpoint leader;
strwlen8_t topic;
uint16_t partitionId;
uint64_t baseSeqNum;
const msg *msgs;
size_t msgsCnt;
};
struct discovered_topic_partitions final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
strwlen8_t topic;
range_base<std::pair<uint64_t, uint64_t> *, uint16_t> watermarks;
};
struct srv_node final {
Switch::endpoint ep;
uint16_t partitions;
uint8_t id;
bool available;
bool blocked;
};
struct srv_topic final {
str_view8 name;
uint16_t partitions;
bool enabled;
uint8_t repl_factor;
};
struct discovered_srv_topics final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
struct {
uint32_t size;
srv_topic *data;
} topics;
};
struct discovered_topology final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
str_view8 cluster_name;
struct {
uint32_t size;
srv_node *data;
} nodes;
};
struct reload_conf_result final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
str_view8 topic;
uint16_t partition;
};
struct created_topic final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
strwlen8_t topic;
};
struct produce_ack final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
strwlen8_t topic;
uint16_t partition;
};
struct consumed_msg final {
union {
uint64_t seqNum;
uint64_t seq_num;
};
uint64_t ts;
strwlen8_t key;
strwlen32_t content;
};
struct srv_status final {
struct {
uint32_t topics;
uint32_t partitions;
uint32_t nodes;
uint32_t open_partitions;
uint32_t time_open_partitions;
} counts;
struct {
uint32_t time_open_partitions;
} metrics;
struct {
char data[64];
uint8_t len;
} cluster_name;
time32_t startup_ts;
uint32_t version;
};
struct partition_content final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
strwlen8_t topic;
uint16_t partition;
range_base<consumed_msg *, uint32_t> msgs;
// https://github.com/phaistos-networks/TANK/issues/1
// For now, this is always true, but when we implement support for pseudo-streaming (see GH issue)
// this may be false, in which case, the response is not complete -- more messages are expected for
// the request with id `clientReqId`
// See TankClient::set_allow_streaming_consume_responses();
bool respComplete;
// NEW:
// set to true if there was no content returned
// regardless of the minFetchSize
// this is the reliable way to check if the partition has been drained
bool drained;
// We can't rely on msgs.back().seqNum + 1 to compute the next sequence number
// to consume from, because if no message at all can be parsed because
// of the request fetch size, msgs will be empty().
// Furthermore, this separation allows us to support (especially in future revisions)
// different semantics
struct
{
// to get more messages
// consume from (topic, partition) starting from seqNum,
// and set fetchSize to be at least minFetchSize
union {
uint64_t seqNum;
uint64_t seq_num;
};
union {
uint32_t minFetchSize;
uint32_t min_fetch_size;
};
} next;
};
struct fault final {
union {
uint32_t clientReqId;
uint32_t client_req_id;
};
enum class Type : uint8_t {
UnknownTopic = 1,
UnknownPartition,
Access,
Network,
BoundaryCheck,
InvalidReq,
SystemFail,
AlreadyExists,
NotAllowed,
Timeout,
UnsupportedReq,
InsufficientReplicas,
} type;
enum class Req : uint8_t {
Consume = 1,
Produce,
Ctrl,
} req;
strwlen8_t topic;
uint16_t partition;
union {
// Set when fault is BoundaryCheck
struct
{
uint64_t firstAvailSeqNum;
uint64_t highWaterMark;
};
// for
uint64_t seq_num;
} ctx;
// handy utility function if type == Type::BoundaryCheck
// adjusts sequence number to be within [firstAvailSeqNum, highWaterMark + 1]
uint64_t adjust_seqnum_by_boundaries(uint64_t seqNum) const {
TANK_EXPECT(type == Type::BoundaryCheck);
if (seqNum < ctx.firstAvailSeqNum) {
seqNum = ctx.firstAvailSeqNum;
} else if (seqNum > ctx.highWaterMark) {
seqNum = ctx.highWaterMark + 1;
}
return seqNum;
}
};
protected:
struct timer final {
enum class Type : uint8_t {
TrackedResponseExpiration = 0,
} type;
eb64_node node;
void reset() {
node.node.leaf_p = nullptr;
}
bool is_linked() const noexcept {
return node.node.leaf_p;
}
};
struct connection;
struct broker;
struct connection_handle final {
connection *c_{nullptr};
uint64_t c_gen;
void reset() {
c_gen = std::numeric_limits<uint64_t>::max();
c_ = nullptr;
}
inline void set(connection *const c) noexcept;
inline connection *get() noexcept;
inline const connection *get() const noexcept;
};
struct request_partition_ctx final {
str_view8 topic;
// for some types of requests, partition is not used
// for some other topic is not used either (e.g SrvStatus)
uint16_t partition;
switch_dlist partitions_list_ll;
// for no_leader, retry chaining where it makes sense
request_partition_ctx *_next;
// XXX:
// clear_request_partition_ctx() shouldn't attempt to clear
// as_op.<foo>.response data if state there is not initialized or
// the resources associated with it were already released e.g in a materialize impl.
//
// UPDATE: we are now using response_valid for that
struct Op final {
union {
struct {
uint64_t seq_num;
uint32_t min_fetch_size;
struct {
struct {
uint64_t seq_num;
size_t min_size;
} next;
struct {
IOBuffer **data;
uint32_t size;
} used_buffers;
struct {
uint32_t cnt;
union {
consumed_msg small[8];
consumed_msg *large; // if (cnt >= sizeof_array(small)) we are going to be allocating
} list;
} msgs;
bool drained;
} response;
} consume;
struct {
struct {
// we will sort them when we construct the response
std::vector<std::pair<uint16_t, std::pair<uint64_t, uint64_t>>> *all;
} response;
} discover_partitions;
struct {
struct {
std::vector<srv_topic> *all;
} response;
} discover_topics;
struct {
struct {
str_view8 cluster_name;
std::vector<srv_node> *all;
} response;
} discover_topology;
struct {
struct {
uint8_t *data;
uint32_t size;
} payload;
// for ProduceWithBase requests
uint64_t first_msg_seqnum;
} produce;
struct {
decltype(std::declval<srv_status>().counts) counts;
decltype(std::declval<srv_status>().metrics) metrics;
time32_t startup_ts;
uint32_t version;
struct {
char data[64];
uint8_t len; // 0 if not cluster aware
} cluster_name;
} srv_status;
};
// UPDATE: 2021-10-11
// now using response_valid to track wether the response is initialized/valid
// so that we will only attempt to release response state if necessary
bool response_valid;
} as_op;
void reset() {
partitions_list_ll.reset();
_next = nullptr;
as_op.response_valid = false;
}
};
struct api_request;
struct retry_bundle final {
switch_dlist retry_bundles_ll;
uint64_t expiration;
api_request *api_req;
eb64_node node;
uint16_t size;
request_partition_ctx *data[0];
};
// a request to a specific broker, for 1+ partitions
// this is managed in a fan-out/coordinator fashion by an api_request
//
// each partition involved with this broker request has a request specific configuration
struct broker_api_request final {
broker *br;
api_request *api_req;
// we need a distinct id for every such request, and that's encoded
// in the broker_outgoing_payload we generate and stream to the broker
// because we need to to encode it in the payload so that
// when the broker responds, its response will include
// this request ID and we can look up the payload by that id (see payloads_map)
uint32_t id;
switch_dlist partitions_list; // tracks request_partition_ctx's
switch_dlist broker_requests_list_ll; // tracked by the api_request via this ll
// when we deliver (in full) a payload to a broker via writev()
// we need to track them, so that if the broker fails, we 'll know which requests those were
switch_dlist pending_responses_list_ll;
bool have_payload;
// Some requests, e.g discover_partitions
// are not associated with any partitions, so we could just include request-state here
// in a union - but for simplicity reasons, we 'll just use a request_partition_ctx as well, where the partititon doesn't really matter
void reset() {
partitions_list.reset();
broker_requests_list_ll.reset();
pending_responses_list_ll.reset();
api_req = nullptr;
br = nullptr;
id = 0;
have_payload = false;
}
};
struct msgs_bucket final {
consumed_msg data[512];
msgs_bucket *next;
};
// coordinates(fan-out) 1+ broker_api_request's
struct managed_buf;
struct api_request final {
enum class Type : uint8_t {
Consume = 0,
Produce,
ProduceWithSeqnum,
DiscoverPartitions,
CreateTopic,
ReloadConfig,
SrvStatus,
DiscoverTopics,
DiscoverTopology,
} type;
uint32_t request_id; // client request ID
#ifdef TANK_RUNTIME_CHECKS
uint64_t init_ms;
#endif
eb64_node api_reqs_expirations_tree_node;
switch_dlist broker_requests_list;
// those are migrated from broker_api_request to api_request
// when we get a response for them
switch_dlist ready_partitions_list;
switch_dlist retry_bundles_list;
std::vector<managed_buf *> managed_bufs;
bool _failed;
void track_ready_part_req(request_partition_ctx *const req_part) {
assert(req_part);
assert(not req_part->partitions_list_ll.empty());
req_part->partitions_list_ll.detach_and_reset();
ready_partitions_list.push_back(&req_part->partitions_list_ll);
}
bool ready() const noexcept {
return retry_bundles_list.empty() and broker_requests_list.empty();
}
void set_failed() noexcept {
_failed = true;
}
bool failed() const noexcept {
return _failed;
}
auto expiration() const noexcept {
return api_reqs_expirations_tree_node.key;
}
// for some types of requests, we may
// want to materialize something in-memory
// so that when we gc_api_request() it will be destroyed
//
// XXX: maybe we need a response_valid for materialied_resp like in request_partition_ctx
union {
struct {
std::pair<uint64_t, uint64_t> *v;
} discover_partitions;
struct {
std::vector<srv_topic> *v;
} discover_topics;
struct {
std::vector<srv_node> *v;
char *_cluster_name_ptr;
} discover_topology;
} materialized_resp;
union As final {
struct Consume final {
uint64_t max_wait;
size_t min_size;
uint8_t flags;
} consume;
struct CreateTopic final {
uint16_t partitions;
str_view32 config;
} create_topic;
As()
: create_topic{} {
}
} as;
void reset() {
broker_requests_list.reset();
ready_partitions_list.reset();
managed_bufs.clear();
retry_bundles_list.reset();
request_id = 0;
api_reqs_expirations_tree_node.key = 0;
api_reqs_expirations_tree_node.node.leaf_p = nullptr;
_failed = false;
memset(&materialized_resp, 0, sizeof(materialized_resp));
}
};
// a *materialized* broker_api_request
// it's really just holds a buffer and the iovecs
struct broker_outgoing_payload final {
broker_outgoing_payload *next;
IOBuffer *b;
broker_api_request *broker_req;
struct IOVECS final {
struct iovec data[256];
uint8_t size;
} iovecs;
};
struct buf_llhdr final {
IOBuffer *b;
buf_llhdr *next;
};
struct managed_buf final {
enum class Flags : uint8_t {
Locked = 1u << 0,
};
managed_buf *next{nullptr};
uint32_t rc{1}, locks_{0};
uint32_t capacity_{0};
uint32_t length{0};
uint32_t offset_{0};
char *data_{nullptr};
// an object may depend on 0+ buffers
// a buffer may be a dependncy of 0+ objects
switch_dlist users_list{&users_list, &users_list};
~managed_buf() {
if (data_) {
std::free(data_);
}
}
void serialize(int8_t *ptr, const size_t l) {
TANK_EXPECT(locks_ == 0);
reserve(l);
memcpy(data_ + length, ptr, l);
length += l;
}
auto size() const noexcept {
return length;
}
const char *data() const noexcept {
return data_;
}
char *data() noexcept {
return data_;
}
void lock() noexcept {
++locks_;
}
bool is_locked() const noexcept {
return locks_;
}
bool unlock() {
TANK_EXPECT(locks_);
return --locks_ == 0;
}
void reserve(const size_t n) {
if (n > capacity_ && likely(n)) {
capacity_ = n;
data_ = static_cast<char *>(realloc(data_, capacity_ * sizeof(char)));
}
}
auto capacity() const noexcept {
return capacity_;
}
void clear() TANK_NOEXCEPT_IF_NORUNTIME_CHECKS {
TANK_EXPECT(locks_ == 0);
length = 0;
locks_ = 0;
offset_ = 0;
next = nullptr;
}
void set_offset(const char *p) {
TANK_EXPECT(p >= data() && p <= data() + size());
offset_ = p - data();
}
void set_offset(const uint32_t o) {
TANK_EXPECT(o <= size());
offset_ = o;
}
void erase_chunk(const uint32_t o, const uint32_t s) {
auto p = data_ + o;
TANK_EXPECT(locks_ == 0);
TANK_EXPECT(o + s <= size());
memmove(p, p + s, size() - (o + s));
length -= s;
}
auto offset() const noexcept {
return offset_;
}
void retain() noexcept {
++rc;
}
bool release() noexcept {
return --rc == 0;
}
auto use_count() const noexcept {
return rc;
}
};
struct connection final {
int fd{-1};
enum class Type : uint8_t {
Tank
} type;
#ifdef HAVE_NETIO_THROTTLE
struct {
struct {
switch_dlist ll;
uint64_t until;
void reset() {
ll.reset();
until = std::numeric_limits<uint64_t>::max();
}
} read, write;
} throttler;
#endif
uint64_t gen;
switch_dlist all_conns_list_ll;
union {
struct {
switch_dlist list;
uint64_t expiration;
};
};
struct {
managed_buf *b;
} in;
struct State final {
enum class Flags : uint8_t {
ConnectionAttempt = 0,
NeedOutAvail,
RetryingConnection,
};
uint8_t flags;
} state;
struct As final {
struct Tank final {
enum class Flags : uint8_t {
ConsideredReqHeader = 0,
InterleavedRespAssembly = 1,
};
#ifdef TANK_CLIENT_FAST_CONSUME
// this is used exclusively for consume responses
struct Response final {
enum class State : uint8_t {
ParseHeader = 0,
ParseTopic,
ParseFirstTopicPartition,
ParsePartition,
ParsePartitionBundle,
ParsePartitionBundleMsgSet,
Drain,
Ready,
} state;
request_partition_ctx *no_leader_l, *retry_l;
bool any_faults;
bool retain_buf;
uint32_t resp_end_offset;
broker_api_request *breq;
uint32_t req_id;
uint32_t hdr_size;
uint8_t topics_cnt;
uint8_t topic_partitions_cnt;
switch_dlist *br_req_partctx_it;
buf_llhdr *used_bufs;
struct {
char data_[256];
uint8_t len;
auto size() const noexcept {
return len;
}
const char *data() const noexcept {
return data_;
}
} topic_name;
struct {
uint64_t highwater_mark;
uint32_t bundles_chunk_len;
uint64_t log_base_seqnum;
uint32_t need_upto, need_from;
struct {
msgs_bucket *first_bucket, *last_bucket;
uint32_t last_bucket_size;
uint32_t consumed;
void reset() {
first_bucket = last_bucket = nullptr;
last_bucket_size = sizeof_array(msgs_bucket::data);
consumed = 0;
}
} capture_ctx;
struct {
uint32_t offset;
uint32_t end;
} bundles_chunk;
struct {
uint8_t codec;
bool sparse;
bool any_captured;
uint64_t first_msg_seqnum, last_msg_seqnum;
uint32_t size;
uint32_t conumed;
union MsgSetContent final {
struct {
const uint8_t *p;
const uint8_t *e;
} tmpbuf_range;
struct {
uint32_t o;
uint32_t e;
} inb_range;
} msgset_content;
struct {
uint64_t ts;
uint32_t msg_idx;
uint64_t min_accepted_seqnum;
uint32_t size;
} cur_msg_set;
} cur_bundle;
} cur_partition;
void reset() {
state = State::ParseHeader;
retain_buf = false;
used_bufs = nullptr;
any_faults = false;
no_leader_l = nullptr;
retry_l = nullptr;
}
} cur_resp;
#endif
uint8_t flags;
broker *br;
void reset() {
flags = 0;
br = nullptr;
#ifdef TANK_CLIENT_FAST_CONSUME
cur_resp.reset();
#endif
}
} tank;
} as;
void reset() noexcept {
in.b = nullptr;
fd = -1;
list.reset();
all_conns_list_ll.reset();
#ifdef HAVE_NETIO_THROTTLE
throttler.read.reset();
throttler.write.reset();
#endif
}
};
uint32_t next_broker_request_id{1};
uint32_t next_api_request_id{1};
struct broker final {
enum : size_t {
max_consequtive_connection_failures = 5,
};
uint64_t blocked_until{0};
eb64_node unreachable_brokers_tree_node{
.node.leaf_p = nullptr,
};
connection_handle ch;
const Switch::endpoint ep;
uint8_t consequtive_connection_failures{0};
switch_dlist all_brokers_ll{&all_brokers_ll, &all_brokers_ll};
uint8_t flags{0};
enum class Flags : uint8_t {
Important = 0,
};
enum class Reachability : uint8_t {
LikelyUnreachable,
LikelyReachable,
MaybeReachable,
Blocked,
} reachability{Reachability::MaybeReachable};
switch_dlist pending_responses_list{&pending_responses_list, &pending_responses_list};
void set_reachability(const Reachability, const size_t);
// Tank node connections will drain a broker's outgoing_content
// i.e data will be tracked by the broker, not the connection
struct {
using payload = broker_outgoing_payload;
payload *front_{nullptr}, *back_{nullptr};
uint8_t front_payload_iovecs_index{0};
bool first_payload_partially_transferred{false};
size_t size() const noexcept {
size_t n{0};
for (auto it = front_; it; it = it->next) {
++n;
}
return n;
}
void push_front(payload *p) noexcept {
p->next = nullptr;
if (front_) {
front_->next = p;
} else {
back_ = p;
}
front_ = p;
}
void push_back(payload *p) {
p->next = nullptr;
if (back_) {
back_->next = p;
TANK_EXPECT(front_);
} else {
front_ = p;