My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
             
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#! /usr/bin/env python

import psycopg2

import time
import datetime
import os
import os.path
import dircache
import shutil
import signal
import threading
import collections

import Queue

import logging

logger = logging.getLogger("monitor")

import socorro.lib.util
import socorro.lib.filesystem
import socorro.lib.psycopghelper as psy
import socorro.lib.JsonDumpStorage as jds
import socorro.lib.threadlib as thr
import socorro.lib.ooid as ooid

#=================================================================================================================
class UuidNotFoundException(Exception):
pass

#=================================================================================================================
class Monitor (object):
#-----------------------------------------------------------------------------------------------------------------
def __init__(self, config):
super(Monitor, self).__init__()

assert "databaseHost" in config, "databaseHost is missing from the configuration"
assert "databaseName" in config, "databaseName is missing from the configuration"
assert "databaseUserName" in config, "databaseUserName is missing from the configuration"
assert "databasePassword" in config, "databasePassword is missing from the configuration"
assert "storageRoot" in config, "storageRoot is missing from the configuration"
assert "deferredStorageRoot" in config, "deferredStorageRoot is missing from the configuration"
assert "dumpPermissions" in config, "dumpPermissions is missing from the configuration"
assert "dirPermissions" in config, "dirPermissions is missing from the configuration"
assert "dumpGID" in config, "dumpGID is missing from the configuration"
assert "jsonFileSuffix" in config, "jsonFileSuffix is missing from the configuration"
assert "dumpFileSuffix" in config, "dumpFileSuffix is missing from the configuration"
assert "processorCheckInTime" in config, "processorCheckInTime is missing from the configuration"
assert "standardLoopDelay" in config, "standardLoopDelay is missing from the configuration"
assert "cleanupJobsLoopDelay" in config, "cleanupJobsLoopDelay is missing from the configuration"
assert "priorityLoopDelay" in config, "priorityLoopDelay is missing from the configuration"
assert "saveSuccessfulMinidumpsTo" in config, "saveSuccessfulMinidumpsTo is missing from the configuration"
assert "saveFailedMinidumpsTo" in config, "saveFailedMinidumpsTo is missing from the configuration"

self.standardLoopDelay = config.standardLoopDelay.seconds
self.cleanupJobsLoopDelay = config.cleanupJobsLoopDelay.seconds
self.priorityLoopDelay = config.priorityLoopDelay.seconds

self.databaseConnectionPool = psy.DatabaseConnectionPool(config.databaseHost, config.databaseName, config.databaseUserName, config.databasePassword, logger)

#self.createLegacyPriorityJobsTable()
#self.legacySearchEventTrigger = threading.Event()
#self.legacySearchEventTrigger.clear()

self.config = config
signal.signal(signal.SIGTERM, Monitor.respondToSIGTERM)
signal.signal(signal.SIGHUP, Monitor.respondToSIGTERM)

self.standardJobStorage = jds.JsonDumpStorage(root=self.config.storageRoot,
jsonSuffix=self.config.jsonFileSuffix,
dumpSuffix=self.config.dumpFileSuffix,
logger=logger)
self.deferredJobStorage = jds.JsonDumpStorage(root=self.config.deferredStorageRoot,
jsonSuffix=self.config.jsonFileSuffix,
dumpSuffix=self.config.dumpFileSuffix,
logger=logger)
self.successfulJobStorage = None
if self.config.saveSuccessfulMinidumpsTo:
self.successfulJobStorage = jds.JsonDumpStorage(root=self.config.saveSuccessfulMinidumpsTo,
jsonSuffix=self.config.jsonFileSuffix,
dumpSuffix=self.config.dumpFileSuffix,
dumpPermissions=self.config.dumpPermissions,
dirPermissions=self.config.dirPermissions,
dumpGID=self.config.dumpGID,
logger=logger)
self.failedJobStorage = None
if self.config.saveFailedMinidumpsTo:
self.failedJobStorage = jds.JsonDumpStorage(root=self.config.saveFailedMinidumpsTo,
jsonSuffix=self.config.jsonFileSuffix,
dumpSuffix=self.config.dumpFileSuffix,
dumpPermissions=self.config.dumpPermissions,
dirPermissions=self.config.dirPermissions,
dumpGID=self.config.dumpGID,
logger=logger)
self.quit = False

#-----------------------------------------------------------------------------------------------------------------
class NoProcessorsRegisteredException (Exception):
pass

