forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloseOutWorkflows_leg.py
executable file
·298 lines (272 loc) · 12.5 KB
/
closeOutWorkflows_leg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
import json
import urllib2,urllib, httplib, sys, re, os, phedexSubscription, dbsTest, duplicateEventsGen
from xml.dom.minidom import getDOMImplementation
def TransferComplete(url, dataset, site):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/phedex/datasvc/json/prod/blockreplicas?dataset='+dataset+'&node='+site+'_MSS')
r2=conn.getresponse()
result = json.loads(r2.read())
blocks=result['phedex']
if 'block' not in blocks.keys():
return False
if len(result['phedex']['block'])==0:
return False
for block in blocks['block']:
if block['replica'][0]['complete']!='y':
return False
return True
def TransferPercentage(url, dataset, site):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/phedex/datasvc/json/prod/blockreplicas?dataset='+dataset+'&node='+site)
r2=conn.getresponse()
result = json.loads(r2.read())
blocks=result['phedex']
if 'block' not in blocks.keys():
return 0
if len(result['phedex']['block'])==0:
return 0
total=len(blocks['block'])
completed=0
for block in blocks['block']:
if block['replica'][0]['complete']=='y':
completed=completed+1
return float(completed)/float(total)
#Returns the site for which a custodial move subscription for a dataset was created, if none is found it return 0
def CustodialMoveSubscriptionCreated(datasetName):
url='https://cmsweb.cern.ch/phedex/datasvc/json/prod/subscriptions?dataset=' + datasetName
result = json.loads(urllib2.urlopen(url).read())
datasets=result['phedex']
if 'dataset' not in datasets.keys():
return False
else:
if len(result['phedex']['dataset'])<1:
return False
for subscription in result['phedex']['dataset'][0]['subscription']:
if subscription['custodial']=='y':
return subscription['node']
return False
def getOverviewRequest():
url='vocms204.cern.ch'
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/monitorSvc/requestmonitor')
r2=conn.getresponse()
requests = json.loads(r2.read())
return requests
def getOverviewRequestsWMStats(url):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'),
key_file = os.getenv('X509_USER_PROXY'))
conn.request("GET",
"/couchdb/wmstats/_design/WMStats/_view/requestByStatusAndType?stale=update_after")
response = conn.getresponse()
data = response.read()
conn.close()
myString=data.decode('utf-8')
workflows=json.loads(myString)['rows']
return workflows
#Returns a list with the custodial sites for a request
def findCustodial(url, requestname):
custodialSites=[]
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+requestname)
r2=conn.getresponse()
request = json.loads(r2.read())
while 'exception' in request:
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
siteList=request['Site Whitelist']
for site in siteList:
if 'T1' in site:
custodialSites.append(site)
return "NoSite"
def classifyCompletedRequests(url, requests):
workflows={'ReDigi':[],'MonteCarloFromGEN':[],'MonteCarlo':[] , 'ReReco':[], 'LHEStepZero':[]}
for request in requests:
name=request['id']
if len(request['key'])<3:
print request
continue
status=request['key'][1]
requestType=request['key'][2]
if status=='completed':
if requestType=='MonteCarlo':
datasets=phedexSubscription.outputdatasetsWorkflow(url, name)
m=re.search('.*/GEN$',datasets[0])
if m:
workflows['LHEStepZero'].append(name)
else:
workflows[requestType].append(name)
if requestType=='MonteCarloFromGEN' or requestType=='LHEStepZero'or requestType=='ReDigi' or requestType=='ReReco':
workflows[requestType].append(name)
return workflows
def testOutputDataset(datasetName):
url='https://cmsweb.cern.ch/phedex/datasvc/json/prod/Subscriptions?dataset=' + datasetName
result = json.loads(urllib2.urlopen(url).read())
datasets=result['phedex']['dataset']
if len(datasets)>0:
dicts=datasets[0]
subscriptions=dicts['subscription']
for subscription in subscriptions:
if subscription['level']=='DATASET' and subscription['custodial']=='y':
return True
else:
return False
def testWorkflow(url, workflow):
datasets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
subscribed=1
for dataset in datasets:
if not testOutputDataset(dataset):
subscribed=0
return 0
return 1
def closeOutReRecoWorkflows(url, workflows):
for workflow in workflows:
if 'RelVal' in workflow:
continue
if 'TEST' in workflow:
continue
datasets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
closeOutWorkflow=True
InputDataset=dbsTest.getInputDataSet(url, workflow)
for dataset in datasets:
duplicate=False
closeOutDataset=True
Percentage=PercentageCompletion(url, workflow, dataset)
PhedexSubscription=testOutputDataset(dataset)
closeOutDataset=False
if Percentage==1 and PhedexSubscription and not duplicate:
closeOutDataset=True
else:
closeOutDataset=False
closeOutWorkflow=closeOutWorkflow and closeOutDataset
print '| %80s | %100s | %4s | %5s| %3s | %5s|%5s| ' % (workflow, dataset,str(int(Percentage*100)), str(PhedexSubscription), 100, duplicate, closeOutDataset)
if closeOutWorkflow:
phedexSubscription.closeOutWorkflow(url, workflow)
print '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
def closeOutRedigiWorkflows(url, workflows):
for workflow in workflows:
#print workflow
closeOutWorkflow=True
InputDataset=dbsTest.getInputDataSet(url, workflow)
datasets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
for dataset in datasets:
closeOutDataset=False
Percentage=PercentageCompletion(url, workflow, dataset)
PhedexSubscription=testOutputDataset(dataset)
duplicate=True
if PhedexSubscription!=False and Percentage>=float(0.95):
duplicate=dbsTest.duplicateRunLumi(dataset)
if Percentage>=float(0.95) and PhedexSubscription and not duplicate:
closeOutDataset=True
else:
closeOutDataset=False
closeOutWorkflow=closeOutWorkflow and closeOutDataset
print '| %80s | %100s | %4s | %5s| %3s | %5s|%5s| ' % (workflow, dataset,str(int(Percentage*100)), str(PhedexSubscription), 100, duplicate, closeOutDataset)
if closeOutWorkflow:
phedexSubscription.closeOutWorkflow(url, workflow)
print '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
#the team on which a requet is running
def getRequestTeam(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
while 'exception' in request:
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
if 'teams' not in request:
return 'NoTeam'
teams=request['teams']
if len(teams)<1:
return 'NoTeam'
else:
return teams[0]
def closeOutMonterCarloRequests(url, workflows):
for workflow in workflows:
datasets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
closeOutWorkflow=True
if getRequestTeam(url, workflow)!='analysis':#If request is not in special queue
for dataset in datasets:
ClosePercentage=0.90
#if 'SMS' in dataset:
# ClosePercentage=1
closeOutDataset=True
Percentage=PercentageCompletion(url, workflow, dataset)
PhedexSubscription=CustodialMoveSubscriptionCreated(dataset)
TransPercen=0
closedBlocks=False
if PhedexSubscription!=False:
site=PhedexSubscription
TransPercen=TransferPercentage(url, dataset, site)
duplicate=True
if PhedexSubscription!=False and Percentage>=float(0.9):
duplicate=dbsTest.duplicateLumi(dataset)
closedBlocks = True #dbsTest.hasAllBlocksClosed(dataset)
if Percentage>=float(ClosePercentage) and PhedexSubscription!=False and not duplicate:
closeOutDataset=True
else:
closeOutDataset=False
closeOutWorkflow=closeOutWorkflow and closeOutDataset
print '| %80s | %100s | %4s | %5s| %3s | %5s|%5s| %5s|' % (workflow, dataset,str(int(Percentage*100)), str(PhedexSubscription), str(int(TransPercen*100)), duplicate, closedBlocks, closeOutDataset)
if closeOutWorkflow:
phedexSubscription.closeOutWorkflow(url, workflow)
print'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
def closeOutStep0Requests(url, workflows):
for workflow in workflows:
#print workflow
datasets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
closeOutWorkflow=True
if getRequestTeam(url, workflow)!='analysis':#If request is not in special queue
for dataset in datasets:
closeOutDataset=False
Percentage=PercentageCompletion(url, workflow, dataset)
PhedexSubscription=CustodialMoveSubscriptionCreated(dataset)
if PhedexSubscription!=False:
site=PhedexSubscription
TransPercen=TransferPercentage(url, dataset, site)
duplicate=dbsTest.duplicateLumi(dataset)
correctLumis=dbsTest.checkCorrectLumisEventGEN(dataset)
if Percentage>=float(0.95) and PhedexSubscription!=False and not duplicate and correctLumis:
closeOutDataset=True
else:
closeOutDataset=False
closeOutWorkflow=closeOutWorkflow and closeOutDataset
print '| %80s | %100s | %4s | %5s| %3s | %5s| %5s| ' % (workflow, dataset,str(int(Percentage*100)), str(PhedexSubscription), str(correctLumis), duplicate, closeOutDataset)
if closeOutWorkflow:
phedexSubscription.closeOutWorkflow(url, workflow)
print'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
# It assumes dataset is an output dataset from the workflow
def PercentageCompletion(url, workflow, dataset):
inputEvents=0
inputEvents=inputEvents+int(dbsTest.getInputEvents(url, workflow))
outputEvents=dbsTest.getOutputEvents(url, workflow, dataset)
if inputEvents==0:
return 0
percentage=outputEvents/float(inputEvents)
return percentage
def main():
url='cmsweb.cern.ch'
print "Gathering Requests"
requests=getOverviewRequestsWMStats(url)
print "Classifying Requests"
workflowsCompleted=classifyCompletedRequests(url, requests)
print '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
print '| Request | OutputDataSet |%Compl|Subscr|Tran|Dupl|Blocks|ClosOu|'
print '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
#closeOutReRecoWorkflows(url, workflowsCompleted['ReReco'])
#closeOutRedigiWorkflows(url, workflowsCompleted['ReDigi'])
closeOutMonterCarloRequests(url, workflowsCompleted['MonteCarlo'])
closeOutMonterCarloRequests(url, workflowsCompleted['MonteCarloFromGEN'])
#closeOutStep0Requests(url, workflowsCompleted['LHEStepZero'])
print "MC Workflows for which couldn't find Custodial Tier1 Site"
if 'NoSite' in workflowsCompleted['MonteCarlo']:
print workflowsCompleted['MonteCarlo']['NoSite']
if 'NoSite' in workflowsCompleted['MonteCarloFromGEN']:
print workflowsCompleted['MonteCarloFromGEN']['NoSite']
sys.exit(0);
if __name__ == "__main__":
main()