| 1 | package Kafka::Internals; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Kafka::Internals - Constants and functions used internally. | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | This documentation refers to C<Kafka::Internals> version 1.07 . | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | use 5.010; | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | our $VERSION = '1.07'; | ||
| 22 | |||
| 23 | use Exporter qw( | ||
| 24 | import | ||
| 25 | ); | ||
| 26 | |||
| 27 | our @EXPORT_OK = qw( | ||
| 28 | $APIKEY_PRODUCE | ||
| 29 | $APIKEY_FETCH | ||
| 30 | $APIKEY_OFFSET | ||
| 31 | $APIKEY_METADATA | ||
| 32 | $APIKEY_FINDCOORDINATOR | ||
| 33 | $APIKEY_APIVERSIONS | ||
| 34 | $APIKEY_OFFSETCOMMIT | ||
| 35 | $APIKEY_OFFSETFETCH | ||
| 36 | $DEFAULT_RAISE_ERROR | ||
| 37 | $MAX_CORRELATIONID | ||
| 38 | $MAX_INT16 | ||
| 39 | $MAX_INT32 | ||
| 40 | $MAX_SOCKET_REQUEST_BYTES | ||
| 41 | $PRODUCER_ANY_OFFSET | ||
| 42 | debug_level | ||
| 43 | _isbig | ||
| 44 | _get_CorrelationId | ||
| 45 | format_message | ||
| 46 | format_reference | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | use overload; | ||
| 52 | use Carp; | ||
| 53 | use Const::Fast; | ||
| 54 | use Data::Dumper (); | ||
| 55 | use Params::Util qw( | ||
| 56 | _INSTANCE | ||
| 57 | ); | ||
| 58 | use Try::Tiny; | ||
| 59 | |||
| 60 | use Kafka qw( | ||
| 61 | %ERROR | ||
| 62 | $ERROR_MISMATCH_ARGUMENT | ||
| 63 | ); | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | =head1 SYNOPSIS | ||
| 68 | |||
| 69 | use 5.010; | ||
| 70 | use strict; | ||
| 71 | use warnings; | ||
| 72 | |||
| 73 | use Kafka::Internals qw( | ||
| 74 | $MAX_SOCKET_REQUEST_BYTES | ||
| 75 | ); | ||
| 76 | |||
| 77 | my $bin_stream_size = $MAX_SOCKET_REQUEST_BYTES; | ||
| 78 | |||
| 79 | =head1 DESCRIPTION | ||
| 80 | |||
| 81 | This module is private and should not be used directly. | ||
| 82 | |||
| 83 | In order to achieve better performance, functions of this module do | ||
| 84 | not perform arguments validation. | ||
| 85 | |||
| 86 | =head2 EXPORT | ||
| 87 | |||
| 88 | The following constants are available for export | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | #-- Api Keys | ||
| 93 | |||
| 94 | =head3 C<$APIKEY_PRODUCE> | ||
| 95 | |||
| 96 | The numeric code that the C<ApiKey> in the request take for the C<ProduceRequest> request type. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | const our $APIKEY_PRODUCE => 0; | ||
| 100 | |||
| 101 | =head3 C<$APIKEY_FETCH> | ||
| 102 | |||
| 103 | The numeric code that the C<ApiKey> in the request take for the C<FetchRequest> request type. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | const our $APIKEY_FETCH => 1; | ||
| 107 | |||
| 108 | =head3 C<$APIKEY_OFFSET> | ||
| 109 | |||
| 110 | The numeric code that the C<ApiKey> in the request take for the C<OffsetRequest> request type. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | const our $APIKEY_OFFSET => 2; | ||
| 114 | |||
| 115 | =head3 C<$APIKEY_METADATA> | ||
| 116 | |||
| 117 | The numeric code that the C<ApiKey> in the request take for the C<MetadataRequest> request type. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | const our $APIKEY_METADATA => 3; | ||
| 121 | # The numeric code that the ApiKey in the request take for the C<LeaderAndIsrRequest> request type. | ||
| 122 | const our $APIKEY_LEADERANDISR => 4; # Not used now | ||
| 123 | # The numeric code that the ApiKey in the request take for the C<StopReplicaRequest> request type. | ||
| 124 | const our $APIKEY_STOPREPLICA => 5; # Not used now | ||
| 125 | |||
| 126 | =head3 C<$APIKEY_OFFSETCOMMIT> | ||
| 127 | |||
| 128 | The numeric code that the ApiKey in the request take for the C<OffsetCommitRequest> request type. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | const our $APIKEY_OFFSETCOMMIT => 8; | ||
| 132 | |||
| 133 | =head3 C<$APIKEY_OFFSETFETCH> | ||
| 134 | |||
| 135 | The numeric code that the ApiKey in the request take for the C<OffsetFetchRequest> request type. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | const our $APIKEY_OFFSETFETCH => 9; | ||
| 139 | |||
| 140 | =head3 C<$APIKEY_FINDCOORDINATOR> | ||
| 141 | |||
| 142 | The numeric code that the C<ApiKey> in the request take for the C<FindCoordinator> request type. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | const our $APIKEY_FINDCOORDINATOR => 10; | ||
| 146 | |||
| 147 | =head3 C<$APIKEY_APIVERSIONS> | ||
| 148 | |||
| 149 | The numeric code that the C<ApiKey> in the request take for the C<ApiVersions> request type. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | const our $APIKEY_APIVERSIONS => 18; | ||
| 153 | |||
| 154 | # Important configuration properties | ||
| 155 | |||
| 156 | =head3 C<$MAX_SOCKET_REQUEST_BYTES> | ||
| 157 | |||
| 158 | The maximum number of bytes in a socket request. | ||
| 159 | |||
| 160 | The maximum size of a request that the socket server will accept. | ||
| 161 | Default limit (as configured in F<server.properties>) is 104857600. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | const our $MAX_SOCKET_REQUEST_BYTES => 100 * 1024 * 1024; | ||
| 165 | |||
| 166 | =head3 C<$PRODUCER_ANY_OFFSET> | ||
| 167 | |||
| 168 | According to Apache Kafka documentation: 'When the producer is sending messages it doesn't actually know the offset and can fill in any | ||
| 169 | value here it likes.' | ||
| 170 | |||
| 171 | =cut | ||
| 172 | const our $PRODUCER_ANY_OFFSET => 0; | ||
| 173 | |||
| 174 | =head3 C<$MAX_CORRELATIONID> | ||
| 175 | |||
| 176 | Largest positive integer on 32-bit machines. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | const our $MAX_CORRELATIONID => 0x7fffffff; | ||
| 180 | |||
| 181 | =head3 C<$MAX_INT32> | ||
| 182 | |||
| 183 | Largest positive integer on 32-bit machines. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | const our $MAX_INT32 => 0x7fffffff; | ||
| 187 | |||
| 188 | =head3 C<$MAX_INT16> | ||
| 189 | |||
| 190 | Largest positive int16 value. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | const our $MAX_INT16 => 0x7fff; | ||
| 194 | |||
| 195 | #-- public functions ----------------------------------------------------------- | ||
| 196 | |||
| 197 | #-- private functions ---------------------------------------------------------- | ||
| 198 | |||
| 199 | # Used to generate a CorrelationId. | ||
| 200 | sub _get_CorrelationId { | ||
| 201 | return( -int( rand( $MAX_CORRELATIONID ) ) ); | ||
| 202 | } | ||
| 203 | |||
| 204 | # Verifies that the argument is of Math::BigInt type | ||
| 205 | sub _isbig { | ||
| 206 | my ( $num ) = @_; | ||
| 207 | |||
| 208 | return defined _INSTANCE( $num, 'Math::BigInt' ); | ||
| 209 | } | ||
| 210 | |||
| 211 | #-- public attributes ---------------------------------------------------------- | ||
| 212 | |||
| 213 | =head2 METHODS | ||
| 214 | |||
| 215 | The following methods are defined in the C<Kafka::Internals>: | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | #-- public methods ------------------------------------------------------------- | ||
| 220 | |||
| 221 | =head3 C<debug_level( $flags )> | ||
| 222 | |||
| 223 | Gets or sets debug level for a particular L<Kafka|Kafka> module, based on environment variable C<PERL_KAFKA_DEBUG> or flags. | ||
| 224 | |||
| 225 | $flags - (string) argument that can be used to pass coma delimited module names (omit C<Kafka::>). | ||
| 226 | |||
| 227 | Returns C<$DEBUG> level for the module from which C<debug_level> was called. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | our %_debug_levels; # per-module levels cache to speed-up multiple calls to debug_level() | ||
| 231 | sub debug_level { | ||
| 232 | no strict 'refs'; ## no critic | ||
| 233 | |||
| 234 | my $class = ref( $_[0] ) || $_[0]; | ||
| 235 | |||
| 236 | return ${ $_debug_levels{ $class } } if @_ == 1 && exists $_debug_levels{ $class }; | ||
| 237 | |||
| 238 | my $flags = $_[1] // $ENV{PERL_KAFKA_DEBUG}; | ||
| 239 | if( defined $flags ) { | ||
| 240 | foreach my $spec ( split /\s*,\s*/, $flags ) { | ||
| 241 | my @elements = split( /\s*:\s*/, $spec, 2 ); | ||
| 242 | my( $module_name, $level ); | ||
| 243 | if ( scalar( @elements ) > 1 ) { | ||
| 244 | ( $module_name, $level ) = @elements; | ||
| 245 | } else { | ||
| 246 | $module_name = ( $class =~ /([^:]+)$/ )[0]; | ||
| 247 | $level = $spec; | ||
| 248 | } | ||
| 249 | |||
| 250 | *{ "Kafka::${module_name}::DEBUG" } = \$level; ## no critic | ||
| 251 | } | ||
| 252 | } | ||
| 253 | $_debug_levels{ $class } = \${ "${class}::DEBUG" }; ## no critic | ||
| 254 | |||
| 255 | return ${ $_debug_levels{ $class } }; | ||
| 256 | } | ||
| 257 | |||
| 258 | =head2 format_reference | ||
| 259 | |||
| 260 | say format_reference( $object ); | ||
| 261 | |||
| 262 | Dumps reference using preconfigured L<Data::Dumper>. Produces less verbose | ||
| 263 | output than default L<Data::Dumper> settings. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | my $dumper; | ||
| 268 | my $empty_array = []; | ||
| 269 | |||
| 270 | sub format_reference { | ||
| 271 | my ( $value ) = @_; | ||
| 272 | |||
| 273 | unless( $dumper ) { | ||
| 274 | $dumper = Data::Dumper->new( $empty_array ) | ||
| 275 | ->Indent( 0 ) | ||
| 276 | ->Terse( 1 ) | ||
| 277 | ->Quotekeys( 0 ) | ||
| 278 | ->Sortkeys( 1 ) | ||
| 279 | ->Useperl( 1 ) # XS version seems to have a bug which sometimes results in modification of original object | ||
| 280 | # ->Sparseseen( 1 ) # speed up since we don't use "Seen" hash | ||
| 281 | ; | ||
| 282 | } | ||
| 283 | |||
| 284 | my $r; | ||
| 285 | if ( | ||
| 286 | overload::Overloaded( $value ) && | ||
| 287 | overload::Method( $value, '""' ) | ||
| 288 | ) { | ||
| 289 | $r = "$value"; # force stringification | ||
| 290 | } else { | ||
| 291 | $r = $dumper->Values( [ $value ] )->Dump; | ||
| 292 | $dumper->Reset->Values( $empty_array ); | ||
| 293 | } | ||
| 294 | |||
| 295 | return $r; | ||
| 296 | } | ||
| 297 | |||
| 298 | =head2 format_message | ||
| 299 | |||
| 300 | $string = format_message( 'Object %d loaded. Status: %s', $id, $message ); | ||
| 301 | |||
| 302 | Returns string formatted using printf-style syntax. | ||
| 303 | |||
| 304 | If there are more than one argument and the first argument contains C<%...> | ||
| 305 | conversions, arguments are converted to a string message using C<sprintf()>. In this case, undefined | ||
| 306 | values are printed as C<< <undef> >> and references are converted to strings using L</format_reference>. | ||
| 307 | |||
| 308 | =cut | ||
| 309 | sub format_message { | ||
| 310 | my $format = shift // return; | ||
| 311 | |||
| 312 | my $got = scalar @_; | ||
| 313 | |||
| 314 | return $format unless $got && $format =~ /\%/; | ||
| 315 | |||
| 316 | my $expected = 0; | ||
| 317 | while ( $format =~ /(%%|%[^%])/g ) { | ||
| 318 | next if $1 eq '%%'; # don't count escape sequence | ||
| 319 | ++$expected; | ||
| 320 | } | ||
| 321 | |||
| 322 | Carp::cluck "Wrong number of arguments: $expected vs $got" unless $got == $expected; | ||
| 323 | |||
| 324 | return sprintf $format, map { | ||
| 325 | !defined $_ | ||
| 326 | ? '<undef>' | ||
| 327 | : ref $_ | ||
| 328 | ? format_reference( $_ ) | ||
| 329 | : $_ | ||
| 330 | } @_; | ||
| 331 | } | ||
| 332 | |||
| 333 | #-- private attributes --------------------------------------------------------- | ||
| 334 | |||
| 335 | #-- private methods ------------------------------------------------------------ | ||
| 336 | |||
| 337 | 1; | ||
| 338 | |||
| 339 | __END__ |