ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/Submitter.py
Revision: 1.139
Committed: Mon Feb 9 21:16:34 2009 UTC (16 years, 2 months ago) by ewv
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_5_0_pre3
Changes since 1.138: +7 -0 lines
Log Message:
Fix [bug #46637] Job identifyer for Dashboard reporting in case of submission to local CAF at US sites does not follow naming convention

File Contents

# User Rev Content
1 nsmirnov 1.1 from Actor import *
2 nsmirnov 1.6 from crab_util import *
3 nsmirnov 1.2 import common
4 corvo 1.9 from ApmonIf import ApmonIf
5 slacapra 1.60 #from random import random
6 corvo 1.30 import time
7 ewv 1.128 import sha
8 ewv 1.139 import socket
9    
10 slacapra 1.60 from ProgressBar import ProgressBar
11     from TerminalController import TerminalController
12 nsmirnov 1.1
13     class Submitter(Actor):
14 slacapra 1.84 def __init__(self, cfg_params, parsed_range, val):
15 nsmirnov 1.1 self.cfg_params = cfg_params
16 slacapra 1.84
17     # get user request
18     nsjobs = -1
19     chosenJobsList = None
20     if val:
21 slacapra 1.91 if val=='range': # for Resubmitter
22     chosenJobsList = parsed_range
23 ewv 1.92 elif val=='all':
24 slacapra 1.84 pass
25     elif (type(eval(val)) is int) and eval(val) > 0:
26     # positive number
27     nsjobs = eval(val)
28     elif (type(eval(val)) is tuple)or( type(eval(val)) is int and eval(val)<0 ) :
29     chosenJobsList = parsed_range
30 ewv 1.92 nsjobs = len(chosenJobsList)
31 slacapra 1.84 else:
32     msg = 'Bad submission option <'+str(val)+'>\n'
33     msg += ' Must be an integer or "all"'
34     msg += ' Generic range is not allowed"'
35     raise CrabException(msg)
36     pass
37 ewv 1.92
38 slacapra 1.84 common.logger.debug(5,'nsjobs '+str(nsjobs))
39     # total jobs
40     nj_list = []
41     # get the first not already submitted
42 spiga 1.115 self.complete_List = common._db.nJobs('list')
43     common.logger.debug(5,'Total jobs '+str(len(self.complete_List)))
44 slacapra 1.84 jobSetForSubmission = 0
45     jobSkippedInSubmission = []
46     datasetpath=self.cfg_params['CMSSW.datasetpath']
47 slacapra 1.97 if string.lower(datasetpath)=='none':
48 fanzago 1.104 datasetpath = None
49 spiga 1.115 tmp_jList = self.complete_List
50 slacapra 1.84 if chosenJobsList != None:
51     tmp_jList = chosenJobsList
52     # build job list
53 ewv 1.133 from WMCore.SiteScreening.BlackWhiteListParser import SEBlackWhiteListParser
54 ewv 1.135 seWhiteList = cfg_params.get('EDG.se_white_list',[])
55     seBlackList = cfg_params.get('EDG.se_black_list',[])
56     self.blackWhiteListParser = SEBlackWhiteListParser(seWhiteList, seBlackList, common.logger)
57 spiga 1.134 for job in common._db.getTask(tmp_jList).jobs:
58 ewv 1.135 cleanedBlackWhiteList = self.blackWhiteListParser.cleanForBlackWhiteList(job['dlsDestination'])
59 ewv 1.128 if (cleanedBlackWhiteList != '') or (datasetpath == None):
60 spiga 1.134 if ( job.runningJob['status'] in ['C','RC'] and \
61     job.runningJob['statusScheduler'] in ['Created',None]):
62 slacapra 1.84 jobSetForSubmission +=1
63 ewv 1.135 nj_list.append(job['id'])
64 ewv 1.92 else:
65 slacapra 1.84 continue
66     else :
67 spiga 1.134 jobSkippedInSubmission.append( job['id'] )
68 slacapra 1.84 if nsjobs >0 and nsjobs == jobSetForSubmission:
69     break
70     pass
71     if nsjobs>jobSetForSubmission:
72 spiga 1.134 common.logger.message('asking to submit '+str(nsjobs)+' jobs, but only '+\
73     str(jobSetForSubmission)+' left: submitting those')
74 slacapra 1.84 if len(jobSkippedInSubmission) > 0 :
75     mess =""
76     for jobs in jobSkippedInSubmission:
77     mess += str(jobs) + ","
78     common.logger.message("Jobs: " +str(mess) + "\n skipped because no sites are hosting this data\n")
79 slacapra 1.89 self.submissionError()
80     pass
81 slacapra 1.84 # submit N from last submitted job
82     common.logger.debug(5,'nj_list '+str(nj_list))
83 ewv 1.92
84 corvo 1.30
85 slacapra 1.84 self.nj_list = nj_list
86 nsmirnov 1.1 return
87 ewv 1.92
88 nsmirnov 1.1 def run(self):
89 nsmirnov 1.2 """
90 slacapra 1.53 The main method of the class: submit jobs in range self.nj_list
91 nsmirnov 1.2 """
92     common.logger.debug(5, "Submitter::run() called")
93 slacapra 1.24
94 spiga 1.112 start = time.time()
95    
96 ewv 1.128 check = self.checkIfCreate()
97    
98 spiga 1.112 if check == 0 :
99     self.SendMLpre()
100 ewv 1.128
101     list_matched , task = self.performMatch()
102     njs = self.perfromSubmission(list_matched, task)
103    
104 spiga 1.112 stop = time.time()
105     common.logger.debug(1, "Submission Time: "+str(stop - start))
106     common.logger.write("Submission time :"+str(stop - start))
107 ewv 1.128
108 spiga 1.112 msg = '\nTotal of %d jobs submitted'%njs
109     if njs != len(self.nj_list) :
110     msg += ' (from %d requested).'%(len(self.nj_list))
111     else:
112     msg += '.'
113     common.logger.message(msg)
114 ewv 1.128
115 spiga 1.112 if (njs < len(self.nj_list) or len(self.nj_list)==0):
116     self.submissionError()
117    
118    
119 ewv 1.128 def checkIfCreate(self):
120 spiga 1.112 """
121     """
122     code = 0
123 spiga 1.94 totalCreatedJobs = 0
124 spiga 1.134 task=common._db.getTask()
125     for job in task.jobs:
126     if job.runningJob['status'] in ['C','RC'] \
127     and job.runningJob['statusScheduler'] == 'Created':totalCreatedJobs +=1
128 slacapra 1.24
129     if (totalCreatedJobs==0):
130 spiga 1.112 common.logger.message("No jobs to be submitted: first create them")
131 ewv 1.128 code = 1
132     return code
133 ewv 1.92
134 gutsche 1.70
135 ewv 1.128 def performMatch(self):
136     """
137 spiga 1.113 """
138 spiga 1.114 common.logger.message("Checking available resources...")
139 ewv 1.128 ### define here the list of distinct destinations sites list
140 spiga 1.94 distinct_dests = common._db.queryDistJob_Attr('dlsDestination', 'jobId' ,self.nj_list)
141    
142    
143     ### define here the list of jobs Id for each distinct list of sites
144 spiga 1.112 self.sub_jobs =[] # list of jobs Id list to submit
145 spiga 1.95 jobs_to_match =[] # list of jobs Id to match
146 ewv 1.128 all_jobs=[]
147 spiga 1.94 count=0
148 ewv 1.128 for distDest in distinct_dests:
149 spiga 1.94 all_jobs.append(common._db.queryAttrJob({'dlsDestination':distDest},'jobId'))
150     sub_jobs_temp=[]
151     for i in self.nj_list:
152 ewv 1.128 if i in all_jobs[count]: sub_jobs_temp.append(i)
153 spiga 1.94 if len(sub_jobs_temp)>0:
154 ewv 1.128 self.sub_jobs.append(sub_jobs_temp)
155 spiga 1.112 jobs_to_match.append(self.sub_jobs[count][0])
156 spiga 1.103 count +=1
157 spiga 1.94 sel=0
158 ewv 1.128 matched=[]
159 spiga 1.95
160     task=common._db.getTask()
161    
162     for id_job in jobs_to_match :
163 spiga 1.121 match = common.scheduler.listMatch(distinct_dests[sel], False)
164 slacapra 1.111 if len(match)>0:
165 spiga 1.121 common.logger.message("Found compatible site(s) for job "+str(id_job))
166 slacapra 1.110 matched.append(sel)
167 spiga 1.77 else:
168 spiga 1.112 common.logger.message("No compatible site found, will not submit jobs "+str(self.sub_jobs[sel]))
169 slacapra 1.110 self.submissionError()
170 spiga 1.94 sel += 1
171 ewv 1.92
172 ewv 1.128 return matched , task
173 spiga 1.112
174     def perfromSubmission(self,matched,task):
175    
176 ewv 1.128 njs=0
177    
178 spiga 1.94 ### Progress Bar indicator, deactivate for debug
179     if not common.logger.debugLevel() :
180 slacapra 1.110 term = TerminalController()
181 ewv 1.128
182     if len(matched)>0:
183 spiga 1.94 common.logger.message(str(len(matched))+" blocks of jobs will be submitted")
184 ewv 1.128 for ii in matched:
185 spiga 1.112 common.logger.debug(1,'Submitting jobs '+str(self.sub_jobs[ii]))
186    
187 slacapra 1.110 try:
188 spiga 1.112 common.scheduler.submit(self.sub_jobs[ii],task)
189 slacapra 1.110 except CrabException:
190     raise CrabException("Job not submitted")
191    
192 corvo 1.74 if not common.logger.debugLevel() :
193 spiga 1.112 try: pbar = ProgressBar(term, 'Submitting '+str(len(self.sub_jobs[ii]))+' jobs')
194 corvo 1.74 except: pbar = None
195 spiga 1.94 if not common.logger.debugLevel():
196     if pbar :
197 spiga 1.112 pbar.update(float(ii+1)/float(len(self.sub_jobs)),'please wait')
198 ewv 1.128 ### check the if the submission succeded Maybe not neede
199 spiga 1.94 if not common.logger.debugLevel():
200     if pbar :
201 spiga 1.112 pbar.update(float(ii+1)/float(len(self.sub_jobs)),'please wait')
202 ewv 1.92
203 ewv 1.128 ### check the if the submission succeded Maybe not needed or at least simplified
204 spiga 1.112 sched_Id = common._db.queryRunJob('schedulerId', self.sub_jobs[ii])
205 spiga 1.95 listId=[]
206 spiga 1.94 run_jobToSave = {'status' :'S'}
207 spiga 1.108 listRunField = []
208 ewv 1.128 for j in range(len(self.sub_jobs[ii])):
209     if str(sched_Id[j]) != '':
210     listId.append(self.sub_jobs[ii][j])
211     listRunField.append(run_jobToSave)
212 spiga 1.112 common.logger.debug(5,"Submitted job # "+ str(self.sub_jobs[ii][j]))
213 spiga 1.94 njs += 1
214 ewv 1.128 common._db.updateRunJob_(listId, listRunField)
215 spiga 1.112 self.SendMLpost(self.sub_jobs[ii])
216    
217 spiga 1.94 else:
218     common.logger.message("The whole task doesn't found compatible site ")
219 ewv 1.92
220 spiga 1.112 return njs
221 spiga 1.99
222     def submissionError(self):
223     ## add some more verbose message in case submission is not complete
224     msg = 'Submission performed using the Requirements: \n'
225     ### TODO_ DS--BL
226     #msg += common.taskDB.dict("jobtype")+' version: '+common.taskDB.dict("codeVersion")+'\n'
227     #msg += '(Hint: please check if '+common.taskDB.dict("jobtype")+' is available at the Sites)\n'
228     if self.cfg_params.has_key('EDG.se_white_list'):
229 spiga 1.136 msg += '\tSE White List: '+self.cfg_params['EDG.se_white_list']+'\n'
230 spiga 1.99 if self.cfg_params.has_key('EDG.se_black_list'):
231 spiga 1.136 msg += '\tSE Black List: '+self.cfg_params['EDG.se_black_list']+'\n'
232 spiga 1.99 if self.cfg_params.has_key('EDG.ce_white_list'):
233 spiga 1.136 msg += '\tCE White List: '+self.cfg_params['EDG.ce_white_list']+'\n'
234 spiga 1.99 if self.cfg_params.has_key('EDG.ce_black_list'):
235 spiga 1.136 msg += '\tCE Black List: '+self.cfg_params['EDG.ce_black_list']+'\n'
236 spiga 1.137 removeDefBL = self.cfg_params.get('EDG.remove_default_blacklist',0)
237     if removeDefBL == '0':
238     msg += '\tNote: All CMS T1s are BlackListed by default \n'
239 spiga 1.136 msg += '\t(Hint: By whitelisting you force the job to run at this particular site(s).\n'
240     msg += '\tPlease check if :\n'
241     msg += '\t\t -- the dataset is available at this site!\n'
242     msg += '\t\t -- the CMSSW version is available at this site!)\n'
243 spiga 1.112 common.logger.message(msg)
244    
245     return
246 spiga 1.99
247 spiga 1.112 def collect_MLInfo(self):
248     """
249 ewv 1.129 Prepare DashBoard information
250 spiga 1.112 """
251 ewv 1.92
252 ewv 1.131 taskId = uniqueTaskName(common._db.queryTask('name'))
253 spiga 1.112 gridName = string.strip(common.scheduler.userName())
254     common.logger.debug(5, "GRIDNAME: "+gridName)
255     taskType = 'analysis'
256 ewv 1.128
257 spiga 1.112 self.datasetPath = self.cfg_params['CMSSW.datasetpath']
258     if string.lower(self.datasetPath)=='none':
259     self.datasetPath = None
260     self.executable = self.cfg_params.get('CMSSW.executable','cmsRun')
261     VO = self.cfg_params.get('EDG.virtual_organization','cms')
262    
263 ewv 1.129 params = {'tool': common.prog_name,
264     'JSToolVersion': common.prog_version_str,
265     'tool_ui': os.environ.get('HOSTNAME',''),
266     'scheduler': common.scheduler.name(),
267     'GridName': gridName,
268     'taskType': taskType,
269     'vo': VO,
270     'user': os.environ.get('USER',''),
271     'taskId': taskId,
272     'datasetFull': self.datasetPath,
273 ewv 1.128 'exe': self.executable }
274 spiga 1.112
275     return params
276 ewv 1.128
277 spiga 1.112 def SendMLpre(self):
278     """
279 ewv 1.128 Send Pre info to ML
280 spiga 1.112 """
281     params = self.collect_MLInfo()
282 ewv 1.128
283 spiga 1.112 params['jobId'] ='TaskMeta'
284 ewv 1.128
285 spiga 1.112 common.apmon.sendToML(params)
286 ewv 1.128
287 spiga 1.112 common.logger.debug(5,'Submission DashBoard Pre-Submission report: '+str(params))
288 ewv 1.128
289 spiga 1.112 return
290 ewv 1.92
291 spiga 1.112 def SendMLpost(self,allList):
292     """
293 ewv 1.128 Send post-submission info to ML
294     """
295     task = common._db.getTask(allList)
296 spiga 1.112
297     params = {}
298     for k,v in self.collect_MLInfo().iteritems():
299     params[k] = v
300 ewv 1.128
301 spiga 1.118
302 spiga 1.112 Sub_Type = 'Direct'
303     for job in task.jobs:
304 ewv 1.128 jj = job['jobId']
305 spiga 1.112 jobId = ''
306     localId = ''
307 ewv 1.128 jid = str(job.runningJob['schedulerId'])
308 ewv 1.130 if common.scheduler.name().upper() in ['CONDOR_G','GLIDEIN']:
309 spiga 1.112 rb = 'OSG'
310 ewv 1.128 taskHash = sha.new(common._db.queryTask('name')).hexdigest()
311 ewv 1.130 jobId = str(jj) + '_https://' + common.scheduler.name() + '/' + taskHash + '/' + str(jj)
312 spiga 1.112 common.logger.debug(5,'JobID for ML monitoring is created for CONDOR_G scheduler:'+jobId)
313 ewv 1.128 elif common.scheduler.name().upper() in ['LSF', 'CAF']:
314 spiga 1.138 jobId= str(jj) + "_https://"+common.scheduler.name()+":/"+jid+"-"+string.replace(str(task['name']),"_","-")
315 spiga 1.112 common.logger.debug(5,'JobID for ML monitoring is created for LSF scheduler:'+jobId)
316     rb = common.scheduler.name()
317     localId = jid
318 ewv 1.139 elif common.scheduler.name().upper() in ['CONDOR']:
319     taskHash = sha.new(common._db.queryTask('name')).hexdigest()
320     jobId = str(jj) + '_https://' + socket.gethostname() + '/' + taskHash + '/' + str(jj)
321     common.logger.debug(5,'JobID for ML monitoring is created for CONDOR scheduler:'+jobId)
322     rb = common.scheduler.name()
323 spiga 1.112 else:
324     jobId = str(jj) + '_' + str(jid)
325     common.logger.debug(5,'JobID for ML monitoring is created for gLite scheduler'+jobId)
326     rb = str(job.runningJob['service'])
327 ewv 1.128
328     dlsDest = job['dlsDestination']
329 spiga 1.125 if len(dlsDest) == 1 :
330     T_SE=str(dlsDest[0])
331     elif len(dlsDest) == 2 :
332     T_SE=str(dlsDest[0])+','+str(dlsDest[1])
333 ewv 1.128 else :
334 spiga 1.112 T_SE=str(len(dlsDest))+'_Selected_SE'
335    
336    
337     infos = { 'jobId': jobId, \
338     'sid': jid, \
339     'broker': rb, \
340     'bossId': jj, \
341     'SubmissionType': Sub_Type, \
342     'TargetSE': T_SE, \
343     'localId' : localId}
344    
345     for k,v in infos.iteritems():
346     params[k] = v
347    
348     common.logger.debug(5,'Submission DashBoard report: '+str(params))
349     common.apmon.sendToML(params)
350 nsmirnov 1.1 return
351 spiga 1.112
352