This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Tarantool -- an efficient key/value data store".

The branch perl-iproto-async has been updated
       via  dd9c4eac159aa6b49b56d82e2444cd824ef3fa2b (commit)
      from  681118717c1a02a948a8997433325095ffeebb2f (commit)

Summary of changes:
 mod/silverbox/client/perl/lib/MR/IProto.pm         |    3 ++-
 mod/silverbox/client/perl/lib/MR/IProto/Cluster.pm |   20 +++++++++++++++++---
 .../client/perl/lib/MR/IProto/Cluster/Server.pm    |   15 ++++++++++++++-
 3 files changed, 33 insertions(+), 5 deletions(-)

commit dd9c4eac159aa6b49b56d82e2444cd824ef3fa2b
Author: Aleksey Mashanov <[email protected]>
Date:   Wed Dec 1 17:43:03 2010 +0300

    Weight support for hash-crc32

diff --git a/mod/silverbox/client/perl/lib/MR/IProto.pm 
b/mod/silverbox/client/perl/lib/MR/IProto.pm
index 12c20dc..19293b2 100644
--- a/mod/silverbox/client/perl/lib/MR/IProto.pm
+++ b/mod/silverbox/client/perl/lib/MR/IProto.pm
@@ -369,11 +369,12 @@ around BUILDARGS => sub {
         $clusterargs{balance} = delete $args{balance} if exists $args{balance};
         $clusterargs{servers} = [
             map {
-                my ($host, $port) = /^(.+):(\d+)$/;
+                my ($host, $port, $weight) = split /:/, $_;
                 $server_class->new(
                     %srvargs,
                     host => $host,
                     port => $port,
+                    defined $weight ? ( weight => $weight ) : (),
                 );
             } split /,/, delete $args{servers}
         ];
diff --git a/mod/silverbox/client/perl/lib/MR/IProto/Cluster.pm 
b/mod/silverbox/client/perl/lib/MR/IProto/Cluster.pm
index 7cd4513..6f015a1 100644
--- a/mod/silverbox/client/perl/lib/MR/IProto/Cluster.pm
+++ b/mod/silverbox/client/perl/lib/MR/IProto/Cluster.pm
@@ -13,6 +13,7 @@ This class is used to implement balancing between several 
servers.
 use Mouse;
 use Mouse::Util::TypeConstraints;
 use MR::IProto::Cluster::Server;
+use String::CRC32 qw(crc32);
 
 =head1 EXPORTED CONSTANTS
 
@@ -118,6 +119,12 @@ has _rr_servers => (
     lazy_build => 1,
 );
 
+has _hash_servers => (
+    is  => 'rw',
+    isa => 'MR::IProto::Cluster::Servers',
+    lazy_build => 1,
+);
+
 =head1 PUBLIC METHODS
 
 =over
@@ -175,7 +182,7 @@ sub _build__ketama {
            push @ketama, [crc32($server->host.$server->port.$i), $server];
         }
     }
-    return sort { $a->[0] cmp $b->[0] } @ketama;
+    return [ sort { $a->[0] cmp $b->[0] } @ketama ];
 }
 
 sub _build__rr_servers {
@@ -183,6 +190,11 @@ sub _build__rr_servers {
     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;
@@ -193,19 +205,21 @@ 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->servers->[$hash % @{$self->servers}];
+        $server = $self->_hash_servers->[$hash % @{$self->_hash_servers}];
         return $server if $server->active;
         $hash += crc32($_, $hash_) >> 16;
     }
 
-    return $self->servers->[rand @{$self->servers}]; #last resort
+    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}) {
diff --git a/mod/silverbox/client/perl/lib/MR/IProto/Cluster/Server.pm 
b/mod/silverbox/client/perl/lib/MR/IProto/Cluster/Server.pm
index 375ca01..a9d7371 100644
--- a/mod/silverbox/client/perl/lib/MR/IProto/Cluster/Server.pm
+++ b/mod/silverbox/client/perl/lib/MR/IProto/Cluster/Server.pm
@@ -19,10 +19,11 @@ use MR::IProto::Message;
 coerce 'MR::IProto::Cluster::Server'
     => from 'Str'
     => via {
-        my ($host, $port) = split /:/, $_;
+        my ($host, $port, $weight) = split /:/, $_;
         __PACKAGE__->new(
             host => $host,
             port => $port,
+            defined $weight ? ( weight => $weight ) : (),
         );
     };
 
@@ -54,6 +55,18 @@ has port => (
     required => 1,
 );
 
+=item weight
+
+Server weight.
+
+=cut
+
+has weight => (
+    is  => 'ro',
+    isa => 'Int',
+    default => 1,
+);
+
 =item connect_timeout
 
 Timeout of connect operation.

-- 
Tarantool -- an efficient key/value data store

_______________________________________________
Mailing list: https://launchpad.net/~tarantool-developers
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~tarantool-developers
More help   : https://help.launchpad.net/ListHelp

Reply via email to