| 1 | package Net::Async::Redis::Cluster::Node; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Object::Pad; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | class Net::Async::Redis::Cluster::Node :isa(IO::Async::Notifier); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '6.006'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Net::Async::Redis::Cluster::Replica; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Scalar::Util qw(refaddr blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | '""' => method { 'NaRedis::Cluster::Node[id=' . $self->id . ']' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | '0+' => method { refaddr($self) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | bool => method { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | field $start:reader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | field $end:reader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
end: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | field $primary:reader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
primary: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | field $replicas:reader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replicas: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | field $cluster:reader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cluster: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | method configure (%args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $start = delete $args{start} if exists $args{start}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $end = delete $args{end} if exists $args{end}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $primary = delete $args{primary} if exists $args{primary}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if(exists $args{replicas}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | $replicas = [ | ||||||||||||||||||||||||||||||||||||||||||
| 30 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | blessed($_) && $_->isa('Net::Async::Redis::Cluster::Replica') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($_) and $_->isa("Net::Async::Redis::Cluster::Replica")
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Net::Async::Redis::Cluster::Replica")
builtin::blessed($_)
$_->isa("Net::Async::Redis::Cluster::Replica")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Net::Async::Redis::Cluster::Replica")
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | ? $_ | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # Upgrade plain arrayref from Redis into a proper object | ||||||||||||||||||||||||||||||||||||||||||
| 34 | : Net::Async::Redis::Cluster::Replica->new( | ||||||||||||||||||||||||||||||||||||||||||
| 35 | host => $_->[0], | ||||||||||||||||||||||||||||||||||||||||||
| 36 | port => $_->[1], | ||||||||||||||||||||||||||||||||||||||||||
| 37 | id => $_->[2] | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | } (delete $args{replicas})->@* | |||||||||||||||||||||||||||||||||||||||||
| 40 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | Scalar::Util::weaken($cluster = delete $args{cluster}) if exists $args{cluster}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | for my $k (@Net::Async::Redis::Cluster::CONFIG_KEYS) { | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{$k} = delete $args{$k} if exists $args{$k}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | return $self->next::method(%args); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | } | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub from_arrayref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_arrayref: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my ($class, $arrayref, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ($start, $end, $primary, @replicas) = $arrayref->@*; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | die 'invalid start' if $start > $end or $start < 0 or $start >= 16384; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$start > $end or $start < 0 or $start >= 16384
$start > $end or $start < 0
MC/DC0%0/3
$start > $end or $start < 0 or $start >= 16384
$start > $end
$start < 0
$start >= 16384
Truth table0%0/4
A or B or C
A= $start > $endB= $start < 0C= $start >= 16384
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | die 'invalid end' if $end >= 16384; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | die 'no primary found' unless $primary; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | die 'invalid primary' unless $primary->[0] ne "" && $primary->[1] > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$primary->[0] ne "" and $primary->[1] > 0
MC/DC0%0/2
$primary->[0] ne "" and $primary->[1] > 0
$primary->[0] ne ""
$primary->[1] > 0
Truth table0%0/3
A and B
A= $primary->[0] ne ""B= $primary->[1] > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | return $class->new( | |||||||||||||||||||||||||||||||||||||||||
| 57 | start => $start, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | end => $end, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | primary => $primary, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | replicas => \@replicas, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | %args | ||||||||||||||||||||||||||||||||||||||||||
| 62 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | method replica_list { $replicas->@* } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replica_list: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | method replica_count { 0 + $replicas->@* } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replica_count: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | method id { $primary->[2] // $self->host_port } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=2 SCAR=14.0
Pod
undocumented
Condition0%0/3
$primary->[2] // $self->host_port
MC/DC0%0/2
$primary->[2] // $self->host_port
$primary->[2]
$self->host_port
Truth table0%0/3
A // B
A= $primary->[2]B= $self->host_port
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | method host_port { join ':', $primary->@[0, 1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host_port: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | method primary_connection ($conn = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
primary_connection: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return $self->{primary_connection} = $conn if $conn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return $self->{primary_connection} ||= $self->establish_primary_connection; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$self{"primary_connection"} ||= $self->establish_primary_connection
MC/DC0%0/2
$$self{"primary_connection"} ||= $self->establish_primary_connection
$$self{"primary_connection"}
$self->establish_primary_connection
Truth table0%0/3
A ||= B
A= $$self{"primary_connection"}B= $self->establish_primary_connection
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | } | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | async method establish_primary_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
establish_primary_connection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->add_child( | |||||||||||||||||||||||||||||||||||||||||
| 77 | my $redis = Net::Async::Redis->new( | ||||||||||||||||||||||||||||||||||||||||||
| 78 | $self->Net::Async::Redis::Cluster::node_config, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | host => $self->primary->[0], | ||||||||||||||||||||||||||||||||||||||||||
| 80 | port => $self->primary->[1], | ||||||||||||||||||||||||||||||||||||||||||
| 81 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->{primary_connection} = $redis; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | await $redis->connected; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | await $self->cluster->node_connection_established($self, $redis); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return $redis; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | } | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | method replica_connection ($conn = undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replica_connection: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return $self->{replica_connection} = $conn if $conn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return $self->{replica_connection} ||= $self->establish_replica_connection; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$self{"replica_connection"} ||= $self->establish_replica_connection
MC/DC0%0/2
$$self{"replica_connection"} ||= $self->establish_replica_connection
$$self{"replica_connection"}
$self->establish_replica_connection
Truth table0%0/3
A ||= B
A= $$self{"replica_connection"}B= $self->establish_replica_connection
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | } | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | async method establish_replica_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
establish_replica_connection: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my ($replica) = $self->replicas->[rand $self->replica_count] or die 'no replicas found'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $log->tracef( | |||||||||||||||||||||||||||||||||||||||||
| 97 | 'Will use replica [%s] on %s:%d for primary [%s]', | ||||||||||||||||||||||||||||||||||||||||||
| 98 | $replica->id, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | $replica->host, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | $replica->port, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | $self->id | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $self->add_child( | |||||||||||||||||||||||||||||||||||||||||
| 104 | my $redis = Net::Async::Redis->new( | ||||||||||||||||||||||||||||||||||||||||||
| 105 | $self->Net::Async::Redis::Cluster::node_config, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | host => $replica->host, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | port => $replica->port, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $self->{replica_connection} = $redis; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | await $redis->connected; | |||||||||||||||||||||||||||||||||||||||||
| 112 | # Replica queries are only allowed in "readonly" mode. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # https://redis.io/docs/latest/commands/readonly/ | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | await $redis->readonly; | |||||||||||||||||||||||||||||||||||||||||
| 115 | # We don't forward clientside cache events here, because that would | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # lead to duplicates when both primary and replica are connected. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $redis; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | } | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||||||