If we limit the number of backups we make, then clean out old ones
        older than block_id - backup_max_age (given in the constructor)
        This method does nothing otherwise.

        Return None on success
        Raise exception on error
5706
def clear_old_backups(self, block_id):
        
        assert self.setup, "Not set up yet.  Call .db_setup() first!"

        if self.backup_max_age is None:
            # never delete backups
            return 

        # find old backups 
        backup_dir = config.get_backups_directory(self.impl, self.working_dir)
        if not os.path.exists(backup_dir):
            return 

        backups = os.listdir( backup_dir )
        for backup_name in backups:
            if backup_name in [".", ".."]:
                continue 

            backup_path = os.path.join(backup_dir, backup_name)
            backup_block = None 

            try:
                backup_block = int(backup_path.split(".")[-1])
            except:
                # not a backup file
                log.info("Skipping non-backup '%s'" % backup_path)

            if not backup_path.endswith( ".bak.%s" % backup_block ):
                # not a backup file 
                log.info("Skipping non-backup '%s'" % backup_path)
                continue
        
            if backup_block + self.backup_max_age < block_id:
                # dead 
                log.info("Removing old backup '%s'" % backup_path)
                try:
                    os.unlink(backup_path)
                except:
                    pass
257883
def max_age(self):
        
        value = defines.OptionRegistry.MAX_AGE.default
        for option in self.options:
            if option.number == defines.OptionRegistry.MAX_AGE.number:
                value = int(option.value)
        return value
773900
function () {
            var cache = this._cache;
            var expirationTime = ((new Date()).getTime() - this._expiresAfter * 1000);

            for (var url in cache) {
                if (cache.hasOwnProperty(url) && cache[url].age < expirationTime) {
                    delete cache[url];
                }
            }
        }
671318
synchronized void checkLeases() {
    int numPathsChecked = 0;
    for(; sortedLeases.size() > 0; ) {
      final Lease oldest = sortedLeases.first();
      if (!oldest.expiredHardLimit()) {
        return;
      }
      
      // internalReleaseLease() removes paths corresponding to empty files,
      // i.e. it needs to modify the collection being iterated over
      // causing ConcurrentModificationException
      String[] leasePaths = new String[oldest.getPaths().size()];
      oldest.getPaths().toArray(leasePaths);
      LOG.info("Lease " + oldest
          + " has expired hard limit. Recovering lease for paths: "
          + Arrays.toString(leasePaths));
      for(String p : leasePaths) {
        if (++numPathsChecked > this.maxPathsPerCheck) {
          return;
        }
        try {
          fsnamesystem.getFSNamesystemMetrics().numLeaseRecoveries.inc();
          fsnamesystem.internalReleaseLeaseOne(
              oldest, p, this.discardLastBlockIfNoSync);
        } catch (IOException e) {
          LOG.error("Cannot release the path "+p+" in the lease "+oldest, e);
          removeLease(oldest, p);
          fsnamesystem.getFSNamesystemMetrics().numLeaseManagerMonitorExceptions.inc();
        }
      }
    }
  }
1191973
static void deleteOldBackups(File root, String[] backups, int daysToKeep,
      int copiesToKeep) {
    Date now = new Date(System.currentTimeMillis());
    
    // leave the copiesToKeep-1 at least (+1 will be the current backup)
    int maxIndex = Math.max(0, backups.length - copiesToKeep + 1);
    
    for (int i = 0; i < maxIndex; i++) {
      String backup = backups[i];
      Date backupDate = null;
      try {
        backupDate = dateForm.get().parse(backup.substring(backup
            .indexOf(File.pathSeparator) + 1));
      } catch (ParseException pex) {
        // This should not happen because of the 
        // way we construct the list
      }
      long backupAge = now.getTime() - backupDate.getTime();
      
      // if daysToKeep is set delete everything older providing that
      // we retain at least copiesToKeep copies
      boolean deleteOldBackup = (daysToKeep > 0
          && backupAge > daysToKeep * 24 * 60 * 60 * 1000);

      // if daysToKeep is set to zero retain most recent copies
      boolean deleteExtraBackup = (daysToKeep == 0);
      
      if (deleteOldBackup || deleteExtraBackup) {
        // This backup is older than daysToKeep, delete it
        try {
          FLOG.info("Deleting backup " + new File(root, backup));
          FileUtil.fullyDelete(new File(root, backup));
          FLOG.info("Deleted backup " + new File(root, backup));
        } catch (IOException iex) {
          FLOG.error("Error deleting backup " + new File(root, backup), iex);
        }
      } else {
        // done with deleting old backups
        break;
      }
    }
  }
396281
def getMaxAge(self):
        ""
        max_age = 0
        cache_control = self.headers.get('cache-control')
        if cache_control:
            params = dict(urlparse.parse_qsl(cache_control))
            max_age = int(params.get('max-age', '0'))
        return max_age
144442
def replay_block(self, block_to_replay, limit=5):
        

        if block_to_replay == 'latest':
            if self.verbose:
                print("Getting latest %s block header" % source.upper())
            block = get_block(self.source, latest=True, verbose=self.verbose)
            if self.verbose:
                print("Latest %s block is #%s" % (self.source.upper(), block['block_number']))
        else:
            blocknum = block_to_replay if type(block_to_replay) == int else block_to_replay['block_number']
            if blocknum < self.parent_fork_block or blocknum < self.child_fork_block:
                raise Exception("Can't replay blocks mined before the fork")

        if type(block_to_replay) is not dict:
            if self.verbose:
                print("Getting %s block header #%s" % (self.source.upper(), block_to_replay))
            block = get_block(self.source, block_number=int(block_to_replay), verbose=self.verbose)
        else:
            block = block_to_replay

        if self.verbose:
            print("Using %s for pushing to %s" % (self.pusher.name, self.destination.upper()))
            print("Using %s for getting %s transactions" % (self.tx_fetcher.name, self.source.upper()))
            print("Finished getting block header,", len(block['txids']), "transactions in block, will replay", (limit or "all of them"))

        results = []
        enforced_limit = (limit or len(block['txids']))

        for i, txid in enumerate(block['txids'][:enforced_limit]):
            print("outside", txid)
            self._replay_tx(txid, i)
1065499
def check
      last_max = -1
      unless (min_id = @page_records.first.min_id) == 0
        raise RuntimeError, "min_id of first record (#{min_id}) " +
          "must be 0."
      end

      @page_records.each do |pr|
        unless pr.min_id == last_max + 1
          raise RuntimeError, "max_id of previous record (#{last_max}) " +
            "must be exactly 1 smaller than current record (#{pr.min_id})."
        end
        last_max = pr.max_id
        pr.check
      end

      unless last_max == 2 ** 64
        raise RuntimeError, "max_id of last records " +
          "(#{@page_records.last.max_id}) must be #{2 ** 64})."
      end
    end
1374621
def Expire(self):
    """"""
    while len(self._age) > self._limit:
      node = self._age.PopLeft()
      self._hash.pop(node.key, None)
      self.KillObject(node.data)
488831
def delete_from(self, basic_block):
        
        if basic_block is None:
            return

        if self.lock:
            return

        self.lock = True

        if self.prev is basic_block:
            if self.prev.next is self:
                self.prev.next = None
            self.prev = None

        for i in range(len(self.comes_from)):
            if self.comes_from[i] is basic_block:
                self.comes_from.pop(i)
                break

        self.lock = False
136403
def no_history_check(func):
    

    def no_history_check_decorator(self, *args, **kwargs):
        if ConnectionBasic.max_history is 0:
            raise IndexError("ConnectionBasic.max_history is set to 0, "
                             "therefore this functionality is disabled")
        return func(self, *args, **kwargs)

    return no_history_check_decorator
