forked from anubhavrohatgi/KClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kclient.h
396 lines (322 loc) · 8.57 KB
/
kclient.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
//
// Created by meox on 04/09/16.
//
#ifndef TICKETKAFKA_KCLIENT_H
#define TICKETKAFKA_KCLIENT_H
#include "util.h"
#include <mutex>
#include <thread>
#include <map>
#include <set>
#include <memory>
#include <functional>
#include <librdkafka/rdkafkacpp.h>
#include <iostream>
#include <future>
class SimplePartionerCb : public RdKafka::PartitionerCb {
public:
using p_fun_t = std::function<int32_t(const RdKafka::Topic *topic, const std::string *key, int32_t partition_cnt, void *msg_opaque)>;
int32_t partitioner_cb (const RdKafka::Topic *topic, const std::string *key,
int32_t partition_cnt, void *msg_opaque) override {
return p_fun(topic, key, partition_cnt, msg_opaque);
}
void setCallBack(p_fun_t f)
{
p_fun = move(f);
}
private:
p_fun_t p_fun;
};
class KRebalanceCb : public RdKafka::RebalanceCb
{
public:
KRebalanceCb() = default;
KRebalanceCb(KRebalanceCb&& rhs)
: partition_cnt(rhs.partition_cnt)
, eof_partition(rhs.eof_partition)
, f_rebalance(rhs.f_rebalance)
{}
void rebalance_cb (RdKafka::KafkaConsumer *consumer,
RdKafka::ErrorCode err,
std::vector<RdKafka::TopicPartition*> &partitions);
size_t get_partition_cnt() const { return partition_cnt; }
size_t get_eof_partition() const { return eof_partition; }
void inc_eof_partion() { eof_partition++; }
void reset_eof_partion() { eof_partition = 0; }
void print() const
{
std::cerr << "partition_cnt: " << partition_cnt << ", eof_partition = " << eof_partition << std::endl;
}
bool is_rebalanced() const
{
std::lock_guard<std::mutex> l{m};
return f_rebalance;
}
private:
void part_list_print (const std::vector<RdKafka::TopicPartition*>&partitions)
{
for (unsigned int i = 0 ; i < partitions.size() ; i++)
std::cerr << partitions[i]->topic() <<
"[" << partitions[i]->partition() << "], ";
std::cerr << "\n";
}
size_t partition_cnt{};
size_t eof_partition{};
mutable std::mutex m;
bool f_rebalance{false};
};
class KTopic
{
public:
KTopic(std::string topic_name, RdKafka::Topic *topic, RdKafka::Producer *producer) :
_topic_name{std::move(topic_name)}, _topic{topic}, _producer{producer}
{}
KTopic(std::string topic_name, RdKafka::Topic* topic, RdKafka::KafkaConsumer* consumer) :
_topic_name{std::move(topic_name)}, _topic{topic}, _consumer{consumer}
{}
RdKafka::ErrorCode produce(const std::string& msg, int32_t partition)
{
return _producer->produce(_topic, partition,
RdKafka::Producer::RK_MSG_COPY /* Copy payload */,
const_cast<char *>(msg.c_str()), msg.size(),
NULL, NULL);
}
bool sync_produce(const std::string& msg, int32_t partition)
{
while(true)
{
RdKafka::ErrorCode resp = produce(msg, partition);
if (resp == RdKafka::ERR__QUEUE_FULL)
_producer->poll(10);
else if (resp != RdKafka::ERR_NO_ERROR)
{
std::cerr << "> Producer error: " << RdKafka::err2str(resp) << "\n";
return false;
}
else
return true;
}
return false;
}
RdKafka::Metadata* metadata(int timeout_ms);
~KTopic() { delete _topic; }
void setPartionsInfo(const std::set<int32_t> &partions) { _partions = partions; }
size_t getNumPartions() const { return _partions.size(); }
const std::set<int32_t>& getPartions() const { return _partions; }
private:
std::string _topic_name{};
RdKafka::Topic* _topic{nullptr};
RdKafka::Producer* _producer{nullptr};
RdKafka::KafkaConsumer* _consumer{nullptr};
std::set<int32_t> _partions;
};
class KProducer
{
public:
KProducer(RdKafka::Producer *producer) : _producer{producer}
{}
KTopic create_topic(const std::string& topic_str);
std::string name() const { return _producer->name(); }
void setTopicConf(RdKafka::Conf *pConf) { topic_conf = pConf; }
int outq_len() const { return _producer->outq_len(); }
int poll(int timeout) const { return _producer->poll(timeout); }
void setPartionsInfo(const std::map<std::string, std::set<int32_t>>* map_part)
{
map_partions = map_part;
}
~KProducer() { delete _producer; }
private:
RdKafka::Producer* _producer{nullptr};
RdKafka::Conf* topic_conf{nullptr};
const std::map<std::string, std::set<int32_t>>* map_partions{nullptr};
};
struct message_raii{
RdKafka::Message* data{nullptr};
~message_raii()
{
delete data;
}
};
class KConsumer
{
public:
KConsumer(RdKafka::KafkaConsumer *consumer) : _consumer{consumer}
{}
void setTopicConf(RdKafka::Conf *pConf) { topic_conf = pConf; }
KTopic create_topic(const std::string& topic_str);
void subscribe(const std::vector<std::string>& topics)
{
_consumer->subscribe(topics);
}
RdKafka::Message* consume(int time_out)
{
return _consumer->consume(time_out);
}
void commit()
{
_consumer->commitSync();
}
void reset_eof_partion()
{
rebalance_cb->reset_eof_partion();
}
void wait_rebalance()
{
while (!rebalance_cb->is_rebalanced())
{
_consumer->poll(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
template<typename F, typename E>
void for_each(int time_out, F && f, E && err)
{
while(true)
{
message_raii message;
message.data = consume(time_out);
if (message.data == nullptr)
break;
const auto msg_err = message.data->err();
switch (message.data->err())
{
case RdKafka::ERR__TIMED_OUT:
{
//std::cerr << "Timeout" << std::endl;
break;
}
case RdKafka::ERR_NO_ERROR:
{
/* Real message */
f(*message.data);
break;
}
case RdKafka::ERR__PARTITION_EOF:
{
rebalance_cb->inc_eof_partion();
rebalance_cb->print();
if (rebalance_cb->get_eof_partition() == rebalance_cb->get_partition_cnt()
&& err(*message.data, msg_err)) {
return;
}
break;
}
case RdKafka::ERR__UNKNOWN_TOPIC:
case RdKafka::ERR__UNKNOWN_PARTITION:
{
std::cerr << "Consume failed: " << message.data->errstr() << std::endl;
if (err(*message.data, msg_err))
return;
break;
}
default:
{
/* Errors */
std::cerr << "Consume failed: " << message.data->errstr() << std::endl;
if (err(*message.data, msg_err))
return;
}
}
}
}
std::string name() const { return _consumer->name(); }
int poll(int timeout_ms) { return _consumer->poll(timeout_ms); }
~KConsumer() { delete _consumer; }
void setRebalanceCb(KRebalanceCb* rebalance)
{
rebalance_cb = rebalance;
}
void close();
private:
RdKafka::KafkaConsumer* _consumer{nullptr};
RdKafka::Conf* topic_conf{nullptr};
KRebalanceCb* rebalance_cb{nullptr};
};
class KEventCb : public RdKafka::EventCb
{
public:
void event_cb (RdKafka::Event &event);
private:
bool run{false};
};
class KClient
{
public:
KClient()
{
conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
topic_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
}
KClient(KClient&& rhs)
: event_cb(std::move(rhs.event_cb))
, rebalance_cb(std::move(rhs.rebalance_cb))
, conf{rhs.conf}
, topic_conf{rhs.topic_conf}
, brokers(std::move(rhs.brokers))
, map_partions(std::move(rhs.map_partions))
{
}
KClient(std::string i_brokers)
: KClient()
{
brokers = std::move(i_brokers);
setGlobalConf("bootstrap.servers", brokers);
setGlobalConf("metadata.broker.list", brokers);
setConf(conf, "event_cb", &event_cb);
setConf(conf, "rebalance_cb", &rebalance_cb);
default_topic_conf();
}
bool setGlobalConf(const std::string& param, const std::string& val)
{
return setConf(conf, param, val);
}
bool setTopicConf(const std::string& param, const std::string& val)
{
return setConf(topic_conf, param, val);
}
bool setPartioner(RdKafka::PartitionerCb& partioner);
bool setPartioner(SimplePartionerCb::p_fun_t fun)
{
partioner.setCallBack(fun);
return setPartioner(partioner);
}
KProducer create_producer();
KConsumer create_consumer();
bool loadMetadata(const std::string& topic_str = "");
void default_topic_conf();
protected:
bool setConf(RdKafka::Conf * p_conf, const std::string& param, const std::string& val)
{
if (!p_conf)
return false;
std::string errstr;
if (p_conf->set(param, val, errstr) != RdKafka::Conf::CONF_OK)
{
std::cerr << errstr << std::endl;
return false;
}
return true;
}
template <typename T>
bool setConf(RdKafka::Conf * p_conf, const std::string& param, T* val)
{
if (!p_conf)
return false;
std::string errstr;
if (p_conf->set(param, val, errstr) != RdKafka::Conf::CONF_OK)
{
std::cerr << errstr << std::endl;
return false;
}
return true;
}
private:
KEventCb event_cb;
KRebalanceCb rebalance_cb;
RdKafka::Conf *conf;
RdKafka::Conf *topic_conf;
std::string brokers;
std::map<std::string, std::set<int32_t>> map_partions;
SimplePartionerCb partioner;
};
#endif //TICKETKAFKA_KCLIENT_H