Beispiel #1
0
 def _packetAcked(self, packet, ownerID, packetID):
     from storage import backup_matrix
     backupID, blockNum, supplierNum, dataORparity = packetid.BidBnSnDp(packetID)
     backup_matrix.RemoteFileReport(
         backupID, blockNum, supplierNum, dataORparity, True)
     if ownerID not in self.statistic:
         self.statistic[ownerID] = [0, 0]
     self.statistic[ownerID][0] += 1
     self.automat('block-acked', (ownerID, packetID))
Beispiel #2
0
 def _packetFailed(self, remoteID, packetID, why):
     from storage import backup_matrix
     backupID, blockNum, supplierNum, dataORparity = packetid.BidBnSnDp(
         packetID)
     backup_matrix.RemoteFileReport(backupID, blockNum, supplierNum,
                                    dataORparity, False)
     if remoteID not in self.statistic:
         self.statistic[remoteID] = [0, 0]
     self.statistic[remoteID][1] += 1
     self.automat('block-failed', (remoteID, packetID))
Beispiel #3
0
 def _packetFailed(self, remoteID, packetID, why):
     from storage import backup_matrix
     backupID, blockNum, supplierNum, dataORparity = packetid.BidBnSnDp(
         packetID)
     backup_matrix.RemoteFileReport(backupID, blockNum, supplierNum,
                                    dataORparity, False)
     if remoteID not in self.statistic:
         self.statistic[remoteID] = {
             'acked': 0,
             'failed': 0,
             'latest': '',
         }
     self.statistic[remoteID]['failed'] += 1
     self.statistic[remoteID]['latest'] += '-'
     self.statistic[remoteID]['latest'] = self.statistic[remoteID][
         'latest'][-STAT_KEEP_LATEST_RESULTS_COUNT:]
     self.automat('block-failed', (remoteID, packetID))
Beispiel #4
0
 def _packetAcked(self, packet, ownerID, packetID):
     from storage import backup_matrix
     backupID, blockNum, supplierNum, dataORparity = packetid.BidBnSnDp(
         packetID)
     backup_matrix.RemoteFileReport(backupID, blockNum, supplierNum,
                                    dataORparity, True)
     if ownerID not in self.statistic:
         self.statistic[ownerID] = {
             'acked': 0,
             'failed': 0,
             'latest': '',
         }
     self.statistic[ownerID]['acked'] += 1
     self.statistic[ownerID]['latest'] += '+'
     self.statistic[ownerID]['latest'] = self.statistic[ownerID]['latest'][
         -STAT_KEEP_LATEST_RESULTS_COUNT:]
     self.automat('block-acked', (ownerID, packetID))
Beispiel #5
0
def ListSummary(dirlist):
    """
    Take directory listing and make summary of format:: BackupID-1-Data 1-1873
    missing for 773,883, BackupID-1-Parity 1-1873 missing for 777,982,
    """
    BackupMax = {}
    BackupAll = {}
    result = ""
    for filename in dirlist:
        if not packetid.Valid(filename):  # if not type we can summarize
            result += filename + "\n"  # then just include filename
        else:
            BackupID, BlockNum, SupNum, DataOrParity = packetid.BidBnSnDp(
                filename)
            LocalID = BackupID + "-" + str(SupNum) + "-" + DataOrParity
            blocknum = int(BlockNum)
            BackupAll[(LocalID, blocknum)] = True
            if LocalID in BackupMax:
                if BackupMax[LocalID] < blocknum:
                    BackupMax[LocalID] = blocknum
            else:
                BackupMax[LocalID] = blocknum
    for BackupName in sorted(BackupMax.keys()):
        missing = []
        thismax = BackupMax[BackupName]
        for blocknum in range(0, thismax):
            if not (BackupName, blocknum) in BackupAll:
                missing.append(str(blocknum))
        result += BackupName + " from 0-" + str(thismax)
        if len(missing) > 0:
            result += ' missing '
            result += ','.join(missing)


#            for m in missing:
#                result += str(m) + ","
        result += "\n"
    return result
