blib/lib/App/karr/Foundation/Limits.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 124 CC 11 SCAR 27.2
CC
11
Cov
0%
_build_per_agent
30.0
_count
20.0
_build_chain_limits
6.0
CRAP 15.1
1 # ABSTRACT: karr-foundation concurrency limits -- machine ceiling, per-agent estimates, chain header
2  
3 package App::karr::Foundation::Limits;
4 our $VERSION = '0.602';
5 use Moo;
6 use Try::Tiny;
7 use App::karr::Error qw( user_error clean_error );
8  
9  
10  
11 has foundation => (
12 is => 'ro',
13 weak_ref => 1,
14 required => 1,
15 );
16  
17  
18 has chain_limits => (
19 is => 'lazy',
20 builder => '_build_chain_limits',
21 );
22  
23 sub _build_chain_limits {
24 my ( $self ) = @_;
25 my $store = $self->foundation->_chain_store or return {};
26 my $limits = try {
27 $self->foundation->_chain_store->header->{limits};
28 } catch {
29 warn "karr-foundation: cannot read the chain header: " . clean_error($_) . "\n";
30 undef;
31 };
32 return ref $limits eq 'HASH' ? $limits : {};
33 }
34  
35  
36 has concurrent => (
37 is => 'lazy',
38 builder => '_build_concurrent',
39 );
40  
41 sub _build_concurrent {
42 my ( $self ) = @_;
43 my $machine = _count( "Config 'concurrent'",
44 $self->foundation->_config_data->{concurrent} ) // 1;
45 my $chain = $self->_soft_count( "The chain header's 'concurrent'",
46 $self->chain_limits->{concurrent} );
47 return defined $chain && $chain < $machine ? $chain : $machine;
48 }
49  
50  
51 has per_agent => (
52 is => 'lazy',
53 builder => '_build_per_agent',
54 );
55  
56 sub _build_per_agent {
57 my ( $self ) = @_;
58 my $agents = $self->foundation->_agents;
59 my $defs = $agents->definitions;
60  
61 my %limit;
62 for my $name ( sort keys %$defs ) {
63 my $n = _count( "Agent '$name' concurrent", $defs->{$name}{concurrent} );
64 $limit{$name} = $n if defined $n;
65 }
66  
67 my $chain = $self->chain_limits->{per_agent};
68 if ( defined $chain && ref $chain ne 'HASH' ) {
69 warn "karr-foundation: the chain header's 'per_agent' is not a mapping "
70 . "of agent name => count -- ignored\n";
71 $chain = undef;
72 }
73 for my $name ( sort keys %{ $chain // {} } ) {
74 # The names here are agent definitions, so this machine's definitions are
75 # what they are checked against. Unknown is not an error (see DESCRIPTION):
76 # the chain is shared, the agent list is not.
77 unless ( $defs->{$name} ) {
78 $self->foundation->_say_verbose(
79 "chain limit for agent '$name' ignored -- not defined on this machine" );
80 next;
81 }
82 my $n = $self->_soft_count( "The chain header's limit for agent '$name'",
83 $chain->{$name} ) // next;
84 $limit{$name} = $n if !defined $limit{$name} || $n < $limit{$name};
85 }
86 return \%limit;
87 }
88  
89 # A count as a config writes one: a positive whole number. undef passes through
90 # as "not set"; anything else is refused rather than rounded, because a
91 # concurrent: 2.5 that silently became 2 -- or a concurrent: 0 that silently
92 # became "never run anything" -- is a configuration that lies to its operator.
93 sub _count {
94 my ( $what, $value ) = @_;
95 return undef unless defined $value;
96 user_error("$what must be a positive whole number, not a structure")
97 if ref $value;
98 my $v = "$value";
99 $v =~ s/\A\s+//;
100 $v =~ s/\s+\z//;
101 user_error("$what must be a positive whole number, not '$value'")
102 unless $v =~ /\A[0-9]+\z/ && $v + 0 > 0;
103 return $v + 0;
104 }
105  
106 # The same for a value that came out of the chain header rather than out of
107 # this machine's config. It warns and yields undef instead of dying: the header
108 # was written elsewhere, and one bad number in it must not stop this machine
109 # from running at its own ceiling.
110 sub _soft_count {
111 my ( $self, $what, $value ) = @_;
112 return undef unless defined $value;
113 return try {
114 _count( $what, $value );
115 } catch {
116 warn "karr-foundation: " . clean_error($_) . " -- ignored\n";
117 undef;
118 };
119 }
120  
121 1;
122  
123 __END__