Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/JobAdapter.cs @ 4042

Last change on this file since 4042 was 4042, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

File size: 13.6 KB
RevLine 
[971]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.Linq;
25using System.Text;
[1377]26using HeuristicLab.Hive.Server.DataAccess;
[971]27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
[1377]29using HeuristicLab.DataAccess.ADOHelper;
[1468]30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
[1580]33using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
[2117]34using System.IO;
[971]35
36namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]37  class JobAdapter :
[1468]38    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
[3011]39                      JobDto,
[1468]40                      dsHiveServer.JobRow>,
[995]41    IJobAdapter {
42    #region Fields
[1580]43    private ManyToManyRelationHelper<
44      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]45      dsHiveServer.RequiredPluginsRow> pluginsManyToManyRelationHelper = null;
[1580]46
47    private ManyToManyRelationHelper<
48      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]49      dsHiveServer.RequiredPluginsRow> PluginsManyToManyRelationHelper {
[1580]50      get {
[1947]51        if (pluginsManyToManyRelationHelper == null) {
52          pluginsManyToManyRelationHelper =
[1580]53            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1758]54              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
[1580]55        }
56
[1947]57        pluginsManyToManyRelationHelper.Session = Session as Session;
[1580]58
[1947]59        return pluginsManyToManyRelationHelper;
[1580]60      }
61    }
62
[1947]63    private ManyToManyRelationHelper<
64      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
65      dsHiveServer.AssignedResourcesRow> assignedManyToManyRelationHelper = null;
66
67    private ManyToManyRelationHelper<
68      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
69      dsHiveServer.AssignedResourcesRow> AssignedManyToManyRelationHelper {
70      get {
71        if (assignedManyToManyRelationHelper == null) {
72          assignedManyToManyRelationHelper =
73            new ManyToManyRelationHelper<dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
74              dsHiveServer.AssignedResourcesRow>(new AssignedResourcesAdapterWrapper(), 0);
75        }
76
77        assignedManyToManyRelationHelper.Session = Session as Session;
78
79        return assignedManyToManyRelationHelper;
80      }
81    }
82
[971]83    private IClientAdapter clientAdapter = null;
84
85    private IClientAdapter ClientAdapter {
86      get {
87        if (clientAdapter == null)
[1468]88          clientAdapter =
[3011]89            this.Session.GetDataAdapter<ClientDto, IClientAdapter>();
[971]90
91        return clientAdapter;
92      }
93    }
94
[1005]95    private IJobResultsAdapter resultsAdapter = null;
96
97    private IJobResultsAdapter ResultsAdapter {
98      get {
99        if (resultsAdapter == null) {
[1468]100          resultsAdapter =
101            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1005]102        }
103
104        return resultsAdapter;
105      }
106    }
[1580]107
108    private IPluginInfoAdapter pluginInfoAdapter = null;
109
110    private IPluginInfoAdapter PluginInfoAdapter {
111      get {
112        if (pluginInfoAdapter == null) {
113          pluginInfoAdapter =
[3011]114            this.Session.GetDataAdapter<HivePluginInfoDto, IPluginInfoAdapter>();
[1580]115        }
116
117        return pluginInfoAdapter;
118      }
119    }
[1947]120
121    private IProjectAdapter projectAdapter = null;
122
123    private IProjectAdapter ProjectAdapter {
124      get {
125        if (projectAdapter == null) {
126          projectAdapter =
[3011]127            this.Session.GetDataAdapter<ProjectDto, IProjectAdapter>();
[1947]128        }
129
130        return projectAdapter;
131      }
132    }
[995]133    #endregion
[971]134
[1468]135    public JobAdapter(): base(new JobAdapterWrapper()) {
136    }
137
[995]138    #region Overrides
[3011]139    protected override JobDto ConvertRow(dsHiveServer.JobRow row,
140      JobDto job) {
[971]141      if (row != null && job != null) {
[995]142        job.Id = row.JobId;
[971]143
144        if (!row.IsParentJobIdNull())
[995]145          job.ParentJob = GetById(row.ParentJobId);
[971]146        else
[975]147          job.ParentJob = null;
[971]148
149        if (!row.IsResourceIdNull())
[995]150          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]151        else
152          job.Client = null;
[1161]153
[1449]154        if (!row.IsUserIdNull())
[1377]155          job.UserId = Guid.Empty;
[1161]156        else
[1377]157          job.UserId = Guid.Empty;
[971]158       
[1092]159        if (!row.IsJobStateNull())
160          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]161        else
[995]162          job.State = State.nullState;
[971]163
[1092]164        if (!row.IsPercentageNull())
165          job.Percentage = row.Percentage;
166        else
167          job.Percentage = 0.0;
168
[1169]169        if (!row.IsDateCreatedNull())
170          job.DateCreated = row.DateCreated;
171        else
172          job.DateCreated = DateTime.MinValue;
173
174        if (!row.IsDateCalculatedNull())
175          job.DateCalculated = row.DateCalculated;
176        else
177          job.DateCalculated = DateTime.MinValue;
178
[1175]179        if (!row.IsPriorityNull())
180          job.Priority = row.Priority;
181        else
182          job.Priority = default(int);
[1169]183
[1505]184        if (!row.IsCoresNeededNull())
185          job.CoresNeeded = row.CoresNeeded;
186        else
187          job.CoresNeeded = default(int);
188
189        if (!row.IsMemoryNeededNull())
190          job.MemoryNeeded = row.MemoryNeeded;
191        else
192          job.MemoryNeeded = default(int);
193
[1947]194        if (!row.IsProjectIdNull())
195          job.Project = ProjectAdapter.GetById(
196            row.ProjectId);
197        else
198          job.Project = null;
199
[1580]200        ICollection<Guid> requiredPlugins =
[1947]201          PluginsManyToManyRelationHelper.GetRelationships(job.Id);
[1580]202       
203        job.PluginsNeeded.Clear();
204        foreach (Guid requiredPlugin in requiredPlugins) {
[3011]205          HivePluginInfoDto pluginInfo =
[1580]206            PluginInfoAdapter.GetById(requiredPlugin);
207
208          job.PluginsNeeded.Add(pluginInfo);
209        }
210
[1947]211        job.AssignedResourceIds =
212          new List<Guid>(
213            AssignedManyToManyRelationHelper.GetRelationships(job.Id));
214
[971]215        return job;
216      } else
217        return null;
218    }
219
[3011]220    protected override dsHiveServer.JobRow ConvertObj(JobDto job,
[971]221      dsHiveServer.JobRow row) {
222      if (job != null && row != null) {
[1449]223        row.JobId = job.Id;
224       
[971]225        if (job.Client != null) {
[1952]226          if (row.IsResourceIdNull()) {
[1021]227            row.ResourceId = job.Client.Id;
228          }
[971]229        } else
230          row.SetResourceIdNull();
231
[975]232        if (job.ParentJob != null) {
[1952]233          if (row.IsParentJobIdNull()) {
[1021]234            row.ParentJobId = job.ParentJob.Id;
235          }
[971]236        } else
237          row.SetParentJobIdNull();
238
[1377]239        if (job.UserId != Guid.Empty) {
[1449]240          if (row.IsUserIdNull() ||
241           row.UserId != Guid.Empty) {
242            row.UserId = Guid.Empty;
[1161]243          }
244        } else
[1449]245          row.SetUserIdNull();
[1161]246
[995]247        if (job.State != State.nullState)
[1092]248          row.JobState = job.State.ToString();
[995]249        else
[1092]250          row.SetJobStateNull();
251
[3011]252        //Todo: commout
253        /*
[1092]254        row.Percentage = job.Percentage;
[1120]255
[1169]256        if (job.DateCreated != DateTime.MinValue)
257          row.DateCreated = job.DateCreated;
258        else
259          row.SetDateCreatedNull();
260
261        if (job.DateCalculated != DateTime.MinValue)
262          row.DateCalculated = job.DateCalculated;
263        else
264          row.SetDateCalculatedNull();
265
266        row.Priority = job.Priority;
[1505]267
268        row.CoresNeeded = job.CoresNeeded;
269
[3011]270        row.MemoryNeeded = job.MemoryNeeded;*/
[1947]271
272        if (job.Project != null)
273          row.ProjectId = job.Project.Id;
274        else
275          row.SetProjectIdNull();
[971]276      }
277
278      return row;
279    }
[995]280    #endregion
281
282    #region IJobAdapter Members
[3011]283    public ICollection<JobDto> GetAllSubjobs(JobDto job) {
[971]284      if (job != null) {
[995]285        return
286          base.FindMultiple(
287            delegate() {
[1449]288              return Adapter.GetDataByParentJob(job.Id);
[995]289            });
[971]290      }
291
[995]292      return null;
[971]293    }
294
[3011]295    public ICollection<JobDto> GetJobsByState(State state) {
[998]296      return
297         base.FindMultiple(
298           delegate() {
[1131]299             return Adapter.GetDataByState(state.ToString());
[998]300           });
301    }
302
[3011]303    public ICollection<JobDto> GetJobsOf(ClientDto client) {
[971]304      if (client != null) {
[995]305        return
306          base.FindMultiple(
307            delegate() {
[1131]308              return Adapter.GetDataByClient(client.Id);
[995]309            });
[971]310      }
311
[995]312      return null;
[971]313    }
314
[3011]315    public ICollection<JobDto> GetActiveJobsOf(ClientDto client) {
[1166]316
317      if (client != null) {
318        return
319          base.FindMultiple(
320            delegate() {
321              return Adapter.GetDataByCalculatingClient(client.Id);
322            });
323      }
324
325      return null;
326    }
327
[3011]328    public ICollection<JobDto> GetJobsOf(Guid userId) {     
[1377]329      return
[995]330          base.FindMultiple(
331            delegate() {
[1449]332              return Adapter.GetDataByUser(userId);
[995]333            });
[971]334    }
[1005]335
[3011]336    public ICollection<JobDto> FindJobs(State state, int cores, int memory,
[2066]337      Guid resourceId) {
[1508]338      return
339         base.FindMultiple(
340           delegate() {
[2066]341             return Adapter.GetDataByStateCoresMemoryResource(
342               state.ToString(),
343               cores,
344               memory,
345               resourceId);
[1508]346           });
347    }
348
[3011]349    public ICollection<JobDto> GetJobsByProject(Guid projectId) {
[1947]350      return
351         base.FindMultiple(
352           delegate() {
353             return Adapter.GetDataByProjectId(projectId);
354           });
[1944]355    }
356
[3011]357    protected override void doUpdate(JobDto obj) {
[1955]358      if (obj != null) {
359        ProjectAdapter.Update(obj.Project);
360        ClientAdapter.Update(obj.Client);
361        Update(obj.ParentJob);
[1952]362
[1955]363        base.doUpdate(obj);
[1580]364
[1955]365        //update relationships
366        List<Guid> relationships =
367          new List<Guid>();
[3011]368        foreach (HivePluginInfoDto pluginInfo in obj.PluginsNeeded) {
[1955]369          //first check if pluginInfo already exists in the db
[3011]370          HivePluginInfoDto found = PluginInfoAdapter.GetByNameVersionBuilddate(
[4042]371            pluginInfo.Name, pluginInfo.Version.ToString(), pluginInfo.BuildDate);
[1955]372          if (found != null) {
373            pluginInfo.Id = found.Id;
374          }
375
376          PluginInfoAdapter.Update(pluginInfo);
377          relationships.Add(pluginInfo.Id);
[1592]378        }
379
[1955]380        PluginsManyToManyRelationHelper.UpdateRelationships(
381          obj.Id, relationships);
382
383        AssignedManyToManyRelationHelper.UpdateRelationships(
384          obj.Id, obj.AssignedResourceIds);
[1580]385      }
386    }
387
[3011]388    protected override bool doDelete(JobDto job) {
[1005]389      if (job != null) {
390        dsHiveServer.JobRow row =
391          GetRowById(job.Id);
392
393        if (row != null) {
394          //Referential integrity with job results
395          ICollection<JobResult> results =
[2099]396            ResultsAdapter.GetResultsOf(job.Id);
[1005]397
398          foreach (JobResult result in results) {
399            ResultsAdapter.Delete(result);
400          }
401
[1580]402          //delete all relationships
[1947]403          PluginsManyToManyRelationHelper.UpdateRelationships(job.Id,
[1759]404            new List<Guid>());
[1580]405
[1592]406          //delete orphaned pluginInfos
[3011]407          ICollection<HivePluginInfoDto> orphanedPluginInfos =
[1592]408             PluginInfoAdapter.GetOrphanedPluginInfos();
[3011]409          foreach(HivePluginInfoDto orphanedPlugin in orphanedPluginInfos) {
[1592]410            PluginInfoAdapter.Delete(orphanedPlugin);
411          }
412
[1468]413          return base.doDelete(job);
[1005]414        }
415      }
416
417      return false;
418    }
[1944]419
[2082]420    /// <summary>
421    /// Gets the computable job with the secified jobId
422    /// </summary>
423    /// <param name="jobId"></param>
424    /// <returns></returns>
[2099]425    public SerializedJob GetSerializedJob(Guid jobId) {
[2092]426      return (SerializedJob)base.doInTransaction(
[2082]427        delegate() {
[2092]428          SerializedJob job =
429            new SerializedJob();
[2082]430
431          job.JobInfo = GetById(jobId);
[2083]432          if (job.JobInfo != null) {
[2092]433            job.SerializedJobData =
[2083]434              base.Adapter.GetSerializedJobById(jobId);
[2082]435
436            return job;
437          } else {
438            return null;
439          }
440        });
441    }
442
[2117]443    public Stream GetSerializedJobStream(Guid jobId, bool useExistingConnection) {
444      return
445        ((JobAdapterWrapper)base.DataAdapterWrapper).
446          GetSerializedJobStream(jobId, useExistingConnection);
447    }
448
[2082]449    /// <summary>
450    /// Saves or update the computable job
451    /// </summary>
452    /// <param name="jobId"></param>
[2099]453    public void UpdateSerializedJob(SerializedJob job) {
[2082]454      if(job != null &&
455        job.JobInfo != null) {
456        base.doInTransaction(
457          delegate() {
[2087]458             Update(job.JobInfo);
459             return base.Adapter.UpdateSerializedJob(
[2092]460               job.SerializedJobData, job.JobInfo.Id);
[2082]461          });
462      }
463     
464    }
465
[971]466    #endregion
467  }
468}
Note: See TracBrowser for help on using the repository browser.