#-----------------------------------------------------------------------------------------------------------------
@staticmethod
def respondToSIGTERM(signalNumber, frame):
""" these classes are instrumented to respond to a KeyboardInterrupt by cleanly shutting down.
This function, when given as a handler to for a SIGTERM event, will make the program respond
to a SIGTERM as neatly as it responds to ^C.
"""
signame = 'SIGTERM'
if signalNumber != signal.SIGTERM: signame = 'SIGHUP'
logger.info("%s - %s detected", threading.currentThread().getName(),signame)
raise KeyboardInterrupt

#-----------------------------------------------------------------------------------------------------------------
def quitCheck(self):
if self.quit:
raise KeyboardInterrupt

#-----------------------------------------------------------------------------------------------------------------
def responsiveSleep (self, seconds):
for x in xrange(int(seconds)):
self.quitCheck()
time.sleep(1.0)

#-----------------------------------------------------------------------------------------------------------------
def getDatabaseConnectionPair (self):
try:
return self.databaseConnectionPool.connectionCursorPair()
except psy.CannotConnectToDatabase:
self.quit = True
self.databaseConnectionPool.cleanup()
socorro.lib.util.reportExceptionAndAbort(logger) # can't continue without a database connection

##-----------------------------------------------------------------------------------------------------------------
#def jsonPathForUuidInJsonDumpStorage(self, uuid):
#try:
#jsonPath = self.standardJobStorage.getJson(uuid)
#except (OSError, IOError):
#try:
#jsonPath = self.deferredJobStorage.getJson(uuid)
#except (OSError, IOError):
#raise UuidNotFoundException("%s cannot be found in standard or deferred storage" % uuid)
#return jsonPath

##-----------------------------------------------------------------------------------------------------------------
#def dumpPathForUuidInJsonDumpStorage(self, uuid):
#try:
#dumpPath = self.standardJobStorage.getDump(uuid)
#except (OSError, IOError):
#try:
#dumpPath = self.deferredJobStorage.getDump(uuid)
#except (OSError, IOError):
#raise UuidNotFoundException("%s cannot be found in standard or deferred storage" % uuid)
#return dumpPath

#-----------------------------------------------------------------------------------------------------------------
def getStorageFor(self, uuid):
try:
self.standardJobStorage.getJson(uuid)
return self.standardJobStorage
except (OSError, IOError):
try:
self.deferredJobStorage.getJson(uuid)
return self.deferredJobStorage
except (OSError, IOError):
raise UuidNotFoundException("%s cannot be found in standard or deferred storage" % uuid)

#-----------------------------------------------------------------------------------------------------------------
def removeUuidFromJsonDumpStorage(self, uuid, **kwargs):
try:
self.standardJobStorage.remove(uuid)
except (jds.NoSuchUuidFound, OSError, IOError):
try:
self.deferredJobStorage.remove(uuid)
except (jds.NoSuchUuidFound, OSError, IOError):
raise UuidNotFoundException("%s cannot be found in standard or deferred storage" % uuid)

#-----------------------------------------------------------------------------------------------------------------
def cleanUpCompletedAndFailedJobs (self):
logger.debug("%s - dealing with completed and failed jobs", threading.currentThread().getName())
# check the jobs table to and deal with the completed and failed jobs
databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
try:
logger.debug("%s - starting loop", threading.currentThread().getName())
saveSuccessfulJobs = bool(self.config.saveSuccessfulMinidumpsTo)
saveFailedJobs = bool(self.config.saveFailedMinidumpsTo)
databaseCursor.execute("select id, uuid, success from jobs where success is not NULL")
logger.debug("%s - sql submitted", threading.currentThread().getName())
for jobId, uuid, success in databaseCursor.fetchall():
self.quitCheck()
logger.debug("%s - checking %s, %s", threading.currentThread().getName(), uuid, success)
try:
currentStorageForThisUuid = self.getStorageFor(uuid)
if success:
if saveSuccessfulJobs:
logger.debug("%s - saving %s", threading.currentThread().getName(), uuid)
self.successfulJobStorage.transferOne(uuid, currentStorageForThisUuid, False, True, aDate=ooid.dateFromOoid(uuid))
#self.successfulJobStorage.transferOne(uuid, currentStorageForThisUuid, False, True, datetime.datetime.now())
else:
if saveFailedJobs:
logger.debug("%s - saving %s", threading.currentThread().getName(), uuid)
self.failedJobStorage.transferOne(uuid, currentStorageForThisUuid, False, True, aDate=ooid.dateFromOoid(uuid))
#self.failedJobStorage.transferOne(uuid, currentStorageForThisUuid, False, True, datetime.datetime.now())
# we no longer need to manually remove it, the transfer already did it
#logger.debug("%s - deleting %s", threading.currentThread().getName(), uuid)
#currentStorageForThisUuid.remove(uuid)
except (jds.NoSuchUuidFound, UuidNotFoundException):
logger.warning("%s - %s wasn't found for cleanup.", threading.currentThread().getName(), uuid)
except OSError, x:
#if str(x) == '[Errno 17] File exists':
socorro.lib.util.reportExceptionAndContinue(logger)
#else:
# raise
databaseCursor.execute("delete from jobs where id = %s", (jobId,))
databaseConnection.commit()
logger.debug("%s - end of this cleanup iteration", threading.currentThread().getName())
except Exception, x:
logger.debug("%s - it died: %s", threading.currentThread().getName(), x)
databaseConnection.rollback()
socorro.lib.util.reportExceptionAndContinue(logger)

