Пример #1
0
        /// <summary>
        /// Removes results (in result database) for which we didn't see an update for a configured amount of time.
        /// </summary>
        private void RemoveOutdatedResults(IConfigurationSection configuration)
        {
            TimeSpan resultsOutdatedAfter = configuration.GetValue <TimeSpan>("ResultsOutdatedAfter");

            DateTime nowItIs = DateTime.UtcNow;
            double   currentUnixTimeStamp = (nowItIs - Program.UnixTimeStampBase).TotalSeconds;

            using (var transaction = this.resultDatabaseContext.Database.BeginTransaction())
            {
                var outdatedRssis = this.resultDatabaseContext.RssiValues.Where(r => IsOutdatedUnixTimeStampColumn(currentUnixTimeStamp, r, resultsOutdatedAfter)).ToList();
                foreach (var item in outdatedRssis)
                {
                    this.logger.LogInformation($"Maintenance{(this.dryRunMode ? " DRY RUN: Would remove" : ": Removing")} RSSI entry for host {item.ForeignId} which hast last been updated at {item.TimeStampString} (i.e. {TimeSpan.FromSeconds(currentUnixTimeStamp - item.UnixTimeStamp)} ago)");
                }

                var outdatedRssiFailures = this.resultDatabaseContext.RssiFailingQueries.Where(r => IsOutdatedTimeStampColumn(nowItIs, r, resultsOutdatedAfter)).ToList();
                foreach (var item in outdatedRssiFailures)
                {
                    this.logger.LogInformation($"Maintenance{(this.dryRunMode ? " DRY RUN: Would remove" : ": Removing")} RSSI failing query entry for host {item.Subnet} which hast last been updated at {item.TimeStamp} (i.e. {item.TimeStamp - nowItIs} ago)");
                }

                var outdatedBgpPeers = this.resultDatabaseContext.BgpPeers.Where(r => IsOutdatedUnixTimeStampColumn(currentUnixTimeStamp, r, resultsOutdatedAfter)).ToList();
                foreach (var item in outdatedBgpPeers)
                {
                    this.logger.LogInformation($"Maintenance{(this.dryRunMode ? " DRY RUN: Would remove" : ": Removing")} BGP peer entry from host {item.LocalAddress} to {item.RemoteAddress} which hast last been updated at {item.TimeStampString} (i.e. {TimeSpan.FromSeconds(currentUnixTimeStamp - item.UnixTimeStamp)} ago)");
                }

                var outdatedBgpPeerFailures = this.resultDatabaseContext.BgpFailingQueries.Where(r => IsOutdatedTimeStampColumn(nowItIs, r, resultsOutdatedAfter)).ToList();
                foreach (var item in outdatedBgpPeerFailures)
                {
                    this.logger.LogInformation($"Maintenance{(this.dryRunMode ? " DRY RUN: Would remove" : ": Removing")} BGP failing peer entry from host {item.Host} which hast last been updated at {item.TimeStamp} (i.e. {item.TimeStamp - nowItIs} ago)");
                }

                var cacheMaintenance = new CacheMaintenance(this.dryRunMode);
                cacheMaintenance.DeleteForAddress(outdatedRssis.Select(e => IPAddress.Parse(e.ForeignId)));
                cacheMaintenance.DeleteForAddress(outdatedRssiFailures.SelectMany(e => e.AffectedHosts.Select(h => IPAddress.Parse(h))));
                cacheMaintenance.DeleteForAddress(outdatedBgpPeers.Select(e => IPAddress.Parse(e.LocalAddress)));
                cacheMaintenance.DeleteForAddress(outdatedBgpPeerFailures.Select(e => IPAddress.Parse(e.Host)));

                if (!this.dryRunMode)
                {
                    this.resultDatabaseContext.RemoveRange(outdatedRssis);
                    this.resultDatabaseContext.RemoveRange(outdatedRssiFailures);
                    this.resultDatabaseContext.RemoveRange(outdatedBgpPeers);
                    this.resultDatabaseContext.RemoveRange(outdatedBgpPeerFailures);

                    this.resultDatabaseContext.SaveChanges();
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Removes the cache entries for failures recorded in the result database
        /// </summary>
        /// <param name="cacheMaintenance">The cache maintenance object that supports deletion of entries.</param>
        private void RemoveCacheEntriesForFailures(CacheMaintenance cacheMaintenance)
        {
            var failures              = this.resultDatabaseContext.RssiFailingQueries;
            var affectedHosts         = failures.Select(q => q.AffectedHosts);
            List <IPAddress> toDelete = new List <IPAddress>();

            foreach (IReadOnlyCollection <string> item in affectedHosts)
            {
                toDelete.AddRange(item.Select(ah => IPAddress.Parse(ah)));
            }

            cacheMaintenance.DeleteForAddress(toDelete.Distinct());
        }