[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>
|
---|
[9219] | 32 | public class ConcurrentTaskDownloader<T> : IDisposable where T : class, ITask {
|
---|
[6976] | 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 |
|
---|
[7115] | 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, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
|
---|
[6976] | 49 | }
|
---|
| 50 |
|
---|
[7115] | 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, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
|
---|
[6976] | 58 | }
|
---|
[7115] | 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, Action<Task, T> onFinishedAction) {
|
---|
[6976] | 64 | task.Exception.Flatten().Handle((e) => { return true; });
|
---|
| 65 | OnExceptionOccured(task.Exception.Flatten());
|
---|
[7115] | 66 | onFinishedAction(task.Result.Item1, null);
|
---|
[6976] | 67 | }
|
---|
| 68 |
|
---|
[7115] | 69 | private Task DownloadTask(object taskId) {
|
---|
[9219] | 70 | Task t = null;
|
---|
| 71 | HiveClient.TryAndRepeat(() => {
|
---|
| 72 | t = HiveServiceLocator.Instance.CallHiveService(s => s.GetTask((Guid)taskId));
|
---|
| 73 | }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task.");
|
---|
| 74 | return t;
|
---|
[7115] | 75 | }
|
---|
| 76 |
|
---|
| 77 | protected Tuple<Task, TaskData> DownloadTaskData(object taskId) {
|
---|
| 78 | return DownloadTaskData((Task)taskId);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | protected Tuple<Task, TaskData> DownloadTaskData(Task task) {
|
---|
[6976] | 82 | downloadSemaphore.WaitOne();
|
---|
[9219] | 83 | TaskData result = null;
|
---|
[6976] | 84 | try {
|
---|
| 85 | if (abort) return null;
|
---|
[9219] | 86 | HiveClient.TryAndRepeat(() => {
|
---|
| 87 | result = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(task.Id));
|
---|
| 88 | }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task data.");
|
---|
| 89 | }
|
---|
| 90 | finally {
|
---|
[6976] | 91 | downloadSemaphore.Release();
|
---|
| 92 | }
|
---|
[7115] | 93 | return new Tuple<Task, TaskData>(task, result);
|
---|
[6976] | 94 | }
|
---|
| 95 |
|
---|
[7115] | 96 | protected Tuple<Task, T> DeserializeTask(Tuple<Task, TaskData> taskData) {
|
---|
[7125] | 97 | deserializeSemaphore.WaitOne();
|
---|
[6976] | 98 | try {
|
---|
[7115] | 99 | if (abort || taskData.Item2 == null || taskData.Item1 == null) return null;
|
---|
| 100 | var deserializedJob = PersistenceUtil.Deserialize<T>(taskData.Item2.Data);
|
---|
| 101 | taskData.Item2.Data = null; // reduce memory consumption.
|
---|
| 102 | return new Tuple<Task, T>(taskData.Item1, deserializedJob);
|
---|
[9219] | 103 | }
|
---|
| 104 | finally {
|
---|
[6976] | 105 | deserializeSemaphore.Release();
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 110 | private void OnExceptionOccured(Exception exception) {
|
---|
| 111 | var handler = ExceptionOccured;
|
---|
| 112 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 113 | }
|
---|
[9219] | 114 |
|
---|
| 115 | #region IDisposable Members
|
---|
| 116 | public void Dispose() {
|
---|
| 117 | deserializeSemaphore.Dispose();
|
---|
| 118 | downloadSemaphore.Dispose();
|
---|
| 119 | }
|
---|
| 120 | #endregion
|
---|
[6976] | 121 | }
|
---|
| 122 | }
|
---|