Beispiel #6
0
    def doScanAndQueue(self, arg):
        global _ShutdownFlag
        if _Debug:
            lg.out(
                _DebugLevel,
                'data_sender.doScanAndQueue _ShutdownFlag=%r' % _ShutdownFlag)
        if _Debug:
            log = open(os.path.join(settings.LogsDir(), 'data_sender.log'),
                       'w')
            log.write('doScanAndQueue %s\n' % time.asctime())
        if _ShutdownFlag:
            if _Debug:
                log.write('doScanAndQueue _ShutdownFlag is True\n')
            self.automat('scan-done')
            if _Debug:
                log.flush()
                log.close()
            return
        for customer_idurl in contactsdb.known_customers():
            if '' not in contactsdb.suppliers(customer_idurl):
                from storage import backup_matrix
                for backupID in misc.sorted_backup_ids(
                        backup_matrix.local_files().keys(), True):
                    packetsBySupplier = backup_matrix.ScanBlocksToSend(
                        backupID)
                    if _Debug:
                        log.write('%s\n' % packetsBySupplier)
                    for supplierNum in packetsBySupplier.keys():
                        supplier_idurl = contactsdb.supplier(
                            supplierNum, customer_idurl=customer_idurl)
                        if not supplier_idurl:
                            lg.warn('?supplierNum? %s for %s' %
                                    (supplierNum, backupID))
                            continue
                        for packetID in packetsBySupplier[supplierNum]:
                            backupID_, _, supplierNum_, _ = packetid.BidBnSnDp(
                                packetID)
                            if backupID_ != backupID:
                                lg.warn('?backupID? %s for %s' %
                                        (packetID, backupID))
                                continue
                            if supplierNum_ != supplierNum:
                                lg.warn('?supplierNum? %s for %s' %
                                        (packetID, backupID))
                                continue
                            if io_throttle.HasPacketInSendQueue(
                                    supplier_idurl, packetID):
                                if _Debug:
                                    log.write(
                                        '%s already in sending queue for %s\n'
                                        % (packetID, supplier_idurl))
                                continue
                            if not io_throttle.OkToSend(supplier_idurl):
                                if _Debug:
                                    log.write('ok to send %s ? - NO!\n' %
                                              supplier_idurl)
                                continue
                            customerGlobalID, pathID = packetid.SplitPacketID(
                                packetID)
                            # tranByID = gate.transfers_out_by_idurl().get(supplier_idurl, [])
                            # if len(tranByID) > 3:
                            #     log.write('transfers by %s: %d\n' % (supplier_idurl, len(tranByID)))
                            #     continue
                            customerGlobalID, pathID = packetid.SplitPacketID(
                                packetID)
                            filename = os.path.join(
                                settings.getLocalBackupsDir(),
                                customerGlobalID,
                                pathID,
                            )
                            if not os.path.isfile(filename):
                                if _Debug:
                                    log.write('%s is not a file\n' % filename)
                                continue
                            if io_throttle.QueueSendFile(
                                    filename,
                                    packetID,
                                    supplier_idurl,
                                    my_id.getLocalID(),
                                    self._packetAcked,
                                    self._packetFailed,
                            ):
                                if _Debug:
                                    log.write(
                                        'io_throttle.QueueSendFile %s\n' %
                                        packetID)
                            else:
                                if _Debug:
                                    log.write(
                                        'io_throttle.QueueSendFile FAILED %s\n'
                                        % packetID)
                            # lg.out(6, '  %s for %s' % (packetID, backupID))
                            # DEBUG
                            # break

        self.automat('scan-done')
        if _Debug:
            log.flush()
            log.close()
