Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/08 12:22:58 (16 years ago)
Author:
gkronber
Message:
  • extracted communication code out of the DistributedEngine into class JobManager
  • implemented a method to retrieve the JobState for a given job (specified by it's guid) in GridServer
  • implemented restarting of jobs in JobManager
  • improved exception handling in JobManager and DistributedEngine

(ticket #136)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Grid/EngineStore.cs

    r35 r219  
    3131    private List<Guid> engineList;
    3232    private Dictionary<Guid, byte[]> waitingEngines;
    33     private Dictionary<Guid, byte[]> runningEngines;
    3433    private Dictionary<Guid, ManualResetEvent> waitHandles;
    3534    private Dictionary<Guid, byte[]> results;
    36     private Dictionary<Guid, string> runningClients;
     35    private Dictionary<Guid, DateTime> resultDate;
    3736    private object bigLock;
    3837    private ChannelFactory<IClient> clientChannelFactory;
     
    4544    public int RunningJobs {
    4645      get {
    47         return runningEngines.Count;
     46        return waitHandles.Count;
    4847      }
    4948    }
     
    5857      engineList = new List<Guid>();
    5958      waitingEngines = new Dictionary<Guid, byte[]>();
    60       runningEngines = new Dictionary<Guid, byte[]>();
    61       runningClients = new Dictionary<Guid, string>();
    6259      waitHandles = new Dictionary<Guid, ManualResetEvent>();
    6360      results = new Dictionary<Guid, byte[]>();
     61      resultDate = new Dictionary<Guid, DateTime>();
    6462      bigLock = new object();
    6563
     
    7371    }
    7472
    75     public bool TryTakeEngine(string clientUrl, out Guid guid, out byte[] engine) {
     73    public bool TryTakeEngine(out Guid guid, out byte[] engine) {
    7674      lock(bigLock) {
    7775        if(engineList.Count == 0) {
     
    8482          engine = waitingEngines[guid];
    8583          waitingEngines.Remove(guid);
    86           runningEngines[guid] = engine;
    87           runningClients[guid] = clientUrl;
    8884          return true;
    8985        }
     
    9389    public void StoreResult(Guid guid, byte[] result) {
    9490      lock(bigLock) {
    95         if(!runningEngines.ContainsKey(guid)) return; // ignore result when the engine is not known to be running
    96 
    97         runningEngines.Remove(guid);
    98         runningClients.Remove(guid);
     91        // clear old results
     92        List<Guid> expiredResults = FindExpiredResults(DateTime.Now.AddHours(-1.0));
     93        foreach(Guid expiredGuid in expiredResults) {
     94          results.Remove(expiredGuid);
     95          waitHandles.Remove(expiredGuid);
     96          resultDate.Remove(expiredGuid);
     97        }
     98        // add the new result
    9999        results[guid] = result;
     100        resultDate[guid] = DateTime.Now;
    100101        waitHandles[guid].Set();
    101102      }
     103    }
     104
     105    private List<Guid> FindExpiredResults(DateTime expirationDate) {
     106      List<Guid> expiredResults = new List<Guid>();
     107      foreach(Guid guid in results.Keys) {
     108        if(resultDate[guid] < expirationDate) {
     109          expiredResults.Add(guid);
     110        }
     111      }
     112      return expiredResults;
    102113    }
    103114
     
    113124      return GetResult(guid, System.Threading.Timeout.Infinite);
    114125    }
     126
    115127    internal byte[] GetResult(Guid guid, int timeout) {
    116128      lock(bigLock) {
    117         if(waitHandles.ContainsKey(guid)) {
     129        // result already available
     130        if(results.ContainsKey(guid)) {
     131          // if the wait-handle for this result is still alive then close and remove it
     132          if(waitHandles.ContainsKey(guid)) {
     133            ManualResetEvent waitHandle = waitHandles[guid];
     134            waitHandle.Close();
     135            waitHandles.Remove(guid);
     136          }
     137          return results[guid];
     138        } else {
     139          // result not yet available, if there is also no wait-handle for that result then we will never have a result and can return null
     140          if(!waitHandles.ContainsKey(guid)) return null;
     141
     142          // otherwise we have a wait-handle and can wait for the result
    118143          ManualResetEvent waitHandle = waitHandles[guid];
     144          // wait
    119145          if(waitHandle.WaitOne(timeout, true)) {
     146            // ok got the result in within the wait time => close and remove the wait-hande and return the result
    120147            waitHandle.Close();
    121148            waitHandles.Remove(guid);
    122149            byte[] result = results[guid];
    123             results.Remove(guid);
    124150            return result;
    125151          } else {
     152            // no result yet return without result
    126153            return null;
    127154          }
    128         } else {
    129           return null;
    130155        }
    131156      }
     
    133158
    134159    internal void AbortEngine(Guid guid) {
    135       string clientUrl = "";
    136160      lock(bigLock) {
    137         if(runningClients.ContainsKey(guid)) {
    138           clientUrl = runningClients[guid];
    139           IClient client = clientChannelFactory.CreateChannel(new EndpointAddress(clientUrl));
    140           client.Abort(guid);
    141         } else if(waitingEngines.ContainsKey(guid)) {
     161        if(waitingEngines.ContainsKey(guid)) {
    142162          byte[] engine = waitingEngines[guid];
    143163          waitingEngines.Remove(guid);
     
    148168      }
    149169    }
     170
     171    internal JobState JobState(Guid guid) {
     172      lock(bigLock) {
     173        if(waitingEngines.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Waiting;
     174        else if(waitHandles.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Busy;
     175        else if(results.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Finished;
     176        else return HeuristicLab.Grid.JobState.Unkown;
     177      }
     178    }
    150179  }
    151180}
Note: See TracChangeset for help on using the changeset viewer.