#-----------------------------------------------------------------------------------------------------------------
def cleanUpDeadProcessors (self, aCursor):
""" look for dead processors - find all the jobs of dead processors and assign them to live processors
then delete the dead processors
"""
logger.info("%s - looking for dead processors", threading.currentThread().getName())
try:
logger.info("%s - threshold %s", threading.currentThread().getName(), self.config.processorCheckInTime)
threshold = psy.singleValueSql(aCursor, "select now() - interval '%s' * 2" % self.config.processorCheckInTime)
#sql = "select id from processors where lastSeenDateTime < '%s'" % (threshold,)
#logger.info("%s - dead processors sql: %s", threading.currentThread().getName(), sql)
aCursor.execute("select id from processors where lastSeenDateTime < '%s'" % (threshold,))
deadProcessors = aCursor.fetchall()
aCursor.connection.commit()
logger.info("%s - dead processors: %s", threading.currentThread().getName(), str(deadProcessors))
if deadProcessors:
logger.info("%s - found dead processor(s):", threading.currentThread().getName())
for aDeadProcessorTuple in deadProcessors:
logger.info("%s - %d is dead", threading.currentThread().getName(), aDeadProcessorTuple[0])
stringOfDeadProcessorIds = ", ".join([str(x[0]) for x in deadProcessors])
logger.info("%s - getting list of live processor(s):", threading.currentThread().getName())
aCursor.execute("select id from processors where lastSeenDateTime >= '%s'" % threshold)
liveProcessors = aCursor.fetchall()
if not liveProcessors:
raise Monitor.NoProcessorsRegisteredException("There are no processors registered")
numberOfLiveProcessors = len(liveProcessors)
logger.info("%s - getting range of queued date for jobs associated with dead processor(s):", threading.currentThread().getName())
aCursor.execute("select min(queueddatetime), max(queueddatetime) from jobs where owner in (%s)" % stringOfDeadProcessorIds)
earliestDeadJob, latestDeadJob = aCursor.fetchall()[0]
if earliestDeadJob is not None and latestDeadJob is not None:
timeIncrement = (latestDeadJob - earliestDeadJob) / numberOfLiveProcessors
for x, liveProcessorId in enumerate(liveProcessors):
lowQueuedTime = x * timeIncrement + earliestDeadJob
highQueuedTime = (x + 1) * timeIncrement + earliestDeadJob
logger.info("%s - assigning jobs from %s to %s to processor %s:", threading.currentThread().getName(), str(lowQueuedTime), str(highQueuedTime), liveProcessorId)
# why is the range >= at both ends? the range must be inclusive, the risk of moving a job twice is low and consequences low, too.
# 1st step: take any jobs of a dead processor that were in progress and reset them to unprocessed
aCursor.execute("""update jobs
set starteddatetime = NULL
where
%%s >= queueddatetime
and queueddatetime >= %%s
and owner in (%s)
and success is NULL""" % stringOfDeadProcessorIds, (highQueuedTime, lowQueuedTime))
# 2nd step: take all jobs of a dead processor and give them to a new owner
aCursor.execute("""update jobs
set owner = %%s
where
%%s >= queueddatetime
and queueddatetime >= %%s
and owner in (%s)""" % stringOfDeadProcessorIds, (liveProcessorId, highQueuedTime, lowQueuedTime))
aCursor.connection.commit()
#3rd step - transfer stalled priority jobs to new processor
for deadProcessorTuple in deadProcessors:
logger.info("%s - re-assigning priority jobs from processor %d:", threading.currentThread().getName(), deadProcessorTuple[0])
try:
aCursor.execute("""insert into priorityjobs (uuid) select uuid from priority_jobs_%d""" % deadProcessorTuple)
aCursor.connection.commit()
except:
aCursor.connection.rollback()
logger.info("%s - removing all dead processors", threading.currentThread().getName())
aCursor.execute("delete from processors where lastSeenDateTime < '%s'" % threshold)
aCursor.connection.commit()
# remove dead processors' priority tables
for aDeadProcessorTuple in deadProcessors:
try:
aCursor.execute("drop table priority_jobs_%d" % aDeadProcessorTuple[0])
aCursor.connection.commit()
except:
logger.warning("%s - cannot clean up dead processor in database: the table 'priority_jobs_%d' may need manual deletion", threading.currentThread().getName(), aDeadProcessorTuple[0])
aCursor.connection.rollback()
except Monitor.NoProcessorsRegisteredException:
self.quit = True
socorro.lib.util.reportExceptionAndAbort(logger, showTraceback=False)
except:
socorro.lib.util.reportExceptionAndContinue(logger)

