forked from salebab/phpkafka
-
Notifications
You must be signed in to change notification settings - Fork 51
/
kafka.c
1242 lines (1170 loc) · 37.6 KB
/
kafka.c
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 2015 Elias Van Ootegem.
*
* 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.
*
* Special thanks to Patrick Reilly and Aleksandar Babic for their work
* On which this extension was actually built.
*/
#include <php.h>
#include "kafka.h"
#include <php_kafka.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#include "kafka.h"
#include "librdkafka/rdkafka.h"
struct consume_cb_params {
int read_count;
zval *return_value;
union {
int *partition_ends;
long *partition_offset;
};
int error_count;
int eop;
int auto_commit;
};
struct produce_cb_params {
int msg_count;
int err_count;
int offset;
int partition;
int errmsg_len;
char *err_msg;
};
static int log_level = 1;
static rd_kafka_t *rk = NULL;
static rd_kafka_type_t rk_type;
char *brokers = "localhost:9092";
int partition = RD_KAFKA_PARTITION_UA;
void kafka_connect(char *brokers)
{
kafka_setup(brokers);
}
void kafka_set_log_level( int ll )
{
log_level = ll;
}
void kafka_msg_delivered (rd_kafka_t *rk,
void *payload, size_t len,
int error_code,
void *opaque, void *msg_opaque)
{
if (error_code && log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - Message delivery failed: %s",
rd_kafka_err2str(error_code));
}
}
void kafka_err_cb (rd_kafka_t *rk, int err, const char *reason, void *opaque)
{
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - ERROR CALLBACK: %s: %s: %s\n",
rd_kafka_name(rk), rd_kafka_err2str(err), reason);
}
if (rk)
rd_kafka_destroy(rk);
}
void kafka_produce_cb_simple(rd_kafka_t *rk, void *payload, size_t len, int err_code, void *opaque, void *msg_opaque)
{
struct produce_cb_params *params = msg_opaque;
if (params)
{
params->msg_count -=1;
}
if (log_level)
{
if (params)
params->err_count += 1;
openlog("phpkafka", 0, LOG_USER);
if (err_code)
syslog(LOG_ERR, "Failed to deliver message %s: %s", (char *) payload, rd_kafka_err2str(err_code));
else
syslog(LOG_DEBUG, "Successfuly delevired message (%zd bytes)", len);
}
}
void kafka_produce_detailed_cb(rd_kafka_t *rk, const rd_kafka_message_t *msg, void *opaque)
{
struct produce_cb_params *params = opaque;
if (params)
{
params->msg_count -= 1;
}
if (msg->err)
{
int offset = params->errmsg_len,
err_len = 0;
const char *errstr = rd_kafka_message_errstr(msg);
err_len = strlen(errstr);
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to deliver message: %s", errstr);
}
if (params)
{
params->err_count += 1;
params->err_msg = realloc(
params->err_msg,
(offset + err_len + 2) * sizeof params->err_msg
);
if (params->err_msg == NULL)
{
params->errmsg_len = 0;
}
else
{
strcpy(
params->err_msg + offset,
errstr
);
offset += err_len;//get new strlen
params->err_msg[offset] = '\n';//add new line
++offset;
params->err_msg[offset] = '\0';//ensure zero terminated string
}
}
return;
}
if (params)
{
params->offset = msg->offset;
params->partition = msg->partition;
}
}
rd_kafka_t *kafka_get_connection(kafka_connection_params params, const char *brokers)
{
rd_kafka_t *r = NULL;
char errstr[512];
rd_kafka_conf_t *conf = rd_kafka_conf_new();
//set error callback
rd_kafka_conf_set_error_cb(conf, kafka_err_cb);
if (params.type == RD_KAFKA_CONSUMER)
{
if (params.queue_buffer)
rd_kafka_conf_set(conf, "queued.min.messages", params.queue_buffer, NULL, 0);
r = rd_kafka_new(params.type, conf, errstr, sizeof errstr);
if (!r)
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to connect to kafka: %s", errstr);
}
//destroy config, no connection to use it...
rd_kafka_conf_destroy(conf);
return NULL;
}
if (!rd_kafka_brokers_add(r, brokers))
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to connect to brokers %s", brokers);
}
rd_kafka_destroy(r);
return NULL;
}
return r;
}
if (params.compression)
{
rd_kafka_conf_res_t result = rd_kafka_conf_set(
conf, "compression.codec",params.compression, errstr, sizeof errstr
);
if (result != RD_KAFKA_CONF_OK)
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ALERT, "Failed to set compression %s: %s", params.compression, errstr);
}
rd_kafka_conf_destroy(conf);
return NULL;
}
}
if (params.retry_count)
{
rd_kafka_conf_res_t result = rd_kafka_conf_set(
conf, "message.send.max.retries",params.retry_count, errstr, sizeof errstr
);
if (result != RD_KAFKA_CONF_OK)
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ALERT, "Failed to set compression %s: %s", params.compression, errstr);
}
rd_kafka_conf_destroy(conf);
return NULL;
}
}
if (params.retry_interval)
{
rd_kafka_conf_res_t result = rd_kafka_conf_set(
conf, "retry.backoff.ms",params.retry_interval, errstr, sizeof errstr
);
if (result != RD_KAFKA_CONF_OK)
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ALERT, "Failed to set compression %s: %s", params.compression, errstr);
}
rd_kafka_conf_destroy(conf);
return NULL;
}
}
if (params.reporting == 1)
rd_kafka_conf_set_dr_cb(conf, kafka_produce_cb_simple);
else if (params.reporting == 2)
rd_kafka_conf_set_dr_msg_cb(conf, kafka_produce_detailed_cb);
r = rd_kafka_new(params.type, conf, errstr, sizeof errstr);
if (!r)
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to connect to kafka: %s", errstr);
}
//destroy config, no connection to use it...
rd_kafka_conf_destroy(conf);
return NULL;
}
if (!rd_kafka_brokers_add(r, brokers))
{
if (params.log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to connect to brokers %s", brokers);
}
rd_kafka_destroy(r);
return NULL;
}
return r;
}
rd_kafka_t *kafka_set_connection(rd_kafka_type_t type, const char *b, int report_level, const char *compression)
{
rd_kafka_t *r = NULL;
char *tmp = brokers;
char errstr[512];
rd_kafka_conf_t *conf = rd_kafka_conf_new();
if (!(r = rd_kafka_new(type, conf, errstr, sizeof(errstr)))) {
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - failed to create new producer: %s", errstr);
}
exit(1);
}
/* Add brokers */
if (rd_kafka_brokers_add(r, b) == 0) {
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "php kafka - No valid brokers specified");
}
exit(1);
}
/* Set up a message delivery report callback.
* It will be called once for each message, either on successful
* delivery to broker, or upon failure to deliver to broker. */
if (type == RD_KAFKA_PRODUCER)
{
if (compression && !strcmp(compression, "none"))
{//silently fail on error ATM...
if (RD_KAFKA_CONF_OK != rd_kafka_conf_set(conf, "compression.codec", compression, errstr, sizeof errstr))
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "Failed to set compression to %s", compression);
}
}
}
if (report_level == 1)
rd_kafka_conf_set_dr_cb(conf, kafka_produce_cb_simple);
else if (report_level == 2)
rd_kafka_conf_set_dr_msg_cb(conf, kafka_produce_detailed_cb);
}
rd_kafka_conf_set_error_cb(conf, kafka_err_cb);
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - using: %s", brokers);
}
return r;
}
void kafka_set_partition(int partition_selected)
{
partition = partition_selected;
}
void kafka_setup(char* brokers_list)
{
brokers = brokers_list;
}
void kafka_destroy(rd_kafka_t *r, int timeout)
{
if(r != NULL)
{
//poll handle status
rd_kafka_poll(r, 0);
if (rd_kafka_outq_len(r) > 0)
{//wait for out-queue to clear
while(rd_kafka_outq_len(r) > 0)
rd_kafka_poll(r, timeout);
timeout = 1;
}
rd_kafka_destroy(r);
//this wait is blocking PHP
//not calling it will yield segfault, though
rd_kafka_wait_destroyed(timeout);
r = NULL;
}
}
//We're no longer relying on the global rk variable (not thread-safe)
static void kafka_init( rd_kafka_type_t type )
{
if (rk && type != rk_type)
{
rd_kafka_destroy(rk);
rk = NULL;
}
if (rk == NULL)
{
char errstr[512];
rd_kafka_conf_t *conf = rd_kafka_conf_new();
if (!(rk = rd_kafka_new(type, conf, errstr, sizeof(errstr)))) {
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - failed to create new producer: %s", errstr);
}
exit(1);
}
/* Add brokers */
if (rd_kafka_brokers_add(rk, brokers) == 0) {
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "php kafka - No valid brokers specified");
}
exit(1);
}
/* Set up a message delivery report callback.
* It will be called once for each message, either on successful
* delivery to broker, or upon failure to deliver to broker. */
if (type == RD_KAFKA_PRODUCER)
rd_kafka_conf_set_dr_cb(conf, kafka_produce_cb_simple);
rd_kafka_conf_set_error_cb(conf, kafka_err_cb);
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - using: %s", brokers);
}
}
}
int kafka_produce_report(rd_kafka_t *r, const char *topic, char *msg, int msg_len, long timeout)
{
char errstr[512];
rd_kafka_topic_t *rkt = NULL;
int partition = RD_KAFKA_PARTITION_UA;
rd_kafka_topic_conf_t *conf = NULL;
struct produce_cb_params pcb = {1, 0, 0, 0, 0, NULL};
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "No connection provided to produce to topic %s", topic);
}
return -2;
}
/* Topic configuration */
conf = rd_kafka_topic_conf_new();
rd_kafka_topic_conf_set(conf,"produce.offset.report", "true", errstr, sizeof errstr );
char timeoutStr[64];
snprintf(timeoutStr, 64, "%lu", timeout);
if (rd_kafka_topic_conf_set(conf, "message.timeout.ms", timeoutStr, errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(
LOG_ERR,
"Failed to configure topic param 'message.timeout.ms' to %lu before producing; config err was: %s",
timeout,
errstr
);
}
rd_kafka_topic_conf_destroy(conf);
return -3;
}
//callback already set in kafka_set_connection
rkt = rd_kafka_topic_new(r, topic, conf);
if (!rkt)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to open topic %s", topic);
}
rd_kafka_topic_conf_destroy(conf);
return -1;
}
//begin producing:
if (rd_kafka_produce(rkt, partition, RD_KAFKA_MSG_F_COPY, msg, msg_len,NULL, 0,&pcb) == -1)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "Failed to produce message: %s", rd_kafka_err2str(rd_kafka_errno2err(errno)));
}
//handle delivery response (callback)
rd_kafka_poll(rk, 0);
rd_kafka_topic_destroy(rkt);
return -1;
}
rd_kafka_poll(rk, 0);
while(pcb.msg_count && rd_kafka_outq_len(r) > 0)
rd_kafka_poll(r, 10);
rd_kafka_topic_destroy(rkt);
return 0;
}
int kafka_produce_batch(rd_kafka_t *r, char *topic, char **msg, int *msg_len, int msg_cnt, int report, long timeout)
{
char errstr[512];
rd_kafka_topic_t *rkt;
struct produce_cb_params pcb = {msg_cnt, 0, 0, 0, 0, NULL};
void *opaque;
int partition = RD_KAFKA_PARTITION_UA;
int i,
err_cnt = 0;
if (report)
opaque = &pcb;
else
opaque = NULL;
rd_kafka_topic_conf_t *topic_conf;
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "phpkafka - no connection to produce to topic: %s", topic);
}
return -2;
}
/* Topic configuration */
topic_conf = rd_kafka_topic_conf_new();
char timeoutStr[64];
snprintf(timeoutStr, 64, "%lu", timeout);
if (rd_kafka_topic_conf_set(topic_conf, "message.timeout.ms", timeoutStr, errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(
LOG_ERR,
"Failed to configure topic param 'message.timeout.ms' to %lu before producing; config err was: %s",
timeout,
errstr
);
}
rd_kafka_topic_conf_destroy(topic_conf);
return -3;
}
/* Create topic */
rkt = rd_kafka_topic_new(r, topic, topic_conf);
//do we have VLA?
rd_kafka_message_t *messages = calloc(sizeof *messages, msg_cnt);
if (messages == NULL)
{//fallback to individual produce calls
for (i=0;i<msg_cnt;++i)
{
if (rd_kafka_produce(rkt, partition, RD_KAFKA_MSG_F_COPY, msg[i], msg_len[i], NULL, 0, opaque) == -1)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - %% Failed to produce to topic %s "
"partition %i: %s",
rd_kafka_topic_name(rkt), partition,
rd_kafka_err2str(
rd_kafka_errno2err(errno)));
}
}
}
}
else
{
for (i=0;i<msg_cnt;++i)
{
messages[i].payload = msg[i];
messages[i].len = msg_len[i];
}
i = rd_kafka_produce_batch(rkt, partition, RD_KAFKA_MSG_F_COPY, messages, msg_cnt);
if (i < msg_cnt)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_WARNING, "Failed to queue full message batch, %d of %d were put in queue", i, msg_cnt);
}
}
err_cnt = msg_cnt - i;
free(messages);
messages = NULL;
}
/* Poll to handle delivery reports */
rd_kafka_poll(r, 0);
/* Wait for messages to be delivered */
while (report && pcb.msg_count && rd_kafka_outq_len(r) > 0)
rd_kafka_poll(r, 10);
//set global to NULL again
rd_kafka_topic_destroy(rkt);
if (report)
err_cnt = pcb.err_count;
return err_cnt;
}
int kafka_produce(rd_kafka_t *r, char* topic, char* msg, int msg_len, int report, long timeout)
{
char errstr[512];
rd_kafka_topic_t *rkt;
struct produce_cb_params pcb = {1, 0, 0, 0, 0, NULL};
void *opaque;
int partition = RD_KAFKA_PARTITION_UA;
//decide whether to pass callback params or not...
if (report)
opaque = &pcb;
else
opaque = NULL;
rd_kafka_topic_conf_t *topic_conf;
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "phpkafka - no connection to produce to topic: %s", topic);
}
return -2;
}
/* Topic configuration */
topic_conf = rd_kafka_topic_conf_new();
char timeoutStr[64];
snprintf(timeoutStr, 64, "%lu", timeout);
if (rd_kafka_topic_conf_set(topic_conf, "message.timeout.ms", timeoutStr, errstr, sizeof(errstr)) != RD_KAFKA_CONF_OK)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(
LOG_ERR,
"Failed to configure topic param 'message.timeout.ms' to %lu before producing; config err was: %s",
timeout,
errstr
);
}
rd_kafka_topic_conf_destroy(topic_conf);
return -3;
}
/* Create topic */
rkt = rd_kafka_topic_new(r, topic, topic_conf);
if (rd_kafka_produce(rkt, partition,
RD_KAFKA_MSG_F_COPY,
/* Payload and length */
msg, msg_len,
/* Optional key and its length */
NULL, 0,
/* Message opaque, provided in
* delivery report callback as
* msg_opaque. */
opaque) == -1) {
if (log_level) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - %% Failed to produce to topic %s "
"partition %i: %s",
rd_kafka_topic_name(rkt), partition,
rd_kafka_err2str(
rd_kafka_errno2err(errno)));
}
rd_kafka_topic_destroy(rkt);
return -1;
}
/* Poll to handle delivery reports */
rd_kafka_poll(r, 0);
/* Wait for messages to be delivered */
while (report && pcb.msg_count && rd_kafka_outq_len(r) > 0)
rd_kafka_poll(r, 10);
//set global to NULL again
rd_kafka_topic_destroy(rkt);
return 0;
}
static
void offset_queue_consume(rd_kafka_message_t *message, void *opaque)
{
struct consume_cb_params *params = opaque;
if (params->eop == 0)
return;
if (message->err)
{
params->error_count += 1;
if (params->auto_commit == 0)
rd_kafka_offset_store(
message->rkt,
message->partition,
message->offset == 0 ? 0 : message->offset -1
);
if (message->err == RD_KAFKA_RESP_ERR__PARTITION_EOF)
{
if (params->partition_offset[message->partition] == -2)
{//no previous message read from this partition
//set offset value to last possible value (-1 or last existing)
//reduce eop count
params->eop -= 1;
params->read_count += 1;
params->partition_offset[message->partition] = message->offset -1;
}
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO,
"phpkafka - %% Consumer reached end of %s [%"PRId32"] "
"message queue at offset %"PRId64"\n",
rd_kafka_topic_name(message->rkt),
message->partition, message->offset);
}
}
return;
}
if (params->partition_offset[message->partition] == -1)
params->eop -= 1;
//we have an offset, save it
params->partition_offset[message->partition] = message->offset;
//tally read_count
params->read_count += 1;
if (params->auto_commit == 0)
rd_kafka_offset_store(
message->rkt,
message->partition,
message->offset == 0 ? 0 : message->offset -1
);
}
static
void queue_consume(rd_kafka_message_t *message, void *opaque)
{
struct consume_cb_params *params = opaque;
zval *return_value = params->return_value;
//all partitions EOF
if (params->eop < 1)
return;
//nothing more to read...
if (params->read_count == 0)
return;
if (message->err)
{
params->error_count += 1;
//if auto-commit is disabled:
if (params->auto_commit == 0)
//store offset
rd_kafka_offset_store(
message->rkt,
message->partition,
message->offset == 0 ? 0 : message->offset -1
);
if (message->err == RD_KAFKA_RESP_ERR__PARTITION_EOF)
{
if (params->partition_ends[message->partition] == 0)
{
params->eop -= 1;
params->partition_ends[message->partition] = 1;
}
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO,
"phpkafka - %% Consumer reached end of %s [%"PRId32"] "
"message queue at offset %"PRId64"\n",
rd_kafka_topic_name(message->rkt),
message->partition, message->offset);
}
return;
}
//add_next_index_string(return_value, rd_kafka_message_errstr(message), 1);
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - %% Consume error for topic \"%s\" [%"PRId32"] "
"offset %"PRId64": %s\n",
rd_kafka_topic_name(message->rkt),
message->partition,
message->offset,
rd_kafka_message_errstr(message)
);
}
return;
}
//only count successful reads!
//-1 means read all from offset until end
if (params->read_count != -1)
params->read_count -= 1;
//add message to return value (perhaps add as array -> offset + msg?
if (message->len > 0) {
add_next_index_stringl(
return_value,
(char *) message->payload,
(int) message->len,
1
);
} else {
add_next_index_string(return_value, "", 1);
}
//store offset if autocommit is disabled
if (params->auto_commit == 0)
rd_kafka_offset_store(
message->rkt,
message->partition,
message->offset
);
}
static rd_kafka_message_t *msg_consume(rd_kafka_message_t *rkmessage,
void *opaque)
{
int *run = opaque;
if (rkmessage->err)
{
*run = 0;
if (rkmessage->err == RD_KAFKA_RESP_ERR__PARTITION_EOF)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO,
"phpkafka - %% Consumer reached end of %s [%"PRId32"] "
"message queue at offset %"PRId64"\n",
rd_kafka_topic_name(rkmessage->rkt),
rkmessage->partition, rkmessage->offset);
}
return NULL;
}
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - %% Consume error for topic \"%s\" [%"PRId32"] "
"offset %"PRId64": %s\n",
rd_kafka_topic_name(rkmessage->rkt),
rkmessage->partition,
rkmessage->offset,
rd_kafka_message_errstr(rkmessage)
);
}
return NULL;
}
return rkmessage;
}
//get topics + partition count
void kafka_get_topics(rd_kafka_t *r, zval *return_value)
{
int i;
const struct rd_kafka_metadata *meta = NULL;
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "phpkafka - no connection to get topics");
}
return;
}
if (RD_KAFKA_RESP_ERR_NO_ERROR == rd_kafka_metadata(r, 1, NULL, &meta, 200)) {
for (i=0;i<meta->topic_cnt;++i) {
add_assoc_long(
return_value,
meta->topics[i].topic,
(long) meta->topics[i].partition_cnt
);
}
}
if (meta) {
rd_kafka_metadata_destroy(meta);
}
}
static
int kafka_partition_count(rd_kafka_t *r, const char *topic)
{
rd_kafka_topic_t *rkt;
rd_kafka_topic_conf_t *conf;
int i;//C89 compliant
//connect as consumer if required
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "phpkafka - no connection to get partition count for topic: %s", topic);
}
return -1;
}
/* Topic configuration */
conf = rd_kafka_topic_conf_new();
/* Create topic */
rkt = rd_kafka_topic_new(r, topic, conf);
//metadata API required rd_kafka_metadata_t** to be passed
const struct rd_kafka_metadata *meta = NULL;
if (RD_KAFKA_RESP_ERR_NO_ERROR == rd_kafka_metadata(r, 0, rkt, &meta, 200))
i = (int) meta->topics->partition_cnt;
else
i = 0;
if (meta) {
rd_kafka_metadata_destroy(meta);
}
rd_kafka_topic_destroy(rkt);
return i;
}
//get the available partitions for a given topic
void kafka_get_partitions(rd_kafka_t *r, zval *return_value, char *topic)
{
//we need a connection!
if (r == NULL)
return;
int i, count = kafka_partition_count(r, topic);
for (i=0;i<count;++i) {
add_next_index_long(return_value, i);
}
}
/**
* @brief Get all partitions for topic and their beginning offsets, useful
* if we're consuming messages without knowing the actual partition beforehand
* @param int **partitions should be pointer to NULL, will be allocated here
* @param const char * topic topic name
* @return int (0 == meta error, -2: no connection, -1: allocation error, all others indicate success (nr of elems in array))
*/
int kafka_partition_offsets(rd_kafka_t *r, long **partitions, const char *topic)
{
rd_kafka_topic_t *rkt = NULL;
rd_kafka_topic_conf_t *conf = NULL;
rd_kafka_queue_t *rkqu = NULL;
struct consume_cb_params cb_params = {0, NULL, NULL, 0, 0, 0};
int i = 0;
//make life easier, 1 level of indirection...
long *values = *partitions;
//connect as consumer if required
if (r == NULL)
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR, "phpkafka - no connection to get offsets of topic: %s", topic);
}
return -2;
}
/* Topic configuration */
conf = rd_kafka_topic_conf_new();
/* Create topic */
rkt = rd_kafka_topic_new(r, topic, conf);
rkqu = rd_kafka_queue_new(rk);
const struct rd_kafka_metadata *meta = NULL;
if (RD_KAFKA_RESP_ERR_NO_ERROR == rd_kafka_metadata(r, 0, rkt, &meta, 5))
{
values = realloc(values, meta->topics->partition_cnt * sizeof *values);
if (values == NULL)
{
*partitions = values;//possible corrupted pointer now
//free metadata, return error
rd_kafka_metadata_destroy(meta);
return -1;
}
//we need eop to reach 0, if there are 4 partitions, start at 3 (0, 1, 2, 3)
cb_params.eop = meta->topics->partition_cnt -1;
cb_params.partition_offset = values;
for (i=0;i<meta->topics->partition_cnt;++i)
{
//initialize: set to -2 for callback
values[i] = -2;
if (rd_kafka_consume_start_queue(rkt, meta->topics->partitions[i].id, RD_KAFKA_OFFSET_BEGINNING, rkqu))
{
if (log_level)
{
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_ERR,
"Failed to start consuming topic %s [%"PRId32"]",
topic, meta->topics->partitions[i].id
);
}
continue;
}
}
//eiter eop reached 0, or the read errors >= nr of partitions
//either way, we've consumed a message from each partition, and therefore, we're done
while(cb_params.eop && cb_params.error_count < meta->topics->partition_cnt)
rd_kafka_consume_callback_queue(rkqu, 100, offset_queue_consume, &cb_params);
//stop consuming for all partitions
for (i=0;i<meta->topics->partition_cnt;++i)
rd_kafka_consume_stop(rkt, meta->topics[0].partitions[i].id);
rd_kafka_queue_destroy(rkqu);
//do we need this poll here?
while(rd_kafka_outq_len(r) > 0)
rd_kafka_poll(r, 5);
//let's be sure to pass along the correct values here...
*partitions = values;
i = meta->topics->partition_cnt;
}
if (meta)
rd_kafka_metadata_destroy(meta);
rd_kafka_topic_destroy(rkt);
return i;
}
void kafka_consume_all(rd_kafka_t *rk, zval *return_value, const char *topic, const char *offset, int item_count)
{
char errstr[512];
rd_kafka_topic_t *rkt;
rd_kafka_topic_conf_t *conf;
const struct rd_kafka_metadata *meta = NULL;
rd_kafka_queue_t *rkqu = NULL;
int current, p, i = 0;
int32_t partition = 0;
int64_t start;
struct consume_cb_params cb_params = {item_count, return_value, NULL, 0, 0, 0};
//check for NULL pointers, all arguments are required!
if (rk == NULL || return_value == NULL || topic == NULL || offset == NULL || strlen(offset) == 0)
return;