| 1 | package Beekeeper::Worker::Extension::SharedCache; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT = qw( shared_cache ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub shared_cache { | ||||||||||||||||||||||||||
|
Subroutine
shared_cache: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | Beekeeper::Worker::Extension::SharedCache::Cache->new( | |||||||||||||||||||||||||||
| 17 | worker => $self, | ||||||||||||||||||||||||||||
| 18 | @_ | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package | ||||||||||||||||||||||||||||
| 23 | Beekeeper::Worker::Extension::SharedCache::Cache; # hide from PAUSE | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Beekeeper::Worker ':log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use constant SYNC_REQUEST_TIMEOUT => 30; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Show errors from perspective of caller | ||||||||||||||||||||||||||||
| 35 | 1 | $Carp::Internal{(__PACKAGE__)}++; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 39 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $worker = $args{'worker'}; | |||||||||||||||||||||||||||
| 42 | 0 | my $id = $args{'id'}; | |||||||||||||||||||||||||||
| 43 | 0 | my $uid = "$$-" . int(rand(90000000)+10000000); | |||||||||||||||||||||||||||
| 44 | 0 | my $pool_id = $worker->{_WORKER}->{pool_id}; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $self = { | ||||||||||||||||||||||||||||
| 47 | id => $id, | ||||||||||||||||||||||||||||
| 48 | uid => $uid, | ||||||||||||||||||||||||||||
| 49 | pool_id => $pool_id, | ||||||||||||||||||||||||||||
| 50 | resolver => $args{'resolver'}, | ||||||||||||||||||||||||||||
| 51 | on_update => $args{'on_update'}, | ||||||||||||||||||||||||||||
| 52 | persist => $args{'persist'}, | ||||||||||||||||||||||||||||
| 53 | max_age => $args{'max_age'}, | ||||||||||||||||||||||||||||
| 54 | 0 | refresh => $args{'refresh'}, | |||||||||||||||||||||||||||
| 55 | synced => 0, | ||||||||||||||||||||||||||||
| 56 | data => {}, | ||||||||||||||||||||||||||||
| 57 | vers => {}, | ||||||||||||||||||||||||||||
| 58 | time => {}, | ||||||||||||||||||||||||||||
| 59 | _BUS => undef, | ||||||||||||||||||||||||||||
| 60 | _BUS_GROUP=> undef, | ||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $self->_load_state if $self->{persist}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | $self->_connect_to_all_brokers($worker); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my $Self = $self; | |||||||||||||||||||||||||||
| 70 | 0 | weaken $Self; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | AnyEvent->now_update; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($self->{max_age}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | $self->{gc_timer} = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 77 | after => $self->{max_age} * rand() + 60, | ||||||||||||||||||||||||||||
| 78 | interval => $self->{max_age}, | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | cb => sub { $Self->_gc }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 0 | ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($self->{refresh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | $self->{refresh_timer} = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 86 | after => $self->{refresh} * rand() + 60, | ||||||||||||||||||||||||||||
| 87 | interval => $self->{refresh}, | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | cb => sub { $Self->_send_sync_request }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 89 | 0 | ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Ping backend brokers to avoid disconnections due to inactivity | ||||||||||||||||||||||||||||
| 93 | $self->{ping_timer} = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 94 | after => 60 * rand(), | ||||||||||||||||||||||||||||
| 95 | interval => 60, | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | cb => sub { $Self->_ping_backend_brokers }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | return $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _connect_to_all_brokers { | ||||||||||||||||||||||||||
|
Subroutine
_connect_to_all_brokers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($self, $worker) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #TODO: using multiple shared_cache from the same worker will cause multiple bus connections | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $worker_bus = $worker->{_BUS}; | |||||||||||||||||||||||||||
| 109 | 0 | my $group_config = Beekeeper::Config->get_bus_group_config( bus_id => $worker_bus->bus_id ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my $bus_group = $self->{_BUS_GROUP} = []; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | foreach my $config (@$group_config) { | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $bus_id = $config->{'bus_id'}; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($bus_id eq $worker_bus->bus_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | # Already connected to our own bus | ||||||||||||||||||||||||||||
| 119 | 0 | $self->_setup_sync_listeners($worker_bus); | |||||||||||||||||||||||||||
| 120 | 0 | $self->_send_sync_request($worker_bus); | |||||||||||||||||||||||||||
| 121 | 0 | $self->{_BUS} = $worker_bus; | |||||||||||||||||||||||||||
| 122 | 0 | weaken $self->{_BUS}; | |||||||||||||||||||||||||||
| 123 | 0 | next; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $bus; $bus = Beekeeper::MQTT->new( | |||||||||||||||||||||||||||
| 127 | %$config, | ||||||||||||||||||||||||||||
| 128 | bus_id => $bus_id, | ||||||||||||||||||||||||||||
| 129 | timeout => 300, | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 131 | # Reconnect | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $errmsg = $_[0] || ""; $errmsg =~ s/\s+/ /sg; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0] || ''
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||||||||||
| 133 | 0 | log_error "Connection to $bus_id failed: $errmsg"; | |||||||||||||||||||||||||||
| 134 | 0 | my $delay = $self->{connect_err}->{$bus_id}++; | |||||||||||||||||||||||||||
| 135 | $self->{reconnect_tmr}->{$bus_id} = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 136 | after => ($delay < 10 ? $delay * 3 : 30), | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | $bus->connect( | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | on_connack => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 140 | 0 | log_warn "Reconnected to $bus_id"; | |||||||||||||||||||||||||||
| 141 | 0 | $self->_setup_sync_listeners($bus); | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | $self->_accept_sync_requests($bus) if $self->{synced}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | ); | |||||||||||||||||||||||||||
| 145 | }, | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||
| 148 | 0 | ); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | push @$bus_group, $bus; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | $bus->connect( | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | on_connack => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 154 | # Setup | ||||||||||||||||||||||||||||
| 155 | 0 | log_debug "Connected to $bus_id"; | |||||||||||||||||||||||||||
| 156 | 0 | $self->_setup_sync_listeners($bus); | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | $self->_accept_sync_requests($bus) if $self->{synced}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | 0 | ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub _setup_sync_listeners { | ||||||||||||||||||||||||||
|
Subroutine
_setup_sync_listeners: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | 0 | my ($self, $bus) = @_; | |||||||||||||||||||||||||||
| 165 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $cache_id = $self->{id}; | |||||||||||||||||||||||||||
| 168 | 0 | my $uid = $self->{uid}; | |||||||||||||||||||||||||||
| 169 | 0 | my $local_bus = $bus->{bus_role}; | |||||||||||||||||||||||||||
| 170 | 0 | my $client_id = $bus->{client_id}; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $topic = "msg/$local_bus/_sync/$cache_id/set"; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | $bus->subscribe( | ||||||||||||||||||||||||||||
| 175 | topic => $topic, | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | on_publish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 177 | # my ($payload_ref, $mqtt_properties) = @_; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | my $entry = decode_json( ${$_[0]} ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | $self->_merge($entry); | |||||||||||||||||||||||||||
| 182 | }, | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | on_suback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 184 | 0 | my ($success) = @_; | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | log_error "Could not subscribe to topic '$topic'" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | ); | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | my $reply_topic = "priv/$client_id/sync-$cache_id"; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | $bus->subscribe( | ||||||||||||||||||||||||||||
| 192 | topic => $reply_topic, | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | on_publish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 194 | 0 | my ($payload_ref, $mqtt_properties) = @_; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | my $dump = decode_json($$payload_ref); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | $self->_merge_dump($dump); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | $self->_sync_completed(1); | |||||||||||||||||||||||||||
| 201 | }, | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | on_suback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 203 | 0 | my ($success) = @_; | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | log_error "Could not subscribe to reply topic '$reply_topic'" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 0 | ); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub _send_sync_request { | ||||||||||||||||||||||||||
|
Subroutine
_send_sync_request: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 210 | 0 | my ($self, $bus) = @_; | |||||||||||||||||||||||||||
| 211 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Do not send more than one sync request at the time | ||||||||||||||||||||||||||||
| 214 | 0 | ▶ | return if $self->{_sync_timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my $cache_id = $self->{id}; | |||||||||||||||||||||||||||
| 217 | 0 | my $uid = $self->{uid}; | |||||||||||||||||||||||||||
| 218 | 0 | my $local_bus = $bus->{bus_role}; | |||||||||||||||||||||||||||
| 219 | 0 | my $client_id = $bus->{client_id}; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | $bus->publish( | |||||||||||||||||||||||||||
| 222 | topic => "req/$local_bus/_sync/$cache_id/dump", | ||||||||||||||||||||||||||||
| 223 | response_topic => "priv/$client_id/sync-$cache_id", | ||||||||||||||||||||||||||||
| 224 | ); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # Ensure that timeout is set properly when the event loop was blocked | ||||||||||||||||||||||||||||
| 227 | 0 | AnyEvent->now_update; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # When a fresh pool is started there is no master to reply sync requests | ||||||||||||||||||||||||||||
| 230 | $self->{_sync_timeout} = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 231 | after => SYNC_REQUEST_TIMEOUT, | ||||||||||||||||||||||||||||
| 232 | 0 | ▶ | cb => sub { $self->_sync_completed(0) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 233 | 0 | ); | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub _sync_completed { | ||||||||||||||||||||||||||
|
Subroutine
_sync_completed: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 237 | 0 | my ($self, $success) = @_; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | delete $self->{_sync_timeout}; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return if $self->{synced}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # BUG: When a fresh pool is started there is no master to reply sync requests. | ||||||||||||||||||||||||||||
| 244 | # When two fresh pools are started at t0 and t1 time, and (t1 - t0) < SYNC_REQUEST_TIMEOUT, | ||||||||||||||||||||||||||||
| 245 | # cache updates in the t0-t1 range are not properly synced in the pool wich was started later | ||||||||||||||||||||||||||||
| 246 | 0 | ▶ | log_debug( "Shared cache '$self->{id}': " . ($success ? "Sync completed" : "Acting as master")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | $self->{synced} = 1; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | foreach my $bus ( @{$self->{_BUS_GROUP}} ) { | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # Connections to other buses could have failed or be in progress | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | next unless $bus->{is_connected}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | $self->_accept_sync_requests($bus); | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | sub _accept_sync_requests { | ||||||||||||||||||||||||||
|
Subroutine
_accept_sync_requests: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 260 | 0 | my ($self, $bus) = @_; | |||||||||||||||||||||||||||
| 261 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 262 | 0 | weaken $bus; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my $cache_id = $self->{id}; | |||||||||||||||||||||||||||
| 265 | 0 | my $uid = $self->{uid}; | |||||||||||||||||||||||||||
| 266 | 0 | my $bus_id = $bus->{bus_id}; | |||||||||||||||||||||||||||
| 267 | 0 | my $local_bus = $bus->{bus_role}; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | log_debug "Shared cache '$self->{id}': Accepting sync requests from $local_bus"; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | my $topic = "\$share/BKPR/req/$local_bus/_sync/$cache_id/dump"; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | $bus->subscribe( | ||||||||||||||||||||||||||||
| 274 | topic => $topic, | ||||||||||||||||||||||||||||
| 275 | 0 | ▶ | on_publish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 276 | 0 | my ($payload_ref, $mqtt_properties) = @_; | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | my $dump = encode_json( $self->dump ); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | $bus->publish( | ||||||||||||||||||||||||||||
| 281 | 0 | topic => $mqtt_properties->{'response_topic'}, | |||||||||||||||||||||||||||
| 282 | payload => \$dump, | ||||||||||||||||||||||||||||
| 283 | ); | ||||||||||||||||||||||||||||
| 284 | }, | ||||||||||||||||||||||||||||
| 285 | 0 | ▶ | on_suback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 286 | 0 | my ($success) = @_; | |||||||||||||||||||||||||||
| 287 | 0 | ▶ | log_error "Could not subscribe to topic '$topic'" unless $success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 0 | ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub _ping_backend_brokers { | ||||||||||||||||||||||||||
|
Subroutine
_ping_backend_brokers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 293 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | foreach my $bus (@{$self->{_BUS_GROUP}}) { | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | next unless $bus->{is_connected}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $bus->pingreq; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 1 | my $_now = 0; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 305 | 0 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 306 | 0 | weaken $self; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 0 | ▶ | croak "Key value is undefined" unless (defined $key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 0 | my $old = $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | $self->{data}->{$key} = $value; | |||||||||||||||||||||||||||
| 313 | 0 | $self->{vers}->{$key}++; | |||||||||||||||||||||||||||
| 314 | 0 | $self->{time}->{$key} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | my $json = encode_json([ | ||||||||||||||||||||||||||||
| 317 | $key, | ||||||||||||||||||||||||||||
| 318 | $value, | ||||||||||||||||||||||||||||
| 319 | $self->{vers}->{$key}, | ||||||||||||||||||||||||||||
| 320 | $self->{time}->{$key}, | ||||||||||||||||||||||||||||
| 321 | $self->{uid}, | ||||||||||||||||||||||||||||
| 322 | 0 | ]); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $self->{on_update}->($key, $value, $old) if $self->{on_update}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # Notify all workers in every cluster about the change | ||||||||||||||||||||||||||||
| 327 | 0 | my @bus_group = grep { $_->{is_connected} } @{$self->{_BUS_GROUP}}; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 0 | unshift @bus_group, $self->{_BUS}; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 0 | foreach my $bus (@bus_group) { | |||||||||||||||||||||||||||
| 332 | 0 | my $local_bus = $bus->{bus_role}; | |||||||||||||||||||||||||||
| 333 | 0 | my $cache_id = $self->{id}; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | $bus->publish( | |||||||||||||||||||||||||||
| 336 | topic => "msg/$local_bus/_sync/$cache_id/set", | ||||||||||||||||||||||||||||
| 337 | payload => \$json, | ||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | unless (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | # Postpone delete because it is necessary to keep the versioning | ||||||||||||||||||||||||||||
| 343 | # of this modification until it is propagated to all workers | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | # Ensure that timer is set properly when the event loop was blocked | ||||||||||||||||||||||||||||
| 346 | 0 | ▶ | if ($_now != time) { $_now = time; AnyEvent->now_update } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | $self->{_destroy}->{$key} = AnyEvent->timer( after => 60, cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 349 | 0 | delete $self->{_destroy}->{$key}; | |||||||||||||||||||||||||||
| 350 | 0 | delete $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 351 | 0 | delete $self->{vers}->{$key}; | |||||||||||||||||||||||||||
| 352 | 0 | delete $self->{time}->{$key}; | |||||||||||||||||||||||||||
| 353 | 0 | }); | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | return 1; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 360 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 366 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | $self->set( $key => undef ); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | ▶ | sub raw_data { | ||||||||||||||||||||||||||
|
Subroutine
raw_data: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 372 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 0 | $self->{data}; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 0 | ▶ | sub _merge { | ||||||||||||||||||||||||||
|
Subroutine
_merge: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 378 | 0 | my ($self, $entry) = @_; | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | my ($key, $value, $version, $time, $uid) = @$entry; | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # Discard updates sent by myself | ||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return if (defined $uid && $uid eq $self->{uid}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $uid and $uid eq $self->{"uid"}
MC/DC0%0/2
defined $uid and $uid eq $self->{"uid"}
defined $uid
$uid eq $self->{"uid"}
Truth table0%0/3
A and B
A= defined $uidB= $uid eq $self->{"uid"}
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | if ($version > ($self->{vers}->{$key} || 0)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$self->{'vers'}{$key} || 0
MC/DC0%0/1
$self->{'vers'}{$key} || 0
$self->{'vers'}{$key}
Truth table0%0/2
A
A= $self->{'vers'}{$key}
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # Received a fresher value for the entry | ||||||||||||||||||||||||||||
| 388 | 0 | my $old = $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | $self->{data}->{$key} = $value; | |||||||||||||||||||||||||||
| 391 | 0 | $self->{vers}->{$key} = $version; | |||||||||||||||||||||||||||
| 392 | 0 | $self->{time}->{$key} = $time; | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 0 | ▶ | $self->{on_update}->($key, $value, $old) if $self->{on_update}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | elsif ($version < $self->{vers}->{$key}) { | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | # Received a stale value (we have a newest version) | ||||||||||||||||||||||||||||
| 399 | 0 | return; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | else { | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # Version conflict, default resolution is to keep newest value | ||||||||||||||||||||||||||||
| 404 | 0 | ▶ | my $resolver = $self->{resolver} || sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | return $_[0]->{time} > $_[1]->{time} ? $_[0] : $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'resolver'} || sub {
return $_[0]{'time'} > $_[1]{'time'} ? $_[0] : $_[1];
}
MC/DC0%0/2
$self->{'resolver'} || sub {
return $_[0]{'time'} > $_[1]{'time'} ? $_[0] : $_[1];
}
$self->{'resolver'}
sub {
return $_[0]{'time'} > $_[1]{'time'} ? $_[0] : $_[1];
}
Truth table0%0/3
A || B
A= $self->{'resolver'}B= sub {
return $_[0]{'time'} > $_[1]{'time'} ? $_[0] : $_[1];
}
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | my $keep = $resolver->( | ||||||||||||||||||||||||||||
| 409 | { # Mine | ||||||||||||||||||||||||||||
| 410 | data => $self->{data}->{$key}, | ||||||||||||||||||||||||||||
| 411 | vers => $self->{vers}->{$key}, | ||||||||||||||||||||||||||||
| 412 | 0 | time => $self->{time}->{$key}, | |||||||||||||||||||||||||||
| 413 | }, | ||||||||||||||||||||||||||||
| 414 | { # Theirs | ||||||||||||||||||||||||||||
| 415 | data => $value, | ||||||||||||||||||||||||||||
| 416 | vers => $version, | ||||||||||||||||||||||||||||
| 417 | time => $time, | ||||||||||||||||||||||||||||
| 418 | }, | ||||||||||||||||||||||||||||
| 419 | ); | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 0 | my $old = $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | $self->{data}->{$key} = $keep->{data}; | |||||||||||||||||||||||||||
| 424 | 0 | $self->{vers}->{$key} = $keep->{vers}; | |||||||||||||||||||||||||||
| 425 | 0 | $self->{time}->{$key} = $keep->{time}; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 0 | ▶ | $self->{on_update}->($key, $keep->{data}, $old) if $self->{on_update}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 0 | ▶ | unless (defined $self->{data}->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | # Postpone delete because it is necessary to keep the versioning | ||||||||||||||||||||||||||||
| 432 | # of this modification until it is propagated to all workers | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | # Ensure that timer is set properly when the event loop was blocked | ||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ($_now != time) { $_now = time; AnyEvent->now_update } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 0 | ▶ | $self->{_destroy}->{$key} = AnyEvent->timer( after => 60, cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 438 | 0 | delete $self->{_destroy}->{$key}; | |||||||||||||||||||||||||||
| 439 | 0 | delete $self->{data}->{$key}; | |||||||||||||||||||||||||||
| 440 | 0 | delete $self->{vers}->{$key}; | |||||||||||||||||||||||||||
| 441 | 0 | delete $self->{time}->{$key}; | |||||||||||||||||||||||||||
| 442 | 0 | }); | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 447 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 0 | my @dump; | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | foreach my $key (keys %{$self->{data}}) { | |||||||||||||||||||||||||||
| 452 | push @dump, [ | ||||||||||||||||||||||||||||
| 453 | $key, | ||||||||||||||||||||||||||||
| 454 | $self->{data}->{$key}, | ||||||||||||||||||||||||||||
| 455 | $self->{vers}->{$key}, | ||||||||||||||||||||||||||||
| 456 | 0 | $self->{time}->{$key}, | |||||||||||||||||||||||||||
| 457 | ]; | ||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | return { | ||||||||||||||||||||||||||||
| 461 | uid => $self->{uid}, | ||||||||||||||||||||||||||||
| 462 | 0 | time => Time::HiRes::time(), | |||||||||||||||||||||||||||
| 463 | dump => \@dump, | ||||||||||||||||||||||||||||
| 464 | }; | ||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 0 | ▶ | sub _merge_dump { | ||||||||||||||||||||||||||
|
Subroutine
_merge_dump: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 468 | 0 | my ($self, $dump) = @_; | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # Discard dumps sent by myself | ||||||||||||||||||||||||||||
| 471 | 0 | ▶ | return if ($dump->{uid} eq $self->{uid}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 0 | foreach my $entry (@{$dump->{dump}}) { | |||||||||||||||||||||||||||
| 474 | 0 | $self->_merge($entry); | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 0 | ▶ | sub touch { | ||||||||||||||||||||||||||
|
Subroutine
touch: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 479 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 0 | ▶ | return unless defined $self->{data}->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 0 | ▶ | croak "No max_age specified (gc is disabled)" unless $self->{max_age}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | my $age = time() - $self->{time}->{$key}; | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | return unless ( $age > $self->{max_age} * 0.3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | 0 | ▶ | return unless ( $age < $self->{max_age} * 1.3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | # Set to current value but without increasing version | ||||||||||||||||||||||||||||
| 491 | 0 | $self->{vers}->{$key}--; | |||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 0 | $self->set( $key => $self->{data}->{$key} ); | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 0 | ▶ | sub _gc { | ||||||||||||||||||||||||||
|
Subroutine
_gc: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 497 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 0 | my $min_time = time() - $self->{max_age} * 1.3; | |||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 0 | foreach my $key (keys %{$self->{data}}) { | |||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 0 | ▶ | next unless ( $self->{time}->{$key} < $min_time ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 0 | ▶ | next unless ( defined $self->{data}->{$key} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 0 | $self->delete( $key ); | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 0 | ▶ | sub _save_state { | ||||||||||||||||||||||||||
|
Subroutine
_save_state: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 511 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 0 | ▶ | return unless ($self->{synced}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 0 | my $id = $self->{id}; | |||||||||||||||||||||||||||
| 516 | 0 | my ($pool_id) = ($self->{pool_id} =~ m/^([\w-]+)$/); # untaint | |||||||||||||||||||||||||||
| 517 | 0 | my $tmp_file = "/tmp/beekeeper-sharedcache-$pool_id-$id.dump"; | |||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | # Avoid stampede when several workers are exiting simultaneously | ||||||||||||||||||||||||||||
| 520 | 0 | ▶ | return if (-e $tmp_file && (stat($tmp_file))[9] == time()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $tmp_file and (stat $tmp_file)[9] == time
MC/DC0%0/2
-e $tmp_file and (stat $tmp_file)[9] == time
-e $tmp_file
(stat $tmp_file)[9] == time
Truth table0%0/3
A and B
A= -e $tmp_fileB= (stat $tmp_file)[9] == time
| |||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | # Lock file because several workers may try to write simultaneously to it | ||||||||||||||||||||||||||||
| 523 | 0 | ▶ | sysopen(my $fh, $tmp_file, O_RDWR|O_CREAT) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 524 | 0 | ▶ | flock($fh, LOCK_EX | LOCK_NB) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 0 | ▶ | truncate($fh, 0) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 0 | print $fh encode_json( $self->dump ); | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | close($fh); | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 0 | ▶ | sub _load_state { | ||||||||||||||||||||||||||
|
Subroutine
_load_state: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 533 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 0 | my $id = $self->{id}; | |||||||||||||||||||||||||||
| 536 | 0 | my ($pool_id) = ($self->{pool_id} =~ m/^([\w-]+)$/); # untaint | |||||||||||||||||||||||||||
| 537 | 0 | my $tmp_file = "/tmp/beekeeper-sharedcache-$pool_id-$id.dump"; | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 0 | ▶ | return unless (-e $tmp_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | # Do not load stale dumps | ||||||||||||||||||||||||||||
| 542 | 0 | ▶ | return if ($self->{max_age} && (stat($tmp_file))[9] < time() - $self->{max_age}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"max_age"} and (stat $tmp_file)[9] < time - $self->{"max_age"}
MC/DC0%0/2
$self->{"max_age"} and (stat $tmp_file)[9] < time - $self->{"max_age"}
$self->{"max_age"}
(stat $tmp_file)[9] < time - $self->{"max_age"}
Truth table0%0/3
A and B
A= $self->{"max_age"}B= (stat $tmp_file)[9] < time - $self->{"max_age"}
| |||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | 0 | local($/); | |||||||||||||||||||||||||||
| 545 | 0 | ▶ | open(my $fh, '<', $tmp_file) or die "Couldn't read $tmp_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 546 | 0 | my $data = <$fh>; | |||||||||||||||||||||||||||
| 547 | 0 | close($fh); | |||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | 0 | local $@; | |||||||||||||||||||||||||||
| 550 | 0 | my $dump = eval { decode_json($data) }; | |||||||||||||||||||||||||||
| 551 | 0 | ▶ | return if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 0 | ▶ | my $min_time = $self->{max_age} ? time() - $self->{max_age} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 0 | foreach my $entry (@{$dump->{dump}}) { | |||||||||||||||||||||||||||
| 556 | # Do not merge stale entries | ||||||||||||||||||||||||||||
| 557 | 0 | ▶ | next if ($min_time && $entry->[3] < $min_time); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$min_time and $entry->[3] < $min_time
MC/DC0%0/2
$min_time and $entry->[3] < $min_time
$min_time
$entry->[3] < $min_time
Truth table0%0/3
A and B
A= $min_timeB= $entry->[3] < $min_time
| |||||||||||||||||||||||||||||
| 558 | 0 | $self->_merge($entry); | |||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | 0 | ▶ | sub _disconnect { | ||||||||||||||||||||||||||
|
Subroutine
_disconnect: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 563 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 0 | ▶ | $self->_save_state if $self->{persist}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | 0 | foreach my $bus (@{$self->{_BUS_GROUP}}) { | |||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | 0 | ▶ | next unless ($bus->{is_connected}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 0 | $bus->disconnect; | |||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 575 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | 0 | $self->_disconnect; | |||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | 1 | 1; | |||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | __END__ | ||||||||||||||||||||||||||||