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

Annotation of /Sprout/ERDBGenerator.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (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 : parrello 1.5 C<1> means it will stop after the first error. The default is C<1>.
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.5 =item resume
137 :    
138 :     If specified, load files that already exist will not be regenerated.
139 :    
140 : parrello 1.1 =item workers
141 :    
142 :     If C<0>, then this is considered to be a worker process and the sections in the
143 :     standard input are processed. If C<1>, then all sections are processed without
144 :     any parallelism and the standard input is ignored. If it is any other number,
145 :     then the appropriate number of worker processes are generated and the sections
146 :     are assigned to them in a round-robin fashion.
147 :    
148 : parrello 1.7 =item memTrace
149 :    
150 :     Trace memory usage at the end of each section.
151 :    
152 : parrello 1.5 =item DBD
153 :    
154 :     Name of the DBD file. If specified, the DBD must be in the main FIG directory
155 :     (specified in C<$FIG_Config::fig>). This option allows the use of an alternate
156 :     DBD during load, so that access to the database by other processes is not
157 :     compromised.
158 :    
159 : parrello 1.1 =back
160 :    
161 :     =cut
162 :    
163 :     # Get the command-line options and parameters.
164 :     my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDBGenerate ERDB Stats) ],
165 :     {
166 :     clear => ["", "if specified, the entire load directory will be cleared"],
167 :     clean => ["", "if specified, temporary files in the load directory will be deleted"],
168 :     clearGroups => ["", "if specified, pre-exising load files from the groups processed will be deleted"],
169 : parrello 1.5 maxErrors => ["1", "if non-zero, the maximum allowed number of section failures"],
170 : parrello 1.2 phone => ["", "phone number (international format) to call when load finishes"],
171 : parrello 1.5 trace => ["3", "tracing level"],
172 : parrello 1.1 workers => ["1", "number of worker processes"],
173 : parrello 1.3 label => ["Main", "name of this process"],
174 : parrello 1.5 resume => ["", "if specified, only groups and sections that do not already have load files will be processed"],
175 : parrello 1.1 sections => ["*", "name of a file in the database's load directory containing a list of sections to process"],
176 : parrello 1.5 DBD => ["", "if specified, the name of a DBD file in the FIG directory"],
177 : parrello 1.7 memTrace => ["", "if specified, memory usage will be traced at the end of each section"],
178 : parrello 1.1 },
179 :     "<database> <group1> <group2> ...",
180 :     @ARGV);
181 :     # This is a list of the options that are for manager scripts only.
182 : parrello 1.5 my @managerOptions = qw(clear clean clearGroups sections);
183 : parrello 1.1 # We're doing heavy pipe stuff, so we need to throw an error on a broken-pipe signal.
184 :     local $SIG{PIPE} = sub { Confess("Broken pipe.") };
185 :     # Insure we catch errors.
186 :     eval {
187 :     # Get the parameters.
188 :     my ($database, @groups) = @parameters;
189 : parrello 1.5 # Check for an alternate DBD.
190 :     my $altDBD = ($options->{DBD} ? "$FIG_Config::fig/$options->{DBD}" : undef);
191 : parrello 1.2 # Connect to the database and get its load directory.
192 : parrello 1.5 my $erdb = ERDB::GetDatabase($database, $altDBD);
193 : parrello 1.2 my $directory = $erdb->LoadDirectory();
194 : parrello 1.7 my $source = $erdb->GetSourceObject();
195 : parrello 1.1 # Fix the group list.
196 : parrello 1.2 my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
197 : parrello 1.1 # Are we a worker or a manager?
198 :     if ($options->{workers} == 0) {
199 :     # Yes, we're a worker.
200 : parrello 1.5 Trace("Worker process $options->{label} started.") if T(2);
201 :     LoadFromInput(\*STDIN, $erdb, \@realGroups, $options);
202 : parrello 1.1 } else {
203 :     # Here we're a manager. If the user wants us to clear the directory,
204 :     # do that first.
205 :     if ($options->{clear}) {
206 :     # Count the number of files deleted.
207 :     my $deleteCount = 0;
208 :     # Get a list of the applicable file names.
209 :     my @files = ERDBGenerate::GetLoadFiles($directory);
210 :     # It's worth noting if we didn't find any.
211 :     if (! @files) {
212 : parrello 1.5 Trace("Load directory is already clear.") if T(2);
213 : parrello 1.1 } else {
214 :     # Delete the files we found.
215 :     for my $file (@files) {
216 :     unlink "$directory/$file";
217 :     $deleteCount++;
218 :     }
219 : parrello 1.5 Trace("$deleteCount files deleted from load directory during Clear.") if T(2);
220 : parrello 1.1 }
221 :     } elsif ($options->{clearGroups}) {
222 :     # Here the user only wants to clear the load files for the specified
223 :     # groups. This operation requires significantly greater care. Get
224 :     # the hash of groups to table names.
225 :     my $groupHash = ERDBLoadGroup::GetGroupHash($erdb);
226 :     # Get a list of the files in this directory in alphabetical order.
227 :     my @files = ERDBGenerate::GetLoadFiles($directory);
228 :     # Get a hash of all the tables to be deleted.
229 :     my %tables = map { $_ => 1 } map { @{$groupHash->{$_}} } @realGroups;
230 :     # We'll count the number of files deleted in here.
231 :     my $deleteCount = 0;
232 :     # Loop through all the files in the directory.
233 :     for my $file (@files) {
234 :     # Extract the relevant table name from the file.
235 :     my ($table) = ERDBGenerate::ParseFileName($file);
236 :     if ($tables{$table}) {
237 :     # This is one of our tables, so delete the file.
238 :     unlink "$directory/$file";
239 :     $deleteCount++;
240 : parrello 1.4 Trace("$deleteCount files deleted.") if T(3) && $deleteCount % 100 == 0;
241 : parrello 1.1 }
242 :     }
243 :     Trace("$deleteCount files deleted from load directory during ClearGroups.") if T(2);
244 :     }
245 : parrello 1.2 # Delete any leftover kill file if it exists.
246 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $directory);
247 :     if (-f $killFileName) {
248 :     Trace("Deleting kill file $killFileName.") if T(2);
249 :     unlink $killFileName;
250 :     }
251 : parrello 1.1 # Now we need to get our list of sections. Check to see if the user
252 :     # supplied a section file.
253 :     my $sectionFile = $options->{sections};
254 :     if ($sectionFile eq "*") {
255 :     # No, so we must create one.
256 :     $sectionFile = "$directory/Sections$$.txt";
257 :     Open(\*SECTIONS, ">$sectionFile");
258 : parrello 1.7 for my $section ($erdb->SectionList($source)) {
259 : parrello 1.1 print SECTIONS "$section\n";
260 :     }
261 :     close SECTIONS;
262 :     } elsif ($sectionFile =~ m#^\w#) {
263 :     # Yes, but it doesn't have a directory name, so add one.
264 :     $sectionFile = "$directory/$sectionFile";
265 :     }
266 :     # Compute the options to be used for worker processes (or ourselves if
267 :     # we're sequential).
268 :     my %workerOptions = %{$options};
269 :     # Get rid of the manager-only options.
270 :     for my $optionID (@managerOptions) {
271 :     delete $workerOptions{$optionID};
272 :     }
273 :     # Insure the worker knows what it is.
274 :     $workerOptions{workers} = 0;
275 :     $workerOptions{forked} = 1;
276 :     $workerOptions{background} = 1;
277 :     # Prepare to read the section file.
278 :     my $ih = Open(undef, "<$sectionFile");
279 :     # Are we a sequential load or a multi-worker manager?
280 :     my $numWorkers = $options->{workers};
281 :     if ($numWorkers == 1) {
282 : parrello 1.2 # We're sequential, so we do all the work ourselves.
283 : parrello 1.1 Trace("Sequential load started.") if T(2);
284 : parrello 1.5 LoadFromInput($ih, $erdb, \@realGroups, \%workerOptions);
285 : parrello 1.1 } else {
286 : parrello 1.5 # Here we need to create the workers. The following array will contain
287 :     # a descriptor for each worker.
288 : parrello 1.1 my @workers = ();
289 : parrello 1.5 # Compute the positional parameters to use for the workers.
290 : parrello 1.3 my $commandParms = join(" ", $database, @realGroups);
291 :     my $command = $0;
292 : parrello 1.1 # Create the workers.
293 :     for (my $i = 0; $i < $numWorkers; $i++) {
294 : parrello 1.3 my $label = "$options->{label}$i";
295 :     $workerOptions{label} = $label;
296 :     my $commandOptions = Tracer::UnparseOptions(\%workerOptions);
297 : parrello 1.5 my $inFile = "$FIG_Config::temp/Pipe-$label.tbl";
298 :     my $oh = Open(undef, ">$inFile");
299 :     my $command = "$command $commandOptions $commandParms <$inFile >null &";
300 :     push @workers, { handle => $oh, label => $label, command => $command };
301 : parrello 1.1 }
302 :     # Now we assign sections to the workers.
303 : parrello 1.3 my $w = 0;
304 : parrello 1.1 while (! eof $ih) {
305 :     # Get the name of the next section.
306 :     my $line = <$ih>;
307 : parrello 1.5 # Get the output handle for the next worker in rotation.
308 :     my $wh = $workers[$w]->{handle};
309 : parrello 1.1 # Send this section to it.
310 : parrello 1.5 print $wh $line;
311 :     Trace(Tracer::Strip($line) . " sent to $workers[$w]->{label}") if T(3);
312 : parrello 1.3 # Position on the next worker.
313 :     $w = ($w + 1) % $numWorkers;
314 : parrello 1.1 }
315 : parrello 1.5 # All done, close the files.
316 :     for my $worker (@workers) {
317 :     close $worker->{handle};
318 :     }
319 :     # Now start the workers.
320 : parrello 1.1 for my $worker (@workers) {
321 : parrello 1.5 my $cmd = $worker->{command};
322 :     Trace("Starting: $cmd") if T(3);
323 :     system($worker->{command});
324 : parrello 1.1 }
325 :     }
326 : parrello 1.4 Trace("Load manager completed.") if T(2);
327 : parrello 1.1 }
328 :     };
329 :     if ($@) {
330 :     Trace("Script failed with error: $@") if T(0);
331 :     } else {
332 :     Trace("Script complete.") if T(2);
333 :     }
334 :     if ($options->{phone}) {
335 :     my $msgID = Tracer::SendSMS($options->{phone}, "ERDBGenerator has ended.");
336 :     if ($msgID) {
337 :     Trace("Phone message sent with ID $msgID.") if T(2);
338 :     } else {
339 :     Trace("Phone message not sent.") if T(2);
340 :     }
341 :     }
342 :    
343 : parrello 1.6 =head2 Internal Methods
344 :    
345 : parrello 1.1 =head3 LoadFromInput
346 :    
347 :     LoadFromInput($ih, $erdb, \@groups, \%options);
348 :    
349 :     Load one or more sections of data for the specified table groups. The IDs
350 :     of the data sections will be read from the standard input. The groups
351 :     will be loaded in the order specified, once per section.
352 :    
353 :     =over 4
354 :    
355 :     =item ih
356 :    
357 :     File handle for the input stream containing the list of sections to process.
358 :    
359 :     =item erdb
360 :    
361 :     Database object containing information about the tables being loaded.
362 :    
363 :     =item groups
364 :    
365 :     Reference to a list of the names for the load groups to process.
366 :    
367 :     =item options
368 :    
369 :     Reference to a hash of the options passed in from the command line.
370 :    
371 :     =back
372 :    
373 :     =cut
374 :    
375 :     sub LoadFromInput {
376 :     # Get the parameters.
377 : parrello 1.5 my ($ih, $erdb, $groups, $options) = @_;
378 : parrello 1.1 # We'll count our errors in here.
379 :     my $errorCount = 0;
380 :     my $maxErrors = $options->{maxErrors};
381 : parrello 1.2 # Compute the kill file name.
382 :     my $killFileName = ERDBLoadGroup::KillFileName($erdb, $erdb->LoadDirectory());
383 :     my $killed = 0;
384 : parrello 1.5 # Slurp in the sections.
385 :     my @sections = ();
386 :     while (! eof $ih) {
387 :     push @sections, Tracer::GetLine($ih);
388 :     }
389 :     # Loop through the groups.
390 :     for my $group (@$groups) {
391 :     # Create a loader for this group.
392 :     my $loader = $erdb->Loader($group, $options);
393 :     # Loop through the sections.
394 :     for my $section (@sections) {
395 : parrello 1.2 # Only proceed if we haven't been killed.
396 :     if (! $killed) {
397 :     # Check for a kill file.
398 :     if (-f $killFileName) {
399 :     # Found one, so kill ourselves.
400 : parrello 1.3 Trace("$options->{label} terminated by kill file.") if T(2);
401 : parrello 1.2 $killed = 1;
402 :     } else {
403 :     # No kill file, so we process the section.
404 : parrello 1.5 Trace("Processing section $section for group $group in $options->{label}.") if T(3);
405 : parrello 1.7 # Memorize the current memory footprint.
406 :     my $memory0 = Tracer::GetMemorySize();
407 : parrello 1.5 my $ok = $loader->ProcessSection($section);
408 : parrello 1.7 # Do memory tracing.
409 :     if ($options->{memTrace}) {
410 :     my $memory1 = Tracer::GetMemorySize();
411 :     Trace("Memory usage by $options->{label} for $group $section was $memory0 to $memory1.") if T(2);
412 :     }
413 : parrello 1.2 # Check to see if we've exceeded the maximum error count. We only care
414 :     # if maxErrors is nonzero.
415 :     if (! $ok && $maxErrors && ++$errorCount >= $maxErrors) {
416 :     Trace("Error limit exceeded in database loader.") if T(0);
417 :     $killed = 1;
418 :     }
419 :     }
420 : parrello 1.1 }
421 :     }
422 : parrello 1.5 Trace("Statistics for $group in $options->{label}:\n" . $loader->DisplayStats()) if T(2);
423 : parrello 1.1 }
424 : parrello 1.5 Trace("Processing finished for worker $options->{label}.") if T(2);
425 : parrello 1.1 }
426 :    
427 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3