Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.Hive/3.3/ConcurrentTaskDownloader.cs @ 15866

Last change on this file since 15866 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Threading;
24using System.Threading.Tasks;
25using HeuristicLab.Common;
26using HeuristicLab.Hive;
27
28namespace HeuristicLab.Clients.Hive {
29  /// <summary>
30  /// Downloads and deserializes jobs. It avoids too many jobs beeing downloaded or deserialized at the same time to avoid memory problems
31  /// </summary>
32  public class ConcurrentTaskDownloader<T> : IDisposable where T : class, ITask {
33    private bool abort = false;
34    // use semaphore to ensure only few concurrenct connections and few SerializedJob objects in memory
35    private Semaphore downloadSemaphore;
36    private Semaphore deserializeSemaphore;
37
38    public ConcurrentTaskDownloader(int concurrentDownloads, int concurrentDeserializations) {
39      downloadSemaphore = new Semaphore(concurrentDownloads, concurrentDownloads);
40      deserializeSemaphore = new Semaphore(concurrentDeserializations, concurrentDeserializations);
41    }
42
43    public void DownloadTaskData(Task t, Action<Task, T> onFinishedAction) {
44      Task<Tuple<Task, T>> task = Task<Tuple<Task, TaskData>>.Factory.StartNew(DownloadTaskData, t)
45                                     .ContinueWith((y) => DeserializeTask(y.Result));
46
47      task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);
48      task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
49    }
50
51    public void DownloadTaskDataAndTask(Guid taskId, Action<Task, T> onFinishedAction) {
52      Task<Tuple<Task, T>> task = Task<Task>.Factory.StartNew(DownloadTask, taskId)
53                                     .ContinueWith((x) => DownloadTaskData(x.Result))
54                                     .ContinueWith((y) => DeserializeTask(y.Result));
55
56      task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);
57      task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
58    }
59
60    private void OnTaskFinished(Task<Tuple<Task, T>> task, Action<Task, T> onFinishedAction) {
61      onFinishedAction(task.Result.Item1, task.Result.Item2);
62    }
63    private void OnTaskFailed(Task<Tuple<Task, T>> task) {
64      task.Exception.Flatten().Handle((e) => { return true; });
65      OnExceptionOccured(task.Exception.Flatten());
66    }
67
68    private Task DownloadTask(object taskId) {
69      Task t = null;
70      HiveClient.TryAndRepeat(() => {
71        t = HiveServiceLocator.Instance.CallHiveService(s => s.GetTask((Guid)taskId));
72      }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task.");
73      return t;
74    }
75
76    protected Tuple<Task, TaskData> DownloadTaskData(object taskId) {
77      return DownloadTaskData((Task)taskId);
78    }
79
80    protected Tuple<Task, TaskData> DownloadTaskData(Task task) {
81      downloadSemaphore.WaitOne();
82      TaskData result = null;
83      try {
84        if (abort) return null;
85        HiveClient.TryAndRepeat(() => {
86          result = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(task.Id));
87        }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task data.");
88      } finally {
89        downloadSemaphore.Release();
90      }
91      return new Tuple<Task, TaskData>(task, result);
92    }
93
94    protected Tuple<Task, T> DeserializeTask(Tuple<Task, TaskData> taskData) {
95      deserializeSemaphore.WaitOne();
96      try {
97        if (abort || taskData.Item2 == null || taskData.Item1 == null) return null;
98        var deserializedJob = PersistenceUtil.Deserialize<T>(taskData.Item2.Data);
99        taskData.Item2.Data = null; // reduce memory consumption.
100        return new Tuple<Task, T>(taskData.Item1, deserializedJob);
101      } finally {
102        deserializeSemaphore.Release();
103      }
104    }
105
106    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
107    private void OnExceptionOccured(Exception exception) {
108      var handler = ExceptionOccured;
109      if (handler != null) handler(this, new EventArgs<Exception>(exception));
110    }
111
112    #region IDisposable Members
113    public void Dispose() {
114      deserializeSemaphore.Dispose();
115      downloadSemaphore.Dispose();
116    }
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.