blib/lib/Kafka/Protocol.pm untested

filter: Statementstmt 0.0% 0 / 651 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 68 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 918 SCAR 83.0
CC
63
Cov
0%
_decode_MessageSet_sized_template
156.0
_snappy_xerial_decompress
90.0
_encode_Message
72.0
CRAP 4032.0
1 package Kafka::Protocol;
2  
3 =head1 NAME
4  
5 Kafka::Protocol - Functions to process messages in the Apache Kafka protocol.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::Protocol> version 1.07 .
10  
11 =cut
12  
13 use 5.010;
14 use strict;
15 use warnings;
16  
17 our $VERSION = '1.07';
18  
19 use Exporter qw(
20 import
21 );
22 our @EXPORT_OK = qw(
23 decode_fetch_response
24 decode_metadata_response
25 decode_offset_response
26 decode_produce_response
27 decode_offsetcommit_response
28 decode_offsetfetch_response
29 encode_fetch_request
30 encode_metadata_request
31 encode_offset_request
32 encode_produce_request
33 encode_api_versions_request
34 decode_api_versions_response
35 encode_find_coordinator_request
36 decode_find_coordinator_response
37 encode_offsetcommit_request
38 encode_offsetfetch_request
39 _decode_MessageSet_template
40 _decode_MessageSet_array
41 _encode_Message
42 _encode_MessageSet_array
43 _encode_string
44 _pack64
45 _unpack64
46 _verify_string
47 $DEFAULT_APIVERSION
48 $IMPLEMENTED_APIVERSIONS
49 $BAD_OFFSET
50 $COMPRESSION_CODEC_MASK
51 $CONSUMERS_REPLICAID
52 $NULL_BYTES_LENGTH
53 $_int64_template
54 );
55  
56 use Compress::Snappy ();
57 use Compress::LZ4Frame ();
58 use Const::Fast;
59 use Gzip::Faster qw( gzip gunzip );
60 use Params::Util qw(
61 _ARRAY
62 _HASH
63 _SCALAR
64 _STRING
65 );
66 use Scalar::Util qw(
67 dualvar
68 );
69 use String::CRC32;
70 use Try::Tiny;
71  
72 use Kafka qw(
73 $BITS64
74 $BLOCK_UNTIL_IS_COMMITTED
75 $COMPRESSION_GZIP
76 $COMPRESSION_NONE
77 $COMPRESSION_SNAPPY
78 $COMPRESSION_LZ4
79 $DEFAULT_MAX_WAIT_TIME
80 %ERROR
81 $ERROR_COMPRESSION
82 $ERROR_MISMATCH_ARGUMENT
83 $ERROR_NOT_BINARY_STRING
84 $ERROR_REQUEST_OR_RESPONSE
85 $NOT_SEND_ANY_RESPONSE
86 $WAIT_WRITTEN_TO_LOCAL_LOG
87 );
88 use Kafka::Exceptions;
89 use Kafka::Internals qw(
90 $APIKEY_FETCH
91 $APIKEY_METADATA
92 $APIKEY_OFFSET
93 $APIKEY_PRODUCE
94 $APIKEY_APIVERSIONS
95 $APIKEY_FINDCOORDINATOR
96 $APIKEY_OFFSETCOMMIT
97 $APIKEY_OFFSETFETCH
98 $PRODUCER_ANY_OFFSET
99 format_message
100 debug_level
101 );
102  
103 =head1 SYNOPSIS
104  
105 use 5.010;
106 use strict;
107 use warnings;
108  
109 use Data::Compare;
110 use Kafka qw(
111 $COMPRESSION_NONE
112 $ERROR_NO_ERROR
113 $REQUEST_TIMEOUT
114 $WAIT_WRITTEN_TO_LOCAL_LOG
115 );
116 use Kafka::Internals qw(
117 $PRODUCER_ANY_OFFSET
118 );
119 use Kafka::Protocol qw(
120 decode_produce_response
121 encode_produce_request
122 );
123  
124 # a encoded produce request hex stream
125 my $encoded = pack( q{H*}, '00000049000000000000000400000001000005dc0000000100076d79746f7069630000000100000000000000200000000000000000000000148dc795a20000ffffffff0000000648656c6c6f21' );
126  
127 # a decoded produce request
128 my $decoded = {
129 CorrelationId => 4,
130 ClientId => q{},
131 RequiredAcks => $WAIT_WRITTEN_TO_LOCAL_LOG,
132 Timeout => $REQUEST_TIMEOUT * 100, # ms
133 topics => [
134 {
135 TopicName => 'mytopic',
136 partitions => [
137 {
138 Partition => 0,
139 MessageSet => [
140 {
141 Offset => $PRODUCER_ANY_OFFSET,
142 MagicByte => 0,
143 Attributes => $COMPRESSION_NONE,
144 Key => q{},
145 Value => 'Hello!',
146 },
147 ],
148 },
149 ],
150 },
151 ],
152 };
153  
154 my $encoded_request = encode_produce_request( $decoded );
155 say 'encoded correctly' if $encoded_request eq $encoded;
156  
157 # a encoded produce response hex stream
158 $encoded = pack( q{H*}, '00000023000000040000000100076d79746f706963000000010000000000000000000000000000' );
159  
160 # a decoded produce response
161 $decoded = {
162 CorrelationId => 4,
163 topics => [
164 {
165 TopicName => 'mytopic',
166 partitions => [
167 {
168 Partition => 0,
169 ErrorCode => $ERROR_NO_ERROR,
170 Offset => 0,
171 },
172 ],
173 },
174 ],
175 };
176  
177 my $decoded_response = decode_produce_response( \$encoded );
178 say 'decoded correctly' if Compare( $decoded_response, $decoded );
179  
180 # more examples, see t/*_decode_encode.t
181  
182 =head1 DESCRIPTION
183  
184 This module is not a user module.
185  
186 In order to achieve better performance,
187 functions of this module do not perform arguments validation.
188  
189 The main features of the C<Kafka::Protocol> module are:
190  
191 =over 3
192  
193 =item *
194  
195 Supports parsing the Apache Kafka protocol.
196  
197 =item *
198  
199 Supports Apache Kafka Requests and Responses (PRODUCE and FETCH).
200 Within this package we currently support
201 access to PRODUCE, FETCH, OFFSET, METADATA Requests and Responses.
202  
203 =item *
204  
205 Support for working with 64 bit elements of the Kafka protocol on 32 bit systems.
206  
207 =back
208  
209 =cut
210  
211 # A Guide To The Kafka Protocol 0.8:
212 # https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
213 #
214 # -- Protocol Primitive Types
215 # int8, int16, int32, int64
216 # Signed integers
217 # stored in big endian order.
218 # bytes, string
219 # consist of a signed integer
220 # giving a length N
221 # followed by N bytes of content.
222 # A length of -1 indicates null.
223 # string uses an int16 for its size,
224 # and bytes uses an int32.
225 # Arrays
226 # These will always be encoded as an int32 size containing the length N
227 # followed by N repetitions of the structure
228 # which can itself be made up of other primitive types.
229 #
230 # -- N.B.
231 # - The response will always match the paired request
232 # - One structure common to both the produce and fetch requests is the message set format.
233 # - MessageSets are not preceded by an int32 like other array elements in the protocol.
234 # - A message set is also the unit of compression in Kafka,
235 # and we allow messages to recursively contain compressed message sets.
236 #
237 # -- Protocol Fields
238 # ApiKey => int16 That identifies the API being invoked
239 # ApiVersion => int16 This is a numeric version number for this api.
240 # Currently the supported version for all APIs is 0.
241 # Attributes => int8 Metadata attributes about the message.
242 # The lowest 2 bits contain the compression codec used for the message.
243 # ClientId => string This is a user supplied identifier for the client application.
244 # CorrelationId => int32 This is a user-supplied integer.
245 # It will be passed back in the response by the server, unmodified.
246 # It is useful for matching request and response between the client and server.
247 # Crc => int32 The CRC32 of the remainder of the message bytes.
248 # ErrorCode => int16 The error from this partition, if any.
249 # Errors are given on a per-partition basis
250 # because a given partition may be unavailable or maintained on a different host,
251 # while others may have successfully accepted the produce request.
252 # FetchOffset => int64 The offset to begin this fetch from.
253 # HighwaterMarkOffset => int64 The offset at the end of the log for this partition.
254 # This can be used by the client to determine how many messages behind the end of the log they are.
255 # - 0.8 documents: Replication design
256 # The high watermark is the offset of the last committed message.
257 # Each log is periodically synced to disks.
258 # Data before the flushed offset is guaranteed to be persisted on disks.
259 # As we will see, the flush offset can be before or after high watermark.
260 # - 0.7 documents: Wire protocol
261 # If the last segment file for the partition is not empty and was modified earlier than TIME,
262 # it will return both the first offset for that segment and the high water mark.
263 # The high water mark is not the offset of the last message,
264 # but rather the offset that the next message sent to the partition will be written to.
265 # Host => string The brokers hostname
266 # Isr => [ReplicaId] The set subset of the replicas that are "caught up" to the leader - a set of in-sync replicas (ISR)
267 # Key => bytes An optional message key
268 # The key can be null.
269 # Leader => int32 The node id for the kafka broker currently acting as leader for this partition.
270 # If no leader exists because we are in the middle of a leader election this id will be -1.
271 # MagicByte => int8 A version id used to allow backwards compatible evolution of the message binary format.
272 # MaxBytes => int32 The maximum bytes to include in the message set for this partition.
273 # MaxNumberOfOffsets => int32 Kafka here is return up to 'MaxNumberOfOffsets' of offsets
274 # MaxWaitTime => int32 The maximum amount of time (ms)
275 # to block waiting
276 # if insufficient data is available at the time the request is issued.
277 # MessageSetSize => int32 The size in bytes of the message set for this partition
278 # MessageSize => int32 The size of the subsequent request or response message in bytes
279 # MinBytes => int32 The minimum number of bytes of messages that must be available to give a response.
280 # If the client sets this to 0 the server will always respond immediately.
281 # If this is set to 1,
282 # the server will respond as soon
283 # as at least one partition
284 # has at least 1 byte of data
285 # or the specified timeout occurs.
286 # By setting higher values
287 # in combination with the timeout
288 # for reading only large chunks of data
289 # NodeId => int32 The id of the broker.
290 # This must be set to a unique integer for each broker.
291 # Offset => int64 The offset used in kafka as the log sequence number.
292 # When the producer is sending messages it doesn't actually know the offset
293 # and can fill in any value here it likes.
294 # Partition => int32 The id of the partition the fetch is for
295 # or the partition that data is being published to
296 # or the partition this response entry corresponds to.
297 # Port => int32 The brokers port
298 # ReplicaId => int32 Indicates the node id of the replica initiating this request.
299 # Normal client consumers should always specify this as -1 as they have no node id.
300 # Replicas => [ReplicaId] The set of alive nodes that currently acts as slaves for the leader for this partition.
301 # RequiredAcks => int16 Indicates how many acknowledgements the servers should receive
302 # before responding to the request.
303 # If it is 0 the server does not send any response.
304 # If it is 1, the server will wait the data is written to the local log before sending a response.
305 # If it is -1 the server will block until the message is committed by all in sync replicas before sending a response.
306 # Size => int32 The size of the subsequent request or response message in bytes
307 # Time => int64 Used to ask for all messages before a certain time (ms).
308 # There are two special values.
309 # Specify -1 to receive the latest offset (this will only ever return one offset).
310 # Specify -2 to receive the earliest available offsets.
311 # Timeout => int32 This provides a maximum time (ms) the server can await the receipt
312 # of the number of acknowledgements in RequiredAcks.
313 # The timeout is not an exact limit on the request time for a few reasons:
314 # (1) it does not include network latency,
315 # (2) the timer begins at the beginning of the processing of this request
316 # so if many requests are queued due to server overload
317 # that wait time will not be included,
318 # (3) we will not terminate a local write
319 # so if the local write time exceeds this timeout it will not be respected.
320 # To get a hard timeout of this type the client should use the socket timeout.
321 # TopicName => string The name of the topic.
322 # Value => bytes The actual message contents
323 # Kafka supports recursive messages in which case this may itself contain a message set.
324 # The message can be null.
325  
326 our $_int64_template; # Used to unpack a 64 bit value
327 if ( $BITS64 ) {
328 $_int64_template = q{q>};
329 # unpack a big-endian signed quad (64-bit) value on 64 bit systems.
330 *_unpack64 = sub { $_[0] };
331 # pack a big-endian signed quad (64-bit) value on 64 bit systems.
332 *_pack64 = sub { pack( q{q>}, $_[0] ) };
333 } else {
334 eval q{ require Kafka::Int64; } ## no critic
335 or die "Cannot load Kafka::Int64 : $@";
336  
337 $_int64_template = q{a[8]};
338 # unpack a big-endian signed quad (64-bit) value on 32 bit systems.
339 *_unpack64 = \&Kafka::Int64::unpackq;
340 # pack a big-endian signed quad (64-bit) value on 32 bit systems.
341 *_pack64 = \&Kafka::Int64::packq;
342 }
343  
344 =head2 EXPORT
345  
346 The following constants are available for export
347  
348 =cut
349  
350 =head3 C<$DEFAULT_APIVERSION>
351  
352 The default API version that will be used as fallback, if it's not possible to
353 detect what the Kafka server supports. Only Kafka servers > 0.10.0.0 can be
354 queried to get which API version they implements. On Kafka servers 0.8.x and
355 0.9.x, the protocol will default to use $DEFAULT_APIVERSION. Currently its
356 value is '0'
357  
358 =cut
359  
360 const our $DEFAULT_APIVERSION => 0;
361  
362 # HashRef, keys are APIKEYs, values are the api version that this protocol
363 # implements. If not populated, the connection will use the $DEFAULT_APIVERSION
364 our $IMPLEMENTED_APIVERSIONS = {};
365  
366 # Attributes
367  
368 # According to Apache Kafka documentation:
369 # Attributes - Metadata attributes about the message.
370 # The lowest 2 bits contain the compression codec used for the message.
371 const our $COMPRESSION_CODEC_MASK => 0b11;
372  
373 =head3 C<$CONSUMERS_REPLICAID>
374  
375 According to Apache Kafka documentation: 'ReplicaId - Normal client consumers should always specify this as -1 as they have no node id.'
376  
377 =cut
378 const our $CONSUMERS_REPLICAID => -1;
379  
380 =head3 C<$NULL_BYTES_LENGTH>
381  
382 According to Apache Kafka documentation: 'Protocol Primitive Types: ... bytes, string - A length of -1 indicates null.'
383  
384 =cut
385 const our $NULL_BYTES_LENGTH => -1;
386  
387 =head3 C<$BAD_OFFSET>
388  
389 According to Apache Kafka documentation: 'Offset - When the producer is sending messages it doesn't actually know the offset
390 and can fill in any value here it likes.'
391  
392 =cut
393 const our $BAD_OFFSET => -1;
394  
395 my ( $_Request_header_template, $_Request_header_length ) = (
396 q{l>s>s>l>s>}, # Size
397 # 2 ApiKey
398 # 2 ApiVersion
399 # 4 CorrelationId
400 # 2 ClientId length
401 10 # 'Size' is not included in the calculation of length
402 );
403 my ( $_ProduceRequest_header_template, $_ProduceRequest_header_length ) = (
404 q{s>l>l>}, # 2 RequiredAcks
405 # 4 Timeout
406 # 4 topics array size
407 10
408 );
409 my ( $_MessageSet_template, $_MessageSet_length ) = (
410 # qq{${_int64_template}l>},
411 q{a[8]l>},
412 # 8 Offset
413 # 4 MessageSize
414 12
415 );
416 my ( $_FetchRequest_header_template, $_FetchRequest_header_length ) = (
417 q{l>l>l>l>},
418 # 4 ReplicaId
419 # 4 MaxWaitTime
420 # 4 MinBytes
421 # 4 topics array size
422 16
423 );
424 my ( $_FetchRequest_header_template_v3, $_FetchRequest_header_length_v3 ) = (
425 q{l>l>l>l>l>},
426 # 4 ReplicaId
427 # 4 MaxWaitTime
428 # 4 MinBytes
429 # 4 MaxBytes
430 # 4 topics array size
431 20
432 );
433 my ( $_FetchRequest_body_template, $_FetchRequest_body_length ) = (
434 # qq{l>${_int64_template}l>},
435 q{l>a[8]l>},
436 # 4 Partition
437 # 8 FetchOffset
438 # 4 MaxBytes
439 16
440 );
441 my ( $_OffsetRequest_header_template, $_OffsetRequest_header_length ) = (
442 q{l>l>}, # 4 ReplicaId
443 # 4 topics array size
444 8
445 );
446 my ( $_OffsetRequest_body_template, $_OffsetRequest_body_length ) = (
447 # qq{l>${_int64_template}l>},
448 q{l>a[8]l>},
449 # 4 Partition
450 # 8 Time
451 # 4 MaxNumberOfOffsets
452 16
453 );
454 my ( $_OffsetRequest_body_template_v1, $_OffsetRequest_body_length_v1 ) = (
455 # qq{l>${_int64_template}},
456 q{l>a[8]},
457 # 4 Partition
458 # 8 Time
459 12
460 );
461 my ( $_FetchResponse_header_template, $_FetchResponse_header_length ) = (
462 q{x[l]l>l>}, # Size (skip)
463 # 4 CorrelationId
464 # 4 topics array size
465 8
466 );
467 my ( $_FetchResponse_header_template_v1, $_FetchResponse_header_length_v1 ) = (
468 q{x[l]l>l>l>}, # Size (skip)
469 # 4 CorrelationId
470 # 4 throttle_time_ms
471 # 4 topics array size
472 12
473 );
474 my ( $_Message_template, $_Message_length ) = (
475 qq(${_int64_template}l>l>ccl>),
476 # 8 Offset
477 # MessageSize
478 # Crc
479 # MagicByte
480 # Attributes
481 # Key length
482 8 # Only Offset length
483 );
484 my ( $_Message_template_with_timestamp, $_Message_length_with_timestamp ) = (
485 qq(${_int64_template}l>l>cc${_int64_template}l>),
486 # 8 Offset
487 # MessageSize
488 # Crc
489 # MagicByte
490 # Attributes
491 # Timestamp
492 # Key length
493 8 # Only Offset length
494 );
495  
496 my ( $_FetchResponse_topic_body_template, $_FetchResponse_topic_body_length )= (
497 qq(s>/al>l>s>${_int64_template}),
498 # TopicName
499 # partitions array size
500 # 4 Partition
501 # 2 ErrorCode
502 # 8 HighwaterMarkOffset
503 14 # without TopicName and partitions array size
504 );
505 my $_Key_or_Value_template = q{X[l]l>/a}; # Key or Value
506  
507 #-- public functions -----------------------------------------------------------
508  
509 =head2 FUNCTIONS
510  
511 The following functions are available for C<Kafka::MockProtocol> module.
512  
513 =cut
514  
515 =head3 C<encode_api_versions_request( $ApiVersions_Request )>
516  
517 Encodes the argument and returns a reference to the encoded binary string
518 representing a Request buffer.
519  
520 This function takes the following arguments:
521  
522 =over 3
523  
524 =item C<$ApiVersions_Request>
525  
526 C<$ApiVersions_Request> is a reference to the hash representing the structure
527 of the APIVERSIONS Request. it contains CorrelationId, ClientId (can be empty
528 string), and ApiVersion (must be 0)
529  
530 =back
531  
532 =cut
533  
534 $IMPLEMENTED_APIVERSIONS->{$APIKEY_APIVERSIONS} = 0;
535 sub encode_api_versions_request {
536 my ( $ApiVersions_Request ) = @_;
537  
538 my @data;
539 my $request = {
540 # template => '...',
541 # len => ...,
542 data => \@data,
543 };
544  
545 _encode_request_header( $request, $APIKEY_APIVERSIONS, $ApiVersions_Request );
546 # Size
547 # ApiKey
548 # ApiVersion
549 # CorrelationId
550 # ClientId
551  
552 return pack( $request->{template}, $request->{len}, @data );
553 }
554  
555 my $_decode_api_version_response_template = q{x[l]l>s>l>X[l]l>/(s>s>s>)};
556 # x[l] # Size (skip)
557 # l> # CorrelationId
558 # s> # ErrorCode
559 # l> # ApiVersions array size
560 # X[l]
561 # l>/( # ApiVersions array
562 # s> # ApiKey
563 # s> # MinVersion
564 # s> # MaxVersion
565 # )
566  
567 =head3 C<decode_api_versions_response( $bin_stream_ref )>
568  
569 Decodes the argument and returns a reference to the hash representing
570 the structure of the APIVERSIONS Response.
571  
572 This function takes the following arguments:
573  
574 =over 3
575  
576 =item C<$bin_stream_ref>
577  
578 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
579 must be a non-empty binary string.
580  
581 =back
582  
583 =cut
584  
585 sub decode_api_versions_response {
586 my ( $bin_stream_ref ) = @_;
587  
588 my @data = unpack( $_decode_api_version_response_template, $$bin_stream_ref );
589  
590 my $i = 0;
591 my $ApiVersions_Response = {};
592  
593 $ApiVersions_Response->{CorrelationId} = $data[ $i++ ]; # CorrelationId
594 $ApiVersions_Response->{ErrorCode} = $data[ $i++ ]; # ErrorCode
595  
596 my $ApiVersions_array = $ApiVersions_Response->{ApiVersions} = [];
597 my $ApiVersions_array_size = $data[ $i++ ]; # ApiVersions array size
598 while ( $ApiVersions_array_size-- ) {
599 push( @$ApiVersions_array, {
600 ApiKey => $data[ $i++ ], # ApiKey
601 MinVersion => $data[ $i++ ], # MinVersion
602 MaxVersion => $data[ $i++ ], # MaxVersion
603 }
604 );
605 }
606  
607 return $ApiVersions_Response;
608 }
609  
610 =head3 C<encode_find_coordinator_request( $FindCoordinator_Request )>
611  
612 Encodes the argument and returns a reference to the encoded binary string
613 representing a Request buffer.
614  
615 This function takes the following arguments:
616  
617 =over 3
618  
619 =item C<$FindCoordinator_Request>
620  
621 C<$FindCoordinator_Request> is a reference to the hash representing the structure
622 of the FINDCOORDINATOR Request. it contains CorrelationId, ClientId (can be empty
623 string), CoordinatorKey and CoordinatorType (for version 1 of protocol)
624  
625 =back
626  
627 =cut
628  
629 $IMPLEMENTED_APIVERSIONS->{$APIKEY_FINDCOORDINATOR} = 1;
630 sub encode_find_coordinator_request {
631 my ( $FindCoordinator_Request ) = @_;
632  
633 my @data;
634 my $request = {
635 # template => '...',
636 # len => ...,
637 data => \@data,
638 };
639  
640 my $api_version =
641 _encode_request_header( $request, $APIKEY_FINDCOORDINATOR, $FindCoordinator_Request );
642 # Size
643 # ApiKey
644 # ApiVersion
645 # CorrelationId
646 # ClientId
647  
648 my $coordinator_key = $FindCoordinator_Request->{CoordinatorKey};
649 my $coordinator_type = $FindCoordinator_Request->{CoordinatorType};
650  
651 $request->{template} .= q{s>}; # string length
652 $request->{len} += 2;
653 _encode_string( $request, $coordinator_key ); # CoordinatorKey (GroupId for version 0)
654 if ($api_version >= 1) {
655 # CoordinatorType (0 for groups)
656 $request->{template} .= q{c>};
657 $request->{len} += 1;
658 push( @{ $request->{data} }, $coordinator_type );
659 }
660  
661 return pack( $request->{template}, $request->{len}, @data );
662 }
663  
664 my $_decode_find_protocol_response_template = q{x[l]l>s>l>s>/al>};
665 # x[l] # Size (skip)
666 # l> # CorrelationId
667 # s> # ErrorCode
668 # l> # NodeId
669 # s>/a # Host
670 # l> # Port
671  
672 my $_decode_find_protocol_response_template_v1 = q{x[l]l>l>s>l>s>/al>};
673 # x[l] # Size (skip)
674 # l> # CorrelationId
675 # l> # throttle_time_ms
676 # s> # ErrorCode
677 # s>/a # ErrorMessage
678 # l> # NodeId
679 # s>/a # Host
680 # l> # Port
681  
682 =head3 C<decode_find_coordinator_response( $bin_stream_ref )>
683  
684 Decodes the argument and returns a reference to the hash representing
685 the structure of the FINDCOORDINATOR Response.
686  
687 This function takes the following arguments:
688  
689 =over 3
690  
691 =item C<$bin_stream_ref>
692  
693 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
694 must be a non-empty binary string.
695  
696 =back
697  
698 =cut
699  
700 sub decode_find_coordinator_response {
701 my ( $bin_stream_ref, $api_version ) = @_;
702  
703 $api_version //= $DEFAULT_APIVERSION;
704 my $is_v1 = $api_version == 1;
705  
706 my @data = unpack( $is_v1 ? $_decode_find_protocol_response_template_v1
707 : $_decode_find_protocol_response_template, $$bin_stream_ref );
708  
709 my $i = 0;
710 my $FindCoordinator_Response = {};
711  
712 $FindCoordinator_Response->{CorrelationId} = $data[ $i++ ];
713 if ($is_v1) {
714 $FindCoordinator_Response->{ThrottleTimeMs} = $data[ $i++ ]; # only v1
715 }
716 $FindCoordinator_Response->{ErrorCode} = $data[ $i++ ];
717 if ($is_v1) {
718 $FindCoordinator_Response->{ErrorMessage} = $data[ $i++ ]; # only v1
719 }
720 $FindCoordinator_Response->{NodeId} = $data[ $i++ ];
721 $FindCoordinator_Response->{Host} = $data[ $i++ ];
722 $FindCoordinator_Response->{Port} = $data[ $i++ ];
723  
724 return $FindCoordinator_Response;
725 }
726  
727 # PRODUCE Request --------------------------------------------------------------
728  
729 =head3 C<encode_produce_request( $Produce_Request, $compression_codec )>
730  
731 Encodes the argument and returns a reference to the encoded binary string
732 representing a Request buffer.
733  
734 This function takes the following arguments:
735  
736 =over 3
737  
738 =item C<$Produce_Request>
739  
740 C<$Produce_Request> is a reference to the hash representing
741 the structure of the PRODUCE Request (examples see C<t/*_decode_encode.t>).
742  
743 =item C<$compression_codec>
744  
745 Optional.
746  
747 C<$compression_codec> sets the required type of C<$messages> compression,
748 if the compression is desirable.
749  
750 Supported codecs:
751 L<$COMPRESSION_NONE|Kafka/$COMPRESSION_NONE>,
752 L<$COMPRESSION_GZIP|Kafka/$COMPRESSION_GZIP>,
753 L<$COMPRESSION_SNAPPY|Kafka/$COMPRESSION_SNAPPY>,
754 L<$COMPRESSION_LZ4|Kafka/$COMPRESSION_LZ4>.
755  
756 NOTE: $COMPRESSION_LZ4 requires Kafka 0.10 or higher, as initial implementation of LZ4 in Kafka did not follow the standard LZ4 framing specification.
757  
758 =back
759  
760 =cut
761  
762 $IMPLEMENTED_APIVERSIONS->{$APIKEY_PRODUCE} = 2;
763 sub encode_produce_request {
764 my ( $Produce_Request, $compression_codec ) = @_;
765  
766 my @data;
767 my $request = {
768 # template => '...',
769 # len => ...,
770 data => \@data,
771 };
772  
773 _encode_request_header( $request, $APIKEY_PRODUCE, $Produce_Request );
774 # Size
775 # ApiKey
776 # ApiVersion
777 # CorrelationId
778 # ClientId
779  
780 my $topics_array = $Produce_Request->{topics};
781 push( @data,
782 $Produce_Request->{RequiredAcks}, # RequiredAcks
783 $Produce_Request->{Timeout}, # Timeout
784 scalar( @$topics_array ), # topics array size
785 );
786 $request->{template} .= $_ProduceRequest_header_template;
787 $request->{len} += $_ProduceRequest_header_length;
788  
789 foreach my $topic ( @$topics_array ) {
790 $request->{template} .= q{s>}; # string length
791 $request->{len} += 2;
792 _encode_string( $request, $topic->{TopicName} ); # TopicName
793  
794 my $partitions_array = $topic->{partitions};
795 push( @data, scalar( @$partitions_array ) );
796 $request->{template} .= q{l>}; # partitions array size
797 $request->{len} += 4;
798 foreach my $partition ( @$partitions_array ) {
799 push( @data, $partition->{Partition} );
800 $request->{template} .= q{l>}; # Partition
801 $request->{len} += 4;
802  
803 _encode_MessageSet_array( $request, $partition->{MessageSet}, $compression_codec );
804 }
805 }
806  
807 return pack( $request->{template}, $request->{len}, @data );
808 }
809  
810 # PRODUCE Response -------------------------------------------------------------
811  
812 my $_decode_produce_response_template = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>${_int64_template}))};
813 # x[l] # Size (skip)
814 # l> # CorrelationId
815  
816 # l> # topics array size
817 # X[l]
818 # l>/( # topics array
819 # s>/a # TopicName
820  
821 # l> # partitions array size
822 # X[l]
823 # l>/( # partitions array
824 # l> # Partition
825 # s> # ErrorCode
826 # $_int64_template # Offset
827 # )
828 # )
829  
830 my $_decode_produce_response_template_v1 = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>${_int64_template}))l>};
831 # x[l] # Size (skip)
832 # l> # CorrelationId
833  
834 # l> # topics array size
835 # X[l]
836 # l>/( # topics array
837 # s>/a # TopicName
838  
839 # l> # partitions array size
840 # X[l]
841 # l>/( # partitions array
842 # l> # Partition
843 # s> # ErrorCode
844 # $_int64_template # Offset
845 # )
846 # )
847 # l> # Throttle_Time_Ms
848  
849 my $_decode_produce_response_template_v2 = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>${_int64_template}${_int64_template}))l>};
850 # x[l] # Size (skip)
851 # l> # CorrelationId
852  
853 # l> # topics array size
854 # X[l]
855 # l>/( # topics array
856 # s>/a # TopicName
857  
858 # l> # partitions array size
859 # X[l]
860 # l>/( # partitions array
861 # l> # Partition
862 # s> # ErrorCode
863 # $_int64_template # Offset
864 # $_int64_template # Log_Append_Time
865 # )
866 # )
867 # l> # Throttle_Time_Ms
868  
869 =head3 C<decode_produce_response( $bin_stream_ref )>
870  
871 Decodes the argument and returns a reference to the hash representing
872 the structure of the PRODUCE Response (examples see C<t/*_decode_encode.t>).
873  
874 This function takes the following arguments:
875  
876 =over 3
877  
878 =item C<$bin_stream_ref>
879  
880 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
881 must be a non-empty binary string.
882  
883 =back
884  
885 =cut
886 sub decode_produce_response {
887 my ( $bin_stream_ref, $api_version ) = @_;
888  
889 $api_version //= $DEFAULT_APIVERSION;
890 my $is_v1 = $api_version == 1;
891 my $is_v2 = $api_version == 2;
892  
893 my @data = unpack( $is_v1 ? $_decode_produce_response_template_v1
894 : $is_v2 ? $_decode_produce_response_template_v2
895 : $_decode_produce_response_template, $$bin_stream_ref );
896  
897 my $i = 0;
898 my $Produce_Response = {};
899  
900 $Produce_Response->{CorrelationId} = $data[ $i++ ]; # CorrelationId
901  
902 my $topics_array = $Produce_Response->{topics} = [];
903 my $topics_array_size = $data[ $i++ ]; # topics array size
904 while ( $topics_array_size-- ) {
905 my $topic = {
906 TopicName => $data[ $i++ ],
907 };
908  
909 my $partitions_array = $topic->{partitions} = [];
910 my $partitions_array_size = $data[ $i++ ]; # partitions array size
911 while ( $partitions_array_size-- ) {
912 my $partition = {
913 Partition => $data[ $i++ ], # Partition
914 ErrorCode => $data[ $i++ ], # ErrorCode
915 Offset => _unpack64( $data[ $i++ ] ), # Offset
916 ( $is_v2 ? (Log_Append_Time => _unpack64( $data[ $i++ ] )) : () ), # Log_Append_Time
917 };
918  
919 push( @$partitions_array, $partition );
920 }
921  
922 push( @$topics_array, $topic );
923 }
924 defined $data[ $i ]
925 and $Produce_Response->{Throttle_Time_Ms} = $data[ $i++ ];
926  
927 return $Produce_Response;
928 }
929  
930 # FETCH Request ----------------------------------------------------------------
931  
932 =head3 C<encode_fetch_request( $Fetch_Request )>
933  
934 Encodes the argument and returns a reference to the encoded binary string
935 representing a Request buffer.
936  
937 This function takes the following arguments:
938  
939 =over 3
940  
941 =item C<$Fetch_Request>
942  
943 C<$Fetch_Request> is a reference to the hash representing
944 the structure of the FETCH Request (examples see C<t/*_decode_encode.t>).
945  
946 =back
947  
948 =cut
949  
950 # version 0, 1, 2 have the same request protocol.
951 $IMPLEMENTED_APIVERSIONS->{$APIKEY_FETCH} = 3;
952 sub encode_fetch_request {
953 my ( $Fetch_Request ) = @_;
954  
955 my @data;
956 my $request = {
957 # template => '...',
958 # len => ...,
959 data => \@data,
960 };
961 my $api_version = _encode_request_header( $request, $APIKEY_FETCH, $Fetch_Request );
962 # Size
963 # ApiKey
964 # ApiVersion
965 # CorrelationId
966 # ClientId
967 my $is_v3 = $api_version == 3;
968  
969 push( @data, $CONSUMERS_REPLICAID ); # ReplicaId
970 my $topics_array = $Fetch_Request->{topics};
971 push( @data,
972 $Fetch_Request->{MaxWaitTime}, # MaxWaitTime
973 $Fetch_Request->{MinBytes}, # MinBytes
974 ( $is_v3 ? $Fetch_Request->{MaxBytes} : () ), # MaxBytes (version 3 only)
975 scalar( @$topics_array ), # topics array size
976 );
977 if ($is_v3) {
978 $request->{template} .= $_FetchRequest_header_template_v3;
979 $request->{len} += $_FetchRequest_header_length_v3;
980 } else {
981 $request->{template} .= $_FetchRequest_header_template;
982 $request->{len} += $_FetchRequest_header_length;
983 }
984  
985 foreach my $topic ( @$topics_array ) {
986 $request->{template} .= q{s>}; # string length
987 $request->{len} += 2;
988 _encode_string( $request, $topic->{TopicName} ); # TopicName
989  
990 my $partitions_array = $topic->{partitions};
991 push( @data, scalar( @$partitions_array ) );
992 $request->{template} .= q{l>}; # partitions array size
993 $request->{len} += 4;
994 foreach my $partition ( @$partitions_array ) {
995 push( @data,
996 $partition->{Partition}, # Partition
997 _pack64( $partition->{FetchOffset} ), # FetchOffset
998 $partition->{MaxBytes}, # MaxBytes
999 );
1000 $request->{template} .= $_FetchRequest_body_template;
1001 $request->{len} += $_FetchRequest_body_length;
1002 }
1003 }
1004  
1005 return pack( $request->{template}, $request->{len}, @data );
1006 }
1007  
1008 # FETCH Response ---------------------------------------------------------------
1009  
1010 =head3 C<decode_fetch_response( $bin_stream_ref )>
1011  
1012 Decodes the argument and returns a reference to the hash representing
1013 the structure of the FETCH Response (examples see C<t/*_decode_encode.t>).
1014  
1015 This function takes the following arguments:
1016  
1017 =over 3
1018  
1019 =item C<$bin_stream_ref>
1020  
1021 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
1022 must be a non-empty binary string.
1023  
1024 =back
1025  
1026 =cut
1027 sub decode_fetch_response {
1028 my ( $bin_stream_ref, $api_version ) = @_;
1029  
1030 $api_version //= $DEFAULT_APIVERSION;
1031 my $is_v1 = $api_version == 1;
1032 my $is_v2 = $api_version == 2;
1033 my $is_v3 = $api_version == 3;
1034  
1035  
1036 # According to Apache Kafka documentation:
1037 # As an optimization the server is allowed to return a partial message at the end of the message set.
1038 # Clients should handle this case.
1039 # NOTE: look inside _decode_MessageSet_template and _decode_MessageSet_array
1040  
1041 my @data;
1042 my $response = {
1043 # template => '...',
1044 # stream_offset => ...,
1045 data => \@data,
1046 bin_stream => $bin_stream_ref,
1047 };
1048  
1049 _decode_fetch_response_template( $response, $api_version );
1050 @data = unpack( $response->{template}, $$bin_stream_ref );
1051  
1052 my $i = 0;
1053 my $Fetch_Response = {};
1054  
1055 $Fetch_Response->{CorrelationId} = $data[ $i++ ]; # CorrelationId
1056 if ($is_v1 || $is_v2 || $is_v3) {
1057 $Fetch_Response->{ThrottleTimeMs} = $data[ $i++ ]; # ( ThrottleTimeMs ) only v1 and above
1058 }
1059  
1060 my $topics_array = $Fetch_Response->{topics} = [];
1061 my $topics_array_size = $data[ $i++ ]; # topics array size
1062 while ( $topics_array_size-- ) {
1063 my $topic = {
1064 TopicName => $ data[ $i++ ], # TopicName
1065 };
1066  
1067 my $partitions_array = $topic->{partitions} = [];
1068 my $partitions_array_size = $data[ $i++ ]; # partitions array size
1069 my ( $MessageSetSize, $MessageSet_array );
1070 while ( $partitions_array_size-- ) {
1071 my $partition = {
1072 Partition => $data[ $i++ ], # Partition
1073 ErrorCode => $data[ $i++ ], # ErrorCode
1074 HighwaterMarkOffset => _unpack64( $data[ $i++ ] ), # HighwaterMarkOffset
1075 };
1076  
1077 $MessageSetSize = $data[ $i++ ]; # MessageSetSize
1078 $MessageSet_array = $partition->{MessageSet} = [];
1079  
1080 _decode_MessageSet_array( $response, $MessageSetSize, \$i, $MessageSet_array );
1081  
1082 push( @$partitions_array, $partition );
1083 }
1084  
1085 push( @$topics_array, $topic );
1086 }
1087  
1088 return $Fetch_Response;
1089 }
1090  
1091 # OFFSET Request ---------------------------------------------------------------
1092  
1093 =head3 C<encode_offset_request( $Offset_Request )>
1094  
1095 Encodes the argument and returns a reference to the encoded binary string
1096 representing a Request buffer.
1097  
1098 This function takes the following arguments:
1099  
1100 =over 3
1101  
1102 =item C<$Offset_Request>
1103  
1104 C<$Offset_Request> is a reference to the hash representing
1105 the structure of the OFFSET Request (examples see C<t/*_decode_encode.t>).
1106  
1107 =back
1108  
1109 =cut
1110 $IMPLEMENTED_APIVERSIONS->{$APIKEY_OFFSET} = 1;
1111 sub encode_offset_request {
1112 my ( $Offset_Request ) = @_;
1113  
1114 my @data;
1115 my $request = {
1116 # template => '...',
1117 # len => ...,
1118 data => \@data,
1119 };
1120 my $api_version = _encode_request_header( $request, $APIKEY_OFFSET, $Offset_Request );
1121 # Size
1122 # ApiKey
1123 # ApiVersion
1124 # CorrelationId
1125 # ClientId
1126  
1127 my $is_v1 = $api_version == 1;
1128  
1129 my $topics_array = $Offset_Request->{topics};
1130 push( @data,
1131 $CONSUMERS_REPLICAID, # ReplicaId
1132 scalar( @$topics_array ), # topics array size
1133 );
1134 $request->{template} .= $_OffsetRequest_header_template;
1135 $request->{len} += $_OffsetRequest_header_length;
1136  
1137 my $body_template = $is_v1 ? $_OffsetRequest_body_template_v1 : $_OffsetRequest_body_template;
1138 my $body_length = $is_v1 ? $_OffsetRequest_body_length_v1 : $_OffsetRequest_body_length;
1139 foreach my $topic ( @$topics_array ) {
1140 $request->{template} .= q{s>}; # string length
1141 $request->{len} += 2;
1142 _encode_string( $request, $topic->{TopicName} ); # TopicName
1143  
1144 my $partitions_array = $topic->{partitions};
1145 push( @data, scalar( @$partitions_array ) );
1146 $request->{template} .= q{l>}; # partitions array size
1147 $request->{len} += 4; # [l] partitions array size
1148 foreach my $partition ( @$partitions_array ) {
1149 push( @data,
1150 $partition->{Partition}, # Partition
1151 _pack64( $partition->{Time} ), # Time
1152 $is_v1 ? () : $partition->{MaxNumberOfOffsets}, # MaxNumberOfOffsets
1153 );
1154 $request->{template} .= $body_template;
1155 $request->{len} += $body_length;
1156 }
1157 }
1158  
1159 # say STDERR $request->{template};
1160 # say STDERR HexDump($_) foreach @data; use Data::HexDump;
1161 return pack( $request->{template}, $request->{len}, @data );
1162 }
1163  
1164 # OFFSET Response --------------------------------------------------------------
1165  
1166 my $_decode_offset_response_template = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>l>X[l]l>/(${_int64_template})))};
1167 # x[l] # Size (skip)
1168 # l> # CorrelationId
1169  
1170 # l> # topics array size
1171 # X[l]
1172 # l>/( # topics array
1173 # s>/a # TopicName
1174  
1175 # l> # PartitionOffsets array size
1176 # X[l]
1177 # l>/( # PartitionOffsets array
1178 # l> # Partition
1179 # s> # ErrorCode
1180  
1181 # l> # Offset array size
1182 # X[l]
1183 # l>/( # Offset array
1184 # $_int64_template # Offset
1185 # )
1186 # )
1187 # )
1188  
1189 my $_decode_offset_response_template_v1 = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>${_int64_template}${_int64_template}))};
1190 # x[l] # Size (skip)
1191 # l> # CorrelationId
1192  
1193 # l> # topics array size
1194 # X[l]
1195 # l>/( # topics array
1196 # s>/a # TopicName
1197  
1198 # l> # PartitionOffsets array size
1199 # X[l]
1200 # l>/( # PartitionOffsets array
1201 # l> # Partition
1202 # s> # ErrorCode
1203 # $_int64_template # Timestamp
1204 # $_int64_template # Offset
1205 # )
1206 # )
1207  
1208 =head3 C<decode_offset_response( $bin_stream_ref )>
1209  
1210 Decodes the argument and returns a reference to the hash representing
1211 the structure of the OFFSET Response (examples see C<t/*_decode_encode.t>).
1212  
1213 This function takes the following arguments:
1214  
1215 =over 3
1216  
1217 =item C<$bin_stream_ref>
1218  
1219 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
1220 must be a non-empty binary string.
1221  
1222 =back
1223  
1224 =cut
1225 sub decode_offset_response {
1226 my ( $bin_stream_ref, $api_version ) = @_;
1227  
1228 $api_version //= $DEFAULT_APIVERSION;
1229 my $is_v1 = $api_version == 1;
1230 my $template = $is_v1 ? $_decode_offset_response_template_v1 : $_decode_offset_response_template;
1231  
1232 my @data = unpack( $template, $$bin_stream_ref );
1233  
1234 my $i = 0;
1235 my $Offset_Response = { };
1236  
1237 $Offset_Response->{CorrelationId} = $data[ $i++ ]; # CorrelationId
1238  
1239 my $topics_array = $Offset_Response->{topics} = [];
1240 my $topics_array_size = $data[ $i++ ]; # topics array size
1241 while ( $topics_array_size-- ) {
1242 my $topic = {
1243 TopicName => $data[ $i++ ], # TopicName
1244 };
1245  
1246 my $PartitionOffsets_array = $topic->{PartitionOffsets} = [];
1247 my $PartitionOffsets_array_size = $data[ $i++ ]; # PartitionOffsets array size
1248 my ( $PartitionOffset, $Offset_array, $Offset_array_size );
1249 while ( $PartitionOffsets_array_size-- ) {
1250 $PartitionOffset = {
1251 Partition => $data[ $i++ ], # Partition
1252 ErrorCode => $data[ $i++ ], # ErrorCode
1253 };
1254 if ($is_v1) {
1255 $PartitionOffset->{Timestamp} = _unpack64($data[ $i++ ]); # Timestamp (v1 only)
1256 $PartitionOffset->{Offset} = _unpack64($data[ $i++ ]); # Offset (v1 only)
1257  
1258 } else {
1259 $Offset_array = $PartitionOffset->{Offset} = [];
1260 $Offset_array_size = $data[ $i++ ]; # Offset array size
1261 while ( $Offset_array_size-- ) {
1262 push( @$Offset_array, _unpack64( $data[ $i++ ] ) ); # Offset
1263 }
1264 }
1265  
1266 push( @$PartitionOffsets_array, $PartitionOffset );
1267 }
1268  
1269 push( @$topics_array, $topic );
1270 }
1271  
1272 return $Offset_Response;
1273 }
1274  
1275 # METADATA Request -------------------------------------------------------------
1276  
1277 =head3 C<encode_metadata_request( $Metadata_Request )>
1278  
1279 Encodes the argument and returns a reference to the encoded binary string
1280 representing a Request buffer.
1281  
1282 This function takes the following arguments:
1283  
1284 =over 3
1285  
1286 =item C<$Metadata_Request>
1287  
1288 C<$Metadata_Request> is a reference to the hash representing
1289 the structure of the METADATA Request (examples see C<t/*_decode_encode.t>).
1290  
1291 =back
1292  
1293 =cut
1294 $IMPLEMENTED_APIVERSIONS->{$APIKEY_METADATA} = 0;
1295 sub encode_metadata_request {
1296 my ( $Metadata_Request ) = @_;
1297  
1298 my @data;
1299 my $request = {
1300 # template => '...',
1301 # len => ...,
1302 data => \@data,
1303 };
1304  
1305 _encode_request_header( $request, $APIKEY_METADATA, $Metadata_Request );
1306 # Size
1307 # ApiKey
1308 # ApiVersion
1309 # CorrelationId
1310 # ClientId
1311  
1312 my $topics_array = $Metadata_Request->{topics};
1313 push( @data, scalar( @$topics_array ) ); # topics array size
1314 $request->{template} .= q{l>};
1315 $request->{len} += 4;
1316  
1317 foreach my $topic ( @$topics_array ) {
1318 $request->{template} .= q{s>}; # string length
1319 $request->{len} += 2;
1320 _encode_string( $request, $topic ); # TopicName
1321 }
1322  
1323 return pack( $request->{template}, $request->{len}, @data );
1324 }
1325  
1326 # METADATA Response ------------------------------------------------------------
1327  
1328 my $_decode_metadata_response_template = q{x[l]l>l>X[l]l>/(l>s>/al>)l>X[l]l>/(s>s>/al>X[l]l>/(s>l>l>l>X[l]l>/(l>)l>X[l]l>/(l>)))};
1329 # x[l] # Size (skip)
1330 # l> # CorrelationId
1331  
1332 # l> # Broker array size
1333 # X[l]
1334 # l>/( # Broker array
1335 # l> # NodeId
1336 # s>/a # Host
1337 # l> # Port
1338 # )
1339  
1340 # l> # TopicMetadata array size
1341 # X[l]
1342 # l>/( # TopicMetadata array
1343 # s> # ErrorCode
1344 # s>/a # TopicName
1345  
1346 # l> # PartitionMetadata array size
1347 # X[l]
1348 # l>/( # PartitionMetadata array
1349 # s> # ErrorCode
1350 # l> # Partition
1351 # l> # Leader
1352  
1353 # l> # Replicas array size
1354 # X[l]
1355 # l>/( # Replicas array
1356 # l> # ReplicaId
1357 # )
1358  
1359 # l> # Isr array size
1360 # X[l]
1361 # l>/( # Isr array
1362 # l> # ReplicaId
1363 # )
1364 # )
1365 # )
1366  
1367 =head3 C<decode_metadata_response( $bin_stream_ref )>
1368  
1369 Decodes the argument and returns a reference to the hash representing
1370 the structure of the METADATA Response (examples see C<t/*_decode_encode.t>).
1371  
1372 This function takes the following arguments:
1373  
1374 =over 3
1375  
1376 =item C<$bin_stream_ref>
1377  
1378 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
1379 must be a non-empty binary string.
1380  
1381 =back
1382  
1383 =cut
1384 sub decode_metadata_response {
1385 my ( $bin_stream_ref ) = @_;
1386  
1387 my @data = unpack( $_decode_metadata_response_template, $$bin_stream_ref );
1388  
1389 my $i = 0;
1390 my $Metadata_Response = {};
1391  
1392 $Metadata_Response->{CorrelationId} = $data[ $i++ ]; # CorrelationId
1393  
1394 my $Broker_array = $Metadata_Response->{Broker} = [];
1395 my $Broker_array_size = $data[ $i++ ]; # Broker array size
1396 while ( $Broker_array_size-- ) {
1397 push( @$Broker_array, {
1398 NodeId => $data[ $i++ ], # NodeId
1399 Host => $data[ $i++ ], # Host
1400 Port => $data[ $i++ ], # Port
1401 }
1402 );
1403 }
1404  
1405 my $TopicMetadata_array = $Metadata_Response->{TopicMetadata} = [];
1406 my $TopicMetadata_array_size = $data[ $i++ ]; # TopicMetadata array size
1407 while ( $TopicMetadata_array_size-- ) {
1408 my $TopicMetadata = {
1409 ErrorCode => $data[ $i++ ], # ErrorCode
1410 TopicName => $data[ $i++ ], # TopicName
1411 };
1412  
1413 my $PartitionMetadata_array = $TopicMetadata->{PartitionMetadata} = [];
1414 my $PartitionMetadata_array_size = $data[ $i++ ]; # PartitionMetadata array size
1415 while ( $PartitionMetadata_array_size-- ) {
1416 my $PartitionMetadata = {
1417 ErrorCode => $data[ $i++ ], # ErrorCode
1418 Partition => $data[ $i++ ], # Partition
1419 Leader => $data[ $i++ ], # Leader
1420 };
1421  
1422 my $Replicas_array = $PartitionMetadata->{Replicas} = [];
1423 my $Replicas_array_size = $data[ $i++ ]; # Replicas array size
1424 while ( $Replicas_array_size-- ) {
1425 push( @$Replicas_array, $data[ $i++ ] ); # ReplicaId
1426 }
1427  
1428 my $Isr_array = $PartitionMetadata->{Isr} = [];
1429 my $Isr_array_size = $data[ $i++ ]; # Isr array size
1430 while ( $Isr_array_size-- ) {
1431 push( @$Isr_array, $data[ $i++ ] ); # ReplicaId
1432 }
1433  
1434 push( @$PartitionMetadata_array, $PartitionMetadata );
1435 }
1436  
1437 push( @$TopicMetadata_array, $TopicMetadata );
1438 }
1439  
1440 return $Metadata_Response;
1441 }
1442  
1443 # OffsetCommit Request -------------------------------------------------------------
1444  
1445 =head3 C<encode_offsetcommit_request( $OffsetCommit_Request )>
1446  
1447 Encodes the argument and returns a reference to the encoded binary string
1448 representing a Request buffer.
1449  
1450 This function takes the following arguments:
1451  
1452 =over 3
1453  
1454 =item C<$OffsetCommit_Request>
1455  
1456 C<$OffsetCommit_Request> is a reference to the hash representing
1457 the structure of the OffsetCommit Request (examples see C<t/*_decode_encode.t>).
1458  
1459 =back
1460  
1461 =cut
1462 $IMPLEMENTED_APIVERSIONS->{$APIKEY_OFFSETCOMMIT} = 1;
1463 sub encode_offsetcommit_request {
1464 my ( $OffsetCommit_Request ) = @_;
1465  
1466 my @data;
1467 my $request = {
1468 # template => '...',
1469 # len => ...,
1470 data => \@data,
1471 };
1472  
1473 my $api_version = _encode_request_header( $request, $APIKEY_OFFSETCOMMIT, $OffsetCommit_Request );
1474 # Size
1475 # ApiKey
1476 # ApiVersion
1477 # CorrelationId
1478 # ClientId
1479  
1480 my $is_v1 = $api_version == 1;
1481  
1482 $request->{template} .= q{s>};
1483 $request->{len} += 2;
1484 _encode_string( $request, $OffsetCommit_Request->{GroupId} ); # GroupId
1485  
1486 if ($is_v1) {
1487 $request->{template} .= q{l>}; # GroupGenerationId
1488 $request->{len} += 4;
1489 ### WARNING TODO: we don't support consumer groups properly, so we set
1490 ### generation id to -1 and member id null (see
1491 ### https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol )
1492 push( @data, -1 );
1493  
1494 $request->{template} .= q{s>}; # string length
1495 $request->{len} += 2;
1496 _encode_string( $request, '' ); # MemberId
1497 }
1498  
1499 my $topics_array = $OffsetCommit_Request->{topics};
1500 push( @data, scalar( @$topics_array ) ); # topics array size
1501 $request->{template} .= q{l>};
1502 $request->{len} += 4;
1503  
1504 foreach my $topic ( @$topics_array ) {
1505 $request->{template} .= q{s>};
1506 $request->{len} += 2;
1507 _encode_string( $request, $topic->{TopicName} ); # TopicName
1508  
1509 my $partitions_array = $topic->{partitions};
1510 push( @data, scalar( @$partitions_array ) ); # partitions array size
1511 $request->{template} .= q{l>};
1512 $request->{len} += 4;
1513  
1514 foreach my $partition ( @$partitions_array ){
1515 push( @data,
1516 $partition->{Partition}, # Partition
1517 $partition->{Offset}, # Offset
1518 );
1519 $request->{template} .= qq{l>${_int64_template}};
1520 $request->{len} += 12;
1521  
1522 if ($is_v1) { # timestamp
1523 ### WARNING TODO: currently we hardcode "now"
1524 push( @data, time() * 1000);
1525 $request->{template} .= qq{${_int64_template}};
1526 $request->{len} += 8;
1527 }
1528  
1529 $request->{template} .= q{s>};
1530 $request->{len} += 2;
1531 _encode_string( $request, $partition->{Metadata} ), # Metadata
1532 }
1533 }
1534 return pack( $request->{template}, $request->{len}, @data );
1535 }
1536  
1537 # OFFSETCOMMIT Response --------------------------------------------------------------
1538  
1539 my $_decode_offsetcommit_response_template = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>s>))};
1540 # x[l] # Size (skip)
1541 # l> # CorrelationId
1542 # l> # topics array size
1543 # X[l]
1544 # l>/( # topics array
1545 # s>/a # TopicName
1546 # l> # partitions array size
1547 # X[l]
1548 # l>/( # partitions array
1549 # l> # Partition
1550 # s> # ErrorCode
1551 # )
1552 # )
1553  
1554 =head3 C<decode_offsetcommit_response( $bin_stream_ref )>
1555  
1556 Decodes the argument and returns a reference to the hash representing
1557 the structure of the OFFSETCOMMIT Response (examples see C<t/*_decode_encode.t>).
1558  
1559 This function takes the following arguments:
1560  
1561 =over 3
1562  
1563 =item C<$bin_stream_ref>
1564  
1565 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
1566 must be a non-empty binary string.
1567  
1568 =back
1569  
1570 =cut
1571 sub decode_offsetcommit_response {
1572 my ( $bin_stream_ref ) = @_;
1573  
1574 my @data = unpack( $_decode_offsetcommit_response_template, $$bin_stream_ref );
1575  
1576 my $i = 0;
1577 my $OffsetCommit_Response = {};
1578  
1579 $OffsetCommit_Response->{CorrelationId} = $data[ $i++ ]; #CorrelationId
1580  
1581 my $topics_array = $OffsetCommit_Response->{topics} = [];
1582 my $topics_array_size = $data[ $i++ ]; # topics array size
1583 while ( $topics_array_size-- ) {
1584 my $topic = {
1585 TopicName => $data[ $i++ ], # TopicName
1586 };
1587  
1588 my $Partition_array = $topic->{partitions} = [];
1589 my $Partition_array_size = $data[ $i++ ]; # Partitions array size
1590 while ( $Partition_array_size-- ) {
1591 my $Partition = {
1592 Partition => $data[ $i++ ], # Partition
1593 ErrorCode => $data[ $i++ ], # ErrorCode
1594 };
1595 push( @$Partition_array, $Partition);
1596 }
1597 push( @$topics_array, $topic);
1598 }
1599 return $OffsetCommit_Response;
1600 }
1601  
1602 # OffsetFetch Request -------------------------------------------------------------
1603  
1604 =head3 C<encode_offsetfetch_request( $OffsetFetch_Request )>
1605  
1606 Encodes the argument and returns a reference to the encoded binary string
1607 representing a Request buffer.
1608  
1609 This function takes the following arguments:
1610  
1611 =over 3
1612  
1613 =item C<$OffsetFetch_Request>
1614  
1615 C<$OffsetFetch_Request> is a reference to the hash representing
1616 the structure of the OffsetFetch Request (examples see C<t/*_decode_encode.t>).
1617  
1618 =back
1619  
1620 =cut
1621 $IMPLEMENTED_APIVERSIONS->{$APIKEY_OFFSETFETCH} = 1;
1622 sub encode_offsetfetch_request {
1623 my ( $OffsetFetch_Request ) = @_;
1624  
1625 my @data;
1626 my $request = {
1627 # template => '...',
1628 # len => ...,
1629 data => \@data,
1630 };
1631  
1632 _encode_request_header( $request, $APIKEY_OFFSETFETCH, $OffsetFetch_Request );
1633 # Size
1634 # ApiKey
1635 # ApiVersion
1636 # CorrelationId
1637 # ClientId
1638  
1639 $request->{template} .= q{s>};
1640 $request->{len} += 2;
1641 _encode_string( $request, $OffsetFetch_Request->{GroupId} ); # GroupId
1642  
1643 my $topics_array = $OffsetFetch_Request->{topics};
1644 push( @data, scalar( @$topics_array ) ); # topics array size
1645 $request->{template} .= q{l>};
1646 $request->{len} += 4;
1647  
1648 foreach my $topic ( @$topics_array ) {
1649 $request->{template} .= q{s>};
1650 $request->{len} += 2;
1651 _encode_string( $request, $topic->{TopicName} ); # TopicName
1652  
1653 my $partitions_array = $topic->{partitions};
1654 push( @data, scalar( @$partitions_array ) ); # partitions array size
1655 $request->{template} .= q{l>};
1656 $request->{len} += 4;
1657  
1658 foreach my $partition ( @$partitions_array ){
1659 push( @data,
1660 $partition->{Partition}, # Partition
1661 );
1662 $request->{template} .= qq{l>};
1663 $request->{len} += 4;
1664 }
1665 }
1666 return pack( $request->{template}, $request->{len}, @data );
1667 }
1668  
1669 # OFFSETFETCH Response --------------------------------------------------------------
1670  
1671 my $_decode_offsetfetch_response_template = qq{x[l]l>l>X[l]l>/(s>/al>X[l]l>/(l>${_int64_template}s>/as>))};
1672 # x[l] # Size (skip)
1673 # l> # CorrelationId
1674 # l> # topics array size
1675 # X[l]
1676 # l>/( # topics array
1677 # s>/a # TopicName
1678 # l> # partitions array size
1679 # X[l]
1680 # l>/( # partitions array
1681 # l> # Partition
1682 # $_int64_template # Offset
1683 # s>/a # Metadata
1684 # s> # ErrorCode
1685 # )
1686 # )
1687  
1688 =head3 C<decode_offsetfetch_response( $bin_stream_ref )>
1689  
1690 Decodes the argument and returns a reference to the hash representing
1691 the structure of the OFFSETFETCH Response (examples see C<t/*_decode_encode.t>).
1692  
1693 This function takes the following arguments:
1694  
1695 =over 3
1696  
1697 =item C<$bin_stream_ref>
1698  
1699 C<$bin_stream_ref> is a reference to the encoded Response buffer. The buffer
1700 must be a non-empty binary string.
1701  
1702 =back
1703  
1704 =cut
1705 sub decode_offsetfetch_response {
1706 my ( $bin_stream_ref ) = @_;
1707  
1708 my @data = unpack( $_decode_offsetfetch_response_template, $$bin_stream_ref );
1709  
1710 my $i = 0;
1711 my $OffsetFetch_Response = {};
1712  
1713 $OffsetFetch_Response->{CorrelationId} = $data[ $i++ ]; #CorrelationId
1714  
1715 my $topics_array = $OffsetFetch_Response->{topics} = [];
1716 my $topics_array_size = $data[ $i++ ]; # topics array size
1717 while ( $topics_array_size-- ) {
1718 my $topic = {
1719 TopicName => $data[ $i++ ], # TopicName
1720 };
1721  
1722 my $Partition_array = $topic->{partitions} = [];
1723 my $Partition_array_size = $data[ $i++ ]; # Partitions array size
1724 while ( $Partition_array_size-- ) {
1725 my $Partition = {
1726 Partition => $data[ $i++ ], # Partition
1727 Offset => $data[ $i++ ], # Partition
1728 Metadata => $data[ $i++ ], # Partition
1729 ErrorCode => $data[ $i++ ], # ErrorCode
1730 };
1731 push( @$Partition_array, $Partition);
1732 }
1733 push( @$topics_array, $topic);
1734 }
1735 return $OffsetFetch_Response;
1736 }
1737  
1738 #-- private functions ----------------------------------------------------------
1739  
1740 # Generates a template to encrypt the request header
1741 sub _encode_request_header {
1742 my ( $request, $api_key, $request_ref ) = @_;
1743  
1744 # we need to find out which API version to use for the request (and the
1745 # response). $request_ref->{ApiVersion} can be specified by the end user,
1746 # or providede by by Kafka::Connection ( see
1747 # Kafka::Connection::_get_api_versions() ). If not provided, we
1748 # default to $DEFAULT_APIVERSION
1749 my $api_version = $request_ref->{ApiVersion} // $DEFAULT_APIVERSION;
1750 @{ $request->{data} } = (
1751 # Size
1752 $api_key, # ApiKey
1753 $api_version, # ApiVersion
1754 $request_ref->{CorrelationId}, # CorrelationId
1755 );
1756 $request->{template} = $_Request_header_template;
1757 $request->{len} = $_Request_header_length;
1758 _encode_string( $request, $request_ref->{ClientId} ); # ClientId
1759  
1760 return $api_version;
1761 }
1762  
1763 # Generates a template to decrypt the fetch response body
1764 sub _decode_fetch_response_template {
1765 my ( $response, $api_version ) = @_;
1766  
1767 my $is_v1 = $api_version == 1;
1768 my $is_v2 = $api_version == 2;
1769 my $is_v3 = $api_version == 3;
1770  
1771 if ($is_v1 || $is_v2 || $is_v3) {
1772 $response->{template} = $_FetchResponse_header_template_v1;
1773 $response->{stream_offset} = $_FetchResponse_header_length_v1; # bytes before topics array size
1774 # [l] Size
1775 # [l] CorrelationId
1776 # [l] throttle_time_ms
1777 } else {
1778 $response->{template} = $_FetchResponse_header_template;
1779 $response->{stream_offset} = $_FetchResponse_header_length; # bytes before topics array size
1780 # [l] Size
1781 # [l] CorrelationId
1782 }
1783 my $topics_array_size = unpack(
1784 q{x[}.$response->{stream_offset} . q{]}
1785 .q{l>}, # topics array size
1786 ${ $response->{bin_stream} }
1787 );
1788 $response->{stream_offset} += 4; # bytes before TopicName length
1789 # [l] topics array size
1790  
1791 my ( $TopicName_length, $partitions_array_size );
1792 while ( $topics_array_size-- ) {
1793 $TopicName_length = unpack(
1794 q{x[}.$response->{stream_offset}
1795 .q{]s>}, # TopicName length
1796 ${ $response->{bin_stream} }
1797 );
1798 $response->{stream_offset} += # bytes before partitions array size
1799 2 # [s] TopicName length
1800 + $TopicName_length # TopicName
1801 ;
1802 $partitions_array_size = unpack(
1803 q{x[}.$response->{stream_offset}
1804 .q{]l>}, # partitions array size
1805 ${ $response->{bin_stream} }
1806 );
1807 $response->{stream_offset} += 4; # bytes before Partition
1808 # [l] partitions array size
1809  
1810 $response->{template} .= $_FetchResponse_topic_body_template;
1811 $response->{stream_offset} += $_FetchResponse_topic_body_length; # (without TopicName and partitions array size)
1812 # bytes before MessageSetSize
1813 # TopicName
1814 # [l] # partitions array size
1815 # [l] Partition
1816 # [s] ErrorCode
1817 # [q] HighwaterMarkOffset
1818  
1819 _decode_MessageSet_template( $response );
1820 }
1821  
1822 return;
1823 }
1824  
1825 # kafka uses a snappy-java compressor, with it's own headers and frame formats
1826 my $XERIAL_SNAPPY_MAGIC_HEADER = "\x82SNAPPY\x00";
1827 my $XERIAL_SNAPPY_BLOCK_SIZE = 0x8000; # 32Kb
1828 my $XERIAL_SNAPPY_FILE_VERSION = 1;
1829  
1830 # https://github.com/xerial/snappy-java
1831 # https://github.com/kubo/snzip/blob/master/snappy-java-format.c
1832 sub _snappy_xerial_decompress {
1833 my ( $data ) = @_;
1834 my $uncompressed;
1835 my $raw_format_suspected = 1;
1836 if ( length($data) > 16 ) {
1837 my ( $header, $x_version, $x_compatversion) = unpack( q{a[8]L>L>}, $data );
1838 if ( $header eq $XERIAL_SNAPPY_MAGIC_HEADER ) {
1839 $raw_format_suspected = 0;
1840 _error( $ERROR_COMPRESSION, 'snappy: bad compatversion in snappy xerial header' ) unless $x_compatversion == $XERIAL_SNAPPY_FILE_VERSION;
1841 _error( $ERROR_COMPRESSION, 'snappy: bad version in snappy xerial header' ) unless $x_version == $XERIAL_SNAPPY_FILE_VERSION;
1842 $data = substr( $data, 16 );
1843 while ( length($data) > 0 ) {
1844 $uncompressed //= '';
1845 _error( $ERROR_COMPRESSION, 'snappy: bad frame length header' ) if length($data) < 4;
1846 my $compressed_frame_length = unpack( q{L>}, $data );
1847 $data = substr( $data, 4 );
1848 _error( $ERROR_COMPRESSION, 'snappy: partial frame ' ) if length($data) < $compressed_frame_length;
1849 my $compressed_frame = substr( $data, 0, $compressed_frame_length, '');
1850 my $uncompressed_frame = Compress::Snappy::decompress( $compressed_frame );
1851 _error( $ERROR_COMPRESSION, 'snappy: can\'t uncompress frame ' ) if not defined $uncompressed_frame;
1852 $uncompressed .= $uncompressed_frame;
1853 }
1854 }
1855 }
1856 if ( $raw_format_suspected ) {
1857 $uncompressed = Compress::Snappy::decompress( $data );
1858 }
1859 return $uncompressed;
1860 }
1861  
1862 sub _snappy_xerial_compress {
1863 my ( $data ) = @_;
1864 my $compressed_data;
1865 while ( length($data) ) {
1866 my $block = substr $data, 0, $XERIAL_SNAPPY_BLOCK_SIZE, '';
1867 my $compressed_block = Compress::Snappy::compress( $block );
1868 _error( $ERROR_COMPRESSION, 'snappy: can\'t compress frame!' ) if not defined $compressed_block;
1869 $compressed_data //= pack( q{a[8]L>L>}, $XERIAL_SNAPPY_MAGIC_HEADER, $XERIAL_SNAPPY_FILE_VERSION, $XERIAL_SNAPPY_FILE_VERSION );
1870 $compressed_data .= pack( q{L>}, length($compressed_block) ) . $compressed_block;
1871 }
1872 return $compressed_data;
1873 }
1874  
1875 sub _decompress_data {
1876 my ( $data, $compression_codec ) = @_;
1877 say STDERR format_message( '[%s] decompress_data request, compression_codec: %s, data: %s',
1878 scalar( localtime ),
1879 $compression_codec,
1880 unpack( 'H*', $data ),
1881 ) if Kafka::Protocol->debug_level // 0 >= 3;
1882  
1883 my $decompressed;
1884 if ( $compression_codec == $COMPRESSION_GZIP ) {
1885 try {
1886 $decompressed = gunzip( $data );
1887 } catch {
1888 _error( $ERROR_COMPRESSION, format_message( 'gunzip failed: %s', $_ ) );
1889 };
1890 } elsif ( $compression_codec == $COMPRESSION_SNAPPY ) {
1891 $decompressed = _snappy_xerial_decompress( $data )
1892 // _error( $ERROR_COMPRESSION, 'Unable to decompress snappy compressed data' );
1893 } elsif ( $compression_codec == $COMPRESSION_LZ4 ) {
1894 # https://cwiki.apache.org/confluence/display/KAFKA/KIP-57+-+Interoperable+LZ4+Framing
1895 # New 0.10 clients (proposed behavior) - produce and consume v1 messages w/ correct LZ4F checksum
1896 if ( Compress::LZ4Frame::looks_like_lz4frame( $data ) ) {
1897 $decompressed = Compress::LZ4Frame::decompress( $data ) // _error( $ERROR_COMPRESSION, 'Unable to decompress LZ4 compressed data' );
1898 } else {
1899 _error( $ERROR_COMPRESSION, 'Unable to decompress LZ4 compressed data. Frame is not valid' );
1900 }
1901  
1902 } else {
1903 _error( $ERROR_COMPRESSION, "Unknown compression codec $compression_codec" );
1904 }
1905 _error( $ERROR_COMPRESSION, 'Decompression produced empty result' ) unless defined $decompressed;
1906 return $decompressed;
1907 }
1908  
1909 sub _compress_data {
1910 my ( $data, $compression_codec ) = @_;
1911 my $compressed;
1912  
1913 # Compression
1914 if ( $compression_codec == $COMPRESSION_GZIP ) {
1915 $compressed = gzip( $data )
1916 // _error( $ERROR_COMPRESSION, 'Unable to compress gzip data' );
1917 } elsif ( $compression_codec == $COMPRESSION_SNAPPY ) {
1918 $compressed = _snappy_xerial_compress( $data )
1919 // _error( $ERROR_COMPRESSION, 'Unable to compress snappy data' );
1920 } elsif ( $compression_codec == $COMPRESSION_LZ4 ) {
1921 # https://cwiki.apache.org/confluence/display/KAFKA/KIP-57+-+Interoperable+LZ4+Framing
1922 # New 0.10 clients (proposed behavior) - produce and consume v1 messages w/ correct LZ4F checksum
1923 $compressed = Compress::LZ4Frame::compress_checksum( $data )
1924 // _error( $ERROR_COMPRESSION, 'Unable to compress LZ4 data' );
1925 } else {
1926 _error( $ERROR_COMPRESSION, "Unknown compression codec $compression_codec" );
1927 }
1928 return $compressed;
1929 }
1930  
1931  
1932 # Decrypts MessageSet
1933 sub _decode_MessageSet_array {
1934 my ( $response, $MessageSetSize, $i_ref, $MessageSet_array_ref, $_override_offset) = @_;
1935 # $_override_offset should not be set manually, it's used when recursing,
1936 # to decode internal compressed message, which have offsets starting at 0,
1937 # 1, etc, and instead we need to set the external wrapping message offset.
1938  
1939 my $data = $response->{data};
1940 my $data_array_size = scalar @{ $data };
1941  
1942 # NOTE: not all messages can be returned
1943 my ( $Message, $MessageSize, $Crc, $Key_length, $Value_length );
1944 while ( $MessageSetSize && $$i_ref < $data_array_size ) {
1945  
1946 my $message_offset = _unpack64( $data->[ $$i_ref++ ] );
1947 if (defined $_override_offset) {
1948 $message_offset = $_override_offset;
1949 }
1950 $Message = {
1951 Offset => $message_offset, # Offset
1952 };
1953  
1954 $MessageSize = $data->[ $$i_ref++ ]; # MessageSize
1955 # NOTE: The CRC is the CRC32 of the remainder of the message bytes.
1956 # This is used to check the integrity of the message on the broker and consumer:
1957 # MagicByte + Attributes + Key length + Key + Value length + Value
1958 $Crc = $data->[ $$i_ref++ ]; # Crc
1959 # WARNING: The current version of the module does not support the following:
1960 # A message set is also the unit of compression in Kafka,
1961 # and we allow messages to recursively contain compressed message sets to allow batch compression.
1962 $Message->{MagicByte} = $data->[ $$i_ref++ ]; # MagicByte
1963 my $is_v1_msg_format = $Message->{MagicByte} == 1;
1964 $Message->{Attributes} = $data->[ $$i_ref++ ]; # Attributes
1965 if ($is_v1_msg_format) {
1966 $Message->{Timestamp} = $data->[ $$i_ref++ ]; # Timestamp
1967 }
1968  
1969 $Key_length = $data->[ $$i_ref++ ]; # Key length
1970 $Message->{Key} = $Key_length == $NULL_BYTES_LENGTH ? q{} : $data->[ $$i_ref++ ]; # Key
1971 $Value_length = $data->[ $$i_ref++ ]; # Value length
1972 $Message->{Value} = $Value_length == $NULL_BYTES_LENGTH ? q{} : $data->[ $$i_ref++ ]; # Value
1973  
1974 if ( my $compression_codec = $Message->{Attributes} & $COMPRESSION_CODEC_MASK ) {
1975 my $decompressed = _decompress_data($Message->{Value}, $compression_codec);
1976 my @data;
1977 my $Value_length = length $decompressed;
1978 my $resp = {
1979 data => \@data,
1980 bin_stream => \$decompressed,
1981 stream_offset => 0,
1982 };
1983 _decode_MessageSet_sized_template( $Value_length, $resp );
1984 @data = unpack( $resp->{template}, ${ $resp->{bin_stream} } );
1985 my $i = 0; # i_ref
1986 my $size = length( $decompressed );
1987 _decode_MessageSet_array(
1988 $resp,
1989 $size, # message set size
1990 \$i, # i_ref
1991 $MessageSet_array_ref,
1992 $message_offset
1993 );
1994 } else {
1995 push( @$MessageSet_array_ref, $Message );
1996 }
1997  
1998 $MessageSetSize -= 12
1999 # [q] Offset
2000 # [l] MessageSize
2001 + $MessageSize # Message
2002 ;
2003 }
2004  
2005 return;
2006 }
2007  
2008 # Generates a template to encode single message within MessageSet
2009 sub _encode_Message {
2010 my ($request, $Key, $Value, $Attributes, $Offset, $v1_format, $Timestamp) = @_;
2011  
2012 # v1 format - supported since 0.10.0, introduces Timestamps
2013 my $MagicByte = $v1_format ? 1 : 0;
2014 $Timestamp //= -1;
2015 $Attributes //= $COMPRESSION_NONE; # According to Apache Kafka documentation:
2016 # The lowest 2 bits contain the compression codec used for the message.
2017 # The other bits should be set to 0.
2018  
2019  
2020 my $key_length = length( $Key );
2021 my $value_length = length( $Value );
2022  
2023 my $message_body = pack(
2024 q{cc} # MagicByte
2025 # Attributes
2026 .( $v1_format ? q{a[8]} : q{} ) # Timestamp - v1 only (when MagicByte > 0) (#$_int64_template)
2027 .q{l>} # Key length
2028 .( $key_length ? qq{a[$key_length]} : q{} ) # Key
2029 .q{l>} # Value length
2030 .( $value_length ? qq{a[$value_length]} : q{} ) # Value
2031 ,
2032 $MagicByte,
2033 $Attributes,
2034 $v1_format ? ( _pack64( $Timestamp ) ) : (),
2035 $key_length ? ( $key_length, $Key ) : ( $NULL_BYTES_LENGTH ),
2036 $value_length ? ( $value_length, $Value ) : ( $NULL_BYTES_LENGTH ),
2037 );
2038  
2039 my $MessageBodySize = length($message_body);
2040 my $MessageSize = $MessageBodySize + 4;
2041  
2042 my $data = $request->{data};
2043 $request->{template} .= $_MessageSet_template . qq{l>a[$MessageBodySize]}; # crc + message_body
2044 $request->{len} += $_MessageSet_length + $MessageSize;
2045  
2046 push @$data, (_pack64( $Offset ), $MessageSize, crc32( $message_body ), $message_body );
2047 return;
2048 }
2049  
2050 # Generates a template to encode MessageSet
2051 sub _encode_MessageSet_array {
2052 my ( $request, $MessageSet_array_ref, $compression_codec ) = @_;
2053  
2054 # not sure if it would be good to mix different formats in different messages in the same set
2055 # so detect if we should use v1 format
2056 my $use_v1_format;
2057 foreach my $MessageSet ( @$MessageSet_array_ref ) {
2058 if ( $MessageSet->{Timestamp} ) {
2059 $use_v1_format = 1;
2060 last;
2061 }
2062 }
2063  
2064 if ( $compression_codec ) {
2065 my $subrequest = {
2066 data => [],
2067 template => '',
2068 len => 0
2069 };
2070 my ( $Key, $Value );
2071  
2072 foreach my $MessageSet ( @$MessageSet_array_ref ) {
2073 _encode_Message( $subrequest,
2074 $Key = $MessageSet->{Key},
2075 $MessageSet->{Value},
2076 $COMPRESSION_NONE,
2077 $PRODUCER_ANY_OFFSET,
2078 $use_v1_format,
2079 $MessageSet->{Timestamp}
2080 );
2081 }
2082  
2083 $Value = pack($subrequest->{template}, @{$subrequest->{data}});
2084  
2085 $MessageSet_array_ref = [
2086 {
2087 Offset => $PRODUCER_ANY_OFFSET,
2088 Key => $Key,
2089 Value => _compress_data($Value, $compression_codec),
2090 }
2091 ];
2092  
2093 }
2094  
2095 my $subrequest = {
2096 data => [],
2097 template => '',
2098 len => 0
2099 };
2100  
2101 foreach my $MessageSet ( @$MessageSet_array_ref ) {
2102 _encode_Message( $subrequest,
2103 $MessageSet->{Key},
2104 $MessageSet->{Value},
2105 $compression_codec // $COMPRESSION_NONE,
2106 $MessageSet->{Offset},
2107 $use_v1_format,
2108 $MessageSet->{Timestamp}
2109 );
2110 }
2111  
2112 my $data = $request->{data};
2113 $request->{template} .= q{l>} . $subrequest->{template};
2114 $request->{len} += 4 + $subrequest->{len};
2115 push @$data, ( $subrequest->{len}, @{ $subrequest->{data} } );
2116  
2117 return;
2118 }
2119  
2120 # Generates a template to decrypt MessageSet
2121 sub _decode_MessageSet_template {
2122 my ( $response ) = @_;
2123  
2124 my $MessageSetSize = unpack(
2125 q{x[}.$response->{stream_offset}
2126 .q{]l>}, # MessageSetSize
2127 ${ $response->{bin_stream} }
2128 );
2129 $response->{template} .= q{l>}; # MessageSetSize
2130 $response->{stream_offset} += 4; # bytes before Offset
2131  
2132 return _decode_MessageSet_sized_template($MessageSetSize, $response);
2133 }
2134  
2135 sub _decode_MessageSet_sized_template {
2136 my ( $MessageSetSize, $response ) = @_;
2137  
2138 my $bin_stream_length = length ${ $response->{bin_stream} };
2139  
2140 my ( $local_template, $MessageSize, $MagicByte, $Key_length, $Value_length );
2141 CREATE_TEMPLATE:
2142 while ( $MessageSetSize ) {
2143 # Not the full MessageSet
2144 # 22 is the minimal size of a v0 message format until (and including) the Key
2145 # Length. If the message version is v1, then it's 22 + 8. We'll check later
2146 last CREATE_TEMPLATE if $MessageSetSize < 22;
2147  
2148 # [q] Offset
2149 # [l] MessageSize
2150 # [l] Crc
2151 # [c] MagicByte
2152 # [c] Attributes
2153 # ( [q] Timestamp ) message format v1 only
2154 # [l] Key length
2155 # [l] Value length
2156  
2157 $local_template = q{};
2158 MESSAGE_SET:
2159 {
2160 $response->{stream_offset} += 8; # the size of the offset data value
2161 ($MessageSize, $MagicByte) = unpack(
2162 q{x[}.$response->{stream_offset}.q{]}
2163 .q{l>} # MessageSize
2164 .q{x[l]} # Crc
2165 .q{c}, # MagicByte
2166  
2167 ${ $response->{bin_stream} }
2168 );
2169  
2170 my $is_v1_msg_format = $MagicByte == 1;
2171 if ($is_v1_msg_format && $MessageSetSize < (22+8)) {
2172 # Not the full MessageSet
2173 $local_template = q{};
2174 last MESSAGE_SET;
2175 }
2176 $local_template .= ( $is_v1_msg_format ? $_Message_template_with_timestamp : $_Message_template );
2177  
2178 $response->{stream_offset} += ($is_v1_msg_format ? 18: 10);
2179  
2180 $Key_length = unpack(
2181 q{x[}.$response->{stream_offset}
2182 .q{]l>}, # Key length
2183 ${ $response->{bin_stream} }
2184 );
2185  
2186 $response->{stream_offset} += 4; # bytes before Key or Value length
2187 # [l] Key length
2188 $response->{stream_offset} += $Key_length # bytes before Key
2189 if $Key_length != $NULL_BYTES_LENGTH; # Key
2190 if ( $bin_stream_length >= $response->{stream_offset} + 4 ) { # + [l] Value length
2191 $local_template .= $_Key_or_Value_template
2192 if $Key_length != $NULL_BYTES_LENGTH;
2193 } else {
2194 # Not the full MessageSet
2195 $local_template = q{};
2196 last MESSAGE_SET;
2197 }
2198  
2199 $local_template .= q{l>}; # Value length
2200 $Value_length = unpack(
2201 q{x[}.$response->{stream_offset}
2202 .q{]l>}, # Value length
2203 ${ $response->{bin_stream} }
2204 );
2205 $response->{stream_offset} += # bytes before Value or next Message
2206 4 # [l] Value length
2207 ;
2208 $response->{stream_offset} += $Value_length # bytes before next Message
2209 if $Value_length != $NULL_BYTES_LENGTH; # Value
2210 if ( $bin_stream_length >= $response->{stream_offset} ) {
2211 $local_template .= $_Key_or_Value_template
2212 if $Value_length != $NULL_BYTES_LENGTH;
2213 } else {
2214 # Not the full MessageSet
2215 $local_template = q{};
2216 last MESSAGE_SET;
2217 }
2218 }
2219  
2220 if ( $local_template ) {
2221 $response->{template} .= $local_template;
2222 $MessageSetSize -= 12
2223 # [q] Offset
2224 # [l] MessageSize
2225 + $MessageSize # Message
2226 ;
2227 } else {
2228 last CREATE_TEMPLATE;
2229 }
2230 }
2231  
2232 return;
2233 }
2234  
2235 # Generates a template to encrypt string
2236 sub _encode_string {
2237 my ( $request, $string ) = @_;
2238  
2239 if ( $string eq q{} ) {
2240 push( @{ $request->{data} }, 0 );
2241 } else {
2242 my $string_length = length $string;
2243 push( @{ $request->{data} }, $string_length, $string );
2244 $request->{template} .= q{a*}; # string;
2245 $request->{len} += $string_length;
2246 }
2247  
2248 return;
2249 }
2250  
2251 # Handler for errors
2252 sub _error {
2253 Kafka::Exception::Protocol->throw( throw_args( @_ ) );
2254  
2255 return;
2256 }
2257  
2258 1;
2259  
2260 __END__