444947
public void checkDataDir() throws DiskErrorException {
    long total_blocks=0, removed_blocks=0;
    List<FSVolume> failed_vols = null;

    failed_vols = volumes.checkDirs();

    //if there no failed volumes return
    if(failed_vols == null) 
      return;
    
    // else 
    // remove related blocks
    long mlsec = System.currentTimeMillis();
    lock.writeLock().lock();
    try {
      volumeMap.removeUnhealthyVolumes(failed_vols);
    } finally {
      lock.writeLock().unlock();
    }
    mlsec = System.currentTimeMillis() - mlsec;
    DataNode.LOG.warn(">>>>>>>>>>>>Removed " + removed_blocks + " out of " + total_blocks +
        "(took " + mlsec + " millisecs)");

    // report the error
    StringBuilder sb = new StringBuilder();
    for(FSVolume fv : failed_vols) {
      sb.append(fv.toString() + ";");
    }

    throw  new DiskErrorException("DataNode failed volumes:" + sb);
  
  }
794204
def clear_from(self, timestamp):
        """"""
        block_size = self.config.block_size
        offset, remainder = timestamp // block_size, timestamp % block_size
        if remainder:
            raise ValueError('Timestamp must be on a block boundary')
        self.driver.clear_from(offset, timestamp)
1246835
private void cleanUpHistory() {
    long oldestAllowedTimestamp = System.currentTimeMillis() - historyLength;
    int trashedNotifications = 0;
    
    if (LOG.isDebugEnabled()) {
      LOG.debug("History cleanup: Checking old notifications to remove from history list ...");
    }
    
    HistoryTreeEntry key = new HistoryTreeEntry(oldestAllowedTimestamp, 0, (byte)0);
    int notificationsCount = 0;
    historyLock.writeLock().lock();
    try {
      notificationsCount = orderedHistoryList.size();
      LOG.warn("History cleanup: size of the history before cleanup: " + notificationsCount);

      if (!historyLimitDisabled && notificationsCount > historyLimit) {
        LOG.warn("History cleanup: Reached physical limit. Number of stored notifications: " + 
            notificationsCount + ". Clearing ...");
      }
      
      int index = Collections.binarySearch(orderedHistoryList, key, comparatorByTS);
      int toDeleteByTS = index >= 0 ? index : - (index + 1);
      int toDeleteByLimit = historyLimitDisabled ? 0 : notificationsCount - (int)historyLimit;
      toDeleteByLimit = toDeleteByLimit > 0 ? toDeleteByLimit : 0;
      
      int toDelete = Math.max(toDeleteByTS, toDeleteByLimit);
      
      // Delete items which are too old
      if (toDelete > 0) {
        LOG.warn("History cleanup: number of the history to cleanup: " + toDelete);
        for (int i = 0; i < toDelete; i++) {
          orderedHistoryList.get(i).removeFromTree();
        }
        
        orderedHistoryList.subList(0, toDelete).clear();
        
        if (toDeleteByLimit > toDeleteByTS) {
          // If we delete a notification because we don't have space left
          trashedNotifications ++;
        }
        notificationsCount = orderedHistoryList.size();
        LOG.warn("History cleanup: size of the history after cleanup: " + notificationsCount);
        
        // clean up history tree, remove the node that has no children and
        // no notifications associated with them.
        cleanUpHistoryTree(historyTree);
      }
      
    } finally {
      historyLock.writeLock().unlock();
    }
    
    core.getMetrics().trashedHistoryNotifications.inc(trashedNotifications);
    core.getMetrics().historySize.set(notificationsCount);
    core.getMetrics().historyQueues.set(historyQueuesCount);
  }
1296924
def cleanup_callbackredirect(max_age=300):
    
    max_age = timezone.now() - timedelta(seconds=max_age)
    logger.debug("Deleting all callback redirects created before {0}".format(max_age.strftime("%b %d %Y %H:%M:%S")))
    CallbackRedirect.objects.filter(created__lte=max_age).delete()
865345
def purge_old_user_tasks():
    
    limit = now() - settings.USER_TASKS_MAX_AGE
    # UserTaskArtifacts will also be removed via deletion cascading
    UserTaskStatus.objects.filter(created__lt=limit).delete()
1245306
private void blockIfNecessary() {
        if (diskPermits == null || diskLimit == Long.MAX_VALUE)
            return;
        try {
            diskPermits.acquire();
        } catch (InterruptedException e) {
            // don't want to re-interrupt so it won't cause SDK stream to be
            // closed in case the thread is reused for a different request
            throw new AbortedException(e);
        }
    }