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

Annotation of /Sprout/ERDBGenerator.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3