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

Annotation of /Sprout/ERDBGenerator.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3