forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KafkaDataTypes.h
1638 lines (1321 loc) · 32.1 KB
/
KafkaDataTypes.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
/*
Copyright (c) 2020 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Authors: Wang Zhulei (wangzhulei@sogou-inc.com)
*/
#ifndef _KAFKA_DATATYPES_H_
#define _KAFKA_DATATYPES_H_
#include <assert.h>
#include <algorithm>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <vector>
#include <string>
#include <string.h>
#include <atomic>
#include <snappy.h>
#include <snappy-sinksource.h>
#include "list.h"
#include "rbtree.h"
#include "kafka_parser.h"
namespace protocol
{
template<class T>
class KafkaList
{
public:
KafkaList()
{
this->t_list = new struct list_head;
INIT_LIST_HEAD(this->t_list);
this->ref = new std::atomic<int>(1);
this->curpos = this->t_list;
}
~KafkaList()
{
if (--*this->ref == 0)
{
struct list_head *pos, *tmp;
T *t;
list_for_each_safe(pos, tmp, this->t_list)
{
t = list_entry(pos, T, list);
list_del(pos);
delete t;
}
delete this->t_list;
delete this->ref;
}
}
KafkaList(KafkaList&& move)
{
this->t_list = move.t_list;
move.t_list = new struct list_head;
INIT_LIST_HEAD(move.t_list);
this->ref = move.ref;
this->curpos = this->t_list;
move.ref = new std::atomic<int>(1);
}
KafkaList& operator= (KafkaList&& move)
{
if (this != &move)
{
this->~KafkaList();
this->t_list = move.t_list;
move.t_list = new struct list_head;
INIT_LIST_HEAD(move.t_list);
this->ref = move.ref;
this->curpos = this->t_list;
move.ref = new std::atomic<int>(1);
}
return *this;
}
KafkaList(const KafkaList& copy)
{
this->ref = copy.ref;
++*this->ref;
this->t_list = copy.t_list;
this->curpos = copy.curpos;
}
KafkaList& operator= (const KafkaList& copy)
{
this->~KafkaList();
this->ref = copy.ref;
++*this->ref;
this->t_list = copy.t_list;
this->curpos = copy.curpos;
return *this;
}
T *add_item(T&& move)
{
T *t = new T;
*t = std::move(move);
list_add_tail(t->get_list(), this->t_list);
return t;
}
void add_item(T& obj)
{
T *t = new T;
*t = obj;
list_add_tail(t->get_list(), this->t_list);
}
struct list_head *get_head() { return this->t_list; }
struct list_head *get_tail() { return this->t_list->prev; }
T *get_first_entry()
{
if (this->t_list == this->t_list->next)
return NULL;
return list_entry(this->t_list->next, T, list);
}
T *get_tail_entry()
{
if (this->t_list == this->get_tail())
return NULL;
return list_entry(this->get_tail(), T, list);
}
T *get_entry(struct list_head *pos)
{
return list_entry(pos, T, list);
}
void rewind()
{
this->curpos = this->t_list;
}
T *get_next()
{
if (this->curpos->next == this->t_list)
return NULL;
this->curpos = this->curpos->next;
return list_entry(this->curpos, T, list);
}
void insert_pos(struct list_head *list, struct list_head *pos)
{
__list_add(list, pos, pos->next);
}
void del_cur()
{
assert(this->curpos != this->t_list);
this->curpos = this->curpos->prev;
list_del(this->curpos->next);
}
private:
struct list_head *t_list;
std::atomic<int> *ref;
struct list_head *curpos;
};
template<class T>
class KafkaMap
{
public:
KafkaMap()
{
this->t_map = new struct rb_root;
this->t_map->rb_node = NULL;
this->ref = new std::atomic<int>(1);
}
~KafkaMap()
{
if (--*this->ref == 0)
{
T *t;
while (this->t_map->rb_node)
{
t = rb_entry(this->t_map->rb_node, T, rb);
rb_erase(this->t_map->rb_node, this->t_map);
delete t;
}
delete this->t_map;
delete this->ref;
}
}
KafkaMap(const KafkaMap& copy)
{
this->ref = copy.ref;
++*this->ref;
this->t_map = copy.t_map;
}
KafkaMap& operator= (const KafkaMap& copy)
{
this->~KafkaMap();
this->ref = copy.ref;
++*this->ref;
this->t_map = copy.t_map;
return *this;
}
T *find_item(int id)
{
rb_node **p = &this->t_map->rb_node;
T *t;
while (*p)
{
t = rb_entry(*p, T, rb);
if (id < t->get_id())
p = &(*p)->rb_left;
else if (id > t->get_id())
p = &(*p)->rb_right;
else
break;
}
return *p ? t : NULL;
}
void add_item(T& obj)
{
rb_node **p = &this->t_map->rb_node;
rb_node *parent = NULL;
T *t;
int id = obj.get_id();
while (*p)
{
parent = *p;
t = rb_entry(*p, T, rb);
if (id < t->get_id())
p = &(*p)->rb_left;
else if (id > t->get_id())
p = &(*p)->rb_right;
else
break;
}
if (*p == NULL)
{
T *nt = new T;
*nt = obj;
rb_link_node(nt->get_rb(), parent, p);
rb_insert_color(nt->get_rb(), this->t_map);
}
}
T *get_first_entry()
{
struct rb_node *p = rb_first(this->t_map);
return rb_entry(p, T, rb);
}
T *get_tail_entry()
{
struct rb_node *p = rb_last(this->t_map);
return rb_entry(p, T, rb);
}
private:
struct rb_root *t_map;
std::atomic<int> *ref;
};
class KafkaConfig
{
public:
void set_produce_timeout(int ms) { this->ptr->produce_timeout = ms; }
int get_produce_timeout() const { return this->ptr->produce_timeout; }
void set_produce_msg_max_bytes(int bytes)
{
this->ptr->produce_msg_max_bytes = bytes;
}
int get_produce_msg_max_bytes() const
{
return this->ptr->produce_msg_max_bytes;
}
void set_produce_msgset_cnt(int cnt)
{
this->ptr->produce_msgset_cnt = cnt;
}
int get_produce_msgset_cnt() const
{
return this->ptr->produce_msgset_cnt;
}
void set_produce_msgset_max_bytes(int bytes)
{
this->ptr->produce_msgset_max_bytes = bytes;
}
int get_produce_msgset_max_bytes() const
{
return this->ptr->produce_msgset_max_bytes;
}
void set_fetch_timeout(int ms) { this->ptr->fetch_timeout = ms; }
int get_fetch_timeout() const { return this->ptr->fetch_timeout; }
void set_fetch_min_bytes(int bytes) { this->ptr->fetch_min_bytes = bytes; }
int get_fetch_min_bytes() const { return this->ptr->fetch_min_bytes; }
void set_fetch_max_bytes(int bytes) { this->ptr->fetch_max_bytes = bytes; }
int get_fetch_max_bytes() const { return this->ptr->fetch_max_bytes; }
void set_fetch_msg_max_bytes(int bytes) { this->ptr->fetch_msg_max_bytes = bytes; }
int get_fetch_msg_max_bytes() const { return this->ptr->fetch_msg_max_bytes; }
void set_offset_timestamp(long long tm)
{
this->ptr->offset_timestamp = tm;
}
long long get_offset_timestamp() const
{
return this->ptr->offset_timestamp;
}
void set_commit_timestamp(long long commit_timestamp)
{
this->ptr->commit_timestamp = commit_timestamp;
}
long long get_commit_timestamp() const { return this->ptr->commit_timestamp; }
void set_session_timeout(int ms) { this->ptr->session_timeout = ms; }
int get_session_timeout() const { return this->ptr->session_timeout; }
void set_rebalance_timeout(int ms) { this->ptr->rebalance_timeout = ms; }
int get_rebalance_timeout() const { return this->ptr->rebalance_timeout; }
void set_retention_time_period(long long ms)
{
this->ptr->retention_time_period = ms;
}
long long get_retention_time_period() const
{
return this->ptr->retention_time_period;
}
void set_produce_acks(int acks) { this->ptr->produce_acks = acks; }
int get_produce_acks() const { return this->ptr->produce_acks; }
void set_allow_auto_topic_creation(bool allow_auto_topic_creation)
{
this->ptr->allow_auto_topic_creation = allow_auto_topic_creation;
}
bool get_allow_auto_topic_creation() const
{
return this->ptr->allow_auto_topic_creation;
}
void set_api_version_request(int api_ver)
{
this->ptr->api_version_request = api_ver;
}
int get_api_version_request() const
{
return this->ptr->api_version_request;
}
bool set_broker_version(const char *version)
{
char *p = strdup(version);
if (!p)
return false;
free(this->ptr->broker_version);
this->ptr->broker_version = p;
return true;
}
const char *get_broker_version() const
{
return this->ptr->broker_version;
}
void set_compress_type(int type) { this->ptr->compress_type = type; }
int get_compress_type() const { return this->ptr->compress_type; }
const char *get_client_id() { return this->ptr->client_id; }
bool set_client_id(const char *client_id)
{
char *p = strdup(client_id);
if (!p)
return false;
free(this->ptr->client_id);
this->ptr->client_id = p;
return true;
}
bool get_check_crcs() const
{
return this->ptr->check_crcs != 0;
}
void set_check_crcs(bool check_crcs)
{
this->ptr->check_crcs = check_crcs;
}
int get_offset_store() const
{
return this->ptr->offset_store;
}
void set_offset_store(int offset_store)
{
this->ptr->offset_store = offset_store;
}
const char *get_sasl_mechanisms() const
{
return this->ptr->sasl.mechanisms;
}
bool set_sasl_mechanisms(const char *mechanisms)
{
char *p = strdup(mechanisms);
if (!p)
return false;
free(this->ptr->sasl.mechanisms);
this->ptr->sasl.mechanisms = p;
if (kafka_sasl_set_mechanisms(this->ptr) != 0)
return false;
return true;
}
const char *get_sasl_username() const
{
return this->ptr->sasl.username;
}
bool set_sasl_username(const char *username)
{
return kafka_sasl_set_username(username, this->ptr) == 0;
}
const char *get_sasl_password() const
{
return this->ptr->sasl.passwd;
}
bool set_sasl_password(const char *passwd)
{
return kafka_sasl_set_username(passwd, this->ptr) == 0;
}
std::string get_sasl_info() const
{
std::string info;
if (strcmp(this->ptr->sasl.mechanisms, "plain") == 0)
{
info = this->ptr->sasl.username;
info += "|";
info += this->ptr->sasl.passwd;
}
return info;
}
kafka_sasl_t *get_sasl()
{
return &this->ptr->sasl;
}
bool new_client()
{
return this->ptr->sasl.client_new(this->ptr) == 0;
}
public:
KafkaConfig()
{
this->ptr = new kafka_config_t;
kafka_config_init(this->ptr);
this->ref = new std::atomic<int>(1);
}
virtual ~KafkaConfig()
{
if (--*this->ref == 0)
{
kafka_config_deinit(this->ptr);
delete this->ptr;
delete this->ref;
}
}
KafkaConfig(KafkaConfig&& move)
{
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_config_t;
kafka_config_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
KafkaConfig& operator= (KafkaConfig&& move)
{
if (this != &move)
{
this->~KafkaConfig();
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_config_t;
kafka_config_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
return *this;
}
KafkaConfig& operator= (const KafkaConfig& copy)
{
this->~KafkaConfig();
this->ptr = copy.ptr;
this->ref = copy.ref;
++*this->ref;
return *this;
}
kafka_config_t *get_raw_ptr() { return this->ptr; }
private:
kafka_config_t *ptr;
std::atomic<int> *ref;
std::string sasl_buf;
};
class KafkaRecord
{
public:
bool set_key(const void *key, size_t key_len)
{
return kafka_record_set_key(key, key_len, this->ptr) == 0;
}
void get_key(const void **key, size_t *key_len) const
{
*key = this->ptr->key;
*key_len = this->ptr->key_len;
}
size_t get_key_len() const { return this->ptr->key_len; }
bool set_value(const void *value, size_t value_len)
{
return kafka_record_set_value(value, value_len, this->ptr) == 0;
}
void get_value(const void **value, size_t *value_len) const
{
*value = this->ptr->value;
*value_len = this->ptr->value_len;
}
size_t get_value_len() const { return this->ptr->value_len; }
bool add_header_pair(const void *key, size_t key_len,
const void *val, size_t val_len);
bool add_header_pair(const std::string& key, const std::string& val);
struct list_head *get_list() { return &this->list; }
const char *get_topic() const { return this->ptr->toppar->topic_name; }
void set_status(short err) { this->ptr->status = err; }
short get_status() const { return this->ptr->status; }
int get_partition() const { return this->ptr->toppar->partition; }
long long get_offset() const { return this->ptr->offset; }
void set_offset(long long offset) { this->ptr->offset = offset; }
long long get_timestamp() const { return this->ptr->timestamp; }
void set_timestamp(long long timestamp) { this->ptr->timestamp = timestamp; }
public:
KafkaRecord()
{
this->ptr = new kafka_record_t;
kafka_record_init(this->ptr);
this->ref = new std::atomic<int>(1);
}
~KafkaRecord()
{
if (--*this->ref == 0)
{
kafka_record_deinit(this->ptr);
delete this->ptr;
delete this->ref;
}
}
KafkaRecord(KafkaRecord&& move)
{
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_record_t;
kafka_record_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
KafkaRecord& operator= (KafkaRecord&& move)
{
if (this != &move)
{
this->~KafkaRecord();
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_record_t;
kafka_record_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
return *this;
}
KafkaRecord& operator= (KafkaRecord& copy)
{
this->~KafkaRecord();
this->ptr = copy.ptr;
this->ref = copy.ref;
++*this->ref;
return *this;
}
kafka_record_t *get_raw_ptr() const { return this->ptr; }
struct list_head *get_header_list() const { return &this->ptr->header_list; }
private:
struct list_head list;
kafka_record_t *ptr;
std::atomic<int> *ref;
friend class KafkaMessage;
friend class KafkaResponse;
friend class KafkaToppar;
};
class KafkaMeta;
class KafkaBroker;
class KafkaToppar;
using KafkaMetaList = KafkaList<KafkaMeta>;
using KafkaBrokerList = KafkaList<KafkaBroker>;
using KafkaBrokerMap = KafkaMap<KafkaBroker>;
using KafkaTopparList = KafkaList<KafkaToppar>;
using KafkaRecordList = KafkaList<KafkaRecord>;
extern KafkaToppar *get_toppar(const char *topic, int partition,
KafkaTopparList *toppar_list);
extern const KafkaMeta *get_meta(const char *topic, KafkaMetaList *meta_list);
class KafkaToppar
{
public:
bool set_topic_partition(const std::string& topic, int partition)
{
return kafka_topic_partition_set_tp(topic.c_str(), partition,
this->ptr) == 0;
}
bool set_topic(const char *topic)
{
this->ptr->topic_name = strdup(topic);
return this->ptr->topic_name != NULL;
}
const char *get_topic() const { return this->ptr->topic_name; }
int get_partition() const { return this->ptr->partition; }
long long get_offset() const { return this->ptr->offset; }
void set_offset(long long offset) { this->ptr->offset = offset; }
long long get_offset_timestamp() const { return this->ptr->offset_timestamp; }
void set_offset_timestamp(long long tm) { this->ptr->offset_timestamp = tm; }
long long get_high_watermark() const { return this->ptr->high_watermark; }
long long get_low_watermark() const { return this->ptr->low_watermark; }
void set_low_watermark(long long offset) { this->ptr->low_watermark = offset; }
public:
KafkaToppar()
{
this->ptr = new kafka_topic_partition_t;
kafka_topic_partition_init(this->ptr);
this->ref = new std::atomic<int>(1);
this->curpos = &this->ptr->record_list;
this->startpos = this->endpos = this->curpos;
}
~KafkaToppar();
KafkaToppar(KafkaToppar&& move)
{
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_topic_partition_t;
kafka_topic_partition_init(move.ptr);
move.ref = new std::atomic<int>(1);
this->curpos = &this->ptr->record_list;
this->startpos = this->endpos = this->curpos;
}
KafkaToppar& operator= (KafkaToppar&& move)
{
if (this != &move)
{
this->~KafkaToppar();
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_topic_partition_t;
kafka_topic_partition_init(move.ptr);
move.ref = new std::atomic<int>(1);
this->curpos = &this->ptr->record_list;
this->startpos = this->endpos = this->curpos;
}
return *this;
}
KafkaToppar(const KafkaToppar& copy)
{
this->ptr = copy.ptr;
this->ref = copy.ref;
++*this->ref;
this->curpos = copy.curpos;
this->startpos = copy.startpos;
this->endpos = copy.endpos;
}
KafkaToppar& operator= (KafkaToppar& copy)
{
this->~KafkaToppar();
this->ptr = copy.ptr;
this->ref = copy.ref;
++*this->ref;
this->curpos = copy.curpos;
this->startpos = copy.startpos;
this->endpos = copy.endpos;
return *this;
}
kafka_topic_partition_t *get_raw_ptr() { return this->ptr; }
struct list_head *get_list() { return &this->list; }
struct list_head *get_record() { return &this->ptr->record_list; }
void set_error(short error) { this->ptr->error = error; }
int get_error() const { return this->ptr->error; }
void add_record(KafkaRecord&& record)
{
KafkaRecord *tmp = new KafkaRecord;
*tmp =std::move(record);
list_add_tail(tmp->get_list(), &this->ptr->record_list);
}
void record_rewind()
{
this->curpos = &this->ptr->record_list;
}
KafkaRecord *get_record_next()
{
if (this->curpos->next == &this->ptr->record_list)
return NULL;
this->curpos = this->curpos->next;
return list_entry(this->curpos, KafkaRecord, list);
}
void del_record_cur()
{
assert(this->curpos != &this->ptr->record_list);
this->curpos = this->curpos->prev;
list_del(this->curpos->next);
}
struct list_head *get_record_startpos()
{
return this->startpos;
}
struct list_head *get_record_endpos()
{
return this->endpos;
}
void restore_record_curpos()
{
this->curpos = this->startpos;
this->endpos = NULL;
}
void save_record_startpos()
{
this->startpos = this->curpos;
}
void save_record_endpos()
{
this->endpos = this->curpos->next;
}
bool record_reach_end()
{
return this->endpos == &this->ptr->record_list;
}
void record_rollback()
{
this->curpos = this->curpos->prev;
}
private:
struct list_head list;
kafka_topic_partition_t *ptr;
std::atomic<int> *ref;
struct list_head *curpos;
struct list_head *startpos;
struct list_head *endpos;
friend class KafkaMessage;
friend class KafkaRequest;
friend class KafkaResponse;
friend class KafkaList<KafkaToppar>;
friend class KafkaCgroup;
friend KafkaToppar *get_toppar(const char *topic, int partition,
KafkaTopparList *toppar_list);
};
class KafkaBroker
{
public:
const char *get_host() const
{
return this->ptr->host;
}
int get_port() const
{
return this->ptr->port;
}
std::string get_uri() const
{
std::string uri = "kafka://";
uri += this->ptr->host;
uri += ":";
uri += std::to_string(this->ptr->port);
return uri;
}
int get_error()
{
return this->ptr->error;
}
public:
KafkaBroker()
{
this->ptr = new kafka_broker_t;
kafka_broker_init(this->ptr);
this->ref = new std::atomic<int>(1);
}
~KafkaBroker()
{
if (this->ref && --*this->ref == 0)
{
kafka_broker_deinit(this->ptr);
delete this->ptr;
delete this->ref;
}
}
KafkaBroker(kafka_broker_t *ptr)
{
this->ptr = ptr;
this->ref = NULL;
}
KafkaBroker(KafkaBroker&& move)
{
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_broker_t;
kafka_broker_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
KafkaBroker& operator= (KafkaBroker&& move)
{
if (this != &move)
{
this->~KafkaBroker();
this->ptr = move.ptr;
this->ref = move.ref;
move.ptr = new kafka_broker_t;
kafka_broker_init(move.ptr);
move.ref = new std::atomic<int>(1);
}
return *this;
}
KafkaBroker(const KafkaBroker& copy)
{
this->ptr = copy.ptr;
this->ref = copy.ref;
if (this->ref)
++*this->ref;
}
KafkaBroker& operator= (const KafkaBroker& copy)
{
this->~KafkaBroker();
this->ptr = copy.ptr;
this->ref = copy.ref;
if (this->ref)
++*this->ref;
return *this;
}
kafka_broker_t *get_raw_ptr() const { return this->ptr; }
struct list_head *get_list() { return &this->list; }
struct rb_node *get_rb() { return &this->rb; }
void set_feature(unsigned features) { this->ptr->features = features; }
bool is_equal(const struct sockaddr *addr, socklen_t socklen) const
{
if (this->ptr->addrlen == socklen && socklen)
return memcmp(addr, &this->ptr->addr, this->ptr->addrlen) == 0;
return false;
}
bool is_equal(const char *host, int port) const
{
if (port == this->ptr->port)
return strcmp(host, this->ptr->host) == 0;
return false;
}
bool is_equal(int node_id) const
{
return this->ptr->node_id == node_id;
}