| 1 | package Punk::Observe::Evaluate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Punk::Observe (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Punk::Observe::Store (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Punk::Observe::Alert (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Punk::Observe::Config (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = $Punk::Observe::VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # The evaluation pass, in the distribution. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # THIS LOOP IS WHAT EVERY HOST USED TO WRITE, and the demo's copy is why it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # must not be written twice more: in one week its transaction had no rollback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # - a single bad write held the SQLite lock for the life of the process - and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # its INSERT omitted a NOT NULL column, so every transition died and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # events table had never held a row. Those were not demo bugs; they were the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # bugs any host would write. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # The shape mirrors Punk::Observe::Health: `run` is the pass over an explicit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # db and store so tests need neither a queue nor an app; `cron_task` wraps it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # in the queue's leader lock; `evaluate_job`/`notify_job` are the Punk::Queue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # task bodies, which receive ($job, @args) and nothing else and recover the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # plugin's state from the app class carried in the args. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # --- the tick transpose ----------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # A bucketed answer is series -> points; an evaluation is an instant with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # every series' value at it. Instants sort as STRINGS, width first - a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # nanosecond instant past 2^53 does not survive numeric comparison, and two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # buckets a microsecond apart would sort as equal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | sub _ticks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ticks: called CC=8 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | my ($res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 15 | my (%rows, %val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 15 | ▶ | for my $s (@{ $res->{series} || [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res->{'series'} || []
MC/DC0%0/1
$res->{'series'} || []
$res->{'series'}
Truth table50%1/2
A
A= $res->{'series'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 14 | ▶ | for my $p (@{ $s->{points} || [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->{'points'} || []
MC/DC0%0/1
$s->{'points'} || []
$s->{'points'}
Truth table50%1/2
A
A= $s->{'points'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 22 | ▶ | push @{ $rows{ $p->[0] } }, ($s->{key} // ''), $p->[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->{"key"} // ""
MC/DC0%0/1
$s->{"key"} // ""
$s->{"key"}
Truth table50%1/2
A
A= $s->{"key"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 22 | ▶ | $val{ $s->{key} // '' } = $p->[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->{"key"} // ""
MC/DC0%0/1
$s->{"key"} // ""
$s->{"key"}
Truth table50%1/2
A
A= $s->{"key"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | ▶ | my @at = sort { length($a) <=> length($b) || $a cmp $b } keys %rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
length $a <=> length $b || $a cmp $b
MC/DC0%0/2
length $a <=> length $b || $a cmp $b
length $a <=> length $b
$a cmp $b
Truth table33%1/3
A || B
A= length $a <=> length $bB= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | return ([ map { { at => $_, rows => $rows{$_} } } @at ], \%val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # --- the window ------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Derived from the rule, never fixed: `for` needs at least that much history | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # to be measured over, and a couple of `every`s of slack keeps the first tick | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # from being the boundary bucket. The demo's fixed hour read twelve times the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # data a 5-minute rule needed and not enough for a 40-minute one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my $WINDOW_FLOOR_NS = 900 * 1_000_000_000; # fifteen minutes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 24 | ▶ | sub _window_ns { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_window_ns: called CC=5 SCAR=17.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 24 | my ($rule) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 24 | ▶ | my $w = 2 * ($rule->{for_ns} || 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rule->{'for_ns'} || 0
MC/DC0%0/1
$rule->{'for_ns'} || 0
$rule->{'for_ns'}
Truth table50%1/2
A
A= $rule->{'for_ns'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 24 | ▶ | my $e = 4 * ($rule->{every_ns} || 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rule->{'every_ns'} || 0
MC/DC0%0/1
$rule->{'every_ns'} || 0
$rule->{'every_ns'}
Truth table50%1/2
A
A= $rule->{'every_ns'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | ▶ | $w = $e if $e > $w; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 24 | ▶ | $w = $WINDOW_FLOOR_NS if $w < $WINDOW_FLOOR_NS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | return $w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # --- what a transition means for delivery ----------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # The state machine's own vocabulary: 1 firing, 2 resolved, 3 resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # because the series vanished, 4 error. 0 is a move that pages nobody - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # ok to pending, pending back to ok, a vanished series returning - and a 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # writes the event and no notification. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | ▶ | sub _event_kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_event_kind: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 18 | my ($from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | ▶ | return 1 if $to eq 'firing'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 12 | ▶ | return 4 if $to eq 'error'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 7 | ▶ | return 3 if $to eq 'stale' && $from eq 'firing'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$to eq "stale" and $from eq "firing"
MC/DC100%2/2
$to eq "stale" and $from eq "firing"
$to eq "stale"
$from eq "firing"
Truth table100%3/3
A and B
A= $to eq "stale"B= $from eq "firing"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | return 2 if $to eq 'ok' && ($from eq 'firing' || $from eq 'error'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$to eq "ok" and $from eq "firing" || $from eq "error"
$from eq "firing" or $from eq "error"
MC/DC33%1/3
$to eq "ok" and $from eq "firing" || $from eq "error"
$to eq "ok"
$from eq "firing"
$from eq "error"
Truth table75%3/4
A and B or C
A= $to eq "ok"B= $from eq "firing"C= $from eq "error"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # --- one pass --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # run(db => $backend, store => $store, %opt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # now the clock, injectable for tests (ns string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # tenant evaluate one tenant; default is every tenant with rules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # group_wait_ns how long a group holds before its first send (default 30s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # repeat_ns re-notify a still-firing group this often (default 0, off) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # force evaluate rules even before next_eval_at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Returns { evaluated, transitions, enqueued, groups => [ {id, token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # rule_id} ] } - the claimed groups are handed back rather than delivered, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # because delivery belongs on the queue with its retries, and a test wants to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # see what WOULD be sent without sending it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 25 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=28 SCAR=33.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 25 | my (%opt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | ▶ | my $db = $opt{db} or Carp::croak('Evaluate::run needs db'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 25 | ▶ | my $store = $opt{store} or Carp::croak('Evaluate::run needs store'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 25 | my $dbh = $db->dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 25 | ▶ | my $now = defined $opt{now} ? $opt{now} : Punk::Observe::now_ns(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | my $wait = defined $opt{group_wait_ns} ? $opt{group_wait_ns} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 25 | ▶ | : 30_000_000_000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | my @tenants = defined $opt{tenant} ? ($opt{tenant}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 25 | ▶ | : map { $_->[0] } @{ $dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 'SELECT DISTINCT tenant FROM alert_rules WHERE enabled = 1') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 25 | my ($evaluated, $transitions) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 25 | for my $tenant (@tenants) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 25 | my $rules = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 'SELECT * FROM alert_rules WHERE tenant = ? AND enabled = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ORDER BY id', { Slice => {} }, $tenant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 25 | for my $r (@$rules) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Each rule keeps its own cadence inside the shared pass; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # pass interval is the resolution, the rule's `every` is honoured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # on top of it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | next if !$opt{force} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | && $r->{next_eval_at} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 25 | ▶ | && _ncmp($r->{next_eval_at}, $now) > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $opt{"force"} and $r->{"next_eval_at"} and _ncmp($r->{"next_eval_at"}, $now) > 0
not $opt{"force"} and $r->{"next_eval_at"}
MC/DC100%3/3
not $opt{"force"} and $r->{"next_eval_at"} and _ncmp($r->{"next_eval_at"}, $now) > 0
not $opt{"force"}
$r->{"next_eval_at"}
_ncmp($r->{"next_eval_at"}, $now) > 0
Truth table100%4/4
not(A) and B and C
A= not $opt{"force"}B= $r->{"next_eval_at"}C= _ncmp($r->{"next_eval_at"}, $now) > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 24 | my $from = Punk::Observe::Store::nsub($now, _window_ns($r)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 24 | my $res = eval { $store->query($r->{query}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 24 | from => $from, to => $now) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 24 | my $qerr = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # THE REASON IS CAPTURED AT BIRTH. Every failure used to collapse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # into an anonymous fail tick, and the screen could only count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # broken rules with nothing to say what was wrong with any of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 24 | my ($ticks, $latest, $reason); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 24 | ▶ | if ($res && $res->{ok} && ($res->{shape} || '') eq 'buckets') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition87%7/8
$res and $res->{"ok"} and ($res->{"shape"} || "") eq "buckets"
$res and $res->{"ok"}
$res->{'shape'} || ''
MC/DC66%2/3
$res and $res->{"ok"} and ($res->{"shape"} || "") eq "buckets"
$res
$res->{"ok"}
$res->{'shape'}
Truth table75%3/4
A and B and C
A= $resB= $res->{"ok"}C= $res->{'shape'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 15 | ($ticks, $latest) = _ticks($res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | ▶ | elsif (!$res) { $reason = _reason($qerr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_reason($qerr) || 'the query died with no message'
MC/DC0%0/1
_reason($qerr) || 'the query died with no message'
_reason($qerr)
Truth table50%1/2
A
A= _reason($qerr)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | || 'the query died with no message' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | elsif (!$res->{ok}) { $reason = _reason($res->{error}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | || 'the store refused the query' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_reason($res->{'error'}) || 'the store refused the query'
MC/DC0%0/1
_reason($res->{'error'}) || 'the store refused the query'
_reason($res->{'error'})
Truth table50%1/2
A
A= _reason($res->{'error'})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | $reason = "the query answered with shape '" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | ▶ | . ($res->{shape} || '') . "' - an alert evaluates a " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res->{'shape'} || ''
MC/DC0%0/1
$res->{'shape'} || ''
$res->{'shape'}
Truth table50%1/2
A
A= $res->{'shape'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | . 'bucketed answer; add a bucket stage to the query'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # A QUERY THAT DID NOT ANSWER IS A FAIL TICK, not a hand-written | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # error state. The machine's own latch then notifies once and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # re-arms on the next success - the demo bypassed this and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # re-derived that logic in SQL, wrongly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # AN EMPTY SUCCESS IS NOT A FAILURE. A query that answered with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # no series in the window is a rule whose every series vanished - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | # the machine's stale semantics, not its error latch - so no fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # tick is minted and the reconcile below retires the rows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 24 | ▶ | $ticks = [ { at => $now, fail => 1 } ] if !$ticks || $reason; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ticks or $reason
MC/DC50%1/2
not $ticks or $reason
not $ticks
$reason
Truth table66%2/3
not(A) or B
A= not $ticksB= $reason
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 24 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 24 | ▶ | if (@$ticks) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 23 | $out = eval { Punk::Observe::Alert::run( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | { op => $r->{op}, threshold => $r->{threshold}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | for => $r->{for_ns}, every => $r->{every_ns} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 23 | $ticks) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 23 | ▶ | next unless $out && @$out; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$out and @$out
MC/DC0%0/2
$out and @$out
$out
@$out
Truth table33%1/3
A and B
A= $outB= @$out
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 23 | ▶ | $transitions += _record($dbh, $r, $out->[-1], $latest || {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$latest || {}
MC/DC100%1/1
$latest || {}
$latest
Truth table100%2/2
A
A= $latest
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | $now, $wait, $reason); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # RETIRE WHAT THIS EVALUATION DID NOT MENTION. The machine stales | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # a series that vanishes within one run, but a series absent from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # the whole window - including the synthetic `all` a fail tick | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # once recorded - was never mentioned again, so its row was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # immortal: an error state outliving its cause by days. Only a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # successful evaluation says anything about liveness, so a fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # tick reconciles nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | $transitions += _reconcile($dbh, $r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 24 | ▶ | $out ? $out->[-1]{states} : [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | $now, $wait) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | unless $reason; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | $dbh->do('UPDATE alert_rules SET next_eval_at = ? WHERE id = ?', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | undef, Punk::Observe::Store::nadd($now, $r->{every_ns}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 24 | $r->{id}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 24 | $evaluated++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 25 | my ($groups, $enqueued) = _flush($dbh, $now, \%opt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 25 | return { evaluated => $evaluated, transitions => $transitions, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | enqueued => $enqueued, groups => $groups }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # Decimal-string comparison for ns instants: width first, then lexical. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | sub _ncmp { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ncmp: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | my ($a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ($a, $b) = ("$a", "$b"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | ▶ | return length($a) <=> length($b) || $a cmp $b; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
length $a <=> length $b || $a cmp $b
MC/DC0%0/2
length $a <=> length $b || $a cmp $b
length $a <=> length $b
$a cmp $b
Truth table33%1/3
A || B
A= length $a <=> length $bB= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # A reason fit for a screen: stringified, stripped of the croak location - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # which names this module's line, not the rule's problem - and bounded, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # store error carrying a request body does not become a TEXT column of it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 8 | ▶ | sub _reason { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reason: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 8 | my ($e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 8 | ▶ | return undef unless defined $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 8 | $e = "$e"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | $e =~ s/ at \S+ line \d+\.?\s*\z//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 8 | $e =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 8 | ▶ | return undef unless length $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 8 | ▶ | $e = substr($e, 0, 497) . '...' if length($e) > 500; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 8 | return $e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # --- recording -------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # State, event and outbox row in ONE transaction with a rollback guard. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # The transaction is the idempotency: a pass that dies here leaves the old | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # state, and the re-run re-derives the same transition. The guard is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # lesson - a die between begin_work and commit with nothing to roll it back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # left the demo's evaluator holding the write lock while it slept, and every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # other writer in the application got "database is locked" with nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # visibly contending. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 24 | ▶ | sub _record { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_record: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 24 | my ($dbh, $rule, $last, $latest, $now, $wait, $reason) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 24 | my $moved = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 24 | ▶ | for my $s (@{ $last->{states} || [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$last->{'states'} || []
MC/DC0%0/1
$last->{'states'} || []
$last->{'states'}
Truth table50%1/2
A
A= $last->{'states'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | my $key = defined $s->{series} && length $s->{series} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 24 | ▶ | ? $s->{series} : 'all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $s->{"series"} and length $s->{"series"}
MC/DC50%1/2
defined $s->{"series"} and length $s->{"series"}
defined $s->{"series"}
length $s->{"series"}
Truth table66%2/3
A and B
A= defined $s->{"series"}B= length $s->{"series"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 24 | ▶ | my $val = $latest->{ $s->{series} // '' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->{"series"} // ""
MC/DC0%0/1
$s->{"series"} // ""
$s->{"series"}
Truth table50%1/2
A
A= $s->{"series"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 24 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | $moved += _record_one($dbh, $rule, $key, $s->{state}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 24 | $s->{since}, $s->{fired_at}, $val, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | $now, $wait, $reason); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 24 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | eval { $dbh->rollback }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 23 | return $moved; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # Rows this evaluation did not mention. An error row - the synthetic `all` a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # fail tick records - resolves once the query answers again, which is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # machine's own re-arm carried across passes; anything else goes stale, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # is the machine's own vanish carried across passes. A stale row stays as it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # is, and a row the machine spoke for this pass is not touched. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 15 | ▶ | sub _reconcile { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reconcile: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 15 | my ($dbh, $rule, $states, $now, $wait) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 15 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 15 | ▶ | for my $s (@{ $states || [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$states || []
MC/DC0%0/1
$states || []
$states
Truth table50%1/2
A
A= $states
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | my $key = defined $s->{series} && length $s->{series} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 14 | ▶ | ? $s->{series} : 'all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $s->{"series"} and length $s->{"series"}
MC/DC50%1/2
defined $s->{"series"} and length $s->{"series"}
defined $s->{"series"}
length $s->{"series"}
Truth table66%2/3
A and B
A= defined $s->{"series"}B= length $s->{"series"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 14 | $seen{$key} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | my $rows = $dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 'SELECT series, state FROM alert_state WHERE rule_id = ?', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 15 | { Slice => {} }, $rule->{id}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 15 | my $moved = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 15 | for my $row (@$rows) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 19 | ▶ | next if $seen{ $row->{series} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 5 | ▶ | next if $row->{state} eq 'stale'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | my $to = $row->{state} eq 'error' ? 'ok' : 'stale'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 4 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 4 | $moved += _record_one($dbh, $rule, $row->{series}, $to, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | $now, undef, undef, $now, $wait); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 4 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | eval { $dbh->rollback }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 15 | return $moved; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 28 | ▶ | sub _record_one { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_record_one: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 28 | my ($dbh, $rule, $series, $new, $since, $fired, $value, $now, $wait, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | $reason) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 28 | $dbh->begin_work; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | my $prev = $dbh->selectrow_hashref( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 'SELECT state FROM alert_state WHERE rule_id = ? AND series = ?', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 28 | undef, $rule->{id}, $series); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 28 | ▶ | my $from = $prev ? $prev->{state} : 'ok'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | $dbh->do(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | INSERT INTO alert_state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | (rule_id, series, state, since, fired_at, last_seen, last_value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | reason) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | VALUES (?,?,?,?,?,?,?,?) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ON CONFLICT (rule_id, series) DO UPDATE SET | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | state = excluded.state, since = excluded.since, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | fired_at = excluded.fired_at, last_seen = excluded.last_seen, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | last_value = excluded.last_value, reason = excluded.reason | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 28 | ▶ | }, undef, $rule->{id}, $series, $new, $since || 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition60%3/5
$since || 0
$fired or $now
MC/DC100%1/1
$since || 0
$since
MC/DC0%0/2
$fired or $now
$fired
$now
Truth table100%2/2
A
A= $since
Truth table33%1/3
A or B
A= $firedB= $now
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | ($new eq 'firing' ? ($fired || $now) : undef), $now, $value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | ($new eq 'error' ? $reason : undef)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 27 | ▶ | if ($from ne $new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 18 | my $kind = _event_kind($from, $new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | $dbh->do(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | INSERT INTO alert_events | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | (rule_id, series, kind, from_state, to_state, value, at) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | VALUES (?,?,?,?,?,?,?) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 18 | }, undef, $rule->{id}, $series, $kind, $from, $new, $value, $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Kind 0 pages nobody; anything else joins the outbox. The unique | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | # key is THE TRANSITION ITSELF - (rule, series, fired_at, at) - so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # concurrent leader inserting the same one is refused, and a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # resolve-and-refire, carrying a new fired_at and a new at, is a new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # notification rather than a duplicate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 18 | ▶ | if ($kind) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | $dbh->do(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | INSERT INTO notifications | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | (rule_id, series, kind, value, at, fired_at, created_at) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | VALUES (?,?,?,?,?,?,?) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | ON CONFLICT DO NOTHING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 17 | ▶ | }, undef, $rule->{id}, $series, $kind, $value, $now, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$fired or $now
$fired or 0
MC/DC0%0/2
$fired or $now
$fired
$now
MC/DC0%0/1
$fired or 0
$fired
Truth table33%1/3
A or B
A= $firedB= $now
Truth table50%1/2
A
A= $fired
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | ($new eq 'firing' ? ($fired || $now) : ($fired || 0)), $now); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 17 | _attach_to_group($dbh, $rule->{id}, $now, $wait); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 27 | $dbh->commit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 27 | ▶ | return $from ne $new ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # The rule's open group, made if there is none. The partial unique index - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # at most one open group per rule - makes two racing leaders collapse onto | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # one row, and makes a member arriving after a flush open a NEW group, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # is "the forty-first service is a second notification" as schema rather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # than as code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 17 | ▶ | sub _attach_to_group { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_to_group: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 17 | my ($dbh, $rule_id, $now, $wait) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 17 | my ($gid) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | "SELECT id FROM notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | WHERE rule_id = ? AND state = 'open'", undef, $rule_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 17 | ▶ | unless ($gid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 12 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | INSERT INTO notification_groups (rule_id, opened_at, due_at) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | VALUES (?,?,?) ON CONFLICT DO NOTHING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | }, undef, $rule_id, $now, Punk::Observe::Store::nadd($now, $wait)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 12 | ($gid) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | "SELECT id FROM notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | WHERE rule_id = ? AND state = 'open'", undef, $rule_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 17 | ▶ | $dbh->do('UPDATE notifications SET group_id = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | WHERE rule_id = ? AND group_id IS NULL AND sent_at IS NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | AND dead = 0', undef, $gid, $rule_id) if $gid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 17 | return $gid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # --- the flush -------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # Claims due groups and reopens stale claims. Returns the claimed groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | # rather than delivering them: delivery belongs on the queue. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | my $LEASE_NS = 900 * 1_000_000_000; # a claim older than this is a crash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 25 | ▶ | sub _flush { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flush: called CC=6 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 25 | my ($dbh, $now, $opt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 25 | ▶ | my $repeat = $opt->{repeat_ns} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt->{'repeat_ns'} || 0
MC/DC0%0/1
$opt->{'repeat_ns'} || 0
$opt->{'repeat_ns'}
Truth table50%1/2
A
A= $opt->{'repeat_ns'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | # A claim nobody followed up is a leader that died between claim and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # enqueue. Reopened loudly - due now - rather than left to rot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 25 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | UPDATE notification_groups SET state = 'open', due_at = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | WHERE state = 'claimed' AND claimed_at <= ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | }, undef, $now, Punk::Observe::Store::nsub($now, $LEASE_NS)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # A sent group whose rule is firing again past repeat_interval re-enters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # the queue; one whose rule went quiet retires instead of re-paging. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 25 | ▶ | if ($repeat) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | UPDATE notification_groups SET state = 'open', due_at = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | WHERE state = 'sent' AND next_repeat_at IS NOT NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | AND next_repeat_at <= ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | AND EXISTS (SELECT 1 FROM alert_state s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | WHERE s.rule_id = notification_groups.rule_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | AND s.state IN ('firing','error')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | }, undef, $now, $now); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | UPDATE notification_groups SET next_repeat_at = NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | WHERE state = 'sent' AND next_repeat_at IS NOT NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | AND next_repeat_at <= ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | AND NOT EXISTS (SELECT 1 FROM alert_state s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | WHERE s.rule_id = notification_groups.rule_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | AND s.state IN ('firing','error')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | }, undef, $now); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 25 | my $due = $dbh->selectall_arrayref(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | SELECT id, rule_id FROM notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | WHERE state = 'open' AND due_at <= ? ORDER BY id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | }, { Slice => {} }, $now); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 25 | my (@claimed, $enqueued); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 25 | for my $g (@$due) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # Single-winner claim: rows-affected zero means another leader took | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | # it, which is a loss it is allowed to have. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | my $n = $dbh->do(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | UPDATE notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | SET state = 'claimed', claimed_at = ?, attempts = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | WHERE id = ? AND state = 'open' AND due_at <= ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 9 | }, undef, $now, $g->{id}, $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 9 | ▶ | next unless $n && $n != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | push @claimed, { id => $g->{id}, rule_id => $g->{rule_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 9 | token => $now }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 25 | return (\@claimed, scalar @claimed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | # --- delivery --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # notify(db => $backend, group => $id, token => $t, on_alert => sub {...}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # prefix => '/observe', app => $class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # Loads the claimed group, suppresses silenced members, builds ONE callback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # invocation, marks sent. Returns what happened rather than hiding it: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # { status => 'sent' | 'stale' | 'empty', ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # A stale token - the group was reaped and re-claimed while this job sat in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # the queue - is a LOUD NO-OP, not a die: dying would spend five retries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # delivering nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 3 | ▶ | sub notify { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
notify: called CC=20 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 3 | my (%opt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 3 | ▶ | my $db = $opt{db} or Carp::croak('Evaluate::notify needs db'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 3 | my $dbh = $db->dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 3 | ▶ | my $gid = $opt{group} or Carp::croak('Evaluate::notify needs group'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 3 | ▶ | my $now = defined $opt{now} ? $opt{now} : Punk::Observe::now_ns(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 3 | my $g = $dbh->selectrow_hashref(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | SELECT g.*, r.tenant, r.name, r.query, r.op, r.threshold, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | r.for_ns, r.every_ns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | FROM notification_groups g JOIN alert_rules r ON r.id = g.rule_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | WHERE g.id = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | }, undef, $gid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | return { status => 'stale' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | unless $g && $g->{state} eq 'claimed' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 3 | ▶ | && defined $opt{token} && "$g->{claimed_at}" eq "$opt{token}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$g and $g->{"state"} eq "claimed" and defined $opt{"token"} and qq[$g->{"claimed_at"}] eq qq[$opt{"token"}]
$g and $g->{"state"} eq "claimed" and defined $opt{"token"}
$g and $g->{"state"} eq "claimed"
MC/DC25%1/4
$g and $g->{"state"} eq "claimed" and defined $opt{"token"} and qq[$g->{"claimed_at"}] eq qq[$opt{"token"}]
$g
$g->{"state"} eq "claimed"
defined $opt{"token"}
qq[$g->{"claimed_at"}] eq qq[$opt{"token"}]
Truth table40%2/5
A and B and C and D
A= $gB= $g->{"state"} eq "claimed"C= defined $opt{"token"}D= qq[$g->{"claimed_at"}] eq qq[$opt{"token"}]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 2 | my $members = $dbh->selectall_arrayref(q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | SELECT id, series, kind, value, at, fired_at FROM notifications | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | WHERE group_id = ? AND sent_at IS NULL AND dead = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | ORDER BY at, id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | }, { Slice => {} }, $gid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | # SILENCES ARE CHECKED HERE, AT DELIVERY, not at insert - a silence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # created after a member was enqueued but before its group came due must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | # still suppress it. Suppression is RECORDED on the row, so the screen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # can say why nobody was paged; the state machine never hears of it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | my $sil = $dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 'SELECT pattern, is_prefix FROM silences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | WHERE tenant = ? AND until > ?', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 2 | { Slice => {} }, $g->{tenant}, $now); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | my (@send, @quiet); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 2 | for my $m (@$members) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 2 | my $key = "$g->{name}/$m->{series}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 2 | ▶ | if (grep { Punk::Observe::Config::silence_match( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 1 | @{$_}{qw(pattern is_prefix)}, $key) } @$sil) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 1 | push @quiet, $m->{id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 1 | else { push @send, $m } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 2 | ▶ | $dbh->do('UPDATE notifications SET suppressed = 1 WHERE id IN (' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | . join(',', ('?') x @quiet) . ')', undef, @quiet) if @quiet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 2 | my %kinds = (1 => 'firing', 2 => 'resolved', 3 => 'vanished', 4 => 'error'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 2 | my $sent = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 2 | ▶ | if (@send && $opt{on_alert}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@send and $opt{"on_alert"}
MC/DC50%1/2
@send and $opt{"on_alert"}
@send
$opt{"on_alert"}
Truth table66%2/3
A and B
A= @sendB= $opt{"on_alert"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | # THE CALLBACK IS THE DELIVERY SEAM. Everything the host needs to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # build a webhook or an email is in the one argument, values raw - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # nanoseconds where the query measured time - because formatting is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # the host's, and `delivery_key` is the idempotency token for a host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # that wants exactly-once on its own side of an at-least-once queue. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | my $event = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | app => $opt{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | tenant => $g->{tenant}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 7 | ▶ | rule => { map { $_ => $g->{$_} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$kinds{$send[0]{'kind'}} || 'firing'
MC/DC0%0/1
$kinds{$send[0]{'kind'}} || 'firing'
$kinds{$send[0]{'kind'}}
Truth table50%1/2
A
A= $kinds{$send[0]{'kind'}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | qw(rule_id name query op threshold for_ns every_ns) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | kind => $kinds{ $send[0]{kind} } || 'firing', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | series => [ map { { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | series => $_->{series}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | kind => $kinds{ $_->{kind} } || '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | value => $_->{value}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 1 | ▶ | at => "$_->{at}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$kinds{$_->{'kind'}} || ''
MC/DC0%0/1
$kinds{$_->{'kind'}} || ''
$kinds{$_->{'kind'}}
Truth table50%1/2
A
A= $kinds{$_->{'kind'}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | fired_at => "$_->{fired_at}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | } } @send ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | count => scalar @send, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | at => "$now", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | path => defined $opt{prefix} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | ? "$opt{prefix}/alerts/$g->{rule_id}" : undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 1 | delivery_key => 'g' . $gid . '.s' . ($g->{sends} + 1), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 1 | $event->{rule}{id} = delete $event->{rule}{rule_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | $opt{on_alert}->($event); # a die propagates: the queue retries | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 1 | $sent = scalar @send; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | # Mark-sent is a CAS on the claim token, AFTER the callback: delivery is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # at-least-once, and the alternative - marking first - is at-most-once, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # which is a lost page. The POD says so, and delivery_key is the remedy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | my $n = $dbh->do(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | UPDATE notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | SET state = 'sent', sent_at = COALESCE(sent_at, ?), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | last_sent_at = ?, sends = sends + 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | next_repeat_at = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | WHERE id = ? AND state = 'claimed' AND claimed_at = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | }, undef, $now, $now, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | ($opt{repeat_ns} ? Punk::Observe::Store::nadd($now, $opt{repeat_ns}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | : undef), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 2 | ▶ | $gid, $opt{token}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 2 | ▶ | $dbh->do('UPDATE notifications SET sent_at = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | WHERE group_id = ? AND sent_at IS NULL AND suppressed = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | AND dead = 0', undef, $now, $gid) if $n && $n != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 2 | ▶ | return { status => (@send ? 'sent' : 'empty'), delivered => $sent, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | suppressed => scalar @quiet }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # --- the queue entry points ------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | # Both bodies recover the plugin's state from the app class in the args - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # the Mailer pattern, because a task body receives ($job, @args) and nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | # else, and the worker compiled the same app class so registration has run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | ▶ | sub _plugin_state { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plugin_state: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | require Punk::Plugin::Observe; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | ▶ | my $st = Punk::Plugin::Observe->state_for($class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | or Carp::croak("Punk::Observe::Evaluate: no Observe plugin state for " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | . "$class - is the worker running the same application " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | . "class as the server?"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | return $st; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | ▶ | sub evaluate_job { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate_job: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | my ($job, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | my $st = _plugin_state($class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | my $q = $job->queue_object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # The queue's cron dedupe is per occurrence, but execution is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # at-least-once and workers are many: the lease is what makes the pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | # single-flight. Losing it is a result, not an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | my $owner = 0 + $$; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | ▶ | return { skipped => 'lock' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | unless $q->lock('observe.evaluate', 60, owner => $owner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | my $out = eval { run( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | db => $st->{db}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | store => Punk::Plugin::Observe::store_for($st, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | $st->{tenant}{fixed} || 'default'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | group_wait_ns => $st->{alerts_opts}{group_wait_ns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | repeat_ns => $st->{alerts_opts}{repeat_ns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | ▶ | ) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$st->{'tenant'}{'fixed'} || 'default'
MC/DC0%0/1
$st->{'tenant'}{'fixed'} || 'default'
$st->{'tenant'}{'fixed'}
Truth table0%0/2
A
A= $st->{'tenant'}{'fixed'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | ▶ | for my $g (@{ $out ? $out->{groups} : [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | $q->enqueue('observe.notify' => [ $class, $g->{id}, "$g->{token}" ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | ▶ | $q->unlock('observe.evaluate', $owner) if $q->can('unlock'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | return { evaluated => $out->{evaluated}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | transitions => $out->{transitions}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | enqueued => $out->{enqueued} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | sub notify_job { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
notify_job: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | my ($job, $class, $gid, $token) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | my $st = _plugin_state($class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | my $out = eval { notify( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | db => $st->{db}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | group => $gid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | token => $token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | on_alert => $st->{opts}{on_alert}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | repeat_ns => $st->{alerts_opts}{repeat_ns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | prefix => $st->{prefix}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | app => $class, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | ) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | # The attempt count and the error land on the GROUP, where the screen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | # can show them; the final failure is a dead letter, recorded and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | # loud, never dropped - and the next fire opens a fresh group, so one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | # unreachable webhook does not stop alerting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | my $dbh = $st->{db}->dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my $last = ($job->retries + 1) >= 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | $dbh->do('UPDATE notification_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | SET attempts = attempts + 1, last_error = ?' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | . ($last ? ", state = 'dead'" : '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | . ' WHERE id = ?', undef, "$err", $gid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | ▶ | $dbh->do("UPDATE notifications SET dead = 1, last_error = ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | WHERE group_id = ? AND sent_at IS NULL", undef, "$err", $gid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | if $last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | # cron_task(%opt) - the Health-shaped closure for a host that declares its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | # own cron rather than letting the plugin do it. Takes the queue, returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | # the transition count, dies on error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 0 | ▶ | sub cron_task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cron_task: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | my (%opt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | ▶ | my $lease = $opt{lease_seconds} || 60; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'lease_seconds'} || 60
MC/DC0%0/1
$opt{'lease_seconds'} || 60
$opt{'lease_seconds'}
Truth table0%0/2
A
A= $opt{'lease_seconds'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | ▶ | my $owner = defined $opt{owner} ? 0 + $opt{owner} : 0 + $$; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | my ($q) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | ▶ | return 0 unless $q->lock('observe.evaluate', $lease, owner => $owner); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | my $out = eval { run(%opt) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | ▶ | if ($out && $opt{on_alert}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$out and $opt{"on_alert"}
MC/DC0%0/2
$out and $opt{"on_alert"}
$out
$opt{"on_alert"}
Truth table0%0/3
A and B
A= $outB= $opt{"on_alert"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | for my $g (@{ $out->{groups} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | my $n = eval { notify(%opt, group => $g->{id}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | token => "$g->{token}") }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | ▶ | $err ||= $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err ||= $@
MC/DC0%0/2
$err ||= $@
$err
$@
Truth table0%0/3
A ||= B
A= $errB= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | ▶ | $q->unlock('observe.evaluate', $owner) if $q->can('unlock'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | ▶ | return $out ? $out->{transitions} : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||