#-----------------------------------------------------------------------------------------------------------------
@staticmethod
def compareSecondOfSequence (x, y):
return cmp(x[1], y[1])

#-----------------------------------------------------------------------------------------------------------------
#@staticmethod
#def secondOfSequence(x):
#return x[1]

#-----------------------------------------------------------------------------------------------------------------
def jobSchedulerIter(self, aCursor):
""" This takes a snap shot of the state of the processors as well as the number of jobs assigned to each
then acts as an iterator that returns a sequence of processor ids. Order of ids returned will assure that
jobs are assigned in a balanced manner
"""
logger.debug("%s - balanced jobSchedulerIter: compiling list of active processors", threading.currentThread().getName())
try:
sql = """select
p.id,
count(j.owner)
from
processors p left join jobs j on p.id = j.owner
and p.lastSeenDateTime > now() - interval %s
and j.success is null
group by p.id"""
try:
aCursor.execute(sql, (self.config.processorCheckInTime,) )
logger.debug("%s - sql succeeded", threading.currentThread().getName())
aCursor.connection.commit()
except psycopg2.ProgrammingError:
logger.debug("%s - some other database transaction failed and didn't close properly. Roll it back and try to continue.", threading.currentThread().getName())
try:
aCursor.connection.rollback()
aCursor.execute(sql)
except:
logger.debug("%s - sql failed for the 2nd time - quit", threading.currentThread().getName())
self.quit = True
aCursor.connection.rollback()
socorro.lib.util.reportExceptionAndAbort(logger)
listOfProcessorIds = [[aRow[0], aRow[1]] for aRow in aCursor.fetchall()] #processorId, numberOfAssignedJobs
logger.debug("%s - listOfProcessorIds: %s", threading.currentThread().getName(), str(listOfProcessorIds))
if not listOfProcessorIds:
raise Monitor.NoProcessorsRegisteredException("There are no processors registered")
while True:
logger.debug("%s - sort the list of (processorId, numberOfAssignedJobs) pairs", threading.currentThread().getName())
listOfProcessorIds.sort(Monitor.compareSecondOfSequence)
# the processor with the fewest jobs is about to be assigned a new job, so increment its count
listOfProcessorIds[0][1] += 1
logger.debug("%s - yield the processorId which had the fewest jobs: %d", threading.currentThread().getName(), listOfProcessorIds[0][0])
yield listOfProcessorIds[0][0]
except Monitor.NoProcessorsRegisteredException:
self.quit = True
socorro.lib.util.reportExceptionAndAbort(logger)

#-----------------------------------------------------------------------------------------------------------------
def unbalancedJobSchedulerIter(self, aCursor):
""" This generator returns a sequence of active processorId without regard to job balance
"""
logger.debug("%s - unbalancedJobSchedulerIter: compiling list of active processors", threading.currentThread().getName())
try:
threshold = psy.singleValueSql( aCursor, "select now() - interval '%s'" % self.config.processorCheckInTime)
aCursor.execute("select id from processors where lastSeenDateTime > '%s'" % threshold)
listOfProcessorIds = [aRow[0] for aRow in aCursor.fetchall()]
if not listOfProcessorIds:
raise Monitor.NoProcessorsRegisteredException("There are no active processors registered")
while True:
for aProcessorId in listOfProcessorIds:
yield aProcessorId
except Monitor.NoProcessorsRegisteredException:
self.quit = True
socorro.lib.util.reportExceptionAndAbort(logger)

