| 1 | package ShardedKV::Continuum::Jump; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This implementation is based heavily on the ShardedKV::Continuum::CHash module by Steffen Mueller | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.014002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Algorithm::ConsistentHash::JumpHash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use JSON::XS qw(encode_json decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | with 'ShardedKV::Continuum'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | has '_orig_continuum_spec' => ( | |||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub choose { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
choose: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $idx = Algorithm::ConsistentHash::JumpHash::jumphash_siphash($key, scalar @{$self->_orig_continuum_spec->{ids}}); | |||||||||||||||||||||||||||
| 25 | 0 | return $self->_orig_continuum_spec->{ids}->[$idx]; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub serialize { | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
serialize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 0 | encode_json( $self->_orig_continuum_spec ) | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub deserialize { | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 0 | return $class->new(from => decode_json( $_[1] )); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub clone { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $clone = ref($self)->new(from => $self->_orig_continuum_spec); | |||||||||||||||||||||||||||
| 41 | 0 | return $clone; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub extend { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
extend: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 0 | my $spec = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | $self->_assert_spec_ok($spec); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Build clone of the original spec (to avoid action at a | ||||||||||||||||||||||||||||
| 51 | # distance) and add the new nodes. | ||||||||||||||||||||||||||||
| 52 | 0 | my $orig_spec = $self->_orig_continuum_spec; | |||||||||||||||||||||||||||
| 53 | my $clone_spec = { | ||||||||||||||||||||||||||||
| 54 | %$orig_spec, # replicas + in case there's other gunk in it, at least make an effort | ||||||||||||||||||||||||||||
| 55 | 0 | ids => [ @{$orig_spec->{ids}} ], # deep clone | |||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | 0 | push @{ $clone_spec->{ids} }, @{ $spec->{ids} }; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | $self->{_orig_continuum_spec} = $clone_spec; | |||||||||||||||||||||||||||
| 60 | 0 | return 1; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub get_bucket_names { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_bucket_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return @{ $self->_orig_continuum_spec()->{ids} }; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub BUILD { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $from = delete $args->{from}; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (ref($from) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->{_orig_continuum_spec} = $from; | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 0 | die "Invalid 'from' specification for " . __PACKAGE__; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub _assert_spec_ok { | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my ($self, $spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_assert_spec_ok: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 82 | Carp::croak("Continuum spec must be a hash of the form {ids => [qw(node1 node2 node3)]}") | ||||||||||||||||||||||||||||
| 83 | if not ref($spec) eq 'HASH' | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | or not ref($spec->{ids}) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY"
MC/DC0%0/2
ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY"
ref $spec eq "HASH"
ref $spec->{"ids"} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $spec eq "HASH"B= ref $spec->{"ids"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 85 | 0 | return 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | use ShardedKV; | ||||||||||||||||||||||||||||
| 96 | use ShardedKV::Continuum::Jump; | ||||||||||||||||||||||||||||
| 97 | my $skv = ShardedKV->new( | ||||||||||||||||||||||||||||
| 98 | continuum => ShardedKV::Continuum::Jump->new( | ||||||||||||||||||||||||||||
| 99 | from => { | ||||||||||||||||||||||||||||
| 100 | ids => [qw(node1 node2 node3 node4)], | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | ), | ||||||||||||||||||||||||||||
| 103 | storages => {...}, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | ... | ||||||||||||||||||||||||||||
| 106 | $skv->extend({ids => [qw(node5 node6 node7)]}); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | A continuum implementation based on Google's Jump consistent hashing algorithm. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | It uses SipHash to turn the string keys into 64-bit integers. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Note that the *order* of shard IDs is significant, unlike with other continuum implementations. This is a limitation of the Jump algorithm. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | * L<ShardedKV> | ||||||||||||||||||||||||||||
| 120 | * L<ShardedKV::Continuum> | ||||||||||||||||||||||||||||
| 121 | * L<ShardedKV::Continuum::CHash> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Damian Gryski, E<lt>damian@gryski.comE<gt> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Copyright (C) 2014 by Damian Gryski | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 132 | it under the same terms as Perl itself, either Perl version 5.18.2 or, | ||||||||||||||||||||||||||||
| 133 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||