Automata/Thread.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« Automata/MapReduce.pm ↑ Summary
1 =head1 NAME
2  
3 DynGig::Automata::Thread - Extends DynGig::Automata::Serial.
4  
5 =cut
6 package DynGig::Automata::Thread;
7  
8 1 ▶use base DynGig::Automata::Serial;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use warnings;
11 use strict;
12 use Carp;
13  
14 use threads;
15 use Thread::Queue;
16  
17 use constant CTX => 'ctx.';
18  
19 our $MAX_THR = 100;
20  
21 =head1 DESCRIPTION
22  
23 Process several targets simultaneously in a user specified number of threads.
24 Each target is processed in sequential steps specified by the user.
25 As a target completes the sequence, a new target takes its thread and starts
26 the sequence, until all targets are exhausted.
27  
28 Execution pauses for a thread/target when an error is encountered in any job.
29 The error associated with the corresponding target is recorded in the alert
30 database. Other threads/targets are not affected in the mean while.
31 Execution resumes for a thread when all errors associated with the
32 correponding target are removed from the alert database.
33  
34 =cut
35 sub run
36 {
37 my ( $this, %param ) = @_;
38 my $error = 'invalid context';
39 ## context
40 croak "$error: not defined" unless my $context = $param{context};
41 croak "$error: not HASH" if ref $context ne 'HASH';
42  
43 my @key = qw( global );
44 map { push @key, $_ if defined $this->{$_} } qw( target begin end );
45  
46 for my $key ( @key )
47 {
48 unless ( defined $context->{$key} )
49 {
50 $context->{$key} = {};
51 }
52 elsif ( ref $context->{$key} ne 'HASH' )
53 {
54 croak "$error: $key not HASH";
55 }
56 }
57  
58 $context->{transient} = {};
59 ## threads
60 my $thread = $param{thread};
61  
62 croak 'invalid thread'
63 if defined $thread && ( ref $thread || $thread !~ /^\d+$/ );
64  
65 $thread = $MAX_THR unless $thread && $thread < $MAX_THR;
66 ## shmem queue
67 my $file = $this->{_file};
68 my $queue = $this->{queue};
69 my %shmem = map { $_ => Thread::Queue->new() }
70  
71 my @job = ( DynGig::Automata::Serial::GLOBAL,
72 map { $_->{name} } @$queue );
73  
74 my $run = $this->{_run} = { shmem => \%shmem, context => $context };
75 ## prepare
76 $this->_prepare( %param );
77 ## alert db
78 my $alert = $run->{alert} ||= DynGig::Automata::EZDB::Alert
79 ->new( $file->{alert}, table => \@job );
80 ## begin
81 my $name = $this->{name};
82 my $logger = $run->{logger};
83 my $shmem = $shmem{ $job[0] };
84  
85 $logger->write( 'START: sequence %s', $name );
86 $this->_job( $this->{begin} );
87 ## sequence
88 for ( my ( $current, %retry, %thread ) = 0; 1; sleep 1 )
89 {
90 my $stuck; ## pause/kill
91  
92 while ( my $pause = DynGig::Util::LockFile::Time->check( $file->{pause} ) )
93 {
94 if ( $pause >= DynGig::Automata::Serial::KILL )
95 {
96 map { $alert->truncate( $_ ) } @job;
97 map { $_->kill( 'KILL' )->detach() } values %thread;
98  
99 $logger->write( 'KILL: sequence %s', $name );
100 goto DONE;
101 }
102 elsif ( ! $stuck )
103 {
104 $logger->write( 'PAUSE: sequence %s', $name );
105 $stuck = $pause;
106 }
107  
108 sleep 10;
109 }
110  
111 $this->_job_param();
112  
113 while ( $shmem->pending() ) ## done/error
114 {
115 my ( $job, $target, $context, $error ) = $shmem->dequeue( 4 );
116  
117 $thread{$target}->join;
118 delete $thread{$target};
119 $context = YAML::XS::Load $context;
120  
121 if ( $job == @$queue ) ## done
122 {
123 $run->{context}{ CTX.$target } = $context->{glocal};
124 $logger->write( 'DONE: %s', $target );
125 next;
126 }
127  
128 my $name = $queue->[$job]{name};
129  
130 $retry{$target} ||= 0;
131  
132 if ( $retry{$target} ++ < $queue->[$job]{retry} ) ## retry
133 {
134 $thread{$target} = threads::async
135 {
136 $this->_sequence( $job, $target, $context );
137 };
138  
139 $logger->write( 'RETRY %d: %s @ %s << %s', $retry{$target},
140 $target, $name, $error );
141 }
142 else ## alert
143 {
144 $alert->set( $name, $target, $error );
145 delete $retry{$target};
146  
147 $logger->write( 'ALERT: %s @ %s << %s',
148 $target, $name, $error );
149 }
150 }
151  
152 my @alert = map { $alert->dump( $_ ) } @job;
153  
154 next if @alert / 2 + keys %thread >= $thread;
155  
156 if ( my @target = $this->_job( $this->{target} ) ) ## new target
157 {
158 my $target = shift @target;
159 my $local = CTX.$target;
160  
161 unless ( defined $context->{$local} )
162 {
163 $context->{$local} = {};
164 }
165 elsif ( ref $context->{$local} ne 'HASH' )
166 {
167 croak "$error: $local not HASH";
168 }
169  
170 my %context = ( transient => {}, glocal => $context->{$local} );
171  
172 $thread{$target} = threads::async
173 {
174 $this->_sequence( 0, $target, \%context );
175 };
176  
177 $logger->write( 'START: %s', $target );
178 }
179 else ## all done
180 {
181 last unless %thread || @alert;
182 }
183 }
184 ## end
185 DONE: $this->_job( $this->{end} );
186 $logger->write( 'DONE: sequence %s', $name );
187 }
188  
189 sub _sequence
190 {
191 local $SIG{ALRM} = sub {};
192  
193 my ( $this, $job, $target, $context ) = @_;
194 my $queue = $this->{queue};
195 my $run = $this->{_run};
196 my %logger = ( logger => sub { $run->{logger}->write( @_ ) } );
197 my @result;
198  
199 for ( ; $job < @$queue; $job ++, @result = '' )
200 {
201 my %context = map { $_ => $run->{context}{$_} } qw( global transient );
202  
203 @result = $this->_eval
204 (
205 $queue->[$job], %{ $this->_job_param( $job ) }, %logger,
206 context_ro => \%context,
207 context => $context,
208 target => $target,
209 );
210  
211 last if shift @result == DynGig::Automata::Serial::ERROR;
212 }
213  
214 $run->{shmem}{DynGig::Automata::Serial::GLOBAL}->enqueue
215 (
216 $job, $target, YAML::XS::Dump( $context ), @result
217 );
218 }
219  
220 sub _job_param ## two faced param override
221 {
222 my ( $this, $job ) = @_;
223 my $run = $this->{_run};
224 my $queue = $this->{queue};
225 my $param = $run->{param};
226 my $shmem = $run->{shmem};
227  
228 if ( defined $job ) ## child
229 {
230 my $name = $queue->[$job]{name};
231  
232 return {} unless my $shmem = $shmem->{$name};
233 return {} unless my $time = $shmem->peek( -2 );
234  
235 my $param = $param->{$name} ||= {};
236  
237 return $param->{$time} if $param->{$time};
238  
239 map { delete $param->{$_} } keys %$param;
240  
241 my $override = eval { YAML::XS::Load $shmem->peek( -1 ) };
242  
243 return $param->{$time} = $override if ! $@ && ref $override eq 'HASH';
244  
245 $shmem->dequeue( 2 );
246 return {};
247 }
248  
249 local $/;
250  
251 for my $job ( @$queue ) ## parent
252 {
253 my $name = $job->{name};
254 my $shmem = $shmem->{$name};
255 my $param = $param->{$name} ||= {};
256 my $file = File::Spec->join( $this->{_dir}{param}, $name );
257  
258 goto NEXT unless -f $file && open my $handle, '<', $file;
259  
260 my $time = ( stat $handle )[9];
261  
262 if ( $param->{$time} )
263 {
264 close $handle;
265 next;
266 }
267  
268 map { delete $param->{$_} } keys %$param;
269 $shmem->enqueue( $time, $param->{$time} = <$handle> );
270  
271 close $handle;
272 next if $shmem->pending == 2;
273  
274 NEXT: $shmem->dequeue_nb( 2 );
275 }
276 }
277  
278 =head1 NOTE
279  
280 See DynGig::Automata
281  
282 =cut
283  
284 1;
285  
286 __END__
« Automata/MapReduce.pm ↑ Summary