[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Threading;
|
---|
| 24 | using System.Threading.Tasks;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Hive;
|
---|
| 27 |
|
---|
| 28 | namespace 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> 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 | TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[7115] | 44 | public void DownloadTaskData(Task t, Action<Task, T> onFinishedAction) {
|
---|
| 45 | Task<Tuple<Task, T>> task = Task<Tuple<Task, TaskData>>.Factory.StartNew(DownloadTaskData, t)
|
---|
| 46 | .ContinueWith((y) => DeserializeTask(y.Result));
|
---|
| 47 |
|
---|
| 48 | task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 49 | task.ContinueWith((x) => OnTaskFailed(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
|
---|
[6976] | 50 | }
|
---|
| 51 |
|
---|
[7115] | 52 | public void DownloadTaskDataAndTask(Guid taskId, Action<Task, T> onFinishedAction) {
|
---|
| 53 | Task<Tuple<Task, T>> task = Task<Task>.Factory.StartNew(DownloadTask, taskId)
|
---|
| 54 | .ContinueWith((x) => DownloadTaskData(x.Result))
|
---|
| 55 | .ContinueWith((y) => DeserializeTask(y.Result));
|
---|
| 56 |
|
---|
| 57 | task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);
|
---|
| 58 | task.ContinueWith((x) => OnTaskFailed(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
|
---|
[6976] | 59 | }
|
---|
[7115] | 60 |
|
---|
| 61 | private void OnTaskFinished(Task<Tuple<Task, T>> task, Action<Task, T> onFinishedAction) {
|
---|
| 62 | onFinishedAction(task.Result.Item1, task.Result.Item2);
|
---|
| 63 | }
|
---|
| 64 | private void OnTaskFailed(Task<Tuple<Task, T>> task, Action<Task, T> onFinishedAction) {
|
---|
[6976] | 65 | task.Exception.Flatten().Handle((e) => { return true; });
|
---|
| 66 | OnExceptionOccured(task.Exception.Flatten());
|
---|
[7115] | 67 | onFinishedAction(task.Result.Item1, null);
|
---|
[6976] | 68 | }
|
---|
| 69 |
|
---|
[7115] | 70 | private Task DownloadTask(object taskId) {
|
---|
[7132] | 71 | return HiveServiceLocator.Instance.CallHiveService(s => s.GetTask((Guid)taskId));
|
---|
[7115] | 72 | }
|
---|
| 73 |
|
---|
| 74 | protected Tuple<Task, TaskData> DownloadTaskData(object taskId) {
|
---|
| 75 | return DownloadTaskData((Task)taskId);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected Tuple<Task, TaskData> DownloadTaskData(Task task) {
|
---|
[6976] | 79 | downloadSemaphore.WaitOne();
|
---|
| 80 | TaskData result;
|
---|
| 81 | try {
|
---|
| 82 | if (abort) return null;
|
---|
[7132] | 83 | result = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(task.Id));
|
---|
[7115] | 84 | } finally {
|
---|
[6976] | 85 | downloadSemaphore.Release();
|
---|
| 86 | }
|
---|
[7115] | 87 | return new Tuple<Task, TaskData>(task, result);
|
---|
[6976] | 88 | }
|
---|
| 89 |
|
---|
[7115] | 90 | protected Tuple<Task, T> DeserializeTask(Tuple<Task, TaskData> taskData) {
|
---|
[7125] | 91 | deserializeSemaphore.WaitOne();
|
---|
[6976] | 92 | try {
|
---|
[7115] | 93 | if (abort || taskData.Item2 == null || taskData.Item1 == null) return null;
|
---|
| 94 | var deserializedJob = PersistenceUtil.Deserialize<T>(taskData.Item2.Data);
|
---|
| 95 | taskData.Item2.Data = null; // reduce memory consumption.
|
---|
| 96 | return new Tuple<Task, T>(taskData.Item1, deserializedJob);
|
---|
| 97 | } finally {
|
---|
[6976] | 98 | deserializeSemaphore.Release();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) {
|
---|
| 103 | e.SetObserved(); // avoid crash of process because task crashes. first exception found is handled in Results property
|
---|
| 104 | OnExceptionOccured(new HiveException("Unobserved Exception in ConcurrentTaskDownloader", e.Exception));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 108 | private void OnExceptionOccured(Exception exception) {
|
---|
| 109 | var handler = ExceptionOccured;
|
---|
| 110 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|