| 1 | package Net::Hadoop::Oozie::SLA::Summary; | ||
| 2 | $Net::Hadoop::Oozie::SLA::Summary::VERSION = '0.113'; | ||
| 3 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Date::Parse 'str2time'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Net::Hadoop::Oozie; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use CHI; | ||
| 13 | use Scalar::Util 'reftype'; | ||
| 14 | |||
| 15 | my $CACHE_TTL = 300; | ||
| 16 | |||
| 17 | extends 'Net::Hadoop::Oozie::SLA'; | ||
| 18 | |||
| 19 | has use_cache => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => sub { die "use_cache should be 1/0" if $_[0] != 0 && $_[0] != 1 }, | ||
| 22 | default => sub { 0 }, | ||
| 23 | lazy => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has _cache => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => sub { return if !$_[0]; die "not a cache object" if ref $_[0] !~ /^CHI/ }, | ||
| 29 | default => sub { | ||
| 30 | my $self = shift; | ||
| 31 | return $self->use_cache | ||
| 32 | ? CHI->new( | ||
| 33 | driver => 'File', | ||
| 34 | root_dir => $self->cache_root_dir, | ||
| 35 | ) | ||
| 36 | : undef; | ||
| 37 | }, | ||
| 38 | lazy => 1, | ||
| 39 | ); | ||
| 40 | |||
| 41 | has cache_root_dir => ( | ||
| 42 | is => 'rw', | ||
| 43 | default => sub { '/tmp/oozie-sla-cache' }, | ||
| 44 | lazy => 1, | ||
| 45 | ); | ||
| 46 | |||
| 47 | has cache_id => ( | ||
| 48 | is => 'rw', | ||
| 49 | default => sub { 'hadoopX' }, | ||
| 50 | lazy => 1, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has oozie_class => ( | ||
| 54 | is => 'rw', | ||
| 55 | default => sub { 'Net::Hadoop::Oozie' }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | # filter by name: Net::Hadoop::Oozie::SLA->new->sla_miss_list(name => '^mysql.*') | ||
| 59 | # | ||
| 60 | sub sla_miss_list { | ||
| 61 | my $self = shift; | ||
| 62 | my %options = (@_ == 1 && reftype $_[0] eq 'HASH') ? %{$_[0]} : @_; | ||
| 63 | |||
| 64 | my $name_regex = $options{name}; | ||
| 65 | if ($name_regex) { | ||
| 66 | eval {qr/$options{name}/} or die '"name" option doesn\'t contain a valid regex'; | ||
| 67 | } | ||
| 68 | my $frozen_time = time; | ||
| 69 | my @time_ranges = ( | ||
| 70 | [ $frozen_time - 86400, $frozen_time, 'Last 24 hours' ], | ||
| 71 | [ $frozen_time - 259200, $frozen_time - 86400, '2 previous days' ], | ||
| 72 | [ $frozen_time - 604800, $frozen_time - 259200, '4 more days' ], | ||
| 73 | ); | ||
| 74 | $self->start( $frozen_time - 604800 ); | ||
| 75 | $self->end( $frozen_time ); | ||
| 76 | #$self->sla_status('MISS'); | ||
| 77 | |||
| 78 | my $coords = $self->oozie_class->new->jobs( | ||
| 79 | jobtype => "coordinators", | ||
| 80 | len => 10_000, | ||
| 81 | filter => { status => [ "RUNNING", "SUSPENDED" ] } | ||
| 82 | ); | ||
| 83 | $coords = [ map { $_->{coordJobName} } @{ $coords->{coordinatorjobs} || [] } ]; | ||
| 84 | |||
| 85 | # loop around the names, retrieving a series of SLA events for each | ||
| 86 | # TODO cache or make parallel | ||
| 87 | my ( @all_sla, $cache_hits, $cache_misses ); | ||
| 88 | my $start_fetch = time; | ||
| 89 | for my $app_name ( @{ $coords || [] } ) { | ||
| 90 | next if ($name_regex && $app_name !~ /$name_regex/); | ||
| 91 | my ( @sla, $from_cache, $cache_key ); | ||
| 92 | if ( $self->use_cache ) { | ||
| 93 | $cache_key = $self->cache_id . '#' . $app_name; | ||
| 94 | my $sla = $self->_cache->get($cache_key); | ||
| 95 | $from_cache = 1 if defined $sla; | ||
| 96 | $cache_hits++ if $from_cache; | ||
| 97 | $cache_misses++ if !$from_cache; | ||
| 98 | @sla = @{ $sla || [] }; | ||
| 99 | } | ||
| 100 | if ( !@sla ) { | ||
| 101 | $self->appname($app_name); | ||
| 102 | @sla = $self->sla(); | ||
| 103 | } | ||
| 104 | #print Dumper \@sla; | ||
| 105 | |||
| 106 | # cache even empty results | ||
| 107 | if ( !$from_cache && $self->use_cache ) { | ||
| 108 | $self->_cache->set( $cache_key, \@sla, $CACHE_TTL + int( rand(120) ) . " seconds" ); | ||
| 109 | } | ||
| 110 | next if !@sla; | ||
| 111 | push @all_sla, grep { $_->{slaMisses} } @sla; | ||
| 112 | } | ||
| 113 | $cache_hits //= 0; | ||
| 114 | $cache_misses //= 0; | ||
| 115 | #warn "cache hits: $cache_hits misses: $cache_misses time: " | ||
| 116 | # . ( time - $start_fetch ) . " sec"; | ||
| 117 | #print Dumper @all_sla; | ||
| 118 | |||
| 119 | @all_sla = sort { $b->{lastModifiedEpoch} <=> $a->{lastModifiedEpoch}} @all_sla; | ||
| 120 | |||
| 121 | # check the actual job status and add it to the structure | ||
| 122 | # my $oozie = Net::Hadoop::Oozie->new() | ||
| 123 | |||
| 124 | # group the SLA events by time period (2 hours, 1 day, 1 week) , | ||
| 125 | # coordinator, and action ID (as a same action can have different workflow | ||
| 126 | # IDs when it's launched several times; better see that by having 2 or more | ||
| 127 | # rows grouped under 1 ID) | ||
| 128 | my $grouped_sla; | ||
| 129 | for my $time_range (@time_ranges) { | ||
| 130 | my ($after, $before) = @$time_range; | ||
| 131 | my (%alerts, $latest); | ||
| 132 | for my $alert (@all_sla) { | ||
| 133 | |||
| 134 | next if !$alert->{parentId}; # for now, ignore those that are not run from a coordinator | ||
| 135 | |||
| 136 | # Not sure the lastModified field is a good indication of when | ||
| 137 | # the SLA event happened, but that's all we have in the JSON; | ||
| 138 | # let's try using that | ||
| 139 | if (!( $alert->{lastModifiedEpoch} >= $after | ||
| 140 | && $alert->{lastModifiedEpoch} < $before )) { | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | |||
| 144 | # they're already sorted, simply push them; since this will be used | ||
| 145 | # by React, we really want arrays, not hashes (you can't easily | ||
| 146 | # iterate on hash keys as you can here, since all hashes are | ||
| 147 | # objects in JS, which add plenty of keys from the prototype); | ||
| 148 | # we'll transform this multi-level hash in a series of nested | ||
| 149 | # arrays later on | ||
| 150 | push @{$alerts{$alert->{coordId}}{$alert->{parentId}}}, $alert; | ||
| 151 | |||
| 152 | $latest->{coord}{ $alert->{coordId} } = $alert->{lastModifiedEpoch} | ||
| 153 | if !$latest->{coord}{ $alert->{coordId} } | ||
| 154 | || $latest->{coord}{ $alert->{coordId} } < $alert->{lastModifiedEpoch}; | ||
| 155 | $latest->{action}{ $alert->{parentId} } = $alert->{lastModifiedEpoch} | ||
| 156 | if !$latest->{action}{ $alert->{parentId} } | ||
| 157 | || $latest->{action}{ $alert->{parentId} } < $alert->{lastModifiedEpoch}; | ||
| 158 | } | ||
| 159 | |||
| 160 | my @sorted_coord_ids | ||
| 161 | = sort { $latest->{coord}{$b} <=> $latest->{coord}{$a} } keys %{ $latest->{coord} }; | ||
| 162 | my @sorted_action_ids = sort { $latest->{action}{$b} <=> $latest->{action}{$a} } | ||
| 163 | keys %{ $latest->{action} }; | ||
| 164 | |||
| 165 | my $grouped_sla_part = { | ||
| 166 | time_range => $time_range, | ||
| 167 | coords => [ | ||
| 168 | map { # iterate coords | ||
| 169 | my $coord_id = $_; | ||
| 170 | { id => $coord_id, | ||
| 171 | actions => [ | ||
| 172 | map { # iterate actions | ||
| 173 | my $action_id = $_; | ||
| 174 | { id => $action_id, | ||
| 175 | alerts => [ @{ $alerts{$coord_id}{$action_id} } ] | ||
| 176 | } # populate alerts | ||
| 177 | } grep { $_ =~ /^$coord_id/ } @sorted_action_ids | ||
| 178 | ] | ||
| 179 | } | ||
| 180 | } @sorted_coord_ids | ||
| 181 | ] | ||
| 182 | }; | ||
| 183 | |||
| 184 | push @$grouped_sla, $grouped_sla_part; | ||
| 185 | } | ||
| 186 | |||
| 187 | return $grouped_sla; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ | ||