Oozie/TheJudge.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Oozie/Role/Common.pm ↑ Summary Oozie/SLA/Summary.pm »
1 package Net::Hadoop::Oozie::TheJudge;
2 $Net::Hadoop::Oozie::TheJudge::VERSION = '0.114';
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  
7 1 ▶use Constant::FromGlobal DEBUG => { int => 1, default => 0, env => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Hadoop::Oozie;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 has 'oozie' => (
12 is => 'rw',
13 default => sub {
14 Net::Hadoop::Oozie->new
15 },
16 lazy => 1,
17 );
18  
19 has 'badge' => (
20 is => 'rw',
21 default => sub { 'x' },
22 lazy => 1,
23 );
24  
25 has name => (
26 is => 'rw',
27 default => sub { 'TheJudge' },
28 );
29  
30 sub question {
31 my $self = shift;
32 my $opt = @_ > 1 ? {@_} : $_[0];
33 my $name = $self->name;
34  
35 my $oozie = $self->oozie;
36  
37 die 'Not an options hashref' if ref $opt ne 'HASH';
38  
39 # retrieve the last $len actions for a coordinator for analysis; this should be
40 # large enough allow the front of the queue to be discarded if there are many
41 # actions in READY state
42 ( my $len = $opt->{len} ) ||= 1000;
43  
44 # if we have no success in the last threshold(s) that actually ran, throw
45 # an alert
46 ( my $soft_limit = $opt->{suspend} ) ||= 10;
47 ( my $hard_limit = $opt->{kill} ) ||= 20;
48  
49 if ( $len < $soft_limit || $len < $hard_limit ) {
50 die "'len' should be higher than 'soft' and 'hard'; there's no point otherwise";
51 }
52  
53 if ( $soft_limit > $hard_limit ) {
54 die "'soft' should be lower than 'hard'; there's no point otherwise"
55 }
56  
57 my $job_id = $opt->{coord} || die "No coordinator ID!";
58  
59 my($job, $job_error);
60 eval {
61 $job = $oozie->job_exists(
62 $job_id,
63 {
64 len => $len,
65 order => 'desc',
66 },
67 );
68 1;
69 } or do {
70 $job_error = $@ || 'Zombie error';
71 };
72  
73 if ( ! $job ) {
74 warn sprintf 'Could not retrieve details for coord id %s. %s',
75 $job_id,
76 $job_error ? "Error: $job_error" : 'Job does not exist.',
77 ;
78 return;
79 }
80  
81 if ( $job->{status} eq 'KILLED' ) {
82 return { verdict => 'R.I.P. citizen' };
83 }
84  
85 my $actions = $job->{actions} || return { verdict => "free" };
86  
87 # take the actions in order; discard all the READY/PREP ones at the front of
88 # the queue, then check the rest
89 while ( my $ax = shift @$actions ) {
90 if ( $ax->{status} !~ /(READY|WAITING|PREP|RUNNING)/ ) {
91 unshift @$actions, $ax;
92 last;
93 }
94 }
95  
96 # keep $hard_limit elements, bail out if the first $soft_limit ones are not all
97 # KILLED
98 splice @$actions, $hard_limit;
99  
100 my $total_killed = grep { $_->{status} && $_->{status} eq 'KILLED' }
101 @{ $actions }[ 0 .. $soft_limit - 1 ]
102 ;
103 if ( $total_killed < $soft_limit ) {
104 DEBUG && printf STDERR "[%s] KILLED: %s < %s\t- [%s] %s (%s)\n",
105 $name,
106 $total_killed,
107 $soft_limit,
108 $self->badge,
109 $job_id,
110 $job->{coordJobName},
111 ;
112 return {};
113 }
114  
115 my $stats;
116  
117 for (@$actions) {
118 $stats->{ $_->{status} }++;
119 $stats->{total}++;
120 }
121  
122 my $sentence = $stats->{KILLED} == $hard_limit ? 'KILLED' : 'SUSPENDED';
123 my $out = sprintf "[%s] Coordinator %s (%s) should be %s\n",
124 $self->badge,
125 $job_id,
126 $job->{coordJobName},
127 $sentence,
128 ;
129  
130 $out .= sprintf "Latest %s actions are:\n", $stats->{total};
131  
132 for (qw(SUCCEEDED KILLED)) {
133 $out .= sprintf " %-10s: %.2f%%\n",
134 $_,
135 ( $stats->{$_} || 0 ) / $stats->{total} * 100,
136 ;
137 }
138  
139 for (@$actions) {
140 $out .= sprintf "action %s (%s) %s\n",
141 @{$_}{qw/ actionNumber lastModifiedTime status/ },
142 ;
143 }
144  
145 $out .= sprintf qq{\n\nOozie console:\n\n%s/?job=%s\n},
146 $oozie->oozie_uri,
147 $job_id,
148 ;
149  
150 return {
151 guilty => 1,
152 sentence => lc $sentence,
153 text => $out,
154 };
155  
156 # we could check the % KILLED and % SUCCEEDED over a longer period,
157 # optionally with a weight like (0.9 ^ days(time - lastModifiedEpoch))
158 }
159  
160 'Drokk';
161  
162 __END__
« Oozie/Role/Common.pm ↑ Summary Oozie/SLA/Summary.pm »