Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/12/11 18:04:25 (13 years ago)
Author:
ascheibe
Message:

#1233

  • fixed a bug in the Slave UI
  • finished renaming Webservice and Dao methods to be consistent with Job/Task naming
  • some cosmetic changes and project dependencies cleanups
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs

    r6725 r6743  
    3636    public HiveDao() { }
    3737
    38     #region Job Methods
    39     public DT.Task GetJob(Guid id) {
     38    #region Task Methods
     39    public DT.Task GetTask(Guid id) {
    4040      using (var db = CreateContext()) {
    4141        return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id));
     
    4343    }
    4444
    45     public IEnumerable<DT.Task> GetJobs(Expression<Func<Task, bool>> predicate) {
     45    public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
    4646      using (var db = CreateContext()) {
    4747        return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
     
    4949    }
    5050
    51     public Guid AddJob(DT.Task dto) {
     51    public Guid AddTask(DT.Task dto) {
    5252      using (var db = CreateContext()) {
    5353        var entity = DT.Convert.ToEntity(dto);
     
    6262    }
    6363
    64     public void UpdateJob(DT.Task dto) {
     64    public void UpdateTask(DT.Task dto) {
    6565      using (var db = CreateContext()) {
    6666        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
     
    7676    }
    7777
    78     public void DeleteJob(Guid id) {
     78    public void DeleteTask(Guid id) {
    7979      using (var db = CreateContext()) {
    8080        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id);
    8181        if (entity != null) db.Tasks.DeleteOnSubmit(entity);
    82         db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
     82        db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger
    8383      }
    8484    }
    8585
    8686    /// <summary>
    87     /// returns all parent jobs which are waiting for their child jobs to finish
     87    /// returns all parent tasks which are waiting for their child tasks to finish
    8888    /// </summary>
    89     /// <param name="resourceIds">list of resourceids which for which the jobs should be valid</param>
    90     /// <param name="count">maximum number of jobs to return</param>
    91     /// <param name="finished">if true, all parent jobs which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
     89    /// <param name="resourceIds">list of resourceids which for which the task should be valid</param>
     90    /// <param name="count">maximum number of task to return</param>
     91    /// <param name="finished">if true, all parent task which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
    9292    /// <returns></returns>
    93     public IEnumerable<DT.Task> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished) {
     93    public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
    9494      using (var db = CreateContext()) {
    9595        var query = from ar in db.AssignedResources
     
    103103                               || child.State == TaskState.Aborted
    104104                               || child.State == TaskState.Failed).All(x => x)
    105                        && (from child in db.Tasks // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
     105                       && (from child in db.Tasks // avoid returning WaitForChildTasks task where no child-task exist (yet)
    106106                           where child.ParentTaskId == ar.Task.TaskId
    107107                           select child).Count() > 0
     
    112112    }
    113113
    114     public IEnumerable<DT.Task> GetWaitingJobs(DT.Slave slave, int count) {
     114    public IEnumerable<DT.Task> GetWaitingTasks(DT.Slave slave, int count) {
    115115      using (var db = CreateContext()) {
    116116        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
    117         var waitingParentJobs = GetParentJobs(resourceIds, count, false);
     117        var waitingParentJobs = GetParentTasks(resourceIds, count, false);
    118118        if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
    119119
     
    124124                       && ar.Task.CoresNeeded <= slave.FreeCores
    125125                       && ar.Task.MemoryNeeded <= slave.FreeMemory
    126                     orderby ar.Task.Priority descending, db.Random() // take random job to avoid the race condition that occurs when this method is called concurrently (the same job would be returned)
     126                    orderby ar.Task.Priority descending, db.Random() // take random task to avoid the race condition that occurs when this method is called concurrently (the same task would be returned)
    127127                    select DT.Convert.ToDto(ar.Task);
    128         var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
    129         return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
    130       }
    131     }
    132 
    133     public DT.Task UpdateJobState(Guid jobId, TaskState jobState, Guid? slaveId, Guid? userId, string exception) {
    134       using (var db = CreateContext()) {
    135         var job = db.Tasks.SingleOrDefault(x => x.TaskId == jobId);
    136         job.State = jobState;
     128        var waitingTasks = (count == 0 ? query : query.Take(count)).ToArray();
     129        return waitingTasks.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
     130      }
     131    }
     132
     133    public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
     134      using (var db = CreateContext()) {
     135        var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
     136        job.State = taskState;
    137137        db.StateLogs.InsertOnSubmit(new StateLog {
    138           TaskId = jobId,
    139           State = jobState,
     138          TaskId = taskId,
     139          State = taskState,
    140140          SlaveId = slaveId,
    141141          UserId = userId,
     
    144144        });
    145145        db.SubmitChanges();
    146         job = db.Tasks.SingleOrDefault(x => x.TaskId == jobId);
     146        job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
    147147        return DT.Convert.ToDto(job);
    148148      }
     
    151151
    152152    #region TaskData Methods
    153     public DT.TaskData GetJobData(Guid id) {
     153    public DT.TaskData GetTaskData(Guid id) {
    154154      using (var db = CreateContext(true)) {
    155155        return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id));
     
    157157    }
    158158
    159     public IEnumerable<DT.TaskData> GetJobDatas(Expression<Func<TaskData, bool>> predicate) {
     159    public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) {
    160160      using (var db = CreateContext(true)) {
    161161        return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
     
    163163    }
    164164
    165     public Guid AddJobData(DT.TaskData dto) {
     165    public Guid AddTaskData(DT.TaskData dto) {
    166166      using (var db = CreateContext(true)) {
    167167        var entity = DT.Convert.ToEntity(dto);
     
    172172    }
    173173
    174     public void UpdateJobData(DT.TaskData dto) {
     174    public void UpdateTaskData(DT.TaskData dto) {
    175175      using (var db = CreateContext(true)) {
    176176        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId);
     
    181181    }
    182182
    183     public void DeleteJobData(Guid id) {
     183    public void DeleteTaskData(Guid id) {
    184184      using (var db = CreateContext()) {
    185185        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
     
    231231
    232232    #region Job Methods
    233     public DT.Job GetHiveExperiment(Guid id) {
     233    public DT.Job GetJob(Guid id) {
    234234      using (var db = CreateContext()) {
    235235        return AddStatsToExperiment(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id)));
     
    248248    }
    249249
    250     public IEnumerable<DT.Job> GetHiveExperiments(Expression<Func<Job, bool>> predicate) {
     250    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
    251251      using (var db = CreateContext()) {
    252252        return db.Jobs.Where(predicate).Select(x => AddStatsToExperiment(db, DT.Convert.ToDto(x))).ToArray();
     
    254254    }
    255255
    256     public Guid AddHiveExperiment(DT.Job dto) {
     256    public Guid AddJob(DT.Job dto) {
    257257      using (var db = CreateContext()) {
    258258        var entity = DT.Convert.ToEntity(dto);
     
    263263    }
    264264
    265     public void UpdateHiveExperiment(DT.Job dto) {
     265    public void UpdateJob(DT.Job dto) {
    266266      using (var db = CreateContext()) {
    267267        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
     
    272272    }
    273273
    274     public void DeleteHiveExperiment(Guid id) {
     274    public void DeleteJob(Guid id) {
    275275      using (var db = CreateContext()) {
    276276        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
     
    282282
    283283    #region JobPermission Methods
    284     public DT.JobPermission GetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
    285       using (var db = CreateContext()) {
    286         return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId));
    287       }
    288     }
    289 
    290     public IEnumerable<DT.JobPermission> GetHiveExperimentPermissions(Expression<Func<JobPermission, bool>> predicate) {
     284    public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) {
     285      using (var db = CreateContext()) {
     286        return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId));
     287      }
     288    }
     289
     290    public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) {
    291291      using (var db = CreateContext()) {
    292292        return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
     
    294294    }
    295295
    296     public void AddHiveExperimentPermission(DT.JobPermission dto) {
     296    public void AddJobPermission(DT.JobPermission dto) {
    297297      using (var db = CreateContext()) {
    298298        var entity = DT.Convert.ToEntity(dto);
     
    302302    }
    303303
    304     public void UpdateHiveExperimentPermission(DT.JobPermission dto) {
     304    public void UpdateJobPermission(DT.JobPermission dto) {
    305305      using (var db = CreateContext()) {
    306306        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId);
     
    311311    }
    312312
    313     public void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
    314       using (var db = CreateContext()) {
    315         var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId);
     313    public void DeleteJobPermission(Guid jobId, Guid grantedUserId) {
     314      using (var db = CreateContext()) {
     315        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
    316316        if (entity != null) db.JobPermissions.DeleteOnSubmit(entity);
    317317        db.SubmitChanges();
     
    322322    /// Sets the permissions for a experiment. makes sure that only one permission per user exists.
    323323    /// </summary>
    324     public void SetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
    325       using (var db = CreateContext()) {
    326         JobPermission hiveExperimentPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId);
    327         if (hiveExperimentPermission != null) {
     324    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
     325      using (var db = CreateContext()) {
     326        JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
     327        if (jobPermission != null) {
    328328          if (permission == Permission.NotAllowed) {
    329329            // not allowed, delete
    330             db.JobPermissions.DeleteOnSubmit(hiveExperimentPermission);
     330            db.JobPermissions.DeleteOnSubmit(jobPermission);
    331331          } else {
    332332            // update
    333             hiveExperimentPermission.Permission = permission;
    334             hiveExperimentPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
     333            jobPermission.Permission = permission;
     334            jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
    335335          }
    336336        } else {
    337337          // insert
    338338          if (permission != Permission.NotAllowed) {
    339             hiveExperimentPermission = new JobPermission() { JobId = hiveExperimentId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
    340             db.JobPermissions.InsertOnSubmit(hiveExperimentPermission);
     339            jobPermission = new JobPermission() { JobId = jobId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
     340            db.JobPermissions.InsertOnSubmit(jobPermission);
    341341          }
    342342        }
     
    613613
    614614    #region Authorization Methods
     615    public Permission GetPermissionForTask(Guid taskId, Guid userId) {
     616      using (var db = CreateContext()) {
     617        return GetPermissionForJob(GetJobForTask(taskId), userId);
     618      }
     619    }
     620
    615621    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
    616622      using (var db = CreateContext()) {
    617         return GetPermissionForExperiment(GetExperimentForJob(jobId), userId);
    618       }
    619     }
    620 
    621     public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
    622       using (var db = CreateContext()) {
    623         Job hiveExperiment = db.Jobs.SingleOrDefault(x => x.JobId == experimentId);
    624         if (hiveExperiment == null) return Permission.NotAllowed;
    625         if (hiveExperiment.OwnerUserId == userId) return Permission.Full;
    626         JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == experimentId && p.GrantedUserId == userId);
     623        Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
     624        if (job == null) return Permission.NotAllowed;
     625        if (job.OwnerUserId == userId) return Permission.Full;
     626        JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
    627627        return permission != null ? permission.Permission : Permission.NotAllowed;
    628628      }
    629629    }
    630630
    631     public Guid GetExperimentForJob(Guid jobId) {
    632       using (var db = CreateContext()) {
    633         return db.Tasks.Single(j => j.TaskId == jobId).JobId;
     631    public Guid GetJobForTask(Guid taskId) {
     632      using (var db = CreateContext()) {
     633        return db.Tasks.Single(j => j.TaskId == taskId).JobId;
    634634      }
    635635    }
     
    765765        }
    766766
    767         // execution times only of finished jobs - necessary to compute efficieny
     767        // execution times only of finished task - necessary to compute efficieny
    768768        var executionTimesFinishedJobs = from job in db.Tasks
    769769                                         where job.State == TaskState.Finished
     
    778778        }
    779779
    780         // start to end times only of finished jobs - necessary to compute efficiency
     780        // start to end times only of finished task - necessary to compute efficiency
    781781        var startToEndTimesFinishedJobs = from job in db.Tasks
    782782                                          where job.State == TaskState.Finished
     
    817817
    818818    #region Helpers
    819     private void CollectChildJobs(HiveDataContext db, Guid parentJobId, List<Task> collection) {
    820       var jobs = db.Tasks.Where(j => j.ParentTaskId == parentJobId);
    821       foreach (var job in jobs) {
    822         collection.Add(job);
    823         if (job.IsParentTask)
    824           CollectChildJobs(db, job.TaskId, collection);
     819    private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) {
     820      var tasks = db.Tasks.Where(j => j.ParentTaskId == parentTaskId);
     821      foreach (var task in tasks) {
     822        collection.Add(task);
     823        if (task.IsParentTask)
     824          CollectChildTasks(db, task.TaskId, collection);
    825825      }
    826826    }
Note: See TracChangeset for help on using the changeset viewer.