[Bio] / Sprout / ERDBGenerator.pl Repository:
ViewVC logotype

Annotation of /Sprout/ERDBGenerator.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     use strict;
21 :     use Tracer;
22 :     use ERDB;
23 :     use ERDBGenerate;
24 :     use ERDBLoadGroup;
25 :    
26 :     =head1 ERDBGenerator Script
27 :    
28 :     ERDBGenerator [options] database group1 group2 ...
29 :    
30 :     Generate ERDB table load files
31 :    
32 :     =head2 Introduction
33 :    
34 :     This script manages the generation of load files for an ERDB database. It can
35 :     either function as a worker process that reads section IDs from the standard
36 :     input and generates the load files for each, or it can function as a management
37 :     process that starts a bunch of workers and gives them work.
38 :    
39 :     The positional parameters include a list of load groups to process and the name
40 :     of the database.
41 :    
42 :     =head2 Positional Parameters
43 :    
44 :     =over 4
45 :    
46 :     =item database
47 :    
48 :     Name of the ERDB database. This should be the class name for the [[ErdbPm]]
49 :     subclass used to access the database.
50 :    
51 :     =item groups
52 :    
53 : parrello 1.2 List of the table groups to load. A C<+> at the end of the list indicates that
54 :     all groups that follow the last-named group in the standard order should
55 :     be loaded. A C<+> by itself loads all groups in standard order.
56 : parrello 1.1
57 :     =back
58 :    
59 :     =head2 Command-Line Options
60 :    
61 :     =over 4
62 :    
63 :     =item background
64 :    
65 :     Save the standard and error output to files. The files will be created
66 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
67 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
68 :     B<user> option above.
69 :    
70 :     =item clean
71 :    
72 :     Remove temporary files from the load directory. Use this option with care,
73 :     since it will crash if a worker process is still running.
74 :    
75 :     =item clear
76 :    
77 :     If specified, all generated files in the load directory with a C<dt>X suffix
78 :     will be erased. This restores the load directory to a pristine, pre-loading state.
79 :    
80 :     =item clearGroups
81 :    
82 :     If specified, all generated files related to each specified group will be
83 :     erased prior to any further processing. This is useful if a single group
84 :     needs to be reloaded and we don't want to be confused by files leftover
85 :     from previous loads.
86 :    
87 :     =item forked
88 :    
89 :     If specified, then the trace file will not be erased during initialization.
90 :     This prevents the worker processes from stepping on each other's trace output.
91 :    
92 :     =item help
93 :    
94 :     Display this command's parameters and options.
95 :    
96 :     =item maxErrors
97 :    
98 :     If specified, then this prcoess will terminate after the specified number of
99 :     section load errors; otherwise, the process will keep going after a section
100 : parrello 1.2 error. A value of C<0> means the process will ignore all errors. A value of
101 :     C<1> means it will stop after the first error.
102 : parrello 1.1
103 :     =item phone
104 :    
105 :     Phone number to message when the script is complete.
106 :    
107 :     =item sections
108 :    
109 :     Name of a file containing a list of sections to process. If C<*> is specified (the
110 :     default), all sections are processed. This options is ignored if C<workers> is
111 :     C<0>. In that case, the list of sections is taken from the standard input. When
112 :     a file name is specified, if it is not an absolute file name, it is presumed to
113 :     be in the database's default load directory.
114 :    
115 :     =item sql
116 :    
117 :     If specified, turns on tracing of SQL activity.
118 :    
119 :     =item trace
120 :    
121 :     Specifies the tracing level. The higher the tracing level, the more messages
122 :     will appear in the trace log. Use E to specify emergency tracing.
123 :    
124 :     =item user
125 :    
126 :     Name suffix to be used for log files. If omitted, the PID is used.
127 :    
128 :     =item warn
129 :    
130 :     Create an event in the RSS feed when an error occurs.
131 :    
132 : parrello 1.3 =item label
133 :    
134 :     Name of this process, for display during tracing.
135 :    
136 : parrello 1.1 =item workers
137 :    
138 :     If C<0>, then this is considered to be a worker process and the sections in the
139 :     standard input are processed. If C<1>, then all sections are processed without
140 :     any parallelism and the standard input is ignored. If it is any other number,
141 :     then the appropriate number of worker processes are generated and the sections
142 :     are assigned to them in a round-robin fashion.
143 :    
144 :     =back
145 :    
146 :     =cut
147 :    
148 :     # Get the command-line options and parameters.
149 :     my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDBGenerate ERDB Stats) ],
150 :     {
151 :     clear => ["", "if specified, the entire load directory will be cleared"],
152 :     clean => ["", "if specified, temporary files in the load directory will be deleted"],
153 :     clearGroups => ["", "if specified, pre-exising load files from the groups processed will be deleted"],
154 : parrello 1.2 maxErrors => ["0", "if non-zero, the maximum allowed number of section failures"],
155 :     phone => ["", "phone number (international format) to call when load finishes"],
156 : parrello 1.1 trace => ["2", "tracing level"],
157 :     workers => ["1", "number of worker processes"],
158 : parrello 1.3 label => ["Main", "name of this process"],
159 : parrello 1.1 resume => ["", "if specified, the specified group and all groups that normally come after it will be loaded"],
160 :     sections => ["*", "name of a file in the database's load directory containing a list of sections to process"],
161 :     },
162 :     "<database> <group1> <group2> ...",
163 :     @ARGV);
164 :     # This is a list of the options that are for manager scripts only.
165 :     my @managerOptions = qw(clear clean clearGroups resume sections);
166 :     # We're doing heavy pipe stuff, so we need to throw an error on a broken-pipe signal.
167 :     local $SIG{PIPE} = sub { Confess("Broken pipe.") };
168 :     # Insure we catch errors.
169 :     eval {
170 :     # Get the parameters.
171 :     my ($database, @groups) = @parameters;
172 : parrello 1.2 # Connect to the database and get its load directory.
173 : parrello 1.1 my $erdb = ERDB::GetDatabase($database);
174 : parrello 1.2 my $directory = $erdb->LoadDirectory();
175 : parrello 1.1 # Fix the group list.
176 : parrello 1.2 my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
177 :     # Get the source object.
178 : parrello 1.1 my $source = $erdb->GetSourceObject();
179 :     # Are we a worker or a manager?
180 :     if ($options->{workers} == 0) {
181 :     # Yes, we're a worker.
182 :     Trace("Worker process $$ started.") if T(2);
183 :     LoadFromInput(\*STDIN, $erdb, $source, \@realGroups, $options);
184 :     } else {
185 :     # Here we're a manager. If the user wants us to clear the directory,
186 :     # do that first.
187 :     if ($options->{clear}) {
188 :     # Count the number of files deleted.
189 :     my $deleteCount = 0;
190 :     # Get a list of the applicable file names.
191 :     my @files = ERDBGenerate::GetLoadFiles($directory);
192 :     # It's worth noting if we didn't find any.
193 :     if (! @files) {
194 :     Tracer("Load directory is already clear.") if T(2);
195 :     } else {
196 :     # Delete the files we found.
197 :     for my $file (@files) {
198 :     unlink "$directory/$file";
199 :     $deleteCount++;
200 :     }
201 :     Tracer("$deleteCount files deleted from load directory during Clear.") if T(2);
202 :     }
203 :     } elsif ($options->{clearGroups}) {
204 :     # Here the user only wants to clear the load files for the specified
205 :     # groups. This operation requires significantly greater care. Get
206 :     # the hash of groups to table names.
207 :     my $groupHash = ERDBLoadGroup::GetGroupHash($erdb);
208 :     # Get a list of the files in this directory in alphabetical order.
209 :     my @files = ERDBGenerate::GetLoadFiles($directory);
210 :     # Get a hash of all the tables to be deleted.
211 :     my %tables = map { $_ => 1 } map { @{$groupHash->{$_}} } @realGroups;
212 :     # We'll count the number of files deleted in here.
213 :     my $deleteCount = 0;
214 :     # Loop through all the files in the directory.
215 :     for my $file (@files) {
216 :     # Extract the relevant table name from the file.
217 :     my ($table) = ERDBGenerate::ParseFileName($file);
218 :     if ($tables{$table}) {
219 :     # This is one of our tables, so delete the file.
220 :     unlink "$directory/$file";
221 :     $deleteCount++;
222 :     }
223 :     }
224 :     Trace("$deleteCount files deleted from load directory during ClearGroups.") if T(2);
225 :     }
226 : parrello 1.2 # Delete any leftover kill file if it exists.
227 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $directory);
228 :     if (-f $killFileName) {
229 :     Trace("Deleting kill file $killFileName.") if T(2);
230 :     unlink $killFileName;
231 :     }
232 : parrello 1.1 # Now we need to get our list of sections. Check to see if the user
233 :     # supplied a section file.
234 :     my $sectionFile = $options->{sections};
235 :     if ($sectionFile eq "*") {
236 :     # No, so we must create one.
237 :     $sectionFile = "$directory/Sections$$.txt";
238 :     Open(\*SECTIONS, ">$sectionFile");
239 :     for my $section ($erdb->SectionList($source)) {
240 :     print SECTIONS "$section\n";
241 :     }
242 :     close SECTIONS;
243 :     } elsif ($sectionFile =~ m#^\w#) {
244 :     # Yes, but it doesn't have a directory name, so add one.
245 :     $sectionFile = "$directory/$sectionFile";
246 :     }
247 :     # Compute the options to be used for worker processes (or ourselves if
248 :     # we're sequential).
249 :     my %workerOptions = %{$options};
250 :     # Get rid of the manager-only options.
251 :     for my $optionID (@managerOptions) {
252 :     delete $workerOptions{$optionID};
253 :     }
254 :     # Insure the worker knows what it is.
255 :     $workerOptions{workers} = 0;
256 :     $workerOptions{forked} = 1;
257 :     $workerOptions{background} = 1;
258 :     # Prepare to read the section file.
259 :     my $ih = Open(undef, "<$sectionFile");
260 :     # Are we a sequential load or a multi-worker manager?
261 :     my $numWorkers = $options->{workers};
262 :     if ($numWorkers == 1) {
263 : parrello 1.2 # We're sequential, so we do all the work ourselves.
264 : parrello 1.1 Trace("Sequential load started.") if T(2);
265 :     LoadFromInput($ih, $erdb, $source, \@realGroups, \%workerOptions);
266 :     } else {
267 : parrello 1.3 # Here we need to create the workers. The following arrays will contain,
268 :     # respectively, a list of open file handles and a list of worker labels.
269 :     # Each one will correspond to a single worker. Writing to the handle in
270 :     # the first sends a section to the worker named in the second.
271 : parrello 1.1 my @workers = ();
272 : parrello 1.3 my @labels = ();
273 :     # Compute the positional parameters to use for the worker.
274 :     my $commandParms = join(" ", $database, @realGroups);
275 :     my $command = $0;
276 : parrello 1.1 # Create the workers.
277 :     for (my $i = 0; $i < $numWorkers; $i++) {
278 : parrello 1.3 my $label = "$options->{label}$i";
279 :     $workerOptions{label} = $label;
280 :     my $commandOptions = Tracer::UnparseOptions(\%workerOptions);
281 :     my $oh = Open(undef, "| $command $commandOptions $commandParms");
282 :     select $oh; $| = 1; select STDOUT;
283 : parrello 1.1 push @workers, $oh;
284 : parrello 1.3 push @labels, $label;
285 : parrello 1.1 }
286 :     # Now we assign sections to the workers.
287 : parrello 1.3 my $w = 0;
288 : parrello 1.1 while (! eof $ih) {
289 :     # Get the name of the next section.
290 :     my $line = <$ih>;
291 :     # Get the next worker in rotation.
292 : parrello 1.3 my $worker = $workers[$w];
293 : parrello 1.1 # Send this section to it.
294 :     print $worker $line;
295 : parrello 1.3 Trace(Tracer::Strip($line) . " sent to $labels[$w]") if T(3);
296 :     # Position on the next worker.
297 :     $w = ($w + 1) % $numWorkers;
298 : parrello 1.1 }
299 :     # All done, wait for the workers to finish.
300 :     for my $worker (@workers) {
301 :     close $worker;
302 :     }
303 :     }
304 :     }
305 :     Trace("Load manager completed.") if T(2);
306 :     };
307 :     if ($@) {
308 :     Trace("Script failed with error: $@") if T(0);
309 :     } else {
310 :     Trace("Script complete.") if T(2);
311 :     }
312 :     if ($options->{phone}) {
313 :     my $msgID = Tracer::SendSMS($options->{phone}, "ERDBGenerator has ended.");
314 :     if ($msgID) {
315 :     Trace("Phone message sent with ID $msgID.") if T(2);
316 :     } else {
317 :     Trace("Phone message not sent.") if T(2);
318 :     }
319 :     }
320 :    
321 :     =head3 LoadFromInput
322 :    
323 :     LoadFromInput($ih, $erdb, \@groups, \%options);
324 :    
325 :     Load one or more sections of data for the specified table groups. The IDs
326 :     of the data sections will be read from the standard input. The groups
327 :     will be loaded in the order specified, once per section.
328 :    
329 :     =over 4
330 :    
331 :     =item ih
332 :    
333 :     File handle for the input stream containing the list of sections to process.
334 :    
335 :     =item erdb
336 :    
337 :     Database object containing information about the tables being loaded.
338 :    
339 :     =item source
340 :    
341 :     Source object used to access the data from which the load file is created.
342 :    
343 :     =item groups
344 :    
345 :     Reference to a list of the names for the load groups to process.
346 :    
347 :     =item options
348 :    
349 :     Reference to a hash of the options passed in from the command line.
350 :    
351 :     =back
352 :    
353 :     =cut
354 :    
355 :     sub LoadFromInput {
356 :     # Get the parameters.
357 :     my ($ih, $erdb, $source, $groups, $options) = @_;
358 :     # Create a load object for each group.
359 :     my %loaders = map { $_ => $erdb->Loader($_, $source, $options) } @{$groups};
360 :     # We'll count our errors in here.
361 :     my $errorCount = 0;
362 :     my $maxErrors = $options->{maxErrors};
363 : parrello 1.2 # Compute the kill file name.
364 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $erdb->LoadDirectory());
365 :     my $killed = 0;
366 : parrello 1.1 # Loop through the sections.
367 : parrello 1.2 while (! $killed && ! eof $ih) {
368 : parrello 1.1 # Get this section ID.
369 :     my ($section) = Tracer::GetLine($ih);
370 :     # Process it for each load group.
371 :     for my $group (@$groups) {
372 : parrello 1.2 # Only proceed if we haven't been killed.
373 :     if (! $killed) {
374 :     # Check for a kill file.
375 :     if (-f $killFileName) {
376 :     # Found one, so kill ourselves.
377 : parrello 1.3 Trace("$options->{label} terminated by kill file.") if T(2);
378 : parrello 1.2 $killed = 1;
379 :     } else {
380 :     # No kill file, so we process the section.
381 : parrello 1.3 Trace("Processing section $section for group $group in $options->{label}($$).") if T(3);
382 : parrello 1.2 my $ok = $loaders{$group}->ProcessSection($section);
383 :     # Check to see if we've exceeded the maximum error count. We only care
384 :     # if maxErrors is nonzero.
385 :     if (! $ok && $maxErrors && ++$errorCount >= $maxErrors) {
386 :     Trace("Error limit exceeded in database loader.") if T(0);
387 :     $killed = 1;
388 :     }
389 :     }
390 : parrello 1.1 }
391 :     }
392 :     }
393 :     # Now we display the statistics for each group.
394 :     for my $group (@$groups) {
395 :     Trace("Statistics for group $group\n" . $loaders{$group}->DisplayStats()) if T(2);
396 :     }
397 :     Trace("Processing finished for worker $$.") if T(2);
398 :     }
399 :    
400 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3