NAME
Kafka::Connection - Object interface to connect to a kafka cluster.
VERSION
This documentation refers to Kafka::Connection
version 0.800_6 .
SYNOPSIS
use 5.010;
use strict;
use warnings;
use Scalar::Util qw(
blessed
);
use Try::Tiny;
# A simple example of Kafka::Connection usage:
use Kafka::Connection;
# connect to local cluster with the defaults
my $connection;
try {
$connection = Kafka::Connection->new( host => 'localhost' );
} catch {
if ( blessed( $_ ) && $_->isa( 'Kafka::Exception' ) ) {
warn $_->message, "\n", $_->trace->as_string, "\n";
exit;
} else {
die $_;
}
};
# Closes the connection and cleans up
undef $connection;
DESCRIPTION
The main features of the Kafka::Connection
class are:
Provides API for communication with Kafka 0.8 cluster.
Performs requests encoding and responses decoding, provides automatic selection or promotion of a leader server from Kafka cluster.
Provides information about Kafka cluster.
CONSTRUCTOR
new
Creates Kafka::Connection
object for interaction with Kafka cluster. Returns created Kafka::Connection
object.
new()
takes arguments in key-value pairs. The following arguments are currently recognized:
host => $host
-
$host
is any Apache Kafka cluster host to connect to. It can be a hostname or the IP-address in the "xx.xx.xx.xx" form.Optional. Either
host
orbroker_list
must be supplied. port => $port
-
Optional, default =
$KAFKA_SERVER_PORT
.$port
is the attribute denoting the port number of the service we want to access (Apache Kafka service).$port
should be an integer number.$KAFKA_SERVER_PORT
is the default Apache Kafka server port constant (9092
) that can be imported from the Kafka module. broker_list => $broker_list
-
Optional,
$broker_list
is a reference to array of the host:port strings, defining the list of Kafka servers. This list will be used to locate the new leader if the server specified viahost => $host
andport => $port
arguments becomes unavailable. Eitherhost
orbroker_list
must be supplied. timeout => $timeout
-
Optional, default =
$REQUEST_TIMEOUT
.$timeout
specifies how long we wait for the remote server to respond.$timeout
is in seconds, could be a positive integer or a floating-point number.$REQUEST_TIMEOUT
is the default timeout that can be imported from the Kafka module.Special behavior when
timeout
is set toundef
:
Alarms are not used internally (namely when performing
gethostbyname
).Default
$REQUEST_TIMEOUT
is used for the rest of IO operations.
CorrelationId => $correlation_id
-
Optional, default =
undef
.Correlation
is a user-supplied integer. It will be passed back with the response by the server, unmodified. The$correlation_id
should be an integer number.An exception is thrown if
CorrelationId
in response does not match the one supplied in request.If
CorrelationId
is not provided, it is set to a random negative integer. SEND_MAX_RETRIES => $retries
-
Optional, default =
$SEND_MAX_RETRIES
.$SEND_MAX_RETRIES
is the default number of retries that can be imported from the Kafka module and = 3 .The leader may be unavailable transiently, which can fail the sending of a message. This property specifies the number of retries when such failures occur. The
$retries
should be an integer number. RETRY_BACKOFF => $backoff
-
Optional, default =
$RETRY_BACKOFF
.$RETRY_BACKOFF
is the default timeout that can be imported from the Kafka module and = 100 ms.Since leader election takes a bit of time, this property specifies the amount of time, in milliseconds, that the producer waits before refreshing the metadata. The
$backoff
should be an integer number.
METHODS
The following methods are defined for the Kafka::Producer
class:
get_known_servers
Returns the list of known Kafka servers (in host:port format).
is_server_known( $server )
Returns true, if $server
(host:port) is known in cluster.
is_server_alive( $server )
Returns true, if successful connection is established with $server
(host:port).
receive_response_to_request( $request )
$request
is a reference to the hash representing the structure of the request.
This method encodes $request
, passes it to the leader of cluster, receives reply, decodes and returns it in a form of hash reference.
WARNING:
This method should be considered private and should not be called by an end user.
In order to achieve better performance, this method does not perform arguments validation.
close_connection( $server )
Closes connection with $server
(defined as host:port).
close
Closes connection with all known Kafka servers.
cluster_errors
Returns a reference to a hash.
Each hash key is the identifier of the server (host:port), and the value is the last communication error with that server.
An empty hash is returned if there were no communication errors.
DIAGNOSTICS
When error is detected, an exception, represented by object of Kafka::Exception::Connection
class, is thrown (see Kafka::Exceptions).
code and a more descriptive message provide information about exception. Consult documentation of the Kafka::Exceptions for the list of all available methods.
Invalid argument
-
Invalid argument was provided to
new
constructor or to other method. Can't send
-
Request cannot be sent to Kafka.
Can't recv
-
Response cannot be received from Kafka.
Can't bind
-
A successful TCP connection can't be established on given host and port.
Can't get metadata
-
Error detected during parsing of response from Kafka.
Leader not found
-
Failed to locate leader of Kafka cluster.
Mismatch CorrelationId
-
Mismatch of
CorrelationId
of request and response. There are no known brokers
-
Failed to locate cluster broker.
Can't get metadata
-
Received meta data is incorrect or missing.
SEE ALSO
The basic operation of the Kafka package modules:
Kafka - constants and messages used by the Kafka package modules.
Kafka::Connection - interface to connect to a Kafka cluster.
Kafka::Producer - interface for producing client.
Kafka::Consumer - interface for consuming client.
Kafka::Message - interface to access Kafka message properties.
Kafka::Int64 - functions to work with 64 bit elements of the protocol on 32 bit systems.
Kafka::Protocol - functions to process messages in the Apache Kafka's Protocol.
Kafka::IO - low-level interface for communication with Kafka server.
Kafka::Exceptions - module designated to handle Kafka exceptions.
Kafka::Internals - internal constants and functions used by several package modules.
A wealth of detail about the Apache Kafka and the Kafka Protocol:
Main page at http://kafka.apache.org/
Kafka Protocol at https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
AUTHOR
Sergey Gladkov, <sgladkov@trackingsoft.com>
CONTRIBUTORS
Alexander Solovey
Jeremy Jordan
Vlad Marchenko
COPYRIGHT AND LICENSE
Copyright (C) 2012-2013 by TrackingSoft LLC.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic at http://dev.perl.org/licenses/artistic.html.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.