| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DynGig::Automata::MapReduce - Sequential map/reduce automation framework. | ||
| 4 | Extends DynGig::Automata::Serial. | ||
| 5 | |||
| 6 | =cut | ||
| 7 | package DynGig::Automata::MapReduce; | ||
| 8 | |||
| 9 | 1 | ▶ | use base DynGig::Automata::Serial; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | use warnings; | ||
| 12 | use strict; | ||
| 13 | use Carp; | ||
| 14 | |||
| 15 | use File::Spec; | ||
| 16 | |||
| 17 | use DynGig::Automata::EZDB::Exclude; | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ( $class, $name ) = @_; | ||
| 22 | |||
| 23 | croak 'undefined/invalid name' if ! defined $name || ref $name; | ||
| 24 | |||
| 25 | bless DynGig::Automata::Serial->new | ||
| 26 | ( | ||
| 27 | name => $name, | ||
| 28 | queue => _MapReduce->queue( File::Spec->join( 'conf', $name ) ), | ||
| 29 | ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub setup | ||
| 33 | { | ||
| 34 | my $this = DynGig::Automata::Serial::setup( @_ ); | ||
| 35 | |||
| 36 | $this->{_run}{exclude} ||= DynGig::Automata::EZDB::Exclude->new | ||
| 37 | ( | ||
| 38 | $this->file( 'exclude' ), | ||
| 39 | table => [ DynGig::Automata::Serial::GLOBAL, $this->job() ], | ||
| 40 | ); | ||
| 41 | |||
| 42 | return $this; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub run | ||
| 46 | { | ||
| 47 | my ( $this, %param ) = @_; | ||
| 48 | my $error = 'invalid context'; | ||
| 49 | ## context | ||
| 50 | croak "$error: not defined" unless my $context = $param{context}; | ||
| 51 | croak "$error: not HASH" if ref $context ne 'HASH'; | ||
| 52 | |||
| 53 | for my $key ( 'global', map { $_->{name} } @{ $this->{queue} } ) | ||
| 54 | { | ||
| 55 | unless ( defined $context->{$key} ) | ||
| 56 | { | ||
| 57 | $context->{$key} = {}; | ||
| 58 | } | ||
| 59 | elsif ( ref $context->{$key} ne 'HASH' ) | ||
| 60 | { | ||
| 61 | croak "$error: $key not HASH"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | $context->{transient} = {}; | ||
| 66 | ## prepare | ||
| 67 | $this->setup(); | ||
| 68 | |||
| 69 | my $run = $this->{_run}; | ||
| 70 | |||
| 71 | $run->{logger} ||= DynGig::Util::Logger->new(); | ||
| 72 | $run->{context} = $context; | ||
| 73 | |||
| 74 | my $exclude = $run->{exclude}; | ||
| 75 | my $logger = sub { $run->{logger}->write( @_ ) }; | ||
| 76 | my %context = map { $_ => $context->{$_} } qw( transient global ); | ||
| 77 | ## sequence | ||
| 78 | for my $job ( @{ $this->{queue} } ) | ||
| 79 | { | ||
| 80 | my $name = $job->{name}; | ||
| 81 | my %param = | ||
| 82 | ( | ||
| 83 | job => $name, | ||
| 84 | exclude => $exclude, | ||
| 85 | name => $this->{name}, | ||
| 86 | ); | ||
| 87 | |||
| 88 | map { $exclude->expire( $_ ) } DynGig::Automata::Serial::GLOBAL, $name; | ||
| 89 | |||
| 90 | my ( $status, $result ) = $this->_eval | ||
| 91 | ( | ||
| 92 | $job, | ||
| 93 | param => \%param, | ||
| 94 | logger => $logger, | ||
| 95 | context => +{ %context, glocal => $context->{ $name } }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | croak $result if $status != DynGig::Automata::Serial::OK; | ||
| 99 | } | ||
| 100 | |||
| 101 | delete $context->{transient}; | ||
| 102 | } | ||
| 103 | |||
| 104 | package _MapReduce; | ||
| 105 | |||
| 106 | use warnings; | ||
| 107 | use strict; | ||
| 108 | use Carp; | ||
| 109 | |||
| 110 | use YAML::XS; | ||
| 111 | |||
| 112 | use DynGig::Util::Time; | ||
| 113 | use DynGig::Util::MapReduce; | ||
| 114 | |||
| 115 | use constant { PRECISION => 30 }; | ||
| 116 | |||
| 117 | sub queue | ||
| 118 | { | ||
| 119 | my ( $class, $conf ) = @_; | ||
| 120 | my ( @queue, %job ); | ||
| 121 | my $error = 'invalid queue config'; | ||
| 122 | |||
| 123 | for my $param ( YAML::XS::LoadFile $conf ) | ||
| 124 | { | ||
| 125 | croak $error unless $param && ref $param eq 'HASH'; | ||
| 126 | |||
| 127 | my $name = $param->{name}; | ||
| 128 | |||
| 129 | croak "$error: invalid/undefined name" if ! defined $name || ref $name; | ||
| 130 | croak "$error: name collision '$name'" if $job{$name}; | ||
| 131 | |||
| 132 | $param->{name} = $job{$name} = "job.$name"; | ||
| 133 | |||
| 134 | my %param = | ||
| 135 | ( | ||
| 136 | interval => DynGig::Util::Time->rel2sec( $param->{interval} ), | ||
| 137 | job => DynGig::Util::MapReduce->new( _param( %$param ) ), | ||
| 138 | ); | ||
| 139 | |||
| 140 | push @queue, { param => \%param, name => $job{$name}, code => \&_code }; | ||
| 141 | } | ||
| 142 | |||
| 143 | return \@queue; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _param | ||
| 147 | { | ||
| 148 | my %param = @_; | ||
| 149 | my $error = "invalid job config $param{name}"; | ||
| 150 | |||
| 151 | for my $key ( qw( batch map reduce ) ) | ||
| 152 | { | ||
| 153 | my $plugin = $param{$key}; | ||
| 154 | |||
| 155 | unless ( defined $plugin ) | ||
| 156 | { | ||
| 157 | next if $key eq 'reduce'; | ||
| 158 | croak "$error: undefined $key"; | ||
| 159 | } | ||
| 160 | |||
| 161 | croak "$error: invalid $key " . ( $@ || '' ) if ref $plugin ne 'HASH' | ||
| 162 | || ref ( $plugin->{code} = do $plugin->{code} ) ne 'CODE' | ||
| 163 | || ref ( $plugin->{param} ||= {} ) ne 'HASH'; | ||
| 164 | |||
| 165 | $plugin->{timeout} = DynGig::Util::Time->rel2sec( $plugin->{timeout} ); | ||
| 166 | } | ||
| 167 | |||
| 168 | return %param; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _code | ||
| 172 | { | ||
| 173 | my %param = @_; | ||
| 174 | my $job = $param{job}; | ||
| 175 | my $logger = $param{logger}; | ||
| 176 | my $context = $param{context}; | ||
| 177 | my $glocal = $context->{glocal}; | ||
| 178 | my $global = $context->{global}; | ||
| 179 | my $name = $job->name(); | ||
| 180 | my $time = time; | ||
| 181 | my $delta = 0; | ||
| 182 | |||
| 183 | if ( my $interval = $param{interval} ) | ||
| 184 | { | ||
| 185 | unless ( my $last = $glocal->{timestamp} ) | ||
| 186 | { | ||
| 187 | $delta = $interval; | ||
| 188 | } | ||
| 189 | elsif ( ( $delta = $last + $interval - $time ) > PRECISION ) | ||
| 190 | { | ||
| 191 | goto DONE; | ||
| 192 | } | ||
| 193 | elsif ( $delta < 0 ) | ||
| 194 | { | ||
| 195 | &$logger( 'OVERDUE: %s for %d seconds', $name, -$delta ) | ||
| 196 | if -$delta > PRECISION; | ||
| 197 | |||
| 198 | $delta = 0; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | my %context = ( $name => $glocal, global => $global->{$name} ||= {} ); | ||
| 203 | |||
| 204 | &$logger( 'START: %s', $name ); | ||
| 205 | |||
| 206 | $job->run | ||
| 207 | ( | ||
| 208 | context => \%context, | ||
| 209 | map { $_ => $param{param} } qw( batch reduce ), | ||
| 210 | ); | ||
| 211 | |||
| 212 | &$logger( 'DONE: %s', $name ); | ||
| 213 | |||
| 214 | $glocal->{timestamp} = $time; | ||
| 215 | DONE: $glocal->{due} = $delta; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head1 NOTE | ||
| 219 | |||
| 220 | See DynGig::Automata | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | 1; | ||
| 225 | |||
| 226 | __END__ | ||