blib/lib/Kafka/Connection.pm untested

filter: Statementstmt 0.0% 0 / 494 Branchbran 0.0% 0 / 210 Conditioncond 0.0% 0 / 211 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 1000 SCAR 93.4
CC
106
Cov
0%
receive_response_to_request
1122.0
new
156.0
_update_metadata
156.0
CRAP 11342.0
1 package Kafka::Connection;
2  
3 =head1 NAME
4  
5 Kafka::Connection - Object interface to connect to a kafka cluster.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::Connection> version 1.07 .
10  
11 =cut
12  
13 use 5.010;
14 use strict;
15 use warnings;
16  
17 our $DEBUG = 0;
18  
19 our $VERSION = '1.07';
20  
21 use Exporter qw(
22 import
23 );
24 our @EXPORT = qw(
25 %RETRY_ON_ERRORS
26 );
27  
28 use Data::Validate::Domain qw(
29 is_hostname
30 );
31 use Data::Validate::IP qw(
32 is_ipv4
33 is_ipv6
34 );
35 use Const::Fast;
36 use List::Util qw(
37 shuffle
38 );
39 use Params::Util qw(
40 _ARRAY
41 _ARRAY0
42 _HASH
43 _NONNEGINT
44 _NUMBER
45 _POSINT
46 _STRING
47 );
48 use Scalar::Util qw(
49 blessed
50 );
51 use Scalar::Util::Numeric qw(
52 isint
53 );
54 use Storable qw(
55 dclone
56 );
57 use Time::HiRes ();
58 use Try::Tiny;
59  
60 use Kafka qw(
61 %ERROR
62  
63 $ERROR_NO_ERROR
64 $ERROR_UNKNOWN
65 $ERROR_OFFSET_OUT_OF_RANGE
66 $ERROR_INVALID_MESSAGE
67 $ERROR_UNKNOWN_TOPIC_OR_PARTITION
68 $ERROR_INVALID_FETCH_SIZE
69 $ERROR_LEADER_NOT_AVAILABLE
70 $ERROR_NOT_LEADER_FOR_PARTITION
71 $ERROR_REQUEST_TIMED_OUT
72 $ERROR_BROKER_NOT_AVAILABLE
73 $ERROR_REPLICA_NOT_AVAILABLE
74 $ERROR_MESSAGE_TOO_LARGE
75 $ERROR_STALE_CONTROLLER_EPOCH
76 $ERROR_NETWORK_EXCEPTION
77 $ERROR_GROUP_LOAD_IN_PROGRESS
78 $ERROR_OFFSET_METADATA_TOO_LARGE
79 $ERROR_GROUP_COORDINATOR_NOT_AVAILABLE
80 $ERROR_NOT_COORDINATOR_FOR_GROUP
81 $ERROR_NOT_ENOUGH_REPLICAS
82 $ERROR_NOT_ENOUGH_REPLICAS_AFTER_APPEND
83 $ERROR_REBALANCE_IN_PROGRESS
84 $ERROR_UNSUPPORTED_VERSION
85  
86 $ERROR_CANNOT_BIND
87 $ERROR_CANNOT_GET_METADATA
88 $ERROR_CANNOT_RECV
89 $ERROR_CANNOT_SEND
90 $ERROR_LEADER_NOT_FOUND
91 $ERROR_GROUP_COORDINATOR_NOT_FOUND
92 $ERROR_MISMATCH_ARGUMENT
93 $ERROR_MISMATCH_CORRELATIONID
94 $ERROR_NO_KNOWN_BROKERS
95 $ERROR_RESPONSEMESSAGE_NOT_RECEIVED
96 $ERROR_SEND_NO_ACK
97 $ERROR_UNKNOWN_APIKEY
98 $ERROR_INCOMPATIBLE_HOST_IP_VERSION
99 $ERROR_NO_CONNECTION
100  
101 $IP_V4
102 $IP_V6
103 $KAFKA_SERVER_PORT
104 $NOT_SEND_ANY_RESPONSE
105 $REQUEST_TIMEOUT
106 $RETRY_BACKOFF
107 $SEND_MAX_ATTEMPTS
108 );
109  
110 use Kafka::Exceptions;
111 use Kafka::Internals qw(
112 $APIKEY_FETCH
113 $APIKEY_METADATA
114 $APIKEY_OFFSET
115 $APIKEY_PRODUCE
116 $APIKEY_FINDCOORDINATOR
117 $APIKEY_APIVERSIONS
118 $APIKEY_OFFSETCOMMIT
119 $APIKEY_OFFSETFETCH
120 $MAX_CORRELATIONID
121 $MAX_INT32
122 debug_level
123 _get_CorrelationId
124 format_message
125 );
126 use Kafka::IO;
127 use Kafka::Protocol qw(
128 $BAD_OFFSET
129 $IMPLEMENTED_APIVERSIONS
130 decode_fetch_response
131 decode_metadata_response
132 decode_offset_response
133 decode_produce_response
134 decode_api_versions_response
135 decode_find_coordinator_response
136 decode_offsetcommit_response
137 decode_offsetfetch_response
138 encode_fetch_request
139 encode_metadata_request
140 encode_offset_request
141 encode_produce_request
142 encode_api_versions_request
143 encode_find_coordinator_request
144 encode_offsetcommit_request
145 encode_offsetfetch_request
146 );
147  
148 =head1 SYNOPSIS
149  
150 use 5.010;
151 use strict;
152 use warnings;
153  
154 use Scalar::Util qw(
155 blessed
156 );
157 use Try::Tiny;
158  
159 # A simple example of Kafka::Connection usage:
160 use Kafka::Connection;
161  
162 # connect to local cluster with the defaults
163 my $connection;
164 try {
165 $connection = Kafka::Connection->new( host => 'localhost' );
166 } catch {
167 my $error = $_;
168 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
169 warn $error->message, "\n", $error->trace->as_string, "\n";
170 exit;
171 } else {
172 die $error;
173 }
174 };
175  
176 # Closes the connection and cleans up
177 $connection->close;
178 undef $connection;
179  
180 =head1 DESCRIPTION
181  
182 The main features of the C<Kafka::Connection> class are:
183  
184 =over 3
185  
186 =item *
187  
188 Provides API for communication with Kafka 0.9+ cluster.
189  
190 =item *
191  
192 Performs requests encoding and responses decoding, provides automatic
193 selection or promotion of a leader server from Kafka cluster.
194  
195 =item *
196  
197 Provides information about Kafka cluster.
198  
199 =back
200  
201 =cut
202  
203 my %protocol = (
204 "$APIKEY_PRODUCE" => {
205 decode => \&decode_produce_response,
206 encode => \&encode_produce_request,
207 },
208 "$APIKEY_FETCH" => {
209 decode => \&decode_fetch_response,
210 encode => \&encode_fetch_request,
211 },
212 "$APIKEY_OFFSET" => {
213 decode => \&decode_offset_response,
214 encode => \&encode_offset_request,
215 },
216 "$APIKEY_METADATA" => {
217 decode => \&decode_metadata_response,
218 encode => \&encode_metadata_request,
219 },
220 "$APIKEY_APIVERSIONS" => {
221 decode => \&decode_api_versions_response,
222 encode => \&encode_api_versions_request,
223 },
224 "$APIKEY_FINDCOORDINATOR" => {
225 decode => \&decode_find_coordinator_response,
226 encode => \&encode_find_coordinator_request,
227 },
228 "$APIKEY_OFFSETCOMMIT" => {
229 decode => \&decode_offsetcommit_response,
230 encode => \&encode_offsetcommit_request,
231 },
232 "$APIKEY_OFFSETFETCH" => {
233 decode => \&decode_offsetfetch_response,
234 encode => \&encode_offsetfetch_request,
235 },
236 );
237  
238 =head2 EXPORT
239  
240 The following constants are available for export
241  
242 =cut
243  
244 =head3 C<%RETRY_ON_ERRORS>
245  
246 These are non-fatal errors, which when happen causes refreshing of meta-data from Kafka followed by
247 another attempt to fetch data.
248  
249 =cut
250 # When any of the following error happens, a possible change in meta-data on server is expected.
251 const our %RETRY_ON_ERRORS => (
252 # $ERROR_NO_ERROR => 1, # 0 - No error
253 $ERROR_UNKNOWN => 1, # -1 - An unexpected server error
254 # $ERROR_OFFSET_OUT_OF_RANGE => 1, # 1 - The requested offset is not within the range of offsets maintained by the server
255 $ERROR_INVALID_MESSAGE => 1, # 2 - Retriable - This message has failed its CRC checksum, exceeds the valid size, or is otherwise corrupt
256 $ERROR_UNKNOWN_TOPIC_OR_PARTITION => 1, # 3 - Retriable - This server does not host this topic-partition
257 # $ERROR_INVALID_FETCH_SIZE => 1, # 4 - The requested fetch size is invalid
258 $ERROR_LEADER_NOT_AVAILABLE => 1, # 5 - Retriable - Unable to write due to ongoing Kafka leader selection
259 $ERROR_NOT_LEADER_FOR_PARTITION => 1, # 6 - Retriable - Server is not a leader for partition
260 $ERROR_REQUEST_TIMED_OUT => 1, # 7 - Retriable - Request time-out
261 $ERROR_BROKER_NOT_AVAILABLE => 1, # 8 - Broker is not available
262 $ERROR_REPLICA_NOT_AVAILABLE => 1, # 9 - Replica not available
263 # $ERROR_MESSAGE_TOO_LARGE => 1, # 10 - The request included a message larger than the max message size the server will accept
264 $ERROR_STALE_CONTROLLER_EPOCH => 1, # 11 - The controller moved to another broker
265 # $ERROR_OFFSET_METADATA_TOO_LARGE => 1, # 12 - The metadata field of the offset request was too large
266 $ERROR_NETWORK_EXCEPTION => 1, # 13 Retriable - The server disconnected before a response was received
267 $ERROR_GROUP_LOAD_IN_PROGRESS => 1, # 14 - Retriable - The coordinator is loading and hence can't process requests for this group
268 $ERROR_GROUP_COORDINATOR_NOT_AVAILABLE => 1, # 15 - Retriable - The group coordinator is not available
269 $ERROR_NOT_COORDINATOR_FOR_GROUP => 1, # 16 - Retriable - This is not the correct coordinator for this group
270  
271 # $ERROR_INVALID_TOPIC_EXCEPTION => 1, # 17 - The request attempted to perform an operation on an invalid topic
272 # $ERROR_RECORD_LIST_TOO_LARGE => 1, # 18 - The request included message batch larger than the configured segment size on the server
273 $ERROR_NOT_ENOUGH_REPLICAS => 1, # 19 - Retriable - Messages are rejected since there are fewer in-sync replicas than required
274 $ERROR_NOT_ENOUGH_REPLICAS_AFTER_APPEND => 1, # 20 - Retriable - Messages are written to the log, but to fewer in-sync replicas than required
275 # $ERROR_INVALID_REQUIRED_ACKS => 1, # 21 - Produce request specified an invalid value for required acks
276 # $ERROR_ILLEGAL_GENERATION => 1, # 22 - Specified group generation id is not valid
277 # $ERROR_INCONSISTENT_GROUP_PROTOCOL => 1, # 23 - The group member's supported protocols are incompatible with those of existing members
278 # $ERROR_INVALID_GROUP_ID => 1, # 24 - The configured groupId is invalid
279 # $ERROR_UNKNOWN_MEMBER_ID => 1, # 25 - The coordinator is not aware of this member
280 # $ERROR_INVALID_SESSION_TIMEOUT => 1, # 26 - The session timeout is not within the range allowed by the broker (as configured by group.min.session.timeout.ms and group.max.session.timeout.ms)
281 $ERROR_REBALANCE_IN_PROGRESS => 1, # 27 - The group is rebalancing, so a rejoin is needed
282 # $ERROR_INVALID_COMMIT_OFFSET_SIZE => 1, # 28 - The committing offset data size is not valid
283 # $ERROR_TOPIC_AUTHORIZATION_FAILED => 1, # 29 - Not authorized to access topics: Topic authorization failed
284 # $ERROR_GROUP_AUTHORIZATION_FAILED => 1, # 30 - Not authorized to access group: Group authorization failed
285 # $ERROR_CLUSTER_AUTHORIZATION_FAILED => 1, # 31 - Cluster authorization failed
286 # $ERROR_INVALID_TIMESTAMP => 1, # 32 - The timestamp of the message is out of acceptable range
287 # $ERROR_UNSUPPORTED_SASL_MECHANISM => 1, # 33 - The broker does not support the requested SASL mechanism
288 # $ERROR_ILLEGAL_SASL_STATE => 1, # 34 - Request is not valid given the current SASL state
289 # $ERROR_UNSUPPORTED_VERSION => 1, # 35 - The version of API is not supported
290 $ERROR_NO_CONNECTION => 1, # may be disconnected due to idle timeout etc.
291 );
292  
293 #-- constructor ----------------------------------------------------------------
294  
295 =head2 CONSTRUCTOR
296  
297 =head3 C<new>
298  
299 Creates C<Kafka::Connection> object for interaction with Kafka cluster.
300 Returns created C<Kafka::Connection> object.
301  
302 C<new()> takes arguments in key-value pairs. The following arguments are currently recognized:
303  
304 =over 3
305  
306 =item C<host =E<gt> $host>
307  
308 C<$host> is any Apache Kafka cluster host to connect to. It can be a hostname or the
309 IP-address in the "xx.xx.xx.xx" form.
310  
311 Optional. Either C<host> or C<broker_list> must be supplied.
312  
313 WARNING:
314  
315 Make sure that you always connect to brokers using EXACTLY the same address or host name
316 as specified in broker configuration (host.name in server.properties).
317 Avoid using default value (when host.name is commented out) in server.properties - always use explicit value instead.
318  
319 =item C<port =E<gt> $port>
320  
321 Optional, default = C<$KAFKA_SERVER_PORT>.
322  
323 C<$port> is the attribute denoting the port number of the service we want to
324 access (Apache Kafka service). C<$port> should be an integer number.
325  
326 C<$KAFKA_SERVER_PORT> is the default Apache Kafka server port constant (C<9092>) that can
327 be imported from the L<Kafka|Kafka> module.
328  
329 =item C<broker_list =E<gt> $broker_list>
330  
331 Optional, C<$broker_list> is a reference to array of the host:port or [IPv6_host]:port strings, defining the list
332 of Kafka servers. This list will be used to locate the new leader if the server specified
333 via C<host =E<gt> $host> and C<port =E<gt> $port> arguments becomes unavailable. Either C<host>
334 or C<broker_list> must be supplied.
335  
336 =item C<ip_version =E<gt> $ip_version>
337  
338 Specify version of IP for interpreting of passed IP address and resolving of host name.
339  
340 Optional, undefined by default, which works in the following way: version of IP address
341 is detected automatically, host name is resolved into IPv4 address.
342  
343 See description of L<$IP_V4|Kafka::IO/$IP_V4>, L<$IP_V6|Kafka::IO/$IP_V6>
344 in C<Kafka> L<EXPORT|Kafka/EXPORT>.
345  
346 =item C<timeout =E<gt> $timeout>
347  
348 Optional, default = C<$Kafka::REQUEST_TIMEOUT>.
349  
350 C<$timeout> specifies how long we wait for the remote server to respond.
351 C<$timeout> is in seconds, could be a positive integer or a floating-point number not bigger than int32 positive integer.
352  
353 Special behavior when C<timeout> is set to C<undef>:
354  
355 =back
356  
357 =over 3
358  
359 =item *
360  
361 Alarms are not used internally (namely when performing C<gethostbyname>).
362  
363 =item *
364  
365 Default C<$REQUEST_TIMEOUT> is used for the rest of IO operations.
366  
367 =back
368  
369 =over 3
370  
371 =item C<SEND_MAX_ATTEMPTS =E<gt> $attempts>
372  
373 Optional, int32 signed integer, default = C<$Kafka::SEND_MAX_ATTEMPTS> .
374  
375 In some circumstances (leader is temporarily unavailable, outdated metadata, etc) we may fail to send a message.
376 This property specifies the maximum number of attempts to send a message.
377 The C<$attempts> should be an integer number.
378  
379 =item C<RETRY_BACKOFF =E<gt> $backoff>
380  
381 Optional, default = C<$Kafka::RETRY_BACKOFF> .
382  
383 Since leader election takes a bit of time, this property specifies the amount of time,
384 in milliseconds, that the producer waits before refreshing the metadata.
385 The C<$backoff> should be an integer number.
386  
387 =item C<AutoCreateTopicsEnable =E<gt> $mode>
388  
389 Optional, default value is 0 (false).
390  
391 Kafka BUG "[KAFKA-1124]" (Fixed in Kafka 0.8.2):
392 I<AutoCreateTopicsEnable> controls how this module handles the first access to non-existent topic
393 when C<auto.create.topics.enable> in server configuration is C<true>.
394 If I<AutoCreateTopicsEnable> is false (default),
395 the first access to non-existent topic produces an exception;
396 however, the topic is created and next attempts to access it will succeed.
397  
398 If I<AutoCreateTopicsEnable> is true, this module waits
399 (according to the C<SEND_MAX_ATTEMPTS> and C<RETRY_BACKOFF> properties)
400 until the topic is created,
401 to avoid errors on the first access to non-existent topic.
402  
403 If C<auto.create.topics.enable> in server configuration is C<false>, this setting has no effect.
404  
405 =item C<MaxLoggedErrors =E<gt> $number>
406  
407 Optional, default value is 100.
408  
409 Defines maximum number of last non-fatal errors that we keep in log. Use method L</nonfatal_errors> to
410 access those errors.
411  
412 =item C<dont_load_supported_api_versions =E<gt> $boolean>
413  
414 Optional, default value is 0 (false).
415  
416 If set to false, when communicating with a broker, the client will
417 automatically try to find out the best version numbers to use for each of the
418 API endpoints.
419  
420 If set to true, the client will always use
421 C<$Kafka::Protocol::DEFAULT_APIVERSION> as API version.
422  
423 WARNING: API versions are supported starting from Kafka 0.10. Set this parameter to true
424 if you're connecting to 0.9.
425  
426 =back
427  
428 =cut
429 sub new {
430 my ( $class, %p ) = @_;
431  
432 my $self = bless {
433 host => q{},
434 port => $KAFKA_SERVER_PORT,
435 broker_list => [],
436 timeout => $REQUEST_TIMEOUT,
437 ip_version => undef,
438 SEND_MAX_ATTEMPTS => $SEND_MAX_ATTEMPTS,
439 RETRY_BACKOFF => $RETRY_BACKOFF,
440 AutoCreateTopicsEnable => 0,
441 MaxLoggedErrors => 100,
442 dont_load_supported_api_versions => 0,
443 }, $class;
444  
445 exists $p{$_} and $self->{$_} = $p{$_} foreach keys %$self;
446  
447 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'host' )
448 unless defined( $self->{host} ) && ( $self->{host} eq q{} || defined( _STRING( $self->{host} ) ) ) && !utf8::is_utf8( $self->{host} );
449 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'port' )
450 unless _POSINT( $self->{port} );
451 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'timeout (%s)', $self->{timeout} ) )
452 unless !defined( $self->{timeout} ) || ( defined _NUMBER( $self->{timeout} ) && int( 1000 * $self->{timeout} ) >= 1 && int( $self->{timeout} * 1000 ) <= $MAX_INT32 );
453 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'broker_list' )
454 unless _ARRAY0( $self->{broker_list} );
455 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'SEND_MAX_ATTEMPTS' )
456 unless _POSINT( $self->{SEND_MAX_ATTEMPTS} );
457 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'RETRY_BACKOFF' )
458 unless _POSINT( $self->{RETRY_BACKOFF} );
459 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'MaxLoggedErrors' )
460 unless defined( _NONNEGINT( $self->{MaxLoggedErrors} ) );
461  
462 my $ip_version = $self->{ip_version};
463 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'ip_version (%s)', $ip_version ) )
464 unless ( !defined( $ip_version ) || ( defined( _NONNEGINT( $ip_version ) ) && ( $ip_version == $IP_V4 || $ip_version == $IP_V6 ) ) );
465  
466 $self->{_metadata} = {}; # {
467 # TopicName => {
468 # Partition => {
469 # 'Leader' => ...,
470 # 'Replicas' => [
471 # ...,
472 # ],
473 # 'Isr' => [
474 # ...,
475 # ],
476 # },
477 # ...,
478 # },
479 # ...,
480 # }
481 $self->{_leaders} = {}; # {
482 # NodeId => host:port or [IPv6_host]:port,
483 # ...,
484 # }
485 $self->{_group_coordinators} = {}; # {
486 # GroupId => host:port or [IPv6_host]:port,
487 # ...,
488 # }
489 $self->{_nonfatal_errors} = [];
490 my $IO_cache = $self->{_IO_cache} = {}; # host:port or [IPv6_host]:port => {
491 # 'NodeId' => ...,
492 # 'IO' => ...,
493 # 'timeout' => ...,
494 # 'host' => ...,
495 # 'port' => ...,
496 # 'error' => ...,
497 # },
498 # ...,
499  
500 # init IO cache
501 foreach my $server ( ( $self->{host} ? $self->_build_server_name( $self->{host}, $self->{port} ) : (), @{ $self->{broker_list} } ) ) {
502 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'bad host:port or broker_list element' )
503 unless $self->_is_like_server( $server );
504 my ( $host, $port ) = _split_host_port( $server );
505 my $correct_server = $self->_build_server_name( $host, $port );
506 $IO_cache->{ $correct_server } = {
507 NodeId => undef,
508 IO => undef,
509 host => $host,
510 port => $port,
511 };
512 }
513  
514 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'server is not specified' )
515 unless keys( %$IO_cache );
516  
517 return $self;
518 }
519  
520 #-- public attributes ----------------------------------------------------------
521  
522 =head2 METHODS
523  
524 The following methods are defined for the C<Kafka::Producer> class:
525  
526 =cut
527  
528 #-- public methods -------------------------------------------------------------
529  
530 =head3 C<get_known_servers>
531  
532 Returns the list of known Kafka servers (in host:port or [IPv6_host]:port format).
533  
534 =cut
535 sub get_known_servers {
536 my ( $self ) = @_;
537  
538 return keys %{ $self->{_IO_cache} };
539 }
540  
541 sub _get_api_versions {
542 my ( $self, $server ) = @_;
543  
544 my $server_metadata = $self->{_IO_cache}->{$server};
545 defined $server_metadata
546 or die "Fatal error: server '$server' is unknown in IO cache, which should not happen";
547  
548 # if we have cached data, just use it
549 defined $server_metadata->{_api_versions}
550 and return $server_metadata->{_api_versions};
551  
552 # no cached data. Initialize empty one
553 my $server_api_versions = $server_metadata->{_api_versions} = {};
554  
555 # use empty data if client doesn't want to detect API versions
556 $self->{dont_load_supported_api_versions}
557 and return $server_api_versions;
558  
559 # call the server and try to get the supported API versions
560 my $api_versions = [];
561 my $error;
562 try {
563 # The ApiVersions API endpoint is only supported on Kafka versions >
564 # 0.10.0.0 so this call may fail. We simply ignore this failure and
565 # carry on.
566 $api_versions = $self->_get_supported_api_versions( $server );
567 }
568 catch {
569 $error = $_;
570 };
571  
572 if( defined $error ) {
573 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
574 if( $error->code == $ERROR_MISMATCH_ARGUMENT ) {
575 # rethrow known fatal errors
576 die $error;
577 }
578 $self->_remember_nonfatal_error( $error->code, $error, $server );
579 } else {
580 die $error;
581 }
582 }
583  
584 foreach my $element (@$api_versions) {
585 # we want to choose which api version to use for each API call. We
586 # try to use the max version that the server supports, with
587 # fallback to the max version the protocol implements. If it's
588 # lower than the min version the kafka server supports, we set it
589 # to -1. If thie API endpoint is called, it'll die.
590 my $kafka_min_version = $element->{MinVersion};
591 my $kafka_max_version = $element->{MaxVersion};
592 my $api_key = $element->{ApiKey};
593 my $implemented_max_version = $IMPLEMENTED_APIVERSIONS->{$api_key} // -1;
594 my $version = $kafka_max_version;
595 $version > $implemented_max_version
596 and $version = $implemented_max_version;
597 $version < $kafka_min_version
598 and $version = -1;
599 $server_api_versions->{$api_key} = $version;
600 }
601  
602 return $server_api_versions;
603 }
604  
605 # Returns the list of supported API versions. This is not really. *Warning*,
606 # this call works only against Kafka 1.10.0.0
607  
608 sub _get_supported_api_versions {
609 my ( $self, $broker ) = @_;
610  
611 my $CorrelationId = _get_CorrelationId();
612 my $decoded_request = {
613 CorrelationId => $CorrelationId,
614 ClientId => q{},
615 ApiVersion => 0,
616 };
617 say STDERR format_message( '[%s] apiversions request: %s',
618 scalar( localtime ),
619 $decoded_request,
620 ) if $self->debug_level;
621 my $encoded_request = $protocol{ $APIKEY_APIVERSIONS }->{encode}->( $decoded_request );
622  
623 my $encoded_response_ref;
624  
625 # receive apiversions. We use a code block because it's actually a loop where
626 # you can do last.
627 {
628 $self->_connectIO( $broker )
629 or last;
630 my $sent = $self->_sendIO( $broker, $encoded_request )
631 or last;
632 $encoded_response_ref = $self->_receiveIO( $broker );
633 }
634  
635 unless ( $encoded_response_ref ) {
636 # NOTE: it is possible to repeat the operation here
637 $self->_error( $ERROR_CANNOT_RECV );
638 }
639  
640 my $decoded_response = $protocol{ $APIKEY_APIVERSIONS }->{decode}->( $encoded_response_ref );
641 say STDERR format_message( '[%s] apiversions response: %s',
642 scalar( localtime ),
643 $decoded_response,
644 ) if $self->debug_level;
645 ( defined( $decoded_response->{CorrelationId} ) && $decoded_response->{CorrelationId} == $CorrelationId )
646 # FATAL error
647 or $self->_error( $ERROR_MISMATCH_CORRELATIONID );
648 my $ErrorCode = $decoded_response->{ErrorCode};
649  
650 # we asked a Kafka < 0.10 ( in this case the call is not
651 # implemented and it dies
652 $ErrorCode == $ERROR_NO_ERROR
653 or $self->_error($ErrorCode);
654  
655 my $api_versions = $decoded_response->{ApiVersions};
656 return $api_versions;
657 }
658  
659 =head3 C<get_metadata( $topic )>
660  
661 If C<$topic> is present, it must be a non-false string of non-zero length.
662  
663 If C<$topic> is absent, this method returns metadata for all topics.
664  
665 Updates kafka cluster's metadata description and returns the hash reference to metadata,
666 which can be schematically described as:
667  
668 {
669 TopicName => {
670 Partition => {
671 'Leader' => ...,
672 'Replicas' => [
673 ...,
674 ],
675 'Isr' => [
676 ...,
677 ],
678 },
679 ...,
680 },
681 ...,
682 }
683  
684 Consult Kafka "Wire protocol" documentation for more details about metadata structure.
685  
686 =cut
687 sub get_metadata {
688 my ( $self, $topic ) = @_;
689  
690 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
691 unless !defined( $topic ) || ( ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic ) );
692  
693 $self->_update_metadata( $topic )
694 # FATAL error
695 or $self->_error( $ERROR_CANNOT_GET_METADATA, format_message( "topic='%s'", $topic ) );
696  
697 my $clone;
698 if ( defined $topic ) {
699 $clone = {
700 $topic => dclone( $self->{_metadata}->{ $topic } )
701 };
702 } else {
703 $clone = dclone( $self->{_metadata} );
704 }
705  
706 return $clone;
707 }
708  
709 =head3 C<is_server_known( $server )>
710  
711 Returns true, if C<$server> (host:port or [IPv6_host]:port) is known in cluster.
712  
713 =cut
714 sub is_server_known {
715 my ( $self, $server ) = @_;
716  
717 $self->_error( $ERROR_MISMATCH_ARGUMENT )
718 unless $self->_is_like_server( $server );
719  
720 return exists $self->{_IO_cache}->{ $server };
721 }
722  
723 # Returns true, if known C<$server> (host:port or [IPv6_host]:port) is accessible.
724 # Checks the accessibility of the server.
725 # This is evil: opens and closes a NEW connection immediately so do not use unless there is a strong reason for it.
726 sub _is_server_alive {
727 my ( $self, $server ) = @_;
728  
729 $self->_error( $ERROR_MISMATCH_ARGUMENT )
730 unless $self->_is_like_server( $server );
731  
732 $self->_error( $ERROR_NO_KNOWN_BROKERS, 'has not yet received the metadata?' )
733 unless $self->get_known_servers;
734  
735 my $io_cache = $self->{_IO_cache};
736 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( "Unknown server '%s' (is not found in the metadata)", $server ) )
737 unless exists( $io_cache->{ $server } );
738  
739 if ( my $io = $self->_connectIO( $server ) ) {
740 return $io->_is_alive;
741 } else {
742 return;
743 }
744 }
745  
746 # this is evil, do not use unless there is a very strong reason for it
747 sub _is_server_connected {
748 my ( $self, $server ) = @_;
749  
750 $self->_error( $ERROR_MISMATCH_ARGUMENT )
751 unless $self->_is_like_server( $server );
752  
753 my $io_cache = $self->{_IO_cache};
754 my $io;
755 unless ( exists( $io_cache->{ $server } ) && ( $io = $io_cache->{ $server }->{IO} ) ) {
756 return;
757 }
758  
759 return $io->_is_alive;
760 }
761  
762 =head3 C<receive_response_to_request( $request, $compression_codec )>
763  
764 =over 3
765  
766 =item C<$request>
767  
768 C<$request> is a reference to the hash representing
769 the structure of the request.
770  
771 This method encodes C<$request>, passes it to the leader of cluster, receives reply, decodes and returns
772 it in a form of hash reference.
773  
774 =back
775  
776 WARNING:
777  
778 =over 3
779  
780 =item *
781  
782 This method should be considered private and should not be called by an end user.
783  
784 =item *
785  
786 In order to achieve better performance, this method does not perform arguments validation.
787  
788 =back
789  
790 =over 3
791  
792 =item C<$compression_codec>
793  
794 Optional.
795  
796 C<$compression_codec> sets the required type of C<$messages> compression,
797 if the compression is desirable.
798  
799 Supported codecs:
800 L<$COMPRESSION_NONE|Kafka/$COMPRESSION_NONE>,
801 L<$COMPRESSION_GZIP|Kafka/$COMPRESSION_GZIP>,
802 L<$COMPRESSION_SNAPPY|Kafka/$COMPRESSION_SNAPPY>,
803 L<$COMPRESSION_LZ4|Kafka/$COMPRESSION_LZ4>.
804  
805 NOTE: $COMPRESSION_LZ4 requires Kafka 0.10 or higher, as initial implementation of LZ4 in Kafka did not follow the standard LZ4 framing specification.
806  
807  
808 =back
809  
810 =cut
811 sub receive_response_to_request {
812 my ( $self, $request, $compression_codec, $response_timeout ) = @_;
813  
814 my $api_key = $request->{ApiKey};
815  
816 my $host_to_send_to = $request->{__send_to__} // 'leader';
817  
818 # WARNING: The current version of the module limited to the following:
819 # supports queries with only one combination of topic + partition (first and only).
820  
821 my $topic_data = $request->{topics}->[0];
822 my $topic_name = $topic_data->{TopicName};
823 my $partition = $topic_data->{partitions}->[0]->{Partition};
824  
825 if (
826 !%{ $self->{_metadata} } # the first request
827 || ( !$self->{AutoCreateTopicsEnable} && defined( $topic_name ) && !exists( $self->{_metadata}->{ $topic_name } ) )
828 ) {
829 $self->_update_metadata( $topic_name ) # hash metadata could be updated
830 # FATAL error
831 or $self->_error( $ERROR_CANNOT_GET_METADATA, format_message( "topic='%s'", $topic_name ), request => $request )
832 ;
833 }
834  
835 $request->{CorrelationId} = _get_CorrelationId() unless exists $request->{CorrelationId};
836  
837 say STDERR format_message( '[%s] compression_codec=%s, request=%s',
838 scalar( localtime ),
839 $compression_codec,
840 $request,
841 ) if $self->debug_level;
842  
843 my( $ErrorCode, $partition_data, $io_error );
844  
845 my $attempt = 0;
846 # we save the original api version of the request, because in the attempt
847 # loop we might be trying different brokers which may support different api
848 # versions.
849 my $original_request_api_version = $request->{ApiVersion};
850 ATTEMPT: while ( ++$attempt <= ( $self->{SEND_MAX_ATTEMPTS} // 1 ) ) {
851 $ErrorCode = $ERROR_NO_ERROR;
852 undef $io_error;
853  
854 my $server;
855 if ($host_to_send_to eq 'leader') {
856 # hash metadata could be updated
857 my $leader = $self->{_metadata}->{ $topic_name }->{ $partition }->{Leader};
858 next ATTEMPT unless defined $leader;
859  
860 $server = $self->{_leaders}->{ $leader };
861 unless ( $server ) {
862 $ErrorCode = $ERROR_LEADER_NOT_FOUND;
863 $self->_remember_nonfatal_error( $ErrorCode, $ERROR{ $ErrorCode }, $server, $topic_name, $partition );
864 next ATTEMPT;
865 }
866 } elsif ( $host_to_send_to eq 'group_coordinator') {
867 my $group_id = $request->{GroupId};
868 if ( !%{ $self->{_group_coordinators} } && defined $group_id) {
869 # first request
870 $self->_update_group_coordinators($group_id);
871 }
872 $server = $self->{_group_coordinators}->{$group_id};
873 unless ( $server ) {
874 $ErrorCode = $ERROR_GROUP_COORDINATOR_NOT_FOUND;
875 $self->_remember_nonfatal_error( $ErrorCode, $ERROR{ $ErrorCode }, $server, $topic_name, $partition );
876 next ATTEMPT;
877 }
878 } else {
879 die "__send_to__ must be either 'leader', 'group_coordinator', or void (will default to 'leader')";
880 }
881  
882 # Send a request to the server
883 if ( $self->_connectIO( $server ) ) {
884 # we can connect to this server, so let's detect the api versions
885 # it and use whatever it supports, except if the request forces us
886 # to use an api version. Warning, the version might end up being
887 # undef if detection against the Kafka server failed, or if
888 # dont_load_supported_api_versions is true. However the Encoder
889 # code knows how to handle it.
890 $request->{ApiVersion} = $original_request_api_version;
891 unless( defined $request->{ApiVersion} ) {
892 $request->{ApiVersion} = $self->_get_api_versions( $server )->{ $api_key };
893 # API versions request may fail and the server may be disconnected
894 unless( $self->_is_IO_connected( $server ) ) {
895 # this attempt does not count, assuming that _get_api_versions will not try to get them from failing broker again
896 redo ATTEMPT;
897 }
898 }
899  
900 my $encoded_request = $protocol{ $api_key }->{encode}->( $request, $compression_codec );
901  
902 unless ( $self->_sendIO( $server, $encoded_request ) ) {
903 $io_error = $self->_io_error( $server );
904 $ErrorCode = $io_error ? $io_error->code : $ERROR_CANNOT_SEND;
905 $self->_closeIO( $server, 1 );
906 }
907 }
908 else {
909 $io_error = $self->_io_error( $server );
910 $ErrorCode = $io_error ? $io_error->code : $ERROR_CANNOT_BIND;
911 }
912  
913 if ( $ErrorCode != $ERROR_NO_ERROR ) {
914 # could not send request due to non-fatal IO error (fatal errors should be thrown by connectIO/sendIO already)
915 $self->_remember_nonfatal_error( $ErrorCode, $self->_io_error( $server ), $server, $topic_name, $partition );
916 if( $api_key == $APIKEY_PRODUCE && !( $ErrorCode == $ERROR_CANNOT_BIND || $ErrorCode == $ERROR_NO_CONNECTION ) ) {
917 # do not retry failed produce requests which may have sent some data already
918 $ErrorCode = $ERROR_CANNOT_SEND;
919 last ATTEMPT;
920 }
921 next ATTEMPT;
922 }
923  
924 my $response;
925 if ( $api_key == $APIKEY_PRODUCE && $request->{RequiredAcks} == $NOT_SEND_ANY_RESPONSE ) {
926 # Do not receive a response, self-forming own response
927 $response = {
928 CorrelationId => $request->{CorrelationId},
929 topics => [
930 {
931 TopicName => $topic_name,
932 partitions => [
933 {
934 Partition => $partition,
935 ErrorCode => 0,
936 Offset => $BAD_OFFSET,
937 },
938 ],
939 },
940 ],
941 };
942 } else {
943 my $encoded_response_ref = $self->_receiveIO( $server, $response_timeout );
944 unless ( $encoded_response_ref ) {
945 if ( $api_key == $APIKEY_PRODUCE ) {
946 # WARNING: Unfortunately, the sent package (one or more messages) does not have a unique identifier
947 # and there is no way to verify the delivery of data
948 $ErrorCode = $ERROR_SEND_NO_ACK;
949  
950 # Should not be allowed to re-send data on the next attempt
951 # FATAL error
952 $self->_error( $ErrorCode, "no ack for request", io_error => $self->_io_error( $server ), request => $request );
953 last ATTEMPT;
954 } else {
955 $ErrorCode = $ERROR_CANNOT_RECV;
956 $self->_remember_nonfatal_error( $ErrorCode, $self->_io_error( $server ), $server, $topic_name, $partition );
957 next ATTEMPT;
958 }
959 }
960 if ( length( $$encoded_response_ref ) > 4 ) { # MessageSize => int32
961 # we also pass the api version that was used for the request,
962 # so that we know how to decode the response
963 $response = $protocol{ $api_key }->{decode}->( $encoded_response_ref, $request->{ApiVersion} );
964 say STDERR format_message( '[%s] response: %s',
965 scalar( localtime ),
966 $response,
967 ) if $self->debug_level;
968 } else {
969 $self->_error( $ERROR_RESPONSEMESSAGE_NOT_RECEIVED, format_message("response length=%s", length( $$encoded_response_ref ) ), io_error => $self->_io_error( $server ), request => $request );
970 }
971 }
972  
973 # FATAL error if correllation does not match
974 $self->_error( $ERROR_MISMATCH_CORRELATIONID, "$response->{CorrelationId} != $request->{CorrelationId}", request => $request, response => $response )
975 unless $response->{CorrelationId} == $request->{CorrelationId}
976 ;
977 $topic_data = $response->{topics}->[0];
978 $partition_data = $topic_data->{ $api_key == $APIKEY_OFFSET ? 'PartitionOffsets' : 'partitions' }->[0];
979  
980 $ErrorCode = $partition_data->{ErrorCode};
981  
982 return $response if $ErrorCode == $ERROR_NO_ERROR; # success
983  
984 if( $api_key == $APIKEY_PRODUCE && $ErrorCode == $ERROR_REQUEST_TIMED_OUT ) {
985 # special case: produce request timed out so we did not get expected ACK and should not retry sending request again
986 # Should not be allowed to re-send data on the next attempt
987 # FATAL error
988 $self->_error( $ERROR_SEND_NO_ACK, format_message( "topic='%s', partition=%s response error: %s", $topic_name, $partition, $ErrorCode ), request => $request, response => $response );
989 last ATTEMPT;
990 }
991  
992 if ( exists $RETRY_ON_ERRORS{ $ErrorCode } ) {
993 $self->_remember_nonfatal_error( $ErrorCode, $ERROR{ $ErrorCode }, $server, $topic_name, $partition );
994 next ATTEMPT;
995 }
996  
997 # FATAL error
998 $self->_error( $ErrorCode, format_message( "topic='%s', partition=%s", $topic_name, $partition ), request => $request );
999 } continue {
1000 # Expect to possible changes in the situation, such as restoration of connection
1001 say STDERR format_message( '[%s] sleeping for %d ms before making request attempt #%d (%s)',
1002 scalar( localtime ),
1003 $self->{RETRY_BACKOFF},
1004 $attempt + 1,
1005 $ErrorCode == $ERROR_NO_ERROR ? 'refreshing metadata' : "ErrorCode ${ErrorCode}",
1006 ) if $self->debug_level;
1007  
1008 Time::HiRes::sleep( $self->{RETRY_BACKOFF} / 1000 );
1009  
1010 $self->_update_metadata( $topic_name )
1011 # FATAL error
1012 or $self->_error( $ErrorCode || $ERROR_CANNOT_GET_METADATA, format_message( "topic='%s', partition=%s", $topic_name, $partition ), request => $request )
1013 ;
1014 if ( $host_to_send_to eq 'group_coordinator') {
1015 $self->_update_group_coordinators($request->{GroupId})
1016 }
1017 }
1018  
1019 # FATAL error
1020 if ( $ErrorCode ) {
1021 $self->_error( $ErrorCode, format_message( "topic='%s'%s", $topic_data->{TopicName}, $partition_data ? ", partition = ".$partition_data->{Partition} : '' ), request => $request, io_error => $io_error );
1022 } else {
1023 $self->_error( $ERROR_UNKNOWN_TOPIC_OR_PARTITION, format_message( "topic='%s', partition=%s", $topic_name, $partition ), request => $request, io_error => $io_error );
1024 }
1025  
1026 return;
1027 }
1028  
1029 =head3 C<exists_topic_partition( $topic, $partition )>
1030  
1031 Returns true if the metadata contains information about specified combination of topic and partition.
1032 Otherwise returns false.
1033  
1034 C<exists_topic_partition()> takes the following arguments:
1035  
1036 =over 3
1037  
1038 =item C<$topic>
1039  
1040 The C<$topic> must be a normal non-false string of non-zero length.
1041  
1042 =item C<$partition>
1043  
1044 =back
1045  
1046 =cut
1047 sub exists_topic_partition {
1048 my ( $self, $topic, $partition ) = @_;
1049  
1050 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
1051 unless defined( $topic ) && ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
1052 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
1053 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
1054  
1055 unless ( %{ $self->{_metadata} } ) { # the first request
1056 $self->_update_metadata( $topic ) # hash metadata could be updated
1057 # FATAL error
1058 or $self->_error( $ERROR_CANNOT_GET_METADATA, format_message( "topic='%s'", $topic ) );
1059 }
1060  
1061 return exists $self->{_metadata}->{ $topic }->{ $partition };
1062 }
1063  
1064 =head3 C<close_connection( $server )>
1065  
1066 Closes connection with C<$server> (defined as host:port or [IPv6_host]:port).
1067  
1068 =cut
1069 sub close_connection {
1070 my ( $self, $server ) = @_;
1071  
1072 unless ( $self->is_server_known( $server ) ) {
1073 return;
1074 }
1075  
1076 $self->_closeIO( $server );
1077 return 1;
1078 }
1079  
1080 =head3 C<close>
1081  
1082 Closes connection with all known Kafka servers.
1083  
1084 =cut
1085 sub close {
1086 my ( $self ) = @_;
1087  
1088 foreach my $server ( $self->get_known_servers ) {
1089 $self->_closeIO( $server );
1090 }
1091  
1092 return;
1093 }
1094  
1095 =head3 C<cluster_errors>
1096  
1097 Returns a reference to a hash.
1098  
1099 Each hash key is the identifier of the server (host:port or [IPv6_host]:port), and the value is the last communication error
1100 with that server.
1101  
1102 An empty hash is returned if there were no communication errors.
1103  
1104 =cut
1105 sub cluster_errors {
1106 my ( $self ) = @_;
1107  
1108 my %errors;
1109 foreach my $server ( $self->get_known_servers ) {
1110 if ( my $error = $self->_io_error( $server ) ) {
1111 $errors{ $server } = $error;
1112 }
1113 }
1114  
1115 return \%errors;
1116 }
1117  
1118 =head3 C<nonfatal_errors>
1119  
1120 Returns a reference to an array of the last non-fatal errors.
1121  
1122 Maximum number of entries is set using C<MaxLoggedErrors> parameter of L<constructor|/new>.
1123  
1124 A reference to the empty array is returned if there were no non-fatal errors or parameter C<MaxLoggedErrors>
1125 is set to 0.
1126  
1127 =cut
1128 sub nonfatal_errors {
1129 my ( $self ) = @_;
1130  
1131 return $self->{_nonfatal_errors};
1132 }
1133  
1134 =head3 C<clear_nonfatals>
1135  
1136 Clears an array of the last non-fatal errors.
1137  
1138 A reference to the empty array is returned because there are no non-fatal errors now.
1139  
1140 =cut
1141 sub clear_nonfatals {
1142 my ( $self ) = @_;
1143  
1144 @{ $self->{_nonfatal_errors} } = ();
1145  
1146 return $self->{_nonfatal_errors};
1147 }
1148  
1149 #-- private attributes ---------------------------------------------------------
1150  
1151 #-- private functions ----------------------------------------------------------
1152  
1153 sub _split_host_port {
1154 my ( $server ) = @_;
1155  
1156 my ( $host, $port ) = $server=~ /^(.+):(\d+)$/;
1157 $host = $1 if $host && $host =~ /^\[(.+)\]$/;
1158  
1159 return( $host, $port );
1160 }
1161  
1162 #-- private methods ------------------------------------------------------------
1163  
1164 # Remember non-fatal error
1165 sub _remember_nonfatal_error {
1166 my ( $self, $error_code, $error, $server, $topic, $partition ) = @_;
1167  
1168 my $max_logged_errors = $self->{MaxLoggedErrors}
1169 or return;
1170  
1171 shift( @{ $self->{_nonfatal_errors} } )
1172 if scalar( @{ $self->{_nonfatal_errors} } ) == $max_logged_errors;
1173 my $msg = format_message( "[%s] Non-fatal error: %s (ErrorCode %s, server '%s', topic '%s', partition %s)",
1174 scalar( localtime ),
1175 $error // ( defined( $error_code ) && exists( $ERROR{ $error_code } ) ? $ERROR{ $error_code } : '<undef>' ),
1176 $error_code // 'IO error',
1177 $server,
1178 $topic,
1179 $partition,
1180 );
1181  
1182 say STDERR $msg
1183 if $self->debug_level;
1184  
1185 push @{ $self->{_nonfatal_errors} }, $msg;
1186  
1187 return $msg;
1188 }
1189  
1190 # Returns identifier of the cluster leader (host:port or [IPv6_host]:port)
1191 sub _find_leader_server {
1192 my ( $self, $node_id ) = @_;
1193  
1194 my $leader_server;
1195 my $IO_cache = $self->{_IO_cache};
1196 my $NodeId;
1197 foreach my $server ( keys %$IO_cache ) {
1198 $NodeId = $IO_cache->{ $server }->{NodeId};
1199 if ( defined( $NodeId ) && $NodeId == $node_id ) {
1200 $leader_server = $server;
1201 last;
1202 }
1203 }
1204  
1205 return $leader_server;
1206 }
1207  
1208 # Form a list of servers to attempt querying of the metadata
1209 sub _get_interviewed_servers {
1210 my ( $self ) = @_;
1211  
1212 my ( @priority, @secondary, @rest );
1213 my $IO_cache = $self->{_IO_cache};
1214 my $server_data;
1215 foreach my $server ( $self->get_known_servers ) {
1216 $server_data = $IO_cache->{ $server };
1217 if ( defined $server_data->{NodeId} ) {
1218 if ( $server_data->{IO} ) {
1219 push @priority, $server;
1220 } else {
1221 push @secondary, $server;
1222 }
1223 } else {
1224 push @rest, $server;
1225 }
1226 }
1227  
1228 return( shuffle( @priority ), shuffle( @secondary ), shuffle( @rest ) );
1229 }
1230  
1231 # Refresh group_coordinators for given topic
1232 sub _update_group_coordinators {
1233 my ($self, $group_id) = @_;
1234  
1235 my $CorrelationId = _get_CorrelationId();
1236 my $decoded_request = {
1237 CorrelationId => $CorrelationId,
1238 ClientId => q{},
1239 CoordinatorKey => $group_id,
1240 CoordinatorType => 0, # type is group
1241 };
1242 say STDERR format_message( '[%s] group coordinators request: %s',
1243 scalar( localtime ),
1244 $decoded_request,
1245 ) if $self->debug_level;
1246 my $encoded_request = $protocol{ $APIKEY_FINDCOORDINATOR }->{encode}->( $decoded_request );
1247  
1248 my $encoded_response_ref;
1249 my @brokers = $self->_get_interviewed_servers;
1250  
1251 # receive coordinator data
1252 foreach my $broker ( @brokers ) {
1253 last if $self->_connectIO( $broker )
1254 && $self->_sendIO( $broker, $encoded_request )
1255 && ( $encoded_response_ref = $self->_receiveIO( $broker ) );
1256 }
1257  
1258 unless ( $encoded_response_ref ) {
1259 # NOTE: it is possible to repeat the operation here
1260 return;
1261 }
1262  
1263 my $decoded_response = $protocol{ $APIKEY_FINDCOORDINATOR }->{decode}->( $encoded_response_ref );
1264 say STDERR format_message( '[%s] group coordinators: %s',
1265 scalar( localtime ),
1266 $decoded_response,
1267 ) if $self->debug_level;
1268 ( defined( $decoded_response->{CorrelationId} ) && $decoded_response->{CorrelationId} == $CorrelationId )
1269 # FATAL error
1270 or $self->_error( $ERROR_MISMATCH_CORRELATIONID );
1271 $decoded_response->{ErrorCode}
1272 and $self->_error( $decoded_response->{ErrorCode} );
1273  
1274 my $IO_cache = $self->{_IO_cache};
1275 my $server = $self->_build_server_name( @{ $decoded_response }{ 'Host', 'Port' } );
1276 $IO_cache->{ $server } = { # can add new servers
1277 IO => $IO_cache->{ $server }->{IO}, # IO or undef
1278 NodeId => $decoded_response->{NodeId},
1279 host => $decoded_response->{Host},
1280 port => $decoded_response->{Port},
1281 };
1282 $self->{_group_coordinators}->{ $group_id } = $server;
1283  
1284 return 1;
1285 }
1286  
1287 # Refresh metadata for given topic
1288 sub _update_metadata {
1289 my ( $self, $topic, $is_recursive_call ) = @_;
1290  
1291 my $CorrelationId = _get_CorrelationId();
1292 my $decoded_request = {
1293 CorrelationId => $CorrelationId,
1294 ClientId => q{},
1295 topics => [
1296 $topic // (),
1297 ],
1298 };
1299 say STDERR format_message( '[%s] metadata request: %s',
1300 scalar( localtime ),
1301 $decoded_request,
1302 ) if $self->debug_level;
1303 my $encoded_request = $protocol{ $APIKEY_METADATA }->{encode}->( $decoded_request );
1304  
1305 my $encoded_response_ref;
1306 my @brokers = $self->_get_interviewed_servers;
1307  
1308 # receive metadata
1309 foreach my $broker ( @brokers ) {
1310 last if $self->_connectIO( $broker )
1311 && $self->_sendIO( $broker, $encoded_request )
1312 && ( $encoded_response_ref = $self->_receiveIO( $broker ) );
1313 }
1314  
1315 unless ( $encoded_response_ref ) {
1316 # NOTE: it is possible to repeat the operation here
1317 return;
1318 }
1319  
1320 my $decoded_response = $protocol{ $APIKEY_METADATA }->{decode}->( $encoded_response_ref );
1321 say STDERR format_message( '[%s] metadata response: %s',
1322 scalar( localtime ),
1323 $decoded_response,
1324 ) if $self->debug_level;
1325 ( defined( $decoded_response->{CorrelationId} ) && $decoded_response->{CorrelationId} == $CorrelationId )
1326 # FATAL error
1327 or $self->_error( $ERROR_MISMATCH_CORRELATIONID );
1328  
1329 unless ( _ARRAY( $decoded_response->{Broker} ) ) {
1330 if ( $self->{AutoCreateTopicsEnable} ) {
1331 return $self->_attempt_update_metadata( $is_recursive_call, $topic, undef, $ERROR_NO_KNOWN_BROKERS );
1332 } else {
1333 # FATAL error
1334 $self->_error( $ERROR_NO_KNOWN_BROKERS, format_message( "topic='%s'", $topic ) );
1335 }
1336 }
1337  
1338 my $IO_cache = $self->{_IO_cache};
1339  
1340 # Clear the previous information about the NodeId in the IO cache
1341 $IO_cache->{ $_ }->{NodeId} = undef for @brokers;
1342  
1343 # In the IO cache update/add obtained server information
1344 foreach my $received_broker ( @{ $decoded_response->{Broker} } ) {
1345 my $server = $self->_build_server_name( @{ $received_broker }{ 'Host', 'Port' } );
1346 $IO_cache->{ $server } = { # can add new servers
1347 IO => $IO_cache->{ $server }->{IO}, # IO or undef
1348 NodeId => $received_broker->{NodeId},
1349 host => $received_broker->{Host},
1350 port => $received_broker->{Port},
1351 };
1352 }
1353  
1354 #NOTE: IO cache does not remove server that's missing in metadata
1355  
1356 # Collect the received metadata
1357 my $received_metadata = {};
1358 my $leaders = {};
1359  
1360 my $ErrorCode = $ERROR_NO_ERROR;
1361 my( $TopicName, $partition );
1362 METADATA_CREATION:
1363 foreach my $topic_metadata ( @{ $decoded_response->{TopicMetadata} } ) {
1364 $TopicName = $topic_metadata->{TopicName};
1365 undef $partition;
1366 last METADATA_CREATION
1367 if ( $ErrorCode = $topic_metadata->{ErrorCode} ) != $ERROR_NO_ERROR;
1368  
1369 foreach my $partition_metadata ( @{ $topic_metadata->{PartitionMetadata} } ) {
1370 $partition = $partition_metadata->{Partition};
1371 last METADATA_CREATION
1372 if ( $ErrorCode = $partition_metadata->{ErrorCode} ) != $ERROR_NO_ERROR
1373 && $ErrorCode != $ERROR_REPLICA_NOT_AVAILABLE;
1374 $ErrorCode = $ERROR_NO_ERROR;
1375  
1376 my $received_partition_data = $received_metadata->{ $TopicName }->{ $partition } = {};
1377 my $leader = $received_partition_data->{Leader} = $partition_metadata->{Leader};
1378 $received_partition_data->{Replicas} = [ @{ $partition_metadata->{Replicas} } ];
1379 $received_partition_data->{Isr} = [ @{ $partition_metadata->{Isr} } ];
1380  
1381 $leaders->{ $leader } = $self->_find_leader_server( $leader );
1382 }
1383 }
1384 if ( $ErrorCode != $ERROR_NO_ERROR ) {
1385 if ( exists $RETRY_ON_ERRORS{ $ErrorCode } ) {
1386 return $self->_attempt_update_metadata( $is_recursive_call, $TopicName, $partition, $ErrorCode );
1387 } else {
1388 # FATAL error
1389 $self->_error( $ErrorCode, format_message( "topic='%s'%s", $TopicName, defined( $partition ) ? ", partition=$partition" : '' ) );
1390 }
1391 }
1392  
1393 # Update metadata for received topics
1394 $self->{_metadata}->{ $_ } = $received_metadata->{ $_ } foreach keys %{ $received_metadata };
1395 $self->{_leaders}->{ $_ } = $leaders->{ $_ } foreach keys %{ $leaders };
1396  
1397 return 1;
1398 }
1399  
1400 # trying to get the metadata without error
1401 sub _attempt_update_metadata {
1402 my ( $self, $is_recursive_call, $topic, $partition, $error_code ) = @_;
1403  
1404 return if $is_recursive_call;
1405 $self->_remember_nonfatal_error( $error_code, $ERROR{ $error_code }, undef, $topic, $partition );
1406  
1407 my $attempts = $self->{SEND_MAX_ATTEMPTS};
1408 ATTEMPTS:
1409 while ( $attempts-- ) {
1410 say STDERR format_message( '[%s] sleeping for %d ms before making update metadata attempt #%d',
1411 scalar( localtime ),
1412 $self->{RETRY_BACKOFF},
1413 $self->{SEND_MAX_ATTEMPTS} - $attempts + 1,
1414 ) if $self->debug_level;
1415 Time::HiRes::sleep( $self->{RETRY_BACKOFF} / 1000 );
1416 return( 1 ) if $self->_update_metadata( $topic, 1 );
1417 }
1418 # FATAL error
1419 $self->_error( $error_code, format_message( "topic='%s'%s", $topic, defined( $partition ) ? ", partition=$partition" : '' ) );
1420  
1421 return;
1422 }
1423  
1424 # forms server identifier using supplied $host, $port
1425 sub _build_server_name {
1426 my ( $self, $host, $port ) = @_;
1427  
1428 $host = "[$host]" if is_ipv6( $host );
1429  
1430 return "$host:$port";
1431 }
1432  
1433 # remembers error communicating with the server
1434 sub _on_io_error {
1435 my ( $self, $server_data, $error ) = @_;
1436 $server_data->{error} = $error;
1437 if( $server_data->{IO} ) {
1438 $server_data->{IO}->close;
1439 $server_data->{IO} = undef;
1440 }
1441  
1442 if( blessed( $error ) && $error->isa('Kafka::Exception') ) {
1443 if( $error->code == $ERROR_MISMATCH_ARGUMENT || $error->code == $ERROR_INCOMPATIBLE_HOST_IP_VERSION ) {
1444 # rethrow known fatal errors
1445 die $error;
1446 }
1447 } else {
1448 # rethrow all unknown errors
1449 die $error;
1450 }
1451  
1452 return;
1453 }
1454  
1455 sub _io_error {
1456 my( $self, $server ) = @_;
1457 my $error;
1458 if( my $server_data = $self->{_IO_cache}->{ $server } ) {
1459 $error = $server_data->{error};
1460 }
1461 return $error;
1462 }
1463  
1464 sub _is_IO_connected {
1465 my ( $self, $server ) = @_;
1466 my $server_data = $self->{_IO_cache}->{ $server } or return;
1467 return $server_data->{IO};
1468 }
1469  
1470 # connects to a server (host:port or [IPv6_host]:port)
1471 sub _connectIO {
1472 my ( $self, $server ) = @_;
1473  
1474 my $server_data = $self->{_IO_cache}->{ $server }
1475 or $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( "Unknown server '%s' (is not found in the metadata)", $server ) )
1476 ;
1477 unless( $server_data->{IO} ) {
1478 my $error;
1479 try {
1480 $server_data->{IO} = Kafka::IO->new(
1481 host => $server_data->{host},
1482 port => $server_data->{port},
1483 timeout => $self->{timeout},
1484 ip_version => $self->{ip_version},
1485 );
1486 $server_data->{error} = undef;
1487 } catch {
1488 $error = $_;
1489 };
1490  
1491 if( defined $error ) {
1492 $self->_on_io_error( $server_data, $error );
1493 return;
1494 }
1495 }
1496  
1497 return $server_data->{IO};
1498 }
1499  
1500 sub _server_data_IO {
1501 my ( $self, $server ) = @_;
1502 my $server_data = $self->{_IO_cache}->{ $server }
1503 or $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( "Unknown server '%s' (is not found in the metadata)", $server ) )
1504 ;
1505 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( "Server '%s' is not connected", $server ) )
1506 unless $server_data->{IO}
1507 ;
1508 return ( $server_data, $server_data->{IO} );
1509 }
1510  
1511 # Send encoded request ($encoded_request) to server ($server)
1512 sub _sendIO {
1513 my ( $self, $server, $encoded_request ) = @_;
1514 my( $server_data, $io ) = $self->_server_data_IO( $server );
1515 my $sent;
1516 my $error;
1517 try {
1518 $sent = $io->send( $encoded_request );
1519 } catch {
1520 $error = $_;
1521 };
1522  
1523 if( defined $error ) {
1524 $self->_on_io_error( $server_data, $error );
1525 }
1526  
1527 return $sent;
1528 }
1529  
1530 # Receive response from a given server
1531 sub _receiveIO {
1532 my ( $self, $server, $response_timeout ) = @_;
1533 my( $server_data, $io ) = $self->_server_data_IO( $server );
1534 my $response_ref;
1535 my $error;
1536 try {
1537 $response_ref = $io->receive( 4, $response_timeout ); # response header must arrive within request-specific timeout if provided
1538 if ( $response_ref && length( $$response_ref ) == 4 ) {
1539 # received 4-byte response header with response size; try receiving the rest
1540 my $message_body_ref = $io->receive( unpack( 'l>', $$response_ref ) );
1541 $$response_ref .= $$message_body_ref;
1542 }
1543 } catch {
1544 $error = $_;
1545 };
1546  
1547 if( defined $error ) {
1548 $self->_on_io_error( $server_data, $error );
1549 }
1550  
1551 return $response_ref;
1552 }
1553  
1554 # Close connectino to $server
1555 sub _closeIO {
1556 my ( $self, $server, $keep_error ) = @_;
1557  
1558 if ( my $server_data = $self->{_IO_cache}->{ $server } ) {
1559 if ( my $io = $server_data->{IO} ) {
1560 $io->close;
1561 $server_data->{error} = undef unless $keep_error;
1562 $server_data->{IO} = undef;
1563 }
1564 }
1565  
1566 return;
1567 }
1568  
1569 # check validity of an argument to match host:port format
1570 sub _is_like_server {
1571 my ( $self, $server ) = @_;
1572  
1573 unless (
1574 defined( $server )
1575 && defined( _STRING( $server ) )
1576 && !utf8::is_utf8( $server )
1577 ) {
1578 return;
1579 }
1580  
1581 my ( $host, $port ) = _split_host_port( $server );
1582 unless ( ( is_hostname( $host ) || is_ipv4( $host ) || is_ipv6( $host ) ) && $port ) {
1583 return;
1584 }
1585  
1586 return $server;
1587 }
1588  
1589 # Handler for errors
1590 sub _error {
1591 my $self = shift;
1592 Kafka::Exception::Connection->throw( throw_args( @_ ) );
1593 }
1594  
1595 1;
1596  
1597 __END__