| 1 | package Net::Async::Redis::Cluster; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Full::Class qw(:v1), extends => qw(Net::Async::Redis::Commands); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '6.006'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | Net::Async::Redis::Cluster - support for cluster routing | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | use Net::Async::Redis::Cluster; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | $loop->add( | ||||||||||||||||||||||||||||||||||||||||||
| 20 | my $cluster = Net::Async::Redis::Cluster->new( | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | await $cluster->bootstrap( | ||||||||||||||||||||||||||||||||||||||||||
| 24 | host => 'redis.local', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | print "Key: " . await $cluster->get('some_key'); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | Provides access to a Redis cluster. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | Usage is similar to L<Net::Async::Redis> with the addition of a L</bootstrap> | ||||||||||||||||||||||||||||||||||||||||||
| 33 | step to find the initial client nodes: | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | $loop->add( | ||||||||||||||||||||||||||||||||||||||||||
| 36 | my $cluster = Net::Async::Redis::Cluster->new( | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | await $cluster->bootstrap( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | host => 'redis.local', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 42 | print "Key: " . await $cluster->get('some_key'); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Note that this adds some overhead to lookups, so you may be better served | ||||||||||||||||||||||||||||||||||||||||||
| 45 | by options such as the L<https://github.com/twitter/twemproxy|twemproxy> | ||||||||||||||||||||||||||||||||||||||||||
| 46 | proxy routing dæmon, or a service mesh such as L<https://istio.io/|istio>. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | See L</configure> for additional cluster configuration parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | use Future::Utils qw(fmap_void fmap_concat); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | use List::BinarySearch::XS qw(binsearch); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | use List::UtilsBy qw(nsort_by); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | use Digest::CRC qw(crc); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | use Cache::LRU; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | use Net::Async::Redis; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | use Net::Async::Redis::Cluster::Node; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | use Net::Async::Redis::Cluster::Multi; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | '""' => sub { 'NaRedis::Cluster[]' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | use constant MAX_SLOTS => 16384; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | # It's unlikely that MAX_SLOTS would ever need | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # to change, but it's also important that if we do change it, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # our bitwise-AND still makes sense in context, i.e. this | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # must always be a power of 2. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | die 'invalid MAX_SLOTS' unless 1 == unpack( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | "b*" => pack( | ||||||||||||||||||||||||||||||||||||||||||
| 78 | N1 => MAX_SLOTS | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ) =~ tr/1/1/; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | our @CONFIG_KEYS = qw( | |||||||||||||||||||||||||||||||||||||||||
| 84 | auth | ||||||||||||||||||||||||||||||||||||||||||
| 85 | pipeline_depth | ||||||||||||||||||||||||||||||||||||||||||
| 86 | stream_read_len | ||||||||||||||||||||||||||||||||||||||||||
| 87 | stream_write_len | ||||||||||||||||||||||||||||||||||||||||||
| 88 | on_disconnect | ||||||||||||||||||||||||||||||||||||||||||
| 89 | client_name | ||||||||||||||||||||||||||||||||||||||||||
| 90 | opentracing | ||||||||||||||||||||||||||||||||||||||||||
| 91 | protocol | ||||||||||||||||||||||||||||||||||||||||||
| 92 | hashrefs | ||||||||||||||||||||||||||||||||||||||||||
| 93 | client_side_cache_size | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Sometimes we really want a key to be on a specific node. We can achieve this by maintaining | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # a mapping from all known slots to a sample key which would resolve to that slot - this can | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # then be applied in a `{slot}` entry in the full key to ensure that the value will end up | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # in the desired slot. To map to specific nodes, you'd combine this with the ->cluster_getslots | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # information, can use any slot in the range covered by that server. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | our @SLOT_MAP; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub key_for_slot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
key_for_slot: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($slot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | populate_slot_map() unless @SLOT_MAP; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return $SLOT_MAP[$slot]; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub populate_slot_map { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
populate_slot_map: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return if @SLOT_MAP; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $pending = MAX_SLOTS; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $data = '0'; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my @map; | |||||||||||||||||||||||||||||||||||||||||
| 116 | ITEM: | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | while($pending) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $slot = crc($data, 16, 0, 0, 0, 0x1021, 0, 0) & (MAX_SLOTS - 1); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless(defined $map[$slot]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $map[$slot] = $data; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | --$pending; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ++$data; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head2 bootstrap | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | Connects to a Redis server and attempts to discover the cluster node configuration. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | await $cluster->bootstrap( | ||||||||||||||||||||||||||||||||||||||||||
| 138 | host => 'redis-primary.local', | ||||||||||||||||||||||||||||||||||||||||||
| 139 | port => 6379, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | $log->errorf('Unable to bootstrap the Redis cluster: %s', $@); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | async sub bootstrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bootstrap: not called CC=1 SCAR=5.1
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $redis; | |||||||||||||||||||||||||||||||||||||||||
| 150 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | $self->add_child( | ||||||||||||||||||||||||||||||||||||||||||
| 152 | $redis = Net::Async::Redis->new( | ||||||||||||||||||||||||||||||||||||||||||
| 153 | $self->node_config, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | host => $args{host}, | ||||||||||||||||||||||||||||||||||||||||||
| 155 | port => $args{port}, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | await $redis->connect; | |||||||||||||||||||||||||||||||||||||||||
| 159 | await $self->apply_slots_from_instance( | ||||||||||||||||||||||||||||||||||||||||||
| 160 | $redis, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | host => $args{host}, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | port => $args{port} | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } finally { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | $redis->remove_from_parent if $redis; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head2 clientside_cache_events | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | Provides combined stream of clientside-cache events from all known Redis primary nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub clientside_cache_events { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clientside_cache_events: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | $self->{clientside_cache_events} ||= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$self{"clientside_cache_events"} ||= do {
$self->ryu->source
}
MC/DC0%0/2
$$self{"clientside_cache_events"} ||= do {
$self->ryu->source
}
$$self{"clientside_cache_events"}
do {
$self->ryu->source
}
Truth table0%0/3
A ||= B
A= $$self{"clientside_cache_events"}B= do {
$self->ryu->source
}
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $self->ryu->source; | |||||||||||||||||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head2 watch_keyspace | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | L<Net::Async::Redis/watch_keyspace> support for gathering notifications | ||||||||||||||||||||||||||||||||||||||||||
| 185 | from all known nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | async sub watch_keyspace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
watch_keyspace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my ($self, $pattern) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | my @sub = await fmap_concat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $_->primary_connection->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | shift->watch_keyspace($pattern); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | } foreach => [$self->{nodes}->@*], concurrent => 4; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | my $combined = Net::Async::Redis::Subscription->new( | |||||||||||||||||||||||||||||||||||||||||
| 198 | redis => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | channel => $pattern | ||||||||||||||||||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $combined->events->emit_from(@sub); | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | return $combined; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =head2 client_setname | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Apply client name to all nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | Note that this only updates the current nodes, it will not | ||||||||||||||||||||||||||||||||||||||||||
| 211 | apply to new nodes. Use the L<Net::Async::Redis/client_name> | ||||||||||||||||||||||||||||||||||||||||||
| 212 | constructor/L</configure> parameter to apply to all nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | async sub client_setname { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_setname: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | await fmap_concat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | $_->primary_connection->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | shift->client_setname($name); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | } foreach => [$self->{nodes}->@*], concurrent => 4; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return 'OK'; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =head2 multi | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | A C<MULTI> transaction on a Redis cluster works slightly differently from | ||||||||||||||||||||||||||||||||||||||||||
| 229 | single-node setups. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =item * issue C<MULTI> on all nodes | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | =item * execute the commands, distributing across nodes as usual | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | =item * issue C<EXEC> or C<DISCARD> as appropriate | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | Note that the coderef is called only once, even if there are multiple nodes involved. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | Currently, there's no optimisation for limiting C<MULTI> to the nodes | ||||||||||||||||||||||||||||||||||||||||||
| 244 | participating in the transaction. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | async method multi ($code) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
multi: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | die 'Need a coderef' unless $code and reftype($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code and builtin::reftype($code) eq "CODE"
MC/DC0%0/2
$code and builtin::reftype($code) eq "CODE"
$code
builtin::reftype($code) eq "CODE"
Truth table0%0/3
A and B
A= $codeB= builtin::reftype($code) eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # Start a transaction on all primary nodes | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my $cmd = Net::Async::Redis::Commands->can('multi'); | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $node_count = 0 + $self->{nodes}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | my %multi_for_node = await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my $redis = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $multi = Net::Async::Redis::Multi->new( | |||||||||||||||||||||||||||||||||||||||||
| 257 | redis => $redis, | ||||||||||||||||||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | await $redis->$cmd; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $node->id => $multi | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | }, foreach => [$self->{nodes}->@*], concurrent => $node_count); | |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # At this point we know all nodes are safely in MULTI mode, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # so we can start sending requests | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my $multi = Net::Async::Redis::Cluster::Multi->new( | |||||||||||||||||||||||||||||||||||||||||
| 266 | redis => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | multi => \%multi_for_node, | ||||||||||||||||||||||||||||||||||||||||||
| 268 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | $multi->$code; | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | await fmap_void(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | shift->exec(sub { }) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | }, foreach => [ values %multi_for_node ], concurrent => $node_count); | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | async sub discard { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
discard: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | my $cmd = Net::Async::Redis::Commands->can('discard'); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | await fmap_concat(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $node->primary_connection->then(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | dynamically $self->{_is_multi} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | shift->$cmd | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | }, foreach => [$self->{nodes}->@*], concurrent => 4); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | (shift @{$self->{pending_multi}})->done; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | async method exec (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
exec: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | $log->tracef('Calling ->exec, with multi = %s and queue %s', '' . $self->{_is_multi}, '' . $self->{multi_queue}); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | my (@res) = await fmap_concat(async sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $log->tracef('Per node ->exec, with multi = %s and queue %s', '' . $self->{_is_multi}, '' . $self->{multi_queue}); | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return await $conn->exec; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | }, foreach => [$self->{nodes}->@*], concurrent => 4); | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | return [ map { $_->@* } grep { $_ } @res ]; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | =head1 METHODS - All nodes | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | These methods operate on all nodes at once, and return | ||||||||||||||||||||||||||||||||||||||||||
| 306 | the data in hashref mapping node ID to the response. | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | async method bgrewriteaof { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bgrewriteaof: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 312 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 315 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | $node->id => await $conn->bgrewriteaof | |||||||||||||||||||||||||||||||||||||||||
| 317 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 319 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | async method bgsave { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bgsave: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 324 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 327 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | $node->id => await $conn->bgsave | |||||||||||||||||||||||||||||||||||||||||
| 329 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 331 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | async method save { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 336 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 339 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | $node->id => await $conn->save | |||||||||||||||||||||||||||||||||||||||||
| 341 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 343 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | async method role { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
role: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 348 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 351 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | $node->id => await $conn->role | |||||||||||||||||||||||||||||||||||||||||
| 353 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 355 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | async method shutdown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 360 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 363 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | $node->id => await $conn->shutdown | |||||||||||||||||||||||||||||||||||||||||
| 365 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 367 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 368 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | async method info (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 372 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 375 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | $node->id => await $conn->info(@args) | |||||||||||||||||||||||||||||||||||||||||
| 377 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 379 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | async method time (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 384 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 387 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | $node->id => await $conn->time(@args) | |||||||||||||||||||||||||||||||||||||||||
| 389 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 391 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | async method slowlog_reset (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slowlog_reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 396 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 399 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | $node->id => await $conn->slowlog_reset(@args) | |||||||||||||||||||||||||||||||||||||||||
| 401 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 403 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | ▶ | async method slowlog_len (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slowlog_len: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 408 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 411 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | $node->id => await $conn->slowlog_len(@args) | |||||||||||||||||||||||||||||||||||||||||
| 413 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 415 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | async method slowlog_get (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slowlog_get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | my @nodes = $self->node_list; | |||||||||||||||||||||||||||||||||||||||||
| 420 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | ▶ | await fmap_concat(async sub ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | my $conn = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 423 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | $node->id => await $conn->slowlog_get(@args) | |||||||||||||||||||||||||||||||||||||||||
| 425 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | }, foreach => [ @nodes ], concurrent => 0 + @nodes) | |||||||||||||||||||||||||||||||||||||||||
| 427 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | =head1 METHODS - Any node | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | These methods operate pick a random node to operate on, | ||||||||||||||||||||||||||||||||||||||||||
| 433 | returning the data as if this was a regular L<Net::Async::Redis> | ||||||||||||||||||||||||||||||||||||||||||
| 434 | instance. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | =head1 METHODS - Internal | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | async sub node_connection_established { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
node_connection_established: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | my ($self, $node, $redis) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | ▶ | $self->clientside_cache_events->emit_from($redis->clientside_cache_events) if $redis->is_client_side_cache_enabled; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | sub future { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
future: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | return $self->loop->new_future(@_); | |||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | ▶ | sub node_config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
node_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | return %{$self}{grep exists $self->{$_}, @CONFIG_KEYS}; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =head2 configure | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | Takes the same parameters as L<Net::Async::Redis/configure>, with the addition of: | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | =item * C<use_read_replica> - direct read commands to replicas, instead of the primary nodes | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 1 | ▶ | sub configure { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
configure: called CC=10 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 473 | 1 | for (@CONFIG_KEYS) { | |||||||||||||||||||||||||||||||||||||||||
| 474 | 10 | ▶ | $self->{$_} = delete $args{$_} if exists $args{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | ▶ | if(exists $args{read_from_replica}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | warn "`read_from_replica` parameter passed - please update your code to use `use_read_replica` instead!"; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | $self->{use_read_replica} = delete $args{read_from_replica}; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | 1 | ▶ | $self->{use_read_replica} = delete $args{use_read_replica} if exists $args{use_read_replica}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 1 | ▶ | die 'invalid protocol requested: ' . $self->{protocol} if defined $self->{protocol} and not $self->{protocol} =~ /^resp[23]$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $$self{"protocol"} and not $$self{"protocol"} =~ /^resp[23]$/u
MC/DC0%0/2
defined $$self{"protocol"} and not $$self{"protocol"} =~ /^resp[23]$/u
defined $$self{"protocol"}
not $$self{"protocol"} =~ /^resp[23]$/u
Truth table33%1/3
A and not(B)
A= defined $$self{"protocol"}B= not $$self{"protocol"} =~ /^resp[23]$/u
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 1 | ▶ | die 'hashref support requires RESP3 (Redis version 6+)' if defined $self->{protocol} and $self->{protocol} eq 'resp2' and $self->{hashrefs}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2" and $$self{"hashrefs"}
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2"
MC/DC0%0/3
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2" and $$self{"hashrefs"}
defined $$self{"protocol"}
$$self{"protocol"} eq "resp2"
$$self{"hashrefs"}
Truth table25%1/4
A and B and C
A= defined $$self{"protocol"}B= $$self{"protocol"} eq "resp2"C= $$self{"hashrefs"}
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | 1 | return $self->next::method(%args); | |||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 1 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 489 | 1 | $self->{cache} = Cache::LRU->new( | |||||||||||||||||||||||||||||||||||||||||
| 490 | size => 10_000 | ||||||||||||||||||||||||||||||||||||||||||
| 491 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | =head2 hash_slot_for_key | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | Calculates the CRC16 hash slot for the given key. | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | Note that keys are expected as bytestrings, if you have a Unicode string | ||||||||||||||||||||||||||||||||||||||||||
| 499 | you'd likely want to convert to UTF-8 first. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | 3 | ▶ | sub hash_slot_for_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash_slot_for_key: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 3 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 3 | ▶ | return $self->{cache}->get($key) // do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$$self{"cache"}->get($key) // do {
my($hash_string) = $key =~ /^[^\{]*\{([^\}]+)\}/u;
$hash_string //= $key;
my $slot = crc($hash_string, 16, 0, 0, 0, 4129, 0, 0) & 16383;
$$self{"cache"}->set($key, $slot);
$slot
}
MC/DC0%0/2
$$self{"cache"}->get($key) // do {
my($hash_string) = $key =~ /^[^\{]*\{([^\}]+)\}/u;
$hash_string //= $key;
my $slot = crc($hash_string, 16, 0, 0, 0, 4129, 0, 0) & 16383;
$$self{"cache"}->set($key, $slot);
$slot
}
$$self{"cache"}->get($key)
do {
my($hash_string) = $key =~ /^[^\{]*\{([^\}]+)\}/u;
$hash_string //= $key;
my $slot = crc($hash_string, 16, 0, 0, 0, 4129, 0, 0) & 16383;
$$self{"cache"}->set($key, $slot);
$slot
}
Truth table33%1/3
A // B
A= $$self{"cache"}->get($key)B= do {
my($hash_string) = $key =~ /^[^\{]*\{([^\}]+)\}/u;
$hash_string //= $key;
my $slot = crc($hash_string, 16, 0, 0, 0, 4129, 0, 0) & 16383;
$$self{"cache"}->set($key, $slot);
$slot
}
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | # Extract out the first non-zero-length substring between the first {} character pair, | ||||||||||||||||||||||||||||||||||||||||||
| 507 | # if any... | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 3 | my ($hash_string) = $key =~ /^[^\{]*\{([^\}]+)\}/; | |||||||||||||||||||||||||||||||||||||||||
| 509 | # ... and if we don't have any matching substrings, we just use the full key | ||||||||||||||||||||||||||||||||||||||||||
| 510 | 3 | ▶ | $hash_string //= $key; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$hash_string //= $key
MC/DC0%0/2
$hash_string //= $key
$hash_string
$key
Truth table66%2/3
A //= B
A= $hash_stringB= $key
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | # see Digest::CRC docs and the XMODEM protocol for more details, but essentially: | ||||||||||||||||||||||||||||||||||||||||||
| 512 | # input, width=16, init=0, xor constant for output = 0, reflect output = 0, polynomial = 0x1021, | ||||||||||||||||||||||||||||||||||||||||||
| 513 | # reflect input = 0, continuation = 0 | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 3 | my $slot = crc($hash_string, 16, 0, 0, 0, 0x1021, 0, 0) & (MAX_SLOTS - 1); | |||||||||||||||||||||||||||||||||||||||||
| 515 | 3 | $self->{cache}->set($key => $slot); | |||||||||||||||||||||||||||||||||||||||||
| 516 | 3 | $slot; | |||||||||||||||||||||||||||||||||||||||||
| 517 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | =head2 replace_nodes | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | Swap the existing node configuration out for a new arrayref of nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | ▶ | sub replace_nodes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace_nodes: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | my ($self, $nodes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | delete $self->{slot_cache}; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | $_->remove_from_parent for splice $self->{nodes}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | $self->add_child($_) for $nodes->@*; | |||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | my $primary_by_hostport = { }; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | for my $node ($nodes->@*) { | |||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | ▶ | if(my $conn = $primary_by_hostport->{$node->host_port}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | $node->primary_connection($primary_by_hostport->{$node->host_port}); | |||||||||||||||||||||||||||||||||||||||||
| 535 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | $primary_by_hostport->{$node->host_port} = $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | $self->{nodes} = $nodes; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | $self | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | =head2 node_list | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | Returns a list of the currently-configured nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | ▶ | sub node_list { shift->{nodes}->@* } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
node_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | ▶ | sub slot_cache { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slot_cache: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | ▶ | $self->{slot_cache} //= [ (undef) x MAX_SLOTS ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$self{"slot_cache"} //= [(undef) x 16384]
MC/DC0%0/1
$$self{"slot_cache"} //= [(undef) x 16384]
$$self{"slot_cache"}
Truth table0%0/2
A
A= $$self{"slot_cache"}
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | =head2 node_for_slot | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | Returns the appropriate L<Net::Async::Redis::Cluster::Node|node> for the given hash key (slot). | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | ▶ | sub node_for_slot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
node_for_slot: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | my ($self, $slot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 564 | 0 | ▶ | return $self->slot_cache->[$slot & (MAX_SLOTS - 1)] //= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->slot_cache->[$slot & 16383] //= do {
my(@nodes) = @{$$self{"nodes"}};
my $idx = &binsearch(sub {
$a < $b->start ? -1 : ($a > $b->end ? 1 : 0);
}
, $slot, \@nodes);
$nodes[$idx]
}
MC/DC0%0/2
$self->slot_cache->[$slot & 16383] //= do {
my(@nodes) = @{$$self{"nodes"}};
my $idx = &binsearch(sub {
$a < $b->start ? -1 : ($a > $b->end ? 1 : 0);
}
, $slot, \@nodes);
$nodes[$idx]
}
$self->slot_cache->[$slot & 16383]
do {
my(@nodes) = @{$$self{"nodes"}};
my $idx = &binsearch(sub {
$a < $b->start ? -1 : ($a > $b->end ? 1 : 0);
}
, $slot, \@nodes);
$nodes[$idx]
}
Truth table0%0/3
A //= B
A= $self->slot_cache->[$slot & 16383]B= do {
my(@nodes) = @{$$self{"nodes"}};
my $idx = &binsearch(sub {
$a < $b->start ? -1 : ($a > $b->end ? 1 : 0);
}
, $slot, \@nodes);
$nodes[$idx]
}
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | my @nodes = $self->{nodes}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | ▶ | my $idx = binsearch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | ▶ | $a < $b->start | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 568 | ? -1 | ||||||||||||||||||||||||||||||||||||||||||
| 569 | : $a > $b->end | ||||||||||||||||||||||||||||||||||||||||||
| 570 | ? 1 | ||||||||||||||||||||||||||||||||||||||||||
| 571 | :0 | ||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | } $slot, @nodes; | |||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | $nodes[$idx] | |||||||||||||||||||||||||||||||||||||||||
| 574 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | ▶ | async sub connection_for_slot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connection_for_slot: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | my ($self, $slot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 579 | 0 | ▶ | my $node = $self->node_for_slot($slot) or die 'no node found for slot'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | return await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 581 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | ▶ | sub node_by_host_port { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
node_by_host_port: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | my ($self, $host, $port) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | my $host_port = join ':', $host, $port; | |||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | my ($node) = first { $host_port eq $_->host_port } $self->{nodes}->@*; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | return $node; | |||||||||||||||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | =head2 register_moved_slot | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | When we get MOVED error we will use this | ||||||||||||||||||||||||||||||||||||||||||
| 593 | sub to rebuild the slot cache | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | async sub register_moved_slot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_moved_slot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | my ($self, $slot, $host_port) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | my ($host, $port) = split /:/, $host_port; | |||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | my $node = $self->node_by_host_port($host, $port); | |||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | ▶ | unless($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | $log->tracef("Failed to find node %s:%s in the original node list", $host, $port); | |||||||||||||||||||||||||||||||||||||||||
| 603 | # Has a replica become a primary? Let's look for a valid node | ||||||||||||||||||||||||||||||||||||||||||
| 604 | 0 | ▶ | await fmap_void(async sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 606 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | my $valid_connection = await $node->primary_connection; | |||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | die 'Cluster status changed and cannot find a valid information source' unless $valid_connection; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | # We'll let *all* our nodes try to tell us about slots, the operation | ||||||||||||||||||||||||||||||||||||||||||
| 610 | # should be atomic so whichever one(s) succeed are hopefully consistent | ||||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | await $self->apply_slots_from_instance( | |||||||||||||||||||||||||||||||||||||||||
| 612 | $valid_connection, | ||||||||||||||||||||||||||||||||||||||||||
| 613 | host => $host, | ||||||||||||||||||||||||||||||||||||||||||
| 614 | port => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 615 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | } catch { | |||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | $log->tracef("Node at %s was invalid", $node->primary); | |||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | }, concurrent => 4, foreach => [ $self->{nodes}->@* ]); | |||||||||||||||||||||||||||||||||||||||||
| 620 | # Try again else propgate failure | ||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | ▶ | $node = $self->node_by_host_port($host, $port) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | or die "Slot $slot has been moved to unknown node"; | ||||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | $self->slot_cache->[$slot & (MAX_SLOTS - 1)] = $node; | |||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | return $node; | |||||||||||||||||||||||||||||||||||||||||
| 626 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | =head2 apply_slots_from_instance | ||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | Connect to a random instance in the cluster | ||||||||||||||||||||||||||||||||||||||||||
| 631 | and execute CLUSTER SLOTS to get information | ||||||||||||||||||||||||||||||||||||||||||
| 632 | about the slots and their distribution. | ||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||
| 634 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | 0 | ▶ | async sub apply_slots_from_instance { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_slots_from_instance: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | my ($self, $redis, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | my ($slots) = await $redis->cluster_slots; | |||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | $log->tracef('Have %d slots', 0 + @$slots); | |||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 0 | my @nodes; | |||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | ▶ | for my $slot_data (nsort_by { $_->[0] } $slots->@*) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | for my $primary ($slot_data->[2]) { | |||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | ▶ | $primary->[0] = $args{host} unless length $primary->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | ▶ | $primary->[1] = $args{port} unless length $primary->[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | my $node = $self->instantiate_node($slot_data); | |||||||||||||||||||||||||||||||||||||||||
| 648 | 0 | $log->tracef( | |||||||||||||||||||||||||||||||||||||||||
| 649 | 'Node %s (%s) handles slots %d-%d and has %d replica(s) - %s', | ||||||||||||||||||||||||||||||||||||||||||
| 650 | $node->id, | ||||||||||||||||||||||||||||||||||||||||||
| 651 | $node->primary, | ||||||||||||||||||||||||||||||||||||||||||
| 652 | $node->start, | ||||||||||||||||||||||||||||||||||||||||||
| 653 | $node->end, | ||||||||||||||||||||||||||||||||||||||||||
| 654 | $node->replica_count, | ||||||||||||||||||||||||||||||||||||||||||
| 655 | $node | ||||||||||||||||||||||||||||||||||||||||||
| 656 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | 0 | push @nodes, $node; | |||||||||||||||||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | $self->replace_nodes(\@nodes); | |||||||||||||||||||||||||||||||||||||||||
| 662 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | ▶ | sub instantiate_node { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate_node: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | my ($self, $slot_data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 666 | 0 | return Net::Async::Redis::Cluster::Node->from_arrayref( | |||||||||||||||||||||||||||||||||||||||||
| 667 | $slot_data, | ||||||||||||||||||||||||||||||||||||||||||
| 668 | cluster => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 669 | $self->node_config | ||||||||||||||||||||||||||||||||||||||||||
| 670 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||
| 673 | =head2 execute_command | ||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||
| 675 | Lookup the correct node for the key then execute the command on that node, | ||||||||||||||||||||||||||||||||||||||||||
| 676 | if there is a mismatch between our slot hashes and Redis's hashes | ||||||||||||||||||||||||||||||||||||||||||
| 677 | we will attempt to rebuild the slot hashes and try again | ||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | ▶ | sub execute_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute_command: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | 0 | my ($self, @cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | $log->tracef('Will execute %s on cluster', join(' ', @cmd)); | |||||||||||||||||||||||||||||||||||||||||
| 684 | 0 | return $self->find_node_and_execute_command(@cmd)->retain; | |||||||||||||||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||
| 687 | 0 | ▶ | async sub execute_command_on_node ($self, %args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute_command_on_node: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | ▶ | my $node = delete $args{node} or die 'need a node'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 689 | 0 | ▶ | my $command = delete $args{command} or die 'need a command'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | $log->tracef('Will execute %s on cluster', join ' ', $command->@*); | |||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||
| 692 | # Some commands have modifiers around them for RESP2/3 transparent support | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 0 | my ($method, @args) = $command->@*; | |||||||||||||||||||||||||||||||||||||||||
| 694 | 0 | $method = lc $method; | |||||||||||||||||||||||||||||||||||||||||
| 695 | ATTEMPT: | ||||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | for my $attempt (1..5) { | |||||||||||||||||||||||||||||||||||||||||
| 697 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 698 | my $redis = await ( | ||||||||||||||||||||||||||||||||||||||||||
| 699 | $self->{use_read_replica} | ||||||||||||||||||||||||||||||||||||||||||
| 700 | && $node->replica_count | ||||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | ▶ | && Net::Async::Redis->extract_spec_for_command($command)->{acl_cat}{'@read'} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$$self{"use_read_replica"} and $node->replica_count and "Net::Async::Redis"->extract_spec_for_command($command)->{"acl_cat"}{"\@read"}
$$self{"use_read_replica"} and $node->replica_count
MC/DC0%0/3
$$self{"use_read_replica"} and $node->replica_count and "Net::Async::Redis"->extract_spec_for_command($command)->{"acl_cat"}{"\@read"}
$$self{"use_read_replica"}
$node->replica_count
"Net::Async::Redis"->extract_spec_for_command($command)->{"acl_cat"}{"\@read"}
Truth table0%0/4
A and B and C
A= $$self{"use_read_replica"}B= $node->replica_countC= "Net::Async::Redis"->extract_spec_for_command($command)->{"acl_cat"}{"\@read"}
| |||||||||||||||||||||||||||||||||||||||||||
| 702 | ? $node->replica_connection | ||||||||||||||||||||||||||||||||||||||||||
| 703 | : $node->primary_connection | ||||||||||||||||||||||||||||||||||||||||||
| 704 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 705 | 0 | return await $redis->$method(@args); | |||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 707 | # Only catch cluster-migration responses - everything else needs to be reported upstream | ||||||||||||||||||||||||||||||||||||||||||
| 708 | 0 | ▶ | die $e unless $e =~ /^(?:MOVED|ASK)\s+/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||
| 710 | 0 | my ($moved, $slot, $host_port) = split ' ', $e; | |||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | ▶ | if($moved eq 'ASK') { # Apply this temporarily - don't record the new slot, it's probably migrating | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 | my ($host, $port) = split /:/, $host_port; | |||||||||||||||||||||||||||||||||||||||||
| 713 | 0 | $node = $self->node_by_host_port($host, $port); | |||||||||||||||||||||||||||||||||||||||||
| 714 | 0 | next ATTEMPT; | |||||||||||||||||||||||||||||||||||||||||
| 715 | } else { # the princess is in a different castle | ||||||||||||||||||||||||||||||||||||||||||
| 716 | 0 | await $self->register_moved_slot($slot => $host_port); | |||||||||||||||||||||||||||||||||||||||||
| 717 | 0 | $node = await $self->find_node($command->@*); | |||||||||||||||||||||||||||||||||||||||||
| 718 | 0 | next ATTEMPT; | |||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||
| 722 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||
| 724 | 0 | ▶ | async sub find_node_and_execute_command ($self, @cmd) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_node_and_execute_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 725 | 0 | my $node = await $self->find_node(@cmd); | |||||||||||||||||||||||||||||||||||||||||
| 726 | 0 | return await $self->execute_command_on_node( | |||||||||||||||||||||||||||||||||||||||||
| 727 | node => $node, | ||||||||||||||||||||||||||||||||||||||||||
| 728 | command => \@cmd | ||||||||||||||||||||||||||||||||||||||||||
| 729 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 730 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||
| 732 | 0 | ▶ | async sub find_node { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_node: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | my ($self, @cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 734 | 0 | my @keys = Net::Async::Redis->extract_keys_for_command(\@cmd); | |||||||||||||||||||||||||||||||||||||||||
| 735 | 0 | ▶ | unless(@keys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | 0 | return $self->{nodes}[rand($self->{nodes}->@*)]; | |||||||||||||||||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | my @slots = map { $self->hash_slot_for_key($_) } @keys; | |||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | my %slots = map { $_ => 1 } @slots; | |||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | ▶ | die 'Multiple slots for command' if keys(%slots) > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 742 | 0 | ▶ | my $slot = $cmd[0] =~ /^p?(?:un)?subscribe/i ? 0 : shift(@slots); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | $log->tracef('Look up hash slot for %s - %d', \@keys, $slot); | |||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | return $self->node_for_slot($slot); | |||||||||||||||||||||||||||||||||||||||||
| 745 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | =head2 ryu | ||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||
| 749 | A L<Ryu::Async> instance for source/sink creation. | ||||||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||||||
| 751 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | ▶ | method ryu { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ryu: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | ▶ | $self->{ryu} ||= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$self{"ryu"} ||= do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu
}
MC/DC0%0/2
$$self{"ryu"} ||= do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu
}
$$self{"ryu"}
do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu
}
Truth table0%0/3
A ||= B
A= $$self{"ryu"}B= do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu
}
| |||||||||||||||||||||||||||||||||||||||||||
| 755 | 0 | $self->add_child( | |||||||||||||||||||||||||||||||||||||||||
| 756 | my $ryu = Ryu::Async->new | ||||||||||||||||||||||||||||||||||||||||||
| 757 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 758 | 0 | $ryu | |||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||
| 760 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | Tom Molesworth C<< <TEAM@cpan.org> >> plus contributors as mentioned in | ||||||||||||||||||||||||||||||||||||||||||
| 767 | L<Net::Async::Redis/CONTRIBUTORS>. | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | Copyright Tom Molesworth and others 2015-2024. | ||||||||||||||||||||||||||||||||||||||||||
| 772 | Licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||