Free cookie consent management tool by TermsFeed Policy Generator

Changeset 502 for trunk


Ignore:
Timestamp:
08/12/08 12:07:15 (16 years ago)
Author:
gkronber
Message:

fixed a few bugs (#197 Use SQLite backend to store waiting engines and results instead of in-memory dictionaries)

Location:
trunk/sources/HeuristicLab.Grid
Files:
3 edited

Legend:

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

    r500 r502  
    4646          using(DbTransaction t = cnn.BeginTransaction()) {
    4747            using(DbCommand cmd = cnn.CreateCommand()) {
    48               cmd.CommandText = "CREATE TABLE Job (ID integer primary key autoincrement, Guid Guid, Status text, CreationTime DateTime, StartTime DateTime, RawData blob)";
     48              cmd.CommandText = "CREATE TABLE Job (ID integer primary key autoincrement, Guid text, Status text, CreationTime DateTime, StartTime DateTime, RawData blob)";
    4949              cmd.Transaction = t;
    5050              cmd.ExecuteNonQuery();
     
    7070              DbParameter guidParam = c.CreateParameter();
    7171              guidParam.ParameterName = "@Guid";
    72               guidParam.Value = guid;
     72              guidParam.Value = guid.ToString();
    7373              c.Parameters.Add(guidParam);
    7474              DbParameter statusParam = c.CreateParameter();
     
    101101    }
    102102
    103     internal List<JobEntry> GetWaitingJobs() {
    104       rwLock.EnterReadLock();
    105       List<JobEntry> jobs = new List<JobEntry>();
    106       try {
    107         using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
    108           cnn.Open();
    109           SQLiteCommand c = cnn.CreateCommand();
    110           c.CommandText = "Select Guid, CreationTime, StartTime, Rawdata from Job where Status=@Status";
    111           DbParameter statusParameter = c.CreateParameter();
    112           statusParameter.ParameterName = "@Status";
    113           statusParameter.Value = JobState.Waiting.ToString();
    114           c.Parameters.Add(statusParameter);
    115           SQLiteDataReader r = c.ExecuteReader();
    116           while(r.Read()) {
     103    internal JobEntry GetNextWaitingJob() {
     104      rwLock.EnterUpgradeableReadLock();
     105      try {
     106        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
     107          cnn.Open();
     108          using(DbTransaction t = cnn.BeginTransaction()) {
     109            DbCommand c = cnn.CreateCommand();
     110            c.Transaction = t;
     111            c.CommandText = "Select Guid, CreationTime, StartTime, Rawdata from Job where Status=@Status order by CreationTime limit 1";
     112            DbParameter statusParameter = c.CreateParameter();
     113            statusParameter.ParameterName = "@Status";
     114            statusParameter.Value = JobState.Waiting.ToString();
     115            c.Parameters.Add(statusParameter);
     116            DbDataReader r = c.ExecuteReader();
     117            if(!r.HasRows) {
     118              r.Close();
     119              t.Commit();
     120              return null;
     121            }
     122            r.Read();
    117123            JobEntry job = new JobEntry();
    118             job.Status = JobState.Waiting;
     124            job.Status = JobState.Busy;
    119125            job.Guid = r.GetGuid(0);
    120126            job.CreationTime = r.GetDateTime(1);
    121             job.StartTime = r.IsDBNull(2)?null:new Nullable<DateTime>(r.GetDateTime(2));
     127            job.StartTime = r.IsDBNull(2) ? null : new Nullable<DateTime>(r.GetDateTime(2));
    122128            job.RawData = (byte[])r.GetValue(3);
    123             jobs.Add(job);
    124           }
    125         }
    126       } finally {
    127         rwLock.ExitReadLock();
    128       }
    129       return jobs;
     129            r.Close();
     130            rwLock.EnterWriteLock();
     131            try {
     132              DbCommand updateCmd = cnn.CreateCommand();
     133              updateCmd.Transaction = t;
     134              updateCmd.CommandText = "Update job set Status=@Status where Guid=@Guid";
     135              statusParameter = updateCmd.CreateParameter();
     136              statusParameter.ParameterName = "@Status";
     137              statusParameter.Value = JobState.Busy.ToString();
     138              DbParameter guidParam = updateCmd.CreateParameter();
     139              guidParam.ParameterName = "@Guid";
     140              guidParam.Value = job.Guid.ToString();
     141              updateCmd.Parameters.Add(statusParameter);
     142              updateCmd.Parameters.Add(guidParam);
     143              updateCmd.ExecuteNonQuery();
     144            } finally {
     145              rwLock.ExitWriteLock();
     146            }
     147            t.Commit();
     148            return job;
     149          }
     150        }
     151      } finally {
     152        rwLock.ExitUpgradeableReadLock();
     153      }
    130154    }
    131155
     
    135159        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
    136160          cnn.Open();
    137           using(SQLiteTransaction t = cnn.BeginTransaction()) {
    138             using(SQLiteCommand c = cnn.CreateCommand()) {
     161          using(DbTransaction t = cnn.BeginTransaction()) {
     162            using(DbCommand c = cnn.CreateCommand()) {
    139163              c.Transaction = t;
    140164              c.CommandText = "Update Job set Status=@Status, RawData=@RawData where Guid=@Guid";
     
    145169              rawDataParam.Value = result;
    146170              guidParam.ParameterName = "@Guid";
    147               guidParam.Value = guid;
     171              guidParam.Value = guid.ToString();
    148172              statusParam.ParameterName = "@Status";
    149               statusParam.Value = JobState.Finished;
     173              statusParam.Value = JobState.Finished.ToString();
    150174              c.Parameters.Add(rawDataParam);
    151175              c.Parameters.Add(statusParam);
     
    166190        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
    167191          cnn.Open();
    168           using(SQLiteTransaction t = cnn.BeginTransaction()) {
    169             using(SQLiteCommand c = cnn.CreateCommand()) {
     192          using(DbTransaction t = cnn.BeginTransaction()) {
     193            using(DbCommand c = cnn.CreateCommand()) {
    170194              c.Transaction = t;
    171195              c.CommandText = "Update Job set Status=@Status, StartTime=@StartTime where Guid=@Guid";
     
    179203                startTimeParam.Value = null;
    180204              guidParam.ParameterName = "@Guid";
    181               guidParam.Value = guid;
     205              guidParam.Value = guid.ToString();
    182206              statusParam.ParameterName = "@Status";
    183               statusParam.Value = jobState;
     207              statusParam.Value = jobState.ToString();
    184208              c.Parameters.Add(startTimeParam);
    185209              c.Parameters.Add(statusParam);
     
    201225        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
    202226          cnn.Open();
    203           SQLiteCommand c = cnn.CreateCommand();
     227          DbCommand c = cnn.CreateCommand();
    204228          c.CommandText = "Select Status, CreationTime, StartTime, Rawdata from Job where Guid=@Guid";
    205229          DbParameter guidParameter = c.CreateParameter();
    206230          guidParameter.ParameterName = "@Guid";
    207           guidParameter.Value = guid;
     231          guidParameter.Value = guid.ToString();
    208232          c.Parameters.Add(guidParameter);
    209           SQLiteDataReader r = c.ExecuteReader();
    210           while(r.Read()) {
     233          DbDataReader r = c.ExecuteReader();
     234          if(r.HasRows) {
     235            r.Read();
    211236            JobEntry job = new JobEntry();
    212237            job.Guid = guid;
    213238            job.Status = (JobState)Enum.Parse(typeof(JobState), r.GetString(0));
    214239            job.CreationTime = r.GetDateTime(1);
    215             job.StartTime = r.IsDBNull(2)?null:new Nullable<DateTime>(r.GetDateTime(2));
     240            job.StartTime = r.IsDBNull(2) ? null : new Nullable<DateTime>(r.GetDateTime(2));
    216241            job.RawData = (byte[])r.GetValue(3);
    217242            return job;
    218243          }
     244          r.Close();
    219245        }
    220246      } finally {
     
    222248      }
    223249      return null;
     250    }
     251
     252    internal JobState GetJobState(Guid guid) {
     253      rwLock.EnterReadLock();
     254      try {
     255        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
     256          cnn.Open();
     257          DbCommand c = cnn.CreateCommand();
     258          c.CommandText = "Select Status from Job where Guid=@Guid";
     259          DbParameter guidParameter = c.CreateParameter();
     260          guidParameter.ParameterName = "@Guid";
     261          guidParameter.Value = guid.ToString();
     262          c.Parameters.Add(guidParameter);
     263          DbDataReader r = c.ExecuteReader();
     264          if(r.HasRows) {
     265            r.Read();
     266            return (JobState)Enum.Parse(typeof(JobState), r.GetString(0));
     267          }
     268          r.Close();
     269        }
     270      } finally {
     271        rwLock.ExitReadLock();
     272      }
     273      return JobState.Unknown;
    224274    }
    225275
  • trunk/sources/HeuristicLab.Grid/EngineStore.cs

    r501 r502  
    6464        database = new Database(connectionString);
    6565      }
    66       database = new Database(connectionString);
    6766    }
    6867
    69     private object t = new object();
    7068    public bool TryTakeEngine(out Guid guid, out byte[] engine) {
    71       lock(t) {
    72         List<JobEntry> waitingJobs = database.GetWaitingJobs();
    73         if(waitingJobs.Count == 0) {
    74           guid = Guid.Empty; engine = null;
    75           return false;
    76         } else {
    77           JobEntry oldestEntry = waitingJobs.OrderBy(a => a.CreationTime).First();
    78           guid = oldestEntry.Guid;
    79           engine = oldestEntry.RawData;
    80           database.UpdateJobState(guid, HeuristicLab.Grid.JobState.Busy);
    81           return true;
    82         }
     69      JobEntry nextWaitingJob = database.GetNextWaitingJob();
     70      if(nextWaitingJob == null) {
     71        guid = Guid.Empty; engine = null;
     72        return false;
     73      } else {
     74        guid = nextWaitingJob.Guid;
     75        engine = nextWaitingJob.RawData;
     76        return true;
    8377      }
    8478    }
     
    107101
    108102    internal JobState JobState(Guid guid) {
    109       return database.GetJob(guid).Status;
     103      return database.GetJobState(guid);
    110104    }
    111105  }
  • trunk/sources/HeuristicLab.Grid/JobManager.cs

    r501 r502  
    4141    private const int MAX_CONNECTION_RETRIES = 10;
    4242    private const int RETRY_TIMEOUT_SEC = 60;
    43     private const int CHECK_RESULTS_TIMEOUT = 3;
     43    private const int RESULT_POLLING_TIMEOUT = 5;
    4444
    4545    private class Job {
     
    150150      try {
    151151        while(true) {
     152          Thread.Sleep(TimeSpan.FromSeconds(RESULT_POLLING_TIMEOUT));
    152153          Job job = null;
    153154          lock(runningQueueLock) {
Note: See TracChangeset for help on using the changeset viewer.