lib/Langertha/Skeid/CapacityProbe.pm

filter: Statementstmt 86.9% 93 / 107 Branchbran 83.3% 35 / 42 Conditioncond 50.0% 12 / 24 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 93.7% 15 / 16 Podpod 100% 7 / 7 totaltotal 79.3% 165 / 208 CC 33 SCAR 20.9
CC
33
Cov
81%
for_node
16.4
__ANON__
7.0
start
6.6
CRAP 8.1
« lib/Langertha/Skeid/Protocol.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm »
1 package Langertha::Skeid::CapacityProbe;
2 90 our $VERSION = '0.004';
3 # ABSTRACT: Background probes that report what a node's real capacity is
4 90 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 90 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 90 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 90 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 90 has skeid => (
12 is => 'ro',
13 required => 1,
14 );
15  
16  
17 90 has node_id => (
18 is => 'ro',
19 required => 1,
20 );
21  
22  
23 has interval_ms => (
24 is => 'rw',
25 14 ▶ default => sub { 2000 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 90 );
27  
28  
29 has config => (
30 is => 'ro',
31 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 90 );
33  
34 90 has _timer => (
35 is => 'rw',
36 clearer => '_clear_timer',
37 );
38  
39  
40 has is_stopped => (
41 is => 'rwp',
42 36 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 90 );
44  
45  
46 12 ▶sub source { return }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
47  
48 # Forget what this probe reported, and nothing another source said.
49 36 ▶sub _forget_own {
Subroutine _forget_own: called CC=2 SCAR=6.9
50 36 my ($self) = @_;
51 36 my $source = $self->source;
52 36 ▶ return $self->skeid->forget_capacity($self->node_id, (defined $source ? (source => $source) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $source ? : 24 12 36
53 }
54  
55  
56 0 ▶sub poll {
Subroutine poll: not called CC=1 SCAR=6.9
Pod documented
57 0 my ($self) = @_;
58 0 croak ref($self) . " must implement poll()";
59 }
60  
61  
62 45 ▶sub poll_interval_seconds {
Subroutine poll_interval_seconds: called CC=5 SCAR=16.7
Pod documented
63 45 my ($self) = @_;
64 45 ▶ my $workers = 0 + ($self->skeid->worker_count // 1);
Condition50%1/2
$self->skeid->worker_count // 1
l!l
450
MC/DC0%0/1
$self->skeid->worker_count // 1
$self->skeid->worker_count
Truth table50%1/2
A
A= $self->skeid->worker_count
Aresult
11
00
65 45 ▶ $workers = 1 if $workers < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $workers < 1 0 45 45
66  
67 45 ▶ my $every = (($self->interval_ms || 2000) * $workers) / 1000;
Condition50%1/2
$self->interval_ms || 2000
l!l
450
MC/DC0%0/1
$self->interval_ms || 2000
$self->interval_ms
Truth table50%1/2
A
A= $self->interval_ms
Aresult
11
00
68 45 ▶ return $every < 0.1 ? 0.1 : $every;
Branch: 1 branch
BranchTrueFalseTotal
$every < 0.1 ? : 1 44 45
69 }
70  
71  
72 19 ▶sub start {
Subroutine start: called CC=6 SCAR=18.9
Pod documented
73 19 my ($self) = @_;
74 19 ▶ return $self->_timer if $self->_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_timer 0 19 19
75 19 require Mojo::IOLoop;
76 19 $self->_set_is_stopped(0);
77  
78 19 my $every = $self->poll_interval_seconds;
79  
80 # A reading older than capacity_max_age_ms is dropped. A probe that polls less often than
81 # that leaves gaps in which inflight decides, and the operator should hear about it once.
82 19 ▶ my $max_age_ms = 0 + ($self->skeid->capacity_max_age_ms // 0);
Condition50%1/2
$self->skeid->capacity_max_age_ms // 0
l!l
190
MC/DC0%0/1
$self->skeid->capacity_max_age_ms // 0
$self->skeid->capacity_max_age_ms
Truth table50%1/2
A
A= $self->skeid->capacity_max_age_ms
Aresult
11
00
83 19 ▶ if ($max_age_ms > 0 && $every * 1000 >= $max_age_ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_age_ms > 0 and $every * 1000 >= $max_age_ms) 15 4 19
Condition66%2/3
$max_age_ms > 0 and $every * 1000 >= $max_age_ms
!ll&&!rl&&r
0415
MC/DC50%1/2
$max_age_ms > 0 and $every * 1000 >= $max_age_ms
$max_age_ms > 0 $every * 1000 >= $max_age_ms
Truth table66%2/3
A and B
A= $max_age_ms > 0B= $every * 1000 >= $max_age_ms
ABresult
0X0
100
111
84 15 warn "capacity probe for '" . $self->node_id . "' polls every " . ($every * 1000)
85 . "ms (interval_ms x workers); it must be below capacity_max_age_ms ($max_age_ms), or its "
86 . "reading expires between polls and inflight decides in the gaps\n";
87 }
88  
89 # Weak, or the timer's closure keeps the probe (and the whole control plane) alive forever.
90 19 my $weak = $self;
91 19 weaken($weak);
92  
93 12 ▶ my $id = Mojo::IOLoop->recurring($every => sub {
Subroutine __ANON__: called CC=4 SCAR=19.4
94 12 ▶ my $probe = $weak or return;
Branch: 1 branch
BranchTrueFalseTotal
my $probe = $weak or return 12 0 12
95 # A probe that dies takes the timer's reactor with it otherwise, and one unreachable
96 # metrics endpoint should not stop the proxy.
97 12 ▶ eval { $probe->poll; 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $probe->poll; 1 } } or do { my $err = $@ || "unknown error"; $err =~ s/\s+\z//; warn q[capacity probe for '] . $probe->node_id . "' failed: $err"; $probe->_forget_own } 12 0 12
98 0 ▶ my $err = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
99 0 $err =~ s/\s+\z//;
100 0 warn "capacity probe for '" . $probe->node_id . "' failed: $err";
101 0 $probe->_forget_own;
102 };
103 19 });
104 19 $self->_timer($id);
105  
106 19 ▶ eval { $self->poll; 1 } or do { $self->_forget_own };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->poll; 1 } } or do { $self->_forget_own } 18 1 19
107 19 return $id;
108 }
109  
110  
111 20 ▶sub stop {
Subroutine stop: called CC=2 SCAR=6.9
Pod documented
112 20 my ($self) = @_;
113 20 $self->_set_is_stopped(1);
114 20 ▶ if (my $id = $self->_timer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_timer) 14 6 20
115 14 require Mojo::IOLoop;
116 14 eval { Mojo::IOLoop->remove($id) };
117 14 $self->_clear_timer;
118 }
119 20 $self->_forget_own;
120 20 return 1;
121 }
122  
123  
124 807 ▶sub for_node {
Subroutine for_node: called CC=15 SCAR=27.9
Pod documented
125 807 my ($class, $skeid, $node) = @_;
126 807 ▶ return unless ref($node) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 807 807
127 807 my $cfg = $node->{capacity};
128 807 ▶ return unless ref($cfg) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "HASH" 778 29 807
129  
130 29 ▶ my $kind = lc($cfg->{probe} // $cfg->{type} // 'inflight');
Condition40%2/5
($cfg->{"probe"} // $cfg->{"type"}) // "inflight"
l!l
290
$cfg->{"probe"} // $cfg->{"type"}
l!l&&r!l&&!r
2900
MC/DC0%0/2
($cfg->{"probe"} // $cfg->{"type"}) // "inflight"
$cfg->{"probe"} $cfg->{"type"}
Truth table33%1/3
A // B
A= $cfg->{"probe"}B= $cfg->{"type"}
ABresult
1X1
011
000
131 29 ▶ return if $kind eq 'inflight' || $kind eq 'none' || $kind eq 'ratelimit';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "inflight" or $kind eq "none" or $kind eq "ratelimit" 2 27 29
Condition83%5/6
$kind eq "inflight" or $kind eq "none" or $kind eq "ratelimit"
l!l&&r!l&&!r
1127
$kind eq "inflight" or $kind eq "none"
l!l&&r!l&&!r
1028
MC/DC66%2/3
$kind eq "inflight" or $kind eq "none" or $kind eq "ratelimit"
$kind eq "inflight" $kind eq "none" $kind eq "ratelimit"
Truth table75%3/4
A or B or C
A= $kind eq "inflight"B= $kind eq "none"C= $kind eq "ratelimit"
ABCresult
1XX1
01X1
0011
0000
132  
133 my %args = (
134 skeid => $skeid,
135 node_id => $node->{id},
136 config => $cfg,
137 27 ▶ (defined $cfg->{interval_ms} ? (interval_ms => 0 + $cfg->{interval_ms}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg->{'interval_ms'} ? : 19 8 27
138 );
139  
140 27 ▶ if ($kind eq 'prometheus') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "prometheus") 4 23 27
141 4 require Langertha::Skeid::CapacityProbe::Prometheus;
142 4 return Langertha::Skeid::CapacityProbe::Prometheus->new(%args);
143 }
144  
145 23 ▶ if ($kind eq 'registry') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "registry") 4 19 23
146 4 require Langertha::Skeid::CapacityProbe::Registry;
147 4 return Langertha::Skeid::CapacityProbe::Registry->new(%args);
148 }
149  
150 19 ▶ if ($kind eq 'custom') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "custom") 18 1 19
151 18 ▶ if (ref($cfg->{code}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cfg->{"code"} eq "CODE") 16 2 18
152 16 require Langertha::Skeid::CapacityProbe::Custom;
153 16 return Langertha::Skeid::CapacityProbe::Custom->new(%args, code => $cfg->{code});
154 }
155 2 ▶ my $custom_class = $cfg->{class} or croak "capacity probe 'custom' needs a code or class";
Branch: 1 branch
BranchTrueFalseTotal
my $custom_class = $cfg->{"class"} or croak("capacity probe 'custom' needs a code or class") 1 1 2
156 # A class name out of a config file is loaded by name, so keep it looking like one.
157 1 ▶ croak "invalid capacity probe class '$custom_class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $custom_class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/ 1 0 1
158 unless $custom_class =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
159 0 my $path = $custom_class . '.pm';
160 0 $path =~ s{::}{/}g;
161 0 require $path;
162 0 return $custom_class->new(%args);
163 }
164  
165 1 croak "unknown capacity probe '$kind'";
166 }
167  
168  
169 517 ▶sub start_for_skeid {
Subroutine start_for_skeid: called CC=5 SCAR=17.8
Pod documented
170 517 my ($class, $skeid) = @_;
171 517 my %probes;
172 517 for my $node (@{$skeid->nodes}) {
173 792 my $probe = eval { $class->for_node($skeid, $node) };
174 792 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 792 792
175 0 my $err = $@; $err =~ s/\s+\z//;
176 0 ▶ warn "capacity probe for '" . ($node->{id} // '?') . "' not started: $err";
Condition0%0/2
$node->{"id"} // "?"
l!l
00
MC/DC0%0/1
$node->{"id"} // "?"
$node->{"id"}
Truth table0%0/2
A
A= $node->{"id"}
Aresult
11
00
177 0 next;
178 }
179 792 ▶ next unless $probe;
Branch: 1 branch
BranchTrueFalseTotal
unless $probe 777 15 792
180 15 $probe->start;
181 15 $probes{$node->{id}} = $probe;
182 }
183 517 return \%probes;
184 }
185  
186  
187 90 1;
188  
189 __END__
« lib/Langertha/Skeid/Protocol.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm »