Newer
Older
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
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
import threading
import time
import os
import uuid
import glob
import copy
from dm.common.constants import dmProcessingStatus
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.common.objects.childProcess import ChildProcess
from dm.common.objects.processingJob import ProcessingJob
from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.common.utility.objectUtility import ObjectUtility
from dm.common.utility.valueUtility import ValueUtility
from dm.common.utility.timeUtility import TimeUtility
from dm.common.utility.threadingUtility import ThreadingUtility
from dm.common.utility.timeBasedProcessingQueue import TimeBasedProcessingQueue
from dm.common.utility.singleton import Singleton
from executionThread import ExecutionThread
from processingJobTracker import ProcessingJobTracker
class ExecutionManager(Singleton):
CONFIG_SECTION_NAME = 'ExecutionManager'
NUMBER_OF_EXECUTION_THREADS_KEY = 'numberofexecutionthreads'
DEFAULT_NUMBER_OF_EXECUTION_THREADS = 3
FILE_PATH_LIST_KEY = 'filePathList'
FILE_PATH_PATTERN_KEY = 'filePathPattern'
FILE_QUERY_DICT_KEY = 'fileQueryDict'
PROCESSING_JOB_START_DELAY_IN_SECONDS = 10
MAX_NUMBER_OF_CHILD_PROCESSES = 10
CHILD_PROCESSES_EVENT_TIMEOUT_IN_SECONDS = 10
# Singleton.
__instanceLock = threading.RLock()
__instance = None
def __init__(self):
ExecutionManager.__instanceLock.acquire()
try:
if ExecutionManager.__instance:
return
ExecutionManager.__instance = self
self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
self.logger.debug('Initializing')
self.lock = threading.RLock()
self.executionThreadDict = {}
self.eventFlag = threading.Event()
self.executionQueue = TimeBasedProcessingQueue()
self.__configure()
self.logger.debug('Initialization complete')
finally:
ExecutionManager.__instanceLock.release()
def __configure(self):
cm = ConfigurationManager.getInstance()
configItems = cm.getConfigItems(ExecutionManager.CONFIG_SECTION_NAME)
self.logger.debug('Got config items: %s' % configItems)
self.nExecutionThreads = int(cm.getConfigOption(ExecutionManager.CONFIG_SECTION_NAME, ExecutionManager.NUMBER_OF_EXECUTION_THREADS_KEY, ExecutionManager.DEFAULT_NUMBER_OF_EXECUTION_THREADS))
def resolveFileSpec(self, processingJob):
if processingJob.has_key('filePathList'):
return self.resolveFilePathList(processingJob)
elif processingJob.has_key('filePathPattern'):
return self.resolveFilePathPattern(processingJob)
elif processingJob.has_key('fileQueryDict'):
return self.resolveFileQueryDict(processingJob)
else:
raise InvalidRequest('Invalid input file specification.')
def resolveFilePathList(self, processingJob):
return processingJob.get('filePathList')
def resolveFilePathPattern(self, processingJob):
if not processingJob.has_key('filePathList'):
globPattern = processingJob.get('filePathPattern')
dataDir = processingJob.get('dataDir')
currentDir = os.getcwd()
if dataDir:
os.chdir(dataDir)
processingJob['filePathList'] = glob.glob(globPattern)
os.chdir(currentDir)
return processingJob.get('filePathList', [])
def resolveFileQueryDict(self, processingJob):
return []
def startProcessingJob(self, processingJob):
workflow = processingJob.get('workflow')
self.logger.debug('Submitted processing job for workflow %s' % workflow.get('name'))
jobId = str(uuid.uuid4())
processingJob['id'] = jobId
startTime = time.time()
processingJob['startTime'] = startTime
processingJob['startTimestamp'] = TimeUtility.formatLocalTimestamp(startTime)
processingJob['nFiles'] = 0
processingJob['status'] = dmProcessingStatus.DM_PROCESSING_STATUS_PENDING
ProcessingJobTracker.getInstance().put(jobId, processingJob)
self.logger.debug('Starting processing job %s timer' % jobId)
timer = threading.Timer(self.PROCESSING_JOB_START_DELAY_IN_SECONDS, self.runProcessingJob, args=[processingJob])
timer.start()
return processingJob
def runProcessingJob(self, processingJob):
try:
childProcessEvent = threading.Event()
processingJob.childProcessEvent = childProcessEvent
jobId = processingJob['id']
self.logger.debug('Timer started for processing job %s.' % jobId)
# Resolve files
filePathList = self.resolveFileSpec(processingJob)
if not filePathList:
raise InvalidRequest('There are no input files available for processing.')
self.logger.debug('There are %s files for processing job %s.' % (len(filePathList), jobId))
processingJob['nFiles'] = len(filePathList)
workflowStages = processingJob.get('workflow').get('stages')
workflowStageKeys = workflowStages.keys()
workflowStageKeys.sort()
processingJob['status'] = dmProcessingStatus.DM_PROCESSING_STATUS_RUNNING
childProcessNumber = 0
workingDir = processingJob.get('workingDir')
for stageKey in workflowStageKeys:
if processingJob['status'] != dmProcessingStatus.DM_PROCESSING_STATUS_RUNNING:
self.logger.debug('Processing job %s status is %s, exiting workflow at stage %s' % (jobId, processingJob['status'], stageKey))
break
self.logger.debug('Starting stage %s for processing job %s.' % (stageKey,jobId))
workflowStage = workflowStages.get(stageKey)
workflowStage['childProcesses'] = {}
workflowStage['nRunningChildProcesses'] = 0
workflowStage['nQueuedChildProcesses'] = 0
command = workflowStage.get('command')
processingJob['activeStage'] = stageKey
variableNames = [word for word in command.split() if word.startswith('$')]
self.logger.debug('Variables found for stage %s: ' % variableNames)
iterateOverFiles = False
for v in variableNames:
variableName = v[1:] # remove '$'
variableValue = processingJob.get(variableName)
if variableName == 'filePathList':
variableValue = ' '.join(filePathList)
if variableValue is not None:
command = command.replace(v, variableValue)
if variableName == 'filePath':
iterateOverFiles = True
if iterateOverFiles:
# We iterate over all files
for f in filePathList:
childProcessEvent.clear()
if processingJob['status'] != dmProcessingStatus.DM_PROCESSING_STATUS_RUNNING:
self.logger.debug('Processing job %s status is %s, exiting workflow at stage %s' % (jobId, processingJob['status'], stageKey))
break
# Wait until we can queue child process
while True:
nQueuedChildProcesses = workflowStage.get('nQueuedChildProcesses')
nRunningChildProcesses = workflowStage.get('nRunningChildProcesses')
nChildProcesses = nQueuedChildProcesses + nRunningChildProcesses
if nChildProcesses < self.MAX_NUMBER_OF_CHILD_PROCESSES:
break
else:
self.logger.debug('There are %s child processes for stage %s (processing job %s), cannot create new one' % (nChildProcesses, stageKey, jobId))
childProcessEvent.wait(self.CHILD_PROCESSES_EVENT_TIMEOUT_IN_SECONDS)
childCommand = command.replace('$filePath', f)
self.logger.debug('Execution command for stage %s, child process %s: %s' % (stageKey, childProcessNumber, childCommand))
childProcess = self.createChildProcess(stageKey, childProcessNumber, childCommand, workingDir)
self.pushToExecutionQueue(processingJob, childProcess)
childProcessNumber += 1
else:
childCommand = command
self.logger.debug('Execution command for stage %s, child process %s: %s' % (stageKey, childProcessNumber, childCommand))
childProcess = self.createChildProcess(stageKey, childProcessNumber, childCommand, workingDir)
self.pushToExecutionQueue(processingJob, childProcess)
childProcessNumber += 1
# Child processed queued, wait until they finish
while True:
childProcessEvent.clear()
nQueuedChildProcesses = workflowStage.get('nQueuedChildProcesses')
nRunningChildProcesses = workflowStage.get('nRunningChildProcesses')
nChildProcesses = nQueuedChildProcesses + nRunningChildProcesses
if nChildProcesses == 0:
self.logger.debug('No more queued/running child processes for stage %s (processing job %s)' % (stageKey, jobId))
break
else:
self.logger.debug('There are %s queued/running child processes for stage %s (processing job %s)' % (nChildProcesses, stageKey, jobId))
childProcessEvent.wait(self.CHILD_PROCESSES_EVENT_TIMEOUT_IN_SECONDS)
except Exception, ex:
processingJob['status'] = dmProcessingStatus.DM_PROCESSING_STATUS_FAILED
processingJob['errorMessage'] = str(ex)
self.logger.error('Processing job %s failed: %s' % (jobId,str(ex)))
if processingJob['status'] == dmProcessingStatus.DM_PROCESSING_STATUS_RUNNING:
processingJob['status'] = dmProcessingStatus.DM_PROCESSING_STATUS_DONE
self.logger.debug('Processing job %s is done.' % jobId)
self.logger.debug('Processing job: %s' % processingJob)
def pushToExecutionQueue(self, processingJob, childProcess):
self.executionQueue.push((processingJob, childProcess))
processingJob.childProcessQueued(childProcess)
self.setEvent()
def createChildProcess(self, stageId, childProcessNumber, command, workingDir):
childProcess = ChildProcess({
'stageId' : stageId,
'childProcessNumber' : childProcessNumber,
'command' : command,
'workingDir' : workingDir,
})
return childProcess
@ThreadingUtility.synchronize
def start(self):
self.logger.debug('Starting execution threads')
for i in range(0, self.nExecutionThreads):
tName = 'ExecutionThread-%s' % i
t = ExecutionThread(tName, self, self.executionQueue)
t.start()
self.executionThreadDict[tName] = t
def stop(self):
self.logger.debug('Stopping execution threads')
for (tName, t) in self.executionThreadDict.items():
t.stop()
self.setEvent()
for (tName, t) in self.executionThreadDict.items():
t.join()
@ThreadingUtility.synchronize
def setEvent(self):
self.eventFlag.set()
@ThreadingUtility.synchronize
def clearEvent(self):
self.eventFlag.clear()
def waitOnEvent(self, timeoutInSeconds=None):
self.eventFlag.wait(timeoutInSeconds)
####################################################################
# Testing
if __name__ == '__main__':
LoggingManager.getInstance().configure()
LoggingManager.getInstance().setConsoleLogLevel('DEBUG')
em = ExecutionManager.getInstance()
print em
em.start()
# Stage Keys
# command (required)
# execOutFormat (optional, to be parsed for variables like $jobId )
# workingDir (optional, default .)
# dataDir (optional, default $workingDir)
# monitorCmd (optional, if exists, will execute monitoring in a timer)
# parallelExec (optional, default False)
# filePathList, filePathPattern, or fileQueryDict (one must be present)
# If $filePathList or $filePath is in the argument list, and
# job spec contains filePathPattern or fileQueryDict, files will be
# resolved into filePathList
# If $filePath is in the argument list, filePathList is iterated over
# for a particular stage
# Variables that get filled automatically
# $username : DM username
# $execHost : execution host
# $id : job id
# $startTime : procesing start time
# $endTime : procesing end time
workflow = {u'owner': u'sveseli', u'stages': {
'1' : {u'command': u'/bin/date', 'stopOnFailure' : False},
'2' : {u'command': u'/bin/ls -l $filePath', 'parallelExec' : True},
'3' : {u'command': u'/bin/ls -c1 $filePathList'},
'4' : {u'command': u'/bin/echo Energy is $energyArg current is $CURRENT_ARG'},
}, u'description': u'My first workflow', u'name': u'workflow-01', u'id': u'589396e0e20bf2465e3a6bf0'}
processingJob = ProcessingJob({
'workflow' : workflow,
'experimentName' : 'exp-01',
'energyArg' : '2TeV',
'CURRENT_ARG' : '2mA',
'filePathList' : [ 'testfile01', 'testfile02', 'testfile03' ],
'dataDir' : '/tmp/data',
'workingDir' : '/tmp/data',
})
em.startProcessingJob(processingJob)
time.sleep(180)
em.stop()