#-----------------------------------------------------------------------------------------------------------------
def queueJob (self, databaseCursor, uuid, processorIdSequenceGenerator, priority=0):
logger.debug("%s - trying to insert %s", threading.currentThread().getName(), uuid)
processorIdAssignedToThisJob = processorIdSequenceGenerator.next()
try:
databaseCursor.execute("insert into jobs (pathname, uuid, owner, priority, queuedDateTime) values (%s, %s, %s, %s, %s)",
('', uuid, processorIdAssignedToThisJob, priority, datetime.datetime.now()))
logger.debug("%s - executed insert for %s",threading.currentThread().getName(), uuid)
databaseCursor.connection.commit()
except:
databaseCursor.connection.rollback()
raise
logger.debug("%s - %s assigned to processor %d", threading.currentThread().getName(), uuid, processorIdAssignedToThisJob)
return processorIdAssignedToThisJob

#-----------------------------------------------------------------------------------------------------------------
def queuePriorityJob (self, databaseCursor, uuid, processorIdSequenceGenerator):
processorIdAssignedToThisJob = self.queueJob(databaseCursor, uuid, processorIdSequenceGenerator, priority=1)
if processorIdAssignedToThisJob:
databaseCursor.execute("insert into priority_jobs_%d (uuid) values ('%s')" % (processorIdAssignedToThisJob, uuid))
databaseCursor.execute("delete from priorityjobs where uuid = %s", (uuid,))
databaseCursor.connection.commit()
return processorIdAssignedToThisJob

#-----------------------------------------------------------------------------------------------------------------
def standardJobAllocationLoop(self):
"""
"""
try:
try:
while (True):
databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
self.cleanUpDeadProcessors(databaseCursor)
self.quitCheck()
# walk the dump indexes and assign jobs
logger.debug("%s - getting jobSchedulerIter", threading.currentThread().getName())
processorIdSequenceGenerator = self.jobSchedulerIter(databaseCursor)
logger.debug("%s - beginning index scan", threading.currentThread().getName())
try:
logger.debug("%s - starting destructiveDateWalk", threading.currentThread().getName())
for uuid in self.standardJobStorage.destructiveDateWalk():
try:
logger.debug("%s - looping: %s", threading.currentThread().getName(), uuid)
self.quitCheck()
self.queueJob(databaseCursor, uuid, processorIdSequenceGenerator)
except KeyboardInterrupt:
logger.debug("%s - inner detects quit", threading.currentThread().getName())
self.quit = True
raise
except:
socorro.lib.util.reportExceptionAndContinue(logger)
logger.debug("%s - ended destructiveDateWalk", threading.currentThread().getName())
except:
socorro.lib.util.reportExceptionAndContinue(logger)
logger.debug("%s - end of loop - about to sleep", threading.currentThread().getName())
self.quitCheck()
self.responsiveSleep(self.standardLoopDelay)
except (KeyboardInterrupt, SystemExit):
logger.debug("%s - outer detects quit", threading.currentThread().getName())
databaseConnection.rollback()
self.quit = True
raise
finally:
databaseConnection.close()
logger.debug("%s - standardLoop done.", threading.currentThread().getName())

#-----------------------------------------------------------------------------------------------------------------
def getPriorityUuids(self, aCursor):
aCursor.execute("select * from priorityjobs;")
setOfPriorityUuids = set()
for aUuidRow in aCursor.fetchall():
setOfPriorityUuids.add(aUuidRow[0])
return setOfPriorityUuids

#-----------------------------------------------------------------------------------------------------------------
def lookForPriorityJobsAlreadyInQueue(self, databaseCursor, setOfPriorityUuids):
# check for uuids already in the queue
for uuid in list(setOfPriorityUuids):
self.quitCheck()
try:
prexistingJobOwner = psy.singleValueSql(databaseCursor, "select owner from jobs where uuid = '%s'" % uuid)
logger.info("%s - priority job %s was already in the queue, assigned to %d", threading.currentThread().getName(), uuid, prexistingJobOwner)
try:
databaseCursor.execute("insert into priority_jobs_%d (uuid) values ('%s')" % (prexistingJobOwner, uuid))
except psycopg2.ProgrammingError:
logger.debug("%s - %s assigned to dead processor %d - wait for reassignment", threading.currentThread().getName(), uuid, prexistingJobOwner)
# likely that the job is assigned to a dead processor
# skip processing it this time around - by next time hopefully it will have been
# re assigned to a live processor
databaseCursor.connection.rollback()
setOfPriorityUuids.remove(uuid)
continue
databaseCursor.execute("delete from priorityjobs where uuid = %s", (uuid,))
databaseCursor.connection.commit()
setOfPriorityUuids.remove(uuid)
except psy.SQLDidNotReturnSingleValue:
#logger.debug("%s - priority job %s was not already in the queue", threading.currentThread().getName(), uuid)
pass

#-----------------------------------------------------------------------------------------------------------------
def uuidInJsonDumpStorage(self, uuid):
try:
uuidPath = self.standardJobStorage.getJson(uuid)
self.standardJobStorage.markAsSeen(uuid)
except (OSError, IOError):
try:
uuidPath = self.deferredJobStorage.getJson(uuid)
self.deferredJobStorage.markAsSeen(uuid)
except (OSError, IOError):
return False
return True

#-----------------------------------------------------------------------------------------------------------------
def lookForPriorityJobsInJsonDumpStorage(self, databaseCursor, setOfPriorityUuids):
# check for jobs in symlink directories
logger.debug("%s - starting lookForPriorityJobsInJsonDumpStorage", threading.currentThread().getName())
processorIdSequenceGenerator = None
for uuid in list(setOfPriorityUuids):
logger.debug("%s - looking for %s", threading.currentThread().getName(), uuid)
if self.uuidInJsonDumpStorage(uuid):
logger.info("%s - priority queuing %s", threading.currentThread().getName(), uuid)
if not processorIdSequenceGenerator:
logger.debug("%s - about to get unbalancedJobScheduler", threading.currentThread().getName())
processorIdSequenceGenerator = self.unbalancedJobSchedulerIter(databaseCursor)
logger.debug("%s - unbalancedJobScheduler successfully fetched", threading.currentThread().getName())
processorIdAssignedToThisJob = self.queuePriorityJob(databaseCursor, uuid, processorIdSequenceGenerator)
logger.info("%s - %s assigned to %d", threading.currentThread().getName(), uuid, processorIdAssignedToThisJob)
setOfPriorityUuids.remove(uuid)
databaseCursor.execute("delete from priorityjobs where uuid = %s", (uuid,))
databaseCursor.connection.commit()

#-----------------------------------------------------------------------------------------------------------------
def priorityJobsNotFound(self, databaseCursor, setOfPriorityUuids, priorityTableName="priorityjobs"):
# we've failed to find the uuids anywhere
for uuid in setOfPriorityUuids:
self.quitCheck()
logger.error("%s - priority uuid %s was never found", threading.currentThread().getName(), uuid)
databaseCursor.execute("delete from %s where uuid = %s" % (priorityTableName, "%s"), (uuid,))
databaseCursor.connection.commit()

#-----------------------------------------------------------------------------------------------------------------
def priorityJobAllocationLoop(self):
logger.info("%s - priorityJobAllocationLoop starting.", threading.currentThread().getName())
symLinkIndexPath = os.path.join(self.config.storageRoot, "index")
deferredSymLinkIndexPath = os.path.join(self.config.deferredStorageRoot, "index")
try:
try:
while (True):
#self.legacySearchEventTrigger.clear()
databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
try:
self.quitCheck()
setOfPriorityUuids = self.getPriorityUuids(databaseCursor)
if setOfPriorityUuids:
logger.debug("%s - beginning search for priority jobs", threading.currentThread().getName())
self.lookForPriorityJobsAlreadyInQueue(databaseCursor, setOfPriorityUuids)
self.lookForPriorityJobsInJsonDumpStorage(databaseCursor, setOfPriorityUuids)
#self.queuePriorityJobsForSearchInLegacyStorage(databaseCursor, setOfPriorityUuids)
self.priorityJobsNotFound(databaseCursor, setOfPriorityUuids)
except KeyboardInterrupt:
logger.debug("%s - inner detects quit", threading.currentThread().getName())
raise
except:
databaseConnection.rollback()
socorro.lib.util.reportExceptionAndContinue(logger)
self.quitCheck()
#self.legacySearchEventTrigger.clear()
logger.debug("%s - sleeping", threading.currentThread().getName())
self.responsiveSleep(self.priorityLoopDelay)
except (KeyboardInterrupt, SystemExit):
logger.debug("%s - outer detects quit", threading.currentThread().getName())
databaseConnection.rollback()
self.quit = True
finally:
#self.legacySearchEventTrigger.set()
logger.info("%s - priorityLoop done.", threading.currentThread().getName())

#-----------------------------------------------------------------------------------------------------------------
def jobCleanupLoop (self):
logger.info("%s - jobCleanupLoop starting.", threading.currentThread().getName())
try:
try:
logger.info("%s - sleeping first.", threading.currentThread().getName())
self.responsiveSleep(self.cleanupJobsLoopDelay)
while True:
logger.info("%s - beginning jobCleanupLoop cycle.", threading.currentThread().getName())
self.cleanUpCompletedAndFailedJobs()
self.responsiveSleep(self.cleanupJobsLoopDelay)
except (KeyboardInterrupt, SystemExit):
logger.debug("%s - got quit message", threading.currentThread().getName())
self.quit = True
except:
socorro.lib.util.reportExceptionAndContinue(logger)
finally:
logger.info("%s - jobCleanupLoop done.", threading.currentThread().getName())

#-----------------------------------------------------------------------------------------------------------------
# legacy storage section: these routines are temporary for the transition between file system storage techniques
#-----------------------------------------------------------------------------------------------------------------

##-----------------------------------------------------------------------------------------------------------------
#def createLegacyPriorityJobsTable (self):
#logger.debug("%s - createLegacyPriorityJobsTable starting.", threading.currentThread().getName())
#databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
#try:
#databaseCursor.execute("create table legacy_priority_jobs (uuid varchar)")
#databaseConnection.commit()
#logger.debug("%s - legacy_priority_jobs table created", threading.currentThread().getName())
#except:
##socorro.lib.util.reportExceptionAndContinue(logger)
#logger.warning("%s - can't create legacy_priority_jobs table, it probably already exists (this is OK)", threading.currentThread().getName())
#databaseConnection.rollback()

##-----------------------------------------------------------------------------------------------------------------
#def queuePriorityJobsForSearchInLegacyStorage(self, databaseCursor, setOfPriorityUuids):
## check for jobs in symlink directories
#logger.debug("%s - starting queuePriorityJobsForSearchInLegacyStorage", threading.currentThread().getName())
#if setOfPriorityUuids:
#processorIdSequenceGenerator = None
#for uuid in list(setOfPriorityUuids):
#setOfPriorityUuids.remove(uuid)
#databaseCursor.execute("delete from priorityjobs where uuid = %s", (uuid,))
#databaseCursor.execute("insert into legacy_priority_jobs (uuid) values (%s)", (uuid,))
#databaseCursor.connection.commit()
#logger.debug("%s - triggering legacy search event", threading.currentThread().getName())
#self.legacySearchEventTrigger.set()

##-----------------------------------------------------------------------------------------------------------------
#def legacyStoragePriorityJobSearchLoop (self):
#logger.debug("%s - starting legacyStoragePriorityJobSearchLoop", threading.currentThread().getName())
#symLinkIndexPath = os.path.join(self.config.storageRoot, "index")
#deferredSymLinkIndexPath = os.path.join(self.config.deferredStorageRoot, "index")
#try:
#try:
#while (True):
#try:
#logger.debug("%s - waiting for legacySearchEventTrigger", threading.currentThread().getName())
#self.legacySearchEventTrigger.wait()
#logger.debug("%s - received legacySearchEventTrigger", threading.currentThread().getName())
#self.quitCheck()
#databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
#processorIdSequenceGenerator = None
#setOfPriorityUuids = sets.Set([x[0] for x in psy.execute(databaseCursor, "select * from legacy_priority_jobs")])
#if setOfPriorityUuids:
#logger.debug("%s - about get unbalancedJobScheduler", threading.currentThread().getName())
#processorIdSequenceGenerator = self.unbalancedJobSchedulerIter(databaseCursor)
#logger.debug("%s - unbalancedJobScheduler successfully fetched", threading.currentThread().getName())
#self.searchForPriorityJobsInLegacyStorage(setOfPriorityUuids, processorIdSequenceGenerator, symLinkIndexPath, 1)
#self.searchForPriorityJobsInLegacyStorage(setOfPriorityUuids, processorIdSequenceGenerator, deferredSymLinkIndexPath, 2)
#self.priorityJobsNotFound(databaseCursor, setOfPriorityUuids, "legacy_priority_jobs")
#except KeyboardInterrupt:
#self.quit = True
#raise
#except psy.CannotConnectToDatabase:
#socorro.lib.util.reportExceptionAndAbort(logger)
#except:
#socorro.lib.util.reportExceptionAndContinue(logger)
#except (KeyboardInterrupt, SystemExit):
#logger.debug("%s - quit detected", threading.currentThread().getName())
##databaseConnection.rollback()
#self.quit = True
#finally:
##databaseConnection.close()
#logger.info("%s - legacy search loop done.", threading.currentThread().getName())