Beispiel #7
0
 def doScanAndQueue(self, *args, **kwargs):
     """
     Action method.
     """
     global _ShutdownFlag
     if _ShutdownFlag:
         if _Debug:
             lg.out(_DebugLevel,
                    'data_sender.doScanAndQueue   _ShutdownFlag is True\n')
         self.automat('scan-done', 0)
         return
     from storage import backup_matrix
     from storage import backup_fs
     backup_matrix.ReadLocalFiles()
     progress = 0
     # if _Debug:
     #     lg.out(_DebugLevel, 'data_sender.doScanAndQueue    with %d known customers' % len(contactsdb.known_customers()))
     for customer_idurl in contactsdb.known_customers():
         if customer_idurl != my_id.getIDURL():
             # TODO: check that later
             if _Debug:
                 lg.out(
                     _DebugLevel + 2,
                     'data_sender.doScanAndQueue  skip sending to another customer: %r'
                     % customer_idurl)
             continue
         known_suppliers = contactsdb.suppliers(customer_idurl)
         if not known_suppliers or id_url.is_some_empty(known_suppliers):
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'data_sender.doScanAndQueue    found empty supplier(s) for customer %r, SKIP'
                     % customer_idurl)
             continue
         known_backups = misc.sorted_backup_ids(
             list(backup_matrix.local_files().keys()), True)
         if _Debug:
             lg.out(
                 _DebugLevel,
                 'data_sender.doScanAndQueue    found %d known suppliers for customer %r with %d backups'
                 %
                 (len(known_suppliers), customer_idurl, len(known_backups)))
         for backupID in known_backups:
             this_customer_idurl = packetid.CustomerIDURL(backupID)
             if this_customer_idurl != customer_idurl:
                 continue
             customerGlobalID, pathID, _ = packetid.SplitBackupID(
                 backupID, normalize_key_alias=True)
             keyAlias = packetid.KeyAlias(customerGlobalID)
             item = backup_fs.GetByID(pathID,
                                      iterID=backup_fs.fsID(
                                          customer_idurl, keyAlias))
             if not item:
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'data_sender.doScanAndQueue    skip sending backup %r path not exist in catalog'
                         % backupID)
                 continue
             if item.key_id and customerGlobalID and customerGlobalID != item.key_id:
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'data_sender.doScanAndQueue    skip sending backup %r key is different in the catalog: %r ~ %r'
                         % (
                             backupID,
                             customerGlobalID,
                             item.key_id,
                         ))
                 continue
             packetsBySupplier = backup_matrix.ScanBlocksToSend(
                 backupID, limit_per_supplier=None)
             total_for_customer = sum(
                 [len(v) for v in packetsBySupplier.values()])
             if total_for_customer:
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'data_sender.doScanAndQueue    sending %r for customer %r with %d pieces'
                         %
                         (item.name(), customer_idurl, total_for_customer))
                 for supplierNum in packetsBySupplier.keys():
                     # supplier_idurl = contactsdb.supplier(supplierNum, customer_idurl=customer_idurl)
                     if supplierNum >= 0 and supplierNum < len(
                             known_suppliers):
                         supplier_idurl = known_suppliers[supplierNum]
                     else:
                         supplier_idurl = None
                     if not supplier_idurl:
                         lg.warn(
                             'skip sending, unknown supplier_idurl supplierNum=%s for %s, customer_idurl=%r'
                             % (supplierNum, backupID, customer_idurl))
                         continue
                     for packetID in packetsBySupplier[supplierNum]:
                         backupID_, _, supplierNum_, _ = packetid.BidBnSnDp(
                             packetID)
                         if backupID_ != backupID:
                             lg.warn(
                                 'skip sending, unexpected backupID supplierNum=%s for %s, customer_idurl=%r'
                                 % (packetID, backupID, customer_idurl))
                             continue
                         if supplierNum_ != supplierNum:
                             lg.warn(
                                 'skip sending, unexpected supplierNum %s for %s, customer_idurl=%r'
                                 % (packetID, backupID, customer_idurl))
                             continue
                         if io_throttle.HasPacketInSendQueue(
                                 supplier_idurl, packetID):
                             if _Debug:
                                 lg.out(
                                     _DebugLevel,
                                     'data_sender.doScanAndQueue    %s already in sending queue for %r'
                                     % (packetID, supplier_idurl))
                             continue
                         latest_progress = self.statistic.get(
                             supplier_idurl, {}).get('latest', '')
                         if len(latest_progress
                                ) >= 3 and latest_progress.endswith('---'):
                             if _Debug:
                                 lg.out(
                                     _DebugLevel + 2,
                                     'data_sender.doScanAndQueue     skip sending to supplier %r because multiple packets already failed'
                                     % supplier_idurl)
                             continue
                         if not io_throttle.OkToSend(supplier_idurl):
                             if _Debug:
                                 lg.out(
                                     _DebugLevel + 2,
                                     'data_sender.doScanAndQueue     skip sending, queue is busy for %r'
                                     % supplier_idurl)
                             continue
                         customerGlobalID, pathID = packetid.SplitPacketID(
                             packetID)
                         filename = os.path.join(
                             settings.getLocalBackupsDir(),
                             customerGlobalID,
                             pathID,
                         )
                         if not os.path.isfile(filename):
                             if _Debug:
                                 lg.out(
                                     _DebugLevel,
                                     'data_sender.doScanAndQueue     %s is not a file'
                                     % filename)
                             continue
                         itemInfo = item.to_json()
                         if io_throttle.QueueSendFile(
                                 filename,
                                 packetID,
                                 supplier_idurl,
                                 my_id.getIDURL(),
                                 lambda packet, ownerID,
                                 packetID: self._packetAcked(
                                     packet, ownerID, packetID, itemInfo),
                                 lambda remoteID, packetID,
                                 why: self._packetFailed(
                                     remoteID, packetID, why, itemInfo),
                         ):
                             progress += 1
                             if _Debug:
                                 lg.out(
                                     _DebugLevel,
                                     'data_sender.doScanAndQueue   for %r put %s in the queue  progress=%d'
                                     % (
                                         item.name(),
                                         packetID,
                                         progress,
                                     ))
                         else:
                             if _Debug:
                                 lg.out(
                                     _DebugLevel,
                                     'data_sender.doScanAndQueue    io_throttle.QueueSendFile FAILED %s'
                                     % packetID)
     if _Debug:
         lg.out(_DebugLevel,
                'data_sender.doScanAndQueue    progress=%s' % progress)
     self.automat('scan-done', progress)
