IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 32461


Ignore:
Timestamp:
Sep 29, 2011, 3:23:06 PM (15 years ago)
Author:
rhenders
Message:

added a method to lock the batch table; reworked methid to get a new batch ID - this is a critical section and is called by clients after locking the table; new method for getting a list of consistently failed IDs; a new class field for the max number of failures before we give up on a batch; new method processingNow() for checking if another client is processing this batch now; some name changes for methods that get lists of batch IDs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/jython/ipptopspsdb.py

    r32452 r32461  
    2020        super(IppToPspsDb, self).__init__(logger, doc, "ipptopspsdatabase")
    2121
    22     '''
    23     Creates a new batch
    24     '''
    25     def createNewBatch(self, batchType, stageID, survey, dvoDb, datastoreProduct):
    26 
    27         sql = "INSERT INTO batch ( \
    28                batch_type, \
    29                stage_id, \
    30                survey, \
    31                dvo_db, \
    32                datastore_product \
    33                ) VALUES ( \
    34                '" + batchType + "', \
    35                " + str(stageID) + ", \
    36                '" + survey + "', \
    37                '" + dvoDb + "', \
    38                '" + datastoreProduct + "' \
    39                )"
    40 
    41         self.execute(sql)
    42 
    43         sql = "SELECT MAX(batch_id) FROM batch"
    44 
    45         batchID = -1;
    46 
    47         try:
    48             rs = self.executeQuery(sql)
    49             rs.first()
    50             batchID = rs.getInt(1)
    51         except:
    52             self.logger.exception("Unable to get batch ID")
    53 
    54         self.logger.debug("Created new batch in ippToPsps database with batchID = %d" % batchID)
    55 
    56         return batchID;
     22        self.MAX_FAILS = 3
    5723
    5824    '''
     
    13298    Returns a list of processed batch IDs that have not yet been loaded to the ODM
    13399    '''
    134     def getBatchIDsUnloadedToODM(self, epoch, dvoGpc1Label):
     100    def getUnloadedBatchIDs(self, epoch, dvoGpc1Label, batchType):
    135101
    136102        sql = "SELECT DISTINCT batch_id \
     
    139105               AND dvo_db = '" + dvoGpc1Label + "' \
    140106               AND loaded_to_datastore = 1 \
     107               AND batch_type = '" + batchType + "' \
    141108               AND loaded_to_ODM = 0"
    142109
     
    157124
    158125    '''
    159     Returns a list of processed batch IDs that have not failed to load and are not yet merged, for this epoch and dvo label
    160     '''
    161     def getUnfinishedBatchIDs(self, epoch, dvoGpc1Label):
     126    Returns a list of processed batch IDs that are merge_worthy, but not yet merged, for this epoch and dvo label
     127    '''
     128    def getUnmergedBatchIDs(self, epoch, dvoGpc1Label, batchType):
    162129
    163130        sql = "SELECT DISTINCT batch_id \
     
    165132               WHERE timestamp > '" + epoch + "' \
    166133               AND dvo_db = '" + dvoGpc1Label + "' \
    167                AND loaded_to_datastore = 1 \
    168                AND merged != 1 \
    169                AND loaded_to_ODM != -1"
    170 
    171 
    172         ids = []
    173         try:
    174             rs = self.executeQuery(sql)
    175             while (rs.next()):
    176                 ids.append(rs.getInt(1))
    177         except:
    178             self.logger.exception("Can't query for unfinished batch ids in ipptopsps Db")
    179 
    180         rs.close()
    181 
    182         self.logger.debug("Found %d unfinished items" % len(ids))
     134               AND loaded_to_ODM = 1 \
     135               AND batch_type = '" + batchType + "' \
     136               AND merged != 1"
     137
     138        ids = []
     139        try:
     140            rs = self.executeQuery(sql)
     141            while (rs.next()):
     142                ids.append(rs.getInt(1))
     143        except:
     144            self.logger.exception("Can't query for unmerged batch ids in ipptopsps Db")
     145
     146        rs.close()
     147
     148        self.logger.debug("Found %d un-merged items" % len(ids))
    183149
    184150        return ids
     
    300266    '''
    301267    def getStages(self):
    302         return ['processed', 'loaded_to_datastore', 'loaded_to_ODM', 'merge_worthy', 'merged', 'deleted_datastore', 'deleted_dxlayer', 'deleted_local']
     268        return ['processed', 'loaded_to_datastore', 'loaded_to_ODM', 'merge_worthy', 'deleted_datastore', 'merged', 'deleted_dxlayer', 'deleted_local']
    303269
    304270    '''
     
    328294    def getTimeOfLastBatchPublished(self, batchType, epoch, dvoGpc1Label):
    329295
    330         minutes = None
     296        seconds = None
    331297
    332298        sql = "SELECT TIMESTAMPDIFF(SECOND, timestamp, now()) \
     
    427393
    428394    '''
     395    Is someone processing this item right now? Checks whether this was attempted within the last 2 HOUR
     396    '''
     397    def processingNow(self, batchType, stage_id, epoch, dvoGpc1Label):
     398
     399        sql = "SELECT COUNT(*) \
     400               FROM batch \
     401               WHERE stage_id = " + str(stage_id) + " \
     402               AND batch_type = '" + batchType + "' \
     403               AND dvo_db = '" + dvoGpc1Label + "' \
     404               AND timestamp BETWEEN now() - INTERVAL 2 HOUR AND now()"
     405
     406        try:
     407            rs = self.executeQuery(sql)
     408            rs.first()
     409            if rs.getInt(1) > 0:
     410                self.logger.errorPair(str(stage_id) + " is already being processed", "skipping")
     411                return True
     412            else:
     413                return False
     414        except:
     415            self.logger.exception("Unable to check whether this batch is being processed")
     416
     417    '''
    429418    Have we already processed and published this batch?
    430419    '''
     
    454443    Has this stage_id consistently failed to process?
    455444    '''
    456     def consistentlyFailed(self, batchType, stage_id, epoch, dvoGpc1Label, count):
     445    def consistentlyFailed(self, batchType, stage_id, epoch, dvoGpc1Label):
    457446
    458447        sql = "SELECT COUNT(*) \
     
    467456            rs = self.executeQuery(sql)
    468457            rs.first()
    469             if rs.getInt(1) >= count:
    470                 self.logger.errorPair(str(stage_id) + " has failed %d times" % count, "skipping")
     458            if rs.getInt(1) >= self.MAX_FAILS:
     459                self.logger.errorPair(str(stage_id) + " has failed %d times" % self.MAX_FAILS, "skipping")
    471460                return True
    472461            else:
     
    475464            self.logger.exception("Unable to check whether this batch has consistently failed")
    476465
     466    '''
     467    Returns a list of stage_ids that have failed the max number of times
     468    '''
     469    def getConsistentlyFailedIDs(self, batchType, epoch, dvoGpc1Label):
     470
     471        sql = "SELECT DISTINCT stage_id, COUNT(stage_id) AS numoccur \
     472               FROM batch \
     473               WHERE batch_type = '" + batchType + "' \
     474               AND timestamp > '" + epoch + "' \
     475               AND dvo_db = '" + dvoGpc1Label + "' \
     476               AND processed = -1 \
     477               GROUP BY stage_id HAVING numoccur >= " + str(self.MAX_FAILS)
     478
     479        ids = []
     480        try:
     481            rs = self.executeQuery(sql)
     482            while (rs.next()):
     483                ids.append(rs.getInt(1))
     484        except:
     485            self.logger.exception("Can't query for consistantly failed ids in ipptopsps Db")
     486
     487        rs.close()
     488
     489        self.logger.debug("Found %d consistantly failed stage_ids for batch type='%s'" % (len(ids), batchType))
     490
     491        return ids
     492
     493    '''
     494    Locks the batch table. This will wait if the lock is held by someone else
     495    '''
     496    def lockBatchTable(self):
     497        self.lockTable("batch")
     498
     499    '''
     500    Creates a new batch. This is done with a locked table in order that no two clients can start work on the same item
     501    '''
     502    def createNewBatch(self, batchType, stageID, survey, epoch, dvoDb, datastoreProduct, force):
     503
     504        batchID = -1;
     505
     506        if force or \
     507            (not self.alreadyProcessed(batchType, stageID, epoch, dvoDb) \
     508            and not self.processingNow(batchType, stageID, epoch, dvoDb) \
     509            and not self.consistentlyFailed(batchType, stageID, epoch, dvoDb)):
     510
     511            sql = "INSERT INTO batch ( \
     512                   batch_type, \
     513                   stage_id, \
     514                   survey, \
     515                   dvo_db, \
     516                   datastore_product \
     517                   ) VALUES ( \
     518                       '" + batchType + "', \
     519                       " + str(stageID) + ", \
     520                       '" + survey + "', \
     521                       '" + dvoDb + "', \
     522                       '" + datastoreProduct + "' \
     523                       )"
     524
     525            self.execute(sql)
     526
     527            sql = "SELECT MAX(batch_id) FROM batch"
     528
     529            try:
     530                rs = self.executeQuery(sql)
     531                rs.first()
     532                batchID = rs.getInt(1)
     533                self.logger.debug("Created new batch in ippToPsps database with batchID = %d" % batchID)
     534            except:
     535                self.logger.exception("Unable to get batch ID")
     536                batchID = -1
     537
     538        return batchID;
    477539     
    478540
Note: See TracChangeset for help on using the changeset viewer.