forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rdkafka_msgset_reader.c
1218 lines (1023 loc) · 50.4 KB
/
rdkafka_msgset_reader.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
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2017 Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @name MessageSet reader interface
*
* Parses FetchResponse for Messages
*
*
* @remark
* The broker may send partial messages, when this happens we bail out
* silently and keep the messages that we successfully parsed.
*
* "A Guide To The Kafka Protocol" states:
* "As an optimization the server is allowed to
* return a partial message at the end of the
* message set.
* Clients should handle this case."
*
* We're handling it by not passing the error upstream.
* This is why most err_parse: goto labels (that are called from buf parsing
* macros) suppress the error message and why log_decode_errors is off
* unless PROTOCOL debugging is enabled.
*
* When a FetchResponse contains multiple partitions, each partition's
* MessageSet may be partial, regardless of the other partitions.
* To make sure the next partition can be parsed, each partition parse
* uses its own sub-slice of only that partition's MessageSetSize length.
*/
#include "rd.h"
#include "rdkafka_int.h"
#include "rdkafka_msg.h"
#include "rdkafka_msgset.h"
#include "rdkafka_topic.h"
#include "rdkafka_partition.h"
#include "rdkafka_header.h"
#include "rdkafka_lz4.h"
#include "rdvarint.h"
#include "crc32c.h"
#if WITH_ZLIB
#include "rdgz.h"
#endif
#if WITH_SNAPPY
#include "snappy.h"
#endif
#if WITH_ZSTD
#include "rdkafka_zstd.h"
#endif
struct msgset_v2_hdr {
int64_t BaseOffset;
int32_t Length;
int32_t PartitionLeaderEpoch;
int8_t MagicByte;
int32_t Crc;
int16_t Attributes;
int32_t LastOffsetDelta;
int64_t BaseTimestamp;
int64_t MaxTimestamp;
int64_t PID;
int16_t ProducerEpoch;
int32_t BaseSequence;
int32_t RecordCount;
};
typedef struct rd_kafka_msgset_reader_s {
rd_kafka_buf_t *msetr_rkbuf; /**< Response read buffer */
int msetr_relative_offsets; /**< Bool: using relative offsets */
/**< Outer/wrapper Message fields. */
struct {
int64_t offset; /**< Relative_offsets: outer message's
* Offset (last offset) */
rd_kafka_timestamp_type_t tstype; /**< Compressed
* MessageSet's
* timestamp type. */
int64_t timestamp; /**< ... timestamp*/
} msetr_outer;
struct msgset_v2_hdr *msetr_v2_hdr; /**< MessageSet v2 header */
const struct rd_kafka_toppar_ver *msetr_tver; /**< Toppar op version of
* request. */
rd_kafka_broker_t *msetr_rkb; /* @warning Not a refcounted
* reference! */
rd_kafka_toppar_t *msetr_rktp; /* @warning Not a refcounted
* reference! */
int msetr_msgcnt; /**< Number of messages in rkq */
int64_t msetr_msg_bytes; /**< Number of bytes in rkq */
rd_kafka_q_t msetr_rkq; /**< Temp Message and error queue */
rd_kafka_q_t *msetr_par_rkq; /**< Parent message and error queue,
* the temp msetr_rkq will be moved
* to this queue when parsing
* is done.
* Refcount is not increased. */
int64_t msetr_next_offset; /**< Next offset to fetch after
* this reader run is done.
* Optional: only used for special
* cases where the per-message offset
* can't be relied on for next
* fetch offset, such as with
* compacted topics. */
int msetr_ctrl_cnt; /**< Number of control messages
* or MessageSets received. */
const char *msetr_srcname; /**< Optional message source string,
* used in debug logging to
* indicate messages were
* from an inner compressed
* message set.
* Not freed (use const memory).
* Add trailing space. */
} rd_kafka_msgset_reader_t;
/* Forward declarations */
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_run (rd_kafka_msgset_reader_t *msetr);
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_msgs_v2 (rd_kafka_msgset_reader_t *msetr);
/**
* @brief Set up a MessageSet reader but don't start reading messages.
*/
static void
rd_kafka_msgset_reader_init (rd_kafka_msgset_reader_t *msetr,
rd_kafka_buf_t *rkbuf,
rd_kafka_toppar_t *rktp,
const struct rd_kafka_toppar_ver *tver,
rd_kafka_q_t *par_rkq) {
memset(msetr, 0, sizeof(*msetr));
msetr->msetr_rkb = rkbuf->rkbuf_rkb;
msetr->msetr_rktp = rktp;
msetr->msetr_tver = tver;
msetr->msetr_rkbuf = rkbuf;
msetr->msetr_srcname = "";
rkbuf->rkbuf_uflow_mitigation = "truncated response from broker (ok)";
/* All parsed messages are put on this temporary op
* queue first and then moved in one go to the real op queue. */
rd_kafka_q_init(&msetr->msetr_rkq, msetr->msetr_rkb->rkb_rk);
/* Make sure enqueued ops get the correct serve/opaque reflecting the
* original queue. */
msetr->msetr_rkq.rkq_serve = par_rkq->rkq_serve;
msetr->msetr_rkq.rkq_opaque = par_rkq->rkq_opaque;
/* Keep (non-refcounted) reference to parent queue for
* moving the messages and events in msetr_rkq to when
* parsing is done. */
msetr->msetr_par_rkq = par_rkq;
}
/**
* @brief Decompress MessageSet, pass the uncompressed MessageSet to
* the MessageSet reader.
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_decompress (rd_kafka_msgset_reader_t *msetr,
int MsgVersion, int Attributes,
int64_t Timestamp, int64_t Offset,
const void *compressed,
size_t compressed_size) {
struct iovec iov = { .iov_base = NULL, .iov_len = 0 };
rd_kafka_toppar_t *rktp = msetr->msetr_rktp;
int codec = Attributes & RD_KAFKA_MSG_ATTR_COMPRESSION_MASK;
rd_kafka_resp_err_t err = RD_KAFKA_RESP_ERR_NO_ERROR;
rd_kafka_buf_t *rkbufz;
switch (codec)
{
#if WITH_ZLIB
case RD_KAFKA_COMPRESSION_GZIP:
{
uint64_t outlenx = 0;
/* Decompress Message payload */
iov.iov_base = rd_gz_decompress(compressed, (int)compressed_size,
&outlenx);
if (unlikely(!iov.iov_base)) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "GZIP",
"Failed to decompress Gzip "
"message at offset %"PRId64
" of %"PRIusz" bytes: "
"ignoring message",
Offset, compressed_size);
err = RD_KAFKA_RESP_ERR__BAD_COMPRESSION;
goto err;
}
iov.iov_len = (size_t)outlenx;
}
break;
#endif
#if WITH_SNAPPY
case RD_KAFKA_COMPRESSION_SNAPPY:
{
const char *inbuf = compressed;
size_t inlen = compressed_size;
int r;
static const unsigned char snappy_java_magic[] =
{ 0x82, 'S','N','A','P','P','Y', 0 };
static const size_t snappy_java_hdrlen = 8+4+4;
/* snappy-java adds its own header (SnappyCodec)
* which is not compatible with the official Snappy
* implementation.
* 8: magic, 4: version, 4: compatible
* followed by any number of chunks:
* 4: length
* ...: snappy-compressed data. */
if (likely(inlen > snappy_java_hdrlen + 4 &&
!memcmp(inbuf, snappy_java_magic, 8))) {
/* snappy-java framing */
char errstr[128];
inbuf = inbuf + snappy_java_hdrlen;
inlen -= snappy_java_hdrlen;
iov.iov_base = rd_kafka_snappy_java_uncompress(
inbuf, inlen,
&iov.iov_len,
errstr, sizeof(errstr));
if (unlikely(!iov.iov_base)) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "SNAPPY",
"%s [%"PRId32"]: "
"Snappy decompression for message "
"at offset %"PRId64" failed: %s: "
"ignoring message",
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
Offset, errstr);
err = RD_KAFKA_RESP_ERR__BAD_COMPRESSION;
goto err;
}
} else {
/* No framing */
/* Acquire uncompressed length */
if (unlikely(!rd_kafka_snappy_uncompressed_length(
inbuf, inlen, &iov.iov_len))) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "SNAPPY",
"Failed to get length of Snappy "
"compressed payload "
"for message at offset %"PRId64
" (%"PRIusz" bytes): "
"ignoring message",
Offset, inlen);
err = RD_KAFKA_RESP_ERR__BAD_COMPRESSION;
goto err;
}
/* Allocate output buffer for uncompressed data */
iov.iov_base = rd_malloc(iov.iov_len);
if (unlikely(!iov.iov_base)) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "SNAPPY",
"Failed to allocate Snappy "
"decompress buffer of size %"PRIusz
"for message at offset %"PRId64
" (%"PRIusz" bytes): %s: "
"ignoring message",
iov.iov_len, Offset, inlen,
rd_strerror(errno));
err = RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE;
goto err;
}
/* Uncompress to outbuf */
if (unlikely((r = rd_kafka_snappy_uncompress(
inbuf, inlen, iov.iov_base)))) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "SNAPPY",
"Failed to decompress Snappy "
"payload for message at offset "
"%"PRId64" (%"PRIusz" bytes): %s: "
"ignoring message",
Offset, inlen,
rd_strerror(-r/*negative errno*/));
rd_free(iov.iov_base);
err = RD_KAFKA_RESP_ERR__BAD_COMPRESSION;
goto err;
}
}
}
break;
#endif
case RD_KAFKA_COMPRESSION_LZ4:
{
err = rd_kafka_lz4_decompress(msetr->msetr_rkb,
/* Proper HC? */
MsgVersion >= 1 ? 1 : 0,
Offset,
/* @warning Will modify compressed
* if no proper HC */
(char *)compressed,
compressed_size,
&iov.iov_base, &iov.iov_len);
if (err)
goto err;
}
break;
#if WITH_ZSTD
case RD_KAFKA_COMPRESSION_ZSTD:
{
err = rd_kafka_zstd_decompress(msetr->msetr_rkb,
(char *)compressed,
compressed_size,
&iov.iov_base, &iov.iov_len);
if (err)
goto err;
}
break;
#endif
default:
rd_rkb_dbg(msetr->msetr_rkb, MSG, "CODEC",
"%s [%"PRId32"]: Message at offset %"PRId64
" with unsupported "
"compression codec 0x%x: message ignored",
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
Offset, (int)codec);
err = RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED;
goto err;
}
rd_assert(iov.iov_base);
/*
* Decompression successful
*/
/* Create a new buffer pointing to the uncompressed
* allocated buffer (outbuf) and let messages keep a reference to
* this new buffer. */
rkbufz = rd_kafka_buf_new_shadow(iov.iov_base, iov.iov_len, rd_free);
rkbufz->rkbuf_rkb = msetr->msetr_rkbuf->rkbuf_rkb;
rd_kafka_broker_keep(rkbufz->rkbuf_rkb);
/* In MsgVersion v0..1 the decompressed data contains
* an inner MessageSet, pass it to a new MessageSet reader.
*
* For MsgVersion v2 the decompressed data are the list of messages.
*/
if (MsgVersion <= 1) {
/* Pass decompressed data (inner Messageset)
* to new instance of the MessageSet parser. */
rd_kafka_msgset_reader_t inner_msetr;
rd_kafka_msgset_reader_init(&inner_msetr,
rkbufz,
msetr->msetr_rktp,
msetr->msetr_tver,
&msetr->msetr_rkq);
inner_msetr.msetr_srcname = "compressed ";
if (MsgVersion == 1) {
/* postproc() will convert relative to
* absolute offsets */
inner_msetr.msetr_relative_offsets = 1;
inner_msetr.msetr_outer.offset = Offset;
/* Apply single LogAppendTime timestamp for
* all messages. */
if (Attributes & RD_KAFKA_MSG_ATTR_LOG_APPEND_TIME) {
inner_msetr.msetr_outer.tstype =
RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME;
inner_msetr.msetr_outer.timestamp = Timestamp;
}
}
/* Parse the inner MessageSet */
err = rd_kafka_msgset_reader_run(&inner_msetr);
/* Transfer message count from inner to outer */
msetr->msetr_msgcnt += inner_msetr.msetr_msgcnt;
msetr->msetr_msg_bytes += inner_msetr.msetr_msg_bytes;
} else {
/* MsgVersion 2 */
rd_kafka_buf_t *orig_rkbuf = msetr->msetr_rkbuf;
rkbufz->rkbuf_uflow_mitigation =
"truncated response from broker (ok)";
/* Temporarily replace read buffer with uncompressed buffer */
msetr->msetr_rkbuf = rkbufz;
/* Read messages */
err = rd_kafka_msgset_reader_msgs_v2(msetr);
/* Restore original buffer */
msetr->msetr_rkbuf = orig_rkbuf;
}
/* Loose our refcnt of the uncompressed rkbuf.
* Individual messages/rko's will have their own reference. */
rd_kafka_buf_destroy(rkbufz);
return err;
err:
/* Enqueue error messsage:
* Create op and push on temporary queue. */
rd_kafka_q_op_err(&msetr->msetr_rkq, RD_KAFKA_OP_CONSUMER_ERR,
err, msetr->msetr_tver->version, rktp, Offset,
"Decompression (codec 0x%x) of message at %"PRIu64
" of %"PRIu64" bytes failed: %s",
codec, Offset, compressed_size, rd_kafka_err2str(err));
return err;
}
/**
* @brief Message parser for MsgVersion v0..1
*
* @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or on single-message errors,
* or any other error code when the MessageSet parser should stop
* parsing (such as for partial Messages).
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_msg_v0_1 (rd_kafka_msgset_reader_t *msetr) {
rd_kafka_buf_t *rkbuf = msetr->msetr_rkbuf;
rd_kafka_toppar_t *rktp = msetr->msetr_rktp;
rd_kafka_broker_t *rkb = msetr->msetr_rkb;
struct {
int64_t Offset; /* MessageSet header */
int32_t MessageSize; /* MessageSet header */
uint32_t Crc;
int8_t MagicByte; /* MsgVersion */
int8_t Attributes;
int64_t Timestamp; /* v1 */
} hdr; /* Message header */
rd_kafkap_bytes_t Key;
rd_kafkap_bytes_t Value;
int32_t Value_len;
rd_kafka_op_t *rko;
size_t hdrsize = 6; /* Header size following MessageSize */
rd_slice_t crc_slice;
rd_kafka_msg_t *rkm;
int relative_offsets = 0;
const char *reloff_str = "";
/* Only log decoding errors if protocol debugging enabled. */
int log_decode_errors = (rkbuf->rkbuf_rkb->rkb_rk->rk_conf.debug &
RD_KAFKA_DBG_PROTOCOL) ? LOG_DEBUG : 0;
size_t message_end;
rd_kafka_buf_read_i64(rkbuf, &hdr.Offset);
rd_kafka_buf_read_i32(rkbuf, &hdr.MessageSize);
message_end = rd_slice_offset(&rkbuf->rkbuf_reader) + hdr.MessageSize;
rd_kafka_buf_read_i32(rkbuf, &hdr.Crc);
if (!rd_slice_narrow_copy_relative(&rkbuf->rkbuf_reader, &crc_slice,
hdr.MessageSize - 4))
rd_kafka_buf_check_len(rkbuf, hdr.MessageSize - 4);
rd_kafka_buf_read_i8(rkbuf, &hdr.MagicByte);
rd_kafka_buf_read_i8(rkbuf, &hdr.Attributes);
if (hdr.MagicByte == 1) { /* MsgVersion */
rd_kafka_buf_read_i64(rkbuf, &hdr.Timestamp);
hdrsize += 8;
/* MsgVersion 1 has relative offsets for compressed MessageSets*/
if (!(hdr.Attributes & RD_KAFKA_MSG_ATTR_COMPRESSION_MASK) &&
msetr->msetr_relative_offsets) {
relative_offsets = 1;
reloff_str = "relative ";
}
} else
hdr.Timestamp = 0;
/* Verify MessageSize */
if (unlikely(hdr.MessageSize < (ssize_t)hdrsize))
rd_kafka_buf_parse_fail(rkbuf,
"Message at %soffset %"PRId64
" MessageSize %"PRId32
" < hdrsize %"PRIusz,
reloff_str,
hdr.Offset, hdr.MessageSize, hdrsize);
/* Early check for partial messages */
rd_kafka_buf_check_len(rkbuf, hdr.MessageSize - hdrsize);
if (rkb->rkb_rk->rk_conf.check_crcs) {
/* Verify CRC32 if desired. */
uint32_t calc_crc;
calc_crc = rd_slice_crc32(&crc_slice);
rd_dassert(rd_slice_remains(&crc_slice) == 0);
if (unlikely(hdr.Crc != calc_crc)) {
/* Propagate CRC error to application and
* continue with next message. */
rd_kafka_q_op_err(&msetr->msetr_rkq,
RD_KAFKA_OP_CONSUMER_ERR,
RD_KAFKA_RESP_ERR__BAD_MSG,
msetr->msetr_tver->version,
rktp,
hdr.Offset,
"Message at %soffset %"PRId64
" (%"PRId32" bytes) "
"failed CRC32 check "
"(original 0x%"PRIx32" != "
"calculated 0x%"PRIx32")",
reloff_str, hdr.Offset,
hdr.MessageSize, hdr.Crc, calc_crc);
rd_kafka_buf_skip_to(rkbuf, message_end);
rd_atomic64_add(&rkb->rkb_c.rx_err, 1);
/* Continue with next message */
return RD_KAFKA_RESP_ERR_NO_ERROR;
}
}
/* Extract key */
rd_kafka_buf_read_bytes(rkbuf, &Key);
/* Extract Value */
rd_kafka_buf_read_bytes(rkbuf, &Value);
Value_len = RD_KAFKAP_BYTES_LEN(&Value);
/* MessageSets may contain offsets earlier than we
* requested (compressed MessageSets in particular),
* drop the earlier messages.
* Note: the inner offset may only be trusted for
* absolute offsets. KIP-31 introduced
* ApiVersion 2 that maintains relative offsets
* of compressed messages and the base offset
* in the outer message is the offset of
* the *LAST* message in the MessageSet.
* This requires us to assign offsets
* after all messages have been read from
* the messageset, and it also means
* we cant perform this offset check here
* in that case. */
if (!relative_offsets &&
hdr.Offset < rktp->rktp_offsets.fetch_offset)
return RD_KAFKA_RESP_ERR_NO_ERROR; /* Continue with next msg */
/* Handle compressed MessageSet */
if (unlikely(hdr.Attributes & RD_KAFKA_MSG_ATTR_COMPRESSION_MASK))
return rd_kafka_msgset_reader_decompress(
msetr, hdr.MagicByte, hdr.Attributes, hdr.Timestamp,
hdr.Offset, Value.data, Value_len);
/* Pure uncompressed message, this is the innermost
* handler after all compression and cascaded
* MessageSets have been peeled off. */
/* Create op/message container for message. */
rko = rd_kafka_op_new_fetch_msg(&rkm, rktp, msetr->msetr_tver->version,
rkbuf,
hdr.Offset,
(size_t)RD_KAFKAP_BYTES_LEN(&Key),
RD_KAFKAP_BYTES_IS_NULL(&Key) ?
NULL : Key.data,
(size_t)RD_KAFKAP_BYTES_LEN(&Value),
RD_KAFKAP_BYTES_IS_NULL(&Value) ?
NULL : Value.data);
/* Assign message timestamp.
* If message was in a compressed MessageSet and the outer/wrapper
* Message.Attribute had a LOG_APPEND_TIME set, use the
* outer timestamp */
if (msetr->msetr_outer.tstype == RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME) {
rkm->rkm_timestamp = msetr->msetr_outer.timestamp;
rkm->rkm_tstype = msetr->msetr_outer.tstype;
} else if (hdr.MagicByte >= 1 && hdr.Timestamp) {
rkm->rkm_timestamp = hdr.Timestamp;
if (hdr.Attributes & RD_KAFKA_MSG_ATTR_LOG_APPEND_TIME)
rkm->rkm_tstype = RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME;
else
rkm->rkm_tstype = RD_KAFKA_TIMESTAMP_CREATE_TIME;
}
/* Enqueue message on temporary queue */
rd_kafka_q_enq(&msetr->msetr_rkq, rko);
msetr->msetr_msgcnt++;
msetr->msetr_msg_bytes += rkm->rkm_key_len + rkm->rkm_len;
return RD_KAFKA_RESP_ERR_NO_ERROR; /* Continue */
err_parse:
/* Count all parse errors as partial message errors. */
rd_atomic64_add(&msetr->msetr_rkb->rkb_c.rx_partial, 1);
return rkbuf->rkbuf_err;
}
/**
* @brief Message parser for MsgVersion v2
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_msg_v2 (rd_kafka_msgset_reader_t *msetr) {
rd_kafka_buf_t *rkbuf = msetr->msetr_rkbuf;
rd_kafka_toppar_t *rktp = msetr->msetr_rktp;
struct {
int64_t Length;
int8_t MsgAttributes;
int64_t TimestampDelta;
int64_t OffsetDelta;
int64_t Offset; /* Absolute offset */
rd_kafkap_bytes_t Key;
rd_kafkap_bytes_t Value;
rd_kafkap_bytes_t Headers;
} hdr;
rd_kafka_op_t *rko;
rd_kafka_msg_t *rkm;
/* Only log decoding errors if protocol debugging enabled. */
int log_decode_errors = (rkbuf->rkbuf_rkb->rkb_rk->rk_conf.debug &
RD_KAFKA_DBG_PROTOCOL) ? LOG_DEBUG : 0;
size_t message_end;
rd_kafka_buf_read_varint(rkbuf, &hdr.Length);
message_end = rd_slice_offset(&rkbuf->rkbuf_reader)+(size_t)hdr.Length;
rd_kafka_buf_read_i8(rkbuf, &hdr.MsgAttributes);
rd_kafka_buf_read_varint(rkbuf, &hdr.TimestampDelta);
rd_kafka_buf_read_varint(rkbuf, &hdr.OffsetDelta);
hdr.Offset = msetr->msetr_v2_hdr->BaseOffset + hdr.OffsetDelta;
/* Skip message if outdated */
if (hdr.Offset < rktp->rktp_offsets.fetch_offset) {
rd_rkb_dbg(msetr->msetr_rkb, MSG, "MSG",
"%s [%"PRId32"]: "
"Skip offset %"PRId64" < fetch_offset %"PRId64,
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
hdr.Offset, rktp->rktp_offsets.fetch_offset);
rd_kafka_buf_skip_to(rkbuf, message_end);
return RD_KAFKA_RESP_ERR_NO_ERROR; /* Continue with next msg */
}
rd_kafka_buf_read_bytes_varint(rkbuf, &hdr.Key);
rd_kafka_buf_read_bytes_varint(rkbuf, &hdr.Value);
/* We parse the Headers later, just store the size (possibly truncated)
* and pointer to the headers. */
hdr.Headers.len = (int32_t)(message_end -
rd_slice_offset(&rkbuf->rkbuf_reader));
rd_kafka_buf_read_ptr(rkbuf, &hdr.Headers.data, hdr.Headers.len);
/* Create op/message container for message. */
rko = rd_kafka_op_new_fetch_msg(&rkm,
rktp, msetr->msetr_tver->version, rkbuf,
hdr.Offset,
(size_t)RD_KAFKAP_BYTES_LEN(&hdr.Key),
RD_KAFKAP_BYTES_IS_NULL(&hdr.Key) ?
NULL : hdr.Key.data,
(size_t)RD_KAFKAP_BYTES_LEN(&hdr.Value),
RD_KAFKAP_BYTES_IS_NULL(&hdr.Value) ?
NULL : hdr.Value.data);
/* Store pointer to unparsed message headers, they will
* be parsed on the first access.
* This pointer points to the rkbuf payload.
* Note: can't perform struct copy here due to const fields (MSVC) */
rkm->rkm_u.consumer.binhdrs.len = hdr.Headers.len;
rkm->rkm_u.consumer.binhdrs.data = hdr.Headers.data;
/* Set timestamp.
*
* When broker assigns the timestamps (LOG_APPEND_TIME) it will
* assign the same timestamp for all messages in a MessageSet
* using MaxTimestamp.
*/
if ((msetr->msetr_v2_hdr->Attributes &
RD_KAFKA_MSG_ATTR_LOG_APPEND_TIME) ||
(hdr.MsgAttributes & RD_KAFKA_MSG_ATTR_LOG_APPEND_TIME)) {
rkm->rkm_tstype = RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME;
rkm->rkm_timestamp = msetr->msetr_v2_hdr->MaxTimestamp;
} else {
rkm->rkm_tstype = RD_KAFKA_TIMESTAMP_CREATE_TIME;
rkm->rkm_timestamp =
msetr->msetr_v2_hdr->BaseTimestamp + hdr.TimestampDelta;
}
/* Enqueue message on temporary queue */
rd_kafka_q_enq(&msetr->msetr_rkq, rko);
msetr->msetr_msgcnt++;
msetr->msetr_msg_bytes += rkm->rkm_key_len + rkm->rkm_len;
return RD_KAFKA_RESP_ERR_NO_ERROR;
err_parse:
/* Count all parse errors as partial message errors. */
rd_atomic64_add(&msetr->msetr_rkb->rkb_c.rx_partial, 1);
return rkbuf->rkbuf_err;
}
/**
* @brief Read v2 messages from current buffer position.
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_msgs_v2 (rd_kafka_msgset_reader_t *msetr) {
while (rd_kafka_buf_read_remain(msetr->msetr_rkbuf)) {
rd_kafka_resp_err_t err;
err = rd_kafka_msgset_reader_msg_v2(msetr);
if (unlikely(err))
return err;
}
return RD_KAFKA_RESP_ERR_NO_ERROR;
}
/**
* @brief MessageSet reader for MsgVersion v2 (FetchRequest v4)
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_v2 (rd_kafka_msgset_reader_t *msetr) {
rd_kafka_buf_t *rkbuf = msetr->msetr_rkbuf;
rd_kafka_toppar_t *rktp = msetr->msetr_rktp;
struct msgset_v2_hdr hdr;
rd_slice_t save_slice;
rd_kafka_resp_err_t err = RD_KAFKA_RESP_ERR_NO_ERROR;
size_t len_start;
size_t payload_size;
int64_t LastOffset; /* Last absolute Offset in MessageSet header */
/* Only log decoding errors if protocol debugging enabled. */
int log_decode_errors = (rkbuf->rkbuf_rkb->rkb_rk->rk_conf.debug &
RD_KAFKA_DBG_PROTOCOL) ? LOG_DEBUG : 0;
rd_kafka_buf_read_i64(rkbuf, &hdr.BaseOffset);
rd_kafka_buf_read_i32(rkbuf, &hdr.Length);
len_start = rd_slice_offset(&rkbuf->rkbuf_reader);
if (unlikely(hdr.Length < RD_KAFKAP_MSGSET_V2_SIZE - 8 - 4))
rd_kafka_buf_parse_fail(rkbuf,
"%s [%"PRId32"] "
"MessageSet at offset %"PRId64
" length %"PRId32" < header size %d",
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
hdr.BaseOffset, hdr.Length,
RD_KAFKAP_MSGSET_V2_SIZE - 8 - 4);
rd_kafka_buf_read_i32(rkbuf, &hdr.PartitionLeaderEpoch);
rd_kafka_buf_read_i8(rkbuf, &hdr.MagicByte);
rd_kafka_buf_read_i32(rkbuf, &hdr.Crc);
if (msetr->msetr_rkb->rkb_rk->rk_conf.check_crcs) {
/* Verify CRC32C if desired. */
uint32_t calc_crc;
rd_slice_t crc_slice;
size_t crc_len = hdr.Length-4-1-4;
if (!rd_slice_narrow_copy_relative(
&rkbuf->rkbuf_reader,
&crc_slice, crc_len))
rd_kafka_buf_check_len(rkbuf, crc_len);
calc_crc = rd_slice_crc32c(&crc_slice);
if (unlikely((uint32_t)hdr.Crc != calc_crc)) {
/* Propagate CRC error to application and
* continue with next message. */
rd_kafka_q_op_err(&msetr->msetr_rkq,
RD_KAFKA_OP_CONSUMER_ERR,
RD_KAFKA_RESP_ERR__BAD_MSG,
msetr->msetr_tver->version,
rktp,
hdr.BaseOffset,
"MessageSet at offset %"PRId64
" (%"PRId32" bytes) "
"failed CRC32C check "
"(original 0x%"PRIx32" != "
"calculated 0x%"PRIx32")",
hdr.BaseOffset,
hdr.Length, hdr.Crc, calc_crc);
rd_kafka_buf_skip_to(rkbuf, crc_len);
rd_atomic64_add(&msetr->msetr_rkb->rkb_c.rx_err, 1);
return RD_KAFKA_RESP_ERR_NO_ERROR;
}
}
rd_kafka_buf_read_i16(rkbuf, &hdr.Attributes);
rd_kafka_buf_read_i32(rkbuf, &hdr.LastOffsetDelta);
LastOffset = hdr.BaseOffset + hdr.LastOffsetDelta;
rd_kafka_buf_read_i64(rkbuf, &hdr.BaseTimestamp);
rd_kafka_buf_read_i64(rkbuf, &hdr.MaxTimestamp);
rd_kafka_buf_read_i64(rkbuf, &hdr.PID);
rd_kafka_buf_read_i16(rkbuf, &hdr.ProducerEpoch);
rd_kafka_buf_read_i32(rkbuf, &hdr.BaseSequence);
rd_kafka_buf_read_i32(rkbuf, &hdr.RecordCount);
/* Payload size is hdr.Length - MessageSet headers */
payload_size = hdr.Length - (rd_slice_offset(&rkbuf->rkbuf_reader) -
len_start);
if (unlikely(payload_size > rd_kafka_buf_read_remain(rkbuf)))
rd_kafka_buf_underflow_fail(rkbuf, payload_size,
"%s [%"PRId32"] "
"MessageSet at offset %"PRId64
" payload size %"PRIusz,
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
hdr.BaseOffset, payload_size);
/* If entire MessageSet contains old outdated offsets, skip it. */
if (LastOffset < rktp->rktp_offsets.fetch_offset) {
rd_kafka_buf_skip(rkbuf, payload_size);
goto done;
}
/* Ignore control messages */
if (unlikely((hdr.Attributes & RD_KAFKA_MSGSET_V2_ATTR_CONTROL))) {
msetr->msetr_ctrl_cnt++;
rd_kafka_buf_skip(rkbuf, payload_size);
goto done;
}
msetr->msetr_v2_hdr = &hdr;
/* Handle compressed MessageSet */
if (hdr.Attributes & RD_KAFKA_MSG_ATTR_COMPRESSION_MASK) {
const void *compressed;
compressed = rd_slice_ensure_contig(&rkbuf->rkbuf_reader,
payload_size);
rd_assert(compressed);
err = rd_kafka_msgset_reader_decompress(
msetr, 2/*MsgVersion v2*/, hdr.Attributes,
hdr.BaseTimestamp, hdr.BaseOffset,
compressed, payload_size);
if (err)
goto err;
} else {
/* Read uncompressed messages */
/* Save original slice, reduce size of the current one to
* be limited by the MessageSet.Length, and then start reading
* messages until the lesser slice is exhausted. */
if (!rd_slice_narrow_relative(&rkbuf->rkbuf_reader,
&save_slice, payload_size))
rd_kafka_buf_check_len(rkbuf, payload_size);
/* Read messages */
err = rd_kafka_msgset_reader_msgs_v2(msetr);
/* Restore wider slice */
rd_slice_widen(&rkbuf->rkbuf_reader, &save_slice);
if (unlikely(err))
goto err;
}
done:
/* Set the next fetch offset to the MessageSet header's last offset + 1
* to avoid getting stuck on compacted MessageSets where the last
* Message in the MessageSet has an Offset < MessageSet header's
* last offset. See KAFKA-5443 */
msetr->msetr_next_offset = LastOffset + 1;
msetr->msetr_v2_hdr = NULL;
return RD_KAFKA_RESP_ERR_NO_ERROR;
err_parse:
/* Count all parse errors as partial message errors. */
rd_atomic64_add(&msetr->msetr_rkb->rkb_c.rx_partial, 1);
err = rkbuf->rkbuf_err;
/* FALLTHRU */
err:
msetr->msetr_v2_hdr = NULL;
return err;
}
/**
* @brief Peek into the next MessageSet to find the MsgVersion.
*
* @param MagicBytep the MsgVersion is returned here on success.
*
* @returns an error on read underflow or if the MsgVersion is
* unsupported.
*/
static rd_kafka_resp_err_t
rd_kafka_msgset_reader_peek_msg_version (rd_kafka_msgset_reader_t *msetr,
int8_t *MagicBytep) {
rd_kafka_buf_t *rkbuf = msetr->msetr_rkbuf;
rd_kafka_toppar_t *rktp = msetr->msetr_rktp;
/* Only log decoding errors if protocol debugging enabled. */
int log_decode_errors = (rkbuf->rkbuf_rkb->rkb_rk->rk_conf.debug &
RD_KAFKA_DBG_PROTOCOL) ? LOG_DEBUG : 0;
size_t read_offset = rd_slice_offset(&rkbuf->rkbuf_reader);
rd_kafka_buf_peek_i8(rkbuf, read_offset+8+4+4, MagicBytep);
if (unlikely(*MagicBytep < 0 || *MagicBytep > 2)) {
int64_t Offset; /* For error logging */
int32_t Length;
rd_kafka_buf_read_i64(rkbuf, &Offset);
rd_rkb_dbg(msetr->msetr_rkb,
MSG | RD_KAFKA_DBG_PROTOCOL | RD_KAFKA_DBG_FETCH,
"MAGICBYTE",
"%s [%"PRId32"]: "
"Unsupported Message(Set) MagicByte %d at "
"offset %"PRId64" "
"(buffer position %"PRIusz"/%"PRIusz"): skipping",
rktp->rktp_rkt->rkt_topic->str,
rktp->rktp_partition,
(int)*MagicBytep, Offset,
read_offset, rd_slice_size(&rkbuf->rkbuf_reader));
if (Offset >= msetr->msetr_rktp->rktp_offsets.fetch_offset) {
rd_kafka_q_op_err(
&msetr->msetr_rkq,
RD_KAFKA_OP_CONSUMER_ERR,
RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED,
msetr->msetr_tver->version, rktp, Offset,
"Unsupported Message(Set) MagicByte %d "
"at offset %"PRId64,
(int)*MagicBytep, Offset);
/* Skip message(set) */
msetr->msetr_rktp->rktp_offsets.fetch_offset = Offset+1;
}
/* Skip this Message(Set).
* If the message is malformed, the skip may trigger err_parse
* and return ERR__BAD_MSG. */
rd_kafka_buf_read_i32(rkbuf, &Length);
rd_kafka_buf_skip(rkbuf, Length);
return RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED;
}
return RD_KAFKA_RESP_ERR_NO_ERROR;
err_parse: