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

Annotation of /Sprout/ERDBGenerator.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (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 : parrello 1.4 Trace("$deleteCount files deleted.") if T(3) && $deleteCount % 100 == 0;
223 : parrello 1.1 }
224 :     }
225 :     Trace("$deleteCount files deleted from load directory during ClearGroups.") if T(2);
226 :     }
227 : parrello 1.2 # Delete any leftover kill file if it exists.
228 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $directory);
229 :     if (-f $killFileName) {
230 :     Trace("Deleting kill file $killFileName.") if T(2);
231 :     unlink $killFileName;
232 :     }
233 : parrello 1.1 # Now we need to get our list of sections. Check to see if the user
234 :     # supplied a section file.
235 :     my $sectionFile = $options->{sections};
236 :     if ($sectionFile eq "*") {
237 :     # No, so we must create one.
238 :     $sectionFile = "$directory/Sections$$.txt";
239 :     Open(\*SECTIONS, ">$sectionFile");
240 :     for my $section ($erdb->SectionList($source)) {
241 :     print SECTIONS "$section\n";
242 :     }
243 :     close SECTIONS;
244 :     } elsif ($sectionFile =~ m#^\w#) {
245 :     # Yes, but it doesn't have a directory name, so add one.
246 :     $sectionFile = "$directory/$sectionFile";
247 :     }
248 :     # Compute the options to be used for worker processes (or ourselves if
249 :     # we're sequential).
250 :     my %workerOptions = %{$options};
251 :     # Get rid of the manager-only options.
252 :     for my $optionID (@managerOptions) {
253 :     delete $workerOptions{$optionID};
254 :     }
255 :     # Insure the worker knows what it is.
256 :     $workerOptions{workers} = 0;
257 :     $workerOptions{forked} = 1;
258 :     $workerOptions{background} = 1;
259 :     # Prepare to read the section file.
260 :     my $ih = Open(undef, "<$sectionFile");
261 :     # Are we a sequential load or a multi-worker manager?
262 :     my $numWorkers = $options->{workers};
263 :     if ($numWorkers == 1) {
264 : parrello 1.2 # We're sequential, so we do all the work ourselves.
265 : parrello 1.1 Trace("Sequential load started.") if T(2);
266 :     LoadFromInput($ih, $erdb, $source, \@realGroups, \%workerOptions);
267 :     } else {
268 : parrello 1.3 # Here we need to create the workers. The following arrays will contain,
269 :     # respectively, a list of open file handles and a list of worker labels.
270 :     # Each one will correspond to a single worker. Writing to the handle in
271 :     # the first sends a section to the worker named in the second.
272 : parrello 1.1 my @workers = ();
273 : parrello 1.3 my @labels = ();
274 :     # Compute the positional parameters to use for the worker.
275 :     my $commandParms = join(" ", $database, @realGroups);
276 :     my $command = $0;
277 : parrello 1.1 # Create the workers.
278 :     for (my $i = 0; $i < $numWorkers; $i++) {
279 : parrello 1.3 my $label = "$options->{label}$i";
280 :     $workerOptions{label} = $label;
281 :     my $commandOptions = Tracer::UnparseOptions(\%workerOptions);
282 :     my $oh = Open(undef, "| $command $commandOptions $commandParms");
283 :     select $oh; $| = 1; select STDOUT;
284 : parrello 1.1 push @workers, $oh;
285 : parrello 1.3 push @labels, $label;
286 : parrello 1.1 }
287 :     # Now we assign sections to the workers.
288 : parrello 1.3 my $w = 0;
289 : parrello 1.1 while (! eof $ih) {
290 :     # Get the name of the next section.
291 :     my $line = <$ih>;
292 :     # Get the next worker in rotation.
293 : parrello 1.3 my $worker = $workers[$w];
294 : parrello 1.1 # Send this section to it.
295 :     print $worker $line;
296 : parrello 1.3 Trace(Tracer::Strip($line) . " sent to $labels[$w]") if T(3);
297 :     # Position on the next worker.
298 :     $w = ($w + 1) % $numWorkers;
299 : parrello 1.1 }
300 :     # All done, wait for the workers to finish.
301 :     for my $worker (@workers) {
302 :     close $worker;
303 :     }
304 :     }
305 : parrello 1.4 Trace("Load manager completed.") if T(2);
306 : parrello 1.1 }
307 :     };
308 :     if ($@) {
309 :     Trace("Script failed with error: $@") if T(0);
310 :     } else {
311 :     Trace("Script complete.") if T(2);
312 :     }
313 :     if ($options->{phone}) {
314 :     my $msgID = Tracer::SendSMS($options->{phone}, "ERDBGenerator has ended.");
315 :     if ($msgID) {
316 :     Trace("Phone message sent with ID $msgID.") if T(2);
317 :     } else {
318 :     Trace("Phone message not sent.") if T(2);
319 :     }
320 :     }
321 :    
322 :     =head3 LoadFromInput
323 :    
324 :     LoadFromInput($ih, $erdb, \@groups, \%options);
325 :    
326 :     Load one or more sections of data for the specified table groups. The IDs
327 :     of the data sections will be read from the standard input. The groups
328 :     will be loaded in the order specified, once per section.
329 :    
330 :     =over 4
331 :    
332 :     =item ih
333 :    
334 :     File handle for the input stream containing the list of sections to process.
335 :    
336 :     =item erdb
337 :    
338 :     Database object containing information about the tables being loaded.
339 :    
340 :     =item source
341 :    
342 :     Source object used to access the data from which the load file is created.
343 :    
344 :     =item groups
345 :    
346 :     Reference to a list of the names for the load groups to process.
347 :    
348 :     =item options
349 :    
350 :     Reference to a hash of the options passed in from the command line.
351 :    
352 :     =back
353 :    
354 :     =cut
355 :    
356 :     sub LoadFromInput {
357 :     # Get the parameters.
358 :     my ($ih, $erdb, $source, $groups, $options) = @_;
359 :     # Create a load object for each group.
360 :     my %loaders = map { $_ => $erdb->Loader($_, $source, $options) } @{$groups};
361 :     # We'll count our errors in here.
362 :     my $errorCount = 0;
363 :     my $maxErrors = $options->{maxErrors};
364 : parrello 1.2 # Compute the kill file name.
365 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $erdb->LoadDirectory());
366 :     my $killed = 0;
367 : parrello 1.1 # Loop through the sections.
368 : parrello 1.2 while (! $killed && ! eof $ih) {
369 : parrello 1.1 # Get this section ID.
370 :     my ($section) = Tracer::GetLine($ih);
371 :     # Process it for each load group.
372 :     for my $group (@$groups) {
373 : parrello 1.2 # Only proceed if we haven't been killed.
374 :     if (! $killed) {
375 :     # Check for a kill file.
376 :     if (-f $killFileName) {
377 :     # Found one, so kill ourselves.
378 : parrello 1.3 Trace("$options->{label} terminated by kill file.") if T(2);
379 : parrello 1.2 $killed = 1;
380 :     } else {
381 :     # No kill file, so we process the section.
382 : parrello 1.3 Trace("Processing section $section for group $group in $options->{label}($$).") if T(3);
383 : parrello 1.2 my $ok = $loaders{$group}->ProcessSection($section);
384 :     # Check to see if we've exceeded the maximum error count. We only care
385 :     # if maxErrors is nonzero.
386 :     if (! $ok && $maxErrors && ++$errorCount >= $maxErrors) {
387 :     Trace("Error limit exceeded in database loader.") if T(0);
388 :     $killed = 1;
389 :     }
390 :     }
391 : parrello 1.1 }
392 :     }
393 :     }
394 :     # Now we display the statistics for each group.
395 :     for my $group (@$groups) {
396 :     Trace("Statistics for group $group\n" . $loaders{$group}->DisplayStats()) if T(2);
397 :     }
398 :     Trace("Processing finished for worker $$.") if T(2);
399 :     }
400 :    
401 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3