source: trunk/CrypPlugins/PeerToPeerManager_NEW/P2PManagerBase_NEW.cs @ 1264

Last change on this file since 1264 was 1264, checked in by arnold, 12 years ago

Little changes in P2PManager and P2PWorker

File size: 25.2 KB
Line 
1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
2
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6
7       http://www.apache.org/licenses/LICENSE-2.0
8
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14*/
15
16using System;
17using System.Collections.Generic;
18using System.Linq;
19using System.Text;
20using Cryptool.PluginBase.Control;
21using Cryptool.Plugins.PeerToPeer.Jobs;
22using Cryptool.PluginBase;
23using Cryptool.PluginBase.Miscellaneous;
24
25/* TODO:
26 * - Publisher-change is possible, but catch old Publishers subscriber list
27 *   isn't implemented yet ((de)serialization of the subscribers is
28 *   implemented and tested)
29 * - Manager-change is possible, but catch job history isn't implemented yes
30 *   ((de)serialization of job management lists)
31 * - Benchmarking the working peers
32 *   (this.distributableJobControl.SetResult() returns the TimeSpan for the result)
33 * - Insert internal Start-/Stop-Button, so Manager can stop its works without
34 *   loosing any Job-Information (this happens at present, when pressing the Stop
35 *   button of the CrypTool Workspace)
36 */
37
38namespace Cryptool.Plugins.PeerToPeer
39{
40    public class P2PManagerBase_NEW : P2PPublisherBase
41    {
42        #region Events and Delegates
43
44        public delegate void ProcessProgress(double progressInPercent);       
45        public delegate void NewJobAllocated(BigInteger jobId);
46        public delegate void ResultReceived(BigInteger jobId);
47        public delegate void JobCanceled(BigInteger jobId);
48        public delegate void NoMoreJobsLeft();
49        public delegate void AllJobResultsReceived(BigInteger lastJobId);
50        public event ProcessProgress OnProcessProgress;
51        /// <summary>
52        /// When a new job was successfully allocated to a worker (after receiving
53        /// its "JobAccepted"-Message), this event is thrown
54        /// </summary>
55        public event NewJobAllocated OnNewJobAllocated;
56        /// <summary>
57        /// When a new job result was received (and accepted) this event is thrown
58        /// </summary>
59        public event ResultReceived OnResultReceived;
60        /// <summary>
61        /// is thrown when an active worker leaves the network and the jobs come back
62        /// </summary>
63        public event JobCanceled OnJobCanceled;
64        /// <summary>
65        /// When the last job from the DistributableJob-Stack is allocated, but
66        /// the Manager is still waiting for some JobResults this event is thrown
67        /// </summary>
68        public event NoMoreJobsLeft OnNoMoreJobsLeft;
69        /// <summary>
70        /// When no more jobs left AND the last "ausstehendes" JobResult comes in,
71        /// this event is thrown
72        /// </summary>
73        public event AllJobResultsReceived OnAllJobResultsReceived;
74
75        #endregion
76
77        #region Variables
78
79        /// <summary>
80        /// this control contains a JobStack and other special
81        /// management for a SPECIAL distributable Job
82        /// </summary>
83        private IDistributableJob distributableJobControl;
84        /// <summary>
85        /// this list contains all jobs, which were sent to workers,
86        /// but the workers hadn't accept/decline the Job at present
87        /// </summary>
88        private Dictionary<BigInteger, PeerId> jobsWaitingForAcceptanceInfo;
89        /// <summary>
90        /// this dict contains all jobs/workers, who were successfully
91        /// distributed (so the manager already had received a JobAccepted Msg)
92        /// </summary>
93        private Dictionary<BigInteger, PeerId> jobsInProgress;
94
95        private bool managerStarted = false;
96        public bool ManagerStarted
97        {
98            get { return this.managerStarted; }
99            private set { this.managerStarted = value; }
100        }
101
102        /// <summary>
103        /// When the Manager is started, this variable must be set.
104        /// </summary>
105        private string sTopic = String.Empty;
106        public string TopicName
107        {
108            get { return this.sTopic; }
109            private set { this.sTopic = value; }
110        }
111
112        private long lAliveMessageInterval;
113        public long AliveMesageInterval
114        {
115            get { return this.lAliveMessageInterval ; }
116            set { this.lAliveMessageInterval = value; }
117        }
118
119        private DateTime startWorkingTime = DateTime.MinValue;
120        /// <summary>
121        /// This value will be initialized after allocating the first job to a worker.
122        /// Before initialization this is MinValue! Used for end time approximation
123        /// </summary>
124        public DateTime StartWorkingTime
125        {
126            get { return this.startWorkingTime; } 
127        }
128
129        private bool lastJobAllocated = false;
130
131        #endregion
132
133        public P2PManagerBase_NEW(IP2PControl p2pControl, IDistributableJob distributableJob) : base(p2pControl)
134        {
135            this.distributableJobControl = distributableJob;
136
137            this.jobsWaitingForAcceptanceInfo = new Dictionary<BigInteger, PeerId>();
138            this.jobsInProgress = new Dictionary<BigInteger, PeerId>();
139        }
140
141        public void StartManager(string sTopic, long aliveMessageInterval)
142        {
143            // only when the main manager plugin is connected with a Peer-PlugIn
144            // and a IWorkerControl-PlugIn, this Manager can start its work
145            if (this.distributableJobControl != null && this.p2pControl != null)
146            {
147                this.distributableJobControl.OnLastResultReceived += new LastResultReceived(distributableJobControl_OnLastResultReceived);
148
149                //set value to null, when restarting the manager
150                this.startWorkingTime = DateTime.MinValue; 
151                this.TopicName = sTopic;
152                this.AliveMesageInterval = aliveMessageInterval;
153                base.Start(this.TopicName, this.AliveMesageInterval);
154            }
155            else
156            {
157                GuiLogging("Manager couldn't be started, because P2P-Peer- or Distributable-Job-PlugIn isn't connected with the Manager or the connection is broken...", NotificationLevel.Warning);
158            }
159        }
160
161        void distributableJobControl_OnLastResultReceived(BigInteger jobId)
162        {
163            if (OnAllJobResultsReceived != null)
164                OnAllJobResultsReceived(jobId);
165
166            /* New Feature. When all JobResults are received, stop Manager, so another
167             * Manager can replace it and then allocate Jobs to the free workers*/
168            GuiLogging("All Job results received, so deregistering from the solution network to accomodate this topic for another Manager.",NotificationLevel.Info);
169            Stop(PubSubMessageType.Unregister);
170        }
171
172        protected override void PeerCompletelyStarted()
173        {
174            base.PeerCompletelyStarted();
175
176            this.ManagerStarted = true;
177            GetProgressInformation();
178            GuiLogging("P2PManager is started right now.", NotificationLevel.Info);
179        }
180
181        public override void Stop(PubSubMessageType msgType)
182        {
183            base.Stop(msgType);
184
185            this.ManagerStarted = false;
186            ((WorkersManagement)this.peerManagement).OnFreeWorkersAvailable -= peerManagement_OnFreeWorkersAvailable;
187            ((WorkersManagement)this.peerManagement).OnSubscriberRemoved -= peerManagement_OnSubscriberRemoved;
188            this.distributableJobControl.OnLastResultReceived -= distributableJobControl_OnLastResultReceived;
189
190            GuiLogging("P2PManager was stopped successully.", NotificationLevel.Info);
191        }
192
193        /// <summary>
194        /// because the manager needs additional peer information for all workers,
195        /// this method is overwritten. WorkersManagement throws events, when
196        /// a Worker leaves or joins the "solution network", so we can re-add or
197        /// allocate a job.
198        /// </summary>
199        /// <param name="aliveMessageInterval"></param>
200        protected override void AssignManagement(long aliveMessageInterval)
201        {
202            this.peerManagement = new WorkersManagement(aliveMessageInterval);
203            this.peerManagement.OnSubscriberRemoved +=new SubscriberManagement.SubscriberRemoved(peerManagement_OnSubscriberRemoved);
204            // waiting for new workers joining the manager or already joined worker, who were set to "free" again
205            ((WorkersManagement)this.peerManagement).OnFreeWorkersAvailable += new WorkersManagement.FreeWorkersAvailable(peerManagement_OnFreeWorkersAvailable);
206        }
207
208        /// <summary>
209        /// only accepts DistributableJob-specific messages (created, checked and transformed by
210        /// the static class JobMessages). All other message are dropped!
211        /// </summary>
212        /// <param name="sender"></param>
213        /// <param name="data"></param>
214        protected override void p2pControl_OnPayloadMessageReceived(PeerId sender, byte[] data)
215        {
216            if (!JobMessages.IsJobMessageType(data[0]))
217            {
218                GuiLogging("Received an undefined message (not a job accepted message or a job result).", NotificationLevel.Debug);
219                return;
220            } 
221            switch (JobMessages.GetMessageJobType(data[0]))
222            {
223                case MessageJobType.JobAcceptanceInfo:
224                    HandleJobAcceptanceMessage(sender, data);
225                    break;
226                case MessageJobType.JobResult:
227                    GuiLogging("Received JobResult message from Peer '" + sender.ToString() + "'. Beginning to set result now.", NotificationLevel.Debug);
228                    HandleJobResultMessage(sender, data);
229                    break;
230                case MessageJobType.Free:
231                    HandleFreeMessage(sender, data);
232                    break;
233                default:
234                    GuiLogging("Obscure Message. First byte: " + Convert.ToInt32(data[0]) + ". Data: (" + Encoding.UTF8.GetString(data) + ") received from '" + sender.ToString() + "'.", NotificationLevel.Info);
235                    break;
236            } // end switch
237            GetProgressInformation();
238        }
239
240        /// <summary>
241        /// This method is only overwritten because we have to ignore the Solution-case in
242        /// the System-Message-Handling (a Peer mustn't send a Solution message, which influences
243        /// the working status of the Manager, because it havn't the overview of the JobParts)
244        /// </summary>
245        /// <param name="sender"></param>
246        /// <param name="msgType"></param>
247        protected override void p2pControl_OnSystemMessageReceived(PeerId sender, PubSubMessageType msgType)
248        {
249            // ignore Solution case, because other worker could work on...
250            if (msgType != PubSubMessageType.Solution)
251                // base class handles all administration cases (register, alive, unregister, ping, pong, ...)
252                base.p2pControl_OnSystemMessageReceived(sender, msgType);
253        }
254
255        #region Handle different DistributableJob-specific, incoming messages
256
257        /// <summary>
258        /// Handles the two job-acceptance cases (accepted or declined). Adds accepted jobs
259        /// to the "inProgress" Dictionary, sets a busy declined worker to free (when message
260        /// is JobDeclined) and removes the job in every case from the waitingForAcceptance list
261        /// </summary>
262        /// <param name="sender"></param>
263        /// <param name="data"></param>
264        private void HandleJobAcceptanceMessage(PeerId sender, byte[] data)
265        {
266            BigInteger jobId = null;
267            if (JobMessages.GetJobAcceptanceMessage(data, out jobId))
268            {
269                this.distributableJobControl.JobAccepted(jobId);
270                lock (this.jobsInProgress)
271                {
272                    if (!this.jobsInProgress.ContainsKey(jobId))
273                    {
274                        // add to jobs in progress, because P2PJobAdmin has accepted the job!
275                        this.jobsInProgress.Add(jobId, sender);
276                        if (OnNewJobAllocated != null)
277                            OnNewJobAllocated(jobId);
278                    }
279                    //else
280                    //    throw (new Exception("Received a JobAccepted message for a already accepted JobId... JobId: " + jobId.ToString()));
281                }
282                GuiLogging("JobId '" + jobId.ToString() + "' was accepted by Peer '" + sender.ToString() + "'.", NotificationLevel.Info);
283            }
284            else // if AcceptanceInfo is declined
285            {
286                this.distributableJobControl.JobDeclined(jobId);
287
288                // set busy worker to free, because he delined the job
289
290                // TODO: maybe create a "black list" for peers, who had declined this kind of Job twice or more...
291                ((WorkersManagement)this.peerManagement).SetBusyWorkerToFree(sender);
292                GuiLogging("JobId '" + jobId.ToString() + "' was declined by Peer '" + sender.ToString() + "'.", NotificationLevel.Info);
293            }
294            // in every case remove the job from thew waiting Dictionary
295            lock (this.jobsWaitingForAcceptanceInfo)
296            {
297                if (this.jobsWaitingForAcceptanceInfo.ContainsKey(jobId))
298                {
299                    this.jobsWaitingForAcceptanceInfo.Remove(jobId);
300                }
301                //else
302                //    throw (new Exception("Received a JobAcceptance-Message for a jobId, which isn't in the waitingForAcceptance-List... JobId: " + jobId.ToString()));
303            }
304        }
305
306        /// <summary>
307        /// Sets the incoming result in the DistributableJob class, removes the job from
308        /// the JobsInProgress Dictionary and throws the OnResultReceivedEvent
309        /// </summary>
310        /// <param name="sender"></param>
311        /// <param name="data"></param>
312        private void HandleJobResultMessage(PeerId sender, byte[] data)
313        {
314            BigInteger jobId;
315
316            byte[] serializedJobResult = JobMessages.GetJobResult(data, out jobId);
317            TimeSpan jobProcessingTime = this.distributableJobControl.SetResult(jobId, serializedJobResult);
318
319            if (OnResultReceived != null)
320                OnResultReceived(jobId);
321
322            GuiLogging("JobResult for Job '" + jobId.ToString() + "' received. Processing Time: "
323                + jobProcessingTime.TotalMinutes.ToString() + " minutes. Worker-Id: '" + sender.ToString() + "'.", NotificationLevel.Info);
324
325            lock (this.jobsInProgress)
326            {
327                if (this.jobsInProgress.ContainsKey(jobId))
328                    this.jobsInProgress.Remove(jobId);
329                //dirty workaround because P2PJobAdmin sends the result msg twice...
330                //else
331                //    throw (new Exception("Received a valid job result, which wasn't allocated before!!!"));
332            }
333        }
334
335        /// <summary>
336        /// If message content declares the sender as a free worker,
337        /// set this worker from busy to free, otherwise do nothing
338        /// </summary>
339        /// <param name="sender"></param>
340        /// <param name="data"></param>
341        private void HandleFreeMessage(PeerId sender, byte[] data)
342        {
343            // only handle the "true"-case, because otherwise there is nothing to do
344            if (JobMessages.GetFreeWorkerStatusMessage(data))
345            {
346                GuiLogging("Received a 'free'-message from Peer '" + sender.ToString() + "'.", NotificationLevel.Debug);
347                // only if worker already exists in the "busy list", it will set to free and event will be thrown
348                ((WorkersManagement)this.peerManagement).SetBusyWorkerToFree(sender);
349            }
350        }
351
352        #endregion
353
354        #region Worker-action-handling
355
356        /// <summary>
357        /// every time when new workers are available, continue distribution of Jobs (if any JobParts left)
358        /// </summary>
359        private void peerManagement_OnFreeWorkersAvailable()
360        {
361            if (!this.ManagerStarted)
362            {
363                GuiLogging("Manager isn't started at present, so I can't disperse the patterns.", NotificationLevel.Error);
364                bool removeSettings = DHT_CommonManagement.DeleteAllPublishersEntries(ref this.p2pControl, this.TopicName);
365                if (removeSettings)
366                    GuiLogging("Manager is stopped, but DHT entries were still existing, so they were deleted!", NotificationLevel.Info);
367                else
368                    throw (new Exception("Critical error in P2PManager. Manager isn't started yet, but the workers can register... Even removing DHT entries weren't possible..."));
369            }
370            else
371            {
372                /* edited by Arnold - 2010.02.23 */
373                // because parallel incoming free workers could run
374                // into concurrence in this method, so some workers
375                // could get more than one job - so they have to
376                // queue the additional jobs.
377                lock (this)
378                {
379                    AllocateJobs();
380                }
381            }
382
383            GetProgressInformation();
384        }
385
386        /// <summary>
387        /// When a Worker leaves the network, its (maybe) allocated JobParts have to
388        /// be pushed back to the main Jobstack
389        /// </summary>
390        /// <param name="peerId"></param>
391        private void peerManagement_OnSubscriberRemoved(PeerId peerId)
392        {
393            GuiLogging("REMOVED worker " + peerId, NotificationLevel.Info);
394
395            // necessary lock, because the amount of jobs in Progress could change while traversing this list
396            lock (this.jobsInProgress)
397            {
398                // push job back and remove list entries for "jobs in progress"
399                List<BigInteger> allJobsForRemovedPeer = (from k in this.jobsInProgress where k.Value == peerId select k.Key).ToList<BigInteger>();
400
401                BigInteger jobId;
402                for (int i = 0; i < allJobsForRemovedPeer.Count; i++)
403                {
404                    jobId = allJobsForRemovedPeer[i];
405                    this.distributableJobControl.Push(jobId);
406                    this.jobsInProgress.Remove(jobId);
407                    if (OnJobCanceled != null)
408                        OnJobCanceled(jobId);
409                    GuiLogging("Pushed job '" + jobId.ToString() + "' back to the stack, because peer left the network.", NotificationLevel.Debug);
410                }
411            }
412
413            // necessary lock, because the amount of jobs in Progress could change while traversing this list
414            lock (this.jobsWaitingForAcceptanceInfo)
415            {
416                // Set the JobDeclined-status for all jobs of the removed peer, which are still waiting
417                // for an acceptance information. Than remove all jobs from the "jobs waiting for acceptance info" List
418                List<BigInteger> allWaitingEntriesForRemovedPeer = (from k in this.jobsWaitingForAcceptanceInfo where k.Value == peerId select k.Key).ToList<BigInteger>();
419
420                for (int i = 0; i < allWaitingEntriesForRemovedPeer.Count; i++)
421                {
422                    this.distributableJobControl.JobDeclined(allWaitingEntriesForRemovedPeer[i]);
423                    this.jobsWaitingForAcceptanceInfo.Remove(allWaitingEntriesForRemovedPeer[i]);
424                    GuiLogging("Declined job '" + allWaitingEntriesForRemovedPeer[i].ToString() + "', because peer left the network.", NotificationLevel.Debug);
425                }
426            }
427
428            GetProgressInformation();
429        }
430       
431        /// <summary>
432        /// Allocates new JobParts to new registered or calling-for-jobs Workers.
433        /// Additionally it adds the allocated job to a waitingForAcceptance Dictionary,
434        /// so it can be checked, if the Worker respond to the Job-allocation
435        /// </summary>
436        private void AllocateJobs()
437        {
438            int i = 0;
439            BigInteger temp_jobId = null;
440            List<PeerId> freePeers = ((WorkersManagement)this.peerManagement).GetFreeWorkers();
441
442            GuiLogging("Trying to allocate " + freePeers.Count + " job(s) to workers.", NotificationLevel.Debug);
443
444            // set the start working time after allocating the FIRST job
445            if (this.startWorkingTime == DateTime.MinValue && freePeers.Count > 0)
446                this.startWorkingTime = DateTime.Now;
447
448            foreach (PeerId worker in freePeers)
449            {
450                byte[] serializedNewJob = this.distributableJobControl.Pop(out temp_jobId);
451                if (serializedNewJob != null) // if this is null, there are no more JobParts on the main stack!
452                {
453                    this.jobsWaitingForAcceptanceInfo.Add(temp_jobId, worker);
454
455                    // set free worker to busy in the peerManagement class
456                    ((WorkersManagement)this.peerManagement).SetFreeWorkerToBusy(worker);
457
458                    // get actual subscriber/worker and send the new job
459                    base.p2pControl.SendToPeer(JobMessages.CreateJobPartMessage(temp_jobId, serializedNewJob), worker);
460
461                    if (OnNewJobAllocated != null)
462                        OnNewJobAllocated(temp_jobId);
463
464                    GuiLogging("Job '" + temp_jobId.ToString() + "' was sent to worker id '" + worker.ToString() + "'", NotificationLevel.Info);
465                    i++;
466                }
467                else
468                {
469                    //todo: insert sending "no more jobs left msg" to the free worker, so it can stop its Free-Msg-Timer
470                    GuiLogging("No more jobs left. So wait for the last results, than close this task.", NotificationLevel.Debug);
471                    if (OnNoMoreJobsLeft != null)
472                        OnNoMoreJobsLeft();
473                }
474            } // end foreach
475            GuiLogging(i + " Job(s) allocated to worker(s).", NotificationLevel.Debug);
476        }
477
478        #endregion
479
480        /// <summary>
481        /// returns the percental progress information of the whole job (value is between 0 and 100)
482        /// </summary>
483        /// <returns>the percental progress information of the whole job</returns>
484        private double GetProgressInformation()
485        {
486            double jobProgressInPercent;
487            double lFinishedAmount = (double)this.distributableJobControl.FinishedAmount.LongValue();
488            double lAllocatedAmount = (double)this.distributableJobControl.AllocatedAmount.LongValue();
489            double lTotalAmount = (double)this.distributableJobControl.TotalAmount.LongValue();
490
491            if (lFinishedAmount > 0 && lAllocatedAmount > 0)
492            {
493                jobProgressInPercent = 30 * (lAllocatedAmount / lTotalAmount) + 100 * (lFinishedAmount / lTotalAmount);
494            }
495            else if (lAllocatedAmount > 0)
496            {
497                jobProgressInPercent = 30 * (lAllocatedAmount / lTotalAmount);
498            }
499            else if (lFinishedAmount > 0)
500            {
501                jobProgressInPercent = 100 * (lFinishedAmount / lTotalAmount);
502            }
503            else
504            {
505                jobProgressInPercent = 0.0;
506            }
507
508            if (OnProcessProgress != null)
509                OnProcessProgress(jobProgressInPercent);
510
511            return jobProgressInPercent;
512        }
513
514        /// <summary>
515        /// returns the estimated end time (correlation between Start Time, Total amount of jobs and finished jobs).
516        /// When no job is finished yet, it returns an empty timespan
517        /// </summary>
518        /// <returns></returns>
519        public DateTime EstimatedEndTime()
520        {
521            DateTime retTime = DateTime.MaxValue;
522            if (this.distributableJobControl.FinishedAmount.LongValue() > 0)
523            {
524                TimeSpan bruteforcingTime = DateTime.Now.Subtract(this.StartWorkingTime);
525                double jobsPerSecond = bruteforcingTime.TotalSeconds / this.distributableJobControl.FinishedAmount.LongValue();
526                double restSeconds = jobsPerSecond * 
527                    (this.distributableJobControl.TotalAmount - this.distributableJobControl.FinishedAmount).LongValue();
528                //retTime.TotalSeconds = jobsPerSecond * (2 - (progressInPercent / 100));
529                retTime = DateTime.Now.AddSeconds(restSeconds);
530            }
531            return retTime;
532        }
533
534        #region Forward PeerManagement Values
535
536        public int FreeWorkers() { return ((WorkersManagement)peerManagement).GetFreeWorkersAmount(); }
537        public int BusyWorkers() { return ((WorkersManagement)peerManagement).GetBusyWorkersAmount(); }
538
539        #endregion
540    }
541}
Note: See TracBrowser for help on using the repository browser.