##-----------------------------------------------------------------------------------------------------------------
#def searchForPriorityJobsInLegacyStorage(self, priorityUuids, processorIdSequenceGenerator, symLinkIndexPath, searchDepth):
## check for jobs in legacy symlink directories
#threadName = threading.currentThread().getName()
#logger.debug("%s - starting searchForPriorityJobsInLegacyStorage in %s", threadName, symLinkIndexPath)
#if not priorityUuids:
#return
#try:
#for path, file, currentDirectory in socorro.lib.filesystem.findFileGenerator(symLinkIndexPath,lambda x: os.path.isdir(x[2]),maxDepth=searchDepth,directorySortFunction=lambda x,y:-cmp(x,y)): # list all directories
#if not priorityUuids:
#break
#for uuid in list(priorityUuids):
#logger.debug("%s - looking for %s", threadName, uuid)
#self.quitCheck()
#absoluteSymLinkPathname = os.path.join(currentDirectory, "%s.symlink" % uuid)
#logger.debug("%s - as %s", threadName, absoluteSymLinkPathname)
#try:
#relativeJsonPathname = os.readlink(absoluteSymLinkPathname)
#absoluteJsonPathname = os.path.normpath(os.path.join(currentDirectory, relativeJsonPathname))
#absoluteDumpPathname = "%s%s" % (absoluteJsonPathname[:-len(self.config.jsonFileSuffix)], self.config.dumpFileSuffix)
#except OSError:
#logger.debug("%s - Not it...", threadName)
#continue
#logger.debug("%s - FOUND", threadName)
#logger.info("%s - priority queuing %s", threadName, absoluteJsonPathname)
#try:
#self.standardJobStorage.copyFrom(uuid, absoluteJsonPathname, absoluteDumpPathname, "legacy", datetime.datetime.now(), False, True)
#databaseConnection, databaseCursor = self.getDatabaseConnectionPair()
#processorIdAssignedToThisJob = self.queuePriorityJob(databaseCursor, uuid, processorIdSequenceGenerator)
#logger.info("%s - %s assigned to %d", threadName, uuid, processorIdAssignedToThisJob)
#except IOError, x:
#logger.warning("%s - unable to process %s because %s", threadName, uuid, x)
#logger.warning("%s - about to remove %s from legacy_priority_jobs", threadName, uuid)
#databaseCursor.execute("delete from legacy_priority_jobs where uuid = %s", (uuid,))
#databaseCursor.connection.commit()
#priorityUuids.remove(uuid)
#except OSError, x:
#logger.warning("%s - searchForPriorityJobsInLegacyStorage had trouble: %s", threadName, x)

## end of legacy storage section
#-----------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------
def start (self):
priorityJobThread = threading.Thread(name="priorityLoopingThread", target=self.priorityJobAllocationLoop)
priorityJobThread.start()
jobCleanupThread = threading.Thread(name="jobCleanupThread", target=self.jobCleanupLoop)
jobCleanupThread.start()
#legacySearchThread = threading.Thread(name="legacySearchThread", target=self.legacyStoragePriorityJobSearchLoop)
#legacySearchThread.start()
try:
try:
self.standardJobAllocationLoop()
finally:
logger.debug("%s - waiting to join.", threading.currentThread().getName())
priorityJobThread.join()
jobCleanupThread.join()
#legacySearchThread.join()
# we're done - kill all the database connections
logger.debug("%s - calling databaseConnectionPool.cleanup().", threading.currentThread().getName())
self.databaseConnectionPool.cleanup()
except KeyboardInterrupt:
logger.debug("%s - KeyboardInterrupt.", threading.currentThread().getName())
raise SystemExit



Show details Hide details

Change log

r1593 by twobraids on Dec 08, 2009   Diff
removed redundant attempt to remove a
json/dump pair from std storage
Go to: 
Project members, sign in to write a code review

Older revisions

r1591 by twobraids on Dec 08, 2009   Diff
still more logging to figure out hang
in production monitor cleanup thread

r1587 by aravind.gottipati on Dec 04, 2009   Diff
Fixing call to transferOne in
cleanUpCompletedAndFailedJobs so the
argument is now a named arg instead of
it defaulting to None
r1586 by twobraids on Dec 04, 2009   Diff
 saving to successful storage was
saving with today's date, rather than
the date of the uuid.  fixed

All revisions of this file

File info

Size: 40506 bytes, 703 lines

File properties

svn:executable
*
Hosted by Google Code