blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm

filter: Statementstmt 94.0% 126 / 134 Branchbran 69.7% 53 / 76 Conditioncond 55.7% 34 / 61 MC/DCmcdc 32.4% 12 / 37 Subroutinesub 95.6% 22 / 23 Podpod 100% 4 / 4 totaltotal 74.9% 251 / 335 CC 64 SCAR 29.1
CC
64
Cov
79%
validate_config
45.1
_consume
17.7
poll
10.4
CRAP 18.4
« lib/Langertha/Skeid/Protocol/Ollama.pm ↑ Summary lib/Langertha/Skeid/Registry.pm »
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 // "?"
l!l
130
MC/DC0%0/1
$node_id // "?"
$node_id
Truth table50%1/2
A
A= $node_id
Aresult
11
00
22 13 ▶ croak "$where must be a hashref" unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 0 13 13
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
BranchTrueFalseTotal
unless $KNOWN{$key} 1 37 38
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
BranchTrueFalseTotal
defined $value and not ref $value and length $value 26 11 37
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
11026
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
ABresult
0X0
100
111
30 12 ▶ croak "$where: secret_env is required" unless $named->('secret_env');
Branch: 1 branch
BranchTrueFalseTotal
unless &$named("secret_env") 3 9 12
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
BranchTrueFalseTotal
if defined $cfg->{$key} and not &$named($key) 0 18 18
Condition66%2/3
defined $cfg->{$key} and not &$named($key)
!ll&&!rl&&r
990
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)
ABresult
0X0
100
111
34 }
35 9 ▶ croak "$where: read_key_env (or admin_key_env) is required"
Branch: 1 branch
BranchTrueFalseTotal
unless &$named("read_key_env") or &$named("admin_key_env") 1 8 9
Condition100%3/3
&$named("read_key_env") or &$named("admin_key_env")
l!l&&r!l&&!r
261
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")
ABresult
1X1
011
000
36 unless $named->('read_key_env') || $named->('admin_key_env');
37 8 ▶ if (defined $cfg->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{"url"}) 1 7 8
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
BranchTrueFalseTotal
if ref $cfg->{"url"} or not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i 1 0 1
Condition33%1/3
ref $cfg->{"url"} or not $cfg->{"url"} =~ m[\Ahttps?://[^/?#]+]i
l!l&&r!l&&!r
010
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
ABresult
1X1
011
000
40 }
41 7 ▶ if (defined $cfg->{interval_ms}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{"interval_ms"}) 2 5 7
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
BranchTrueFalseTotal
unless &looks_like_number($cfg->{"interval_ms"}) and $cfg->{"interval_ms"} > 0 0 2 2
Condition33%1/3
&looks_like_number($cfg->{"interval_ms"}) and $cfg->{"interval_ms"} > 0
!ll&&!rl&&r
002
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
ABresult
0X0
100
111
44 }
45 7 ▶ if (defined $cfg->{max_skew_s}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{"max_skew_s"}) 0 7 7
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
BranchTrueFalseTotal
unless &looks_like_number($cfg->{"max_skew_s"}) and $cfg->{"max_skew_s"} >= 0 0 0 0
Condition0%0/3
&looks_like_number($cfg->{"max_skew_s"}) and $cfg->{"max_skew_s"} >= 0
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
48 }
49 7 ▶ if (defined $cfg->{tags}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{"tags"}) 0 7 7
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
BranchTrueFalseTotal
if ref $cfg->{"tags"} and ref $cfg->{"tags"} ne "ARRAY" 0 0 0
Condition0%0/3
ref $cfg->{"tags"} and ref $cfg->{"tags"} ne "ARRAY"
!ll&&!rl&&r
000
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $cfg->{"url"} and length $cfg->{"url"} 0 28 28
Condition33%1/3
defined $cfg->{"url"} and length $cfg->{"url"}
!ll&&!rl&&r
2800
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"}
ABresult
0X0
100
111
90  
91 28 ▶ my ($node) = grep { ($_->{id} // '') eq $self->node_id } @{$self->skeid->nodes};
Condition50%1/2
$_->{"id"} // ""
l!l
420
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
92 28 ▶ my $base = $node ? ($node->{url} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$node ? : 28 0 28
Condition50%1/2
$node->{"url"} // ""
l!l
280
MC/DC0%0/1
$node->{"url"} // ""
$node->{"url"}
Truth table50%1/2
A
A= $node->{"url"}
Aresult
11
00
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"
l!l
028
MC/DC0%0/1
$cfg->{"path"} // "/skeid/registry/snapshot"
$cfg->{"path"}
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
96 28 ▶ $path = "/$path" unless $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 28 28
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
BranchTrueFalseTotal
unless defined $tags 19 0 19
104 0 ▶ return [ ref($tags) eq 'ARRAY' ? @$tags : split(/[,\s]+/, "$tags") ];
Branch: 1 branch
BranchTrueFalseTotal
ref $tags eq 'ARRAY' ? : 0 0 0
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
BranchTrueFalseTotal
if $self->_inflight_poll 0 31 31
114  
115 31 my $cfg = $self->config;
116 31 ▶ my $secret = defined($cfg->{secret_env}) ? ($ENV{$cfg->{secret_env}} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{'secret_env'} ? : 29 2 31
Condition50%1/2
$ENV{$cfg->{"secret_env"}} // ""
l!l
290
MC/DC0%0/1
$ENV{$cfg->{"secret_env"}} // ""
$ENV{$cfg->{"secret_env"}}
Truth table50%1/2
A
A= $ENV{$cfg->{"secret_env"}}
Aresult
11
00
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
BranchTrueFalseTotal
defined $cfg->{'read_key_env'} ? : 2 29 31
120 31 ▶ my $bearer = defined($cfg->{$key_env}) ? ($ENV{$cfg->{$key_env}} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{$key_env} ? : 29 2 31
Condition50%1/2
$ENV{$cfg->{$key_env}} // ""
l!l
290
MC/DC0%0/1
$ENV{$cfg->{$key_env}} // ""
$ENV{$cfg->{$key_env}}
Truth table50%1/2
A
A= $ENV{$cfg->{$key_env}}
Aresult
11
00
121 31 ▶ unless (length($secret) && length($bearer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $secret and length $bearer) 4 27 31
Condition100%3/3
length $secret and length $bearer
!ll&&!rl&&r
3127
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (length $secret < "Langertha::Skeid::Registry"->MIN_SECRET_BYTES) 0 27 27
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
BranchTrueFalseTotal
if $self->is_stopped 0 27 27
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
l!l
270
MC/DC0%0/1
$res->code // 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
144 27 ▶ return $self->_reject(unreachable => "HTTP $status")
Branch: 1 branch
BranchTrueFalseTotal
unless $status >= 200 and $status < 300 1 26 27
Condition66%2/3
$status >= 200 and $status < 300
!ll&&!rl&&r
0126
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless "Langertha::Skeid::Registry"->verify($body, $signature, $secret) 4 22 26
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
BranchTrueFalseTotal
unless ref $snapshot eq "HASH" 0 22 22
154 return $self->_reject(malformed => 'unknown schema version')
155 22 ▶ unless ($snapshot->{version} // '') eq Langertha::Skeid::Registry->SCHEMA_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless ($snapshot->{"version"} // "") eq "Langertha::Skeid::Registry"->SCHEMA_VERSION 0 22 22
Condition50%1/2
$snapshot->{"version"} // ""
l!l
220
MC/DC0%0/1
$snapshot->{"version"} // ""
$snapshot->{"version"}
Truth table50%1/2
A
A= $snapshot->{"version"}
Aresult
11
00
156 22 my ($generated_at, $ttl) = @{$snapshot}{qw(generated_at ttl)};
157 22 ▶ return $self->_reject(malformed => 'generated_at / ttl missing')
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($generated_at) and &looks_like_number($ttl) and $ttl > 0 0 22 22
Condition33%2/6
&looks_like_number($generated_at) and &looks_like_number($ttl) and $ttl > 0
!ll&&!rl&&r
0022
&looks_like_number($generated_at) and &looks_like_number($ttl)
!ll&&!rl&&r
0022
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
defined $self->config->{'max_skew_s'} ? : 0 22 22
162 22 ▶ return $self->_reject(future => 'generated_at is ahead of this clock by more than max_skew_s')
Branch: 1 branch
BranchTrueFalseTotal
if $generated_at > $now + $skew 1 21 22
163 if $generated_at > $now + $skew;
164 21 ▶ return $self->_reject(stale => 'older than its ttl')
Branch: 1 branch
BranchTrueFalseTotal
if $now - $generated_at > $ttl 1 20 21
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
BranchTrueFalseTotal
if defined $last and $generated_at < $last 1 19 20
Condition100%3/3
defined $last and $generated_at < $last
!ll&&!rl&&r
4151
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $was and $was eq $state 14 17 31
Condition100%3/3
defined $was and $was eq $state
!ll&&!rl&&r
61114
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if not defined $was and $state eq "accepted" 4 13 17
Condition100%3/3
not defined $was and $state eq "accepted"
!ll&&!rl&&r
1124
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"
ABresult
0X0
100
111
201 13 ▶ warn "registry probe for '" . $self->node_id . "': $state"
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 10 3 13
202 . (defined $detail ? " ($detail)" : '') . "\n";
203 13 return;
204 }
205  
206  
207 3 1;
208  
209 __END__
« lib/Langertha/Skeid/Protocol/Ollama.pm ↑ Summary lib/Langertha/Skeid/Registry.pm »