- Timestamp:
- 08/12/08 12:07:15 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Grid
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Grid/Database.cs
r500 r502 46 46 using(DbTransaction t = cnn.BeginTransaction()) { 47 47 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)"; 49 49 cmd.Transaction = t; 50 50 cmd.ExecuteNonQuery(); … … 70 70 DbParameter guidParam = c.CreateParameter(); 71 71 guidParam.ParameterName = "@Guid"; 72 guidParam.Value = guid ;72 guidParam.Value = guid.ToString(); 73 73 c.Parameters.Add(guidParam); 74 74 DbParameter statusParam = c.CreateParameter(); … … 101 101 } 102 102 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(); 117 123 JobEntry job = new JobEntry(); 118 job.Status = JobState. Waiting;124 job.Status = JobState.Busy; 119 125 job.Guid = r.GetGuid(0); 120 126 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)); 122 128 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 } 130 154 } 131 155 … … 135 159 using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) { 136 160 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()) { 139 163 c.Transaction = t; 140 164 c.CommandText = "Update Job set Status=@Status, RawData=@RawData where Guid=@Guid"; … … 145 169 rawDataParam.Value = result; 146 170 guidParam.ParameterName = "@Guid"; 147 guidParam.Value = guid ;171 guidParam.Value = guid.ToString(); 148 172 statusParam.ParameterName = "@Status"; 149 statusParam.Value = JobState.Finished ;173 statusParam.Value = JobState.Finished.ToString(); 150 174 c.Parameters.Add(rawDataParam); 151 175 c.Parameters.Add(statusParam); … … 166 190 using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) { 167 191 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()) { 170 194 c.Transaction = t; 171 195 c.CommandText = "Update Job set Status=@Status, StartTime=@StartTime where Guid=@Guid"; … … 179 203 startTimeParam.Value = null; 180 204 guidParam.ParameterName = "@Guid"; 181 guidParam.Value = guid ;205 guidParam.Value = guid.ToString(); 182 206 statusParam.ParameterName = "@Status"; 183 statusParam.Value = jobState ;207 statusParam.Value = jobState.ToString(); 184 208 c.Parameters.Add(startTimeParam); 185 209 c.Parameters.Add(statusParam); … … 201 225 using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) { 202 226 cnn.Open(); 203 SQLiteCommand c = cnn.CreateCommand();227 DbCommand c = cnn.CreateCommand(); 204 228 c.CommandText = "Select Status, CreationTime, StartTime, Rawdata from Job where Guid=@Guid"; 205 229 DbParameter guidParameter = c.CreateParameter(); 206 230 guidParameter.ParameterName = "@Guid"; 207 guidParameter.Value = guid ;231 guidParameter.Value = guid.ToString(); 208 232 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(); 211 236 JobEntry job = new JobEntry(); 212 237 job.Guid = guid; 213 238 job.Status = (JobState)Enum.Parse(typeof(JobState), r.GetString(0)); 214 239 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)); 216 241 job.RawData = (byte[])r.GetValue(3); 217 242 return job; 218 243 } 244 r.Close(); 219 245 } 220 246 } finally { … … 222 248 } 223 249 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; 224 274 } 225 275 -
trunk/sources/HeuristicLab.Grid/EngineStore.cs
r501 r502 64 64 database = new Database(connectionString); 65 65 } 66 database = new Database(connectionString);67 66 } 68 67 69 private object t = new object();70 68 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; 83 77 } 84 78 } … … 107 101 108 102 internal JobState JobState(Guid guid) { 109 return database.GetJob (guid).Status;103 return database.GetJobState(guid); 110 104 } 111 105 } -
trunk/sources/HeuristicLab.Grid/JobManager.cs
r501 r502 41 41 private const int MAX_CONNECTION_RETRIES = 10; 42 42 private const int RETRY_TIMEOUT_SEC = 60; 43 private const int CHECK_RESULTS_TIMEOUT = 3;43 private const int RESULT_POLLING_TIMEOUT = 5; 44 44 45 45 private class Job { … … 150 150 try { 151 151 while(true) { 152 Thread.Sleep(TimeSpan.FromSeconds(RESULT_POLLING_TIMEOUT)); 152 153 Job job = null; 153 154 lock(runningQueueLock) {
Note: See TracChangeset
for help on using the changeset viewer.