Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Hive_Management_Console_Refactoring_Ticket508/HeuristicLab.Grid/EngineStore.cs @ 1314

Last change on this file since 1314 was 506, checked in by gkronber, 16 years ago

added locking for thread-safe update of the counter variables (#197 Use SQLite backend to store waiting engines and results instead of in-memory dictionaries)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Linq;
26using System.Threading;
27using System.ServiceModel;
28using System.Data.Common;
29
30namespace HeuristicLab.Grid {
31  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
32  public class EngineStore : IEngineStore {
33    private Database database;
34
35    private static readonly string dbFile = AppDomain.CurrentDomain.BaseDirectory + "/enginestore.db3";
36    private static readonly string connectionString = "Data Source=\"" + dbFile + "\";Pooling=False";
37
38    private int waitingJobs;
39    public int WaitingJobs {
40      get {
41        return waitingJobs;
42      }
43    }
44    private int runningJobs;
45    public int RunningJobs {
46      get {
47        return runningJobs;
48      }
49    }
50    private int results;
51    public int WaitingResults {
52      get {
53        return results;
54      }
55    }
56    private object locker = new object();
57    public EngineStore() {
58      DbProviderFactory fact;
59      fact = DbProviderFactories.GetFactory("System.Data.SQLite");
60      if(!System.IO.File.Exists(dbFile)) {
61        database = new Database(connectionString);
62        database.CreateNew();
63      } else {
64        database = new Database(connectionString);
65      }
66
67      // init counters
68      waitingJobs = (int)database.GetJobCount(JobState.Waiting);
69      runningJobs = (int)database.GetJobCount(JobState.Busy);
70      results = (int)database.GetJobCount(JobState.Finished);
71    }
72
73    public bool TryTakeEngine(out Guid guid, out byte[] engine) {
74      JobEntry nextWaitingJob = database.GetNextWaitingJob();
75      if(nextWaitingJob == null) {
76        guid = Guid.Empty; engine = null;
77        return false;
78      } else {
79        guid = nextWaitingJob.Guid;
80        engine = nextWaitingJob.RawData;
81        lock(locker) {
82          runningJobs++;
83          waitingJobs--;
84        }
85        return true;
86      }
87    }
88
89    public void StoreResult(Guid guid, byte[] result) {
90      database.DeleteExpiredResults();
91      // add the new result
92      database.SetJobResult(guid, result);
93      lock(locker) {
94        results++;
95        runningJobs--;
96      }
97    }
98
99    internal void AddEngine(Guid guid, byte[] engine) {
100      database.InsertJob(guid, JobState.Waiting, engine);
101      lock(locker) {
102        waitingJobs++;
103      }
104    }
105
106    internal byte[] GetResult(Guid guid) {
107      if(GetJobState(guid) == JobState.Finished) {
108        JobEntry entry = database.GetJob(guid);
109        return entry.RawData;
110      } else {
111        // JobState is Busy, Waiting or Unknown
112        // no result yet, check for which jobs we waited too long and requeue those jobs
113        database.RestartExpiredActiveJobs();
114        return null;
115      }
116    }
117
118    internal JobState GetJobState(Guid guid) {
119      return database.GetJobState(guid);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.