| 1 | package Langertha::Skeid::CapacityProbe::Registry; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.004'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Capacity probe reading a downstream Skeid's signed registry snapshot | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use JSON::MaybeXS qw(decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Langertha::Skeid::Registry; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | extends 'Langertha::Skeid::CapacityProbe'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | my %KNOWN = map { $_ => 1 } qw(probe type url path read_key_env admin_key_env secret_env interval_ms tags max_skew_s); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 13 | ▶ | sub validate_config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_config: called CC=23 SCAR=38.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | my ($class, $cfg, $node_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 13 | ▶ | my $where = "node '" . ($node_id // '?') . "' capacity (registry)"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node_id // "?"
MC/DC0%0/1
$node_id // "?"
$node_id
Truth table50%1/2
A
A= $node_id
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | croak "$where must be a hashref" unless ref($cfg) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 13 | for my $key (sort keys %$cfg) { | |||||||||||||||||||||||||||||||||||||||||
| 24 | croak "$where: unknown key '$key' (secrets are named by read_key_env / admin_key_env / " | ||||||||||||||||||||||||||||||||||||||||||
| 25 | . 'secret_env, never ' | ||||||||||||||||||||||||||||||||||||||||||
| 26 | . 'written into the config)' | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 38 | ▶ | unless $KNOWN{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 12 | ▶ | my $named = sub { my $value = $cfg->{$_[0]}; defined($value) && !ref($value) && length($value) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
Condition66%2/3
defined $value and not ref $value
MC/DC50%1/2
defined $value and not ref $value
defined $value
not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | ▶ | croak "$where: secret_env is required" unless $named->('secret_env'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 9 | for my $key (qw(read_key_env admin_key_env)) { | |||||||||||||||||||||||||||||||||||||||||
| 32 | croak "$where: $key must name an environment variable" | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 18 | ▶ | if defined($cfg->{$key}) && !$named->($key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cfg->{$key} and not &$named($key)
MC/DC0%0/2
defined $cfg->{$key} and not &$named($key)
defined $cfg->{$key}
not &$named($key)
Truth table66%2/3
A and not(B)
A= defined $cfg->{$key}B= not &$named($key)
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | croak "$where: read_key_env (or admin_key_env) is required" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&$named("read_key_env") or &$named("admin_key_env")
MC/DC100%2/2
&$named("read_key_env") or &$named("admin_key_env")
&$named("read_key_env")
&$named("admin_key_env")
Truth table100%3/3
A or B
A= &$named("read_key_env")B= &$named("admin_key_env")
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | unless $named->('read_key_env') || $named->('admin_key_env'); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | ▶ | if (defined $cfg->{url}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | croak "$where: url must be an absolute http(s) URL" | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if ref($cfg->{url}) || $cfg->{url} !~ m{\Ahttps?://[^/?#]+}i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $cfg->{"url"} or not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i
MC/DC0%0/2
ref $cfg->{"url"} or not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i
ref $cfg->{"url"}
not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i
Truth table33%1/3
A or not(B)
A= ref $cfg->{"url"}B= not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | if (defined $cfg->{interval_ms}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | croak "$where: interval_ms must be a positive number" | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | unless looks_like_number($cfg->{interval_ms}) && $cfg->{interval_ms} > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($cfg->{"interval_ms"}) and $cfg->{"interval_ms"} > 0
MC/DC0%0/2
&looks_like_number($cfg->{"interval_ms"}) and $cfg->{"interval_ms"} > 0
&looks_like_number($cfg->{"interval_ms"})
$cfg->{"interval_ms"} > 0
Truth table33%1/3
A and B
A= &looks_like_number($cfg->{"interval_ms"})B= $cfg->{"interval_ms"} > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | ▶ | if (defined $cfg->{max_skew_s}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | croak "$where: max_skew_s must be a number >= 0" | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless looks_like_number($cfg->{max_skew_s}) && $cfg->{max_skew_s} >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&looks_like_number($cfg->{"max_skew_s"}) and $cfg->{"max_skew_s"} >= 0
MC/DC0%0/2
&looks_like_number($cfg->{"max_skew_s"}) and $cfg->{"max_skew_s"} >= 0
&looks_like_number($cfg->{"max_skew_s"})
$cfg->{"max_skew_s"} >= 0
Truth table0%0/3
A and B
A= &looks_like_number($cfg->{"max_skew_s"})B= $cfg->{"max_skew_s"} >= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | ▶ | if (defined $cfg->{tags}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | croak "$where: tags must be a list or a string" | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ref($cfg->{tags}) && ref($cfg->{tags}) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $cfg->{"tags"} and ref $cfg->{"tags"} ne "ARRAY"
MC/DC0%0/2
ref $cfg->{"tags"} and ref $cfg->{"tags"} ne "ARRAY"
ref $cfg->{"tags"}
ref $cfg->{"tags"} ne "ARRAY"
Truth table0%0/3
A and B
A= ref $cfg->{"tags"}B= ref $cfg->{"tags"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | has _ua => ( | ||||||||||||||||||||||||||||||||||||||||||
| 57 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__ua: not called CC=1 SCAR=1.2
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $ua = Mojo::UserAgent->new; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $ua->connect_timeout(2); | |||||||||||||||||||||||||||||||||||||||||
| 61 | # Shorter than the poll interval, for the reason the Prometheus probe gives: a poll still | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # out when the next tick fires is a probe queueing on itself. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $ua->request_timeout(3); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return $ua; | |||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | has _inflight_poll => ( | ||||||||||||||||||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | has state => ( | ||||||||||||||||||||||||||||||||||||||||||
| 75 | is => 'rwp', | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 7 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # generated_at of the last accepted snapshot; nothing older is believed after it. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | has _last_generated_at => ( | ||||||||||||||||||||||||||||||||||||||||||
| 81 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 28 | ▶ | sub url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=8 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 28 | my $cfg = $self->config; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 28 | ▶ | return $cfg->{url} if defined($cfg->{url}) && length($cfg->{url}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $cfg->{"url"} and length $cfg->{"url"}
MC/DC0%0/2
defined $cfg->{"url"} and length $cfg->{"url"}
defined $cfg->{"url"}
length $cfg->{"url"}
Truth table33%1/3
A and B
A= defined $cfg->{"url"}B= length $cfg->{"url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 28 | ▶ | my ($node) = grep { ($_->{id} // '') eq $self->node_id } @{$self->skeid->nodes}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"id"} // ""
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 28 | ▶ | my $base = $node ? ($node->{url} // '') : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{"url"} // ""
MC/DC0%0/1
$node->{"url"} // ""
$node->{"url"}
Truth table50%1/2
A
A= $node->{"url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 28 | $base =~ s{/v\d+/?$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | $base =~ s{/+$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 28 | ▶ | my $path = $cfg->{path} // '/skeid/registry/snapshot'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cfg->{"path"} // "/skeid/registry/snapshot"
MC/DC0%0/1
$cfg->{"path"} // "/skeid/registry/snapshot"
$cfg->{"path"}
Truth table50%1/2
A
A= $cfg->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 28 | ▶ | $path = "/$path" unless $path =~ m{^/}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 28 | return $base . $path; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | ▶ | sub _tags { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tags: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 19 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 19 | my $tags = $self->config->{tags}; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 19 | ▶ | return [] unless defined $tags; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return [ ref($tags) eq 'ARRAY' ? @$tags : split(/[,\s]+/, "$tags") ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 37 | ▶ | sub source { 'registry' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
source: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 31 | ▶ | sub poll { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
poll: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 31 | ▶ | return if $self->_inflight_poll; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 31 | my $cfg = $self->config; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 31 | ▶ | my $secret = defined($cfg->{secret_env}) ? ($ENV{$cfg->{secret_env}} // '') : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ENV{$cfg->{"secret_env"}} // ""
MC/DC0%0/1
$ENV{$cfg->{"secret_env"}} // ""
$ENV{$cfg->{"secret_env"}}
Truth table50%1/2
A
A= $ENV{$cfg->{"secret_env"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | # The read key when configured, never falling back to the admin key: an operator who named | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # a read key does not want the admin key on the wire (skeid #49). | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 31 | ▶ | my $key_env = defined($cfg->{read_key_env}) ? 'read_key_env' : 'admin_key_env'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 31 | ▶ | my $bearer = defined($cfg->{$key_env}) ? ($ENV{$cfg->{$key_env}} // '') : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ENV{$cfg->{$key_env}} // ""
MC/DC0%0/1
$ENV{$cfg->{$key_env}} // ""
$ENV{$cfg->{$key_env}}
Truth table50%1/2
A
A= $ENV{$cfg->{$key_env}}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 31 | ▶ | unless (length($secret) && length($bearer)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $secret and length $bearer
MC/DC100%2/2
length $secret and length $bearer
length $secret
length $bearer
Truth table100%3/3
A and B
A= length $secretB= length $bearer
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | return $self->_reject(missing_secret => "$key_env or secret_env is not set"); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # The downstream refuses to publish with a shorter secret, so no snapshot could verify. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 27 | ▶ | if (length($secret) < Langertha::Skeid::Registry->MIN_SECRET_BYTES) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return $self->_reject(missing_secret => 'secret_env is shorter than ' | |||||||||||||||||||||||||||||||||||||||||
| 127 | . Langertha::Skeid::Registry->MIN_SECRET_BYTES . ' bytes'); | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 27 | $self->_inflight_poll(1); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 27 | ▶ | $self->_ua->get($self->url => { Authorization => "Bearer $bearer" } => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 27 | my (undef, $tx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 27 | $self->_inflight_poll(0); | |||||||||||||||||||||||||||||||||||||||||
| 134 | # Stopped while the request was out: this answer belongs to no running probe. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 27 | ▶ | return if $self->is_stopped; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 27 | $self->_consume($tx->res, $secret); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 27 | }); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 27 | return; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 27 | ▶ | sub _consume { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_consume: called CC=16 SCAR=28.8
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 27 | my ($self, $res, $secret) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 27 | ▶ | my $status = $res->code // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res->code // 0
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 27 | ▶ | return $self->_reject(unreachable => "HTTP $status") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 200 and $status < 300
MC/DC50%1/2
$status >= 200 and $status < 300
$status >= 200
$status < 300
Truth table66%2/3
A and B
A= $status >= 200B= $status < 300
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | unless $status >= 200 && $status < 300; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 26 | my $body = $res->body; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 26 | my $signature = $res->headers->header(Langertha::Skeid::Registry->SIGNATURE_HEADER); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 26 | ▶ | return $self->_reject(bad_signature => 'signature missing or not valid for this body') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | unless Langertha::Skeid::Registry->verify($body, $signature, $secret); | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 22 | my $snapshot = eval { decode_json($body) }; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 22 | ▶ | return $self->_reject(malformed => 'body is not a JSON object') unless ref($snapshot) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | return $self->_reject(malformed => 'unknown schema version') | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 22 | ▶ | unless ($snapshot->{version} // '') eq Langertha::Skeid::Registry->SCHEMA_VERSION; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$snapshot->{"version"} // ""
MC/DC0%0/1
$snapshot->{"version"} // ""
$snapshot->{"version"}
Truth table50%1/2
A
A= $snapshot->{"version"}
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 22 | my ($generated_at, $ttl) = @{$snapshot}{qw(generated_at ttl)}; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 22 | ▶ | return $self->_reject(malformed => 'generated_at / ttl missing') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
&looks_like_number($generated_at) and &looks_like_number($ttl) and $ttl > 0
&looks_like_number($generated_at) and &looks_like_number($ttl)
MC/DC0%0/3
&looks_like_number($generated_at) and &looks_like_number($ttl) and $ttl > 0
&looks_like_number($generated_at)
&looks_like_number($ttl)
$ttl > 0
Truth table25%1/4
A and B and C
A= &looks_like_number($generated_at)B= &looks_like_number($ttl)C= $ttl > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | unless looks_like_number($generated_at) && looks_like_number($ttl) && $ttl > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 22 | my $now = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 22 | ▶ | my $skew = defined($self->config->{max_skew_s}) ? 0 + $self->config->{max_skew_s} : 5; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 22 | ▶ | return $self->_reject(future => 'generated_at is ahead of this clock by more than max_skew_s') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | if $generated_at > $now + $skew; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 21 | ▶ | return $self->_reject(stale => 'older than its ttl') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | if $now - $generated_at > $ttl; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 20 | my $last = $self->_last_generated_at; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 20 | ▶ | return $self->_reject(replayed => 'older than the last snapshot accepted') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $last and $generated_at < $last
MC/DC100%2/2
defined $last and $generated_at < $last
defined $last
$generated_at < $last
Truth table100%3/3
A and B
A= defined $lastB= $generated_at < $last
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | if defined($last) && $generated_at < $last; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 19 | $self->_last_generated_at(0 + $generated_at); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 19 | my $reading = Langertha::Skeid::Registry->reading_from_snapshot($snapshot, tags => $self->_tags); | |||||||||||||||||||||||||||||||||||||||||
| 172 | $self->skeid->set_capacity_reading( | ||||||||||||||||||||||||||||||||||||||||||
| 173 | $self->node_id, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | source => $self->source, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | used => $reading->{used}, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | limit => $reading->{limit}, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 19 | at => 0 + $generated_at, | |||||||||||||||||||||||||||||||||||||||||
| 178 | expires_at => $generated_at + $ttl, | ||||||||||||||||||||||||||||||||||||||||||
| 179 | interval_ms => $self->poll_interval_seconds * 1000, | ||||||||||||||||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 19 | $self->_enter('accepted'); | |||||||||||||||||||||||||||||||||||||||||
| 182 | 19 | return; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # Unknown means inflight decides -- but only this probe's reading is dropped. A backoff another | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # source recorded is still true whatever this snapshot was. | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 12 | ▶ | sub _reject { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reject: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 12 | my ($self, $state, $detail) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 12 | $self->_forget_own; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 12 | $self->_enter($state, $detail); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 12 | return; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 31 | ▶ | sub _enter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_enter: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 31 | my ($self, $state, $detail) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 31 | my $was = $self->state; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 31 | $self->_set_state($state); | |||||||||||||||||||||||||||||||||||||||||
| 198 | 31 | ▶ | return if defined($was) && $was eq $state; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $was and $was eq $state
MC/DC100%2/2
defined $was and $was eq $state
defined $was
$was eq $state
Truth table100%3/3
A and B
A= defined $wasB= $was eq $state
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | # The first success is the expected case and not worth a line; everything else is. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 17 | ▶ | return if !defined($was) && $state eq 'accepted'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $was and $state eq "accepted"
MC/DC100%2/2
not defined $was and $state eq "accepted"
not defined $was
$state eq "accepted"
Truth table100%3/3
not(A) and B
A= not defined $wasB= $state eq "accepted"
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 13 | ▶ | warn "registry probe for '" . $self->node_id . "': $state" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | . (defined $detail ? " ($detail)" : '') . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 13 | return; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | __END__ | ||||||||||||||||||||||||||||||||||||||||||