Changeset 1655


Ignore:
Timestamp:
Jun 16, 2010, 10:25:53 AM (12 years ago)
Author:
Sven Rech
Message:

better alive checking mechanism for quadratic sieve

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/QuadraticSieve/PeerToPeer.cs

    r1653 r1655  
    3636    class PeerToPeer
    3737    {
     38        /// <summary>
     39        /// This structure is used to hold all important peer performance informations in a queue
     40        /// </summary>
    3841        private struct PeerPerformanceInformations
    3942        {
     
    4144            public int peerID;
    4245            public double performance;
    43             public int lastAlive;           
    44 
    45             public PeerPerformanceInformations(DateTime lastChecked, int peerID, double performance, int lastAlive)
    46             {
    47                 this.lastAlive = lastAlive;
     46
     47            public PeerPerformanceInformations(DateTime lastChecked, int peerID, double performance)
     48            {
    4849                this.lastChecked = lastChecked;
    4950                this.peerID = peerID;
     
    5657        private BigInteger factor;
    5758        private int head;
     59        private DateTime headLastUpdated;
    5860        private Queue storequeue;   //yields to store in the DHT
    5961        private Queue loadqueue;    //yields that have been loaded from the DHT
     
    6769        private HashSet<int> activePeers;                                 //A set of active peers
    6870        private double ourPerformance = 0;
    69         private int aliveCounter = 0;       //This is stored together with the performance in the DHT
    7071        private string ourName;
    7172        private uint downloaded = 0;
     
    157158                        name = nameCache[ownerID];
    158159
    159                     //Performance and alive informations:
     160                    //Performance informations:
    160161                    if (!activePeers.Contains(ownerID))
    161                     {
    162                         UpdatePeerPerformanceAndAliveInformation(ownerID);
    163                         activePeers.Add(ownerID);
    164                         UpdateActivePeerInformation();
    165                     }
     162                        UpdatePeerPerformanceInformation(ownerID);
    166163                }
    167164
     
    182179        {
    183180            bool succ = ReadAndEnqueueYield(index);
    184             if (!succ)               
     181            if (!succ)
    185182            {
    186183                var e = new KeyValuePair<int, DateTime>(index, DateTime.Now);
     
    198195            //Stores all the indices (together with there check date) which belong to lost packets (i.e. packets that can't be load anymore):
    199196            Queue<KeyValuePair<int, DateTime>> lostIndices = new Queue<KeyValuePair<int, DateTime>>();
    200             double lastPerformance = 0;
    201             DateTime performanceLastPut = new DateTime();
     197            double lastStoredPerformance = 0;
     198            int lastStoredHead = 0;
    202199
    203200            try
     
    205202                while (!stopLoadStoreThread)
    206203                {
    207                     //Store our performance and our alive counter in the DHT, either if the performance changed or when the last write was more than 1 minute ago:
    208                     if (ourPerformance != lastPerformance || performanceLastPut.CompareTo(DateTime.Now.Subtract(new TimeSpan(0, 1, 0))) < 0)
     204                    //Store our performance and our head in the DHT as soon as the performance or the head changed:
     205                    if (ourPerformance != lastStoredPerformance || lastStoredHead != head)
    209206                    {
    210207                        P2PManager.Retrieve(PerformanceIdentifier(ourID));      //just to outsmart the versioning system
    211                         P2PManager.Store(PerformanceIdentifier(ourID), concat(BitConverter.GetBytes(ourPerformance), BitConverter.GetBytes(aliveCounter++)));
    212                         lastPerformance = ourPerformance;
    213                         performanceLastPut = DateTime.Now;
    214                     }
    215 
    216                     //updates all peer performances which have last been checked more than 2 minutes ago and check if they are still alive:
    217                     while (peerPerformances.Count != 0 && peerPerformances.Peek().lastChecked.CompareTo(DateTime.Now.Subtract(new TimeSpan(0, 2, 0))) < 0)
     208                        P2PManager.Store(PerformanceIdentifier(ourID), concat(BitConverter.GetBytes(ourPerformance), BitConverter.GetBytes(head)));
     209                        lastStoredPerformance = ourPerformance;
     210                        lastStoredHead = head;
     211                    }
     212
     213                    //updates all peer performances which have last been checked more than 1 minute ago and check if they are still alive:
     214                    while (peerPerformances.Count != 0 && peerPerformances.Peek().lastChecked.CompareTo(DateTime.Now.Subtract(new TimeSpan(0, 1, 0))) < 0)
    218215                    {
    219216                        var e = peerPerformances.Dequeue();
    220 
    221                         byte[] performancebytes = P2PManager.Retrieve(PerformanceIdentifier(e.peerID));
    222                         if (performancebytes != null)
    223                         {
    224                             double performance = BitConverter.ToDouble(performancebytes, 0);
    225                             int peerAliveCounter = BitConverter.ToInt32(performancebytes, 8);
    226                             if (peerAliveCounter <= e.lastAlive)
    227                             {
    228                                 activePeers.Remove(e.peerID);
    229                                 UpdateActivePeerInformation();
    230                             }
    231                             else
    232                                 peerPerformances.Enqueue(new PeerPerformanceInformations(DateTime.Now, e.peerID, performance, peerAliveCounter));
    233                         }
    234                         else
    235                         {
    236                             activePeers.Remove(e.peerID);
    237                         }                       
     217                        UpdatePeerPerformanceInformation(e.peerID);
    238218                    }
    239219
     
    307287        }
    308288
    309         private void UpdatePeerPerformanceAndAliveInformation(int peerID)
     289        private void UpdatePeerPerformanceInformation(int peerID)
    310290        {
    311291            byte[] performancebytes = P2PManager.Retrieve(PerformanceIdentifier(peerID));
     
    313293            {
    314294                double performance = BitConverter.ToDouble(performancebytes, 0);
    315                 int peerAliveCounter = BitConverter.ToInt32(performancebytes, 8);
    316                 peerPerformances.Enqueue(new PeerPerformanceInformations(DateTime.Now, peerID, performance, peerAliveCounter));
    317             }
     295                int peersHead = BitConverter.ToInt32(performancebytes, 8);
     296
     297                //Check if peer hasn't updated its head for more than 1 minute and if so, set him inactive:
     298                if (peersHead < head && headLastUpdated.CompareTo(DateTime.Now.Subtract(new TimeSpan(0, 1, 0))) < 0)
     299                {
     300                    activePeers.Remove(peerID);                   
     301                }
     302                else    //set active
     303                {
     304                    peerPerformances.Enqueue(new PeerPerformanceInformations(DateTime.Now, peerID, performance));
     305                    activePeers.Add(peerID);
     306                }
     307            }
     308            else
     309            {
     310                activePeers.Remove(peerID);
     311            }
     312            UpdateActivePeerInformation();
    318313        }
    319314
     
    336331                {
    337332                    head = dhthead;
     333                    headLastUpdated = DateTime.Now;
    338334                    SetProgressYield(head - 1, 0, null);
    339335                }
Note: See TracChangeset for help on using the changeset viewer.