Beispiel #8
0
 def doScanAndQueue(self, *args, **kwargs):
     """
     Action method.
     """
     global _ShutdownFlag
     if _ShutdownFlag:
         if _Debug:
             lg.out(_DebugLevel, 'data_sender.doScanAndQueue   _ShutdownFlag is True\n')
         self.automat('scan-done', 0)
         return
     from storage import backup_matrix
     from storage import backup_fs
     backup_matrix.ReadLocalFiles()
     progress = 0
     if _Debug:
         lg.out(_DebugLevel, 'data_sender.doScanAndQueue    with %d known customers' % len(contactsdb.known_customers()))
     for customer_idurl in contactsdb.known_customers():
         if customer_idurl != my_id.getLocalID():
             # TODO: check that later
             if _Debug:
                 lg.out(_DebugLevel + 6, 'data_sender.doScanAndQueue  skip sending to another customer: %r' % customer_idurl)
             continue
         known_suppliers = contactsdb.suppliers(customer_idurl)
         if not known_suppliers or id_url.is_some_empty(known_suppliers):
             if _Debug:
                 lg.out(_DebugLevel, 'data_sender.doScanAndQueue    found empty supplier(s) for customer %r, SKIP' % customer_idurl)
             continue
         known_backups = misc.sorted_backup_ids(list(backup_matrix.local_files().keys()), True)
         if _Debug:
             lg.out(_DebugLevel, 'data_sender.doScanAndQueue    found %d known suppliers for customer %r with %d backups' % (
                 len(known_suppliers), customer_idurl, len(known_backups)))
         for backupID in known_backups:
             this_customer_idurl = packetid.CustomerIDURL(backupID)
             if this_customer_idurl != customer_idurl:
                 continue
             customerGlobalID, pathID, _ = packetid.SplitBackupID(backupID, normalize_key_alias=True)
             item = backup_fs.GetByID(pathID, iterID=backup_fs.fsID(customer_idurl=customer_idurl))
             if not item:
                 if _Debug:
                     lg.out(_DebugLevel, 'data_sender.doScanAndQueue    skip sending backup %r path not exist in catalog' % backupID)
                 continue
             if item.key_id and customerGlobalID and customerGlobalID != item.key_id:
                 if _Debug:
                     lg.out(_DebugLevel, 'data_sender.doScanAndQueue    skip sending backup %r key is different in the catalog' % backupID)
                 continue
             packetsBySupplier = backup_matrix.ScanBlocksToSend(backupID, limit_per_supplier=None)
             total_for_customer = sum([len(v) for v in packetsBySupplier.values()])
             if _Debug:
                 lg.out(_DebugLevel, 'data_sender.doScanAndQueue    to be delivered for customer %r : %d' % (customer_idurl, total_for_customer))
             for supplierNum in packetsBySupplier.keys():
                 # supplier_idurl = contactsdb.supplier(supplierNum, customer_idurl=customer_idurl)
                 if supplierNum >= 0 and supplierNum < len(known_suppliers):
                     supplier_idurl = known_suppliers[supplierNum]
                 else:
                     supplier_idurl = None
                 if not supplier_idurl:
                     lg.warn('skip sending, unknown supplier_idurl supplierNum=%s for %s, customer_idurl=%r' % (
                         supplierNum, backupID, customer_idurl))
                     continue
                 for packetID in packetsBySupplier[supplierNum]:
                     backupID_, _, supplierNum_, _ = packetid.BidBnSnDp(packetID)
                     if backupID_ != backupID:
                         lg.warn('skip sending, unexpected backupID supplierNum=%s for %s, customer_idurl=%r' % (
                             packetID, backupID, customer_idurl))
                         continue
                     if supplierNum_ != supplierNum:
                         lg.warn('skip sending, unexpected supplierNum %s for %s, customer_idurl=%r' % (
                             packetID, backupID, customer_idurl))
                         continue
                     if io_throttle.HasPacketInSendQueue(supplier_idurl, packetID):
                         if _Debug:
                             lg.out(_DebugLevel, 'data_sender.doScanAndQueue    %s already in sending queue for %r' % (packetID, supplier_idurl))
                         continue
                     if not io_throttle.OkToSend(supplier_idurl):
                         if _Debug:
                             lg.out(_DebugLevel + 6, 'data_sender.doScanAndQueue     skip sending, queue is busy for %r\n' % supplier_idurl)
                         continue
                     # customerGlobalID, pathID = packetid.SplitPacketID(packetID)
                     # tranByID = gate.transfers_out_by_idurl().get(supplier_idurl, [])
                     # if len(tranByID) > 3:
                     #     log.write(u'transfers by %s: %d\n' % (supplier_idurl, len(tranByID)))
                     #     continue
                     customerGlobalID, pathID = packetid.SplitPacketID(packetID)
                     filename = os.path.join(
                         settings.getLocalBackupsDir(),
                         customerGlobalID,
                         pathID,
                     )
                     if not os.path.isfile(filename):
                         if _Debug:
                             lg.out(_DebugLevel, 'data_sender.doScanAndQueue     %s is not a file\n' % filename)
                         continue
                     if io_throttle.QueueSendFile(
                         filename,
                         packetID,
                         supplier_idurl,
                         my_id.getIDURL(),
                         self._packetAcked,
                         self._packetFailed,
                     ):
                         progress += 1
                         if _Debug:
                             lg.out(_DebugLevel, 'data_sender.doScanAndQueue   put %s in the queue  progress=%d' % (packetID, progress, ))
                     else:
                         if _Debug:
                             lg.out(_DebugLevel, 'data_sender.doScanAndQueue    io_throttle.QueueSendFile FAILED %s' % packetID)
     if _Debug:
         lg.out(_DebugLevel, 'data_sender.doScanAndQueue progress=%s' % progress)
     self.automat('scan-done', progress)
