/usr/share/perl5/MR/IProto/Cluster.pm is in libmr-tarantool-perl 0.0.24-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | package MR::IProto::Cluster;
=head1 NAME
MR::IProto::Cluster - cluster of servers
=head1 DESCRIPTION
This class is used to implement balancing between several servers.
=cut
use Mouse;
use Mouse::Util::TypeConstraints;
use MR::IProto::Cluster::Server;
use Digest::CRC qw(crc32);
=head1 EXPORTED CONSTANTS
=over
=item RR
Round robin algorithm
=item HASH
Hashing algorithm using CRC32
=item KETAMA
Ketama algorithm
=back
=cut
use Exporter 'import';
our @EXPORT_OK = qw( RR HASH KETAMA );
coerce 'MR::IProto::Cluster'
=> from 'Str'
=> via { __PACKAGE__->new( servers => $_ ) };
subtype 'MR::IProto::Cluster::Servers'
=> as 'ArrayRef[MR::IProto::Cluster::Server]'
=> where { scalar @_ };
coerce 'MR::IProto::Cluster::Servers'
=> from 'Str'
=> via {
my $type = find_type_constraint('MR::IProto::Cluster::Server');
[ map $type->coerce($_), split /,/, $_ ];
};
use constant {
RR => 1,
HASH => 2,
KETAMA => 3,
};
enum 'MR::IProto::Balance' => (
RR,
HASH,
KETAMA,
);
coerce 'MR::IProto::Balance'
=> from 'Str',
=> via {
$_ eq 'hash-crc32' ? HASH
: $_ eq 'ketama' ? KETAMA
: RR;
};
=head1 ATTRIBUTES
=over
=item balance
Balancing algorithms.
Possible values are constants: RR, HASH, KETAMA.
Or their string analogs: 'round-robin', 'hash-crc32', 'ketama'.
=cut
has balance => (
is => 'ro',
isa => 'MR::IProto::Balance',
default => RR,
coerce => 1,
);
=item servers
ArrayRef of L<MR::IProto::Cluster::Server>.
=cut
has servers => (
is => 'ro',
isa => 'MR::IProto::Cluster::Servers',
required => 1,
coerce => 1,
);
=back
=cut
has _one => (
is => 'ro',
isa => 'Maybe[MR::IProto::Cluster::Server]',
lazy_build => 1,
);
has _ketama => (
is => 'ro',
isa => 'ArrayRef[ArrayRef]',
lazy_build => 1,
);
has _rr_servers => (
is => 'rw',
isa => 'MR::IProto::Cluster::Servers',
lazy_build => 1,
);
has _hash_servers => (
is => 'rw',
isa => 'MR::IProto::Cluster::Servers',
lazy_build => 1,
);
=head1 PUBLIC METHODS
=over
=item server( $key? )
Get server from balancing using C<$key>.
=cut
sub server {
my ($self, $key) = @_;
my $one = $self->_one;
return $one if defined $one;
my $method = $self->balance == RR ? '_balance_rr'
: $self->balance == KETAMA ? '_balance_ketama'
: '_balance_hash';
return $self->$method($key);
}
=item timeout( $new? )
Used to set C<$new> timeout value to all servers.
If argument is skipped and timeout is equal for all servers then returns
it value, if timeout is different then returns undef.
=cut
sub timeout {
my $self = shift;
if(@_) {
my $timeout = shift;
$_->timeout($timeout) foreach @{$self->servers};
return $timeout;
}
else {
my $timeout;
foreach my $t ( map $_->timeout, @{$self->servers} ) {
return if defined $timeout && $timeout != $t;
$timeout = $t unless defined $timeout;
}
return $timeout;
}
}
=back
=cut
sub _build__one {
my ($self) = @_;
return @{$self->servers} == 1 ? $self->servers->[0] : undef;
}
sub _build__ketama {
my $self = shift;
my @ketama;
foreach my $server (@{$self->servers}) {
for my $i (0..10) {
push @ketama, [crc32($server->host.$server->port.$i), $server];
}
}
return [ sort { $a->[0] cmp $b->[0] } @ketama ];
}
sub _build__rr_servers {
my ($self) = @_;
return [ @{ $self->servers } ];
}
sub _build__hash_servers {
my ($self) = @_;
return [ map { my @s; my $s = $_; push @s, $s for ( 1 .. $s->weight ); @s } @{ $self->servers } ];
}
sub _balance_rr {
my ($self) = @_;
$self->_clear_rr_servers() if @{$self->_rr_servers} == 0;
return splice(@{$self->_rr_servers}, int(rand(@{$self->_rr_servers})), 1);
}
sub _balance_hash {
my ($self, $key) = @_;
my ($hash_, $hash, $server);
die "Cannot balance hash without key" unless defined $key;
$hash = $hash_ = crc32($key) >> 16;
for (0..19) {
$server = $self->_hash_servers->[$hash % @{$self->_hash_servers}];
return $server if $server->active;
$hash += crc32($_, $hash_) >> 16;
}
return $self->_hash_servers->[rand @{$self->_hash_servers}]; #last resort
}
sub _balance_ketama {
my ($self, $key) = @_;
die "Cannot balance ketama without key" unless defined $key;
my $idx = crc32($key);
foreach (@{$self->_ketama}) {
next unless ($_);
return $_->[1] if ($_->[0] >= $idx);
}
return $self->_ketama->[0]->[1];
}
=head1 SEE ALSO
L<MR::IProto>, L<MR::IProto::Cluster::Server>.
=cut
no Mouse;
__PACKAGE__->meta->make_immutable();
1;
|