ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/JOBROBOT/TaskSource
Revision: 1.26
Committed: Tue Nov 7 23:21:45 2006 UTC (18 years, 5 months ago) by gutsche
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
bug fix in check if number of pending jobs is larger than maximum value

File Contents

# User Rev Content
1 gutsche 1.1 #!/usr/bin/env perl
2    
3     ##H This drop box agent initiates tasks on all published datasets.
4     ##H A task is an application to run on a dataset at a particular site.
5     ##H It keeps a record of tasks created and avoids submitting a task too
6     ##H frequently.
7     ##H
8     ##H Usage:
9     ##H
10 gutsche 1.21 ##H TaskSource
11     ##H
12     ##H -state agent state directory, including inbox
13     ##H -next next agent to pass the drops to; can be given several times
14     ##H -wait time to wait in seconds between work scans
15     ##H -sec_between_inits seconds between submission of same dataset/tier combination
16     ##H -siteconfig file with list of valid SE and their schedule limits
17 gutsche 1.25 ##H -dbsdlspath path to DBS/DLS discovery ascii files
18 gutsche 1.21 ##H -max-site-queue default value for maximum events scheduled per site
19 gutsche 1.22 ##H -max-jobs maximum number of jobs per project, -1 to deactivate
20 gutsche 1.21 ##H -scheduler scheduler
21     ##H -events number of events per job
22     ##H -pattern DBS discovery pattern
23     ##H -validtiers perl string pattern with valid tiers (DIGI|RECO)
24     ##H -simcfg CMSSW parameter-set for tier SIM
25     ##H -simoutput putput (comma separated) for tier SIM
26     ##H -digicfg CMSSW parameter-set for tier DIGI
27     ##H -digioutput putput (comma separated) for tier DIGI
28     ##H -recocfg CMSSW parameter-set for tier RECO
29     ##H -recooutput putput (comma separated) for tier RECO
30 gutsche 1.1
31     BEGIN {
32     use strict; use warnings; $^W=1;
33     our $me = $0; $me =~ s|.*/||;
34     our $home = $0; $home =~ s|/[^/]+$||; $home ||= "."; $home .= "/../PHEDEX/Toolkit/Common";
35     unshift(@INC, $home);
36     }
37    
38     ######################################################################
39     use UtilsHelp;
40 gutsche 1.21 my %args = (WAITTIME => 600, SECS_PER_EVENT => 1., MAX_SITE_QUEUE => 10);
41     while (scalar @ARGV) {
42     if ($ARGV[0] eq '-state' && scalar @ARGV > 1) {
43     shift (@ARGV); $args{DROPDIR}= shift(@ARGV);
44     } elsif ($ARGV[0] eq '-next' && scalar @ARGV > 1) {
45     shift (@ARGV); push (@{$args{NEXTDIR}}, shift(@ARGV));
46     } elsif ($ARGV[0] eq '-wait' && scalar @ARGV > 1) {
47     shift (@ARGV); $args{WAITTIME} = shift(@ARGV);
48     } elsif ($ARGV[0] eq '-secs_between_inits' && scalar @ARGV > 1) {
49     shift (@ARGV); $args{SECS_BETWEEN_INITS} = shift(@ARGV);
50     } elsif ($ARGV[0] eq '-siteconfig' && scalar @ARGV > 1) {
51     shift (@ARGV); $args{SITECONFIG} = shift(@ARGV);
52 gutsche 1.25 } elsif ($ARGV[0] eq '-dbsdlspath' && scalar @ARGV > 1) {
53     shift (@ARGV); $args{DBSDLSPATH} = shift(@ARGV);
54 gutsche 1.21 } elsif ($ARGV[0] eq '-max-site-queue' && scalar @ARGV > 1) {
55     shift (@ARGV); $args{MAX_SITE_QUEUE} = shift(@ARGV);
56 gutsche 1.22 } elsif ($ARGV[0] eq '-max-jobs' && scalar @ARGV > 1) {
57     shift (@ARGV); $args{MAX_JOBS} = shift(@ARGV);
58 gutsche 1.21 } elsif ($ARGV[0] eq '-scheduler' && scalar @ARGV > 1) {
59     shift (@ARGV); $args{SCHEDULER} = shift(@ARGV);
60     } elsif ($ARGV[0] eq '-events' && scalar @ARGV > 1) {
61     shift (@ARGV); $args{EVENTS} = shift(@ARGV);
62     } elsif ($ARGV[0] eq '-pattern' && scalar @ARGV > 1) {
63     shift (@ARGV); $args{PATTERN} = shift(@ARGV);
64     } elsif ($ARGV[0] eq '-validtiers' && scalar @ARGV > 1) {
65     shift (@ARGV); $args{VALIDTIERS} = shift(@ARGV);
66     } elsif ($ARGV[0] eq '-simcfg' && scalar @ARGV > 1) {
67     shift (@ARGV); $args{SIMCFG} = shift(@ARGV);
68     } elsif ($ARGV[0] eq '-simoutput' && scalar @ARGV > 1) {
69     shift (@ARGV); $args{SIMOUTPUT} = shift(@ARGV);
70     } elsif ($ARGV[0] eq '-digicfg' && scalar @ARGV > 1) {
71     shift (@ARGV); $args{DIGICFG} = shift(@ARGV);
72     } elsif ($ARGV[0] eq '-digioutput' && scalar @ARGV > 1) {
73     shift (@ARGV); $args{DIGIOUTPUT} = shift(@ARGV);
74     } elsif ($ARGV[0] eq '-recocfg' && scalar @ARGV > 1) {
75     shift (@ARGV); $args{RECOCFG} = shift(@ARGV);
76     } elsif ($ARGV[0] eq '-recooutput' && scalar @ARGV > 1) {
77     shift (@ARGV); $args{RECOOUTPUT} = shift(@ARGV);
78     } elsif ($ARGV[0] eq '-h') {
79     &usage();
80     } else {
81     last;
82     }
83 gutsche 1.1 }
84    
85 gutsche 1.21 if (@ARGV || !$args{DROPDIR}) {
86     die "Insufficient parameters, use -h for help.\n";
87 gutsche 1.1 }
88    
89     (new TaskSource (%args))->process();
90    
91     ######################################################################
92     # Routines specific to this agent.
93     package TaskSource; use strict; use warnings; use base 'UtilsAgent';
94     use File::Path;
95     use UtilsCommand;
96     use UtilsLogging;
97     use UtilsTiming;
98     use UtilsNet;
99     use POSIX;
100    
101     sub new
102 gutsche 1.21 {
103 gutsche 1.1 my $proto = shift;
104     my $class = ref($proto) || $proto;
105     my $self = $class->SUPER::new(@_);
106 gutsche 1.21 my %params = (MAX_SITE_QUEUE => undef, # max number of jobs per site
107 gutsche 1.22 MAX_JOBS => undef, # max number of jobs per project, -1 to deactivate
108 gutsche 1.21 SECS_BETWEEN_INITS => 600, # path to siteconfig file
109     SITECONFIG => undef, # path to siteconfig file
110 gutsche 1.25 DBSDLSPATH => "", # path to DBSDLS discovery ascii files
111 gutsche 1.21 EVENTS => 1000, # number of events per job
112     PATTERN => "*", # DBS discovery pattern
113     VALIDTIERS => undef, # perl string pattern with valid tiers (DIGI|RECO)
114     SIMCFG => undef, # CMSSW parameter-set for SIM tier
115     DIGICFG => undef, # CMSSW parameter-set for DIGI tier
116     RECOCFG => undef, # CMSSW parameter-set for RECO tier
117     SIMOUTPUT => undef, # output (comma separated) for SIM tier
118     DIGIOUTPUT => undef, # output (comma separated) for DIGI tier
119     RECOOUTPUT => undef, # output (comma separated) for RECO tier
120     SCHEDULER => "edg"); # standard scheduler
121 gutsche 1.1 my %args = (@_);
122     map { $self->{$_} = defined $args{$_} ? $args{$_} : $params{$_} } keys %params;
123     bless $self, $class;
124     return $self;
125 gutsche 1.21 }
126 gutsche 1.1
127     sub init
128 gutsche 1.21 {
129 gutsche 1.1 my ($self) = @_;
130     $self->{TASKREPO} = "$self->{DROPDIR}/tasks";
131     -d "$self->{TASKREPO}"
132 gutsche 1.21 || mkdir "$self->{TASKREPO}"
133 gutsche 1.1 || die "$self->{TASKREPO}: cannot create directory: $!\n";
134    
135     # Determine if links supports -dump-width option
136     $self->{LINKS_OPTS} = [];
137     open (LINKS_HELP, "links -help 2>/dev/null |");
138 gutsche 1.4 if ( grep(/-no-numbering/, <LINKS_HELP>) ) {
139     push(@{$self->{LINKS_OPTS}}, qw(-dump-width 300 -no-numbering 1));
140     } elsif ( grep(/-dump-width/, <LINKS_HELP>) ) {
141     push(@{$self->{LINKS_OPTS}}, qw(-dump-width 300));
142 gutsche 1.1 }
143     close (LINKS_HELP);
144 gutsche 1.21 }
145 gutsche 1.1
146     # Find out how many jobs are pending for each site. This is
147     # insensitive to the job type, and we only check once in the
148     # beginning to avoid favouring one dataset over another --
149     # once we decide to proceed for a site, we submit jobs for
150     # all datasets.
151     sub getSiteStatus
152 gutsche 1.18 {
153 gutsche 1.1 my ($self) = @_;
154     my %result = ();
155     my $taskrepo = $self->{TASKREPO};
156 gutsche 1.21 foreach my $site (<$taskrepo/*/*>) {
157     my ($sitename) = ($site =~ m|.*/(.*)|);
158     foreach my $d (<$site/*/*>) {
159     if (! -f "$d/JOB_CREATE_LOG.txt") {
160     $result{$sitename}{C} ||= 0;
161     $result{$sitename}{C}++;
162     next;
163     }
164 gutsche 1.18
165 gutsche 1.21 my $f = (<$d/crab_*/share/db/jobs>)[0];
166     next if ! defined $f;
167 gutsche 1.18
168 gutsche 1.21 foreach my $status (split(/\n/, &input($f) || '')) {
169     my @statusarray = split('\|', $status);
170     $result{$sitename}{$statusarray[1]} ||= 0;
171     $result{$sitename}{$statusarray[1]}++;
172     }
173 gutsche 1.18 }
174 gutsche 1.21 }
175 gutsche 1.18
176 gutsche 1.1 return %result;
177 gutsche 1.18 }
178 gutsche 1.1
179 gutsche 1.21 sub createHashFromString
180     {
181     my ($self, $string) = @_;
182     my $cmd = "mktemp /tmp/hash_XXXXXX";
183     open (HASHFILE, "$cmd 2>/dev/null |") or die "cannot run `$cmd': $!\n";
184     my $hash_file_name;
185     foreach my $hash_line (split(/\n/, <HASHFILE> || '')) {
186     chomp($hash_line);
187     $hash_file_name = $hash_line;
188     }
189     $cmd = "echo $string >> $hash_file_name";
190     system $cmd;
191     # open (FILLHASHFILE, "$cmd 2>/dev/null |") or die "cannot run `$cmd': $!\n";
192     $cmd = "cksum $hash_file_name";
193     open (CKSUMHASHFILE, "$cmd 2>/dev/null |") or die "cannot run `$cmd': $!\n";
194     my ($hash, $dummy_one, $dummy_two);
195     foreach my $hash_output (split(/\n/, <CKSUMHASHFILE> || '')) {
196     chomp($hash_output);
197     ($hash, $dummy_one, $dummy_two) = split(/ /,$hash_output);
198     }
199     $cmd = "rm -f $hash_file_name";
200     system $cmd;
201     # open (RMHASHFILE, "$cmd 2>/dev/null |") or die "cannot run `$cmd': $!\n";
202    
203     return $hash;
204     }
205    
206 gutsche 1.1 sub idle
207     {
208 gutsche 1.21 &logmsg ("DBS/DLS discovery");
209 gutsche 1.1 my ($self, @pending) = @_;
210     eval {
211     # Get status of how busy the sites are. We obtain this only once
212     # in order to not favour datasets "early on" in the list.
213     my %sitestats = $self->getSiteStatus ();
214 gutsche 1.21 if (keys %sitestats) {
215     my @load;
216     foreach my $site (sort keys %sitestats) {
217     push (@load, "$site" . join("", map { " $_=$sitestats{$site}{$_}" }
218     sort keys %{$sitestats{$site}}));
219 gutsche 1.1 }
220 gutsche 1.21 &logmsg ("current load: ", join ("; ", @load));
221     }
222 gutsche 1.1
223 gutsche 1.25 # DBS/DLS discovery
224     # if ascii DBS/DLS discovery file exists in DBSDLSPATH with name: dlsdls_query_$cleaned_pattern.txt
225     # where cleaned pattern is the pattern with / replaced by _
226     # take ascii file, otherwise do discovery
227     my $cleaned_pattern = $self->{PATTERN};
228     $cleaned_pattern =~ s/\//_/g;
229     my $patternfile = $self->{DBSDLSPATH} . "/dbsdls_query_" . $cleaned_pattern . ".txt";
230     if ( -e "$patternfile" ) {
231     &logmsg("Use DBS/DLS ascii file: $patternfile");
232     open (PUBLISHED,"< $patternfile");
233     } else {
234     my $cmd = $ENV{PYTHONSCRIPT} . "/DBSDLSQuery.py --pattern \"$self->{PATTERN}\"";
235     open (PUBLISHED, "$cmd 2>/dev/null |") or die "cannot run `$cmd': $!\n";
236     }
237 gutsche 1.21 while (<PUBLISHED>) {
238     chomp;
239     &timeStart($self->{STARTTIME});
240     my ($datapath, $site, $totalevents) = split(/ /, $_);
241     # read in siteconfig dynamically
242     if ( $self->{SITECONFIG} ) {
243     open(siteconfig_file,"$self->{SITECONFIG}");
244     my @line_array = <siteconfig_file>;
245     my $validsites = "(";
246     for (my $i = 0; $i < scalar @line_array; ++$i) {
247     if (defined $line_array[$i] ) {
248     if ( $line_array[$i] !~ /^#/ && $line_array[$i] !~ /^\n/ ) {
249     my ($tempsite, $tempshortsite, $templimit) = split(/ /,$line_array[$i]);
250     chomp($tempsite);
251     $validsites = $validsites . $tempsite . "|";
252 gutsche 1.19 }
253     }
254 gutsche 1.5 }
255 gutsche 1.21 chop($validsites);
256     $validsites = $validsites . ")";
257     close siteconfig_file;
258 gutsche 1.24 if ( $validsites eq ")" ) {
259     next;
260     } else {
261     next if ($validsites && $site !~ /$validsites/);
262     }
263 gutsche 1.5 }
264 gutsche 1.22 # check if totalevents is larger than $EVENTS * $MAX_JOBS
265     if ($self->{MAX_JOBS} != -1) {
266     if ( $self->{MAX_JOBS} * $self->{EVENTS} < $totalevents ) {
267     $totalevents = $self->{MAX_JOBS} * $self->{EVENTS};
268     }
269     }
270 gutsche 1.21 $self->createTask($datapath, $site, $totalevents, %sitestats);
271 gutsche 1.1 }
272     };
273     do { chomp ($@); &alert ($@); } if $@;
274    
275     $self->nap ($self->{WAITTIME});
276     }
277    
278     sub createTask()
279     {
280 corvo 1.3 my ($self) = shift(@_);
281 gutsche 1.5 my ($datasetpath, $site, $totalevents, %sitestats) = @_;
282    
283     my($n, $dataset, $tier, $owner) = split(/\//, $datasetpath);
284    
285 gutsche 1.21 # filter on tier
286     next if ($self->{VALIDTIERS} && $tier !~ /$self->{VALIDTIERS}/);
287    
288     my ($cfg, $output);
289     if ($tier =~ /RECO/) {
290     $cfg = $self->{RECOCFG};
291     $output = $self->{RECOOUTPUT};
292     } elsif ($tier =~ /DIGI/) {
293     $cfg = $self->{DIGICFG};
294     $output = $self->{DIGIOUTPUT};
295     } elsif ($tier =~ /SIM/) {
296     $cfg = $self->{SIMCFG};
297     $output = $self->{SIMOUTPUT};
298     } else {
299     &logmsg ("No valid tier, defaults for cfg and output");
300     $cfg = "robot.cfg";
301     $output = "";
302 gutsche 1.5 }
303    
304 gutsche 1.8 # take site specific or if not found default max site queue
305 gutsche 1.20 # read in from siteconfig file
306     my %sitelimits = ();
307 gutsche 1.21 my %siteNameTranslation = ();
308 gutsche 1.20 if ( $self->{SITECONFIG} ) {
309     open(siteconfig_file,"$self->{SITECONFIG}");
310     my @line_array = <siteconfig_file>;
311     for (my $i = 0; $i < scalar @line_array; ++$i) {
312     if (defined $line_array[$i] ) {
313     if ( $line_array[$i] !~ /^#/ && $line_array[$i] !~ /^\n/ ) {
314 gutsche 1.21 my ($tempsite, $tempshortsite, $templimit) = split(/ /,$line_array[$i]);
315 gutsche 1.20 chomp($tempsite);
316 gutsche 1.21 chomp($tempshortsite);
317 gutsche 1.20 chomp($templimit);
318     $sitelimits{$tempsite} = $templimit;
319 gutsche 1.21 $siteNameTranslation{$tempsite} = $tempshortsite;
320 gutsche 1.20 }
321     }
322     }
323     close siteconfig_file;
324     }
325 gutsche 1.21
326     # If the site isn't too busy already, ignore.
327     my $pending = ($sitestats{$siteNameTranslation{$site}}{S} || 0);
328     $pending += ($sitestats{$siteNameTranslation{$site}}{C} || 0);
329 gutsche 1.20 my $max_queue = $sitelimits{$site} || $self->{MAX_SITE_QUEUE};
330 gutsche 1.26 if ( $pending > $max_queue ) {
331 gutsche 1.4 next;
332     }
333    
334     # Find out what is already pending for this task. First find all
335     # existing tasks in the repository, the latest generation.
336     my $datestamp = strftime ("%y%m%d", gmtime(time()));
337 gutsche 1.21 my $taskdir = "$self->{TASKREPO}/$datestamp/$siteNameTranslation{$site}/$tier";
338    
339     # create short unique taskbase from $datestamp.$site.$dataset.$tier.$owner
340     my $taskbase = $self->createHashFromString ("$datestamp.$site.$dataset.$tier.$owner");
341     chomp($taskbase);
342    
343 gutsche 1.4 my @existing = sort { $a <=> $b } map { /.*\.(\d+)$/ } <$taskdir/$taskbase.*>;
344     my $curgen = pop(@existing) || 0;
345     my $nextgen = $curgen + 1;
346    
347     # OK to create the task if enough time has passed from previous
348     # task creation, or there is no previous task.
349     if (! -f "$taskdir/$taskbase.$curgen/crab.cfg"
350 gutsche 1.21 || (((stat("$taskdir/$taskbase.$curgen/crab.cfg"))[9]
351     < time() - $self->{SECS_BETWEEN_INITS})))
352 gutsche 1.4 {
353     my $mydir = $0; $mydir =~ s|/[^/]+$||;
354     my $drop = sprintf("%s.%03d", $taskbase, $nextgen);
355 gutsche 1.21 my $ret = &runcmd ("$mydir/CrabJobs",
356     "-cfg", "$mydir/$cfg",
357     "-datasetpath", $datasetpath,
358 gutsche 1.4 "-output", $output,
359 gutsche 1.21 "-totalevents", $totalevents,
360     "-whitelist", $site,
361     "-eventsperjob", $self->{EVENTS},
362 gutsche 1.4 "-scheduler", $self->{SCHEDULER},
363 gutsche 1.21 "-jobname", "$taskdir/$drop");
364 gutsche 1.4 die "$drop: failed to create task: @{[&runerror($ret)]}\n" if $ret;
365 gutsche 1.21
366 gutsche 1.4 &output ("$taskdir/$drop/TASK_INIT.txt",
367     &mytimeofday () . "\n");
368    
369     my $dropdir = "$self->{WORKDIR}/$drop";
370     mkdir "$dropdir" || die "$dropdir: cannot create: $!\n";
371 gutsche 1.21 if (&output ("$dropdir/task", "$taskdir/$drop")) {
372     &touch ("$dropdir/done");
373     $self->relayDrop ($drop);
374 gutsche 1.23 &logmsg("stats: $drop $datestamp $site $dataset $tier $owner @{[&formatElapsedTime($self->{STARTTIME})]} success");
375 gutsche 1.21 } else {
376 gutsche 1.23 &alert ("$drop $datestamp $site $dataset $tier $owner: failed to create drop");
377 gutsche 1.21 &rmtree ([ "$self->{WORKDIR}/$drop" ]);
378     }
379 gutsche 1.4 }
380     }