« blib/lib/Langertha/Skeid/Protocol.pm
↑ Summary
blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm »
| 1 | package Langertha::Skeid::CapacityProbe; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 50 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Background probes that report what a node's real capacity is | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 50 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 50 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 50 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 50 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 50 | has skeid => ( | |||||||||||||||||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 50 | 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 | 50 | ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | has config => ( | ||||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 50 | ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 50 | 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 | 50 | ); | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$self->skeid->worker_count // 1
$self->skeid->worker_count
Truth table50%1/2
A
A= $self->skeid->worker_count
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 45 | ▶ | $workers = 1 if $workers < 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 45 | ▶ | my $every = (($self->interval_ms || 2000) * $workers) / 1000; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->interval_ms || 2000
MC/DC0%0/1
$self->interval_ms || 2000
$self->interval_ms
Truth table50%1/2
A
A= $self->interval_ms
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 45 | ▶ | return $every < 0.1 ? 0.1 : $every; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 19 | ▶ | if ($max_age_ms > 0 && $every * 1000 >= $max_age_ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$max_age_ms > 0 and $every * 1000 >= $max_age_ms
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $err = $@ || 'unknown error'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'unknown error'
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 751 | ▶ | sub for_node { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
for_node: called CC=15 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 751 | my ($class, $skeid, $node) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 751 | ▶ | return unless ref($node) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 751 | my $cfg = $node->{capacity}; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 751 | ▶ | return unless ref($cfg) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 29 | ▶ | my $kind = lc($cfg->{probe} // $cfg->{type} // 'inflight'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($cfg->{"probe"} // $cfg->{"type"}) // "inflight"
$cfg->{"probe"} // $cfg->{"type"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 29 | ▶ | return if $kind eq 'inflight' || $kind eq 'none' || $kind eq 'ratelimit'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$kind eq "inflight" or $kind eq "none" or $kind eq "ratelimit"
$kind eq "inflight" or $kind eq "none"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 27 | ▶ | if ($kind eq 'prometheus') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 18 | ▶ | if (ref($cfg->{code}) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 329 | ▶ | sub start_for_skeid { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_for_skeid: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 329 | my ($class, $skeid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 329 | my %probes; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 329 | for my $node (@{$skeid->nodes}) { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 736 | my $probe = eval { $class->for_node($skeid, $node) }; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 736 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $err = $@; $err =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | warn "capacity probe for '" . ($node->{id} // '?') . "' not started: $err"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$node->{"id"} // "?"
MC/DC0%0/1
$node->{"id"} // "?"
$node->{"id"}
Truth table0%0/2
A
A= $node->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 736 | ▶ | next unless $probe; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 15 | $probe->start; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 15 | $probes{$node->{id}} = $probe; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 329 | return \%probes; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 50 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Langertha::Skeid::CapacityProbe - Background probes that report what a node's real capacity is | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | version 0.003 | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | C<inflight> counts what B<this> process sent to a node. That is exact for one Skeid in front of | ||||||||||||||||||||||||||||||||||||||||||
| 206 | one node, and wrong the moment anything else sends work there — a second frontend, a prefork | ||||||||||||||||||||||||||||||||||||||||||
| 207 | worker, a batch job, an engineer with C<curl>. Each counter then sees only its own share, every | ||||||||||||||||||||||||||||||||||||||||||
| 208 | instance believes the node is emptier than it is, and together they over-admit. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | A probe reports what the node itself says instead. See ADR 0009 for why this rather than a | ||||||||||||||||||||||||||||||||||||||||||
| 211 | shared counter in Redis. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | =head2 Writing one | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | Subclass this, implement L</poll>, and report through L<Langertha::Skeid/set_capacity_reading>. | ||||||||||||||||||||||||||||||||||||||||||
| 216 | Two rules that are not negotiable: | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | =item * B<Never on the request path.> Polling is a timer. A probe that resolves during a | ||||||||||||||||||||||||||||||||||||||||||
| 221 | request has moved a network round-trip into the latency of a request that did not ask for it. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | =item * B<Report nothing rather than something old.> If the source cannot be reached, call | ||||||||||||||||||||||||||||||||||||||||||
| 224 | C<forget_capacity>. Admission falls back to C<inflight>, which is merely imprecise; a stale | ||||||||||||||||||||||||||||||||||||||||||
| 225 | reading is confidently wrong. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =head2 skeid | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | The control plane to report to. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =head2 node_id | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | Which node this probe describes. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | =head2 interval_ms | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | How often to poll (default 2000). The right value is a trade between staleness and load on the | ||||||||||||||||||||||||||||||||||||||||||
| 240 | node's metrics endpoint, and ADR 0009 leaves it open pending measurement — 2s is a starting | ||||||||||||||||||||||||||||||||||||||||||
| 241 | point, not a finding. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | =head2 config | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | The node's C<capacity> block, verbatim. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | =head2 is_stopped | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | True once L</stop> has run. A poll whose answer arrives after that (an HTTP request still in | ||||||||||||||||||||||||||||||||||||||||||
| 250 | flight at a restart) must not report: the node may be gone, or a new probe may already describe | ||||||||||||||||||||||||||||||||||||||||||
| 251 | it. A subclass that reports asynchronously checks this before it writes. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =head2 source | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | The C<source> this probe writes its readings under (C<prometheus>, C<registry>), or undef when | ||||||||||||||||||||||||||||||||||||||||||
| 256 | it does not have a fixed one -- a C<custom> callback picks its own. When the probe fails or | ||||||||||||||||||||||||||||||||||||||||||
| 257 | stops it forgets only a reading under this source, so a C<429> backoff the passive rate-limit | ||||||||||||||||||||||||||||||||||||||||||
| 258 | probe recorded outlives a probe that went blind (ADR 0017). Undef forgets whatever is there. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | =head2 poll | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | $probe->poll; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | What a subclass implements: read the source, then either C<set_capacity_reading> or | ||||||||||||||||||||||||||||||||||||||||||
| 265 | C<forget_capacity> on L</skeid>. Must not block. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =head2 poll_interval_seconds | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | How often this process actually polls: L</interval_ms> multiplied by the worker count. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | Every worker runs its own copy of the timer, so without the multiplier four workers on a 2s | ||||||||||||||||||||||||||||||||||||||||||
| 272 | interval would hit the node's metrics endpoint every 500ms — the probe becoming the load it | ||||||||||||||||||||||||||||||||||||||||||
| 273 | was meant to measure. What the operator configured is the rate the *node* sees from the | ||||||||||||||||||||||||||||||||||||||||||
| 274 | process group (ADR 0010). | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | $probe->start; | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | Begins polling on a timer, and polls once immediately so the first request does not have to | ||||||||||||||||||||||||||||||||||||||||||
| 281 | wait an interval for a reading. Safe to call twice. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | =head2 stop | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | Stops polling and drops the node's reading, so admission goes back to C<inflight> rather than | ||||||||||||||||||||||||||||||||||||||||||
| 286 | acting on whatever this probe last said. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =head2 for_node | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | my $probe = Langertha::Skeid::CapacityProbe->for_node($skeid, $node); | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | Builds the probe a node's C<capacity> block asks for, or nothing when it asks for none. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | capacity: | ||||||||||||||||||||||||||||||||||||||||||
| 295 | probe: prometheus # or: inflight, custom, registry | ||||||||||||||||||||||||||||||||||||||||||
| 296 | url: http://gpu-1:8000/metrics | ||||||||||||||||||||||||||||||||||||||||||
| 297 | interval_ms: 2000 | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | C<probe> may also be spelled C<type>. C<inflight> (the default; also C<none>, and an absent | ||||||||||||||||||||||||||||||||||||||||||
| 300 | block) means no probe object at all — that is the default admission path, not a probe that | ||||||||||||||||||||||||||||||||||||||||||
| 301 | reports the same thing. C<ratelimit> is likewise not built here: it is | ||||||||||||||||||||||||||||||||||||||||||
| 302 | passive, read off responses the proxy already has, and needs nothing running. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | C<registry> is for a node that is itself a Skeid: it pulls that Skeid's signed registry | ||||||||||||||||||||||||||||||||||||||||||
| 305 | snapshot (L<Langertha::Skeid::CapacityProbe::Registry>, ADR 0017). | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | C<custom> takes either a C<code> callback (given the probe, reports through the same methods) | ||||||||||||||||||||||||||||||||||||||||||
| 308 | or a C<class> to load, because Skeid is generic and the built-ins only cover the engines we | ||||||||||||||||||||||||||||||||||||||||||
| 309 | happen to know. The class must look like a Perl package name, is loaded by name and built | ||||||||||||||||||||||||||||||||||||||||||
| 310 | with C<skeid>, C<node_id>, C<config> and C<interval_ms>. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | C<interval_ms> in the block becomes L</interval_ms>. Croaks on an unknown probe, and on a | ||||||||||||||||||||||||||||||||||||||||||
| 313 | C<custom> block with neither C<code> nor a valid C<class>. | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =head2 start_for_skeid | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | my $probes = Langertha::Skeid::CapacityProbe->start_for_skeid($skeid); | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | Builds and starts a probe for every node that asks for one, and returns them by node id. The | ||||||||||||||||||||||||||||||||||||||||||
| 320 | caller holds them: a probe that goes out of scope stops polling. A node whose block does not | ||||||||||||||||||||||||||||||||||||||||||
| 321 | build is skipped with a warning, so one bad block does not stop the others. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =item * L<Langertha::Skeid::CapacityProbe::Prometheus>, L<Langertha::Skeid::CapacityProbe::Registry>, | ||||||||||||||||||||||||||||||||||||||||||
| 328 | L<Langertha::Skeid::CapacityProbe::Custom> | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =item * L<Langertha::Skeid/set_capacity_reading>, L<Langertha::Skeid/observe_response_headers> -- | ||||||||||||||||||||||||||||||||||||||||||
| 331 | the reading every probe reports, and the passive one | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 340 | L<https://github.com/Getty/langertha-skeid/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 359 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =cut | ||||||||||||||||||||||||||||||||||||||||||