| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Hadoop::YARN::ResourceManager; | 
| 2 |  |  |  |  |  |  | $Net::Hadoop::YARN::ResourceManager::VERSION = '0.201'; | 
| 3 | 4 |  |  | 4 |  | 30164 | use strict; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 100 |  | 
| 4 | 4 |  |  | 4 |  | 13 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 84 |  | 
| 5 | 4 |  |  | 4 |  | 39 | use 5.10.0; | 
|  | 4 |  |  |  |  | 10 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 14 | use Data::Dumper; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 187 |  | 
| 8 | 4 |  |  | 4 |  | 657 | use Moo; | 
|  | 4 |  |  |  |  | 15264 |  | 
|  | 4 |  |  |  |  | 21 |  | 
| 9 | 4 |  |  |  |  | 264 | use Ref::Util qw( | 
| 10 |  |  |  |  |  |  | is_ref | 
| 11 |  |  |  |  |  |  | is_arrayref | 
| 12 |  |  |  |  |  |  | is_hashref | 
| 13 | 4 |  |  | 4 |  | 3705 | ); | 
|  | 4 |  |  |  |  | 1643 |  | 
| 14 | 4 |  |  |  |  | 3653 | use Scalar::Util qw( | 
| 15 |  |  |  |  |  |  | refaddr | 
| 16 | 4 |  |  | 4 |  | 35 | ); | 
|  | 4 |  |  |  |  | 6 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | with 'Net::Hadoop::YARN::Roles::Common'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has '+servers' => ( default => sub { ["localhost:8088"] }, ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has '+add_host_key' => ( default => sub { 1 } ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub active_rm { | 
| 27 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 28 | 0 | 0 |  |  |  |  | my $opt  = is_hashref $_[0] ? shift @_ : {}; | 
| 29 | 0 |  |  |  |  |  | my $rv; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 0 |  |  |  |  |  | foreach my $server ( @{ $self->servers } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | my $info = $self->info({ server => $server }); | 
| 33 | 0 |  | 0 |  |  |  | my $haState = $info->{haState} || next; | 
| 34 | 0 | 0 |  |  |  |  | if ( $haState eq 'ACTIVE' ) { | 
| 35 | 0 |  |  |  |  |  | $rv = $server; | 
| 36 | 0 |  |  |  |  |  | last; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 |  |  |  |  | if ( ! $rv ) { | 
| 41 |  |  |  |  |  |  | die sprintf "Failed to locate the active YARN Resource Manager from these hosts: %s", | 
| 42 | 0 |  |  |  |  |  | join( q{, }, @{ $self->servers } ), | 
|  | 0 |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | ; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 | 0 |  |  |  |  | if ( $opt->{hostname_only} ) { | 
| 47 | 0 |  |  |  |  |  | return +( split m{[:]}xms, $rv )[0]; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | return $rv; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub info { | 
| 55 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 56 | 0 | 0 |  |  |  |  | my $opt  = is_hashref $_[0] ? shift @_ : {}; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | my $res = $self->_get( | 
| 59 |  |  |  |  |  |  | "cluster/info", | 
| 60 |  |  |  |  |  |  | undef, | 
| 61 | 0 |  | 0 |  |  |  | ( $opt->{server} or () ), | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 65 |  |  |  |  |  |  | $res, | 
| 66 | 0 |  | 0 |  |  |  | $res->{clusterInfo} || $res, | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub metrics { | 
| 72 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 73 | 0 | 0 |  |  |  |  | my $opt  = is_hashref $_[0] ? shift @_ : {}; | 
| 74 |  |  |  |  |  |  | my $res = $self->_get( | 
| 75 |  |  |  |  |  |  | "cluster/metrics", | 
| 76 |  |  |  |  |  |  | undef, | 
| 77 | 0 |  | 0 |  |  |  | ( $opt->{server} or () ), | 
| 78 |  |  |  |  |  |  | ); | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 81 |  |  |  |  |  |  | $res, | 
| 82 | 0 |  | 0 |  |  |  | $res->{clusterMetrics} || $res, | 
| 83 |  |  |  |  |  |  | ); | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub scheduler { | 
| 88 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 89 | 0 |  |  |  |  |  | my $res  = $self->_get("cluster/scheduler"); | 
| 90 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 91 |  |  |  |  |  |  | $res, | 
| 92 | 0 |  | 0 |  |  |  | $res->{schedulerInfo} || $res, | 
| 93 |  |  |  |  |  |  | ); | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub apps { | 
| 98 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 99 | 0 |  |  |  |  |  | my $app_id; | 
| 100 |  |  |  |  |  |  | my $options; | 
| 101 | 0 | 0 |  |  |  |  | if ( @_ == 1 ) { | 
|  |  | 0 |  |  |  |  |  | 
| 102 | 0 | 0 |  |  |  |  | if ( !ref $_[0] ) { | 
| 103 | 0 |  |  |  |  |  | $app_id = shift; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | else { | 
| 106 | 0 |  |  |  |  |  | $options = shift; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | elsif ( @_ > 1 ) { | 
| 110 | 0 |  |  |  |  |  | $options = {@_}; | 
| 111 |  |  |  |  |  |  | } | 
| 112 | 0 | 0 |  |  |  |  | my $res = $self->_get( | 
| 113 |  |  |  |  |  |  | $app_id ? "cluster/apps/$app_id" : ( "cluster/apps", { params => $options } ) | 
| 114 |  |  |  |  |  |  | ); | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 117 |  |  |  |  |  |  | $res, | 
| 118 | 0 |  | 0 |  |  |  | $res->{apps}{app} || $res->{app} || $res, | 
| 119 |  |  |  |  |  |  | ); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub appattempts { | 
| 124 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 125 | 0 | 0 |  |  |  |  | my $app_id = shift or die "No app ID provided"; | 
| 126 | 0 |  |  |  |  |  | my $res = $self->_get( "cluster/apps/$app_id/appattempts" ); | 
| 127 | 0 |  |  |  |  |  | return $res; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | # TODO check all states and add filter (validation) | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | sub appstatistics { | 
| 134 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 135 | 0 |  |  |  |  |  | my $options; | 
| 136 | 0 | 0 | 0 |  |  |  | if ( @_ == 1 && ref $_[0] ) { | 
|  |  | 0 |  |  |  |  |  | 
| 137 | 0 |  |  |  |  |  | $options = shift; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  | elsif ( @_ > 1 ) { | 
| 140 | 0 |  |  |  |  |  | $options = {@_}; | 
| 141 |  |  |  |  |  |  | } | 
| 142 | 0 | 0 |  |  |  |  | my $res = $self->_get( | 
| 143 |  |  |  |  |  |  | "cluster/appstatistics", | 
| 144 |  |  |  |  |  |  | ( $options ? { | 
| 145 |  |  |  |  |  |  | params => $options, | 
| 146 |  |  |  |  |  |  | } : () ), | 
| 147 |  |  |  |  |  |  | ); | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 | 0 |  |  |  |  | if ($res) { | 
| 150 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 151 |  |  |  |  |  |  | $res, | 
| 152 |  |  |  |  |  |  | $res->{appStatInfo}{statItem} || $res->{statItem}, | 
| 153 | 0 |  | 0 |  |  |  | ); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  |  | return; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | sub nodes { | 
| 161 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 162 | 0 |  |  |  |  |  | my $node_id; | 
| 163 |  |  |  |  |  |  | my $options; | 
| 164 | 0 | 0 |  |  |  |  | if ( @_ == 1 ) { | 
|  |  | 0 |  |  |  |  |  | 
| 165 | 0 | 0 |  |  |  |  | if ( !ref $_[0] ) { | 
| 166 | 0 |  |  |  |  |  | $node_id = shift; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  | else { | 
| 169 | 0 |  |  |  |  |  | $options = shift; | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  | elsif ( @_ > 1 ) { | 
| 173 | 0 |  |  |  |  |  | $options = {@_}; | 
| 174 |  |  |  |  |  |  | } | 
| 175 | 0 | 0 |  |  |  |  | my $res = $self->_get( | 
| 176 |  |  |  |  |  |  | $node_id ? "cluster/nodes/$node_id" | 
| 177 |  |  |  |  |  |  | :  ( | 
| 178 |  |  |  |  |  |  | "cluster/nodes", | 
| 179 |  |  |  |  |  |  | { params => $options }, | 
| 180 |  |  |  |  |  |  | ) | 
| 181 |  |  |  |  |  |  | ); | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | return $self->_apply_host_key( | 
| 185 |  |  |  |  |  |  | $res, | 
| 186 | 0 |  | 0 |  |  |  | $res->{nodes}{node} || $res->{node} || $res, | 
| 187 |  |  |  |  |  |  | ); | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | sub _apply_host_key { | 
| 193 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 194 | 0 |  |  |  |  |  | my $res  = shift; | 
| 195 | 0 |  |  |  |  |  | my $rv   = shift; | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  |  | 
| 198 | 0 | 0 | 0 |  |  |  | if (   is_ref( $res ) | 
|  |  |  | 0 |  |  |  |  | 
| 199 |  |  |  |  |  |  | && is_ref( $rv ) | 
| 200 |  |  |  |  |  |  | && ( refaddr $res eq refaddr $rv ) | 
| 201 |  |  |  |  |  |  | ) { | 
| 202 | 0 |  |  |  |  |  | return $rv; | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 0 |  |  |  |  |  | my $host_key  = $self->host_key; | 
| 206 | 0 |  |  |  |  |  | my $this_host = $res->{ $host_key }; | 
| 207 |  |  |  |  |  |  |  | 
| 208 | 0 | 0 |  |  |  |  | if ( is_hashref $rv ) { | 
|  |  | 0 |  |  |  |  |  | 
| 209 | 0 |  |  |  |  |  | $rv->{ $host_key } = $this_host; | 
| 210 |  |  |  |  |  |  | } | 
| 211 |  |  |  |  |  |  | elsif ( is_arrayref $rv ) { | 
| 212 | 0 |  |  |  |  |  | foreach my $e ( @{ $rv } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 213 | 0 |  |  |  |  |  | $e->{ $host_key } = $this_host; | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  | } | 
| 216 |  |  |  |  |  |  | else { | 
| 217 | 0 |  |  |  |  |  | die "Got unknown data: $rv"; | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 | 0 |  |  |  |  |  | return $rv; | 
| 221 |  |  |  |  |  |  | } | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | 1; | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | __END__ |