Beispiel #9
0
 def doScanAndQueue(self, *args, **kwargs):
     """
     Action method.
     """
     global _ShutdownFlag
     if _Debug:
         lg.out(
             _DebugLevel,
             'data_sender.doScanAndQueue _ShutdownFlag=%r' % _ShutdownFlag)
     if _ShutdownFlag:
         if _Debug:
             lg.out(_DebugLevel, '        _ShutdownFlag is True\n')
         self.automat('scan-done')
         return
     from storage import backup_matrix
     backup_matrix.ReadLocalFiles()
     progress = 0
     for customer_idurl in contactsdb.known_customers():
         if customer_idurl != my_id.getLocalIDURL():
             # TODO: check that later
             if _Debug:
                 lg.out(
                     _DebugLevel + 6,
                     '    skip sending to another customer: %r' %
                     customer_idurl)
             continue
         known_suppliers = contactsdb.suppliers(customer_idurl)
         if b'' in known_suppliers or '' in known_suppliers:
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     '        found empty supplier for customer %r, SKIP' %
                     customer_idurl)
             continue
         known_backups = misc.sorted_backup_ids(
             list(backup_matrix.local_files().keys()), True)
         if _Debug:
             lg.out(
                 _DebugLevel,
                 '        found %d known suppliers for customer %r with %d backups'
                 %
                 (len(known_suppliers), customer_idurl, len(known_backups)))
         for backupID in known_backups:
             this_customer_idurl = packetid.CustomerIDURL(backupID)
             if this_customer_idurl != customer_idurl:
                 continue
             packetsBySupplier = backup_matrix.ScanBlocksToSend(backupID)
             if _Debug:
                 lg.out(
                     _DebugLevel, '        packets for customer %r : %s' %
                     (customer_idurl, packetsBySupplier))
             for supplierNum in packetsBySupplier.keys():
                 # supplier_idurl = contactsdb.supplier(supplierNum, customer_idurl=customer_idurl)
                 try:
                     supplier_idurl = known_suppliers[supplierNum]
                 except:
                     lg.exc()
                     continue
                 if not supplier_idurl:
                     lg.warn(
                         'unknown supplier_idurl supplierNum=%s for %s, customer_idurl=%r'
                         % (supplierNum, backupID, customer_idurl))
                     continue
                 for packetID in packetsBySupplier[supplierNum]:
                     backupID_, _, supplierNum_, _ = packetid.BidBnSnDp(
                         packetID)
                     if backupID_ != backupID:
                         lg.warn(
                             'unexpected backupID supplierNum=%s for %s, customer_idurl=%r'
                             % (packetID, backupID, customer_idurl))
                         continue
                     if supplierNum_ != supplierNum:
                         lg.warn(
                             'unexpected supplierNum %s for %s, customer_idurl=%r'
                             % (packetID, backupID, customer_idurl))
                         continue
                     if io_throttle.HasPacketInSendQueue(
                             supplier_idurl, packetID):
                         if _Debug:
                             lg.out(
                                 _DebugLevel,
                                 '        %s already in sending queue for %r'
                                 % (packetID, supplier_idurl))
                         continue
                     if not io_throttle.OkToSend(supplier_idurl):
                         if _Debug:
                             lg.out(
                                 _DebugLevel + 6,
                                 '        skip, not ok to send %s\n' %
                                 supplier_idurl)
                         continue
                     customerGlobalID, pathID = packetid.SplitPacketID(
                         packetID)
                     # tranByID = gate.transfers_out_by_idurl().get(supplier_idurl, [])
                     # if len(tranByID) > 3:
                     #     log.write(u'transfers by %s: %d\n' % (supplier_idurl, len(tranByID)))
                     #     continue
                     customerGlobalID, pathID = packetid.SplitPacketID(
                         packetID)
                     filename = os.path.join(
                         settings.getLocalBackupsDir(),
                         customerGlobalID,
                         pathID,
                     )
                     if not os.path.isfile(filename):
                         if _Debug:
                             lg.out(_DebugLevel,
                                    '        %s is not a file\n' % filename)
                         continue
                     if io_throttle.QueueSendFile(
                             filename,
                             packetID,
                             supplier_idurl,
                             my_id.getLocalID(),
                             self._packetAcked,
                             self._packetFailed,
                     ):
                         progress += 1
                         if _Debug:
                             lg.out(
                                 _DebugLevel,
                                 '        io_throttle.QueueSendFile %s' %
                                 packetID)
                     else:
                         if _Debug:
                             lg.out(
                                 _DebugLevel,
                                 '        io_throttle.QueueSendFile FAILED %s'
                                 % packetID)
     if _Debug:
         lg.out(_DebugLevel,
                'data_sender.doScanAndQueue progress=%s' % progress)
     self.automat('scan-done')