Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/10/08 17:22:04 (16 years ago)
Author:
gkronber
Message:

improved contention problem by using ReaderWriterLock in the DB proxy and reducing the number of threads in the RunScheduler (ticket #189)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Server/RunScheduler.cs

    r378 r380  
    3737    private const int RELEASE_INTERVAL = 5;
    3838    private object remoteCommLock = new object();
     39    private object collectionsLock = new object();
     40    private Queue<WaitHandle> waithandles;
     41    private Dictionary<WaitHandle, AtomicOperation> runningOperations;
     42    private Dictionary<WaitHandle, long> runningEntries;
    3943
    4044    public RunScheduler(Database database, JobManager jobManager) {
    4145      this.database = database;
    4246      this.jobManager = jobManager;
     47      runningOperations = new Dictionary<WaitHandle, AtomicOperation>();
     48      runningEntries = new Dictionary<WaitHandle, long>();
     49      waithandles = new Queue<WaitHandle>();
     50      Thread resultsGatheringThread = new Thread(GatherResults);
     51      resultsGatheringThread.Start();
    4352    }
    4453    public void Run() {
     
    6473        }
    6574
    66         ThreadPool.QueueUserWorkItem(WaitForFinishedRun, new object[] {wHandle, op, entry});
     75        lock(collectionsLock) {
     76          waithandles.Enqueue(wHandle);
     77          runningOperations[wHandle] = op;
     78          runningEntries[wHandle] = entry.Id;
     79        }
    6780      }
    6881    }
    6982
    70     private void WaitForFinishedRun(object state) {
    71       object[] param = (object[])state;
    72       WaitHandle wHandle = (WaitHandle)param[0];
    73       AtomicOperation op = (AtomicOperation)param[1];
    74       RunEntry entry = (RunEntry)param[2];
    75       wHandle.WaitOne();
    76       wHandle.Close();
    77       lock(remoteCommLock) {
    78         jobManager.EndExecuteOperation(op);
    79         database.UpdateRunStatus(entry.Id, ProcessStatus.Finished);
    80         database.UpdateRunFinished(entry.Id, DateTime.Now);
     83    private void GatherResults() {
     84      while(true) {
     85        if(waithandles.Count == 0) Thread.Sleep(1000);
     86        else {
     87          WaitHandle w;
     88          lock(collectionsLock) {
     89            w = waithandles.Dequeue();
     90          }
     91          w.WaitOne();
     92          long id;
     93          AtomicOperation op;
     94          lock(collectionsLock) {
     95            id = runningEntries[w];
     96            runningEntries.Remove(w);
     97            op = runningOperations[w];
     98            runningOperations.Remove(w);
     99          }
     100          w.Close();
     101          lock(remoteCommLock) {
     102            jobManager.EndExecuteOperation(op);
     103            database.UpdateRunStatus(id, ProcessStatus.Finished);
     104            database.UpdateRunFinished(id, DateTime.Now);
     105          }
     106        }
    81107      }
    82108    }
Note: See TracChangeset for help on using the changeset viewer.